CRM-14181 fixes, deleted tsEnum() and code fixes
[civicrm-core.git] / CRM / UF / Page / Field.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 CiviCRM Profile 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 */
44class CRM_UF_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 * @return array $_actionLinks
66 *
67 */ function &actionLinks() {
68 if (!isset(self::$_actionLinks)) {
69 self::$_actionLinks = array(
70 CRM_Core_Action::UPDATE => array(
71 'name' => ts('Edit'),
72 'url' => 'civicrm/admin/uf/group/field/update',
73 'qs' => 'reset=1&action=update&id=%%id%%&gid=%%gid%%',
74 'title' => ts('Edit CiviCRM Profile Field'),
75 ),
76 CRM_Core_Action::PREVIEW => array(
77 'name' => ts('Preview'),
78 'url' => 'civicrm/admin/uf/group/field',
79 'qs' => 'action=preview&id=%%id%%&field=1',
80 'title' => ts('Preview CiviCRM Profile Field'),
81 ),
82 CRM_Core_Action::DISABLE => array(
83 'name' => ts('Disable'),
4d17a233 84 'ref' => 'crm-enable-disable',
6a488035
TO
85 'title' => ts('Disable CiviCRM Profile Field'),
86 ),
87 CRM_Core_Action::ENABLE => array(
88 'name' => ts('Enable'),
4d17a233 89 'ref' => 'crm-enable-disable',
6a488035
TO
90 'title' => ts('Enable CiviCRM Profile Field'),
91 ),
92 CRM_Core_Action::DELETE => array(
93 'name' => ts('Delete'),
94 'url' => 'civicrm/admin/uf/group/field',
95 'qs' => 'action=delete&id=%%id%%',
96 'title' => ts('Enable CiviCRM Profile Field'),
97 ),
98 );
99 }
100 return self::$_actionLinks;
101 }
102
103 /**
104 * Browse all CiviCRM Profile group fields.
105 *
106 * @return void
107 * @access public
108 * @static
109 */
110 function browse() {
4d17a233 111 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
6a488035
TO
112 $ufField = array();
113 $ufFieldBAO = new CRM_Core_BAO_UFField();
114
115 // fkey is gid
116 $ufFieldBAO->uf_group_id = $this->_gid;
117 $ufFieldBAO->orderBy('weight', 'field_name');
118 $ufFieldBAO->find();
119
120 $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid);
121 $this->assign('otherModules', $otherModules);
122
123 $isGroupReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_reserved');
124 $this->assign('isGroupReserved', $isGroupReserved);
125
126 $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid);
127 if ($profileType == 'Contribution' || $profileType == 'Membership' || $profileType == 'Activity' || $profileType == 'Participant') {
128 $this->assign('skipCreate', TRUE);
129 }
130
131 $locationType = array();
b2b0530a 132 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
133
134 $fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
135 $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields);
136
137 $select = array();
138 foreach ($fields as $name => $field) {
139 if ($name) {
140 $select[$name] = $field['title'];
141 }
142 }
143 $select['group'] = ts('Group(s)');
144 $select['tag'] = ts('Tag(s)');
145
146 while ($ufFieldBAO->fetch()) {
147 $ufField[$ufFieldBAO->id] = array();
148 $phoneType = $locType = '';
149 CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]);
150 CRM_Core_DAO_UFField::addDisplayEnums($ufField[$ufFieldBAO->id]);
151
152 $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label;
153
154 $action = array_sum(array_keys($this->actionLinks()));
155 if ($ufFieldBAO->is_active) {
156 $action -= CRM_Core_Action::ENABLE;
157 }
158 else {
159 $action -= CRM_Core_Action::DISABLE;
160 }
161
162 if ($ufFieldBAO->is_reserved) {
163 $action -= CRM_Core_Action::UPDATE;
164 $action -= CRM_Core_Action::DISABLE;
165 $action -= CRM_Core_Action::DELETE;
166 }
167 $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight'];
168 $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
169 $action,
170 array(
171 'id' => $ufFieldBAO->id,
172 'gid' => $this->_gid,
87dab4a4
AH
173 ),
174 ts('more'),
175 FALSE,
176 'ufField.row.actions',
177 'UFField',
178 $ufFieldBAO->id
6a488035
TO
179 );
180 }
181
182 $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field',
183 "reset=1&action=browse&gid={$this->_gid}"
184 );
185 $filter = "uf_group_id = {$this->_gid}";
186 CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField',
187 'id', $returnURL, $filter
188 );
189
190 $this->assign('ufField', $ufField);
191
192 // retrieve showBestResult from session
193 $session = CRM_Core_Session::singleton();
194 $showBestResult = $session->get('showBestResult');
195 $this->assign('showBestResult', $showBestResult);
196 $session->set('showBestResult', 0);
197 }
198
199 /**
200 * edit CiviCRM Profile data.
201 *
202 * editing would involved modifying existing fields + adding data to new fields.
203 *
204 * @param string $action the action to be invoked
205 *
206 * @return void
207 * @access public
208 */
209 function edit($action) {
210 // create a simple controller for editing CiviCRM Profile data
211 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action);
212
213 // set the userContext stack
214 $session = CRM_Core_Session::singleton();
215 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
216 'reset=1&action=browse&gid=' . $this->_gid
217 ));
218 $controller->set('gid', $this->_gid);
219 $controller->setEmbedded(TRUE);
220 $controller->process();
221 $controller->run();
222 }
223
224 /**
225 * Run the page.
226 *
227 * This method is called after the page is created. It checks for the
228 * type of action and executes that action.
229 *
230 * @return void
231 * @access public
232 *
233 */
234 function run() {
235 // get the group id
236 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
237 $this, FALSE, 0
238 );
239
240 if ($this->_gid) {
241 $groupTitle = CRM_Core_BAO_UFGroup::getTitle($this->_gid);
242 $this->assign('gid', $this->_gid);
243 $this->assign('groupTitle', $groupTitle);
244 CRM_Utils_System::setTitle(ts('%1 - CiviCRM Profile Fields', array(1 => $groupTitle)));
245 }
246
247 // get the requested action
248 $action = CRM_Utils_Request::retrieve('action', 'String',
249 // default to 'browse'
250 $this, FALSE, 'browse'
251 );
252
253 // assign vars to templates
254 $this->assign('action', $action);
255
256 $id = CRM_Utils_Request::retrieve('id', 'Positive',
257 $this, FALSE, 0
258 );
259
260 // what action to take ?
261 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE)) {
262 // no browse for edit/update/view/delete
263 $this->edit($action);
264 }
265 elseif ($action & CRM_Core_Action::PREVIEW) {
266 $this->preview($id, $this->_gid);
267 }
268 else {
269 $this->browse();
270 }
271
272 // Call the parents run method
273 return parent::run();
274 }
275
276 /**
277 * Preview custom field
278 *
279 * @param int $id custom field id
280 *
281 * @return void
282 * @access public
283 */
284 function preview($fieldId, $groupId) {
285 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
286 $session = CRM_Core_Session::singleton();
287 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
288 'reset=1&action=browse&gid=' . $this->_gid
289 ));
290 $controller->set('fieldId', $fieldId);
291 $controller->set('id', $groupId);
292 $controller->setEmbedded(TRUE);
293 $controller->process();
294 $controller->run();
295 }
296}
297