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