Merge pull request #12364 from JMAConsulting/dev-core-210
[civicrm-core.git] / CRM / Contact / Form / Inline / IM.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
5a409b50 35 * Form helper class for an IM object.
6a488035
TO
36 */
37class CRM_Contact_Form_Inline_IM extends CRM_Contact_Form_Inline {
38
39 /**
fe482240 40 * Ims of the contact that is been viewed.
6a488035
TO
41 */
42 private $_ims = array();
43
44 /**
fe482240 45 * No of im blocks for inline edit.
6a488035
TO
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 ims
56 $im = new CRM_Core_BAO_IM();
57 $im->contact_id = $this->_contactId;
58
59 $this->_ims = CRM_Core_BAO_Block::retrieveBlock($im, NULL);
60 }
61
62 /**
fe482240 63 * Build the form object elements for im object.
6a488035
TO
64 */
65 public function buildQuickForm() {
66 parent::buildQuickForm();
67
68 $totalBlocks = $this->_blockCount;
69 $actualBlockCount = 1;
70 if (count($this->_ims) > 1) {
71 $actualBlockCount = $totalBlocks = count($this->_ims);
72 if ($totalBlocks < $this->_blockCount) {
73 $additionalBlocks = $this->_blockCount - $totalBlocks;
74 $totalBlocks += $additionalBlocks;
75 }
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_IM::buildQuickForm($this, $blockId, TRUE);
89 }
90
91 $this->addFormRule(array('CRM_Contact_Form_Inline_IM', '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['im']) && is_array($fields['im'])) {
6a488035
TO
107 foreach ($fields['im'] as $instance => $blockValues) {
108 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
109
110 if ($dataExists) {
111 $hasData[] = $instance;
a7488080 112 if (!empty($blockValues['is_primary'])) {
6a488035 113 $hasPrimary[] = $instance;
8cc574cf 114 if (!$primaryID && !empty($blockValues['im'])) {
353ffa53 115 $primaryID = $blockValues['im'];
6a488035
TO
116 }
117 }
118 }
119 }
120
121 if (empty($hasPrimary) && !empty($hasData)) {
122 $errors["im[1][is_primary]"] = ts('One IM should be marked as primary.');
123 }
124
125 if (count($hasPrimary) > 1) {
92fcb95f 126 $errors["im[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one IM can be marked as primary.');
6a488035
TO
127 }
128 }
129 return $errors;
130 }
131
132 /**
fe482240 133 * Set defaults for the form.
6a488035
TO
134 *
135 * @return array
6a488035
TO
136 */
137 public function setDefaultValues() {
138 $defaults = array();
139 if (!empty($this->_ims)) {
140 foreach ($this->_ims as $id => $value) {
141 $defaults['im'][$id] = $value;
142 }
143 }
144 else {
145 // get the default location type
146 $locationType = CRM_Core_BAO_LocationType::getDefault();
147 $defaults['im'][1]['location_type_id'] = $locationType->id;
148 }
149 return $defaults;
150 }
151
152 /**
fe482240 153 * Process the form.
6a488035
TO
154 */
155 public function postProcess() {
156 $params = $this->exportValues();
157
158 // Process / save IMs
159 $params['contact_id'] = $this->_contactId;
160 $params['updateBlankLocInfo'] = TRUE;
a3b489b9 161 $params['im']['isIdSet'] = TRUE;
85c882c5 162 foreach ($this->_ims as $count => $value) {
163 if (!empty($value['id']) && isset($params['im'][$count])) {
164 $params['im'][$count]['id'] = $value['id'];
165 }
166 }
6a488035
TO
167 CRM_Core_BAO_Block::create('im', $params);
168
169 $this->log();
170 $this->response();
171 }
96025800 172
6a488035 173}