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