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