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