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