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