send most action links thru hook_civicrm_links
[civicrm-core.git] / CRM / Custom / Page / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * Create a page for displaying Custom Sets.
38 *
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
41 * page.
42 *
43 */
44 class CRM_Custom_Page_Group extends CRM_Core_Page {
45
46 /**
47 * The action links that we need to display for the browse screen
48 *
49 * @var array
50 */
51 private static $_actionLinks;
52
53 /**
54 * Get the action links for this page.
55 *
56 * @param null
57 *
58 * @return array array of action links that we need to display for the browse screen
59 * @access public
60 */
61 function &actionLinks() {
62 // check if variable _actionsLinks is populated
63 if (!isset(self::$_actionLinks)) {
64 self::$_actionLinks = array(
65 CRM_Core_Action::BROWSE => array(
66 'name' => ts('View and Edit Custom Fields'),
67 'url' => 'civicrm/admin/custom/group/field',
68 'qs' => 'reset=1&action=browse&gid=%%id%%',
69 'title' => ts('View and Edit Custom Fields'),
70 ),
71 CRM_Core_Action::PREVIEW => array(
72 'name' => ts('Preview'),
73 'url' => 'civicrm/admin/custom/group',
74 'qs' => 'action=preview&reset=1&id=%%id%%',
75 'title' => ts('Preview Custom Data Set'),
76 ),
77 CRM_Core_Action::UPDATE => array(
78 'name' => ts('Settings'),
79 'url' => 'civicrm/admin/custom/group',
80 'qs' => 'action=update&reset=1&id=%%id%%',
81 'title' => ts('Edit Custom Set'),
82 ),
83 CRM_Core_Action::DISABLE => array(
84 'name' => ts('Disable'),
85 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_CustomGroup' . '\',\'' . 'enable-disable' . '\' );"',
86 'ref' => 'disable-action',
87 'title' => ts('Disable Custom Set'),
88 ),
89 CRM_Core_Action::ENABLE => array(
90 'name' => ts('Enable'),
91 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_CustomGroup' . '\',\'' . 'disable-enable' . '\' );"',
92 'ref' => 'enable-action',
93 'title' => ts('Enable Custom Set'),
94 ),
95 CRM_Core_Action::DELETE => array(
96 'name' => ts('Delete'),
97 'url' => 'civicrm/admin/custom/group',
98 'qs' => 'action=delete&reset=1&id=%%id%%',
99 'title' => ts('Delete Custom Set'),
100 ),
101 );
102 }
103 return self::$_actionLinks;
104 }
105
106 /**
107 * Run the page.
108 *
109 * This method is called after the page is created. It checks for the
110 * type of action and executes that action.
111 * Finally it calls the parent's run method.
112 *
113 * @param null
114 *
115 * @return void
116 * @access public
117 *
118 */
119 function run() {
120 // get the requested action
121 $action = CRM_Utils_Request::retrieve('action', 'String',
122 // default to 'browse'
123 $this, FALSE, 'browse'
124 );
125
126 if ($action & CRM_Core_Action::DELETE) {
127 $session = CRM_Core_Session::singleton();
128 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/', 'action=browse'));
129 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteGroup', "Delete Cutom Set", NULL);
130 $id = CRM_Utils_Request::retrieve('id', 'Positive',
131 $this, FALSE, 0
132 );
133 $controller->set('id', $id);
134 $controller->setEmbedded(TRUE);
135 $controller->process();
136 $controller->run();
137 }
138 // assign vars to templates
139 $this->assign('action', $action);
140 $id = CRM_Utils_Request::retrieve('id', 'Positive',
141 $this, FALSE, 0
142 );
143
144 // what action to take ?
145 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
146 $this->edit($id, $action);
147 }
148 elseif ($action & CRM_Core_Action::PREVIEW) {
149 $this->preview($id);
150 }
151 else {
152 // finally browse the custom groups
153 $this->browse();
154 }
155 // parent run
156 return parent::run();
157 }
158
159 /**
160 * edit custom group
161 *
162 * @param int $id custom group id
163 * @param string $action the action to be invoked
164 *
165 * @return void
166 * @access public
167 */
168 function edit($id, $action) {
169 // create a simple controller for editing custom data
170 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Group', ts('Custom Set'), $action);
171
172 // set the userContext stack
173 $session = CRM_Core_Session::singleton();
174 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/', 'action=browse'));
175 $controller->set('id', $id);
176 $controller->setEmbedded(TRUE);
177 $controller->process();
178 $controller->run();
179 }
180
181 /**
182 * Preview custom group
183 *
184 * @param int $id custom group id
185 *
186 * @return void
187 * @access public
188 */
189 function preview($id) {
190 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), NULL);
191 $session = CRM_Core_Session::singleton();
192 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group', 'action=browse'));
193 $controller->set('groupId', $id);
194 $controller->setEmbedded(TRUE);
195 $controller->process();
196 $controller->run();
197 }
198
199 /**
200 * Browse all custom data groups.
201 *
202 * @param string $action the action to be invoked
203 *
204 * @return void
205 * @access public
206 */
207 function browse($action = NULL) {
208 // get all custom groups sorted by weight
209 $customGroup = array();
210 $dao = new CRM_Core_DAO_CustomGroup();
211 $dao->is_reserved = FALSE;
212 $dao->orderBy('weight, title');
213 $dao->find();
214
215 while ($dao->fetch()) {
216 $customGroup[$dao->id] = array();
217 CRM_Core_DAO::storeValues($dao, $customGroup[$dao->id]);
218 // form all action links
219 $action = array_sum(array_keys($this->actionLinks()));
220
221 // update enable/disable links depending on custom_group properties.
222 if ($dao->is_active) {
223 $action -= CRM_Core_Action::ENABLE;
224 }
225 else {
226 $action -= CRM_Core_Action::DISABLE;
227 }
228 $customGroup[$dao->id]['order'] = $customGroup[$dao->id]['weight'];
229 $customGroup[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
230 array('id' => $dao->id),
231 ts('more'),
232 FALSE,
233 'customGroup.row.actions',
234 'CustomGroup',
235 $dao->id
236 );
237 }
238
239 $customGroupExtends = CRM_Core_SelectValues::customGroupExtends();
240 foreach ($customGroup as $key => $array) {
241 CRM_Core_DAO_CustomGroup::addDisplayEnums($customGroup[$key]);
242 $customGroup[$key]['extends_display'] = $customGroupExtends[$customGroup[$key]['extends']];
243 }
244
245 //fix for Displaying subTypes
246 $subTypes = array();
247
248 $subTypes['Activity'] = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
249 $subTypes['Contribution'] = CRM_Contribute_PseudoConstant::financialType( );
250 $subTypes['Membership'] = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
251 $subTypes['Event'] = CRM_Core_OptionGroup::values('event_type');
252 $subTypes['Grant'] = CRM_Core_OptionGroup::values('grant_type');
253 $subTypes['Campaign'] = CRM_Campaign_PseudoConstant::campaignType();
254 $subTypes['Participant'] = array();
255 $subTypes['ParticipantRole'] = CRM_Core_OptionGroup::values('participant_role');;
256 $subTypes['ParticipantEventName'] = CRM_Event_PseudoConstant::event();
257 $subTypes['ParticipantEventType'] = CRM_Core_OptionGroup::values('event_type');
258 $subTypes['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL);
259 $subTypes['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL);
260 $subTypes['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL);
261
262
263 $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Individual');
264 $relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Organization');
265 $relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Household');
266
267 $allRelationshipType = array();
268 $allRelationshipType = array_merge($relTypeInd, $relTypeOrg);
269 $allRelationshipType = array_merge($allRelationshipType, $relTypeHou);
270
271 //adding subtype specific relationships CRM-5256
272 $relSubType = CRM_Contact_BAO_ContactType::subTypeInfo();
273 foreach ($relSubType as $subType => $val) {
274 $subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
275 FALSE, 'label', TRUE, $subType
276 );
277 $allRelationshipType = array_merge($allRelationshipType, $subTypeRelationshipTypes);
278 }
279
280 $subTypes['Relationship'] = $allRelationshipType;
281
282 $cSubTypes = CRM_Core_Component::contactSubTypes();
283 $contactSubTypes = array();
284 foreach ($cSubTypes as $key => $value) {
285 $contactSubTypes[$key] = $key;
286 }
287
288 $subTypes['Contact'] = $contactSubTypes;
289
290 CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($subTypes);
291
292 foreach ($customGroup as $key => $values) {
293 $subValue = CRM_Utils_Array::value('extends_entity_column_value', $customGroup[$key]);
294 $subName = CRM_Utils_Array::value('extends_entity_column_id', $customGroup[$key]);
295 $type = CRM_Utils_Array::value('extends', $customGroup[$key]);
296 if ($subValue) {
297 $subValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
298 substr($subValue, 1, -1)
299 );
300 $colValue = NULL;
301 foreach ($subValue as $sub) {
302 if ($sub) {
303 if ($type == 'Participant') {
304 if ($subName == 1) {
305 $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantRole'][$sub] : $subTypes['ParticipantRole'][$sub];
306 }
307 elseif ($subName == 2) {
308 $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantEventName'][$sub] : $subTypes['ParticipantEventName'][$sub];
309 }
310 elseif ($subName == 3) {
311 $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantEventType'][$sub] : $subTypes['ParticipantEventType'][$sub];
312 }
313 }
314 elseif ($type == 'Relationship') {
315 $colValue = $colValue ? $colValue . ', ' . $subTypes[$type][$sub . '_a_b'] : $subTypes[$type][$sub . '_a_b'];
316 if (isset($subTypes[$type][$sub . '_b_a'])) {
317 $colValue = $colValue ? $colValue . ', ' . $subTypes[$type][$sub . '_b_a'] : $subTypes[$type][$sub . '_b_a'];
318 }
319 }
320 else {
321 $colValue = $colValue ? ($colValue . (isset($subTypes[$type][$sub]) ? ', ' . $subTypes[$type][$sub] : '')) : (isset($subTypes[$type][$sub]) ? $subTypes[$type][$sub] : '');
322 }
323 }
324 }
325 $customGroup[$key]["extends_entity_column_value"] = $colValue;
326 }
327 else {
328 if (is_array(CRM_Utils_Array::value($type, $subTypes))) {
329 $customGroup[$key]["extends_entity_column_value"] = ts("Any");
330 }
331 }
332 }
333
334 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group', "reset=1&action=browse");
335 CRM_Utils_Weight::addOrder($customGroup, 'CRM_Core_DAO_CustomGroup',
336 'id', $returnURL
337 );
338
339 $this->assign('rows', $customGroup);
340 }
341 }
342