Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Create a page for displaying CiviCRM Profile Fields. | |
20 | * | |
21 | * Heart of this class is the run method which checks | |
22 | * for action type and then displays the appropriate | |
23 | * page. | |
24 | * | |
25 | */ | |
26 | class CRM_UF_Page_Field extends CRM_Core_Page { | |
27 | ||
96f50de2 CW |
28 | public $useLivePageJS = TRUE; |
29 | ||
6a488035 | 30 | /** |
fe482240 | 31 | * The group id of the field. |
6a488035 TO |
32 | * |
33 | * @var int | |
6a488035 TO |
34 | */ |
35 | protected $_gid; | |
36 | ||
37 | /** | |
fe482240 | 38 | * The action links that we need to display for the browse screen. |
6a488035 TO |
39 | * |
40 | * @var array | |
6a488035 TO |
41 | */ |
42 | private static $_actionLinks; | |
43 | ||
44 | /** | |
45 | * Get the action links for this page. | |
46 | * | |
a6c01b45 | 47 | * @return array |
95ea96be | 48 | */ |
d1424f8f | 49 | public static function &actionLinks() { |
6a488035 | 50 | if (!isset(self::$_actionLinks)) { |
be2fb01f CW |
51 | self::$_actionLinks = [ |
52 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
53 | 'name' => ts('Edit'), |
54 | 'url' => 'civicrm/admin/uf/group/field/update', | |
55 | 'qs' => 'reset=1&action=update&id=%%id%%&gid=%%gid%%', | |
56 | 'title' => ts('Edit CiviCRM Profile Field'), | |
be2fb01f CW |
57 | ], |
58 | CRM_Core_Action::PREVIEW => [ | |
6a488035 TO |
59 | 'name' => ts('Preview'), |
60 | 'url' => 'civicrm/admin/uf/group/field', | |
61 | 'qs' => 'action=preview&id=%%id%%&field=1', | |
62 | 'title' => ts('Preview CiviCRM Profile Field'), | |
be2fb01f CW |
63 | ], |
64 | CRM_Core_Action::DISABLE => [ | |
6a488035 | 65 | 'name' => ts('Disable'), |
4d17a233 | 66 | 'ref' => 'crm-enable-disable', |
6a488035 | 67 | 'title' => ts('Disable CiviCRM Profile Field'), |
be2fb01f CW |
68 | ], |
69 | CRM_Core_Action::ENABLE => [ | |
6a488035 | 70 | 'name' => ts('Enable'), |
4d17a233 | 71 | 'ref' => 'crm-enable-disable', |
6a488035 | 72 | 'title' => ts('Enable CiviCRM Profile Field'), |
be2fb01f CW |
73 | ], |
74 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
75 | 'name' => ts('Delete'), |
76 | 'url' => 'civicrm/admin/uf/group/field', | |
77 | 'qs' => 'action=delete&id=%%id%%', | |
78 | 'title' => ts('Enable CiviCRM Profile Field'), | |
be2fb01f CW |
79 | ], |
80 | ]; | |
6a488035 TO |
81 | } |
82 | return self::$_actionLinks; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Browse all CiviCRM Profile group fields. | |
87 | * | |
88 | * @return void | |
6a488035 | 89 | */ |
00be9182 | 90 | public function browse() { |
6f231148 CW |
91 | $resourceManager = CRM_Core_Resources::singleton(); |
92 | if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) { | |
96ed17aa | 93 | $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header'); |
6f231148 CW |
94 | } |
95 | ||
be2fb01f | 96 | $ufField = []; |
6a488035 TO |
97 | $ufFieldBAO = new CRM_Core_BAO_UFField(); |
98 | ||
99 | // fkey is gid | |
100 | $ufFieldBAO->uf_group_id = $this->_gid; | |
101 | $ufFieldBAO->orderBy('weight', 'field_name'); | |
102 | $ufFieldBAO->find(); | |
103 | ||
104 | $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid); | |
105 | $this->assign('otherModules', $otherModules); | |
106 | ||
107 | $isGroupReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'is_reserved'); | |
108 | $this->assign('isGroupReserved', $isGroupReserved); | |
109 | ||
7e50650d PN |
110 | $isMixedProfile = CRM_Core_BAO_UFField::checkProfileType($this->_gid); |
111 | if ($isMixedProfile) { | |
6a488035 TO |
112 | $this->assign('skipCreate', TRUE); |
113 | } | |
114 | ||
be2fb01f | 115 | $locationType = []; |
b2b0530a | 116 | $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
117 | |
118 | $fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE); | |
119 | $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields); | |
120 | ||
be2fb01f | 121 | $select = []; |
6a488035 TO |
122 | foreach ($fields as $name => $field) { |
123 | if ($name) { | |
124 | $select[$name] = $field['title']; | |
125 | } | |
126 | } | |
127 | $select['group'] = ts('Group(s)'); | |
128 | $select['tag'] = ts('Tag(s)'); | |
129 | ||
77d0b1f8 | 130 | $visibility = CRM_Core_SelectValues::ufVisibility(); |
6a488035 | 131 | while ($ufFieldBAO->fetch()) { |
be2fb01f | 132 | $ufField[$ufFieldBAO->id] = []; |
6a488035 TO |
133 | $phoneType = $locType = ''; |
134 | CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]); | |
77d0b1f8 | 135 | $ufField[$ufFieldBAO->id]['visibility_display'] = $visibility[$ufFieldBAO->visibility]; |
6a488035 TO |
136 | |
137 | $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label; | |
138 | ||
d1424f8f | 139 | $action = array_sum(array_keys(self::actionLinks())); |
6a488035 TO |
140 | if ($ufFieldBAO->is_active) { |
141 | $action -= CRM_Core_Action::ENABLE; | |
142 | } | |
143 | else { | |
144 | $action -= CRM_Core_Action::DISABLE; | |
145 | } | |
146 | ||
147 | if ($ufFieldBAO->is_reserved) { | |
148 | $action -= CRM_Core_Action::UPDATE; | |
149 | $action -= CRM_Core_Action::DISABLE; | |
150 | $action -= CRM_Core_Action::DELETE; | |
151 | } | |
152 | $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight']; | |
153 | $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), | |
154 | $action, | |
be2fb01f | 155 | [ |
6a488035 TO |
156 | 'id' => $ufFieldBAO->id, |
157 | 'gid' => $this->_gid, | |
be2fb01f | 158 | ], |
87dab4a4 AH |
159 | ts('more'), |
160 | FALSE, | |
161 | 'ufField.row.actions', | |
162 | 'UFField', | |
163 | $ufFieldBAO->id | |
6a488035 TO |
164 | ); |
165 | } | |
166 | ||
167 | $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
168 | "reset=1&action=browse&gid={$this->_gid}" | |
169 | ); | |
170 | $filter = "uf_group_id = {$this->_gid}"; | |
171 | CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField', | |
172 | 'id', $returnURL, $filter | |
173 | ); | |
174 | ||
175 | $this->assign('ufField', $ufField); | |
176 | ||
177 | // retrieve showBestResult from session | |
178 | $session = CRM_Core_Session::singleton(); | |
179 | $showBestResult = $session->get('showBestResult'); | |
180 | $this->assign('showBestResult', $showBestResult); | |
181 | $session->set('showBestResult', 0); | |
182 | } | |
183 | ||
184 | /** | |
100fef9d | 185 | * Edit CiviCRM Profile data. |
6a488035 TO |
186 | * |
187 | * editing would involved modifying existing fields + adding data to new fields. | |
188 | * | |
5ce1712d TO |
189 | * @param string $action |
190 | * The action to be invoked. | |
6a488035 TO |
191 | * |
192 | * @return void | |
6a488035 | 193 | */ |
00be9182 | 194 | public function edit($action) { |
6a488035 TO |
195 | // create a simple controller for editing CiviCRM Profile data |
196 | $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Field', ts('CiviCRM Profile Field'), $action); | |
197 | ||
198 | // set the userContext stack | |
199 | $session = CRM_Core_Session::singleton(); | |
200 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
353ffa53 TO |
201 | 'reset=1&action=browse&gid=' . $this->_gid |
202 | )); | |
6a488035 TO |
203 | $controller->set('gid', $this->_gid); |
204 | $controller->setEmbedded(TRUE); | |
205 | $controller->process(); | |
206 | $controller->run(); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Run the page. | |
211 | * | |
212 | * This method is called after the page is created. It checks for the | |
213 | * type of action and executes that action. | |
214 | * | |
215 | * @return void | |
6a488035 | 216 | */ |
00be9182 | 217 | public function run() { |
6a488035 TO |
218 | // get the group id |
219 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', | |
220 | $this, FALSE, 0 | |
221 | ); | |
222 | ||
223 | if ($this->_gid) { | |
224 | $groupTitle = CRM_Core_BAO_UFGroup::getTitle($this->_gid); | |
225 | $this->assign('gid', $this->_gid); | |
226 | $this->assign('groupTitle', $groupTitle); | |
be2fb01f | 227 | CRM_Utils_System::setTitle(ts('%1 - CiviCRM Profile Fields', [1 => $groupTitle])); |
6a488035 TO |
228 | } |
229 | ||
230 | // get the requested action | |
231 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
232 | // default to 'browse' | |
233 | $this, FALSE, 'browse' | |
234 | ); | |
235 | ||
236 | // assign vars to templates | |
237 | $this->assign('action', $action); | |
238 | ||
239 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
240 | $this, FALSE, 0 | |
241 | ); | |
242 | ||
243 | // what action to take ? | |
244 | if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE)) { | |
245 | // no browse for edit/update/view/delete | |
246 | $this->edit($action); | |
247 | } | |
248 | elseif ($action & CRM_Core_Action::PREVIEW) { | |
249 | $this->preview($id, $this->_gid); | |
250 | } | |
251 | else { | |
252 | $this->browse(); | |
253 | } | |
254 | ||
255 | // Call the parents run method | |
256 | return parent::run(); | |
257 | } | |
258 | ||
259 | /** | |
fe482240 | 260 | * Preview custom field. |
6a488035 | 261 | * |
5ce1712d TO |
262 | * @param int $fieldId |
263 | * Custom field id. | |
c490a46a | 264 | * @param int $groupId |
6a488035 TO |
265 | * |
266 | * @return void | |
6a488035 | 267 | */ |
00be9182 | 268 | public function preview($fieldId, $groupId) { |
6a488035 TO |
269 | $controller = new CRM_Core_Controller_Simple('CRM_UF_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW); |
270 | $session = CRM_Core_Session::singleton(); | |
271 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
353ffa53 TO |
272 | 'reset=1&action=browse&gid=' . $this->_gid |
273 | )); | |
6a488035 TO |
274 | $controller->set('fieldId', $fieldId); |
275 | $controller->set('id', $groupId); | |
276 | $controller->setEmbedded(TRUE); | |
277 | $controller->process(); | |
278 | $controller->run(); | |
279 | } | |
96025800 | 280 | |
6a488035 | 281 | } |