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