<?php

App::uses('Model', 'Model');

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */

class Role extends AppModel {

	
	public function threaded_to_list($data_array, $model='Role', $key='id', $value='name', &$list = array(), $counter = 0, $separator='....')
	{
		if ( ! is_array($data_array))
			return array();

		foreach ($data_array AS $data)
		{
			$list[$data[$model][$key]] = str_repeat($separator, $counter).$data[$model][$value]." (Grade - ".$data[$model]['grade'].")";
			if ( ! empty($data['children']))
			{
				$this->threaded_to_list($data['children'], $model, $key, $value, $list, $counter + 1);
			}
		}
		return $list;
	}
	
	public function getrolesList(){
		$roles = $this->find('threaded');
		$list = $this->threaded_to_list($roles);
		return $list;
	}
	
	public function getList($user){
		$conditions = array();  
		if ($user['Role']['grade']!= 1){ 
			//$conditions = array('grade >='=>$user['Role']['grade']);
			$conditions = array('grade >'=>$user['Role']['grade']);
		}	

		$roles = $this->find('threaded', array('conditions'=>$conditions));
		$list = $this->threaded_to_list($roles); 
		return $list;
		
		
	}
}