| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * |
| 38 | * @package CRM |
| 39 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 40 | * $Id$ |
| 41 | * |
| 42 | */ |
| 43 | class CRM_ACL_Form_ACL extends CRM_Admin_Form { |
| 44 | |
| 45 | /** |
| 46 | * This function sets the default values for the form. |
| 47 | * |
| 48 | * @access public |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | function setDefaultValues() { |
| 53 | $defaults = parent::setDefaultValues(); |
| 54 | |
| 55 | if ($this->_action & CRM_Core_Action::ADD) { |
| 56 | $defaults['object_type'] = 1; |
| 57 | } |
| 58 | |
| 59 | $showHide = new CRM_Core_ShowHideBlocks(); |
| 60 | |
| 61 | if (isset($defaults['object_table'])) { |
| 62 | switch ($defaults['object_table']) { |
| 63 | case 'civicrm_saved_search': |
| 64 | $defaults['group_id'] = $defaults['object_id']; |
| 65 | $defaults['object_type'] = 1; |
| 66 | $showHide->addShow("id-group-acl"); |
| 67 | $showHide->addHide("id-profile-acl"); |
| 68 | $showHide->addHide("id-custom-acl"); |
| 69 | $showHide->addHide("id-event-acl"); |
| 70 | break; |
| 71 | |
| 72 | case 'civicrm_uf_group': |
| 73 | $defaults['uf_group_id'] = $defaults['object_id']; |
| 74 | $defaults['object_type'] = 2; |
| 75 | $showHide->addHide("id-group-acl"); |
| 76 | $showHide->addShow("id-profile-acl"); |
| 77 | $showHide->addHide("id-custom-acl"); |
| 78 | $showHide->addHide("id-event-acl"); |
| 79 | break; |
| 80 | |
| 81 | case 'civicrm_custom_group': |
| 82 | $defaults['custom_group_id'] = $defaults['object_id']; |
| 83 | $defaults['object_type'] = 3; |
| 84 | $showHide->addHide("id-group-acl"); |
| 85 | $showHide->addHide("id-profile-acl"); |
| 86 | $showHide->addShow("id-custom-acl"); |
| 87 | $showHide->addHide("id-event-acl"); |
| 88 | break; |
| 89 | |
| 90 | case 'civicrm_event': |
| 91 | $defaults['event_id'] = $defaults['object_id']; |
| 92 | $defaults['object_type'] = 4; |
| 93 | $showHide->addHide("id-group-acl"); |
| 94 | $showHide->addHide("id-profile-acl"); |
| 95 | $showHide->addHide("id-custom-acl"); |
| 96 | $showHide->addShow("id-event-acl"); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | else { |
| 101 | $showHide->addHide("id-group-acl"); |
| 102 | $showHide->addHide("id-profile-acl"); |
| 103 | $showHide->addHide("id-custom-acl"); |
| 104 | $showHide->addHide("id-event-acl"); |
| 105 | } |
| 106 | |
| 107 | // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist) |
| 108 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
| 109 | $showHide->addToTemplate(); |
| 110 | } |
| 111 | |
| 112 | return $defaults; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Function to build the form |
| 117 | * |
| 118 | * @return void |
| 119 | * @access public |
| 120 | */ |
| 121 | public function buildQuickForm() { |
| 122 | parent::buildQuickForm(); |
| 123 | |
| 124 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $attributes = CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL'); |
| 129 | |
| 130 | $this->add('text', 'name', ts('Description'), CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE); |
| 131 | |
| 132 | $operations = array('' => ts('- select -')) + CRM_ACL_BAO_ACL::operation(); |
| 133 | $this->add('select', |
| 134 | 'operation', |
| 135 | ts('Operation'), |
| 136 | $operations, TRUE |
| 137 | ); |
| 138 | |
| 139 | $objTypes = array('1' => ts('A group of contacts'), |
| 140 | '2' => ts('A profile'), |
| 141 | '3' => ts('A set of custom data fields'), |
| 142 | ); |
| 143 | |
| 144 | if (CRM_Core_Permission::access('CiviEvent')) { |
| 145 | $objTypes['4'] = ts('Events'); |
| 146 | } |
| 147 | |
| 148 | $extra = array('onclick' => "showObjectSelect();"); |
| 149 | $this->addRadio('object_type', |
| 150 | ts('Type of Data'), |
| 151 | $objTypes, |
| 152 | $extra, |
| 153 | ' ', TRUE |
| 154 | ); |
| 155 | |
| 156 | |
| 157 | $label = ts('Role'); |
| 158 | $role = array('-1' => ts('- select role -'), |
| 159 | '0' => ts('Everyone'), |
| 160 | ) + CRM_Core_OptionGroup::values('acl_role'); |
| 161 | $this->add('select', 'entity_id', $label, $role, TRUE); |
| 162 | |
| 163 | $group = array('-1' => ts('- select -'), |
| 164 | '0' => ts('All Groups'), |
| 165 | ) + CRM_Core_PseudoConstant::group(); |
| 166 | |
| 167 | $customGroup = array('-1' => ts('- select -'), |
| 168 | '0' => ts('All Custom Groups'), |
| 169 | ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id'); |
| 170 | |
| 171 | $ufGroup = array('-1' => ts('- select -'), |
| 172 | '0' => ts('All Profiles'), |
| 173 | ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id'); |
| 174 | |
| 175 | $event = array('-1' => ts('- select -'), |
| 176 | '0' => ts('All Events'), |
| 177 | ) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); |
| 178 | |
| 179 | $this->add('select', 'group_id', ts('Group'), $group); |
| 180 | $this->add('select', 'custom_group_id', ts('Custom Data'), $customGroup); |
| 181 | $this->add('select', 'uf_group_id', ts('Profile'), $ufGroup); |
| 182 | $this->add('select', 'event_id', ts('Event'), $event); |
| 183 | |
| 184 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
| 185 | |
| 186 | $this->addFormRule(array('CRM_ACL_Form_ACL', 'formRule')); |
| 187 | } |
| 188 | |
| 189 | static function formRule($params) { |
| 190 | $showHide = new CRM_Core_ShowHideBlocks(); |
| 191 | |
| 192 | // Make sure role is not -1 |
| 193 | if ($params['entity_id'] == -1) { |
| 194 | $errors['entity_id'] = ts('Please assign this permission to a Role.'); |
| 195 | } |
| 196 | |
| 197 | $validOperations = array('View', 'Edit'); |
| 198 | $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data"); |
| 199 | |
| 200 | // Figure out which type of object we're permissioning on and make sure user has selected a value. |
| 201 | switch ($params['object_type']) { |
| 202 | case 1: |
| 203 | if ($params['group_id'] == -1) { |
| 204 | $errors['group_id'] = ts('Please select a Group (or ALL Groups).'); |
| 205 | $showHide->addShow("id-group-acl"); |
| 206 | $showHide->addHide("id-profile-acl"); |
| 207 | $showHide->addHide("id-custom-acl"); |
| 208 | $showHide->addHide("id-event-acl"); |
| 209 | } |
| 210 | if (!in_array($params['operation'], $validOperations)) { |
| 211 | $errors['operation'] = $operationMessage; |
| 212 | } |
| 213 | break; |
| 214 | |
| 215 | case 2: |
| 216 | if ($params['uf_group_id'] == -1) { |
| 217 | $errors['uf_group_id'] = ts('Please select a Profile (or ALL Profiles).'); |
| 218 | $showHide->addShow("id-profile-acl"); |
| 219 | $showHide->addHide("id-group-acl"); |
| 220 | $showHide->addHide("id-custom-acl"); |
| 221 | $showHide->addHide("id-event-acl"); |
| 222 | } |
| 223 | break; |
| 224 | |
| 225 | case 3: |
| 226 | if ($params['custom_group_id'] == -1) { |
| 227 | $errors['custom_group_id'] = ts('Please select a set of Custom Data (or ALL Custom Data).'); |
| 228 | $showHide->addShow("id-custom-acl"); |
| 229 | $showHide->addHide("id-group-acl"); |
| 230 | $showHide->addHide("id-profile-acl"); |
| 231 | $showHide->addHide("id-event-acl"); |
| 232 | } |
| 233 | if (!in_array($params['operation'], $validOperations)) { |
| 234 | $errors['operation'] = $operationMessage; |
| 235 | } |
| 236 | break; |
| 237 | |
| 238 | case 4: |
| 239 | if ($params['event_id'] == -1) { |
| 240 | $errors['event_id'] = ts('Please select an Event (or ALL Events).'); |
| 241 | $showHide->addShow("id-event-acl"); |
| 242 | $showHide->addHide("id-custom-acl"); |
| 243 | $showHide->addHide("id-group-acl"); |
| 244 | $showHide->addHide("id-profile-acl"); |
| 245 | } |
| 246 | if (!in_array($params['operation'], $validOperations)) { |
| 247 | $errors['operation'] = $operationMessage; |
| 248 | } |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | $showHide->addToTemplate(); |
| 253 | |
| 254 | return empty($errors) ? TRUE : $errors; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Function to process the form |
| 259 | * |
| 260 | * @access public |
| 261 | * |
| 262 | * @return void |
| 263 | */ |
| 264 | public function postProcess() { |
| 265 | // note this also resets any ACL cache |
| 266 | CRM_Core_BAO_Cache::deleteGroup('contact fields'); |
| 267 | |
| 268 | |
| 269 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 270 | CRM_ACL_BAO_ACL::del($this->_id); |
| 271 | CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success'); |
| 272 | } |
| 273 | else { |
| 274 | $params = $this->controller->exportValues($this->_name); |
| 275 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 276 | $params['deny'] = 0; |
| 277 | $params['entity_table'] = 'civicrm_acl_role'; |
| 278 | |
| 279 | // Figure out which type of object we're permissioning on and set object_table and object_id. |
| 280 | switch ($params['object_type']) { |
| 281 | case 1: |
| 282 | $params['object_table'] = 'civicrm_saved_search'; |
| 283 | $params['object_id'] = $params['group_id']; |
| 284 | break; |
| 285 | |
| 286 | case 2: |
| 287 | $params['object_table'] = 'civicrm_uf_group'; |
| 288 | $params['object_id'] = $params['uf_group_id']; |
| 289 | break; |
| 290 | |
| 291 | case 3: |
| 292 | $params['object_table'] = 'civicrm_custom_group'; |
| 293 | $params['object_id'] = $params['custom_group_id']; |
| 294 | break; |
| 295 | |
| 296 | case 4: |
| 297 | $params['object_table'] = 'civicrm_event'; |
| 298 | $params['object_id'] = $params['event_id']; |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | if ($this->_id) { |
| 303 | $params['id'] = $this->_id; |
| 304 | } |
| 305 | |
| 306 | CRM_ACL_BAO_ACL::create($params); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |