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 | ||
46 | /** | |
47 | * The group id of the field | |
48 | * | |
49 | * @var int | |
50 | * @access protected | |
51 | */ | |
52 | protected $_gid; | |
53 | ||
54 | /** | |
55 | * The action links that we need to display for the browse screen | |
56 | * | |
57 | * @var array | |
58 | * @access private | |
59 | */ | |
60 | private static $_actionLinks; | |
61 | ||
62 | /** | |
63 | * Get the action links for this page. | |
64 | * | |
65 | * @return array $_actionLinks | |
66 | * | |
67 | */ 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 | 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_UFField' . '\',\'' . 'enable-disable' . '\',0,\'UFField\' );"', | |
85 | 'ref' => 'disable-action', | |
86 | 'title' => ts('Disable CiviCRM Profile Field'), | |
87 | ), | |
88 | CRM_Core_Action::ENABLE => array( | |
89 | 'name' => ts('Enable'), | |
90 | 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_UFField' . '\',\'' . 'disable-enable' . '\',0,\'UFField\' );"', | |
91 | 'ref' => 'enable-action', | |
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 | ||
147 | while ($ufFieldBAO->fetch()) { | |
148 | $ufField[$ufFieldBAO->id] = array(); | |
149 | $phoneType = $locType = ''; | |
150 | CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]); | |
151 | CRM_Core_DAO_UFField::addDisplayEnums($ufField[$ufFieldBAO->id]); | |
152 | ||
153 | $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label; | |
154 | ||
155 | $action = array_sum(array_keys($this->actionLinks())); | |
156 | if ($ufFieldBAO->is_active) { | |
157 | $action -= CRM_Core_Action::ENABLE; | |
158 | } | |
159 | else { | |
160 | $action -= CRM_Core_Action::DISABLE; | |
161 | } | |
162 | ||
163 | if ($ufFieldBAO->is_reserved) { | |
164 | $action -= CRM_Core_Action::UPDATE; | |
165 | $action -= CRM_Core_Action::DISABLE; | |
166 | $action -= CRM_Core_Action::DELETE; | |
167 | } | |
168 | $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight']; | |
169 | $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), | |
170 | $action, | |
171 | array( | |
172 | 'id' => $ufFieldBAO->id, | |
173 | 'gid' => $this->_gid, | |
174 | ) | |
175 | ); | |
176 | } | |
177 | ||
178 | $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
179 | "reset=1&action=browse&gid={$this->_gid}" | |
180 | ); | |
181 | $filter = "uf_group_id = {$this->_gid}"; | |
182 | CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField', | |
183 | 'id', $returnURL, $filter | |
184 | ); | |
185 | ||
186 | $this->assign('ufField', $ufField); | |
187 | ||
188 | // retrieve showBestResult from session | |
189 | $session = CRM_Core_Session::singleton(); | |
190 | $showBestResult = $session->get('showBestResult'); | |
191 | $this->assign('showBestResult', $showBestResult); | |
192 | $session->set('showBestResult', 0); | |
193 | } | |
194 | ||
195 | /** | |
196 | * edit CiviCRM Profile data. | |
197 | * | |
198 | * editing would involved modifying existing fields + adding data to new fields. | |
199 | * | |
200 | * @param string $action the action to be invoked | |
201 | * | |
202 | * @return void | |
203 | * @access public | |
204 | */ | |
205 | function edit($action) { | |
206 | // create a simple controller for editing CiviCRM Profile data | |
207 | $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action); | |
208 | ||
209 | // set the userContext stack | |
210 | $session = CRM_Core_Session::singleton(); | |
211 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
212 | 'reset=1&action=browse&gid=' . $this->_gid | |
213 | )); | |
214 | $controller->set('gid', $this->_gid); | |
215 | $controller->setEmbedded(TRUE); | |
216 | $controller->process(); | |
217 | $controller->run(); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Run the page. | |
222 | * | |
223 | * This method is called after the page is created. It checks for the | |
224 | * type of action and executes that action. | |
225 | * | |
226 | * @return void | |
227 | * @access public | |
228 | * | |
229 | */ | |
230 | function run() { | |
231 | // get the group id | |
232 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', | |
233 | $this, FALSE, 0 | |
234 | ); | |
235 | ||
236 | if ($this->_gid) { | |
237 | $groupTitle = CRM_Core_BAO_UFGroup::getTitle($this->_gid); | |
238 | $this->assign('gid', $this->_gid); | |
239 | $this->assign('groupTitle', $groupTitle); | |
240 | CRM_Utils_System::setTitle(ts('%1 - CiviCRM Profile Fields', array(1 => $groupTitle))); | |
241 | } | |
242 | ||
243 | // get the requested action | |
244 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
245 | // default to 'browse' | |
246 | $this, FALSE, 'browse' | |
247 | ); | |
248 | ||
249 | // assign vars to templates | |
250 | $this->assign('action', $action); | |
251 | ||
252 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
253 | $this, FALSE, 0 | |
254 | ); | |
255 | ||
256 | // what action to take ? | |
257 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE)) { | |
258 | // no browse for edit/update/view/delete | |
259 | $this->edit($action); | |
260 | } | |
261 | elseif ($action & CRM_Core_Action::PREVIEW) { | |
262 | $this->preview($id, $this->_gid); | |
263 | } | |
264 | else { | |
265 | $this->browse(); | |
266 | } | |
267 | ||
268 | // Call the parents run method | |
269 | return parent::run(); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Preview custom field | |
274 | * | |
275 | * @param int $id custom field id | |
276 | * | |
277 | * @return void | |
278 | * @access public | |
279 | */ | |
280 | function preview($fieldId, $groupId) { | |
281 | $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW); | |
282 | $session = CRM_Core_Session::singleton(); | |
283 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
284 | 'reset=1&action=browse&gid=' . $this->_gid | |
285 | )); | |
286 | $controller->set('fieldId', $fieldId); | |
287 | $controller->set('id', $groupId); | |
288 | $controller->setEmbedded(TRUE); | |
289 | $controller->process(); | |
290 | $controller->run(); | |
291 | } | |
292 | } | |
293 |