Merge pull request #6635 from eileenmcnaughton/comments
[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 32 */
6a488035
TO
33abstract 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
144 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
145 $this->assign('action', $this->_action);
146
147 // get 'id' if present
148 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
149
2aa397bc 150 require_once str_replace('_', DIRECTORY_SEPARATOR, $this->getBAOName()) . ".php";
6a488035
TO
151
152 if ($id) {
153 if (!$this->checkPermission($id, NULL)) {
154 CRM_Core_Error::fatal(ts('You do not have permission to make changes to the record'));
155 }
156 }
157
2aa397bc 158 if ($this->_action & (CRM_Core_Action::VIEW |
6a488035
TO
159 CRM_Core_Action::ADD |
160 CRM_Core_Action::UPDATE |
161 CRM_Core_Action::COPY |
162 CRM_Core_Action::ENABLE |
163 CRM_Core_Action::DISABLE |
164 CRM_Core_Action::DELETE
165 )
166 ) {
167 // use edit form for view, add or update or delete
168 $this->edit($this->_action, $id);
169 }
170 else {
171 // if no action or browse
172 $this->browse(NULL, $sort);
173 }
174
175 return parent::run();
176 }
177
a0ee3941
EM
178 /**
179 * @return string
180 */
00be9182 181 public function superRun() {
6a488035
TO
182 return parent::run();
183 }
184
185 /**
100fef9d 186 * Browse all entities.
6a488035 187 */
00be9182 188 public function browse() {
353ffa53 189 $n = func_num_args();
6a488035 190 $action = ($n > 0) ? func_get_arg(0) : NULL;
353ffa53
TO
191 $sort = ($n > 0) ? func_get_arg(1) : NULL;
192 $links = &$this->links();
6a488035
TO
193 if ($action == NULL) {
194 if (!empty($links)) {
195 $action = array_sum(array_keys($links));
196 }
197 }
198 if ($action & CRM_Core_Action::DISABLE) {
199 $action -= CRM_Core_Action::DISABLE;
200 }
201 if ($action & CRM_Core_Action::ENABLE) {
202 $action -= CRM_Core_Action::ENABLE;
203 }
4d5c2eb5 204 $baoString = $this->getBAOName();
205 $object = new $baoString();
6a488035
TO
206
207 $values = array();
208
d424ffde 209 // lets make sure we get the stuff sorted by name if it exists
6a488035
TO
210 $fields = &$object->fields();
211 $key = '';
a7488080 212 if (!empty($fields['title'])) {
6a488035
TO
213 $key = 'title';
214 }
a7488080 215 elseif (!empty($fields['label'])) {
6a488035
TO
216 $key = 'label';
217 }
a7488080 218 elseif (!empty($fields['name'])) {
6a488035
TO
219 $key = 'name';
220 }
221
222 if (trim($sort)) {
223 $object->orderBy($sort);
224 }
225 elseif ($key) {
226 $object->orderBy($key . ' asc');
227 }
228
b500fbea
EM
229 //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form
230 // this is loaded onto then replace with something like '__' & test
231 $separator = CRM_Core_DAO::VALUE_SEPARATOR;
232 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator);
6a488035
TO
233 // find all objects
234 $object->find();
235 while ($object->fetch()) {
236 if (!isset($object->mapping_type_id) ||
237 // "1 for Search Builder"
238 $object->mapping_type_id != 1
239 ) {
240 $permission = CRM_Core_Permission::EDIT;
241 if ($key) {
242 $permission = $this->checkPermission($object->id, $object->$key);
243 }
244 if ($permission) {
245 $values[$object->id] = array();
246 CRM_Core_DAO::storeValues($object, $values[$object->id]);
247
77d0b1f8 248 if (is_a($object, 'CRM_Contact_DAO_RelationshipType')) {
249 $values[$object->id]['contact_type_a_display'] = $contactTypes[$values[$object->id]['contact_type_a']];
250 $values[$object->id]['contact_type_b_display'] = $contactTypes[$values[$object->id]['contact_type_b']];
251 }
6a488035
TO
252
253 // populate action links
254 $this->action($object, $action, $values[$object->id], $links, $permission);
255
256 if (isset($object->mapping_type_id)) {
cbf48754 257 $mappintTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Mapping', 'mapping_type_id');
6a488035
TO
258 $values[$object->id]['mapping_type'] = $mappintTypes[$object->mapping_type_id];
259 }
260 }
261 }
262 }
263 $this->assign('rows', $values);
264 }
265
266 /**
267 * Given an object, get the actions that can be associated with this
268 * object. Check the is_active and is_required flags to display valid
269 * actions
270 *
6a0b768e
TO
271 * @param CRM_Core_DAO $object
272 * The object being considered.
273 * @param int $action
274 * The base set of actions.
275 * @param array $values
276 * The array of values that we send to the template.
277 * @param array $links
278 * The array of links.
279 * @param string $permission
280 * The permission assigned to this object.
da6b46f4
EM
281 *
282 * @param bool $forceAction
6a488035 283 */
00be9182 284 public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
6a488035 285 $values['class'] = '';
353ffa53
TO
286 $newAction = $action;
287 $hasDelete = $hasDisable = TRUE;
6a488035 288
a7488080 289 if (!empty($values['name']) && in_array($values['name'], array(
353ffa53
TO
290 'encounter_medium',
291 'case_type',
d5cc0fc2 292 'case_status',
353ffa53
TO
293 ))
294 ) {
6a488035
TO
295 static $caseCount = NULL;
296 if (!isset($caseCount)) {
297 $caseCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE);
298 }
299 if ($caseCount > 0) {
300 $hasDelete = $hasDisable = FALSE;
301 }
302 }
303
304 if (!$forceAction) {
305 if (array_key_exists('is_reserved', $object) && $object->is_reserved) {
306 $values['class'] = 'reserved';
307 // check if object is relationship type
308 $object_type = get_class($object);
d2e138ee
KJ
309
310 $exceptions = array(
311 'CRM_Contact_BAO_RelationshipType',
312 'CRM_Core_BAO_LocationType',
313 'CRM_Badge_BAO_Layout',
314 );
315
316 if (in_array($object_type, $exceptions)) {
6a488035
TO
317 $newAction = CRM_Core_Action::VIEW + CRM_Core_Action::UPDATE;
318 }
319 else {
320 $newAction = 0;
321 $values['action'] = '';
322 return;
323 }
324 }
325 else {
326 if (array_key_exists('is_active', $object)) {
327 if ($object->is_active) {
328 if ($hasDisable) {
329 $newAction += CRM_Core_Action::DISABLE;
330 }
331 }
332 else {
333 $newAction += CRM_Core_Action::ENABLE;
334 }
335 }
336 }
337 }
338
339 //CRM-4418, handling edit and delete separately.
340 $permissions = array($permission);
341 if ($hasDelete && ($permission == CRM_Core_Permission::EDIT)) {
342 //previously delete was subset of edit
343 //so for consistency lets grant delete also.
344 $permissions[] = CRM_Core_Permission::DELETE;
345 }
346
347 // make sure we only allow those actions that the user is permissioned for
348 $newAction = $newAction & CRM_Core_Action::mask($permissions);
349
350 $values['action'] = CRM_Core_Action::formLink($links, $newAction, array('id' => $object->id));
351 }
352
353 /**
354 * Edit this entity.
355 *
6a0b768e
TO
356 * @param int $mode
357 * What mode for the form ?.
358 * @param int $id
359 * Id of the entity (for update, view operations).
6a488035 360 *
77b97be7
EM
361 * @param bool $imageUpload
362 * @param bool $pushUserContext
6a488035 363 */
00be9182 364 public function edit($mode, $id = NULL, $imageUpload = FALSE, $pushUserContext = TRUE) {
6a488035
TO
365 $controller = new CRM_Core_Controller_Simple($this->editForm(),
366 $this->editName(),
367 $mode,
368 $imageUpload
369 );
370
371 // set the userContext stack
372 if ($pushUserContext) {
373 $session = CRM_Core_Session::singleton();
374 $session->pushUserContext(CRM_Utils_System::url($this->userContext($mode), $this->userContextParams($mode)));
375 }
376 if ($id !== NULL) {
377 $controller->set('id', $id);
378 }
379 $controller->set('BAOName', $this->getBAOName());
380 $this->addValues($controller);
381 $controller->setEmbedded(TRUE);
382 $controller->process();
383 $controller->run();
384 }
96025800 385
6a488035 386}