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