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