Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Contact / Form / Inline / Phone.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
5a409b50 35 * Form helper class for an Phone object.
6a488035
TO
36 */
37class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
38
39 /**
100fef9d 40 * Phones of the contact that is been viewed
6a488035
TO
41 */
42 private $_phones = array();
43
44 /**
45 * No of phone blocks for inline edit
46 */
47 private $_blockCount = 6;
48
49 /**
fe482240 50 * Call preprocess.
6a488035
TO
51 */
52 public function preProcess() {
53 parent::preProcess();
54
55 //get all the existing phones
56 $phone = new CRM_Core_BAO_Phone();
57 $phone->contact_id = $this->_contactId;
58
59 $this->_phones = CRM_Core_BAO_Block::retrieveBlock($phone, NULL);
60 }
61
62 /**
fe482240 63 * Build the form object elements for phone object.
6a488035
TO
64 */
65 public function buildQuickForm() {
66 parent::buildQuickForm();
67
68 $totalBlocks = $this->_blockCount;
69 $actualBlockCount = 1;
70 if (count($this->_phones) > 1) {
71 $actualBlockCount = $totalBlocks = count($this->_phones);
72 if ($totalBlocks < $this->_blockCount) {
353ffa53
TO
73 $additionalBlocks = $this->_blockCount - $totalBlocks;
74 $totalBlocks += $additionalBlocks;
75 }
6a488035
TO
76 else {
77 $actualBlockCount++;
78 $totalBlocks++;
79 }
80 }
81
82 $this->assign('actualBlockCount', $actualBlockCount);
83 $this->assign('totalBlocks', $totalBlocks);
84
85 $this->applyFilter('__ALL__', 'trim');
86
87 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
88 CRM_Contact_Form_Edit_Phone::buildQuickForm($this, $blockId, TRUE);
89 }
90
91 $this->addFormRule(array('CRM_Contact_Form_Inline_Phone', 'formRule'));
92 }
93
94 /**
fe482240 95 * Global validation rules for the form.
6a488035 96 *
77c5b619
TO
97 * @param array $fields
98 * Posted values of the form.
99 * @param array $errors
100 * List of errors to be posted back to the form.
6a488035 101 *
a6c01b45 102 * @return array
6a488035 103 */
00be9182 104 public static function formRule($fields, $errors) {
6a488035 105 $hasData = $hasPrimary = $errors = array();
a7488080 106 if (!empty($fields['phone']) && is_array($fields['phone'])) {
e60f24eb 107 $primaryID = NULL;
6a488035
TO
108 foreach ($fields['phone'] as $instance => $blockValues) {
109 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
110
111 if ($dataExists) {
112 $hasData[] = $instance;
a7488080 113 if (!empty($blockValues['is_primary'])) {
6a488035 114 $hasPrimary[] = $instance;
8cc574cf 115 if (!$primaryID && !empty($blockValues['phone'])) {
353ffa53 116 $primaryID = $blockValues['phone'];
6a488035
TO
117 }
118 }
119 }
120 }
121
122 if (empty($hasPrimary) && !empty($hasData)) {
123 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
124 }
125
126 if (count($hasPrimary) > 1) {
92fcb95f 127 $errors["phone[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one phone can be marked as primary.');
6a488035
TO
128 }
129 }
130 return $errors;
131 }
132
133 /**
fe482240 134 * Set defaults for the form.
6a488035
TO
135 *
136 * @return array
6a488035
TO
137 */
138 public function setDefaultValues() {
139 $defaults = array();
140 if (!empty($this->_phones)) {
141 foreach ($this->_phones as $id => $value) {
142 $defaults['phone'][$id] = $value;
143 }
144 }
145 else {
146 // get the default location type
147 $locationType = CRM_Core_BAO_LocationType::getDefault();
148 $defaults['phone'][1]['location_type_id'] = $locationType->id;
149 }
150 return $defaults;
151 }
152
153 /**
fe482240 154 * Process the form.
6a488035
TO
155 */
156 public function postProcess() {
157 $params = $this->exportValues();
158
159 // Process / save phones
160 $params['contact_id'] = $this->_contactId;
161 $params['updateBlankLocInfo'] = TRUE;
a3b489b9 162 $params['phone']['isIdSet'] = TRUE;
85c882c5 163 foreach ($this->_phones as $count => $value) {
164 if (!empty($value['id']) && isset($params['phone'][$count])) {
165 $params['phone'][$count]['id'] = $value['id'];
166 }
167 }
6a488035
TO
168 CRM_Core_BAO_Block::create('phone', $params);
169
170 $this->log();
171 $this->response();
172 }
96025800 173
6a488035 174}