1: <?php
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: class Mage_Adminhtml_Block_Permissions_Tab_Rolesedit extends Mage_Adminhtml_Block_Widget_Form
35: implements Mage_Adminhtml_Block_Widget_Tab_Interface
36: {
37: 38: 39: 40: 41:
42: public function getTabLabel()
43: {
44: return Mage::helper('adminhtml')->__('Role Resources');
45: }
46:
47: 48: 49: 50: 51:
52: public function getTabTitle()
53: {
54: return $this->getTabLabel();
55: }
56:
57: 58: 59: 60: 61:
62: public function canShowTab()
63: {
64: return true;
65: }
66:
67: 68: 69: 70: 71:
72: public function isHidden()
73: {
74: return false;
75: }
76:
77: 78: 79: 80:
81: public function __construct()
82: {
83: parent::__construct();
84:
85: $rid = Mage::app()->getRequest()->getParam('rid', false);
86:
87: $resources = Mage::getModel('admin/roles')->getResourcesList();
88:
89: $rules_set = Mage::getResourceModel('admin/rules_collection')->getByRoles($rid)->load();
90:
91: $selrids = array();
92:
93: foreach ($rules_set->getItems() as $item) {
94: $itemResourceId = $item->getResource_id();
95: if (array_key_exists(strtolower($itemResourceId), $resources) && $item->getPermission() == 'allow') {
96: $resources[$itemResourceId]['checked'] = true;
97: array_push($selrids, $itemResourceId);
98: }
99: }
100:
101: $this->setSelectedResources($selrids);
102:
103: $this->setTemplate('permissions/rolesedit.phtml');
104:
105:
106: }
107:
108: 109: 110: 111: 112:
113: public function getEverythingAllowed()
114: {
115: return in_array('all', $this->getSelectedResources());
116: }
117:
118: 119: 120: 121: 122:
123: public function getResTreeJson()
124: {
125: $rid = Mage::app()->getRequest()->getParam('rid', false);
126: $resources = Mage::getModel('admin/roles')->getResourcesTree();
127:
128: $rootArray = $this->_getNodeJson($resources->admin, 1);
129:
130: $json = Mage::helper('core')->jsonEncode(isset($rootArray['children']) ? $rootArray['children'] : array());
131:
132: return $json;
133: }
134:
135: 136: 137: 138: 139: 140: 141:
142: protected function _sortTree($a, $b)
143: {
144: return $a['sort_order']<$b['sort_order'] ? -1 : ($a['sort_order']>$b['sort_order'] ? 1 : 0);
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: protected function _getNodeJson($node, $level = 0)
155: {
156: $item = array();
157: $selres = $this->getSelectedResources();
158:
159: if ($level != 0) {
160: $item['text'] = Mage::helper('adminhtml')->__((string)$node->title);
161: $item['sort_order'] = isset($node->sort_order) ? (string)$node->sort_order : 0;
162: $item['id'] = (string)$node->attributes()->aclpath;
163:
164: if (in_array($item['id'], $selres))
165: $item['checked'] = true;
166: }
167: if (isset($node->children)) {
168: $children = $node->children->children();
169: } else {
170: $children = $node->children();
171: }
172: if (empty($children)) {
173: return $item;
174: }
175:
176: if ($children) {
177: $item['children'] = array();
178:
179: foreach ($children as $child) {
180: if ($child->getName() != 'title' && $child->getName() != 'sort_order') {
181: if (!(string)$child->title) {
182: continue;
183: }
184: if ($level != 0) {
185: $item['children'][] = $this->_getNodeJson($child, $level+1);
186: } else {
187: $item = $this->_getNodeJson($child, $level+1);
188: }
189: }
190: }
191: if (!empty($item['children'])) {
192: usort($item['children'], array($this, '_sortTree'));
193: }
194: }
195: return $item;
196: }
197: }
198: