fixed spelling of address on lines 122 and 247
[civicrm-core.git] / CRM / Contact / Form / Task / AlterPreferences.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 * This class provides the functionality to alter a privacy
20 * options for selected contacts
21 */
22 class CRM_Contact_Form_Task_AlterPreferences extends CRM_Contact_Form_Task {
23
24 /**
25 * Build the form object.
26 */
27 public function buildQuickForm() {
28 // add select for preferences
29
30 $options = [ts('Add Selected Options'), ts('Remove selected options')];
31
32 $this->addRadio('actionTypeOption', ts('actionTypeOption'), $options);
33
34 $privacyOptions = CRM_Core_SelectValues::privacy();
35
36 foreach ($privacyOptions as $prefID => $prefName) {
37 $this->_prefElement = &$this->addElement('checkbox', "pref[$prefID]", NULL, $prefName);
38 }
39
40 $this->addDefaultButtons(ts('Set Privacy Options'));
41 }
42
43 public function addRules() {
44 $this->addFormRule(['CRM_Contact_Form_Task_AlterPreferences', 'formRule']);
45 }
46
47 /**
48 * Set the default form values.
49 *
50 *
51 * @return array
52 * the default array reference
53 */
54 public function setDefaultValues() {
55 $defaults = [];
56
57 $defaults['actionTypeOption'] = 0;
58 return $defaults;
59 }
60
61 /**
62 * @param CRM_Core_Form $form
63 * @param $rule
64 *
65 * @return array
66 */
67 public static function formRule($form, $rule) {
68 $errors = [];
69 if (empty($form['pref']) && empty($form['contact_taglist'])) {
70 $errors['_qf_default'] = ts("Please select at least one privacy option.");
71 }
72 return $errors;
73 }
74
75 /**
76 * Process the form after the input has been submitted and validated.
77 */
78 public function postProcess() {
79 //get the submitted values in an array
80 $params = $this->controller->exportValues($this->_name);
81
82 $actionTypeOption = $params['actionTypeOption'] ?? NULL;
83 // If remove option has been selected set new privacy value to "false"
84 $privacyValueNew = empty($actionTypeOption);
85
86 // check if any privay option has been checked
87 if (!empty($params['pref'])) {
88 $privacyValues = $params['pref'];
89 $count = 0;
90 foreach ($this->_contactIds as $contact_id) {
91 $contact = new CRM_Contact_BAO_Contact();
92 $contact->id = $contact_id;
93
94 foreach ($privacyValues as $privacy_key => $privacy_value) {
95 $contact->$privacy_key = $privacyValueNew;
96 }
97 $contact->save();
98 $count++;
99 }
100 // Status message
101 $privacyOptions = CRM_Core_SelectValues::privacy();
102 $status = [];
103 foreach ($privacyValues as $privacy_key => $privacy_value) {
104 $label = $privacyOptions[$privacy_key];
105 $status[] = $privacyValueNew ? ts("Added '%1'", [1 => $label]) : ts("Removed '%1'", [1 => $label]);
106 }
107
108 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
109 if ($count > 1) {
110 $title = ts('%1 Contacts Updated', [1 => $count]);
111 }
112 else {
113 $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact_id, 'display_name');
114 $title = ts('%1 Updated', [1 => $name]);
115 }
116
117 CRM_Core_Session::setStatus($status, $title, 'success');
118 }
119 }
120
121 }