Merge pull request #17380 from seamuslee001/symfony_4_4
[civicrm-core.git] / CRM / Contact / Form / Inline / CommunicationPreferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form helper class for communication preferences inline edit section.
20 */
21 class CRM_Contact_Form_Inline_CommunicationPreferences extends CRM_Contact_Form_Inline {
22
23 /**
24 * Build the form object elements for communication preferences.
25 */
26 public function buildQuickForm() {
27 parent::buildQuickForm();
28 CRM_Contact_Form_Edit_CommunicationPreferences::buildQuickForm($this);
29 $this->addFormRule(['CRM_Contact_Form_Edit_CommunicationPreferences', 'formRule'], $this);
30 }
31
32 /**
33 * Set defaults for the form.
34 *
35 * @return array
36 */
37 public function setDefaultValues() {
38 $defaults = parent::setDefaultValues();
39
40 if (!empty($defaults['preferred_language'])) {
41 $languages = CRM_Contact_BAO_Contact::buildOptions('preferred_language');
42 $defaults['preferred_language'] = CRM_Utils_Array::key($defaults['preferred_language'], $languages);
43 }
44
45 // CRM-7119: set preferred_language to default if unset
46 if (empty($defaults['preferred_language'])) {
47 $config = CRM_Core_Config::singleton();
48 $defaults['preferred_language'] = $config->lcMessages;
49 }
50
51 // CRM-19135: where CRM_Core_BAO_Contact::getValues() set label as a default value instead of reserved 'value',
52 // the code is to ensure we always set default to value instead of label
53 if (!empty($defaults['preferred_mail_format'])) {
54 $defaults['preferred_mail_format'] = array_search($defaults['preferred_mail_format'], CRM_Core_SelectValues::pmf());
55 }
56
57 if (empty($defaults['communication_style_id'])) {
58 $defaults['communication_style_id'] = array_pop(CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1'));
59 }
60
61 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
62 $name = "{$greeting}_display";
63 $this->assign($name, CRM_Utils_Array::value($name, $defaults));
64 }
65 return $defaults;
66 }
67
68 /**
69 * Process the form.
70 */
71 public function postProcess() {
72 $params = $this->exportValues();
73
74 // Process / save communication preferences
75
76 // this is a chekbox, so mark false if we dont get a POST value
77 $params['is_opt_out'] = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
78 $params['contact_type'] = $this->_contactType;
79 $params['contact_id'] = $this->_contactId;
80
81 if (!empty($this->_contactSubType)) {
82 $params['contact_sub_type'] = $this->_contactSubType;
83 }
84
85 if (!isset($params['preferred_communication_method'])) {
86 $params['preferred_communication_method'] = 'null';
87 }
88 CRM_Contact_BAO_Contact::create($params);
89
90 $this->response();
91 }
92
93 }