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