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