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