1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Arrilot\BitrixMigrations\Constructors; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Arrilot\BitrixMigrations\Helpers; |
8
|
|
|
use Arrilot\BitrixMigrations\Logger; |
9
|
|
|
use Bitrix\Highloadblock\HighloadBlockLangTable; |
10
|
|
|
use Bitrix\Highloadblock\HighloadBlockTable; |
11
|
|
|
|
12
|
|
|
class HighloadBlock |
13
|
|
|
{ |
14
|
|
|
use FieldConstructor; |
15
|
|
|
|
16
|
|
|
public $lang; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Добавить HL |
20
|
|
|
* @throws \Exception |
21
|
|
|
*/ |
22
|
|
|
public function add() |
23
|
|
|
{ |
24
|
|
|
$result = HighloadBlockTable::add($this->getFieldsWithDefault()); |
25
|
|
|
|
26
|
|
|
if (!$result->isSuccess()) { |
27
|
|
|
throw new \Exception(join(', ', $result->getErrorMessages())); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
foreach ($this->lang as $lid => $name) { |
31
|
|
|
HighloadBlockLangTable::add([ |
32
|
|
|
"ID" => $result->getId(), |
33
|
|
|
"LID" => $lid, |
34
|
|
|
"NAME" => $name |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
Logger::log("Добавлен HL {$this->fields['NAME']}", Logger::COLOR_GREEN); |
39
|
|
|
|
40
|
|
|
return $result->getId(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Обновить HL |
45
|
|
|
* @param $table_name |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function update($table_name) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$id = Helpers::getHlId($table_name); |
51
|
|
|
$result = HighloadBlockTable::update($id, $this->fields); |
52
|
|
|
|
53
|
|
|
if (!$result->isSuccess()) { |
54
|
|
|
throw new \Exception(join(', ', $result->getErrorMessages())); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
Logger::log("Обновлен HL {$table_name}", Logger::COLOR_GREEN); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Удалить HL |
62
|
|
|
* @param $table_name |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public static function delete($table_name) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$id = Helpers::getHlId($table_name); |
68
|
|
|
$result = HighloadBlockTable::delete($id); |
69
|
|
|
|
70
|
|
|
if (!$result->isSuccess()) { |
71
|
|
|
throw new \Exception(join(', ', $result->getErrorMessages())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
Logger::log("Удален HL {$table_name}", Logger::COLOR_GREEN); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Установить настройки для добавления HL по умолчанию |
79
|
|
|
* @param string $name Название highload-блока |
80
|
|
|
* @param string $table_name Название таблицы с элементами highload-блока. |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function constructDefault($name, $table_name) |
84
|
|
|
{ |
85
|
|
|
return $this->setName($name)->setTableName($table_name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Название highload-блока. |
90
|
|
|
* @param string $name |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setName($name) |
94
|
|
|
{ |
95
|
|
|
$this->fields['NAME'] = $name; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Название таблицы с элементами highload-блока. |
102
|
|
|
* @param string $table_name |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setTableName($table_name) |
106
|
|
|
{ |
107
|
|
|
$this->fields['TABLE_NAME'] = $table_name; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Установить локализацию |
114
|
|
|
* @param $lang |
115
|
|
|
* @param $text |
116
|
|
|
* @return HighloadBlock |
117
|
|
|
*/ |
118
|
|
|
public function setLang($lang, $text) |
119
|
|
|
{ |
120
|
|
|
$this->lang[$lang] = $text; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.