Commit | Line | Data |
---|---|---|
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 | */ | |
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 | |
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'), | |
4d17a233 | 86 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
87 | 'title' => ts('Disable CiviCRM Profile Field'), |
88 | ), | |
89 | CRM_Core_Action::ENABLE => array( | |
90 | 'name' => ts('Enable'), | |
4d17a233 | 91 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
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 | $ufField = array(); | |
114 | $ufFieldBAO = new CRM_Core_BAO_UFField(); | |
115 | ||
116 | // fkey is gid | |
117 | $ufFieldBAO->uf_group_id = $this->_gid; | |
118 | $ufFieldBAO->orderBy('weight', 'field_name'); | |
119 | $ufFieldBAO->find(); | |
120 | ||
121 | $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid); | |
122 | $this->assign('otherModules', $otherModules); | |
123 | ||
124 | $isGroupReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_reserved'); | |
125 | $this->assign('isGroupReserved', $isGroupReserved); | |
126 | ||
127 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); | |
128 | if ($profileType == 'Contribution' || $profileType == 'Membership' || $profileType == 'Activity' || $profileType == 'Participant') { | |
129 | $this->assign('skipCreate', TRUE); | |
130 | } | |
131 | ||
132 | $locationType = array(); | |
b2b0530a | 133 | $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
134 | |
135 | $fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE); | |
136 | $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields); | |
137 | ||
138 | $select = array(); | |
139 | foreach ($fields as $name => $field) { | |
140 | if ($name) { | |
141 | $select[$name] = $field['title']; | |
142 | } | |
143 | } | |
144 | $select['group'] = ts('Group(s)'); | |
145 | $select['tag'] = ts('Tag(s)'); | |
146 | ||
77d0b1f8 | 147 | $visibility = CRM_Core_SelectValues::ufVisibility(); |
6a488035 TO |
148 | while ($ufFieldBAO->fetch()) { |
149 | $ufField[$ufFieldBAO->id] = array(); | |
150 | $phoneType = $locType = ''; | |
151 | CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]); | |
77d0b1f8 | 152 | $ufField[$ufFieldBAO->id]['visibility_display'] = $visibility[$ufFieldBAO->visibility]; |
6a488035 TO |
153 | |
154 | $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label; | |
155 | ||
156 | $action = array_sum(array_keys($this->actionLinks())); | |
157 | if ($ufFieldBAO->is_active) { | |
158 | $action -= CRM_Core_Action::ENABLE; | |
159 | } | |
160 | else { | |
161 | $action -= CRM_Core_Action::DISABLE; | |
162 | } | |
163 | ||
164 | if ($ufFieldBAO->is_reserved) { | |
165 | $action -= CRM_Core_Action::UPDATE; | |
166 | $action -= CRM_Core_Action::DISABLE; | |
167 | $action -= CRM_Core_Action::DELETE; | |
168 | } | |
169 | $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight']; | |
170 | $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), | |
171 | $action, | |
172 | array( | |
173 | 'id' => $ufFieldBAO->id, | |
174 | 'gid' => $this->_gid, | |
87dab4a4 AH |
175 | ), |
176 | ts('more'), | |
177 | FALSE, | |
178 | 'ufField.row.actions', | |
179 | 'UFField', | |
180 | $ufFieldBAO->id | |
6a488035 TO |
181 | ); |
182 | } | |
183 | ||
184 | $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
185 | "reset=1&action=browse&gid={$this->_gid}" | |
186 | ); | |
187 | $filter = "uf_group_id = {$this->_gid}"; | |
188 | CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField', | |
189 | 'id', $returnURL, $filter | |
190 | ); | |
191 | ||
192 | $this->assign('ufField', $ufField); | |
193 | ||
194 | // retrieve showBestResult from session | |
195 | $session = CRM_Core_Session::singleton(); | |
196 | $showBestResult = $session->get('showBestResult'); | |
197 | $this->assign('showBestResult', $showBestResult); | |
198 | $session->set('showBestResult', 0); | |
199 | } | |
200 | ||
201 | /** | |
202 | * edit CiviCRM Profile data. | |
203 | * | |
204 | * editing would involved modifying existing fields + adding data to new fields. | |
205 | * | |
206 | * @param string $action the action to be invoked | |
207 | * | |
208 | * @return void | |
209 | * @access public | |
210 | */ | |
211 | function edit($action) { | |
212 | // create a simple controller for editing CiviCRM Profile data | |
213 | $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action); | |
214 | ||
215 | // set the userContext stack | |
216 | $session = CRM_Core_Session::singleton(); | |
217 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
218 | 'reset=1&action=browse&gid=' . $this->_gid | |
219 | )); | |
220 | $controller->set('gid', $this->_gid); | |
221 | $controller->setEmbedded(TRUE); | |
222 | $controller->process(); | |
223 | $controller->run(); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Run the page. | |
228 | * | |
229 | * This method is called after the page is created. It checks for the | |
230 | * type of action and executes that action. | |
231 | * | |
232 | * @return void | |
233 | * @access public | |
234 | * | |
235 | */ | |
236 | function run() { | |
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 | * | |
281 | * @param int $id custom field id | |
282 | * | |
283 | * @return void | |
284 | * @access public | |
285 | */ | |
286 | 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 |