3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to alter a privacy
20 * options for selected contacts
22 class CRM_Contact_Form_Task_AlterPreferences
extends CRM_Contact_Form_Task
{
25 * Build the form object.
27 public function buildQuickForm() {
28 // add select for preferences
30 $options = [ts('Add Selected Options'), ts('Remove selected options')];
32 $this->addRadio('actionTypeOption', ts('actionTypeOption'), $options);
34 $privacyOptions = CRM_Core_SelectValues
::privacy();
36 foreach ($privacyOptions as $prefID => $prefName) {
37 $this->_prefElement
= &$this->addElement('checkbox', "pref[$prefID]", NULL, $prefName);
40 $this->addDefaultButtons(ts('Set Privacy Options'));
43 public function addRules() {
44 $this->addFormRule(['CRM_Contact_Form_Task_AlterPreferences', 'formRule']);
48 * Set the default form values.
52 * the default array reference
54 public function setDefaultValues() {
57 $defaults['actionTypeOption'] = 0;
62 * @param CRM_Core_Form $form
67 public static function formRule($form, $rule) {
69 if (empty($form['pref']) && empty($form['contact_taglist'])) {
70 $errors['_qf_default'] = ts("Please select at least one privacy option.");
76 * Process the form after the input has been submitted and validated.
78 public function postProcess() {
79 //get the submitted values in an array
80 $params = $this->controller
->exportValues($this->_name
);
82 $actionTypeOption = $params['actionTypeOption'] ??
NULL;
83 // If remove option has been selected set new privacy value to "false"
84 $privacyValueNew = empty($actionTypeOption);
86 // check if any privay option has been checked
87 if (!empty($params['pref'])) {
88 $privacyValues = $params['pref'];
90 foreach ($this->_contactIds
as $contact_id) {
91 $contact = new CRM_Contact_BAO_Contact();
92 $contact->id
= $contact_id;
94 foreach ($privacyValues as $privacy_key => $privacy_value) {
95 $contact->$privacy_key = $privacyValueNew;
101 $privacyOptions = CRM_Core_SelectValues
::privacy();
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]);
108 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
110 $title = ts('%1 Contacts Updated', [1 => $count]);
113 $name = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contact_id, 'display_name');
114 $title = ts('%1 Updated', [1 => $name]);
117 CRM_Core_Session
::setStatus($status, $title, 'success');