| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | abstract class CRM_Core_Page_Basic extends CRM_Core_Page { |
| 36 | |
| 37 | protected $_action; |
| 38 | |
| 39 | /** |
| 40 | * Define all the abstract functions here. |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * Name of the BAO to perform various DB manipulations. |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | abstract protected function getBAOName(); |
| 49 | |
| 50 | /** |
| 51 | * An array of action links. |
| 52 | * |
| 53 | * @return array |
| 54 | * (reference) |
| 55 | */ |
| 56 | abstract protected function &links(); |
| 57 | |
| 58 | /** |
| 59 | * Name of the edit form class. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | abstract protected function editForm(); |
| 64 | |
| 65 | /** |
| 66 | * Name of the form. |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | abstract protected function editName(); |
| 71 | |
| 72 | /** |
| 73 | * UserContext to pop back to. |
| 74 | * |
| 75 | * @param int $mode |
| 76 | * Mode that we are in. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | abstract protected function userContext($mode = NULL); |
| 81 | |
| 82 | /** |
| 83 | * Get userContext params. |
| 84 | * |
| 85 | * @param int $mode |
| 86 | * Mode that we are in. |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | public function userContextParams($mode = NULL) { |
| 91 | return 'reset=1&action=browse'; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Allow objects to be added based on permission. |
| 96 | * |
| 97 | * @param int $id |
| 98 | * The id of the object. |
| 99 | * @param int $name |
| 100 | * The name or title of the object. |
| 101 | * |
| 102 | * @return string |
| 103 | * permission value if permission is granted, else null |
| 104 | */ |
| 105 | public function checkPermission($id, $name) { |
| 106 | return CRM_Core_Permission::EDIT; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Allows the derived class to add some more state variables to |
| 111 | * the controller. By default does nothing, and hence is abstract |
| 112 | * |
| 113 | * @param CRM_Core_Controller $controller |
| 114 | * The controller object. |
| 115 | * |
| 116 | * @return void |
| 117 | */ |
| 118 | public function addValues($controller) { |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Class constructor. |
| 123 | * |
| 124 | * @param string $title |
| 125 | * Title of the page. |
| 126 | * @param int $mode |
| 127 | * Mode of the page. |
| 128 | * |
| 129 | * @return \CRM_Core_Page_Basic |
| 130 | */ |
| 131 | public function __construct($title = NULL, $mode = NULL) { |
| 132 | parent::__construct($title, $mode); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Run the basic page (run essentially starts execution for that page). |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function run() { |
| 141 | // CRM-9034 |
| 142 | // dont see args or pageArgs being used, so we should |
| 143 | // consider eliminating them in a future version |
| 144 | $n = func_num_args(); |
| 145 | $args = ($n > 0) ? func_get_arg(0) : NULL; |
| 146 | $pageArgs = ($n > 1) ? func_get_arg(1) : NULL; |
| 147 | $sort = ($n > 2) ? func_get_arg(2) : NULL; |
| 148 | // what action do we want to perform ? (store it for smarty too.. :) |
| 149 | |
| 150 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); |
| 151 | $this->assign('action', $this->_action); |
| 152 | |
| 153 | // get 'id' if present |
| 154 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); |
| 155 | |
| 156 | require_once str_replace('_', DIRECTORY_SEPARATOR, $this->getBAOName()) . ".php"; |
| 157 | |
| 158 | if ($id) { |
| 159 | if (!$this->checkPermission($id, NULL)) { |
| 160 | CRM_Core_Error::fatal(ts('You do not have permission to make changes to the record')); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if ($this->_action & (CRM_Core_Action::VIEW | |
| 165 | CRM_Core_Action::ADD | |
| 166 | CRM_Core_Action::UPDATE | |
| 167 | CRM_Core_Action::COPY | |
| 168 | CRM_Core_Action::ENABLE | |
| 169 | CRM_Core_Action::DISABLE | |
| 170 | CRM_Core_Action::DELETE |
| 171 | ) |
| 172 | ) { |
| 173 | // use edit form for view, add or update or delete |
| 174 | $this->edit($this->_action, $id); |
| 175 | } |
| 176 | else { |
| 177 | // if no action or browse |
| 178 | $this->browse(NULL, $sort); |
| 179 | } |
| 180 | |
| 181 | return parent::run(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @return string |
| 186 | */ |
| 187 | public function superRun() { |
| 188 | return parent::run(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Browse all entities. |
| 193 | * |
| 194 | * @return void |
| 195 | */ |
| 196 | public function browse() { |
| 197 | $n = func_num_args(); |
| 198 | $action = ($n > 0) ? func_get_arg(0) : NULL; |
| 199 | $sort = ($n > 0) ? func_get_arg(1) : NULL; |
| 200 | $links = &$this->links(); |
| 201 | if ($action == NULL) { |
| 202 | if (!empty($links)) { |
| 203 | $action = array_sum(array_keys($links)); |
| 204 | } |
| 205 | } |
| 206 | if ($action & CRM_Core_Action::DISABLE) { |
| 207 | $action -= CRM_Core_Action::DISABLE; |
| 208 | } |
| 209 | if ($action & CRM_Core_Action::ENABLE) { |
| 210 | $action -= CRM_Core_Action::ENABLE; |
| 211 | } |
| 212 | $baoString = $this->getBAOName(); |
| 213 | $object = new $baoString(); |
| 214 | |
| 215 | $values = array(); |
| 216 | |
| 217 | // lets make sure we get the stuff sorted by name if it exists |
| 218 | $fields = &$object->fields(); |
| 219 | $key = ''; |
| 220 | if (!empty($fields['title'])) { |
| 221 | $key = 'title'; |
| 222 | } |
| 223 | elseif (!empty($fields['label'])) { |
| 224 | $key = 'label'; |
| 225 | } |
| 226 | elseif (!empty($fields['name'])) { |
| 227 | $key = 'name'; |
| 228 | } |
| 229 | |
| 230 | if (trim($sort)) { |
| 231 | $object->orderBy($sort); |
| 232 | } |
| 233 | elseif ($key) { |
| 234 | $object->orderBy($key . ' asc'); |
| 235 | } |
| 236 | |
| 237 | //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form |
| 238 | // this is loaded onto then replace with something like '__' & test |
| 239 | $separator = CRM_Core_DAO::VALUE_SEPARATOR; |
| 240 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator); |
| 241 | // find all objects |
| 242 | $object->find(); |
| 243 | while ($object->fetch()) { |
| 244 | if (!isset($object->mapping_type_id) || |
| 245 | // "1 for Search Builder" |
| 246 | $object->mapping_type_id != 1 |
| 247 | ) { |
| 248 | $permission = CRM_Core_Permission::EDIT; |
| 249 | if ($key) { |
| 250 | $permission = $this->checkPermission($object->id, $object->$key); |
| 251 | } |
| 252 | if ($permission) { |
| 253 | $values[$object->id] = array(); |
| 254 | CRM_Core_DAO::storeValues($object, $values[$object->id]); |
| 255 | |
| 256 | if (is_a($object, 'CRM_Contact_DAO_RelationshipType')) { |
| 257 | $values[$object->id]['contact_type_a_display'] = $contactTypes[$values[$object->id]['contact_type_a']]; |
| 258 | $values[$object->id]['contact_type_b_display'] = $contactTypes[$values[$object->id]['contact_type_b']]; |
| 259 | } |
| 260 | |
| 261 | // populate action links |
| 262 | $this->action($object, $action, $values[$object->id], $links, $permission); |
| 263 | |
| 264 | if (isset($object->mapping_type_id)) { |
| 265 | $mappintTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Mapping', 'mapping_type_id'); |
| 266 | $values[$object->id]['mapping_type'] = $mappintTypes[$object->mapping_type_id]; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | $this->assign('rows', $values); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Given an object, get the actions that can be associated with this |
| 276 | * object. Check the is_active and is_required flags to display valid |
| 277 | * actions |
| 278 | * |
| 279 | * @param CRM_Core_DAO $object |
| 280 | * The object being considered. |
| 281 | * @param int $action |
| 282 | * The base set of actions. |
| 283 | * @param array $values |
| 284 | * The array of values that we send to the template. |
| 285 | * @param array $links |
| 286 | * The array of links. |
| 287 | * @param string $permission |
| 288 | * The permission assigned to this object. |
| 289 | * |
| 290 | * @param bool $forceAction |
| 291 | * |
| 292 | * @return void |
| 293 | */ |
| 294 | public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) { |
| 295 | $values['class'] = ''; |
| 296 | $newAction = $action; |
| 297 | $hasDelete = $hasDisable = TRUE; |
| 298 | |
| 299 | if (!empty($values['name']) && in_array($values['name'], array( |
| 300 | 'encounter_medium', |
| 301 | 'case_type', |
| 302 | 'case_status', |
| 303 | )) |
| 304 | ) { |
| 305 | static $caseCount = NULL; |
| 306 | if (!isset($caseCount)) { |
| 307 | $caseCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE); |
| 308 | } |
| 309 | if ($caseCount > 0) { |
| 310 | $hasDelete = $hasDisable = FALSE; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (!$forceAction) { |
| 315 | if (array_key_exists('is_reserved', $object) && $object->is_reserved) { |
| 316 | $values['class'] = 'reserved'; |
| 317 | // check if object is relationship type |
| 318 | $object_type = get_class($object); |
| 319 | |
| 320 | $exceptions = array( |
| 321 | 'CRM_Contact_BAO_RelationshipType', |
| 322 | 'CRM_Core_BAO_LocationType', |
| 323 | 'CRM_Badge_BAO_Layout', |
| 324 | ); |
| 325 | |
| 326 | if (in_array($object_type, $exceptions)) { |
| 327 | $newAction = CRM_Core_Action::VIEW + CRM_Core_Action::UPDATE; |
| 328 | } |
| 329 | else { |
| 330 | $newAction = 0; |
| 331 | $values['action'] = ''; |
| 332 | return; |
| 333 | } |
| 334 | } |
| 335 | else { |
| 336 | if (array_key_exists('is_active', $object)) { |
| 337 | if ($object->is_active) { |
| 338 | if ($hasDisable) { |
| 339 | $newAction += CRM_Core_Action::DISABLE; |
| 340 | } |
| 341 | } |
| 342 | else { |
| 343 | $newAction += CRM_Core_Action::ENABLE; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | //CRM-4418, handling edit and delete separately. |
| 350 | $permissions = array($permission); |
| 351 | if ($hasDelete && ($permission == CRM_Core_Permission::EDIT)) { |
| 352 | //previously delete was subset of edit |
| 353 | //so for consistency lets grant delete also. |
| 354 | $permissions[] = CRM_Core_Permission::DELETE; |
| 355 | } |
| 356 | |
| 357 | // make sure we only allow those actions that the user is permissioned for |
| 358 | $newAction = $newAction & CRM_Core_Action::mask($permissions); |
| 359 | |
| 360 | $values['action'] = CRM_Core_Action::formLink($links, $newAction, array('id' => $object->id)); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Edit this entity. |
| 365 | * |
| 366 | * @param int $mode |
| 367 | * What mode for the form ?. |
| 368 | * @param int $id |
| 369 | * Id of the entity (for update, view operations). |
| 370 | * |
| 371 | * @param bool $imageUpload |
| 372 | * @param bool $pushUserContext |
| 373 | * |
| 374 | * @return void |
| 375 | */ |
| 376 | public function edit($mode, $id = NULL, $imageUpload = FALSE, $pushUserContext = TRUE) { |
| 377 | $controller = new CRM_Core_Controller_Simple($this->editForm(), |
| 378 | $this->editName(), |
| 379 | $mode, |
| 380 | $imageUpload |
| 381 | ); |
| 382 | |
| 383 | // set the userContext stack |
| 384 | if ($pushUserContext) { |
| 385 | $session = CRM_Core_Session::singleton(); |
| 386 | $session->pushUserContext(CRM_Utils_System::url($this->userContext($mode), $this->userContextParams($mode))); |
| 387 | } |
| 388 | if ($id !== NULL) { |
| 389 | $controller->set('id', $id); |
| 390 | } |
| 391 | $controller->set('BAOName', $this->getBAOName()); |
| 392 | $this->addValues($controller); |
| 393 | $controller->setEmbedded(TRUE); |
| 394 | $controller->process(); |
| 395 | $controller->run(); |
| 396 | } |
| 397 | |
| 398 | } |