Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | /** | |
100fef9d | 42 | * Phones of the contact that is been viewed |
6a488035 TO |
43 | */ |
44 | private $_phones = array(); | |
45 | ||
46 | /** | |
47 | * No of phone blocks for inline edit | |
48 | */ | |
49 | private $_blockCount = 6; | |
50 | ||
51 | /** | |
100fef9d | 52 | * Call preprocess |
6a488035 TO |
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 | /** | |
c490a46a | 65 | * Build the form object elements for phone object |
6a488035 TO |
66 | * |
67 | * @return void | |
6a488035 TO |
68 | */ |
69 | public function buildQuickForm() { | |
70 | parent::buildQuickForm(); | |
71 | ||
72 | $totalBlocks = $this->_blockCount; | |
73 | $actualBlockCount = 1; | |
74 | if (count($this->_phones) > 1) { | |
75 | $actualBlockCount = $totalBlocks = count($this->_phones); | |
76 | if ($totalBlocks < $this->_blockCount) { | |
77 | $additionalBlocks = $this->_blockCount - $totalBlocks; | |
78 | $totalBlocks += $additionalBlocks; | |
79 | } | |
80 | else { | |
81 | $actualBlockCount++; | |
82 | $totalBlocks++; | |
83 | } | |
84 | } | |
85 | ||
86 | $this->assign('actualBlockCount', $actualBlockCount); | |
87 | $this->assign('totalBlocks', $totalBlocks); | |
88 | ||
89 | $this->applyFilter('__ALL__', 'trim'); | |
90 | ||
91 | for ($blockId = 1; $blockId < $totalBlocks; $blockId++) { | |
92 | CRM_Contact_Form_Edit_Phone::buildQuickForm($this, $blockId, TRUE); | |
93 | } | |
94 | ||
95 | $this->addFormRule(array('CRM_Contact_Form_Inline_Phone', 'formRule')); | |
96 | } | |
97 | ||
98 | /** | |
100fef9d | 99 | * Global validation rules for the form |
6a488035 | 100 | * |
6c8f6e67 EM |
101 | * @param array $fields posted values of the form |
102 | * @param array $errors list of errors to be posted back to the form | |
6a488035 | 103 | * |
6c8f6e67 | 104 | * @return array $errors@static |
6a488035 | 105 | */ |
00be9182 | 106 | public static function formRule($fields, $errors) { |
6a488035 | 107 | $hasData = $hasPrimary = $errors = array(); |
a7488080 | 108 | if (!empty($fields['phone']) && is_array($fields['phone'])) { |
6a488035 TO |
109 | $primaryID = null; |
110 | foreach ($fields['phone'] as $instance => $blockValues) { | |
111 | $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues); | |
112 | ||
113 | if ($dataExists) { | |
114 | $hasData[] = $instance; | |
a7488080 | 115 | if (!empty($blockValues['is_primary'])) { |
6a488035 | 116 | $hasPrimary[] = $instance; |
8cc574cf | 117 | if (!$primaryID && !empty($blockValues['phone'])) { |
6a488035 TO |
118 | $primaryID = $blockValues['phone']; |
119 | } | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | if (empty($hasPrimary) && !empty($hasData)) { | |
125 | $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.'); | |
126 | } | |
127 | ||
128 | if (count($hasPrimary) > 1) { | |
129 | $errors["phone[".array_pop($hasPrimary)."][is_primary]"] = ts('Only one phone can be marked as primary.'); | |
130 | } | |
131 | } | |
132 | return $errors; | |
133 | } | |
134 | ||
135 | /** | |
100fef9d | 136 | * Set defaults for the form |
6a488035 TO |
137 | * |
138 | * @return array | |
6a488035 TO |
139 | */ |
140 | public function setDefaultValues() { | |
141 | $defaults = array(); | |
142 | if (!empty($this->_phones)) { | |
143 | foreach ($this->_phones as $id => $value) { | |
144 | $defaults['phone'][$id] = $value; | |
145 | } | |
146 | } | |
147 | else { | |
148 | // get the default location type | |
149 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
150 | $defaults['phone'][1]['location_type_id'] = $locationType->id; | |
151 | } | |
152 | return $defaults; | |
153 | } | |
154 | ||
155 | /** | |
100fef9d | 156 | * Process the form |
6a488035 TO |
157 | * |
158 | * @return void | |
6a488035 TO |
159 | */ |
160 | public function postProcess() { | |
161 | $params = $this->exportValues(); | |
162 | ||
163 | // Process / save phones | |
164 | $params['contact_id'] = $this->_contactId; | |
165 | $params['updateBlankLocInfo'] = TRUE; | |
166 | CRM_Core_BAO_Block::create('phone', $params); | |
167 | ||
168 | $this->log(); | |
169 | $this->response(); | |
170 | } | |
171 | } |