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_User_Edit_Tab_Roles extends Mage_Adminhtml_Block_Widget_Grid
28: {
29:
30: public function __construct()
31: {
32: parent::__construct();
33: $this->setId('permissionsUserRolesGrid');
34: $this->setDefaultSort('sort_order');
35: $this->setDefaultDir('asc');
36:
37: $this->setTitle(Mage::helper('adminhtml')->__('User Roles Information'));
38: $this->setUseAjax(true);
39: }
40:
41: protected function _addColumnFilterToCollection($column)
42: {
43: if ($column->getId() == 'assigned_user_role') {
44: $userRoles = $this->_getSelectedRoles();
45: if (empty($userRoles)) {
46: $userRoles = 0;
47: }
48: if ($column->getFilter()->getValue()) {
49: $this->getCollection()->addFieldToFilter('role_id', array('in'=>$userRoles));
50: }
51: else {
52: if($userRoles) {
53: $this->getCollection()->addFieldToFilter('role_id', array('nin'=>$userRoles));
54: }
55: }
56: }
57: else {
58: parent::_addColumnFilterToCollection($column);
59: }
60: return $this;
61: }
62:
63: protected function _prepareCollection()
64: {
65: $collection = Mage::getResourceModel('api/role_collection');
66: $collection->setRolesFilter();
67: $this->setCollection($collection);
68: return parent::_prepareCollection();
69: }
70:
71: protected function _prepareColumns()
72: {
73:
74: $this->addColumn('assigned_user_role', array(
75: 'header_css_class' => 'a-center',
76: 'header' => Mage::helper('adminhtml')->__('Assigned'),
77: 'type' => 'radio',
78: 'html_name' => 'roles[]',
79: 'values' => $this->_getSelectedRoles(),
80: 'align' => 'center',
81: 'index' => 'role_id'
82: ));
83:
84: 85: 86: 87: 88: 89:
90:
91: $this->addColumn('role_name', array(
92: 'header' =>Mage::helper('adminhtml')->__('Role Name'),
93: 'index' =>'role_name'
94: ));
95:
96: return parent::_prepareColumns();
97: }
98:
99: public function getGridUrl()
100: {
101: return $this->getUrl('*/*/rolesGrid', array('user_id' => Mage::registry('api_user')->getUserId()));
102: }
103:
104: protected function _getSelectedRoles($json=false)
105: {
106: if ( $this->getRequest()->getParam('user_roles') != "" ) {
107: return $this->getRequest()->getParam('user_roles');
108: }
109: $uRoles = Mage::registry('api_user')->getRoles();
110: if ($json) {
111: $jsonRoles = Array();
112: foreach($uRoles as $urid) $jsonRoles[$urid] = 0;
113: return Mage::helper('core')->jsonEncode((object)$jsonRoles);
114: } else {
115: return $uRoles;
116: }
117: }
118:
119: }
120: