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