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