Merge pull request #4686 from lcdservices/CRM-15698-b
[civicrm-core.git] / CRM / Contact / Form / Task / AlterPreferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 object
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 /**
84 * @param CRM_Core_Form $form
85 * @param $rule
86 *
87 * @return array
88 */
89 static function formRule($form, $rule) {
90 $errors = array();
91 if (empty($form['pref']) && empty($form['contact_taglist'])) {
92 $errors['_qf_default'] = ts("Please select at least one privacy option.");
93 }
94 return $errors;
95 }
96
97 /**
98 * Process the form after the input has been submitted and validated
99 *
100 * @access public
101 *
102 * @return void
103 */
104 public function postProcess() {
105 //get the submitted values in an array
106 $params = $this->controller->exportValues($this->_name);
107
108 $actionTypeOption = CRM_Utils_Array::value('actionTypeOption', $params, NULL);
109 // If remove option has been selected set new privacy value to "false"
110 $privacyValueNew = empty($actionTypeOption);
111
112 // check if any privay option has been checked
113 if (!empty($params['pref'])) {
114 $privacyValues = $params['pref'];
115 $count = 0;
116 foreach($this->_contactIds as $contact_id) {
117 $contact = new CRM_Contact_BAO_Contact();
118 $contact->id = $contact_id;
119
120 foreach($privacyValues as $privacy_key => $privacy_value) {
121 $contact->$privacy_key = $privacyValueNew;
122 }
123 $contact->save();
124 $count++;
125 }
126 // Status message
127 $privacyOptions = CRM_Core_SelectValues::privacy();
128 $status = array();
129 foreach($privacyValues as $privacy_key => $privacy_value) {
130 $label = $privacyOptions[$privacy_key];
131 $status[] = $privacyValueNew ? ts("Added '%1'", array(1 => $label)) : ts("Removed '%1'", array(1 => $label));
132 }
133
134 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
135 if ($count > 1) {
136 $title = ts('%1 Contacts Updated', array(1 => $count));
137 }
138 else {
139 $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact_id, 'display_name');
140 $title = ts('%1 Updated', array(1 => $name));
141 }
142
143 CRM_Core_Session::setStatus($status, $title, 'success');
144 }
145 }
146 }