Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | */ | |
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 | * @access public | |
52 | */ | |
53 | ||
54 | abstract function getBAOName(); | |
55 | ||
56 | /** | |
57 | * an array of action links | |
58 | * | |
59 | * @return array (reference) | |
60 | * @access public | |
61 | */ | |
62 | abstract function &links(); | |
63 | ||
64 | /** | |
65 | * name of the edit form class | |
66 | * | |
67 | * @return string | |
68 | * @access public | |
69 | */ | |
70 | abstract function editForm(); | |
71 | ||
72 | /** | |
73 | * name of the form | |
74 | * | |
75 | * @return string | |
76 | * @access public | |
77 | */ | |
78 | abstract function editName(); | |
79 | ||
80 | /** | |
81 | * userContext to pop back to | |
82 | * | |
83 | * @param int $mode mode that we are in | |
84 | * | |
85 | * @return string | |
86 | * @access public | |
87 | */ | |
88 | abstract function userContext($mode = NULL); | |
89 | ||
90 | /** | |
91 | * function to get userContext params | |
92 | * | |
93 | * @param int $mode mode that we are in | |
94 | * | |
95 | * @return string | |
96 | * @access public | |
97 | */ | |
98 | function userContextParams($mode = NULL) { | |
99 | return 'reset=1&action=browse'; | |
100 | } | |
101 | ||
102 | /** | |
103 | * allow objects to be added based on permission | |
104 | * | |
105 | * @param int $id the id of the object | |
106 | * @param int $name the name or title of the object | |
107 | * | |
108 | * @return string permission value if permission is granted, else null | |
109 | * @access public | |
110 | */ | |
111 | public function checkPermission($id, $name) { | |
112 | return CRM_Core_Permission::EDIT; | |
113 | } | |
114 | ||
115 | /** | |
116 | * allows the derived class to add some more state variables to | |
117 | * the controller. By default does nothing, and hence is abstract | |
118 | * | |
119 | * @param CRM_Core_Controller $controller the controller object | |
120 | * | |
121 | * @return void | |
122 | * @access public | |
123 | */ | |
124 | function addValues($controller) {} | |
125 | ||
126 | /** | |
127 | * class constructor | |
128 | * | |
129 | * @param string $title title of the page | |
fd31fa4c | 130 | * @param int $mode mode of the page |
6a488035 | 131 | * |
fd31fa4c | 132 | * @return \CRM_Core_Page_Basic |
6a488035 TO |
133 | */ |
134 | 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 | 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 & | |
168 | (CRM_Core_Action::VIEW | | |
169 | CRM_Core_Action::ADD | | |
170 | CRM_Core_Action::UPDATE | | |
171 | CRM_Core_Action::COPY | | |
172 | CRM_Core_Action::ENABLE | | |
173 | CRM_Core_Action::DISABLE | | |
174 | CRM_Core_Action::DELETE | |
175 | ) | |
176 | ) { | |
177 | // use edit form for view, add or update or delete | |
178 | $this->edit($this->_action, $id); | |
179 | } | |
180 | else { | |
181 | // if no action or browse | |
182 | $this->browse(NULL, $sort); | |
183 | } | |
184 | ||
185 | return parent::run(); | |
186 | } | |
187 | ||
a0ee3941 EM |
188 | /** |
189 | * @return string | |
190 | */ | |
6a488035 TO |
191 | function superRun() { |
192 | return parent::run(); | |
193 | } | |
194 | ||
195 | /** | |
196 | * browse all entities. | |
197 | * | |
da6b46f4 | 198 | * @internal param int $action |
6a488035 TO |
199 | * |
200 | * @return void | |
201 | * @access public | |
202 | */ | |
203 | 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 | } | |
4d5c2eb5 | 219 | $baoString = $this->getBAOName(); |
220 | $object = new $baoString(); | |
6a488035 TO |
221 | |
222 | $values = array(); | |
223 | ||
224 | /* | |
225 | * lets make sure we get the stuff sorted by name if it exists | |
226 | */ | |
227 | ||
228 | $fields = &$object->fields(); | |
229 | $key = ''; | |
a7488080 | 230 | if (!empty($fields['title'])) { |
6a488035 TO |
231 | $key = 'title'; |
232 | } | |
a7488080 | 233 | elseif (!empty($fields['label'])) { |
6a488035 TO |
234 | $key = 'label'; |
235 | } | |
a7488080 | 236 | elseif (!empty($fields['name'])) { |
6a488035 TO |
237 | $key = 'name'; |
238 | } | |
239 | ||
240 | if (trim($sort)) { | |
241 | $object->orderBy($sort); | |
242 | } | |
243 | elseif ($key) { | |
244 | $object->orderBy($key . ' asc'); | |
245 | } | |
246 | ||
b500fbea EM |
247 | //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form |
248 | // this is loaded onto then replace with something like '__' & test | |
249 | $separator = CRM_Core_DAO::VALUE_SEPARATOR; | |
250 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator); | |
6a488035 TO |
251 | // find all objects |
252 | $object->find(); | |
253 | while ($object->fetch()) { | |
254 | if (!isset($object->mapping_type_id) || | |
255 | // "1 for Search Builder" | |
256 | $object->mapping_type_id != 1 | |
257 | ) { | |
258 | $permission = CRM_Core_Permission::EDIT; | |
259 | if ($key) { | |
260 | $permission = $this->checkPermission($object->id, $object->$key); | |
261 | } | |
262 | if ($permission) { | |
263 | $values[$object->id] = array(); | |
264 | CRM_Core_DAO::storeValues($object, $values[$object->id]); | |
265 | ||
77d0b1f8 | 266 | if (is_a($object, 'CRM_Contact_DAO_RelationshipType')) { |
267 | $values[$object->id]['contact_type_a_display'] = $contactTypes[$values[$object->id]['contact_type_a']]; | |
268 | $values[$object->id]['contact_type_b_display'] = $contactTypes[$values[$object->id]['contact_type_b']]; | |
269 | } | |
6a488035 TO |
270 | |
271 | // populate action links | |
272 | $this->action($object, $action, $values[$object->id], $links, $permission); | |
273 | ||
274 | if (isset($object->mapping_type_id)) { | |
cbf48754 | 275 | $mappintTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Mapping', 'mapping_type_id'); |
6a488035 TO |
276 | $values[$object->id]['mapping_type'] = $mappintTypes[$object->mapping_type_id]; |
277 | } | |
278 | } | |
279 | } | |
280 | } | |
281 | $this->assign('rows', $values); | |
282 | } | |
283 | ||
284 | /** | |
285 | * Given an object, get the actions that can be associated with this | |
286 | * object. Check the is_active and is_required flags to display valid | |
287 | * actions | |
288 | * | |
289 | * @param CRM_Core_DAO $object the object being considered | |
da6b46f4 EM |
290 | * @param int $action the base set of actions |
291 | * @param array $values the array of values that we send to the template | |
292 | * @param array $links the array of links | |
293 | * @param string $permission the permission assigned to this object | |
294 | * | |
295 | * @param bool $forceAction | |
6a488035 TO |
296 | * |
297 | * @return void | |
298 | * @access private | |
299 | */ | |
300 | function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) { | |
301 | $values['class'] = ''; | |
302 | $newAction = $action; | |
303 | $hasDelete = $hasDisable = TRUE; | |
304 | ||
a7488080 | 305 | if (!empty($values['name']) && in_array($values['name'], array( |
6a488035 TO |
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); | |
d2e138ee KJ |
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)) { | |
6a488035 TO |
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 - what mode for the form ? | |
369 | * @param int $id - id of the entity (for update, view operations) | |
370 | * | |
77b97be7 EM |
371 | * @param bool $imageUpload |
372 | * @param bool $pushUserContext | |
373 | * | |
6a488035 TO |
374 | * @return void |
375 | */ | |
376 | function edit($mode, $id = NULL, $imageUpload = FALSE, $pushUserContext = TRUE) { | |
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 | } | |
397 | } | |
398 |