Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
targetsalemailconnector.php
1<?php
2
3namespace Bitrix\Sale\Bigdata;
4
7
8Loc::loadMessages(__FILE__);
9
10if (Loader::includeModule('sender'))
11{
12 class TargetSaleMailConnector extends \Bitrix\Sender\Connector
13 {
14 public function getName()
15 {
16 return Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_NAME');
17 }
18
19 public function getCode()
20 {
21 return 'target_sale';
22 }
23
24 public function getData()
25 {
26 $productIds = array();
27
28 if (is_array($this->getFieldValue('PRODUCTS')) && count($this->getFieldValue('PRODUCTS')))
29 {
30 $productIds = array_values($this->getFieldValue('PRODUCTS'));
31 }
32
33 $userProducts = array();
34
35 foreach ($productIds as $productId)
36 {
37 $response = \Bitrix\Sale\Bigdata\Cloud::getPotentialConsumers($productId);
38
39 if (!empty($response['users']))
40 {
41 foreach ($response['users'] as $userId)
42 {
43 $userProducts[(string) $userId][] = $productId;
44 }
45 }
46 }
47
48 $rows = array();
49
50 if (!empty($userProducts))
51 {
52 $result = \Bitrix\Main\UserTable::getList(array(
53 'select' => array('USER_ID' => 'ID', 'NAME', 'EMAIL'),
54 'filter' => array(
55 '=ID' => array_keys($userProducts)
56 )
57 ));
58
59 while ($row = $result->fetch())
60 {
61 $row['PRODUCTS'] = $userProducts[$row['USER_ID']];
62
63 $rows[] = $row;
64 }
65 }
66
67 return $rows;
68 }
69
70 public static function getPersonalizeList()
71 {
72 return array(
73 array(
74 'CODE' => 'PRODUCTS',
75 'NAME' => Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_PRODUCTS_TITLE'),
76 'DESC' => Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_PRODUCTS_DESC')
77 )
78 );
79 }
80
81 public function getForm()
82 {
83 // items selector
84 $html = '<div id="send_bigdata_pcons_list_%CONNECTOR_NUM%" style="margin-bottom: 20px">';
85
86 // dummy for events
87 $html .= '<input type="hidden" id="send_bigdata_changeform_dummy_%CONNECTOR_NUM%" name="'.$this->getFieldName('RND').'" value="">';
88
89 $html .= '</div>';
90
91 $html .= "
92 <script>
93
94 function deleteProduct_%CONNECTOR_NUM%(id)
95 {
96 var li = BX('send_bigdata_pcons_list_%CONNECTOR_NUM%_e' + id);
97 BX.remove(li);
98
99 BX('send_bigdata_changeform_dummy_%CONNECTOR_NUM%').value = Math.random();
100 BX.fireEvent(BX('send_bigdata_changeform_dummy_%CONNECTOR_NUM%'), 'change');
101 }
102
103
104 function catchProduct_%CONNECTOR_NUM%(e)
105 {
106 // skip duplicates
107 if (BX('send_bigdata_pcons_list_%CONNECTOR_NUM%_e'+e.id))
108 {
109 return false;
110 }
111
112 // check if there is already limit
113 var limit = 25;
114
115 var currentCount = BX.findChildren(BX('send_bigdata_pcons_list_%CONNECTOR_NUM%'), {tag: 'li'}).length;
116
117 if (currentCount >= limit)
118 {
119 // show notice
120 var obPopupWin = BX.PopupWindowManager.create('sender_select_products_limit_%CONNECTOR_NUM%', null, {
121 autoHide: false,
122 offsetLeft: 0,
123 offsetTop: 0,
124 overlay : true,
125 closeByEsc: true,
126 titleBar: true,
127 closeIcon: {top: '10px', right: '10px'}
128 });
129
130 obPopupWin.setTitleBar({
131 content: BX.create(
132 'span', {html: '<b>' + '".\CUtil::JSEscape(Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_SELECT_LIMIT_TITLE'))."' + '</b>', 'props': {'className': 'access-title-bar'}}
133 )
134 });
135
136 var msg = '".\CUtil::JSEscape(Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_SELECT_LIMIT_MSG'))."';
137 msg = msg.replace('#LIMIT#', limit);
138
139 obPopupWin.setContent(msg);
140 obPopupWin.setButtons([new BX.PopupWindowButton({
141 text: '".\CUtil::JSEscape(Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_SELECT_LIMIT_CLOSE'))."',
142 events: {click: function(){
143 this.popupWindow.close();
144 }}
145 })]);
146
147 obPopupWin.show();
148
149 return false;
150 }
151
152 // item
153 var fieldName = \"{$this->getFieldName('PRODUCTS[]')}\".replace(\"\[\]\", \"[\"+e.id+\"]\");
154 var itemElement = document.createElement('li');
155 var title = e.name + ' (' + e.id + ') ';
156
157 itemElement.id = 'send_bigdata_pcons_list_%CONNECTOR_NUM%_e'+e.id;
158 itemElement.innerHTML = title + ' [ <a href=\"#\" onclick=\"deleteProduct_%CONNECTOR_NUM%('+e.id+'); return false;\">'+'".\CUtil::JSEscape(Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_SELECT_DEL'))."'+'</a> ]';
159 itemElement.innerHTML += '<input type=\"hidden\" name=\"'+fieldName+'\" value=\"'+e.id+'\"> ';
160
161 BX('send_bigdata_pcons_list_%CONNECTOR_NUM%').appendChild(itemElement);
162 }
163
164 function AddProductSearch_%CONNECTOR_NUM%()
165 {
166 var productPopup = new BX.CDialog({
167 content_url: '/bitrix/tools/sale/product_search_dialog.php?lang=".LANGUAGE_ID."&caller=sender_target_sale&func_name=catchProduct_%CONNECTOR_NUM%',
168 height: Math.max(500, window.innerHeight-400),
169 width: Math.max(800, window.innerWidth-400),
170 draggable: true,
171 resizable: true,
172 min_height: 500,
173 min_width: 800
174 });
175
176 BX.addCustomEvent(productPopup, 'onBeforeWindowClose', function(){
177 BX.fireEvent(BX('send_bigdata_changeform_dummy_%CONNECTOR_NUM%'), 'change');
178 });
179
180 productPopup.Show();
181 }
182 </script>
183
184 <button onclick='AddProductSearch_%CONNECTOR_NUM%(); return false;'>".htmlspecialcharsbx(Loc::getMessage('SALE_BIGDATA_TARGET_CONNECTOR_SELECT_TITLE'))."</button>
185 ";
186
187 if ($this->getFieldValue('PRODUCTS'))
188 {
189 // select titles
190 $titles = array();
191
192 $result = \Bitrix\Iblock\ElementTable::getList(array(
193 'select' => array('ID', 'NAME'),
194 'filter' => array('=ID' => $this->getFieldValue('PRODUCTS'))
195 ));
196
197 while ($row = $result->fetch())
198 {
199 $titles[(int) $row['ID']] = $row['NAME'];
200 }
201
202 // preset
203 $html .= "<script>".PHP_EOL;
204
205 foreach ($this->getFieldValue('PRODUCTS') as $productId)
206 {
207 $html .= 'catchProduct_%CONNECTOR_NUM%('.\CUtil::PhpToJSObject(array(
208 'id' => $productId,
209 'name' => $titles[(int) $productId]
210 )).');'.PHP_EOL;
211 }
212
213
214 $html .= "</script>";
215 }
216
217 return $html;
218 }
219
220 public static function onConnectorList()
221 {
222 $arData['CONNECTOR'] = __CLASS__;
223
224 return $arData;
225 }
226
227 }
228}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29