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