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: class Mage_Adminhtml_Block_Api_Tab_Rolesedit extends Mage_Adminhtml_Block_Widget_Form {
28:
29: public function __construct() {
30: parent::__construct();
31:
32: $rid = Mage::app()->getRequest()->getParam('rid', false);
33:
34: $resources = Mage::getModel('api/roles')->getResourcesList();
35:
36: $rules_set = Mage::getResourceModel('api/rules_collection')->getByRoles($rid)->load();
37:
38: $selrids = array();
39:
40: foreach ($rules_set->getItems() as $item) {
41: if (array_key_exists(strtolower($item->getResource_id()), $resources)
42: && $item->getApiPermission() == 'allow')
43: {
44: $resources[$item->getResource_id()]['checked'] = true;
45: array_push($selrids, $item->getResource_id());
46: }
47: }
48:
49: $this->setSelectedResources($selrids);
50:
51: $this->setTemplate('api/rolesedit.phtml');
52:
53:
54: }
55:
56: public function getEverythingAllowed()
57: {
58: return in_array('all', $this->getSelectedResources());
59: }
60:
61: public function getResTreeJson()
62: {
63: $rid = Mage::app()->getRequest()->getParam('rid', false);
64: $resources = Mage::getModel('api/roles')->getResourcesTree();
65:
66: $rootArray = $this->_getNodeJson($resources,1);
67:
68: $json = Mage::helper('core')->jsonEncode(isset($rootArray['children']) ? $rootArray['children'] : array());
69:
70: return $json;
71: }
72:
73: protected function _sortTree($a, $b)
74: {
75: return $a['sort_order']<$b['sort_order'] ? -1 : ($a['sort_order']>$b['sort_order'] ? 1 : 0);
76: }
77:
78:
79: protected function _getNodeJson($node, $level=0)
80: {
81: $item = array();
82: $selres = $this->getSelectedResources();
83:
84: if ($level != 0) {
85: $item['text']= (string)$node->title;
86: $item['sort_order']= isset($node->sort_order) ? (string)$node->sort_order : 0;
87: $item['id'] = (string)$node->attributes()->aclpath;
88:
89: if (in_array($item['id'], $selres))
90: $item['checked'] = true;
91: }
92: if (isset($node->children)) {
93: $children = $node->children->children();
94: } else {
95: $children = $node->children();
96: }
97: if (empty($children)) {
98: return $item;
99: }
100:
101: if ($children) {
102: $item['children'] = array();
103:
104: foreach ($children as $child) {
105: if ($child->getName()!='title' && $child->getName()!='sort_order' && $child->attributes()->module) {
106: if ($level != 0) {
107: $item['children'][] = $this->_getNodeJson($child, $level+1);
108: } else {
109: $item = $this->_getNodeJson($child, $level+1);
110: }
111: }
112: }
113: if (!empty($item['children'])) {
114: usort($item['children'], array($this, '_sortTree'));
115: }
116: }
117: return $item;
118: }
119: }
120: