Merge pull request #23960 from seamuslee001/fix_static_calling_trait
[civicrm-core.git] / CRM / Core / Page / Basic.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 16 */
6a488035
TO
17abstract class CRM_Core_Page_Basic extends CRM_Core_Page {
18
19 protected $_action;
20
21 /**
fe482240 22 * Define all the abstract functions here.
6a488035
TO
23 */
24
25 /**
fe482240 26 * Name of the BAO to perform various DB manipulations.
6a488035
TO
27 *
28 * @return string
6a488035 29 */
d5cc0fc2 30 abstract protected function getBAOName();
6a488035
TO
31
32 /**
fe482240 33 * An array of action links.
6a488035 34 *
a6c01b45
CW
35 * @return array
36 * (reference)
6a488035 37 */
d5cc0fc2 38 abstract protected function &links();
6a488035
TO
39
40 /**
fe482240 41 * Name of the edit form class.
6a488035
TO
42 *
43 * @return string
6a488035 44 */
d5cc0fc2 45 abstract protected function editForm();
6a488035
TO
46
47 /**
fe482240 48 * Name of the form.
6a488035
TO
49 *
50 * @return string
6a488035 51 */
d5cc0fc2 52 abstract protected function editName();
6a488035
TO
53
54 /**
fe482240 55 * UserContext to pop back to.
6a488035 56 *
6a0b768e
TO
57 * @param int $mode
58 * Mode that we are in.
6a488035
TO
59 *
60 * @return string
6a488035 61 */
d5cc0fc2 62 abstract protected function userContext($mode = NULL);
6a488035
TO
63
64 /**
fe482240 65 * Get userContext params.
6a488035 66 *
6a0b768e
TO
67 * @param int $mode
68 * Mode that we are in.
6a488035
TO
69 *
70 * @return string
6a488035 71 */
00be9182 72 public function userContextParams($mode = NULL) {
6a488035
TO
73 return 'reset=1&action=browse';
74 }
75
76 /**
fe482240 77 * Allow objects to be added based on permission.
6a488035 78 *
6a0b768e
TO
79 * @param int $id
80 * The id of the object.
81 * @param int $name
82 * The name or title of the object.
6a488035 83 *
a6c01b45
CW
84 * @return string
85 * permission value if permission is granted, else null
6a488035
TO
86 */
87 public function checkPermission($id, $name) {
88 return CRM_Core_Permission::EDIT;
89 }
90
91 /**
100fef9d 92 * Allows the derived class to add some more state variables to
6a488035
TO
93 * the controller. By default does nothing, and hence is abstract
94 *
6a0b768e
TO
95 * @param CRM_Core_Controller $controller
96 * The controller object.
6a488035 97 */
2aa397bc
TO
98 public function addValues($controller) {
99 }
6a488035
TO
100
101 /**
fe482240 102 * Class constructor.
6a488035 103 *
6a0b768e
TO
104 * @param string $title
105 * Title of the page.
106 * @param int $mode
107 * Mode of the page.
6a488035 108 *
fd31fa4c 109 * @return \CRM_Core_Page_Basic
6a488035 110 */
00be9182 111 public function __construct($title = NULL, $mode = NULL) {
6a488035
TO
112 parent::__construct($title, $mode);
113 }
114
115 /**
116 * Run the basic page (run essentially starts execution for that page).
6a488035 117 */
00be9182 118 public function run() {
6a488035 119 // CRM-9034
0b014509 120 // do not see args or pageArgs being used, so we should
6a488035 121 // consider eliminating them in a future version
353ffa53
TO
122 $n = func_num_args();
123 $args = ($n > 0) ? func_get_arg(0) : NULL;
6a488035 124 $pageArgs = ($n > 1) ? func_get_arg(1) : NULL;
353ffa53 125 $sort = ($n > 2) ? func_get_arg(2) : NULL;
6a488035
TO
126 // what action do we want to perform ? (store it for smarty too.. :)
127
5d6d0104 128 $id = $this->getIdAndAction();
6a488035 129
2aa397bc 130 if ($this->_action & (CRM_Core_Action::VIEW |
6a488035
TO
131 CRM_Core_Action::ADD |
132 CRM_Core_Action::UPDATE |
133 CRM_Core_Action::COPY |
134 CRM_Core_Action::ENABLE |
135 CRM_Core_Action::DISABLE |
136 CRM_Core_Action::DELETE
137 )
138 ) {
139 // use edit form for view, add or update or delete
140 $this->edit($this->_action, $id);
141 }
142 else {
143 // if no action or browse
144 $this->browse(NULL, $sort);
145 }
146
147 return parent::run();
148 }
149
5d6d0104
AH
150 /**
151 * Retrieve the action and ID from the request.
152 *
153 * Action is assigned to the template while we're at it. This is pulled from
154 * the `run()` method above.
155 *
156 * @return int
157 * The ID if present, or 0.
c1bd8375 158 * @throws \CRM_Core_Exception
5d6d0104
AH
159 */
160 public function getIdAndAction() {
161 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
162 $this->assign('action', $this->_action);
163
c1bd8375
MWMC
164 $this->assign('selectedChild', CRM_Utils_Request::retrieve('selectedChild', 'Alphanumeric', $this));
165
5d6d0104
AH
166 // get 'id' if present
167 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
168
5d6d0104
AH
169 if ($id) {
170 if (!$this->checkPermission($id, NULL)) {
beb414cc 171 CRM_Core_Error::statusBounce(ts('You do not have permission to make changes to the record'));
5d6d0104
AH
172 }
173 }
174
175 return $id;
176 }
177
a0ee3941
EM
178 /**
179 * @return string
180 */
00be9182 181 public function superRun() {
6a488035
TO
182 return parent::run();
183 }
184
185 /**
100fef9d 186 * Browse all entities.
6a488035 187 */
00be9182 188 public function browse() {
353ffa53 189 $n = func_num_args();
6a488035 190 $action = ($n > 0) ? func_get_arg(0) : NULL;
353ffa53
TO
191 $sort = ($n > 0) ? func_get_arg(1) : NULL;
192 $links = &$this->links();
6a488035
TO
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 }
6a488035 204
844c89d4 205 $this->assign('rows', $this->getRows($sort, $action, $links));
6a488035
TO
206 }
207
208 /**
209 * Given an object, get the actions that can be associated with this
210 * object. Check the is_active and is_required flags to display valid
211 * actions
212 *
6a0b768e
TO
213 * @param CRM_Core_DAO $object
214 * The object being considered.
215 * @param int $action
216 * The base set of actions.
217 * @param array $values
218 * The array of values that we send to the template.
219 * @param array $links
220 * The array of links.
221 * @param string $permission
222 * The permission assigned to this object.
da6b46f4
EM
223 *
224 * @param bool $forceAction
6a488035 225 */
00be9182 226 public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
6a488035 227 $values['class'] = '';
353ffa53
TO
228 $newAction = $action;
229 $hasDelete = $hasDisable = TRUE;
6a488035 230
be2fb01f 231 if (!empty($values['name']) && in_array($values['name'], [
5d6d0104
AH
232 'encounter_medium',
233 'case_type',
234 'case_status',
be2fb01f 235 ])) {
6a488035
TO
236 static $caseCount = NULL;
237 if (!isset($caseCount)) {
238 $caseCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE);
239 }
240 if ($caseCount > 0) {
241 $hasDelete = $hasDisable = FALSE;
242 }
243 }
244
81eb94b5
AF
245 $object_type = get_class($object);
246
6a488035 247 if (!$forceAction) {
44e54f47 248 if (property_exists($object, 'is_reserved') && $object->is_reserved) {
6a488035
TO
249 $values['class'] = 'reserved';
250 // check if object is relationship type
d2e138ee 251
be2fb01f 252 $exceptions = [
d2e138ee
KJ
253 'CRM_Contact_BAO_RelationshipType',
254 'CRM_Core_BAO_LocationType',
255 'CRM_Badge_BAO_Layout',
be2fb01f 256 ];
d2e138ee
KJ
257
258 if (in_array($object_type, $exceptions)) {
6a488035
TO
259 $newAction = CRM_Core_Action::VIEW + CRM_Core_Action::UPDATE;
260 }
261 else {
262 $newAction = 0;
263 $values['action'] = '';
264 return;
265 }
266 }
267 else {
44e54f47 268 if (property_exists($object, 'is_active')) {
6a488035
TO
269 if ($object->is_active) {
270 if ($hasDisable) {
271 $newAction += CRM_Core_Action::DISABLE;
272 }
273 }
274 else {
275 $newAction += CRM_Core_Action::ENABLE;
276 }
277 }
278 }
279 }
280
281 //CRM-4418, handling edit and delete separately.
be2fb01f 282 $permissions = [$permission];
6a488035
TO
283 if ($hasDelete && ($permission == CRM_Core_Permission::EDIT)) {
284 //previously delete was subset of edit
285 //so for consistency lets grant delete also.
286 $permissions[] = CRM_Core_Permission::DELETE;
287 }
288
289 // make sure we only allow those actions that the user is permissioned for
290 $newAction = $newAction & CRM_Core_Action::mask($permissions);
291
81eb94b5
AF
292 $values['action'] = CRM_Core_Action::formLink(
293 $links,
294 $newAction,
295 ['id' => $object->id],
296 'more',
297 FALSE,
298 "basic.$object_type.page",
299 $object_type,
300 $object->id
301 );
6a488035
TO
302 }
303
304 /**
305 * Edit this entity.
306 *
6a0b768e
TO
307 * @param int $mode
308 * What mode for the form ?.
309 * @param int $id
310 * Id of the entity (for update, view operations).
6a488035 311 *
77b97be7
EM
312 * @param bool $imageUpload
313 * @param bool $pushUserContext
6a488035 314 */
00be9182 315 public function edit($mode, $id = NULL, $imageUpload = FALSE, $pushUserContext = TRUE) {
6a488035
TO
316 $controller = new CRM_Core_Controller_Simple($this->editForm(),
317 $this->editName(),
318 $mode,
319 $imageUpload
320 );
321
322 // set the userContext stack
323 if ($pushUserContext) {
324 $session = CRM_Core_Session::singleton();
325 $session->pushUserContext(CRM_Utils_System::url($this->userContext($mode), $this->userContextParams($mode)));
326 }
327 if ($id !== NULL) {
328 $controller->set('id', $id);
329 }
330 $controller->set('BAOName', $this->getBAOName());
331 $this->addValues($controller);
332 $controller->setEmbedded(TRUE);
333 $controller->process();
334 $controller->run();
335 }
96025800 336
844c89d4
EM
337 /**
338 * @param $sort
339 * @param $action
340 * @param array $links
341 *
342 * @return array
343 */
344 protected function getRows($sort, $action, array $links): array {
345 $baoString = $this->getBAOName();
346 $object = new $baoString();
347
348 $values = [];
349
350 // lets make sure we get the stuff sorted by name if it exists
351 $fields = &$object->fields();
352 $key = '';
353 if (!empty($fields['title'])) {
354 $key = 'title';
355 }
356 elseif (!empty($fields['label'])) {
357 $key = 'label';
358 }
359 elseif (!empty($fields['name'])) {
360 $key = 'name';
361 }
362
363 if (trim($sort)) {
364 $object->orderBy($sort);
365 }
366 elseif ($key) {
367 $object->orderBy($key . ' asc');
368 }
369
370 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE);
371 // find all objects
372 $object->find();
373 while ($object->fetch()) {
374 if (!isset($object->mapping_type_id) ||
375 // "1 for Search Builder"
376 $object->mapping_type_id != 1
377 ) {
378 $permission = CRM_Core_Permission::EDIT;
379 if ($key) {
380 $permission = $this->checkPermission($object->id, $object->$key);
381 }
382 if ($permission) {
383 $values[$object->id] = [];
384 CRM_Core_DAO::storeValues($object, $values[$object->id]);
385
386 if (is_a($object, 'CRM_Contact_DAO_RelationshipType')) {
387 if (isset($values[$object->id]['contact_type_a'])) {
388 $values[$object->id]['contact_type_a_display'] = $contactTypes[$values[$object->id]['contact_type_a']];
389 }
390 if (isset($values[$object->id]['contact_type_b'])) {
391 $values[$object->id]['contact_type_b_display'] = $contactTypes[$values[$object->id]['contact_type_b']];
392 }
393 }
394
395 // populate action links
396 $this->action($object, $action, $values[$object->id], $links, $permission);
397
398 if (isset($object->mapping_type_id)) {
399 $mappintTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Mapping', 'mapping_type_id');
400 $values[$object->id]['mapping_type'] = $mappintTypes[$object->mapping_type_id];
401 }
402 }
403 }
404 }
7c9e1b3b
EM
405 foreach ($this->getExpectedRowProperties() as $key) {
406 foreach ($values as $index => $value) {
407 if (!array_key_exists($key, $value)) {
408 $values[$index][$key] = NULL;
409 }
410 }
411 }
844c89d4
EM
412 return $values;
413 }
414
7c9e1b3b
EM
415 /**
416 * Get any properties that should always be present in each row (null if no value).
417 *
418 * @return array
419 */
420 protected function getExpectedRowProperties(): array {
421 return [];
422 }
423
6a488035 424}