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