<?php

/**
* @version 2.8.8
* @package Joomla 2.5
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

jimport( 'joomla.html.pagination' );
global $mainframe;
$listDirn = $mainframe->getUserStateFromRequest('com_dtregister.category.list.filter_order_Dir','filter_order_Dir', 'asc', 'cmd');

$listOrder = $mainframe->getUserStateFromRequest('com_dtregister.category.list.filter_order','filter_order', 'lft', 'cmd');

$ordering 	= ($listOrder == 'lft');
$rows = $this->getModel('category')->find('parent_id > 0', " $listOrder $listDirn "); // lft asc ,

$saveOrder 	= ($listOrder == 'lft' && $listDirn == 'asc');

$pageNav = new JPagination(count($rows), 0, 10);
$accesOptions = array('0'=>JText::_('DT_PUBLIC'),'1'=>JText::_('DT_REGISTERED'),'2'=>JText::_('DT_SPECIAL'));
$children = array();
foreach ($rows as &$item) {
	        $item->id =$item->categoryId;
			$this->ordering[$item->parent_id][] = $item->id;
		}

        /*foreach ($rows as $v )

		{

			$pt = $v->parent_id;

			$list = @$children[$pt] ? $children[$pt] : array();

			array_push( $list, $v );

			$children[$pt] = $list;

		}

	    $catlist=array();

		  if(isset($children[0]) && is_array($children[0]))

		  foreach($children[0] as $pcategory){

		   $catlist[]=$pcategory;

		   if(isset($children[$pcategory->categoryId])){

		       foreach($children[$pcategory->categoryId] as $childcat){

				   $childcat->categoryName = "|_".$childcat->categoryName;

			       $catlist[]=$childcat;

			   }

		   }

		}

		$rows = $catlist;	*/

?>

<form action="index.php" method="post" name="adminForm" autocomplete="off">

  <p><?php echo JText::_( 'DT_CATEGORY_INSTRUCTIONS'); ?></p>

<!--# Category ID ,Category Name, Parent Category, Access Level ,Status, Reorder, Order Delete -->
   <table  align="left" style="width:900px">
       <tr>
           <th width="20" class="dt_heading"><input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $rows ); ?>);" /></th>
           <th width="5" align="left" class="dt_heading">#</th>
           <th width="10" align="left" class="dt_heading"><?php echo JText::_( 'DT_CATEGORY_ID' ); ?></th>
           <th width="100" align="left" class="dt_heading"><?php echo JText::_( 'DT_CATEGORY_NAME' ); ?></th>
          
           <th width="100" align="left" class="dt_heading"><?php echo JText::_( 'DT_ACCESS_LEVEL' ); ?></th>
           <th width="25" align="left" class="dt_heading"><?php echo JText::_( 'DT_STATUS' ); ?></th>
           <th  width="30" class="dt_heading"><?php echo JText::_( 'DT_REORDER' ); ?>&nbsp;<?php echo JText::_( 'DT_ORDER' );  echo JHTML::_('grid.order', $rows ); ?></th>
           
           <th width="20" align="left" class="dt_heading"><?php echo JText::_( 'DT_DELETE' ); ?></th>
	</tr>

	<?php

	  $k = 0;
      $n = count($rows);
	  $originalOrders = array();
      foreach ($rows as $i => $row) {
      $orderkey	= array_search($row->id, $this->ordering[$row->parent_id]);
	  
	  $link1 = 'index.php?option=com_dtregister&task=edit&controller=category&id='. $row->categoryId;

      $link2 = 'index.php?option=com_dtregister&task=delete&controller=category&id='. $row->categoryId;

      $color = ($row->color!="")?'style="background-color:'.$row->color.';"':'';
	  $published = DtHtml::published($row, $i);
	 /* if ($row->level > 1)

	  {

		  $parentsStr = "";

		  $_currentParentId = $row->parent_id;

		  $parentsStr = " ".$_currentParentId;

		  for ($i = 0; $i < $row->level; $i++)

		  {

			  foreach ($this->ordering as $k => $v)

			  {

				  $v = implode("-", $v);

				  $v = "-".$v."-";

				  if (strpos($v, "-" . $_currentParentId . "-") !== false)

				  {

					  $parentsStr .= " ".$k;

					  $_currentParentId = $k;

					  break;

				  }

			  }

		  }

	  }

	  else

	  {

		  $parentsStr = "";

	  }*/

	?>

	<tr class="<?php echo "row$k"; ?>">
       <td <?php echo $color; ?>>
         <input type="checkbox" id="cb<?php echo $i;?>" name="cid[]" value="<?php echo $row->categoryId; ?>" onclick="isChecked(this.checked);" />
         
       </td>
       <td><?php echo $k+1; ?></td>
       <td><?php echo $row->categoryId;?></td>
       <td>
       <?php echo str_repeat('<span class="gi">&mdash;</span>', $row->level - 1) ?>
       <a href="<?php echo $link1?>"><?php echo stripslashes($row->categoryName);?></a></td>
      
 
       <td><?php echo $this->accessOptions[$row->access]; ?></td>       
       <td><?php echo $published;?></td>
       <td class="order">
	  
          <?php $saveOrder = 1; if ($saveOrder) { ?>
              <span><?php echo $pageNav->orderUpIcon($i, isset($this->ordering[$row->parent_id][$orderkey - 1]), 'orderup', 'JLIB_HTML_MOVE_UP', $ordering); ?></span>
              <span><?php echo $pageNav->orderDownIcon($i, $pageNav->total, isset($this->ordering[$row->parent_id][$orderkey + 1]), 'orderdown', 'JLIB_HTML_MOVE_DOWN', $ordering); ?></span>
          <?php } ?>
          <?php $disabled = $saveOrder ?  '' : 'disabled="disabled"'; ?>
          <input type="text" name="order[]" size="5" value="<?php echo $orderkey + 1;?>" <?php echo $disabled ?> class="text-area-order" />
          <?php $originalOrders[] = $orderkey + 1; ?>
      
  </td>
       
        <td><a href="<?php echo $link2?>"><img src="components/com_dtregister/assets/images/delete.jpg" style="height:16px; width:16px; border: 0px; padding-left:3px"</a></td>

	  <?php $k = $k+ 1; 
	  } 
	  
	  ?>

	</tr>

	</table>

  <input name="task" type="hidden" value="edit" />

  <input type="hidden" name="controller" value="category" />

  <input name="option" type="hidden" value="<?php echo DTR_COM_COMPONENT; ?>" />
  <input type="hidden" name="original_order_values" value="<?php echo implode($originalOrders, ','); ?>" />
  <input type="hidden" name="boxchecked" value="0" />

</form>

<script language="javascript">

			function savecategoryOrder(n,task)

			{

				for ( var j = 0; j <= n; j++ )

				{

					box = eval( "document.adminForm.cb" + j );

					if ( box ) {

						if ( box.checked == false ) {

							box.checked = true;

						}

					} else {

						alert("You cannot change the order of items, as an item in the list is `Checked Out`");

						return;

					}

				}

				submitform(task);

			}

		</script>

   <script language="javascript" type="text/javascript">

        function hideMainMenu()

       {

    window.location = "index.php?option=com_dtregister&task=new&controller=category&hidemainmenu=1";

        }

Joomla.submitbutton = function(pressbutton) {

var form = document.adminForm;

		if (pressbutton) {

			submitform( pressbutton );

			return;

		}

}

</script>

