Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | * Create a page for displaying Custom Fields. | |
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_Field extends CRM_Core_Page { | |
45 | ||
96f50de2 CW |
46 | public $useLivePageJS = TRUE; |
47 | ||
6a488035 TO |
48 | /** |
49 | * The group id of the field | |
50 | * | |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | protected $_gid; | |
54 | ||
55 | /** | |
56 | * The action links that we need to display for the browse screen | |
57 | * | |
58 | * @var array | |
6a488035 TO |
59 | */ |
60 | private static $_actionLinks; | |
61 | ||
62 | /** | |
63 | * Get the action links for this page. | |
64 | * | |
65 | * @param null | |
66 | * | |
67 | * @return array array of action links that we need to display for the browse screen | |
eea16664 | 68 | */ |
00be9182 | 69 | public function &actionLinks() { |
6a488035 | 70 | if (!isset(self::$_actionLinks)) { |
6a488035 TO |
71 | self::$_actionLinks = array( |
72 | CRM_Core_Action::UPDATE => array( | |
73 | 'name' => ts('Edit Field'), | |
74 | 'url' => 'civicrm/admin/custom/group/field/update', | |
75 | 'qs' => 'action=update&reset=1&gid=%%gid%%&id=%%id%%', | |
76 | 'title' => ts('Edit Custom Field'), | |
77 | ), | |
78 | CRM_Core_Action::BROWSE => array( | |
79 | 'name' => ts('Edit Multiple Choice Options'), | |
80 | 'url' => 'civicrm/admin/custom/group/field/option', | |
81 | 'qs' => 'reset=1&action=browse&gid=%%gid%%&fid=%%id%%', | |
82 | 'title' => ts('List Custom Options'), | |
83 | ), | |
84 | CRM_Core_Action::PREVIEW => array( | |
85 | 'name' => ts('Preview Field Display'), | |
86 | 'url' => 'civicrm/admin/custom/group/field', | |
87 | 'qs' => 'action=preview&reset=1&gid=%%gid%%&id=%%id%%', | |
88 | 'title' => ts('Preview Custom Field'), | |
89 | ), | |
90 | CRM_Core_Action::DISABLE => array( | |
91 | 'name' => ts('Disable'), | |
12798ddc | 92 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
93 | 'title' => ts('Disable Custom Field'), |
94 | ), | |
95 | CRM_Core_Action::ENABLE => array( | |
96 | 'name' => ts('Enable'), | |
12798ddc | 97 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
98 | 'title' => ts('Enable Custom Field'), |
99 | ), | |
100 | CRM_Core_Action::EXPORT => array( | |
101 | 'name' => ts('Move'), | |
102 | 'url' => 'civicrm/admin/custom/group/field/move', | |
704f21c0 | 103 | 'class' => 'small-popup', |
6a488035 TO |
104 | 'qs' => 'reset=1&fid=%%id%%', |
105 | 'title' => ts('Move Custom Field'), | |
106 | ), | |
107 | CRM_Core_Action::DELETE => array( | |
108 | 'name' => ts('Delete'), | |
109 | 'url' => 'civicrm/admin/custom/group/field', | |
110 | 'qs' => 'action=delete&reset=1&gid=%%gid%%&id=%%id%%', | |
111 | 'title' => ts('Delete Custom Field'), | |
6a488035 TO |
112 | ), |
113 | ); | |
114 | } | |
115 | return self::$_actionLinks; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Browse all custom group fields. | |
120 | * | |
121 | * @param null | |
122 | * | |
123 | * @return void | |
6a488035 | 124 | */ |
00be9182 | 125 | public function browse() { |
6f231148 CW |
126 | $resourceManager = CRM_Core_Resources::singleton(); |
127 | if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) { | |
96ed17aa | 128 | $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header'); |
6f231148 CW |
129 | } |
130 | ||
6a488035 TO |
131 | $customField = array(); |
132 | $customFieldBAO = new CRM_Core_BAO_CustomField(); | |
133 | ||
134 | // fkey is gid | |
135 | $customFieldBAO->custom_group_id = $this->_gid; | |
136 | $customFieldBAO->orderBy('weight, label'); | |
137 | $customFieldBAO->find(); | |
138 | ||
139 | while ($customFieldBAO->fetch()) { | |
140 | $customField[$customFieldBAO->id] = array(); | |
141 | CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]); | |
142 | $action = array_sum(array_keys($this->actionLinks())); | |
143 | if ($customFieldBAO->is_active) { | |
144 | $action -= CRM_Core_Action::ENABLE; | |
145 | } | |
146 | else { | |
147 | $action -= CRM_Core_Action::DISABLE; | |
148 | } | |
149 | ||
150 | switch ($customFieldBAO->data_type) { | |
151 | case "String": | |
152 | case "Int": | |
153 | case "Float": | |
154 | case "Money": | |
155 | // if Multi Select field is selected in custom field | |
156 | if ($customFieldBAO->html_type == 'Text') { | |
157 | $action -= CRM_Core_Action::BROWSE; | |
158 | } | |
159 | break; | |
160 | ||
161 | case "ContactReference": | |
162 | case "Memo": | |
163 | case "Date": | |
164 | case "Boolean": | |
165 | case "StateProvince": | |
166 | case "Country": | |
167 | case "File": | |
168 | case "Link": | |
169 | $action -= CRM_Core_Action::BROWSE; | |
170 | break; | |
171 | } | |
172 | ||
173 | $customFieldDataType = CRM_Core_BAO_CustomField::dataType(); | |
174 | $customField[$customFieldBAO->id]['data_type'] = $customFieldDataType[$customField[$customFieldBAO->id]['data_type']]; | |
175 | $customField[$customFieldBAO->id]['order'] = $customField[$customFieldBAO->id]['weight']; | |
176 | $customField[$customFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, | |
177 | array( | |
178 | 'id' => $customFieldBAO->id, | |
179 | 'gid' => $this->_gid, | |
87dab4a4 AH |
180 | ), |
181 | ts('more'), | |
182 | FALSE, | |
183 | 'customField.row.actions', | |
184 | 'CustomField', | |
185 | $customFieldBAO->id | |
6a488035 TO |
186 | ); |
187 | } | |
188 | ||
189 | $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_gid}"); | |
190 | $filter = "custom_group_id = {$this->_gid}"; | |
191 | CRM_Utils_Weight::addOrder($customField, 'CRM_Core_DAO_CustomField', | |
192 | 'id', $returnURL, $filter | |
193 | ); | |
194 | ||
195 | $this->assign('customField', $customField); | |
196 | } | |
197 | ||
198 | /** | |
100fef9d | 199 | * Edit custom data. |
6a488035 TO |
200 | * |
201 | * editing would involved modifying existing fields + adding data to new fields. | |
202 | * | |
c4ca4892 TO |
203 | * @param string $action |
204 | * The action to be invoked. | |
6a488035 TO |
205 | * |
206 | * @return void | |
6a488035 | 207 | */ |
00be9182 | 208 | public function edit($action) { |
6a488035 TO |
209 | // create a simple controller for editing custom dataCRM/Custom/Page/Field.php |
210 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Field', ts('Custom Field'), $action); | |
211 | ||
212 | // set the userContext stack | |
213 | $session = CRM_Core_Session::singleton(); | |
214 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid)); | |
215 | ||
216 | $controller->set('gid', $this->_gid); | |
217 | $controller->setEmbedded(TRUE); | |
218 | $controller->process(); | |
219 | $controller->run(); | |
220 | } | |
221 | ||
222 | /** | |
223 | * Run the page. | |
224 | * | |
225 | * This method is called after the page is created. It checks for the | |
226 | * type of action and executes that action. | |
227 | * | |
228 | * @param null | |
229 | * | |
230 | * @return void | |
6a488035 | 231 | */ |
00be9182 | 232 | public function run() { |
6a488035 | 233 | |
fa3a5fe2 CW |
234 | |
235 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
236 | $this, FALSE, 0 | |
6a488035 | 237 | ); |
d06700a7 | 238 | |
fa3a5fe2 CW |
239 | if ($id) { |
240 | $values = civicrm_api3('custom_field', 'getsingle', array('id' => $id)); | |
241 | $this->_gid = $values['custom_group_id']; | |
242 | } | |
243 | // get the group id | |
244 | else { | |
245 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', | |
246 | $this | |
247 | ); | |
248 | } | |
249 | ||
d06700a7 | 250 | if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) { |
e89941dc | 251 | CRM_Core_Error::fatal("You cannot add or edit fields in a reserved custom field-set."); |
d06700a7 RN |
252 | } |
253 | ||
6a488035 TO |
254 | $action = CRM_Utils_Request::retrieve('action', 'String', |
255 | // default to 'browse' | |
256 | $this, FALSE, 'browse' | |
257 | ); | |
258 | ||
259 | if ($action & CRM_Core_Action::DELETE) { | |
260 | ||
261 | $session = CRM_Core_Session::singleton(); | |
262 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid)); | |
263 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteField', "Delete Custom Field", ''); | |
264 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
265 | $this, FALSE, 0 | |
266 | ); | |
267 | $controller->set('id', $id); | |
268 | $controller->setEmbedded(TRUE); | |
269 | $controller->process(); | |
270 | $controller->run(); | |
271 | $fieldValues = array('custom_group_id' => $this->_gid); | |
272 | $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_CustomField', $id, $fieldValues); | |
273 | } | |
274 | ||
275 | if ($this->_gid) { | |
276 | $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_gid); | |
277 | $this->assign('gid', $this->_gid); | |
278 | $this->assign('groupTitle', $groupTitle); | |
e2046b33 CW |
279 | if ($action & CRM_Core_Action::BROWSE) { |
280 | CRM_Utils_System::setTitle(ts('%1 - Custom Fields', array(1 => $groupTitle))); | |
281 | } | |
6a488035 TO |
282 | } |
283 | ||
284 | // assign vars to templates | |
285 | $this->assign('action', $action); | |
286 | ||
6a488035 TO |
287 | // what action to take ? |
288 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { | |
289 | // no browse for edit/update/view | |
290 | $this->edit($action); | |
291 | } | |
292 | elseif ($action & CRM_Core_Action::PREVIEW) { | |
293 | $this->preview($id); | |
294 | } | |
295 | else { | |
296 | $this->browse(); | |
297 | } | |
298 | ||
299 | // Call the parents run method | |
300 | return parent::run(); | |
301 | } | |
302 | ||
303 | /** | |
304 | * Preview custom field | |
305 | * | |
c4ca4892 TO |
306 | * @param int $id |
307 | * Custom field id. | |
6a488035 TO |
308 | * |
309 | * @return void | |
6a488035 | 310 | */ |
00be9182 | 311 | public function preview($id) { |
6a488035 TO |
312 | $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW); |
313 | $session = CRM_Core_Session::singleton(); | |
314 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid)); | |
315 | $controller->set('fieldId', $id); | |
316 | $controller->set('groupId', $this->_gid); | |
317 | $controller->setEmbedded(TRUE); | |
318 | $controller->process(); | |
319 | $controller->run(); | |
320 | } | |
321 | } |