3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * form helper class for an Phone object
39 class CRM_Contact_Form_Inline_Phone
extends CRM_Contact_Form_Inline
{
42 * phones of the contact that is been viewed
44 private $_phones = array();
47 * No of phone blocks for inline edit
49 private $_blockCount = 6;
54 public function preProcess() {
57 //get all the existing phones
58 $phone = new CRM_Core_BAO_Phone();
59 $phone->contact_id
= $this->_contactId
;
61 $this->_phones
= CRM_Core_BAO_Block
::retrieveBlock($phone, NULL);
65 * build the form elements for phone object
70 public function buildQuickForm() {
71 parent
::buildQuickForm();
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;
87 $this->assign('actualBlockCount', $actualBlockCount);
88 $this->assign('totalBlocks', $totalBlocks);
90 $this->applyFilter('__ALL__', 'trim');
92 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
93 CRM_Contact_Form_Edit_Phone
::buildQuickForm($this, $blockId, TRUE);
96 $this->addFormRule(array('CRM_Contact_Form_Inline_Phone', 'formRule'));
100 * global validation rules for the form
102 * @param array $fields posted values of the form
103 * @param array $errors list of errors to be posted back to the form
109 static function formRule($fields, $errors) {
110 $hasData = $hasPrimary = $errors = array();
111 if (CRM_Utils_Array
::value('phone', $fields) && is_array($fields['phone'])) {
113 foreach ($fields['phone'] as $instance => $blockValues) {
114 $dataExists = CRM_Contact_Form_Contact
::blockDataExists($blockValues);
117 $hasData[] = $instance;
118 if (CRM_Utils_Array
::value('is_primary', $blockValues)) {
119 $hasPrimary[] = $instance;
121 CRM_Utils_Array
::value('phone', $blockValues)) {
122 $primaryID = $blockValues['phone'];
128 if (empty($hasPrimary) && !empty($hasData)) {
129 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
132 if (count($hasPrimary) > 1) {
133 $errors["phone[".array_pop($hasPrimary)."][is_primary]"] = ts('Only one phone can be marked as primary.');
140 * set defaults for the form
145 public function setDefaultValues() {
147 if (!empty($this->_phones
)) {
148 foreach ($this->_phones
as $id => $value) {
149 $defaults['phone'][$id] = $value;
153 // get the default location type
154 $locationType = CRM_Core_BAO_LocationType
::getDefault();
155 $defaults['phone'][1]['location_type_id'] = $locationType->id
;
166 public function postProcess() {
167 $params = $this->exportValues();
169 // Process / save phones
170 $params['contact_id'] = $this->_contactId
;
171 $params['updateBlankLocInfo'] = TRUE;
172 CRM_Core_BAO_Block
::create('phone', $params);