1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class AccessRightsTreeInputCheckboxHelper extends AppHelper {
	public $helpers = array("Form");

//	private $aroObject = array();
//	private $aclComponent = null;
 	private $permissions = null;
	private $formModel = "Menu";
	private $mainULAttributes = "";
	
	public function __construct(View $view, $settings = array()) {
		parent::__construct($view, $settings);
		
//		$this->aclComponent = $settings["aclComponentObject"];
//		$this->aroObject = $settings["aroObject"];
 		$this->permissions = $settings["permissions"];
		$this->formModel = $settings["formModel"];
		$this->mainULAttributes = $settings["mainULAttributes"];
	}
		
	public function generateAccessRightsTreeInputCheckbox(array $data = array(), $strPath = "", $inputName = "", $depth=0) {
		$strUL = "<ul>";
		
		if ($depth == 0) {
			$strUL = "<ul ".$this->mainULAttributes.">";
		}
		
		$path = "";
		$inputArrStrName = "";

		foreach ($data as $item) {
			$access = false;
			
			if (is_array($item["children"]) && (count($item["children"]) > 0)) {
				// controllers
				// Users
				$path = $item["Aco"]["alias"];
				$inputArrStrName = $path;
		
//				if ($this->aclComponent->check($this->aroObject, $path)) {
 				if ($this->__checkPermission($item["Aco"]["id"], $this->permissions)) {
					$access = true;
				}
				
				$strUL .= "<li id={$item["Aco"]["alias"]}Item>";

				if ($access) {
					$strUL .= $this->Form->checkbox("{$this->formModel}.{$inputArrStrName}.{$path}", array(
							"id" => $item["Aco"]["alias"],
							"value" => "1",
							"class" => "access_rights",
							"checked" => "checked"
						)
					);
					
				} else {
					$strUL .= $this->Form->checkbox("{$this->formModel}.{$inputArrStrName}.{$path}", array(
							"id" => $item["Aco"]["alias"],
							"value" => "1",
							"class" => "access_rights"
						)
					);
				}
				
				$strUL .= $this->Form->label("{$this->formModel}.{$item["Aco"]["alias"]}");
				$strUL .= $this->generateAccessRightsTreeInputCheckbox($item['children'], $path, $inputArrStrName, ($depth+1));
				$strUL .= "</li>";
					
			} else {
				// Users/index, Users/add, Users/etc
				// Groups/index, Groups/add, Groups/etc
				$path =  $strPath."/".$item['Aco']['alias'];
				$inputArrStrName = "{$inputName}.children.{$item["Aco"]["alias"]}";
				
//				if ($this->aclComponent->check($this->aroObject, $path)) {
 				if ($this->__checkPermission($item["Aco"]["id"], $this->permissions)) {
					$access = true;
				}
		
				$strUL .= "<li>";
				
				if ($access) {
					$strUL .= $this->Form->checkbox("{$this->formModel}.{$inputArrStrName}", array(
							"class" => "access_rights {$strPath}",
							"checked" => "checked"
						)
					);
					
				} else {
					$strUL .= $this->Form->checkbox("{$this->formModel}.{$inputArrStrName}", array(
							"class" => "access_rights {$strPath}"
						)
					);
				}
				
				$strUL .= $this->Form->label("{$this->formModel}.{$item["Aco"]["alias"]}");
				$strUL .= "</li>";
			}
		}
		
		$strUL .= "</ul>";
		
		return $strUL;	
	}
	
 	private function __checkPermission($itemPermission, array $permissions) {
 		$ret = false;
 		$count = count($permissions);

 		if ($count == 0) {
 			return false;
 		} else {
 			if (in_array(1, $permissions)) {
 				return true;
 			} else {
 				if (in_array($itemPermission, $permissions)) {
 					$ret = true;
 				}
 			}
 		}
	
 		return $ret;
 	}
}