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