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