Merge pull request #7963 from jitendrapurohit/CRM-18174
[civicrm-core.git] / CRM / UF / Page / Field.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 */
53 protected $_gid;
54
55 /**
56 * The action links that we need to display for the browse screen.
57 *
58 * @var array
59 */
60 private static $_actionLinks;
61
62 /**
63 * Get the action links for this page.
64 *
65 * @return array
66 */
67 public static 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'),
84 'ref' => 'crm-enable-disable',
85 'title' => ts('Disable CiviCRM Profile Field'),
86 ),
87 CRM_Core_Action::ENABLE => array(
88 'name' => ts('Enable'),
89 'ref' => 'crm-enable-disable',
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 */
108 public function browse() {
109 $resourceManager = CRM_Core_Resources::singleton();
110 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
111 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
112 }
113
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 $isMixedProfile = CRM_Core_BAO_UFField::checkProfileType($this->_gid);
129 if ($isMixedProfile) {
130 $this->assign('skipCreate', TRUE);
131 }
132
133 $locationType = array();
134 $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
135
136 $fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
137 $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields);
138
139 $select = array();
140 foreach ($fields as $name => $field) {
141 if ($name) {
142 $select[$name] = $field['title'];
143 }
144 }
145 $select['group'] = ts('Group(s)');
146 $select['tag'] = ts('Tag(s)');
147
148 $visibility = CRM_Core_SelectValues::ufVisibility();
149 while ($ufFieldBAO->fetch()) {
150 $ufField[$ufFieldBAO->id] = array();
151 $phoneType = $locType = '';
152 CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]);
153 $ufField[$ufFieldBAO->id]['visibility_display'] = $visibility[$ufFieldBAO->visibility];
154
155 $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label;
156
157 $action = array_sum(array_keys(self::actionLinks()));
158 if ($ufFieldBAO->is_active) {
159 $action -= CRM_Core_Action::ENABLE;
160 }
161 else {
162 $action -= CRM_Core_Action::DISABLE;
163 }
164
165 if ($ufFieldBAO->is_reserved) {
166 $action -= CRM_Core_Action::UPDATE;
167 $action -= CRM_Core_Action::DISABLE;
168 $action -= CRM_Core_Action::DELETE;
169 }
170 $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight'];
171 $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
172 $action,
173 array(
174 'id' => $ufFieldBAO->id,
175 'gid' => $this->_gid,
176 ),
177 ts('more'),
178 FALSE,
179 'ufField.row.actions',
180 'UFField',
181 $ufFieldBAO->id
182 );
183 }
184
185 $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field',
186 "reset=1&action=browse&gid={$this->_gid}"
187 );
188 $filter = "uf_group_id = {$this->_gid}";
189 CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField',
190 'id', $returnURL, $filter
191 );
192
193 $this->assign('ufField', $ufField);
194
195 // retrieve showBestResult from session
196 $session = CRM_Core_Session::singleton();
197 $showBestResult = $session->get('showBestResult');
198 $this->assign('showBestResult', $showBestResult);
199 $session->set('showBestResult', 0);
200 }
201
202 /**
203 * Edit CiviCRM Profile data.
204 *
205 * editing would involved modifying existing fields + adding data to new fields.
206 *
207 * @param string $action
208 * The action to be invoked.
209 *
210 * @return void
211 */
212 public function edit($action) {
213 // create a simple controller for editing CiviCRM Profile data
214 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action);
215
216 // set the userContext stack
217 $session = CRM_Core_Session::singleton();
218 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
219 'reset=1&action=browse&gid=' . $this->_gid
220 ));
221 $controller->set('gid', $this->_gid);
222 $controller->setEmbedded(TRUE);
223 $controller->process();
224 $controller->run();
225 }
226
227 /**
228 * Run the page.
229 *
230 * This method is called after the page is created. It checks for the
231 * type of action and executes that action.
232 *
233 * @return void
234 */
235 public function run() {
236 // get the group id
237 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
238 $this, FALSE, 0
239 );
240
241 if ($this->_gid) {
242 $groupTitle = CRM_Core_BAO_UFGroup::getTitle($this->_gid);
243 $this->assign('gid', $this->_gid);
244 $this->assign('groupTitle', $groupTitle);
245 CRM_Utils_System::setTitle(ts('%1 - CiviCRM Profile Fields', array(1 => $groupTitle)));
246 }
247
248 // get the requested action
249 $action = CRM_Utils_Request::retrieve('action', 'String',
250 // default to 'browse'
251 $this, FALSE, 'browse'
252 );
253
254 // assign vars to templates
255 $this->assign('action', $action);
256
257 $id = CRM_Utils_Request::retrieve('id', 'Positive',
258 $this, FALSE, 0
259 );
260
261 // what action to take ?
262 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE)) {
263 // no browse for edit/update/view/delete
264 $this->edit($action);
265 }
266 elseif ($action & CRM_Core_Action::PREVIEW) {
267 $this->preview($id, $this->_gid);
268 }
269 else {
270 $this->browse();
271 }
272
273 // Call the parents run method
274 return parent::run();
275 }
276
277 /**
278 * Preview custom field.
279 *
280 * @param int $fieldId
281 * Custom field id.
282 * @param int $groupId
283 *
284 * @return void
285 */
286 public function preview($fieldId, $groupId) {
287 $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
288 $session = CRM_Core_Session::singleton();
289 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field',
290 'reset=1&action=browse&gid=' . $this->_gid
291 ));
292 $controller->set('fieldId', $fieldId);
293 $controller->set('id', $groupId);
294 $controller->setEmbedded(TRUE);
295 $controller->process();
296 $controller->run();
297 }
298
299 }