Skip to content

Instantly share code, notes, and snippets.

@maxchene
Created April 17, 2014 12:20
Show Gist options
  • Save maxchene/10979037 to your computer and use it in GitHub Desktop.
Save maxchene/10979037 to your computer and use it in GitHub Desktop.
<?php
App::uses('AppHelper', 'View/Helper');
App::uses('CakeResponse', 'Network');
class NestableHelper extends AppHelper {
public function generate($categories, $options = array()){
$this->__options = array_merge(
array(
'model' => null,
'listType' => 'ol',
'id' => null,
'class' => 'dd-list',
'wrapper' => 'div'
),
(array)$options
);
if($this->__options['model'] == null){
$model = current($this->request->params['models']);
$model = $model['className'];
}
$listType = $this->__options['listType'];
$this->__options['id'] !== null ? $id = ' id="'.$this->__options['id'].'"' : $id = '';
$this->__options['class'] !== null ? $class = ' class="'.$this->__options['class'].'"' : $class = '';
$wrapper = $this->__options['wrapper'];
$this->html .= '<'.$listType.$id.$class.'>';
foreach ($categories as $key => $category) {
$this->html .= '<li data-id="'.$category[$model]['id'].'" class="dd-item">';
$this->html .= '<'.$wrapper.' class="dd-handle">';
$this->html .= $category[$model]['name'];
$this->html .= '</'.$wrapper.'>' ;
if (!empty($category['children'])) {
$this->generate($category['children'], array('model' => $this->__options['model'], 'listType' => $this->__options['listType'], 'wrapper' => $this->__options['wrapper']));
}
$this->html .= '</li>';
}
$this->html .= '</'.$listType.'>';
return $this->html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment