<?php 
class ApCategoryListHelper extends AppHelper {

  var $name = 'ApCategoryList';
  var $helpers = array('Html', 'Tree');

  var $__settings = array();
  var $_categories = array();

  function menulist($data, $settings = array()) {
    $this->__settings = array_merge(array(
        'model' => null,
        'div_id' => null,
        'div_class' => null,
      ), (array)$settings);
    $this->_categories = $data;

    if ($this->_categories) {
      $out = '<div';
      if (!empty($this->__settings['div_id'])) {
        $out .= ' id="' . $this->__settings['div_id'] . '"';
      }
      if (!empty($this->__settings['div_class'])) {
        $out .= ' class="' . $this->__settings['div_class'] . '"';
      }
      $out .= '>';
      $option = array('element' => 'ap_c_list');
      if (!empty($this->__settings['model'])) {
        $option['model'] = $this->__settings['model'];
      }
      $out .= $this->Tree->generate($this->_categories, $option);
      $out .= '</div>';
      return $this->output($out);
    }
  }

  function cclist($data, $current = null, $settings = array()) {
    $lview = ClassRegistry::getObject('view');
    $this->__settings = array_merge(array(
        'div_id' => null,
        'div_class' => null,
        'dl_id' => null,
        'dl_class' => null,
        'gchild_sep' => ', ',
      ), (array)$settings);
    $this->_categories = $data;

    if ($this->_categories) {
      $out = '';
      if (!empty($current)) {
        $parent_id = $current['Category']['id'];
        $cChild = array();
        foreach ($this->_categories as $val) {
          if ($val['Category']['parent_id'] == $parent_id) {
            $cChild[] = $val;
          }
        }
        if (!empty($cChild)) {
          $out .= '<div';
          if (!empty($this->__settings['div_id'])) {
            $out .= ' id="' . $this->__settings['div_id'] . '"';
          }
          if (!empty($this->__settings['div_class'])) {
            $out .= ' class="' . $this->__settings['div_class'] . '"';
          }
          $out .= '>';

          $index = 1;
          foreach ($cChild as $child) {
            $out .= '<dl';
            if (!empty($this->__settings['dl_id'])) {
              $out .= ' id="' . $this->__settings['dl_id'] . '"';
            }
            $out .= ' class="';
            if ($index % 2 == 0) {
              $out .= 'even';
            } else {
              $out .= 'odd';
            }
            if (!empty($this->__settings['dl_class'])) {
              ' ' . $this->__settings['dl_class'];
            }
            $out .= '"';
            $out .= '>';
            $out .= '<dt>' . $lview->renderElement('ap_c_list', array('data' => $child)) . '</dt>';
            $cgChild = array();
            foreach ($this->_categories as $gchild) {
              if ($gchild['Category']['parent_id'] == $child['Category']['id']) {
                $cgChild[] = $lview->renderElement('ap_c_list', array('data' => $gchild));
              }
            }
            if (!empty($cgChild)) {
              $out .= '<dd>';
              $out .= implode($this->__settings['gchild_sep'], $cgChild);
              $out .= '</dd>';
            }
            $out .= '</dl>';
            $index++;
          }
          $out .= '</div>';
        }
      }
      return $this->output($out);
    }
  }
}
?>
