Merge pull request #15475 from mecachisenros/externUrl
[civicrm-core.git] / CRM / Contact / Form / Inline / IM.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form helper class for an IM object.
20 */
21 class CRM_Contact_Form_Inline_IM extends CRM_Contact_Form_Inline {
22
23 /**
24 * Ims of the contact that is been viewed.
25 * @var array
26 */
27 private $_ims = [];
28
29 /**
30 * No of im blocks for inline edit.
31 * @var int
32 */
33 private $_blockCount = 6;
34
35 /**
36 * Call preprocess.
37 */
38 public function preProcess() {
39 parent::preProcess();
40
41 //get all the existing ims
42 $im = new CRM_Core_BAO_IM();
43 $im->contact_id = $this->_contactId;
44
45 $this->_ims = CRM_Core_BAO_Block::retrieveBlock($im, NULL);
46 }
47
48 /**
49 * Build the form object elements for im object.
50 */
51 public function buildQuickForm() {
52 parent::buildQuickForm();
53
54 $totalBlocks = $this->_blockCount;
55 $actualBlockCount = 1;
56 if (count($this->_ims) > 1) {
57 $actualBlockCount = $totalBlocks = count($this->_ims);
58 if ($totalBlocks < $this->_blockCount) {
59 $additionalBlocks = $this->_blockCount - $totalBlocks;
60 $totalBlocks += $additionalBlocks;
61 }
62 else {
63 $actualBlockCount++;
64 $totalBlocks++;
65 }
66 }
67
68 $this->assign('actualBlockCount', $actualBlockCount);
69 $this->assign('totalBlocks', $totalBlocks);
70
71 $this->applyFilter('__ALL__', 'trim');
72
73 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
74 CRM_Contact_Form_Edit_IM::buildQuickForm($this, $blockId, TRUE);
75 }
76
77 $this->addFormRule(['CRM_Contact_Form_Inline_IM', 'formRule']);
78 }
79
80 /**
81 * Global validation rules for the form.
82 *
83 * @param array $fields
84 * Posted values of the form.
85 * @param array $errors
86 * List of errors to be posted back to the form.
87 *
88 * @return array
89 */
90 public static function formRule($fields, $errors) {
91 $hasData = $hasPrimary = $errors = [];
92 if (!empty($fields['im']) && is_array($fields['im'])) {
93 foreach ($fields['im'] as $instance => $blockValues) {
94 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
95
96 if ($dataExists) {
97 $hasData[] = $instance;
98 if (!empty($blockValues['is_primary'])) {
99 $hasPrimary[] = $instance;
100 if (!$primaryID && !empty($blockValues['im'])) {
101 $primaryID = $blockValues['im'];
102 }
103 }
104 }
105 }
106
107 if (empty($hasPrimary) && !empty($hasData)) {
108 $errors["im[1][is_primary]"] = ts('One IM should be marked as primary.');
109 }
110
111 if (count($hasPrimary) > 1) {
112 $errors["im[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one IM can be marked as primary.');
113 }
114 }
115 return $errors;
116 }
117
118 /**
119 * Set defaults for the form.
120 *
121 * @return array
122 */
123 public function setDefaultValues() {
124 $defaults = [];
125 if (!empty($this->_ims)) {
126 foreach ($this->_ims as $id => $value) {
127 $defaults['im'][$id] = $value;
128 }
129 }
130 else {
131 // get the default location type
132 $locationType = CRM_Core_BAO_LocationType::getDefault();
133 $defaults['im'][1]['location_type_id'] = $locationType->id;
134 }
135 return $defaults;
136 }
137
138 /**
139 * Process the form.
140 */
141 public function postProcess() {
142 $params = $this->exportValues();
143
144 // Process / save IMs
145 $params['contact_id'] = $this->_contactId;
146 $params['updateBlankLocInfo'] = TRUE;
147 $params['im']['isIdSet'] = TRUE;
148 foreach ($this->_ims as $count => $value) {
149 if (!empty($value['id']) && isset($params['im'][$count])) {
150 $params['im'][$count]['id'] = $value['id'];
151 }
152 }
153 CRM_Core_BAO_Block::create('im', $params);
154
155 $this->log();
156 $this->response();
157 }
158
159 }