--CRM-18204, remove Create mode, Edit mode, listing mode and HMTL form snippet links...
[civicrm-core.git] / CRM / UF / Page / Field.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
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
96f50de2
CW
46 public $useLivePageJS = TRUE;
47
6a488035 48 /**
fe482240 49 * The group id of the field.
6a488035
TO
50 *
51 * @var int
6a488035
TO
52 */
53 protected $_gid;
54
55 /**
fe482240 56 * The action links that we need to display for the browse screen.
6a488035
TO
57 *
58 * @var array
6a488035
TO
59 */
60 private static $_actionLinks;
61
62 /**
63 * Get the action links for this page.
64 *
a6c01b45 65 * @return array
95ea96be 66 */
d1424f8f 67 public static function &actionLinks() {
6a488035
TO
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
6a488035 107 */
00be9182 108 public function browse() {
6f231148
CW
109 $resourceManager = CRM_Core_Resources::singleton();
110 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
96ed17aa 111 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
6f231148
CW
112 }
113
6a488035
TO
114 $ufField = array();
115 $ufFieldBAO = new CRM_Core_BAO_UFField();
116
117 // fkey is gid
118 $ufFieldBAO->uf_group_id = $this->_gid;
119 $ufFieldBAO->orderBy('weight', 'field_name');
120 $ufFieldBAO->find();
121
122 $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid);
123 $this->assign('otherModules', $otherModules);
124
125 $isGroupReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_reserved');
126 $this->assign('isGroupReserved', $isGroupReserved);
127
128 $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid);
c84e7ae3
PN
129 $contactTypes = CRM_Contact_BAO_ContactType::contactTypes();
130 $contactTypes[] = 'Contact';
131 if (!in_array($profileType, $contactTypes)) {
6a488035
TO
132 $this->assign('skipCreate', TRUE);
133 }
134
135 $locationType = array();
b2b0530a 136 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
137
138 $fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
139 $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields);
140
141 $select = array();
142 foreach ($fields as $name => $field) {
143 if ($name) {
144 $select[$name] = $field['title'];
145 }
146 }
147 $select['group'] = ts('Group(s)');
148 $select['tag'] = ts('Tag(s)');
149
77d0b1f8 150 $visibility = CRM_Core_SelectValues::ufVisibility();
6a488035
TO
151 while ($ufFieldBAO->fetch()) {
152 $ufField[$ufFieldBAO->id] = array();
153 $phoneType = $locType = '';
154 CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]);
77d0b1f8 155 $ufField[$ufFieldBAO->id]['visibility_display'] = $visibility[$ufFieldBAO->visibility];
6a488035
TO
156
157 $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label;
158
d1424f8f 159 $action = array_sum(array_keys(self::actionLinks()));
6a488035
TO
160 if ($ufFieldBAO->is_active) {
161 $action -= CRM_Core_Action::ENABLE;
162 }
163 else {
164 $action -= CRM_Core_Action::DISABLE;
165 }
166
167 if ($ufFieldBAO->is_reserved) {
168 $action -= CRM_Core_Action::UPDATE;
169 $action -= CRM_Core_Action::DISABLE;
170 $action -= CRM_Core_Action::DELETE;
171 }
172 $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight'];
173 $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
174 $action,
175 array(
176 'id' => $ufFieldBAO->id,
177 'gid' => $this->_gid,
87dab4a4
AH
178 ),
179 ts('more'),
180 FALSE,
181 'ufField.row.actions',
182 'UFField',
183 $ufFieldBAO->id
6a488035
TO
184 );
185 }
186
187 $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field',
188 "reset=1&action=browse&gid={$this->_gid}"
189 );
190 $filter = "uf_group_id = {$this->_gid}";
191 CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField',
192 'id', $returnURL, $filter
193 );
194
195 $this->assign('ufField', $ufField);
196
197 // retrieve showBestResult from session
198 $session = CRM_Core_Session::singleton();
199 $showBestResult = $session->get('showBestResult');
200 $this->assign('showBestResult', $showBestResult);
201 $session->set('showBestResult', 0);
202 }
203
204 /**
100fef9d 205 * Edit CiviCRM Profile data.
6a488035
TO
206 *
207 * editing would involved modifying existing fields + adding data to new fields.
208 *
5ce1712d
TO
209 * @param string $action
210 * The action to be invoked.
6a488035
TO
211 *
212 * @return void
6a488035 213 */
00be9182 214 public function edit($action) {
6a488035
TO
215 // create a simple controller for editing CiviCRM Profile data
216 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action);
217
218 // set the userContext stack
219 $session = CRM_Core_Session::singleton();
220 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
353ffa53
TO
221 'reset=1&action=browse&gid=' . $this->_gid
222 ));
6a488035
TO
223 $controller->set('gid', $this->_gid);
224 $controller->setEmbedded(TRUE);
225 $controller->process();
226 $controller->run();
227 }
228
229 /**
230 * Run the page.
231 *
232 * This method is called after the page is created. It checks for the
233 * type of action and executes that action.
234 *
235 * @return void
6a488035 236 */
00be9182 237 public function run() {
6a488035
TO
238 // get the group id
239 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
240 $this, FALSE, 0
241 );
242
243 if ($this->_gid) {
244 $groupTitle = CRM_Core_BAO_UFGroup::getTitle($this->_gid);
245 $this->assign('gid', $this->_gid);
246 $this->assign('groupTitle', $groupTitle);
247 CRM_Utils_System::setTitle(ts('%1 - CiviCRM Profile Fields', array(1 => $groupTitle)));
248 }
249
250 // get the requested action
251 $action = CRM_Utils_Request::retrieve('action', 'String',
252 // default to 'browse'
253 $this, FALSE, 'browse'
254 );
255
256 // assign vars to templates
257 $this->assign('action', $action);
258
259 $id = CRM_Utils_Request::retrieve('id', 'Positive',
260 $this, FALSE, 0
261 );
262
263 // what action to take ?
264 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE)) {
265 // no browse for edit/update/view/delete
266 $this->edit($action);
267 }
268 elseif ($action & CRM_Core_Action::PREVIEW) {
269 $this->preview($id, $this->_gid);
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 *
5ce1712d
TO
282 * @param int $fieldId
283 * Custom field id.
c490a46a 284 * @param int $groupId
6a488035
TO
285 *
286 * @return void
6a488035 287 */
00be9182 288 public function preview($fieldId, $groupId) {
6a488035
TO
289 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
290 $session = CRM_Core_Session::singleton();
291 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
353ffa53
TO
292 'reset=1&action=browse&gid=' . $this->_gid
293 ));
6a488035
TO
294 $controller->set('fieldId', $fieldId);
295 $controller->set('id', $groupId);
296 $controller->setEmbedded(TRUE);
297 $controller->process();
298 $controller->run();
299 }
96025800 300
6a488035 301}