CRM-15662 fix for ACL's do not work when target is a smart group
[civicrm-core.git] / CRM / Contact / Form / Inline / Phone.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 * form helper class for an Phone object
38 */
39 class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
40
41 /**
42 * phones of the contact that is been viewed
43 */
44 private $_phones = array();
45
46 /**
47 * No of phone blocks for inline edit
48 */
49 private $_blockCount = 6;
50
51 /**
52 * call preprocess
53 */
54 public function preProcess() {
55 parent::preProcess();
56
57 //get all the existing phones
58 $phone = new CRM_Core_BAO_Phone();
59 $phone->contact_id = $this->_contactId;
60
61 $this->_phones = CRM_Core_BAO_Block::retrieveBlock($phone, NULL);
62 }
63
64 /**
65 * build the form elements for phone object
66 *
67 * @return void
68 * @access public
69 */
70 public function buildQuickForm() {
71 parent::buildQuickForm();
72
73 $totalBlocks = $this->_blockCount;
74 $actualBlockCount = 1;
75 if (count($this->_phones) > 1) {
76 $actualBlockCount = $totalBlocks = count($this->_phones);
77 if ($totalBlocks < $this->_blockCount) {
78 $additionalBlocks = $this->_blockCount - $totalBlocks;
79 $totalBlocks += $additionalBlocks;
80 }
81 else {
82 $actualBlockCount++;
83 $totalBlocks++;
84 }
85 }
86
87 $this->assign('actualBlockCount', $actualBlockCount);
88 $this->assign('totalBlocks', $totalBlocks);
89
90 $this->applyFilter('__ALL__', 'trim');
91
92 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
93 CRM_Contact_Form_Edit_Phone::buildQuickForm($this, $blockId, TRUE);
94 }
95
96 $this->addFormRule(array('CRM_Contact_Form_Inline_Phone', 'formRule'));
97 }
98
99 /**
100 * global validation rules for the form
101 *
102 * @param array $fields posted values of the form
103 * @param array $errors list of errors to be posted back to the form
104 *
105 * @return array $errors@static
106 * @access public
107 */
108 static function formRule($fields, $errors) {
109 $hasData = $hasPrimary = $errors = array();
110 if (!empty($fields['phone']) && is_array($fields['phone'])) {
111 $primaryID = null;
112 foreach ($fields['phone'] as $instance => $blockValues) {
113 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
114
115 if ($dataExists) {
116 $hasData[] = $instance;
117 if (!empty($blockValues['is_primary'])) {
118 $hasPrimary[] = $instance;
119 if (!$primaryID && !empty($blockValues['phone'])) {
120 $primaryID = $blockValues['phone'];
121 }
122 }
123 }
124 }
125
126 if (empty($hasPrimary) && !empty($hasData)) {
127 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
128 }
129
130 if (count($hasPrimary) > 1) {
131 $errors["phone[".array_pop($hasPrimary)."][is_primary]"] = ts('Only one phone can be marked as primary.');
132 }
133 }
134 return $errors;
135 }
136
137 /**
138 * set defaults for the form
139 *
140 * @return array
141 * @access public
142 */
143 public function setDefaultValues() {
144 $defaults = array();
145 if (!empty($this->_phones)) {
146 foreach ($this->_phones as $id => $value) {
147 $defaults['phone'][$id] = $value;
148 }
149 }
150 else {
151 // get the default location type
152 $locationType = CRM_Core_BAO_LocationType::getDefault();
153 $defaults['phone'][1]['location_type_id'] = $locationType->id;
154 }
155 return $defaults;
156 }
157
158 /**
159 * process the form
160 *
161 * @return void
162 * @access public
163 */
164 public function postProcess() {
165 $params = $this->exportValues();
166
167 // Process / save phones
168 $params['contact_id'] = $this->_contactId;
169 $params['updateBlankLocInfo'] = TRUE;
170 CRM_Core_BAO_Block::create('phone', $params);
171
172 $this->log();
173 $this->response();
174 }
175 }