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