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