3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 abstract class CRM_Core_Page_Basic
extends CRM_Core_Page
{
22 * Define all the abstract functions here.
26 * Name of the BAO to perform various DB manipulations.
30 abstract protected function getBAOName();
33 * An array of action links.
38 abstract protected function &links();
41 * Name of the edit form class.
45 abstract protected function editForm();
52 abstract protected function editName();
55 * UserContext to pop back to.
58 * Mode that we are in.
62 abstract protected function userContext($mode = NULL);
65 * Get userContext params.
68 * Mode that we are in.
72 public function userContextParams($mode = NULL) {
73 return 'reset=1&action=browse';
77 * Allow objects to be added based on permission.
80 * The id of the object.
82 * The name or title of the object.
85 * permission value if permission is granted, else null
87 public function checkPermission($id, $name) {
88 return CRM_Core_Permission
::EDIT
;
92 * Allows the derived class to add some more state variables to
93 * the controller. By default does nothing, and hence is abstract
95 * @param CRM_Core_Controller $controller
96 * The controller object.
98 public function addValues($controller) {
104 * @param string $title
109 * @return \CRM_Core_Page_Basic
111 public function __construct($title = NULL, $mode = NULL) {
112 parent
::__construct($title, $mode);
116 * Run the basic page (run essentially starts execution for that page).
118 public function run() {
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.. :)
128 $id = $this->getIdAndAction();
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
139 // use edit form for view, add or update or delete
140 $this->edit($this->_action
, $id);
143 // if no action or browse
144 $this->browse(NULL, $sort);
147 return parent
::run();
151 * Retrieve the action and ID from the request.
153 * Action is assigned to the template while we're at it. This is pulled from
154 * the `run()` method above.
157 * The ID if present, or 0.
158 * @throws \CRM_Core_Exception
160 public function getIdAndAction() {
161 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
162 $this->assign('action', $this->_action
);
164 $this->assign('selectedChild', CRM_Utils_Request
::retrieve('selectedChild', 'Alphanumeric', $this));
166 // get 'id' if present
167 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, 0);
170 if (!$this->checkPermission($id, NULL)) {
171 CRM_Core_Error
::statusBounce(ts('You do not have permission to make changes to the record'));
181 public function superRun() {
182 return parent
::run();
186 * Browse all entities.
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));
198 if ($action & CRM_Core_Action
::DISABLE
) {
199 $action -= CRM_Core_Action
::DISABLE
;
201 if ($action & CRM_Core_Action
::ENABLE
) {
202 $action -= CRM_Core_Action
::ENABLE
;
205 $this->assign('rows', $this->getRows($sort, $action, $links));
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
213 * @param CRM_Core_DAO $object
214 * The object being considered.
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.
224 * @param bool $forceAction
226 public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
227 $values['class'] = '';
228 $newAction = $action;
229 $hasDelete = $hasDisable = TRUE;
231 if (!empty($values['name']) && in_array($values['name'], [
236 static $caseCount = NULL;
237 if (!isset($caseCount)) {
238 $caseCount = CRM_Case_BAO_Case
::caseCount(NULL, FALSE);
240 if ($caseCount > 0) {
241 $hasDelete = $hasDisable = FALSE;
245 $object_type = get_class($object);
248 if (property_exists($object, 'is_reserved') && $object->is_reserved
) {
249 $values['class'] = 'reserved';
250 // check if object is relationship type
253 'CRM_Contact_BAO_RelationshipType',
254 'CRM_Core_BAO_LocationType',
255 'CRM_Badge_BAO_Layout',
258 if (in_array($object_type, $exceptions)) {
259 $newAction = CRM_Core_Action
::VIEW + CRM_Core_Action
::UPDATE
;
263 $values['action'] = '';
268 if (property_exists($object, 'is_active')) {
269 if ($object->is_active
) {
271 $newAction +
= CRM_Core_Action
::DISABLE
;
275 $newAction +
= CRM_Core_Action
::ENABLE
;
281 //CRM-4418, handling edit and delete separately.
282 $permissions = [$permission];
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
;
289 // make sure we only allow those actions that the user is permissioned for
290 $newAction = $newAction & CRM_Core_Action
::mask($permissions);
292 $values['action'] = CRM_Core_Action
::formLink(
295 ['id' => $object->id
],
298 "basic.$object_type.page",
308 * What mode for the form ?.
310 * Id of the entity (for update, view operations).
312 * @param bool $imageUpload
313 * @param bool $pushUserContext
315 public function edit($mode, $id = NULL, $imageUpload = FALSE, $pushUserContext = TRUE) {
316 $controller = new CRM_Core_Controller_Simple($this->editForm(),
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)));
328 $controller->set('id', $id);
330 $controller->set('BAOName', $this->getBAOName());
331 $this->addValues($controller);
332 $controller->setEmbedded(TRUE);
333 $controller->process();
340 * @param array $links
344 protected function getRows($sort, $action, array $links): array {
345 $baoString = $this->getBAOName();
346 $object = new $baoString();
350 // lets make sure we get the stuff sorted by name if it exists
351 $fields = &$object->fields();
353 if (!empty($fields['title'])) {
356 elseif (!empty($fields['label'])) {
359 elseif (!empty($fields['name'])) {
364 $object->orderBy($sort);
367 $object->orderBy($key . ' asc');
370 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
373 while ($object->fetch()) {
374 if (!isset($object->mapping_type_id
) ||
375 // "1 for Search Builder"
376 $object->mapping_type_id
!= 1
378 $permission = CRM_Core_Permission
::EDIT
;
380 $permission = $this->checkPermission($object->id
, $object->$key);
383 $values[$object->id
] = [];
384 CRM_Core_DAO
::storeValues($object, $values[$object->id
]);
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']];
390 if (isset($values[$object->id
]['contact_type_b'])) {
391 $values[$object->id
]['contact_type_b_display'] = $contactTypes[$values[$object->id
]['contact_type_b']];
395 // populate action links
396 $this->action($object, $action, $values[$object->id
], $links, $permission);
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
];
405 foreach ($this->getExpectedRowProperties() as $key) {
406 foreach ($values as $index => $value) {
407 if (!array_key_exists($key, $value)) {
408 $values[$index][$key] = NULL;
416 * Get any properties that should always be present in each row (null if no value).
420 protected function getExpectedRowProperties(): array {