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