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_Role_Grid_User extends Mage_Adminhtml_Block_Widget_Grid
35: {
36:
37: public function __construct()
38: {
39: parent::__construct();
40: $this->setDefaultSort('role_user_id');
41: $this->setDefaultDir('asc');
42: $this->setId('roleUserGrid');
43: $this->setDefaultFilter(array('in_role_users'=>1));
44: $this->setUseAjax(true);
45: }
46:
47: protected function _addColumnFilterToCollection($column)
48: {
49: if ($column->getId() == 'in_role_users') {
50: $inRoleIds = $this->_getUsers();
51: if (empty($inRoleIds)) {
52: $inRoleIds = 0;
53: }
54: if ($column->getFilter()->getValue()) {
55: $this->getCollection()->addFieldToFilter('user_id', array('in'=>$inRoleIds));
56: }
57: else {
58: if($inRoleIds) {
59: $this->getCollection()->addFieldToFilter('user_id', array('nin'=>$inRoleIds));
60: }
61: }
62: }
63: else {
64: parent::_addColumnFilterToCollection($column);
65: }
66: return $this;
67: }
68:
69: protected function _prepareCollection()
70: {
71: $roleId = $this->getRequest()->getParam('rid');
72: Mage::register('RID', $roleId);
73: $collection = Mage::getModel('admin/roles')->getUsersCollection();
74: $this->setCollection($collection);
75: return parent::_prepareCollection();
76: }
77:
78: protected function _prepareColumns()
79: {
80: $this->addColumn('in_role_users', array(
81: 'header_css_class' => 'a-center',
82: 'type' => 'checkbox',
83: 'name' => 'in_role_users',
84: 'values' => $this->_getUsers(),
85: 'align' => 'center',
86: 'index' => 'user_id'
87: ));
88:
89: $this->addColumn('role_user_id', array(
90: 'header' =>Mage::helper('adminhtml')->__('User ID'),
91: 'width' =>5,
92: 'align' =>'left',
93: 'sortable' =>true,
94: 'index' =>'user_id'
95: ));
96:
97: $this->addColumn('role_user_username', array(
98: 'header' =>Mage::helper('adminhtml')->__('User Name'),
99: 'align' =>'left',
100: 'index' =>'username'
101: ));
102:
103: $this->addColumn('role_user_firstname', array(
104: 'header' =>Mage::helper('adminhtml')->__('First Name'),
105: 'align' =>'left',
106: 'index' =>'firstname'
107: ));
108:
109: $this->addColumn('role_user_lastname', array(
110: 'header' =>Mage::helper('adminhtml')->__('Last Name'),
111: 'align' =>'left',
112: 'index' =>'lastname'
113: ));
114:
115: $this->addColumn('role_user_email', array(
116: 'header' =>Mage::helper('adminhtml')->__('Email'),
117: 'width' =>40,
118: 'align' =>'left',
119: 'index' =>'email'
120: ));
121:
122: $this->addColumn('role_user_is_active', array(
123: 'header' => Mage::helper('adminhtml')->__('Status'),
124: 'index' => 'is_active',
125: 'align' =>'left',
126: 'type' => 'options',
127: 'options' => array('1' => Mage::helper('adminhtml')->__('Active'), '0' => Mage::helper('adminhtml')->__('Inactive')),
128: ));
129:
130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146:
147:
148: return parent::_prepareColumns();
149: }
150:
151: public function getGridUrl()
152: {
153: $roleId = $this->getRequest()->getParam('rid');
154: return $this->getUrl('*/*/editrolegrid', array('rid' => $roleId));
155: }
156:
157: protected function _getUsers($json=false)
158: {
159: if ( $this->getRequest()->getParam('in_role_user') != "" ) {
160: return $this->getRequest()->getParam('in_role_user');
161: }
162: $roleId = ( $this->getRequest()->getParam('rid') > 0 ) ? $this->getRequest()->getParam('rid') : Mage::registry('RID');
163: $users = Mage::getModel('admin/roles')->setId($roleId)->getRoleUsers();
164: if (sizeof($users) > 0) {
165: if ( $json ) {
166: $jsonUsers = Array();
167: foreach($users as $usrid) $jsonUsers[$usrid] = 0;
168: return Mage::helper('core')->jsonEncode((object)$jsonUsers);
169: } else {
170: return array_values($users);
171: }
172: } else {
173: if ( $json ) {
174: return '{}';
175: } else {
176: return array();
177: }
178: }
179: }
180:
181: }
182:
183: