Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for custom data | |
38 | * | |
39 | * It delegates the work to lower level subclasses and integrates the changes | |
40 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
41 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
42 | * | |
43 | */ | |
44 | class CRM_Profile_Form extends CRM_Core_Form { | |
7da04cde | 45 | const |
6a488035 | 46 | MODE_REGISTER = 1, |
353ffa53 TO |
47 | MODE_SEARCH = 2, |
48 | MODE_CREATE = 4, | |
49 | MODE_EDIT = 8; | |
6a488035 TO |
50 | |
51 | protected $_mode; | |
52 | ||
53 | protected $_skipPermission = FALSE; | |
54 | ||
55 | /** | |
fe482240 | 56 | * The contact id that we are editing. |
6a488035 TO |
57 | * |
58 | * @var int | |
59 | */ | |
60 | protected $_id; | |
61 | ||
62 | /** | |
fe482240 | 63 | * The group id that we are editing. |
6a488035 TO |
64 | * |
65 | * @var int | |
66 | */ | |
67 | protected $_gid; | |
68 | ||
a8387f19 | 69 | /** |
a3dbd1bc | 70 | * @var array details of the UFGroup used on this page |
a8387f19 | 71 | */ |
a3dbd1bc | 72 | protected $_ufGroup = array('name' => 'unknown'); |
a8387f19 | 73 | |
6a488035 | 74 | /** |
fe482240 | 75 | * The group id that we are passing in url. |
6a488035 TO |
76 | * |
77 | * @var int | |
78 | */ | |
79 | public $_grid; | |
80 | ||
426ac5e0 | 81 | /** |
fe482240 | 82 | * Name of button for saving matching contacts. |
426ac5e0 EM |
83 | * @var |
84 | */ | |
85 | protected $_duplicateButtonName; | |
6a488035 | 86 | /** |
fe482240 | 87 | * The title of the category we are editing. |
6a488035 TO |
88 | * |
89 | * @var string | |
90 | */ | |
91 | protected $_title; | |
92 | ||
93 | /** | |
fe482240 | 94 | * The fields needed to build this form. |
6a488035 TO |
95 | * |
96 | * @var array | |
97 | */ | |
98 | public $_fields; | |
99 | ||
100 | /** | |
fe482240 | 101 | * store contact details. |
6a488035 TO |
102 | * |
103 | * @var array | |
104 | */ | |
105 | protected $_contact; | |
106 | ||
6a488035 | 107 | /** |
fe482240 | 108 | * Do we allow updates of the contact. |
6a488035 TO |
109 | * |
110 | * @var int | |
111 | */ | |
112 | public $_isUpdateDupe = 0; | |
113 | ||
90d804ed ML |
114 | /** |
115 | * Dedupe using a specific rule (CRM-6131). | |
116 | * Not currently exposed in profile settings, but can be set in a buildForm hook. | |
117 | */ | |
118 | public $_ruleGroupID = NULL; | |
119 | ||
6a488035 TO |
120 | public $_isAddCaptcha = FALSE; |
121 | ||
122 | protected $_isPermissionedChecksum = FALSE; | |
123 | ||
124 | /** | |
125 | * THe context from which we came from, allows us to go there if redirect not set | |
126 | * | |
127 | * @var string | |
128 | */ | |
129 | protected $_context; | |
130 | ||
131 | /** | |
fe482240 | 132 | * THe contact type for registration case. |
6a488035 TO |
133 | * |
134 | * @var string | |
135 | */ | |
136 | protected $_ctype = NULL; | |
137 | ||
6a488035 TO |
138 | /** |
139 | * Store profile ids if multiple profile ids are passed using comma separated. | |
140 | * Currently lets implement this functionality only for dialog mode | |
141 | */ | |
142 | protected $_profileIds = array(); | |
143 | ||
144 | /** | |
145 | * Contact profile having activity fields? | |
146 | * | |
147 | * @var string | |
148 | */ | |
149 | protected $_isContactActivityProfile = FALSE; | |
150 | ||
151 | /** | |
fe482240 | 152 | * Activity Id connected to the profile. |
6a488035 TO |
153 | * |
154 | * @var string | |
155 | */ | |
156 | protected $_activityId = NULL; | |
157 | ||
158 | ||
159 | protected $_multiRecordFields = NULL; | |
160 | ||
161 | protected $_recordId = NULL; | |
162 | ||
163 | /** | |
100fef9d | 164 | * Action for multi record profile (create/edit/delete) |
6a488035 TO |
165 | * |
166 | * @var string | |
167 | */ | |
168 | protected $_multiRecord = NULL; | |
169 | ||
170 | protected $_multiRecordProfile = FALSE; | |
171 | ||
172 | protected $_recordExists = TRUE; | |
173 | ||
174 | protected $_customGroupTitle = NULL; | |
175 | ||
176 | protected $_deleteButtonName = NULL; | |
177 | ||
178 | protected $_customGroupId = NULL; | |
179 | ||
180 | protected $_currentUserID = NULL; | |
353ffa53 | 181 | protected $_session = NULL; |
d5965a37 | 182 | |
6e62b28c TM |
183 | /** |
184 | * Explicitly declare the entity api name. | |
185 | */ | |
186 | public function getDefaultEntity() { | |
187 | return 'Profile'; | |
188 | } | |
6a488035 TO |
189 | |
190 | /** | |
100fef9d | 191 | * Pre processing work done here. |
6a488035 TO |
192 | * |
193 | * gets session variables for table name, id of entity in table, type of entity and stores them. | |
194 | * | |
195 | * @param | |
196 | * | |
197 | * @return void | |
6a488035 | 198 | */ |
00be9182 | 199 | public function preProcess() { |
353ffa53 | 200 | $this->_id = $this->get('id'); |
6a488035 | 201 | $this->_profileIds = $this->get('profileIds'); |
353ffa53 TO |
202 | $this->_grid = CRM_Utils_Request::retrieve('grid', 'Integer', $this); |
203 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
6a488035 TO |
204 | |
205 | //unset from session when $_GET doesn't have it | |
206 | //except when the form is submitted | |
207 | if (empty($_POST)) { | |
208 | if (!array_key_exists('multiRecord', $_GET)) { | |
209 | $this->set('multiRecord', NULL); | |
210 | } | |
211 | if (!array_key_exists('recordId', $_GET)) { | |
212 | $this->set('recordId', NULL); | |
213 | } | |
214 | } | |
215 | ||
216 | $this->_session = CRM_Core_Session::singleton(); | |
217 | $this->_currentUserID = $this->_session->get('userID'); | |
218 | ||
219 | if ($this->_mode == self::MODE_EDIT) { | |
220 | //specifies the action being done on a multi record field | |
221 | $multiRecordAction = CRM_Utils_Request::retrieve('multiRecord', 'String', $this); | |
dc98079b | 222 | $this->_multiRecord = (!is_numeric($multiRecordAction)) ? CRM_Core_Action::resolve($multiRecordAction) : $multiRecordAction; |
6a488035 TO |
223 | if ($this->_multiRecord) { |
224 | $this->set('multiRecord', $this->_multiRecord); | |
225 | } | |
226 | ||
227 | if ($this->_multiRecord && | |
353ffa53 TO |
228 | !in_array($this->_multiRecord, array(CRM_Core_Action::UPDATE, CRM_Core_Action::ADD, CRM_Core_Action::DELETE)) |
229 | ) { | |
6a488035 TO |
230 | CRM_Core_Error::fatal(ts('Proper action not specified for this custom value record profile')); |
231 | } | |
232 | } | |
233 | $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate'); | |
234 | ||
a3dbd1bc | 235 | $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0)); |
6a488035 TO |
236 | |
237 | if ((count($gids) > 1) && !$this->_profileIds && empty($this->_profileIds)) { | |
238 | if (!empty($gids)) { | |
239 | foreach ($gids as $pfId) { | |
240 | $this->_profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive'); | |
241 | } | |
242 | } | |
243 | ||
244 | // check if we are rendering mixed profiles | |
245 | if (CRM_Core_BAO_UFGroup::checkForMixProfiles($this->_profileIds)) { | |
246 | CRM_Core_Error::fatal(ts('You cannot combine profiles of multiple types.')); | |
247 | } | |
248 | ||
249 | // for now consider 1'st profile as primary profile and validate it | |
250 | // i.e check for profile type etc. | |
251 | // FIX ME: validations for other than primary | |
252 | $this->_gid = $this->_profileIds[0]; | |
253 | $this->set('gid', $this->_gid); | |
254 | $this->set('profileIds', $this->_profileIds); | |
255 | } | |
256 | ||
257 | if (!$this->_gid) { | |
a3dbd1bc TO |
258 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0); |
259 | $this->set('gid', $this->_gid); | |
260 | } | |
261 | ||
6a488035 TO |
262 | $this->_activityId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET'); |
263 | if (is_numeric($this->_activityId)) { | |
264 | $latestRevisionId = CRM_Activity_BAO_Activity::getLatestActivityId($this->_activityId); | |
265 | if ($latestRevisionId) { | |
266 | $this->_activityId = $latestRevisionId; | |
267 | } | |
268 | } | |
269 | $this->_isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($this->_gid); | |
270 | ||
46312a4d | 271 | //get values for ufGroupName, captch and dupe update. |
6a488035 TO |
272 | if ($this->_gid) { |
273 | $dao = new CRM_Core_DAO_UFGroup(); | |
274 | $dao->id = $this->_gid; | |
275 | if ($dao->find(TRUE)) { | |
276 | $this->_isUpdateDupe = $dao->is_update_dupe; | |
277 | $this->_isAddCaptcha = $dao->add_captcha; | |
a3dbd1bc | 278 | $this->_ufGroup = (array) $dao; |
6a488035 TO |
279 | } |
280 | $dao->free(); | |
a3dbd1bc | 281 | |
cacc57d7 TO |
282 | if (!CRM_Utils_Array::value('is_active', $this->_ufGroup)) { |
283 | CRM_Core_Error::fatal(ts('The requested profile (gid=%1) is inactive or does not exist.', array( | |
284 | 1 => $this->_gid, | |
285 | ))); | |
286 | } | |
a3dbd1bc | 287 | } |
cacc57d7 | 288 | $this->assign('ufGroupName', $this->_ufGroup['name']); |
6a488035 TO |
289 | |
290 | $gids = empty($this->_profileIds) ? $this->_gid : $this->_profileIds; | |
291 | ||
292 | // if we dont have a gid use the default, else just use that specific gid | |
293 | if (($this->_mode == self::MODE_REGISTER || $this->_mode == self::MODE_CREATE) && !$this->_gid) { | |
294 | $this->_ctype = CRM_Utils_Request::retrieve('ctype', 'String', $this, FALSE, 'Individual', 'REQUEST'); | |
295 | $this->_fields = CRM_Core_BAO_UFGroup::getRegistrationFields($this->_action, $this->_mode, $this->_ctype); | |
296 | } | |
297 | elseif ($this->_mode == self::MODE_SEARCH) { | |
298 | $this->_fields = CRM_Core_BAO_UFGroup::getListingFields($this->_action, | |
299 | CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY, | |
300 | FALSE, | |
301 | $gids, | |
302 | TRUE, NULL, | |
303 | $this->_skipPermission, | |
304 | CRM_Core_Permission::SEARCH | |
305 | ); | |
306 | } | |
307 | else { | |
308 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($gids, FALSE, NULL, | |
309 | NULL, NULL, | |
310 | FALSE, NULL, | |
311 | $this->_skipPermission, | |
312 | NULL, | |
313 | ($this->_action == CRM_Core_Action::ADD) ? CRM_Core_Permission::CREATE : CRM_Core_Permission::EDIT | |
314 | ); | |
315 | $multiRecordFieldListing = FALSE; | |
316 | //using selector for listing of multirecord fields | |
317 | if ($this->_mode == self::MODE_EDIT && $this->_gid) { | |
318 | CRM_Core_BAO_UFGroup::shiftMultiRecordFields($this->_fields, $this->_multiRecordFields); | |
319 | ||
320 | if ($this->_multiRecord) { | |
321 | if ($this->_multiRecord != CRM_Core_Action::ADD) { | |
322 | $this->_recordId = CRM_Utils_Request::retrieve('recordId', 'Positive', $this); | |
0db6c3e1 TO |
323 | } |
324 | else { | |
6a488035 TO |
325 | $this->_recordId = NULL; |
326 | $this->set('recordId', NULL); | |
327 | } | |
b44e3f84 | 328 | //record id is necessary for _multiRecord view and update/edit action |
6a488035 | 329 | if (!$this->_recordId |
353ffa53 TO |
330 | && ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE) |
331 | ) { | |
6a488035 TO |
332 | CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) requires record id while performing this action', |
333 | array(1 => $this->_gid) | |
334 | )); | |
0db6c3e1 TO |
335 | } |
336 | elseif (empty($this->_multiRecordFields)) { | |
dc98079b | 337 | CRM_Core_Error::fatal(ts('No Multi-Record Fields configured for this profile (gid=%1)', |
6a488035 TO |
338 | array(1 => $this->_gid) |
339 | )); | |
340 | } | |
341 | ||
342 | $fieldId = CRM_Core_BAO_CustomField::getKeyID(key($this->_multiRecordFields)); | |
343 | $customGroupDetails = CRM_Core_BAO_CustomGroup::getGroupTitles(array($fieldId)); | |
344 | $this->_customGroupTitle = $customGroupDetails[$fieldId]['groupTitle']; | |
345 | $this->_customGroupId = $customGroupDetails[$fieldId]['groupID']; | |
346 | ||
347 | if ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE) { | |
348 | //record exists check | |
349 | foreach ($this->_multiRecordFields as $key => $field) { | |
350 | $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); | |
351 | } | |
352 | $getValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); | |
353 | ||
354 | if (array_key_exists($this->_recordId, $getValues)) { | |
355 | $this->_recordExists = TRUE; | |
0db6c3e1 TO |
356 | } |
357 | else { | |
6a488035 TO |
358 | $this->_recordExists = FALSE; |
359 | if ($this->_multiRecord & CRM_Core_Action::UPDATE) { | |
360 | CRM_Core_Session::setStatus(ts('Note: The record %1 doesnot exists. Upon save a new record will be create', array(1 => $this->_recordId)), ts('Record doesnot exist'), 'alert'); | |
361 | } | |
362 | } | |
363 | } | |
364 | if ($this->_multiRecord & CRM_Core_Action::ADD) { | |
365 | $this->_maxRecordLimit = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupDetails[$fieldId]['groupID'], $this->_id); | |
366 | if ($this->_maxRecordLimit) { | |
367 | CRM_Core_Session::setStatus(ts('You cannot add a new record as maximum allowed limit is reached'), ts('Sorry'), 'error'); | |
368 | } | |
369 | } | |
370 | ||
0db6c3e1 TO |
371 | } |
372 | elseif (!empty($this->_multiRecordFields) | |
353ffa53 | 373 | && (!$this->_multiRecord || !in_array($this->_multiRecord, array( |
c5c263ca AH |
374 | CRM_Core_Action::DELETE, |
375 | CRM_Core_Action::UPDATE, | |
376 | ))) | |
353ffa53 | 377 | ) { |
96ed17aa | 378 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header'); |
6a488035 TO |
379 | //multirecord listing page |
380 | $multiRecordFieldListing = TRUE; | |
381 | $page = new CRM_Profile_Page_MultipleRecordFieldsListing(); | |
382 | $cs = $this->get('cs'); | |
383 | $page->set('pageCheckSum', $cs); | |
384 | $page->set('contactId', $this->_id); | |
385 | $page->set('profileId', $this->_gid); | |
386 | $page->set('action', CRM_Core_Action::BROWSE); | |
387 | $page->set('multiRecordFieldListing', $multiRecordFieldListing); | |
388 | $page->run(); | |
389 | } | |
390 | } | |
391 | $this->assign('multiRecordFieldListing', $multiRecordFieldListing); | |
392 | ||
393 | // is profile double-opt in? | |
a7488080 | 394 | if (!empty($this->_fields['group']) && |
6a488035 TO |
395 | CRM_Core_BAO_UFGroup::isProfileDoubleOptin() |
396 | ) { | |
397 | $emailField = FALSE; | |
398 | foreach ($this->_fields as $name => $values) { | |
399 | if (substr($name, 0, 6) == 'email-') { | |
400 | $emailField = TRUE; | |
401 | } | |
402 | } | |
403 | ||
404 | if (!$emailField) { | |
405 | $status = ts("Email field should be included in profile if you want to use Group(s) when Profile double-opt in process is enabled."); | |
406 | $this->_session->setStatus($status); | |
407 | } | |
408 | } | |
409 | ||
426ac5e0 | 410 | //transferring all the multi-record custom fields in _fields |
6a488035 TO |
411 | if ($this->_multiRecord && !empty($this->_multiRecordFields)) { |
412 | $this->_fields = $this->_multiRecordFields; | |
413 | $this->_multiRecordProfile = TRUE; | |
0db6c3e1 TO |
414 | } |
415 | elseif ($this->_multiRecord && empty($this->_multiRecordFields)) { | |
6a488035 | 416 | CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); |
426ac5e0 | 417 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); |
6a488035 TO |
418 | } |
419 | } | |
420 | ||
421 | if (!is_array($this->_fields)) { | |
422 | CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); | |
426ac5e0 | 423 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); |
6a488035 TO |
424 | } |
425 | } | |
426 | ||
427 | /** | |
c490a46a | 428 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
429 | * the default values are retrieved from the database |
430 | * | |
6a488035 TO |
431 | * |
432 | * @return void | |
433 | */ | |
00be9182 | 434 | public function setDefaultsValues() { |
6a488035 TO |
435 | $this->_defaults = array(); |
436 | if ($this->_multiRecordProfile && ($this->_multiRecord == CRM_Core_Action::DELETE)) { | |
437 | return; | |
438 | } | |
439 | ||
440 | if ($this->_mode != self::MODE_SEARCH) { | |
441 | // set default values for country / state to start with | |
442 | CRM_Core_BAO_UFGroup::setRegisterDefaults($this->_fields, $this->_defaults); | |
443 | } | |
444 | ||
445 | if ($this->_id && !$this->_multiRecordProfile) { | |
446 | if ($this->_isContactActivityProfile) { | |
447 | $contactFields = $activityFields = array(); | |
448 | foreach ($this->_fields as $fieldName => $field) { | |
449 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { | |
450 | $activityFields[$fieldName] = $field; | |
451 | } | |
452 | else { | |
453 | $contactFields[$fieldName] = $field; | |
454 | } | |
455 | } | |
456 | ||
457 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_id, $contactFields, $this->_defaults, TRUE); | |
458 | if ($this->_activityId) { | |
459 | CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $this->_activityId, 'Activity', $this->_defaults, TRUE); | |
460 | } | |
461 | } | |
462 | else { | |
463 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_id, $this->_fields, $this->_defaults, TRUE); | |
464 | } | |
465 | } | |
466 | ||
467 | //set custom field defaults | |
468 | if ($this->_multiRecordProfile) { | |
469 | foreach ($this->_multiRecordFields as $key => $field) { | |
470 | $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); | |
471 | } | |
472 | ||
dc98079b | 473 | $defaultValues = array(); |
6a488035 TO |
474 | if ($this->_multiRecord && $this->_multiRecord == CRM_Core_Action::UPDATE) { |
475 | $defaultValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); | |
476 | if ($this->_recordExists == TRUE) { | |
477 | $defaultValues = $defaultValues[$this->_recordId]; | |
0db6c3e1 TO |
478 | } |
479 | else { | |
6a488035 TO |
480 | $defaultValues = NULL; |
481 | } | |
482 | } | |
483 | ||
484 | if (!empty($defaultValues)) { | |
485 | foreach ($defaultValues as $key => $value) { | |
486 | $name = "custom_{$key}"; | |
487 | $htmlType = $this->_multiRecordFields[$name]['html_type']; | |
488 | if ($htmlType != 'File') { | |
489 | if (isset($value)) { | |
490 | CRM_Core_BAO_CustomField::setProfileDefaults($key, | |
491 | $name, | |
492 | $this->_defaults, | |
493 | $this->_id, | |
494 | $this->_mode, | |
495 | $value | |
496 | ); | |
0db6c3e1 TO |
497 | } |
498 | else { | |
6a488035 TO |
499 | $this->_defaults[$name] = ""; |
500 | } | |
501 | } | |
502 | ||
503 | if ($htmlType == 'File') { | |
504 | $entityId = $this->_id; | |
505 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity' && | |
353ffa53 TO |
506 | $this->_activityId |
507 | ) { | |
6a488035 TO |
508 | $entityId = $this->_activityId; |
509 | } | |
510 | $url = CRM_Core_BAO_CustomField::getFileURL($entityId, $key); | |
511 | ||
512 | if ($url) { | |
513 | $customFiles[$name]['displayURL'] = ts("Attached File") . ": {$url['file_url']}"; | |
514 | ||
515 | $deleteExtra = ts("Are you sure you want to delete attached file?"); | |
353ffa53 TO |
516 | $fileId = $url['file_id']; |
517 | $deleteURL = CRM_Utils_System::url('civicrm/file', | |
518 | "reset=1&id={$fileId}&eid=$entityId&fid={$key}&action=delete" | |
519 | ); | |
6a488035 TO |
520 | $text = ts("Delete Attached File"); |
521 | $customFiles[$field['name']]['deleteURL'] = "<a href=\"{$deleteURL}\" onclick = \"if (confirm( ' $deleteExtra ' )) this.href+='&confirmed=1'; else return false;\">$text</a>"; | |
522 | ||
523 | // also delete the required rule that we've set on the form element | |
524 | $this->removeFileRequiredRules($name); | |
525 | } | |
526 | } | |
527 | } | |
528 | } | |
455d8c42 | 529 | } |
530 | else { | |
6a488035 TO |
531 | foreach ($this->_fields as $name => $field) { |
532 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
533 | $htmlType = $field['html_type']; | |
534 | if ((!isset($this->_defaults[$name]) || $htmlType == 'File') && | |
353ffa53 TO |
535 | (CRM_Utils_Array::value('field_type', $field) != 'Activity') |
536 | ) { | |
6a488035 TO |
537 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, |
538 | $name, | |
539 | $this->_defaults, | |
540 | $this->_id, | |
541 | $this->_mode | |
542 | ); | |
543 | } | |
544 | ||
545 | if ($htmlType == 'File') { | |
546 | $entityId = $this->_id; | |
455d8c42 | 547 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity' && $this->_activityId) { |
6a488035 TO |
548 | $entityId = $this->_activityId; |
549 | } | |
550 | $url = CRM_Core_BAO_CustomField::getFileURL($entityId, $customFieldID); | |
551 | ||
552 | if ($url) { | |
553 | $customFiles[$field['name']]['displayURL'] = ts("Attached File") . ": {$url['file_url']}"; | |
554 | ||
555 | $deleteExtra = ts("Are you sure you want to delete attached file?"); | |
353ffa53 TO |
556 | $fileId = $url['file_id']; |
557 | $deleteURL = CRM_Utils_System::url('civicrm/file', | |
558 | "reset=1&id={$fileId}&eid=$entityId&fid={$customFieldID}&action=delete" | |
6a488035 TO |
559 | ); |
560 | $text = ts("Delete Attached File"); | |
561 | $customFiles[$field['name']]['deleteURL'] = "<a href=\"{$deleteURL}\" onclick = \"if (confirm( ' $deleteExtra ' )) this.href+='&confirmed=1'; else return false;\">$text</a>"; | |
562 | ||
563 | // also delete the required rule that we've set on the form element | |
564 | $this->removeFileRequiredRules($field['name']); | |
565 | } | |
566 | } | |
567 | } | |
568 | } | |
569 | } | |
570 | if (isset($customFiles)) { | |
571 | $this->assign('customFiles', $customFiles); | |
572 | } | |
573 | ||
574 | if ($this->_multiRecordProfile) { | |
575 | $this->setDefaults($this->_defaults); | |
576 | return; | |
577 | } | |
578 | ||
a7488080 | 579 | if (!empty($this->_defaults['image_URL'])) { |
77d45291 | 580 | list($imageWidth, $imageHeight) = getimagesize(CRM_Utils_String::unstupifyUrl($this->_defaults['image_URL'])); |
6a488035 TO |
581 | list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); |
582 | $this->assign("imageWidth", $imageWidth); | |
583 | $this->assign("imageHeight", $imageHeight); | |
584 | $this->assign("imageThumbWidth", $imageThumbWidth); | |
585 | $this->assign("imageThumbHeight", $imageThumbHeight); | |
586 | $this->assign("imageURL", $this->_defaults['image_URL']); | |
455d8c42 | 587 | $this->removeFileRequiredRules('image_URL'); |
6a488035 TO |
588 | } |
589 | ||
590 | if (array_key_exists('contact_sub_type', $this->_defaults) && | |
591 | !empty($this->_defaults['contact_sub_type']) | |
592 | ) { | |
593 | $this->_defaults['contact_sub_type'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
594 | trim($this->_defaults['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR) | |
595 | ); | |
596 | } | |
597 | ||
598 | $this->setDefaults($this->_defaults); | |
599 | } | |
600 | ||
601 | /** | |
fe482240 | 602 | * Build the form object. |
6a488035 TO |
603 | * |
604 | * @return void | |
6a488035 TO |
605 | */ |
606 | public function buildQuickForm() { | |
a3dbd1bc TO |
607 | $this->add('hidden', 'gid', $this->_gid); |
608 | ||
a8387f19 TO |
609 | switch ($this->_mode) { |
610 | case self::MODE_CREATE: | |
611 | case self::MODE_EDIT: | |
612 | case self::MODE_REGISTER: | |
a3dbd1bc | 613 | CRM_Utils_Hook::buildProfile($this->_ufGroup['name']); |
a8387f19 | 614 | break; |
dc98079b | 615 | |
a8387f19 | 616 | case self::MODE_SEARCH: |
a3dbd1bc | 617 | CRM_Utils_Hook::searchProfile($this->_ufGroup['name']); |
a8387f19 | 618 | break; |
dc98079b | 619 | |
a8387f19 TO |
620 | default: |
621 | } | |
622 | ||
6a488035 TO |
623 | //lets have single status message, CRM-4363 |
624 | $return = FALSE; | |
625 | $statusMessage = NULL; | |
626 | if (($this->_multiRecord & CRM_Core_Action::ADD) && $this->_maxRecordLimit) { | |
627 | return; | |
628 | } | |
629 | ||
630 | if (($this->_multiRecord & CRM_Core_Action::DELETE)) { | |
631 | if (!$this->_recordExists) { | |
632 | CRM_Core_Session::setStatus(ts('The record %1 doesnot exists', array(1 => $this->_recordId)), ts('Record doesnot exists'), 'alert'); | |
633 | } | |
634 | else { | |
635 | $this->assign('deleteRecord', TRUE); | |
636 | } | |
637 | return; | |
638 | } | |
639 | ||
640 | CRM_Core_BAO_Address::checkContactSharedAddressFields($this->_fields, $this->_id); | |
641 | ||
642 | // we should not allow component and mix profiles in search mode | |
643 | if ($this->_mode != self::MODE_REGISTER) { | |
644 | //check for mix profile fields (eg: individual + other contact type) | |
645 | if (CRM_Core_BAO_UFField::checkProfileType($this->_gid)) { | |
646 | if (($this->_mode & self::MODE_EDIT) && $this->_isContactActivityProfile) { | |
647 | $errors = self::validateContactActivityProfile($this->_activityId, $this->_id, $this->_gid); | |
648 | if (!empty($errors)) { | |
649 | $statusMessage = array_pop($errors); | |
650 | $return = TRUE; | |
651 | } | |
652 | } | |
653 | else { | |
654 | $statusMessage = ts('Profile search, view and edit are not supported for Profiles which include fields for more than one record type.'); | |
655 | $return = TRUE; | |
656 | } | |
657 | } | |
658 | ||
659 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); | |
660 | ||
661 | if ($this->_id) { | |
662 | $contactTypes = CRM_Contact_BAO_Contact::getContactTypes($this->_id); | |
353ffa53 | 663 | $contactType = $contactTypes[0]; |
6a488035 TO |
664 | |
665 | array_shift($contactTypes); | |
666 | $contactSubtypes = $contactTypes; | |
667 | ||
668 | $profileSubType = FALSE; | |
669 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { | |
670 | $profileSubType = $profileType; | |
671 | $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType); | |
672 | } | |
673 | ||
674 | if ( | |
675 | ($profileType != 'Contact' && !$this->_isContactActivityProfile) && | |
dc98079b | 676 | (($profileSubType && !empty($contactSubtypes) && (!in_array($profileSubType, $contactSubtypes))) || |
6a488035 TO |
677 | ($profileType != $contactType)) |
678 | ) { | |
679 | $return = TRUE; | |
680 | if (!$statusMessage) { | |
d5cc0fc2 | 681 | $statusMessage = ts("This profile is configured for contact type '%1'. It cannot be used to edit contacts of other types.", |
6a488035 TO |
682 | array(1 => $profileSubType ? $profileSubType : $profileType)); |
683 | } | |
684 | } | |
685 | } | |
686 | ||
687 | if ( | |
353ffa53 TO |
688 | in_array( |
689 | $profileType, | |
690 | array("Membership", "Participant", "Contribution") | |
691 | ) | |
6a488035 TO |
692 | ) { |
693 | $return = TRUE; | |
694 | if (!$statusMessage) { | |
695 | $statusMessage = ts('Profile is not configured for the selected action.'); | |
696 | } | |
697 | } | |
698 | } | |
699 | ||
426ac5e0 | 700 | //lets have single status message, |
6a488035 TO |
701 | $this->assign('statusMessage', $statusMessage); |
702 | if ($return) { | |
703 | return FALSE; | |
704 | } | |
705 | ||
706 | $this->assign('id', $this->_id); | |
707 | $this->assign('mode', $this->_mode); | |
708 | $this->assign('action', $this->_action); | |
709 | $this->assign('fields', $this->_fields); | |
710 | $this->assign('fieldset', (isset($this->_fieldset)) ? $this->_fieldset : ""); | |
711 | ||
712 | // should we restrict what we display | |
713 | $admin = TRUE; | |
714 | if ($this->_mode == self::MODE_EDIT) { | |
715 | $admin = FALSE; | |
716 | // show all fields that are visibile: | |
717 | // if we are a admin OR the same user OR acl-user with access to the profile | |
718 | // or we have checksum access to this contact (i.e. the user without a login) - CRM-5909 | |
719 | if ( | |
720 | CRM_Core_Permission::check('administer users') || | |
721 | $this->_id == $this->_currentUserID || | |
722 | $this->_isPermissionedChecksum || | |
723 | in_array( | |
724 | $this->_gid, | |
725 | CRM_ACL_API::group( | |
726 | CRM_Core_Permission::EDIT, | |
727 | NULL, | |
728 | 'civicrm_uf_group', | |
ff4f7744 | 729 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id') |
6a488035 TO |
730 | ) |
731 | ) | |
732 | ) { | |
733 | $admin = TRUE; | |
734 | } | |
735 | } | |
736 | ||
737 | // if false, user is not logged-in. | |
738 | $anonUser = FALSE; | |
739 | if (!$this->_currentUserID) { | |
740 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
741 | $primaryLocationType = $defaultLocationType->id; | |
353ffa53 | 742 | $anonUser = TRUE; |
6a488035 TO |
743 | } |
744 | $this->assign('anonUser', $anonUser); | |
745 | ||
746 | $addCaptcha = array(); | |
747 | $emailPresent = FALSE; | |
748 | ||
6a488035 TO |
749 | // add the form elements |
750 | foreach ($this->_fields as $name => $field) { | |
751 | // make sure that there is enough permission to expose this field | |
752 | if (!$admin && $field['visibility'] == 'User and User Admin Only') { | |
753 | unset($this->_fields[$name]); | |
754 | continue; | |
755 | } | |
756 | ||
757 | // since the CMS manages the email field, suppress the email display if in | |
758 | // register mode which occur within the CMS form | |
759 | if ($this->_mode == self::MODE_REGISTER && substr($name, 0, 5) == 'email') { | |
760 | unset($this->_fields[$name]); | |
761 | continue; | |
762 | } | |
763 | ||
764 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
6a488035 TO |
765 | |
766 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, $this->_mode); | |
767 | ||
dd78acd9 TO |
768 | if ($field['add_to_group_id']) { |
769 | $addToGroupId = $field['add_to_group_id']; | |
770 | } | |
771 | ||
6a488035 TO |
772 | //build array for captcha |
773 | if ($field['add_captcha']) { | |
774 | $addCaptcha[$field['group_id']] = $field['add_captcha']; | |
775 | } | |
776 | ||
777 | if (($name == 'email-Primary') || ($name == 'email-' . isset($primaryLocationType) ? $primaryLocationType : "")) { | |
778 | $emailPresent = TRUE; | |
779 | $this->_mail = $name; | |
780 | } | |
781 | } | |
782 | ||
783 | // add captcha only for create mode. | |
784 | if ($this->_mode == self::MODE_CREATE) { | |
785 | // suppress captcha for logged in users only | |
786 | if ($this->_currentUserID) { | |
787 | $this->_isAddCaptcha = FALSE; | |
788 | } | |
4c9b6178 | 789 | elseif (!$this->_isAddCaptcha && !empty($addCaptcha)) { |
6a488035 TO |
790 | $this->_isAddCaptcha = TRUE; |
791 | } | |
792 | ||
793 | if ($this->_gid) { | |
794 | $dao = new CRM_Core_DAO_UFGroup(); | |
795 | $dao->id = $this->_gid; | |
796 | $dao->addSelect(); | |
797 | $dao->addSelect('is_update_dupe'); | |
798 | if ($dao->find(TRUE)) { | |
799 | if ($dao->is_update_dupe) { | |
800 | $this->_isUpdateDupe = $dao->is_update_dupe; | |
801 | } | |
802 | } | |
803 | } | |
804 | } | |
805 | else { | |
806 | $this->_isAddCaptcha = FALSE; | |
807 | } | |
808 | ||
809 | //finally add captcha to form. | |
810 | if ($this->_isAddCaptcha) { | |
811 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); | |
812 | $captcha->add($this); | |
813 | } | |
814 | $this->assign("isCaptcha", $this->_isAddCaptcha); | |
815 | ||
dd78acd9 TO |
816 | if ($this->_mode != self::MODE_SEARCH) { |
817 | if (isset($addToGroupId)) { | |
818 | $this->_ufGroup['add_to_group_id'] = $addToGroupId; | |
819 | } | |
820 | } | |
821 | ||
7f1a247f | 822 | //let's do set defaults for the profile |
823 | $this->setDefaultsValues(); | |
824 | ||
6a488035 | 825 | $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, NULL); |
2be4a151 BS |
826 | |
827 | if ($this->_mode == self::MODE_CREATE || $this->_mode == self::MODE_EDIT) { | |
6a488035 TO |
828 | CRM_Core_BAO_CMSUser::buildForm($this, $this->_gid, $emailPresent, $action); |
829 | } | |
830 | else { | |
831 | $this->assign('showCMS', FALSE); | |
832 | } | |
833 | ||
834 | $this->assign('groupId', $this->_gid); | |
835 | ||
6a488035 TO |
836 | // if view mode pls freeze it with the done button. |
837 | if ($this->_action & CRM_Core_Action::VIEW) { | |
838 | $this->freeze(); | |
839 | } | |
840 | ||
841 | if ($this->_context == 'dialog') { | |
842 | $this->addElement( | |
843 | 'submit', | |
844 | $this->_duplicateButtonName, | |
845 | ts('Save Matching Contact') | |
846 | ); | |
847 | } | |
848 | } | |
849 | ||
ffd93213 | 850 | /** |
fe482240 | 851 | * Validate profile and provided activity Id. |
c490a46a CW |
852 | * |
853 | * @param int $activityId | |
854 | * @param int $contactId | |
855 | * @param int $gid | |
ffd93213 EM |
856 | * |
857 | * @return array | |
858 | */ | |
00be9182 | 859 | public static function validateContactActivityProfile($activityId, $contactId, $gid) { |
6a488035 TO |
860 | $errors = array(); |
861 | if (!$activityId) { | |
862 | $errors[] = 'Profile is using one or more activity fields, and is missing the activity Id (aid) in the URL.'; | |
863 | return $errors; | |
864 | } | |
865 | ||
866 | $activityDetails = array(); | |
867 | $activityParams = array('id' => $activityId); | |
868 | CRM_Activity_BAO_Activity::retrieve($activityParams, $activityDetails); | |
869 | ||
870 | if (empty($activityDetails)) { | |
871 | $errors[] = 'Invalid Activity Id (aid).'; | |
872 | return $errors; | |
873 | } | |
874 | ||
875 | $profileActivityTypes = CRM_Core_BAO_UFGroup::groupTypeValues($gid, 'Activity'); | |
876 | ||
8cc574cf | 877 | if ((!empty($profileActivityTypes['Activity']) && |
6a488035 TO |
878 | !in_array($activityDetails['activity_type_id'], $profileActivityTypes['Activity']) |
879 | ) || | |
880 | (!in_array($contactId, $activityDetails['assignee_contact']) && | |
881 | !in_array($contactId, $activityDetails['target_contact']) | |
882 | ) | |
883 | ) { | |
884 | $errors[] = 'This activity cannot be edited or viewed via this profile.'; | |
885 | } | |
886 | ||
887 | return $errors; | |
888 | } | |
889 | ||
890 | /** | |
fe482240 | 891 | * Global form rule. |
6a488035 | 892 | * |
68c9fb83 TO |
893 | * @param array $fields |
894 | * The input form values. | |
895 | * @param array $files | |
896 | * The uploaded files if any. | |
897 | * @param CRM_Core_Form $form | |
898 | * The form object. | |
6a488035 | 899 | * |
72b3a70c CW |
900 | * @return bool|array |
901 | * true if no errors, else array of errors | |
6a488035 | 902 | */ |
00be9182 | 903 | public static function formRule($fields, $files, $form) { |
a3dbd1bc | 904 | CRM_Utils_Hook::validateProfile($form->_ufGroup['name']); |
a8387f19 | 905 | |
6a488035 TO |
906 | $errors = array(); |
907 | // if no values, return | |
908 | if (empty($fields)) { | |
909 | return TRUE; | |
910 | } | |
911 | ||
912 | $register = NULL; | |
913 | ||
914 | // hack we use a -1 in options to indicate that its registration | |
915 | if ($form->_id) { | |
916 | $form->_isUpdateDupe = 1; | |
917 | } | |
918 | ||
919 | if ($form->_mode == CRM_Profile_Form::MODE_REGISTER) { | |
920 | $register = TRUE; | |
921 | } | |
922 | ||
923 | // dont check for duplicates during registration validation: CRM-375 | |
8cc574cf | 924 | if (!$register && empty($fields['_qf_Edit_upload_duplicate'])) { |
6a488035 | 925 | // fix for CRM-3240 |
a7488080 | 926 | if (!empty($fields['email-Primary'])) { |
6a488035 TO |
927 | $fields['email'] = CRM_Utils_Array::value('email-Primary', $fields); |
928 | } | |
929 | ||
930 | // fix for CRM-6141 | |
8cc574cf | 931 | if (!empty($fields['phone-Primary-1']) && empty($fields['phone-Primary'])) { |
6a488035 TO |
932 | $fields['phone-Primary'] = $fields['phone-Primary-1']; |
933 | } | |
934 | ||
935 | $ctype = CRM_Core_BAO_UFGroup::getContactType($form->_gid); | |
936 | // If all profile fields is of Contact Type then consider | |
937 | // profile is of Individual type(default). | |
938 | if (!$ctype) { | |
939 | $ctype = 'Individual'; | |
940 | } | |
941 | $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, $ctype); | |
942 | if ($form->_mode == CRM_Profile_Form::MODE_CREATE) { | |
943 | // fix for CRM-2888 | |
944 | $exceptions = array(); | |
945 | } | |
946 | else { | |
947 | // for edit mode we need to allow our own record to be a dupe match! | |
948 | $exceptions = array($form->_session->get('userID')); | |
949 | } | |
950 | ||
951 | // for dialog mode we should always use fuzzy rule. | |
952 | $ruleType = 'Unsupervised'; | |
953 | if ($form->_context == 'dialog') { | |
954 | $ruleType = 'Supervised'; | |
955 | } | |
956 | ||
957 | $dedupeParams['check_permission'] = FALSE; | |
958 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, | |
959 | $ctype, | |
960 | $ruleType, | |
90d804ed ML |
961 | $exceptions, |
962 | $form->_ruleGroupID | |
6a488035 TO |
963 | ); |
964 | if ($ids) { | |
965 | if ($form->_isUpdateDupe == 2) { | |
966 | CRM_Core_Session::setStatus(ts('Note: this contact may be a duplicate of an existing record.'), ts('Possible Duplicate Detected'), 'alert'); | |
967 | } | |
968 | elseif ($form->_isUpdateDupe == 1) { | |
969 | if (!$form->_id) { | |
970 | $form->_id = $ids[0]; | |
971 | } | |
972 | } | |
973 | else { | |
974 | if ($form->_context == 'dialog') { | |
975 | $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE); | |
976 | ||
977 | $duplicateContactsLinks = '<div class="matching-contacts-found">'; | |
353ffa53 | 978 | $duplicateContactsLinks .= ts('One matching contact was found. ', array( |
c5c263ca AH |
979 | 'count' => count($contactLinks['rows']), |
980 | 'plural' => '%count matching contacts were found.<br />', | |
981 | )); | |
6a488035 | 982 | if ($contactLinks['msg'] == 'view') { |
353ffa53 | 983 | $duplicateContactsLinks .= ts('You can View the existing contact.', array( |
c5c263ca AH |
984 | 'count' => count($contactLinks['rows']), |
985 | 'plural' => 'You can View the existing contacts.', | |
986 | )); | |
6a488035 TO |
987 | } |
988 | else { | |
353ffa53 | 989 | $duplicateContactsLinks .= ts('You can View or Edit the existing contact.', array( |
c5c263ca AH |
990 | 'count' => count($contactLinks['rows']), |
991 | 'plural' => 'You can View or Edit the existing contacts.', | |
992 | )); | |
6a488035 TO |
993 | } |
994 | $duplicateContactsLinks .= '</div>'; | |
995 | $duplicateContactsLinks .= '<table class="matching-contacts-actions">'; | |
996 | $row = ''; | |
d5cc0fc2 | 997 | for ($i = 0; $i < count($contactLinks['rows']); $i++) { |
6a488035 TO |
998 | $row .= ' <tr> '; |
999 | $row .= ' <td class="matching-contacts-name"> '; | |
1000 | $row .= $contactLinks['rows'][$i]['display_name']; | |
1001 | $row .= ' </td>'; | |
1002 | $row .= ' <td class="matching-contacts-email"> '; | |
1003 | $row .= $contactLinks['rows'][$i]['primary_email']; | |
1004 | $row .= ' </td>'; | |
1005 | $row .= ' <td class="action-items"> '; | |
1006 | $row .= $contactLinks['rows'][$i]['view'] . ' '; | |
1007 | $row .= $contactLinks['rows'][$i]['edit']; | |
1008 | $row .= ' </td>'; | |
1009 | $row .= ' </tr> '; | |
1010 | } | |
1011 | ||
1012 | $duplicateContactsLinks .= $row . '</table>'; | |
1013 | $duplicateContactsLinks .= "If you're sure this record is not a duplicate, click the 'Save Matching Contact' button below."; | |
1014 | ||
1015 | $errors['_qf_default'] = $duplicateContactsLinks; | |
1016 | ||
1017 | // let smarty know that there are duplicates | |
1018 | $template = CRM_Core_Smarty::singleton(); | |
1019 | $template->assign('isDuplicate', 1); | |
1020 | } | |
1021 | else { | |
1022 | $errors['_qf_default'] = ts('A record already exists with the same information.'); | |
1023 | } | |
1024 | } | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | foreach ($fields as $key => $value) { | |
1029 | list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $key, 3); | |
1030 | if ($fieldName == 'state_province' && !empty($fields["country-{$locTypeId}"])) { | |
1031 | // Validate Country - State list | |
1032 | $countryId = $fields["country-{$locTypeId}"]; | |
1033 | $stateProvinceId = $value; | |
1034 | ||
1035 | if ($stateProvinceId && $countryId) { | |
1036 | $stateProvinceDAO = new CRM_Core_DAO_StateProvince(); | |
1037 | $stateProvinceDAO->id = $stateProvinceId; | |
1038 | $stateProvinceDAO->find(TRUE); | |
1039 | ||
1040 | if ($stateProvinceDAO->country_id != $countryId) { | |
1041 | // country mismatch hence display error | |
1042 | $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); | |
353ffa53 TO |
1043 | $countries = CRM_Core_PseudoConstant::country(); |
1044 | $errors[$key] = "State/Province " . $stateProvinces[$stateProvinceId] . " is not part of " . $countries[$countryId] . ". It belongs to " . $countries[$stateProvinceDAO->country_id] . "."; | |
6a488035 TO |
1045 | } |
1046 | } | |
1047 | } | |
1048 | ||
1049 | if ($fieldName == 'county' && $fields["state_province-{$locTypeId}"]) { | |
1050 | // Validate County - State list | |
1051 | $stateProvinceId = $fields["state_province-{$locTypeId}"]; | |
1052 | $countyId = $value; | |
1053 | ||
1054 | if ($countyId && $stateProvinceId) { | |
1055 | $countyDAO = new CRM_Core_DAO_County(); | |
1056 | $countyDAO->id = $countyId; | |
1057 | $countyDAO->find(TRUE); | |
1058 | ||
1059 | if ($countyDAO->state_province_id != $stateProvinceId) { | |
1060 | // state province mismatch hence display error | |
1061 | $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); | |
353ffa53 TO |
1062 | $counties = CRM_Core_PseudoConstant::county(); |
1063 | $errors[$key] = "County " . $counties[$countyId] . " is not part of " . $stateProvinces[$stateProvinceId] . ". It belongs to " . $stateProvinces[$countyDAO->state_province_id] . "."; | |
6a488035 TO |
1064 | } |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
1069 | if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { | |
1070 | $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); | |
1071 | if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) { | |
1072 | $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', | |
1073 | array(1 => ucwords(str_replace('_', ' ', $greeting))) | |
1074 | ); | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | return empty($errors) ? TRUE : $errors; | |
1080 | } | |
1081 | ||
1082 | /** | |
1083 | * Process the user submitted custom data values. | |
1084 | * | |
6a488035 TO |
1085 | * |
1086 | * @return void | |
1087 | */ | |
1088 | public function postProcess() { | |
1089 | $params = $this->controller->exportValues($this->_name); | |
1090 | ||
1091 | //if the delete record button is clicked | |
1092 | if ($this->_deleteButtonName) { | |
a7488080 | 1093 | if (!empty($_POST[$this->_deleteButtonName]) && $this->_recordId) { |
6a488035 | 1094 | $filterParams['id'] = $this->_customGroupId; |
426ac5e0 EM |
1095 | $returnProperties = array('is_multiple', 'table_name'); |
1096 | CRM_Core_DAO::commonRetrieve("CRM_Core_DAO_CustomGroup", $filterParams, $returnValues, $returnProperties); | |
a7488080 | 1097 | if (!empty($returnValues['is_multiple'])) { |
6a488035 TO |
1098 | if ($tableName = CRM_Utils_Array::value('table_name', $returnValues)) { |
1099 | $sql = "DELETE FROM {$tableName} WHERE id = %1 AND entity_id = %2"; | |
1100 | $sqlParams = array( | |
353ffa53 TO |
1101 | 1 => array($this->_recordId, 'Integer'), |
1102 | 2 => array($this->_id, 'Integer'), | |
1103 | ); | |
dc98079b TO |
1104 | CRM_Core_DAO::executeQuery($sql, $sqlParams); |
1105 | CRM_Core_Session::setStatus(ts('Your record has been deleted.'), ts('Deleted'), 'success'); | |
6a488035 TO |
1106 | } |
1107 | } | |
1108 | return; | |
1109 | } | |
1110 | } | |
a3dbd1bc | 1111 | CRM_Utils_Hook::processProfile($this->_ufGroup['name']); |
a7488080 | 1112 | if (!empty($params['image_URL'])) { |
6a488035 TO |
1113 | CRM_Contact_BAO_Contact::processImageParams($params); |
1114 | } | |
1115 | ||
1116 | $greetingTypes = array( | |
1117 | 'addressee' => 'addressee_id', | |
1118 | 'email_greeting' => 'email_greeting_id', | |
1119 | 'postal_greeting' => 'postal_greeting_id', | |
1120 | ); | |
1121 | ||
1122 | $details = array(); | |
1123 | if ($this->_id) { | |
1124 | $contactDetails = CRM_Contact_BAO_Contact::getHierContactDetails($this->_id, | |
1125 | $greetingTypes | |
1126 | ); | |
1127 | $details = $contactDetails[0][$this->_id]; | |
1128 | } | |
8cc574cf | 1129 | if (!(!empty($details['addressee_id']) || !empty($details['email_greeting_id']) || |
353ffa53 TO |
1130 | CRM_Utils_Array::value('postal_greeting_id', $details) |
1131 | ) | |
1132 | ) { | |
6a488035 TO |
1133 | |
1134 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); | |
1135 | //Though Profile type is contact we need | |
1136 | //Individual/Household/Organization for setting Greetings. | |
1137 | if ($profileType == 'Contact') { | |
1138 | $profileType = 'Individual'; | |
1139 | //if we editing Household/Organization. | |
1140 | if ($this->_id) { | |
1141 | $profileType = CRM_Contact_BAO_Contact::getContactType($this->_id); | |
1142 | } | |
1143 | } | |
1144 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { | |
1145 | $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType); | |
1146 | } | |
1147 | ||
1148 | foreach ($greetingTypes as $key => $value) { | |
1149 | if (!array_key_exists($key, $params)) { | |
1150 | $params[$key] = CRM_Contact_BAO_Contact_Utils::defaultGreeting($profileType, $key); | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | $transaction = new CRM_Core_Transaction(); | |
1156 | ||
426ac5e0 | 1157 | //used to send subscribe mail to the group which user want. |
6a488035 TO |
1158 | //if the profile double option in is enabled |
1159 | $mailingType = array(); | |
6a488035 TO |
1160 | |
1161 | $result = NULL; | |
1162 | foreach ($params as $name => $values) { | |
1163 | if (substr($name, 0, 6) == 'email-') { | |
1164 | $result['email'] = $values; | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | //array of group id, subscribed by contact | |
1169 | $contactGroup = array(); | |
a7488080 | 1170 | if (!empty($params['group']) && |
6a488035 TO |
1171 | CRM_Core_BAO_UFGroup::isProfileDoubleOptin() |
1172 | ) { | |
1173 | $groupSubscribed = array(); | |
a7488080 | 1174 | if (!empty($result['email'])) { |
6a488035 TO |
1175 | if ($this->_id) { |
1176 | $contactGroups = new CRM_Contact_DAO_GroupContact(); | |
1177 | $contactGroups->contact_id = $this->_id; | |
1178 | $contactGroups->status = 'Added'; | |
1179 | $contactGroups->find(); | |
1180 | $contactGroup = array(); | |
1181 | while ($contactGroups->fetch()) { | |
1182 | $contactGroup[] = $contactGroups->group_id; | |
1183 | $groupSubscribed[$contactGroups->group_id] = 1; | |
1184 | } | |
1185 | } | |
1186 | foreach ($params['group'] as $key => $val) { | |
1187 | if (!$val) { | |
1188 | unset($params['group'][$key]); | |
1189 | continue; | |
1190 | } | |
1191 | $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
1192 | $key, 'group_type', 'id' | |
1193 | ); | |
1194 | $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1195 | substr($groupTypes, 1, -1) | |
1196 | ); | |
1197 | //filter group of mailing type and unset it from params | |
1198 | if (in_array(2, $groupType)) { | |
1199 | //if group is already subscribed , ignore it | |
1200 | $groupExist = CRM_Utils_Array::key($key, $contactGroup); | |
1201 | if (!isset($groupExist)) { | |
1202 | $mailingType[] = $key; | |
1203 | unset($params['group'][$key]); | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | } | |
1208 | } | |
1209 | ||
a3dbd1bc | 1210 | $addToGroupId = CRM_Utils_Array::value('add_to_group_id', $this->_ufGroup); |
bada0f66 | 1211 | if (!empty($addToGroupId)) { |
6a488035 TO |
1212 | //run same check whether group is a mailing list |
1213 | $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
1214 | $addToGroupId, 'group_type', 'id' | |
1215 | ); | |
1216 | $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1217 | substr($groupTypes, 1, -1) | |
1218 | ); | |
1219 | //filter group of mailing type and unset it from params | |
8cc574cf | 1220 | if (in_array(2, $groupType) && !empty($result['email']) && |
6a488035 TO |
1221 | CRM_Core_BAO_UFGroup::isProfileAddToGroupDoubleOptin() |
1222 | ) { | |
1223 | if (!count($contactGroup)) { | |
1224 | //array of group id, subscribed by contact | |
1225 | $contactGroup = array(); | |
1226 | if ($this->_id) { | |
1227 | $contactGroups = new CRM_Contact_DAO_GroupContact(); | |
1228 | $contactGroups->contact_id = $this->_id; | |
1229 | $contactGroups->status = 'Added'; | |
1230 | $contactGroups->find(); | |
1231 | $contactGroup = array(); | |
1232 | while ($contactGroups->fetch()) { | |
1233 | $contactGroup[] = $contactGroups->group_id; | |
1234 | $groupSubscribed[$contactGroups->group_id] = 1; | |
1235 | } | |
1236 | } | |
1237 | } | |
1238 | //if group is already subscribed , ignore it | |
1239 | $groupExist = CRM_Utils_Array::key($addToGroupId, $contactGroup); | |
1240 | if (!isset($groupExist)) { | |
1241 | $mailingType[] = $addToGroupId; | |
1242 | $addToGroupId = NULL; | |
1243 | } | |
1244 | } | |
1245 | else { | |
1246 | // since we are directly adding contact to group lets unset it from mailing | |
1247 | if ($key = array_search($addToGroupId, $mailingType)) { | |
1248 | unset($mailingType[$key]); | |
1249 | } | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | if ($this->_grid) { | |
1254 | $params['group'] = $groupSubscribed; | |
1255 | } | |
1256 | ||
1257 | // commenting below code, since we potentially | |
1258 | // triggered maximum name field formatting cases during CRM-4430. | |
1259 | // CRM-4343 | |
1260 | // $params['preserveDBName'] = true; | |
1261 | ||
1262 | $profileFields = $this->_fields; | |
1263 | if (($this->_mode & self::MODE_EDIT) && $this->_activityId && $this->_isContactActivityProfile) { | |
1264 | $profileFields = $activityParams = array(); | |
1265 | foreach ($this->_fields as $fieldName => $field) { | |
1266 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { | |
1267 | if (isset($params[$fieldName])) { | |
1268 | $activityParams[$fieldName] = $params[$fieldName]; | |
1269 | } | |
1270 | if (isset($params['activity_date_time'])) { | |
1271 | $activityParams['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); | |
1272 | } | |
a7488080 | 1273 | if (!empty($params[$fieldName]) && isset($params["{$fieldName}_id"])) { |
6a488035 TO |
1274 | $activityParams[$fieldName] = $params["{$fieldName}_id"]; |
1275 | } | |
1276 | } | |
1277 | else { | |
1278 | $profileFields[$fieldName] = $field; | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | if (!empty($activityParams)) { | |
1283 | $activityParams['version'] = 3; | |
1284 | $activityParams['id'] = $this->_activityId; | |
1285 | $activityParams['skipRecentView'] = TRUE; | |
1286 | civicrm_api('Activity', 'create', $activityParams); | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | if ($this->_multiRecord && $this->_recordId && $this->_multiRecordFields && $this->_recordExists) { | |
1291 | $params['customRecordValues'][$this->_recordId] = array_keys($this->_multiRecordFields); | |
1292 | } | |
1293 | ||
1294 | $this->_id = CRM_Contact_BAO_Contact::createProfileContact( | |
1295 | $params, | |
1296 | $profileFields, | |
1297 | $this->_id, | |
1298 | $addToGroupId, | |
1299 | $this->_gid, | |
1300 | $this->_ctype, | |
1301 | TRUE | |
1302 | ); | |
1303 | ||
1304 | //mailing type group | |
1305 | if (!empty($mailingType)) { | |
1306 | // we send in the contactID so we match the same groups and are exact, rather than relying on email | |
1307 | // CRM-8710 | |
1308 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($mailingType, $result, $this->_id, 'profile'); | |
1309 | } | |
1310 | ||
1311 | $ufGroups = array(); | |
1312 | if ($this->_gid) { | |
1313 | $ufGroups[$this->_gid] = 1; | |
1314 | } | |
1315 | elseif ($this->_mode == self::MODE_REGISTER) { | |
1316 | $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration'); | |
1317 | } | |
1318 | ||
1319 | foreach ($ufGroups as $gId => $val) { | |
1320 | if ($notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify')) { | |
1321 | $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $this->_id, NULL); | |
1322 | CRM_Core_BAO_UFGroup::commonSendMail($this->_id, $values); | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | //create CMS user (if CMS user option is selected in profile) | |
a7488080 | 1327 | if (!empty($params['cms_create_account']) && |
8ccd852d | 1328 | ($this->_mode == self::MODE_CREATE || $this->_mode == self::MODE_EDIT) |
6a488035 TO |
1329 | ) { |
1330 | $params['contactID'] = $this->_id; | |
1331 | if (!CRM_Core_BAO_CMSUser::create($params, $this->_mail)) { | |
1332 | CRM_Core_Session::setStatus(ts('Your profile is not saved and Account is not created.'), ts('Profile Error'), 'error'); | |
b24f69db | 1333 | CRM_Core_Error::debug_log_message("Rolling back transaction as CMSUser Create failed in Profile_Form for contact " . $params['contactID']); |
6a488035 TO |
1334 | $transaction->rollback(); |
1335 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/create', | |
353ffa53 TO |
1336 | 'reset=1&gid=' . $this->_gid |
1337 | )); | |
6a488035 TO |
1338 | } |
1339 | } | |
1340 | ||
1341 | $transaction->commit(); | |
1342 | } | |
1343 | ||
ffd93213 EM |
1344 | /** |
1345 | * @param null $suffix | |
1346 | * | |
1347 | * @return null|string | |
1348 | */ | |
00be9182 | 1349 | public function checkTemplateFileExists($suffix = NULL) { |
6a488035 TO |
1350 | if ($this->_gid) { |
1351 | $templateFile = "CRM/Profile/Form/{$this->_gid}/{$this->_name}.{$suffix}tpl"; | |
1352 | $template = CRM_Core_Form::getTemplate(); | |
1353 | if ($template->template_exists($templateFile)) { | |
1354 | return $templateFile; | |
1355 | } | |
1356 | ||
1357 | // lets see if we have customized by name | |
1358 | $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name'); | |
1359 | if ($ufGroupName) { | |
1360 | $templateFile = "CRM/Profile/Form/{$ufGroupName}/{$this->_name}.{$suffix}tpl"; | |
1361 | if ($template->template_exists($templateFile)) { | |
1362 | return $templateFile; | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | return NULL; | |
1367 | } | |
1368 | ||
ffd93213 | 1369 | /** |
fe482240 | 1370 | * Use the form name to create the tpl file name. |
ffd93213 EM |
1371 | * |
1372 | * @return string | |
ffd93213 EM |
1373 | */ |
1374 | /** | |
1375 | * @return string | |
1376 | */ | |
00be9182 | 1377 | public function getTemplateFileName() { |
6a488035 TO |
1378 | $fileName = $this->checkTemplateFileExists(); |
1379 | return $fileName ? $fileName : parent::getTemplateFileName(); | |
1380 | } | |
1381 | ||
ffd93213 EM |
1382 | /** |
1383 | * Default extra tpl file basically just replaces .tpl with .extra.tpl | |
1384 | * i.e. we dont override | |
1385 | * | |
1386 | * @return string | |
ffd93213 EM |
1387 | */ |
1388 | /** | |
1389 | * @return string | |
1390 | */ | |
00be9182 | 1391 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
1392 | $fileName = $this->checkTemplateFileExists('extra.'); |
1393 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
1394 | } | |
96025800 | 1395 | |
6a488035 | 1396 | } |