00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class Mage_Adminhtml_Block_Api_Tab_Rolesedit extends Mage_Adminhtml_Block_Widget_Form {
00028
00029 public function __construct() {
00030 parent::__construct();
00031
00032 $rid = Mage::app()->getRequest()->getParam('rid', false);
00033
00034 $resources = Mage::getModel('api/roles')->getResourcesList();
00035
00036 $rules_set = Mage::getResourceModel('api/rules_collection')->getByRoles($rid)->load();
00037
00038 $selrids = array();
00039
00040 foreach ($rules_set->getItems() as $item) {
00041 if (array_key_exists(strtolower($item->getResource_id()), $resources) && $item->getPermission() == 'allow') {
00042 $resources[$item->getResource_id()]['checked'] = true;
00043 array_push($selrids, $item->getResource_id());
00044 }
00045 }
00046
00047 $this->setSelectedResources($selrids);
00048
00049 $this->setTemplate('api/rolesedit.phtml');
00050
00051
00052 }
00053
00054 public function getEverythingAllowed()
00055 {
00056 return in_array('all', $this->getSelectedResources());
00057 }
00058
00059 public function getResTreeJson()
00060 {
00061 $rid = Mage::app()->getRequest()->getParam('rid', false);
00062 $resources = Mage::getModel('api/roles')->getResourcesTree();
00063
00064 $rootArray = $this->_getNodeJson($resources,1);
00065
00066 $json = Zend_Json::encode(isset($rootArray['children']) ? $rootArray['children'] : array());
00067
00068 return $json;
00069 }
00070
00071 protected function _sortTree($a, $b)
00072 {
00073 return $a['sort_order']<$b['sort_order'] ? -1 : ($a['sort_order']>$b['sort_order'] ? 1 : 0);
00074 }
00075
00076
00077 protected function _getNodeJson($node, $level=0)
00078 {
00079 $item = array();
00080 $selres = $this->getSelectedResources();
00081
00082 if ($level != 0) {
00083 $item['text']= (string)$node->title;
00084 $item['sort_order']= isset($node->sort_order) ? (string)$node->sort_order : 0;
00085 $item['id'] = (string)$node->attributes()->aclpath;
00086
00087 if (in_array($item['id'], $selres))
00088 $item['checked'] = true;
00089 }
00090 if (isset($node->children)) {
00091 $children = $node->children->children();
00092 } else {
00093 $children = $node->children();
00094 }
00095 if (empty($children)) {
00096 return $item;
00097 }
00098
00099 if ($children) {
00100 $item['children'] = array();
00101
00102 foreach ($children as $child) {
00103 if ($child->getName()!='title' && $child->getName()!='sort_order' && $child->attributes()->module) {
00104 if ($level != 0) {
00105 $item['children'][] = $this->_getNodeJson($child, $level+1);
00106 } else {
00107 $item = $this->_getNodeJson($child, $level+1);
00108 }
00109 }
00110 }
00111 if (!empty($item['children'])) {
00112 usort($item['children'], array($this, '_sortTree'));
00113 }
00114 }
00115 return $item;
00116 }
00117 }