forked from arrilot/bitrix-migrations
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
909 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Arrilot\BitrixMigrations\Traits; | ||
|
||
/** | ||
* Class ClearCacheTrait | ||
* @package Arrilot\BitrixMigrations\Traits | ||
* | ||
* @since 11.04.2021 | ||
*/ | ||
class ClearCacheTrait | ||
{ | ||
/** | ||
* Очищает все виды кэша. | ||
* | ||
* @return void | ||
*/ | ||
protected function clearCache() : void | ||
{ | ||
global $USER_FIELD_MANAGER; | ||
if ($USER_FIELD_MANAGER) { | ||
$USER_FIELD_MANAGER->CleanCache(); | ||
} | ||
|
||
BXClearCache(true, '/'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Arrilot\BitrixMigrations\Traits; | ||
|
||
use CEventType; | ||
use Exception; | ||
|
||
/** | ||
* Trait EmailEventTrait | ||
* @package Arrilot\BitrixMigrations\Traits | ||
* | ||
* @since 11.04.2021 | ||
*/ | ||
trait EmailEventTrait | ||
{ | ||
/** | ||
* Создает новый тип почтовых событий. | ||
* | ||
* @param array $eventData | ||
* | ||
* @return array | ||
* | ||
* @throws Exception | ||
*/ | ||
public function createEmailEventType(array $eventData): array | ||
{ | ||
if ($this->findEmailEventType($eventData)) { | ||
throw new Exception("Email event type {$eventData['EVENT_NAME']} already exists"); | ||
} | ||
|
||
$res = CEventType::add($eventData); | ||
if (!$res) { | ||
throw new Exception("Can't create email event type {$eventData['EVENT_NAME']}: {$et->LAST_ERROR}"); | ||
} | ||
|
||
return ["Email event type {$eventData['EVENT_NAME']}({$res}) created"]; | ||
} | ||
|
||
/** | ||
* Обновляет тип почтовых событий. | ||
* | ||
* @param array $eventData | ||
* | ||
* @return array | ||
* | ||
* @throws Exception | ||
*/ | ||
public function updateEmailEventType(array $eventData): array | ||
{ | ||
if (!($event = $this->findEmailEventType($eventData))) { | ||
throw new Exception("Can't find {$eventData['EVENT_NAME']} email event type"); | ||
} | ||
$et = new CEventType; | ||
unset($eventData['EVENT_NAME'], $eventData['LID']); | ||
$result = CEventType::update(['ID' => $event['ID']], $eventData); | ||
if (!$result) { | ||
throw new Exception("Can't update email event type {$eventData['EVENT_NAME']}: {$et->LAST_ERROR}"); | ||
} | ||
|
||
return ["Email event type {$event['EVENT_NAME']}({$event['ID']}) updated"]; | ||
} | ||
|
||
/** | ||
* Удаляет тип почтового события по его идентификатору (EVENT_NAME). | ||
* | ||
* @param array $eventData | ||
* | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
public function deleteEmailEventType(array $eventData): array | ||
{ | ||
if (!($event = $this->findEmailEventType($eventData))) { | ||
throw new Exception("Can't find {$eventData['EVENT_NAME']} email event type"); | ||
} | ||
|
||
CEventType::delete(['ID' => $event['ID']]); | ||
|
||
return ["Email event type {$eventData['EVENT_NAME']}({$event['ID']}) deleted"]; | ||
} | ||
|
||
/** | ||
* Ищет тип почтового события по массиву параметров. | ||
* | ||
* @param array $eventData | ||
* | ||
* @return array|null | ||
* | ||
* @throws Exception | ||
*/ | ||
public function findEmailEventType(array $eventData): ?array | ||
{ | ||
if (empty($eventData['EVENT_NAME'])) { | ||
throw new Exception('Empty email event type name'); | ||
} | ||
$filter = ['TYPE_ID' => $eventData['EVENT_NAME']]; | ||
if (!empty($eventData['LID'])) { | ||
$filter['LID'] = $eventData['LID']; | ||
} | ||
|
||
return CEventType::getList($filter)->fetch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Arrilot\BitrixMigrations\Traits; | ||
|
||
use CEventMessage; | ||
use Exception; | ||
|
||
/** | ||
* Trait EmailTemplateTrait | ||
* @package Arrilot\BitrixMigrations\Traits | ||
* | ||
* @since 11.04.2021 | ||
*/ | ||
trait EmailTemplateTrait | ||
{ | ||
/** | ||
* Создает новый шаблон для почтового сообщения. | ||
* | ||
* @param array $templateData | ||
* | ||
* @return array | ||
* | ||
* @throws Exception | ||
*/ | ||
public function createEmailTemplate(array $templateData): array | ||
{ | ||
if ($this->findEmailTemplate($templateData)) { | ||
throw new Exception("Email template for event {$templateData['EVENT_NAME']} already exists"); | ||
} | ||
|
||
$et = new CEventMessage; | ||
$res = $et->add($templateData); | ||
if (!$res) { | ||
throw new Exception("Can't create email event type {$templateData['EVENT_NAME']}: {$et->LAST_ERROR}"); | ||
} | ||
|
||
return ["Email template({$res}) for event {$templateData['EVENT_NAME']} created"]; | ||
} | ||
|
||
/** | ||
* Обновляет шаблон почтового сообщения. | ||
* | ||
* @param array $templateData | ||
* @param array $search | ||
* | ||
* @return array | ||
* | ||
* @throws Exception | ||
*/ | ||
public function updateEmailTemplate(array $search, array $templateData): array | ||
{ | ||
if (!($template = $this->findEmailTemplate($search))) { | ||
throw new Exception("Email template for event {$templateData['EVENT_NAME']} not found"); | ||
} | ||
|
||
$et = new CEventMessage; | ||
unset($templateData['EVENT_NAME'], $templateData['LID']); | ||
$res = $et->update($template['ID'], $templateData); | ||
if (!$res) { | ||
throw new Exception("Can't update template for event {$templateData['EVENT_NAME']}: {$et->LAST_ERROR}"); | ||
} | ||
|
||
return ["Email template({$template['ID']}) for event {$template['EVENT_NAME']} updated"]; | ||
} | ||
|
||
/** | ||
* Удаляет шаблон почтового сообщения. | ||
* | ||
* @param array $templateData | ||
* | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
public function deleteEmailTemplate(array $templateData): array | ||
{ | ||
if (!($template = $this->findEmailTemplate($templateData))) { | ||
throw new Exception("Email template for event {$templateData['EVENT_NAME']} not found"); | ||
} | ||
|
||
$et = new CEventMessage; | ||
$res = $et->delete($template['ID']); | ||
|
||
if (!$res) { | ||
throw new Exception("Can't delete template({$template['ID']}) for type {$templateData['EVENT_NAME']}"); | ||
} | ||
|
||
return ["Email template({$template['ID']}) for type {$templateData['EVENT_NAME']} deleted"]; | ||
} | ||
|
||
/** | ||
* Ищет шаблон почтового сообщения по массиву параметров. | ||
* | ||
* @param array $templateData | ||
* | ||
* @return array|null | ||
* | ||
* @throws Exception | ||
*/ | ||
public function findEmailTemplate(array $templateData): ?array | ||
{ | ||
if (empty($templateData['EVENT_NAME'])) { | ||
throw new Exception('Empty email event type name'); | ||
} | ||
|
||
$filter = ['TYPE_ID' => $templateData['EVENT_NAME']]; | ||
if (!empty($templateData['LID'])) { | ||
$filter['SITE_ID'] = $templateData['LID']; | ||
} | ||
if (!empty($templateData['SUBJECT'])) { | ||
$filter['SUBJECT'] = $templateData['SUBJECT']; | ||
} | ||
|
||
$return = null; | ||
$rsMess = CEventMessage::GetList( | ||
($by = 'site_id'), | ||
($order = 'desc'), | ||
$filter | ||
); | ||
while ($template = $rsMess->fetch()) { | ||
if ($return) { | ||
throw new Exception("More than one template are found: {$templateData['EVENT_NAME']}"); | ||
} | ||
$return = $template; | ||
} | ||
|
||
return $return; | ||
} | ||
} |
Oops, something went wrong.