Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
senderconnector.php
1<?php
9namespace Bitrix\Iblock;
10
11use Bitrix\Main,
13
14Loc::loadMessages(__FILE__);
15
17{
24 public static function onConnectorListIblock($data)
25 {
26 $data['CONNECTOR'] = 'Bitrix\Iblock\SenderConnectorIblock';
27
28 return $data;
29 }
30}
31
32if (Main\Loader::includeModule('sender'))
33{
34 class SenderConnectorIblock extends \Bitrix\Sender\Connector
35 {
39 public function getName()
40 {
41 return Loc::getMessage('sender_connector_iblock_name');
42 }
43
47 public function getCode()
48 {
49 return "iblock";
50 }
51
55 public function requireConfigure()
56 {
57 return true;
58 }
59
61 public function getData()
62 {
63 $iblockId = $this->getFieldValue('IBLOCK', null);
64 $propertyNameId = $this->getFieldValue('PROPERTY_NAME', null);
65 $propertyEmailId = $this->getFieldValue('PROPERTY_EMAIL', null);
66
67 if($iblockId && $propertyEmailId)
68 {
69 // if property is property with code like '123'
70 $propertyNameValue = null;
73 {
75 {
76 $propertyEmailId = "PROPERTY_" . $propertyEmailId;
78 }
79 else
80 {
82 }
83 }
84 $selectFields = array($propertyEmailValue);
85
87 {
89 {
90 $propertyNameId = "PROPERTY_" . $propertyNameId;
92 }
93 else
94 {
96 }
97
98 $selectFields[] = $propertyNameValue;
99 }
100
101 $filter = array('IBLOCK_ID' => $iblockId, '!'.$propertyEmailId => false);
102 $iblockElementListDb = \CIBlockElement::getList(array('id' => 'asc'), $filter, false, false, $selectFields);
103
104 // replace property names from PROPERTY_123_VALUE to EMAIL, NAME
105 $iblockElementDb = new CDBResultSenderConnector($iblockElementListDb);
106 $iblockElementDb->senderConnectorFieldEmail = $propertyEmailValue;
107 $iblockElementDb->senderConnectorFieldName = $propertyNameValue;
108 }
109 else
110 {
112 $iblockElementDb->InitFromArray(array());
113 }
114
115
116 return $iblockElementDb;
117 }
118
123 public function getForm()
124 {
125 /*
126 * select iblock list
127 */
128 $iblockList = array();
129 $iblockDb = IblockTable::getList(array(
130 'select' => array('ID', 'NAME'),
131 ));
132 while($iblock = $iblockDb->fetch())
133 {
134 $iblockList[] = $iblock;
135 }
136 if(!empty($iblockList))
137 $iblockList = array_merge(
138 array(array('ID' => '', 'NAME' => Loc::getMessage('sender_connector_iblock_select'))),
139 $iblockList
140 );
141 else
142 $iblockList = array_merge(
143 array(array('ID' => '', 'NAME' => Loc::getMessage('sender_connector_iblock_empty'))),
144 $iblockList
145 );
146
147 /*
148 * select properties from all iblocks
149 */
151 $propertyList = array();
152 $propertyList[''][] = array('ID' => '', 'NAME' => Loc::getMessage('sender_connector_iblock_select'));
153 $propertyList['EMPTY'][] = array('ID' => '', 'NAME' => Loc::getMessage('sender_connector_iblock_prop_empty'));
154 $iblockFieldsDb = PropertyTable::getList(array(
155 'select' => array('ID', 'NAME', 'IBLOCK_ID'),
156 'filter' => array('=PROPERTY_TYPE' => PropertyTable::TYPE_STRING)
157 ));
158 while($iblockFields = $iblockFieldsDb->fetch())
159 {
160 // add property
161 $propertyList[$iblockFields['IBLOCK_ID']][] = array(
162 'ID' => $iblockFields['ID'],
163 'NAME' => $iblockFields['NAME']
164 );
165
166 // add property link to iblock
167 $propertyToIblock[$iblockFields['ID']] = $iblockFields['IBLOCK_ID'];
168 }
169
170
171 $fieldList = static::getIblockFieldList();
172 // add default value
173 $fieldList = array_merge(
174 array(array('ID' => '', 'NAME' => Loc::getMessage('sender_connector_iblock_field_select'))),
175 $fieldList
176 );
177 foreach($iblockList as $iblock)
178 {
179 if(!$iblock['ID'])
180 {
181 continue;
182 }
183
184 if(!isset($propertyList[$iblock['ID']]) || !is_array($propertyList[$iblock['ID']]))
185 {
186 $propertyList[$iblock['ID']] = array();
187 }
188 else
189 {
190 // add delimiter between fields and properties
191 $propertyList[$iblock['ID']] = array_merge(
192 array(array('ID' => '------', 'NAME' => '-----------------', 'DISABLED' => true)),
193 $propertyList[$iblock['ID']]
194 );
195 }
196
197 $propertyList[$iblock['ID']] = array_merge($fieldList, $propertyList[$iblock['ID']]);
198 }
199
200
201 /*
202 * create html-control of iblock list
203 */
204 $iblockInput = '<select name="'.$this->getFieldName('IBLOCK').'" id="'.$this->getFieldId('IBLOCK').'" onChange="IblockSelect'.$this->getFieldId('IBLOCK').'()">';
205 foreach($iblockList as $iblock)
206 {
207 $inputSelected = ($iblock['ID'] == $this->getFieldValue('IBLOCK') ? 'selected' : '');
208 $iblockInput .= '<option value="'.$iblock['ID'].'" '.$inputSelected.'>';
209 $iblockInput .= Main\Text\HtmlFilter::encode($iblock['NAME']);
210 $iblockInput .= '</option>';
211 }
212 $iblockInput .= '</select>';
213
214
215 /*
216 * create html-control of properties list for name
217 */
218 $iblockPropertyNameInput = '<select name="'.$this->getFieldName('PROPERTY_NAME').'" id="'.$this->getFieldId('PROPERTY_NAME').'">';
219 if(array_key_exists($this->getFieldValue('PROPERTY_NAME', 0), $propertyToIblock))
220 {
221 $propSet = $propertyList[$propertyToIblock[$this->getFieldValue('PROPERTY_NAME', 0)]];
222 }
223 elseif(array_key_exists($this->getFieldValue('IBLOCK', 0), $propertyList))
224 {
225 $propSet = $propertyList[$this->getFieldValue('IBLOCK')];
226 }
227 else
228 {
229 $propSet = $propertyList[''];
230 }
231 foreach($propSet as $property)
232 {
233 $inputSelected = $property['ID'] == $this->getFieldValue('PROPERTY_NAME') ? 'selected' : '';
234 $inputDisabled = (isset($property['DISABLED']) && $property['DISABLED']) ? 'disabled' : '';
235 $iblockPropertyNameInput .= '<option value="'.$property['ID'].'" '.$inputSelected.' '.$inputDisabled.'>';
236 $iblockPropertyNameInput .= Main\Text\HtmlFilter::encode($property['NAME']);
237 $iblockPropertyNameInput .= '</option>';
238 }
239 $iblockPropertyNameInput .= '</select>';
240
241
242 /*
243 * create html-control of properties list for email
244 */
245 $iblockPropertyEmailInput = '<select name="'.$this->getFieldName('PROPERTY_EMAIL').'" id="'.$this->getFieldId('PROPERTY_EMAIL').'">';
246 if(array_key_exists($this->getFieldValue('PROPERTY_EMAIL', 0), $propertyToIblock))
247 {
248 $propSet = $propertyList[$propertyToIblock[$this->getFieldValue('PROPERTY_EMAIL', 0)]];
249 }
250 elseif(array_key_exists($this->getFieldValue('IBLOCK', 0), $propertyList))
251 {
252 $propSet = $propertyList[$this->getFieldValue('IBLOCK')];
253 }
254 else
255 {
256 $propSet = $propertyList[''];
257 }
258 foreach($propSet as $property)
259 {
260 $inputSelected = ($property['ID'] == $this->getFieldValue('PROPERTY_EMAIL') ? 'selected' : '');
261 $inputDisabled = (isset($property['DISABLED']) && $property['DISABLED']) ? 'disabled' : '';
262 $iblockPropertyEmailInput .= '<option value="'.$property['ID'].'" '.$inputSelected.' '.$inputDisabled.'>';
263 $iblockPropertyEmailInput .= Main\Text\HtmlFilter::encode($property['NAME']);
264 $iblockPropertyEmailInput .= '</option>';
265 }
266 $iblockPropertyEmailInput .= '</select>';
267
268
269 $jsScript = "
270 <script>
271 function IblockSelect".$this->getFieldId('IBLOCK')."()
272 {
273 var iblock = BX('".$this->getFieldId('IBLOCK')."');
274 IblockPropertyAdd(iblock, BX('".$this->getFieldId('PROPERTY_NAME')."'));
275 IblockPropertyAdd(iblock, BX('".$this->getFieldId('PROPERTY_EMAIL')."'));
276 }
277 function IblockPropertyAdd(iblock, iblockProperty)
278 {
279 if(iblockProperty.length>0)
280 {
281 for (var j in iblockProperty.options)
282 {
283 iblockProperty.options.remove(j);
284 }
285 }
286 var propList = {};
287 if(iblockProperties[iblock.value] && iblockProperties[iblock.value].length>0)
288 propList = iblockProperties[iblock.value];
289 else
290 propList = iblockProperties['EMPTY'];
291 for(var i in propList)
292 {
293 var optionName = propList[i]['NAME'];
294 var optionValue = propList[i]['ID'];
295 var optionDisabled = propList[i]['DISABLED'];
296 var newOption = new Option(optionName, optionValue);
297 if(optionDisabled)
298 {
299 newOption.disabled = true;
300 }
301 iblockProperty.options.add(newOption);
302 }
303
304 }
305
306 var iblockProperties = ".\CUtil::PhpToJSObject($propertyList).";
307 </script>
308 ";
309
310
311
312 return '
313 '.Loc::getMessage('sender_connector_iblock_required_settings').'
314 <br/><br/>
315 <table>
316 <tr>
317 <td>'.Loc::getMessage('sender_connector_iblock_field_iblock').'</td>
318 <td>'.$iblockInput.'</td>
319 </tr>
320 <tr>
321 <td>'.Loc::getMessage('sender_connector_iblock_field_name').'</td>
322 <td>'.$iblockPropertyNameInput.'</td>
323 </tr>
324 <tr>
325 <td>'.Loc::getMessage('sender_connector_iblock_field_email').'</td>
326 <td>'.$iblockPropertyEmailInput.'</td>
327 </tr>
328 </table>
329 '.$jsScript.'
330 ';
331 }
332
333 protected static function getIblockFieldList()
334 {
335 $fieldCodeList = array('NAME', 'CODE', 'PREVIEW_TEXT', 'DETAIL_TEXT');
336
337 $resultList = array();
338 $entity = ElementTable::getEntity();
339 foreach($fieldCodeList as $fieldCode)
340 {
341 $field = $entity->getField($fieldCode);
342 $resultList[] = array(
343 'ID' => $fieldCode,
344 'NAME' => $field->getTitle()
345 );
346 }
347
348 return $resultList;
349 }
350 }
351}
352
353class CDBResultSenderConnector extends \CDBResult
354{
357
358
362 public function Fetch()
363 {
364 $fields = parent::Fetch();
365 if($fields)
366 {
367 $keysForUnset = array();
368 if ($this->senderConnectorFieldName)
369 {
370 if(isset($fields[$this->senderConnectorFieldName."_VALUE"]))
371 {
372 $fields['NAME'] = $fields[$this->senderConnectorFieldName."_VALUE"];
373 $keysForUnset[] = $this->senderConnectorFieldName."_VALUE";
374 $keysForUnset[] = $this->senderConnectorFieldName."_VALUE"."_ID";
375 }
376 elseif(isset($fields[$this->senderConnectorFieldName]))
377 {
378 $fields['NAME'] = $fields[$this->senderConnectorFieldName];
379 if($this->senderConnectorFieldName != 'NAME')
380 $keysForUnset[] = $this->senderConnectorFieldName;
381 }
382 }
383
384 if ($this->senderConnectorFieldEmail)
385 {
386 if(isset($fields[$this->senderConnectorFieldEmail."_VALUE"]))
387 {
388 $fields['EMAIL'] = $fields[$this->senderConnectorFieldEmail."_VALUE"];
389 $keysForUnset[] = $this->senderConnectorFieldEmail."_VALUE";
390 $keysForUnset[] = $this->senderConnectorFieldEmail."_VALUE"."_ID";
391 }
392 elseif(isset($fields[$this->senderConnectorFieldEmail]))
393 {
394 $fields['EMAIL'] = $fields[$this->senderConnectorFieldEmail];
395 $keysForUnset[] = $this->senderConnectorFieldEmail;
396 }
397 }
398
399 if (count($keysForUnset)>0)
400 {
401 $keysForUnset = array_unique($keysForUnset);
402 foreach($keysForUnset as $key) unset($fields[$key]);
403 }
404 }
405
406 return $fields;
407 }
408}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29