Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexbanner.php
1<?php
8namespace Bitrix\Seo\Adv;
9
11use Bitrix\Main;
15
16Loc::loadMessages(__FILE__);
17
52{
53 const ENGINE = 'yandex_direct';
54
55 const MAX_TITLE_LENGTH = 35;
56 const MAX_TEXT_LENGTH = 81;
57
58 const CACHE_LIFETIME = 3600;
59
60 const MARKED = 'D';
61
62 private static $engine = null;
63
64 protected static $priorityList = array(
65 -1 => Engine\YandexDirect::PRIORITY_LOW,
66 0 => Engine\YandexDirect::PRIORITY_MEDIUM,
67 1 => Engine\YandexDirect::PRIORITY_HIGH,
68 );
69
75 public static function getFilePath()
76 {
77 return __FILE__;
78 }
79
85 public static function getTableName()
86 {
87 return 'b_seo_adv_banner';
88 }
89
95 public static function getMap()
96 {
97 return array_merge(
98 parent::getMap(),
99 array(
100 'CAMPAIGN_ID' => array(
101 'data_type' => 'integer',
102 'required' => true,
103 'title' => Loc::getMessage('ADV_CAMPAIGN_ENTITY_CAMPAIGN_ID_FIELD'),
104 ),
105 'GROUP_ID' => array(
106 'data_type' => 'integer',
107 'title' => Loc::getMessage('ADV_CAMPAIGN_ENTITY_GROUP_ID_FIELD'),
108 ),
109 'AUTO_QUANTITY_OFF' => array(
110 'data_type' => 'enum',
111 'values' => array(static::INACTIVE, static::ACTIVE, static::MARKED),
112 'title' => Loc::getMessage('ADV_CAMPAIGN_ENTITY_AUTO_QUANTITY_OFF_FIELD'),
113 ),
114 'AUTO_QUANTITY_ON' => array(
115 'data_type' => 'enum',
116 'values' => array(static::INACTIVE, static::ACTIVE, static::MARKED),
117 'title' => Loc::getMessage('ADV_CAMPAIGN_ENTITY_AUTO_QUANTITY_ON_FIELD'),
118 ),
119 'CAMPAIGN' => array(
120 'data_type' => 'Bitrix\Seo\Adv\YandexCampaignTable',
121 'reference' => array('=this.CAMPAIGN_ID' => 'ref.ID'),
122 ),
123 'GROUP' => array(
124 'data_type' => 'Bitrix\Seo\Adv\YandexGroupTable',
125 'reference' => array('=this.GROUP_ID' => 'ref.ID'),
126 ),
127 )
128 );
129 }
130
136 public static function getEngine()
137 {
138 if(!self::$engine)
139 {
140 self::$engine = new Engine\YandexDirect();
141 }
142 return self::$engine;
143 }
144
156 public static function onBeforeAdd(Entity\Event $event)
157 {
158 $result = new Entity\EventResult();
159
160 $data = $event->getParameter("fields");
161
162 $engine = self::getEngine();
163 $ownerInfo = $engine->getCurrentUser();
164
165 if(!static::$skipRemoteUpdate)
166 {
167 $data["SETTINGS"] = self::createParam($engine, $data, $result);
168 $data["XML_ID"] = 'Error';
169 }
170 else
171 {
172 $data["XML_ID"] = $data["SETTINGS"]["BannerID"];
173 }
174
175 $data["NAME"] = $data["SETTINGS"]["Title"];
176 $data["ENGINE_ID"] = $engine->getId();
177
178 $data['OWNER_ID'] = $ownerInfo['id'];
179 $data['OWNER_NAME'] = $ownerInfo['login'];
180
181 if(!static::$skipRemoteUpdate && $result->getType() == Entity\EventResult::SUCCESS)
182 {
183 try
184 {
185 $data["XML_ID"] = $engine->addBanner($data["SETTINGS"]);
186
187 $bannerSettings = $engine->getBanners(array($data['XML_ID']));
188 $data['SETTINGS'] = $bannerSettings[0];
189 }
191 {
192 $result->addError(new Entity\FieldError(
193 static::getEntity()->getField('ENGINE_ID'),
194 $e->getMessage(),
195 $e->getCode()
196 ));
197 }
198 }
199
200 $data['LAST_UPDATE'] = new Main\Type\DateTime();
201 $data['ACTIVE'] = $data['SETTINGS']['StatusArchive'] == Engine\YandexDirect::BOOL_YES
202 ? static::INACTIVE
203 : static::ACTIVE;
204
205 $result->modifyFields($data);
206
207 return $result;
208 }
209
221 public static function onBeforeUpdate(Entity\Event $event)
222 {
223 $result = new Entity\EventResult();
224
225 $primary = $event->getParameter("primary");
226 $data = $event->getParameter("fields");
227
228 $currentData = static::getByPrimary($primary);
229 $currentData = $currentData->fetch();
230
231 if($currentData)
232 {
233 $engine = self::getEngine();
234
235 if($currentData['ENGINE_ID'] != $engine->getId())
236 {
237 $result->addError(new Entity\FieldError(
238 static::getEntity()->getField('ENGINE_ID'),
239 Loc::getMessage("SEO_CAMPAIGN_ERROR_WRONG_ENGINE")
240 ));
241 }
242
243 $ownerInfo = $engine->getCurrentUser();
244
245 if($currentData['OWNER_ID'] != $ownerInfo['id'])
246 {
247 $result->addError(new Entity\FieldError(
248 static::getEntity()->getField('OWNER_ID'),
249 Loc::getMessage("SEO_CAMPAIGN_ERROR_WRONG_OWNER")
250 ));
251 }
252
253 $data['OWNER_NAME'] = $ownerInfo['login'];
254 $data['XML_ID'] = $currentData['XML_ID'];
255
256 if(!static::$skipRemoteUpdate)
257 {
258 $data["SETTINGS"] = self::createParam($engine, $data, $result);
259 if(is_array($data["SETTINGS"]['Phrases']))
260 {
261 $currentPhrases = $currentData["SETTINGS"]["Phrases"];
262
263 foreach($data["SETTINGS"]['Phrases'] as $key => $phraseInfo)
264 {
265 foreach($currentPhrases as $k => $currentPhrase)
266 {
267 if($currentPhrase['Phrase'] == $phraseInfo['Phrase'])
268 {
269 $data["SETTINGS"]['Phrases'][$key]['PhraseID'] = $currentPhrase['PhraseID'];
270 unset($currentPhrases[$k]);
271 }
272 }
273 }
274 }
275 }
276
277 $data["NAME"] = $data["SETTINGS"]["Title"];
278
279 if(!static::$skipRemoteUpdate && $result->getType() == Entity\EventResult::SUCCESS)
280 {
281 try
282 {
283 $engine->updateBanner($data["SETTINGS"]);
284
285 $bannerSettings = $engine->getBanners(array($data['XML_ID']));
286 $data['SETTINGS'] = $bannerSettings[0];
287 }
289 {
290 $result->addError(
291 new Entity\FieldError(
292 static::getEntity()->getField('ENGINE_ID'),
293 $e->getMessage(),
294 $e->getCode()
295 )
296 );
297 }
298 }
299
300 $data['LAST_UPDATE'] = new Main\Type\DateTime();
301 $data['ACTIVE'] = $data['SETTINGS']['StatusArchive'] == Engine\YandexDirect::BOOL_YES
302 ? static::INACTIVE
303 : static::ACTIVE;
304
305 $result->modifyFields($data);
306 }
307
308 return $result;
309 }
310
311
322 public static function onDelete(Entity\Event $event)
323 {
324 if(!static::$skipRemoteUpdate)
325 {
326 $primary = $event->getParameter("primary");
327
328 $engine = self::getEngine();
329
330 $dbRes = static::getList(array(
331 'filter' => array(
332 '=ID' => $primary,
333 '=ENGINE_ID' => $engine->getId(),
334 ),
335 'select' => array(
336 'XML_ID', 'CAMPAIGN_XML_ID' => 'CAMPAIGN.XML_ID',
337 )
338 ));
339 $banner = $dbRes->fetch();
340
341 if($banner)
342 {
343 $engine->deleteBanners($banner['CAMPAIGN_XML_ID'], array($banner['XML_ID']));
344 }
345 }
346 }
347
376 protected static function createParam(Engine\YandexDirect $engine, array $data, Entity\EventResult $result)
377 {
378 $bannerParam = array();
379
380 $newBanner = true;
381
382 if(!empty($data["XML_ID"]))
383 {
384 $newBanner = false;
385 $bannerParam["BannerID"] = $data["XML_ID"];
386 }
387
388 if(!empty($data["CAMPAIGN_ID"]))
389 {
390 $dbRes = YandexCampaignTable::getByPrimary($data["CAMPAIGN_ID"]);
391 $campaign = $dbRes->fetch();
392 if($campaign)
393 {
394 $data['SETTINGS']['CampaignID'] = $campaign['XML_ID'];
395 }
396 else
397 {
398 $result->addError(new Entity\FieldError(
399 static::getEntity()->getField('CAMPAIGN_ID'),
400 Loc::getMessage('SEO_BANNER_ERROR_CAMPAIGN_NOT_FOUND')
401 ));
402 }
403 }
404
405 if($newBanner || isset($data['SETTINGS']['CampaignID']))
406 {
407 $bannerParam['CampaignID'] = $data['SETTINGS']['CampaignID'];
408 }
409
410 if($newBanner || isset($data['SETTINGS']["Title"]))
411 {
412 $bannerParam["Title"] = trim($data['SETTINGS']["Title"]);
413
414 if($bannerParam["Title"] == '')
415 {
416 $result->addError(new Entity\FieldError(
417 static::getEntity()->getField('NAME'),
418 Loc::getMessage('SEO_BANNER_ERROR_NO_NAME')
419 ));
420 }
421 elseif(mb_strlen($bannerParam["Title"]) > static::MAX_TITLE_LENGTH)
422 {
423 $result->addError(new Entity\FieldError(
424 static::getEntity()->getField('NAME'),
425 Loc::getMessage('SEO_BANNER_ERROR_LONG_NAME', array(
426 "#MAX#" => static::MAX_TITLE_LENGTH,
427 ))
428 ));
429 }
430 }
431
432 if($newBanner || isset($data['SETTINGS']["Text"]))
433 {
434 $bannerParam["Text"] = trim($data['SETTINGS']["Text"]);
435 if($bannerParam["Text"] == '')
436 {
437 $result->addError(new Entity\FieldError(
438 static::getEntity()->getField('SETTINGS'),
439 Loc::getMessage('SEO_BANNER_ERROR_NO_TEXT')
440 ));
441 }
442 elseif(mb_strlen($bannerParam["Text"]) > static::MAX_TEXT_LENGTH)
443 {
444 $result->addError(new Entity\FieldError(
445 static::getEntity()->getField('SETTINGS'),
446 Loc::getMessage('SEO_BANNER_ERROR_LONG_TEXT', array(
447 "#MAX#" => static::MAX_TEXT_LENGTH,
448 ))
449 ));
450 }
451 }
452
453 if($newBanner || isset($data['SETTINGS']["Href"]))
454 {
455 $bannerParam["Href"] = trim($data['SETTINGS']["Href"]);
456 if($bannerParam["Href"] == '')
457 {
458 $result->addError(new Entity\FieldError(
459 static::getEntity()->getField('SETTINGS'),
460 Loc::getMessage('SEO_BANNER_ERROR_NO_HREF')
461 ));
462 }
463 }
464
465// format Geolocation seletc to string
466 if($newBanner || isset($data["SETTINGS"]["Geo"]))
467 {
468 if(is_array($data["SETTINGS"]["Geo"]))
469 {
470 $data["SETTINGS"]["Geo"] = implode(",", $data["SETTINGS"]["Geo"]);
471 }
472
473 $bannerParam["Geo"] = $data["SETTINGS"]["Geo"];
474 }
475
476 if($newBanner || isset($data["SETTINGS"]["Phrases"]))
477 {
478 if($data["SETTINGS"]["Geo"] == '')
479 {
480 $result->addError(new Entity\FieldError(
481 static::getEntity()->getField('SETTINGS'),
482 Loc::getMessage('SEO_BANNER_ERROR_NO_GEO')
483 ));
484 }
485 elseif(!is_array($data["SETTINGS"]["Phrases"]) || count($data["SETTINGS"]["Phrases"]) <= 0)
486 {
487 $result->addError(new Entity\FieldError(
488 static::getEntity()->getField('SETTINGS'),
489 Loc::getMessage('SEO_BANNER_ERROR_NO_PHRASES')
490 ));
491 }
492 else
493 {
494 $bannerParam["Phrases"] = $data["SETTINGS"]["Phrases"];
495
496 foreach($bannerParam["Phrases"] as $key => $phraseInfo)
497 {
498 if(is_numeric($phraseInfo['AutoBudgetPriority']))
499 $phraseInfo['AutoBudgetPriority'] = static::$priorityList[intval($phraseInfo['AutoBudgetPriority'])];
500
501 $bannerParam["Phrases"][$key] = $phraseInfo;
502 }
503 }
504 }
505
506 if($newBanner || isset($data["SETTINGS"]["MinusKeywords"]))
507 {
508 if(!is_array($data["SETTINGS"]["MinusKeywords"]))
509 {
510 if($data["SETTINGS"]["MinusKeywords"] <> '')
511 {
512 $data["SETTINGS"]["MinusKeywords"] = array();
513 }
514 else
515 {
516 $data["SETTINGS"]["MinusKeywords"] = array($data["SETTINGS"]["MinusKeywords"]);
517 }
518 }
519
520 $bannerParam["MinusKeywords"] = $data["SETTINGS"]["MinusKeywords"];
521 }
522
523 if(!$newBanner && $result->getType() == Entity\EventResult::SUCCESS)
524 {
525 try
526 {
527 $yandexBannerParam = $engine->getBanners(array($data["XML_ID"]));
528
529 if(!is_array($yandexBannerParam) || count($yandexBannerParam) <= 0)
530 {
531 $result->addError(new Entity\FieldError(
532 static::getEntity()->getField('XML_ID'),
534 'SEO_CAMPAIGN_ERROR_BANNER_NOT_FOUND',
535 array('#ID#' => $data["XML_ID"])
536 )
537 ));
538 }
539 else
540 {
541 $bannerParam = array_replace_recursive($yandexBannerParam[0], $bannerParam);
542 }
543 }
545 {
546 $result->addError(
547 new Entity\FieldError(
548 static::getEntity()->getField('ENGINE_ID'),
549 $e->getMessage(),
550 $e->getCode()
551 )
552 );
553 }
554 }
555
556 return $bannerParam;
557 }
558
559 public static function markStopped(array $idList)
560 {
561 if(count($idList) > 0)
562 {
563 $connection = Main\Application::getConnection();
564 $sqlHelper = $connection->getSqlHelper();
565
566 $idList = array_map("intval", $idList);
567
568 $update = $sqlHelper->prepareUpdate(static::getTableName(), array(
569 "AUTO_QUANTITY_OFF" => static::MARKED,
570 ));
571
572 $connection->queryExecute(
573 "UPDATE ".static::getTableName()." SET ".$update[0]." WHERE ID IN (".implode(",", $idList).")"
574 );
575 }
576 }
577
578 public static function markResumed(array $idList)
579 {
580 if(count($idList) > 0)
581 {
582 $connection = Main\Application::getConnection();
583 $sqlHelper = $connection->getSqlHelper();
584
585 $idList = array_map("intval", $idList);
586
587 $update = $sqlHelper->prepareUpdate(static::getTableName(), array(
588 "AUTO_QUANTITY_ON" => static::MARKED,
589 ));
590
591 $connection->queryExecute(
592 "UPDATE ".static::getTableName()." SET ".$update[0]." WHERE ID IN (".implode(",", $idList).")"
593 );
594 }
595 }
596
597 public static function unMarkStopped(array $idList)
598 {
599 if(count($idList) > 0)
600 {
601 $connection = Main\Application::getConnection();
602 $sqlHelper = $connection->getSqlHelper();
603
604 $idList = array_map("intval", $idList);
605
606 $update = $sqlHelper->prepareUpdate(static::getTableName(), array(
607 "AUTO_QUANTITY_OFF" => static::ACTIVE,
608 ));
609
610 $connection->queryExecute(
611 "UPDATE ".static::getTableName()." SET ".$update[0]." WHERE ID IN (".implode(",", $idList).")"
612 );
613 }
614 }
615
616 public static function unMarkResumed(array $idList)
617 {
618 if(count($idList) > 0)
619 {
620 $connection = Main\Application::getConnection();
621 $sqlHelper = $connection->getSqlHelper();
622
623 $idList = array_map("intval", $idList);
624
625 $update = $sqlHelper->prepareUpdate(static::getTableName(), array(
626 "AUTO_QUANTITY_ON" => static::ACTIVE,
627 ));
628
629 $connection->queryExecute(
630 "UPDATE ".static::getTableName()." SET ".$update[0]." WHERE ID IN (".implode(",", $idList).")"
631 );
632 }
633 }
634}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static onDelete(Entity\Event $event)
static onBeforeUpdate(Entity\Event $event)
static unMarkStopped(array $idList)
static createParam(Engine\YandexDirect $engine, array $data, Entity\EventResult $result)
static markResumed(array $idList)
static onBeforeAdd(Entity\Event $event)
static markStopped(array $idList)
static unMarkResumed(array $idList)