-- CRM-12682 applied patch and made some modifications
[civicrm-core.git] / CRM / Contact / Form / Task / AlterPreferences.php
1
2 <?php
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36
37 /**
38 * This class provides the functionality to alter a privacy
39 * options for selected contacts
40 */
41 class CRM_Contact_Form_Task_AlterPreferences extends CRM_Contact_Form_Task {
42
43 /**
44 * Build the form
45 *
46 * @access public
47 *
48 * @return void
49 */
50 function buildQuickForm() {
51 // add select for preferences
52
53 $options = array(ts('Add Selected Options'), ts('Remove selected options'));
54
55 $this->addRadio('actionTypeOption', ts('actionTypeOption'), $options);
56
57 $privacyOptions = CRM_Core_SelectValues::privacy();
58
59 foreach ($privacyOptions as $prefID => $prefName) {
60 $this->_prefElement = &$this->addElement('checkbox', "pref[$prefID]", NULL, $prefName);
61 }
62
63 $this->addDefaultButtons(ts('Set Privacy Options'));
64 }
65
66 function addRules() {
67 $this->addFormRule(array('CRM_Contact_Form_Task_AlterPreferences', 'formRule'));
68 }
69
70 /**
71 * Set the default form values
72 *
73 * @access protected
74 *
75 * @return array the default array reference
76 */
77 function setDefaultValues() {
78 $defaults = array();
79
80 $defaults['actionTypeOption'] = 0;
81 return $defaults;
82 }
83
84 static function formRule($form, $rule) {
85 $errors = array();
86 if (empty($form['pref']) && empty($form['contact_taglist'])) {
87 $errors['_qf_default'] = ts("Please select at least one privacy option.");
88 }
89 return $errors;
90 }
91
92 /**
93 * process the form after the input has been submitted and validated
94 *
95 * @access public
96 *
97 * @return None
98 */
99 public function postProcess() {
100 //get the submitted values in an array
101 $params = $this->controller->exportValues($this->_name);
102
103 $actionTypeOption = CRM_Utils_Array::value('actionTypeOption', $params, NULL);
104 // If remove option has been selected set new privacy value to "false"
105 $privacyValueNew = empty($actionTypeOption);
106
107 // check if any privay option has been checked
108 if (!empty($params['pref'])) {
109 $privacyValues = $params['pref'];
110 $count = 0;
111 foreach($this->_contactIds as $contact_id) {
112 $contact = new CRM_Contact_BAO_Contact();
113 $contact->id = $contact_id;
114
115 foreach($privacyValues as $privacy_key => $privacy_value) {
116 $contact->$privacy_key = $privacyValueNew;
117 }
118 $contact->save();
119 $count++;
120 }
121 // Status message
122 $privacyOptions = CRM_Core_SelectValues::privacy();
123 $status = array();
124 foreach($privacyValues as $privacy_key => $privacy_value) {
125 $label = $privacyOptions[$privacy_key];
126 $status[] = $privacyValueNew ? ts("Added '%1'", array(1 => $label)) : ts("Removed '%1'", array(1 => $label));
127 }
128
129 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
130 if ($count > 1) {
131 $title = ts('%1 Contacts Updated', array(1 => $count));
132 }
133 else {
134 $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact_id, 'display_name');
135 $title = ts('%1 Updated', array(1 => $name));
136 }
137
138 CRM_Core_Session::setStatus($status, $title, 'success');
139 }
140 }
141 }