3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * Form helper class for an Phone object.
37 class CRM_Contact_Form_Inline_Phone
extends CRM_Contact_Form_Inline
{
40 * Phones of the contact that is been viewed
42 private $_phones = array();
45 * No of phone blocks for inline edit
47 private $_blockCount = 6;
52 public function preProcess() {
55 //get all the existing phones
56 $phone = new CRM_Core_BAO_Phone();
57 $phone->contact_id
= $this->_contactId
;
59 $this->_phones
= CRM_Core_BAO_Block
::retrieveBlock($phone, NULL);
63 * Build the form object elements for phone object.
65 public function buildQuickForm() {
66 parent
::buildQuickForm();
68 $totalBlocks = $this->_blockCount
;
69 $actualBlockCount = 1;
70 if (count($this->_phones
) > 1) {
71 $actualBlockCount = $totalBlocks = count($this->_phones
);
72 if ($totalBlocks < $this->_blockCount
) {
73 $additionalBlocks = $this->_blockCount
- $totalBlocks;
74 $totalBlocks +
= $additionalBlocks;
82 $this->assign('actualBlockCount', $actualBlockCount);
83 $this->assign('totalBlocks', $totalBlocks);
85 $this->applyFilter('__ALL__', 'trim');
87 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
88 CRM_Contact_Form_Edit_Phone
::buildQuickForm($this, $blockId, TRUE);
91 $this->addFormRule(array('CRM_Contact_Form_Inline_Phone', 'formRule'));
95 * Global validation rules for the form.
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.
104 public static function formRule($fields, $errors) {
105 $hasData = $hasPrimary = $errors = array();
106 if (!empty($fields['phone']) && is_array($fields['phone'])) {
108 foreach ($fields['phone'] as $instance => $blockValues) {
109 $dataExists = CRM_Contact_Form_Contact
::blockDataExists($blockValues);
112 $hasData[] = $instance;
113 if (!empty($blockValues['is_primary'])) {
114 $hasPrimary[] = $instance;
115 if (!$primaryID && !empty($blockValues['phone'])) {
116 $primaryID = $blockValues['phone'];
122 if (empty($hasPrimary) && !empty($hasData)) {
123 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
126 if (count($hasPrimary) > 1) {
127 $errors["phone[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one phone can be marked as primary.');
134 * Set defaults for the form.
138 public function setDefaultValues() {
140 if (!empty($this->_phones
)) {
141 foreach ($this->_phones
as $id => $value) {
142 $defaults['phone'][$id] = $value;
146 // get the default location type
147 $locationType = CRM_Core_BAO_LocationType
::getDefault();
148 $defaults['phone'][1]['location_type_id'] = $locationType->id
;
156 public function postProcess() {
157 $params = $this->exportValues();
159 // Process / save phones
160 $params['contact_id'] = $this->_contactId
;
161 $params['updateBlankLocInfo'] = TRUE;
162 $params['phone']['isIdSet'] = TRUE;
163 foreach ($this->_phones
as $count => $value) {
164 if (!empty($value['id']) && isset($params['phone'][$count])) {
165 $params['phone'][$count]['id'] = $value['id'];
168 CRM_Core_BAO_Block
::create('phone', $params);