Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class provides the functionality to alter a privacy | |
36 | * options for selected contacts | |
37 | */ | |
38 | class CRM_Contact_Form_Task_AlterPreferences extends CRM_Contact_Form_Task { | |
39 | ||
40 | /** | |
fe482240 | 41 | * Build the form object. |
6a488035 | 42 | */ |
353ffa53 | 43 | public function buildQuickForm() { |
6a488035 TO |
44 | // add select for preferences |
45 | ||
46 | $options = array(ts('Add Selected Options'), ts('Remove selected options')); | |
47 | ||
48 | $this->addRadio('actionTypeOption', ts('actionTypeOption'), $options); | |
49 | ||
50 | $privacyOptions = CRM_Core_SelectValues::privacy(); | |
51 | ||
52 | foreach ($privacyOptions as $prefID => $prefName) { | |
53 | $this->_prefElement = &$this->addElement('checkbox', "pref[$prefID]", NULL, $prefName); | |
54 | } | |
55 | ||
56 | $this->addDefaultButtons(ts('Set Privacy Options')); | |
57 | } | |
58 | ||
00be9182 | 59 | public function addRules() { |
6a488035 TO |
60 | $this->addFormRule(array('CRM_Contact_Form_Task_AlterPreferences', 'formRule')); |
61 | } | |
62 | ||
63 | /** | |
fe482240 | 64 | * Set the default form values. |
6a488035 | 65 | * |
6a488035 | 66 | * |
a6c01b45 CW |
67 | * @return array |
68 | * the default array reference | |
6a488035 | 69 | */ |
00be9182 | 70 | public function setDefaultValues() { |
6a488035 TO |
71 | $defaults = array(); |
72 | ||
73 | $defaults['actionTypeOption'] = 0; | |
74 | return $defaults; | |
75 | } | |
76 | ||
86538308 | 77 | /** |
c490a46a | 78 | * @param CRM_Core_Form $form |
86538308 EM |
79 | * @param $rule |
80 | * | |
81 | * @return array | |
82 | */ | |
00be9182 | 83 | public static function formRule($form, $rule) { |
6a488035 TO |
84 | $errors = array(); |
85 | if (empty($form['pref']) && empty($form['contact_taglist'])) { | |
86 | $errors['_qf_default'] = ts("Please select at least one privacy option."); | |
87 | } | |
88 | return $errors; | |
89 | } | |
90 | ||
91 | /** | |
fe482240 | 92 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
93 | */ |
94 | public function postProcess() { | |
95 | //get the submitted values in an array | |
96 | $params = $this->controller->exportValues($this->_name); | |
97 | ||
98 | $actionTypeOption = CRM_Utils_Array::value('actionTypeOption', $params, NULL); | |
99 | // If remove option has been selected set new privacy value to "false" | |
100 | $privacyValueNew = empty($actionTypeOption); | |
101 | ||
102 | // check if any privay option has been checked | |
103 | if (!empty($params['pref'])) { | |
104 | $privacyValues = $params['pref']; | |
105 | $count = 0; | |
22e263ad | 106 | foreach ($this->_contactIds as $contact_id) { |
6a488035 TO |
107 | $contact = new CRM_Contact_BAO_Contact(); |
108 | $contact->id = $contact_id; | |
109 | ||
22e263ad | 110 | foreach ($privacyValues as $privacy_key => $privacy_value) { |
6a488035 TO |
111 | $contact->$privacy_key = $privacyValueNew; |
112 | } | |
113 | $contact->save(); | |
114 | $count++; | |
115 | } | |
116 | // Status message | |
117 | $privacyOptions = CRM_Core_SelectValues::privacy(); | |
118 | $status = array(); | |
22e263ad | 119 | foreach ($privacyValues as $privacy_key => $privacy_value) { |
6a488035 TO |
120 | $label = $privacyOptions[$privacy_key]; |
121 | $status[] = $privacyValueNew ? ts("Added '%1'", array(1 => $label)) : ts("Removed '%1'", array(1 => $label)); | |
122 | } | |
513a9a13 | 123 | |
6a488035 TO |
124 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; |
125 | if ($count > 1) { | |
126 | $title = ts('%1 Contacts Updated', array(1 => $count)); | |
127 | } | |
128 | else { | |
129 | $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact_id, 'display_name'); | |
130 | $title = ts('%1 Updated', array(1 => $name)); | |
131 | } | |
513a9a13 | 132 | |
6a488035 TO |
133 | CRM_Core_Session::setStatus($status, $title, 'success'); |
134 | } | |
135 | } | |
96025800 | 136 | |
6a488035 | 137 | } |