<?php

function adListSubCats($ParentId, $template, $KeywordsVar = 'SearchWord')
{
    global $var_list, $m_cat_filter, $m_category_sort, $m_category_sortby,
           $objCatList, $objSession,$pathtoroot, $objConfig,$admin;

    if($objCatList->NumCategories()==0)
        return "<br>No Categories<br><br>";

   	$o="<table border=0 cellspacing=2 width=\"100%\"><tbody><tr>";

	$i=1;
    $list = $objSession->GetVariable($KeywordsVar);
    if(strlen($list))
        $Keywords = explode(",",$list);

    $template = $pathtoroot.$admin."/templates/".$template;
    
    $topleft   = 0;
    $topright  = 0;
    $rightcount = 0;
    $totalcats  = 0;
    $totalcats   =   $objCatList->NumItems();
    $topleft   =  ceil($totalcats/2);
    $topright  =  $totalcats-$topleft;
  
    for ($x=0;$x<$topleft;$x++) 
    {
        //printingleft
        $cat = $objCatList->Items[$x];

        if($cat->Get("CategoryId")!=$ParentId)
        {      
            if ($i > 2)
            {	
                $o.="</tr>\n<tr>";
                $i = 1;
            }

            //$cat->LoadPermissions($objSession->Get("GroupId"));
            if(strlen($list))
            {
                $h = $cat->AdminParseTemplate($template);
                $h = HighlightKeywords($Keywords, $h);
                $o .= $h;
                $h = "";
            }
            else
                $o.=$cat->AdminParseTemplate($template);
            
            $i++;
        }
        //printingright

        if ($rightcount < $topright && (($x+$topleft) <$totalcats)) 
        {
            $cat = $objCatList->Items[$x+$topleft];   
            if($cat->Get("CategoryId")!=$ParentId)
            {  
                if ($i > 2)
                {	
                    $o.="</tr>\n<tr>";
                    $i = 1;
                }
                //$cat->LoadPermissions($objSession->Get("GroupId"));
                if(strlen($list))
                {
                    $h = $cat->AdminParseTemplate($template);
                    $h = HighlightKeywords($Keywords, $h);
                    $o .= $h;
                    $h = "";
                }
                else
                    $o.=$cat->AdminParseTemplate($template);

                $i++;
            }
            $rightcount++;
        }
    }
	$o .="\n</tr></tbody></table>\n";   
	return $o;
}

function list_custom($c,$Type)
{
	global $objSession, $objCustomFieldList, $objCustomDataList,
           $imagesURL;

	$field_list = $objCustomFieldList->Query_CustomField("Type=$Type");

	if (count($field_list) == 0)
		return "No Custom Fields<br><br>";

	$o="";

	$i = 1;
	$objCustomDataList->SourceTable = $objSession->GetEditTable("CustomMetaData");
	$objCustomDataList->LoadResource($c->Get("ResourceId"));
	foreach($field_list as $field)
	{
		$fieldname = $field->Get("FieldName");
        $fieldlabel = $field->Get("FieldLabel");
		$fieldid = $field->Get("CustomFieldId");

		$f = $objCustomDataList->GetDataItem($fieldid);
		if(is_object($f))
		{
		  $fieldvalue = $f->Get("Value");
		}
		else
		  $fieldvalue="";
		$o .="<tr " . int_table_color_ret() .">";
		$o .="<td valign=\"top\"><img src=\"$imagesURL/itemicons/icon16_custom.gif\" height=\"16\" width=\"16\" align=\"absmiddle\"><span class=\"text\"> $fieldname</span></td>
              <td valign=\"top\"><span class=\"text\">$fieldlabel</span></td> 
			<td>
				<input type=\"text\" name=\"customdata[$fieldid]\" class=\"text\" size=\"30\" value=\"$fieldvalue\">
			</td></tr>\n";

		$i++;
	}
	return $o;
}


function adListRelatedCategories($Item)
{
  global $objCatList;

  if(is_object($Item))
  {  
    $catlist = $Item->GetRelatedCategories(0);
    $o = "";
    foreach($catlist as $catrel)
    {
      $o .= "<input type=checkbox NAME=\"dellist[]\" VALUE=\"".$catrel->Get("RelationshipId")."\">";
      $cat = $objCatList->GetByResource($catrel->Get("TargetId"));
      $path = $cat->ParentNames();
      if(strlen($path))
      {      
        $o .= implode(">",$path);      
      }
      $o .="<br>\n";
    }
  }
  return $o;
}

function adListRelatedItems($Item)
{
  global $objCatList;

  if(is_object($Item))
  {  
    $item_list=$Item->GetRelatedItems(0);
    $o = "";
    foreach($item_list as $i)
    {   
      $data = $i->GetTargetItemData();
      $o .= "<tr ".int_table_color_ret().">";      
      $o .= "<TD>";      
      $o .= "<input type=checkbox NAME=\"dellist[]\" VALUE=\"".$i->Get("RelationshipId")."\">";
      $o .= $data[$data["TitleField"]];
      $o .= "</TD>";
      $o .= "<TD>".$data["SourceTable"]."</TD>";
      $cat =& $objCatList->GetCategory($data["CategoryId"]);
      $o .= "<TD>";
      $path = $cat->ParentNames();
      if(strlen($path))
      {      
        $o .= implode(">",$path);      
      }
      $o .= "</TD></TR>";
    }
  }
  return $o;
}

function adListItemReviews($Item)
{
    global $pathtoroot,$admin;

    $Reviews = $Item->GetItemReviews();
    $o = "";
    if($Reviews->ItemCount()>0)
    {
        foreach($Reviews->ItemList as $r)
        {
            $o .= "<TR ".int_table_color_ret().">";
            $o .= "<TD width=\"5%\">";
            if($r->Get("Pending")==0)
            {
                $o .= "<INPUT TYPE=checkbox NAME=\"deletelist[]\" VALUE=\"".$r->Get("ReviewId")."\"> Delete";
            }
            $o .= "</TD><TD width=\"5%\">";
            if($r->Get("Pending")!=0)
            {
                $o .= "<INPUT TYPE=checkbox NAME=\"approvelist[]\" VALUE=\"".$r->Get("ReviewId")."\"> Approve";
            }
            $o .= "</TD>";
            $o .= $r->parse_template(admintemplate($pathtoroot.$admin."/templates/review_element.tpl"));
            $o .= "</TR>";
        }
    }
    return $o;
}


function adImageUploadFormTags($img)
{
    static $file_count=0;
        
    $o = "";
    if($file_count==0)
    {
        $o .= "<INPUT TYPE=HIDDEN NAME=\"img\" VALUE=1>";
    }
    $o .= "<TD><INPUT TYPE=TEXT NAME=\"img_Name_$file_count\" VALUE=\"".$img->Get("Name")."\"></TD>";
    $o .= "<TD><INPUT TYPE=TEXT NAME=\"img_Alt_$file_count\" VALUE=\"".$img->Get("AltName")."\"></TD>";
    $o .= "<TD>";

    $o .= "<INPUT TYPE=TEXT size=40 NAME=\"img_Url_$file_count\" VALUE=\"".$img->Get("Url")."\">";
    $o .= "</TD><TD>";
    $o .= "<INPUT TYPE=FILE class=\"button\" VALUE=\"".$img->Get("Url")."\" NAME=\"$file_count\">";

    $o .= "<INPUT TYPE=HIDDEN NAME=\"img_Res_$file_count\" VALUE=\"".$img->Get("ResourceId")."\">";
    $o .= "<INPUT TYPE=HIDDEN NAME=\"img_Rel_$file_count\" VALUE=\"".$img->Get("RelatedTo")."\">";
    $o .= "<INPUT TYPE=HIDDEN NAME=\"img_Thumb_$file_count\" VALUE=\"".$img->Get("IsThumbnail")."\">";
    $parts = pathinfo($img->Get("LocalPath"));
    $destdir = $parts["dirname"];

    $o .= "<INPUT TYPE=HIDDEN NAME=\"img_DestDir_$file_count\" VALUE=\"$destdir\">";
    if($img->Get("RelatedTo")>0)
      $o .= "<input type=\"submit\" name=\"img_Del_$file_count\" value=\"Delete\" class=\"button2\">";
    $o .= "</TD>";
    $file_count++;
    return $o;
}

	function m_GetModuleInfo($info_type)
	{
		// get information for building sql in
		switch($info_type)
		{
			case 'rel_list': // Edit Category -> Relations List
				return Array(	'MainTable' => 'Category', 'ItemNameField' => 'CachedNavbar',
								'ItemNamePhrase' => 'la_Text_Category', 'TargetType' => 1);
				break;
			
			case 'summary_pending':
				global $imagesURL;
				$ret = Array();
				
				$UserURL = $GLOBALS['adminURL'].'/users/user_list.php?env='.BuildEnv();
				
				// pending users
				$ret[] = Array(	'link' => "config_val('User_View', '4' ,'$UserURL')",
								'icon_image' => $imagesURL.'/itemicons/icon16_user_pending.gif',
								'phrase' => 'la_Text_Users', 'list_var_name' => 'objUsers' );
								
				// pending categories
				$ret[] = Array(	'link' => "PendingLink('category','Category_View',41)",
								'icon_image' => $imagesURL.'/itemicons/icon16_cat_pending.gif',
								'phrase' => 'la_tab_Categories', 'list_var_name' => 'objCatList');
				
				return $ret;
				break;
		}
		
		return false;
	}

?>