Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
agentcontracttable.php
1<?php
2namespace Bitrix\Catalog;
3
5use Bitrix\Main\Entity\EventResult;
20
52{
58 public static function getTableName(): string
59 {
60 return 'b_catalog_agent_contract';
61 }
62
68 public static function getMap(): array
69 {
70 return [
71 'ID' => new IntegerField(
72 'ID',
73 [
74 'primary' => true,
75 'autocomplete' => true,
76 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_ID_FIELD'),
77 ]
78 ),
79 'AGENT_PRODUCT' => new Reference(
80 'AGENT_PRODUCT',
81 AgentProductTable::class,
82 Join::on('this.ID', 'ref.CONTRACT_ID')
83 ),
84 'TITLE' => new StringField(
85 'TITLE',
86 [
87 'required' => true,
88 'validation' => function()
89 {
90 return[
91 new LengthValidator(null, 255),
92 ];
93 },
94 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_TITLE_FIELD'),
95 ]
96 ),
97 'CONTRACTOR_ID' => new IntegerField(
98 'CONTRACTOR_ID',
99 [
100 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_CONTRACTOR_ID_FIELD'),
101 ]
102 ),
103 'CONTRACTOR' => new Reference(
104 'CONTRACTOR',
105 ContractorTable::class,
106 Join::on('this.CONTRACTOR_ID', 'ref.ID')
107 ),
108 'DATE_MODIFY' => new DatetimeField(
109 'DATE_MODIFY',
110 [
111 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_DATE_MODIFY_FIELD'),
112 ]
113 ),
114 'DATE_CREATE' => new DatetimeField(
115 'DATE_CREATE',
116 [
117 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_DATE_CREATE_FIELD'),
118 'default_value' => new DateTime(),
119 ]
120 ),
121 'MODIFIED_BY' => new IntegerField(
122 'MODIFIED_BY',
123 [
124 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_MODIFIED_BY_FIELD'),
125 ]
126 ),
127 'MODIFIED_BY_USER' => new Reference(
128 'MODIFIED_BY_USER',
129 UserTable::class,
130 Join::on('this.MODIFIED_BY', 'ref.ID')
131 ),
132 'CREATED_BY' => new IntegerField(
133 'CREATED_BY',
134 [
135 'title' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_CREATED_BY_FIELD'),
136 ]
137 ),
138 'CREATED_BY_USER' => new Reference(
139 'CREATED_BY_USER',
140 UserTable::class,
141 Join::on('this.CREATED_BY', 'ref.ID')
142 ),
143 ];
144 }
145
146 public static function withProductList(Query $query, array $productIds)
147 {
148 Collection::normalizeArrayValuesByInt($productIds);
149 if (empty($productIds))
150 {
151 return;
152 }
153
154 $tableName = AgentProductTable::getTableName();
155 $whereExpression = '(PRODUCT_ID IN (' . implode(',', $productIds) . '))';
156
157 $connection = Application::getConnection();
158 $helper = $connection->getSqlHelper();
159 $productType = $helper->forSql(AgentProductTable::PRODUCT_TYPE_PRODUCT);
160
161 $query->whereExpr("
162 (
163 CASE WHEN EXISTS (
164 SELECT ID
165 FROM {$tableName}
166 WHERE CONTRACT_ID = %s
167 AND PRODUCT_TYPE = '{$productType}'
168 AND {$whereExpression}
169 )
170 THEN 1
171 ELSE 0
172 END
173 ) = 1
174 ", ['ID']);
175 }
176
177 public static function withSectionList(Query $query, array $sectionIds)
178 {
179 Collection::normalizeArrayValuesByInt($sectionIds);
180 if (empty($sectionIds))
181 {
182 return;
183 }
184
185 $tableName = AgentProductTable::getTableName();
186 $whereExpression = '(PRODUCT_ID IN (' . implode(',', $sectionIds) . '))';
187
188 $connection = Application::getConnection();
189 $helper = $connection->getSqlHelper();
190 $productType = $helper->forSql(AgentProductTable::PRODUCT_TYPE_SECTION);
191
192 $query->whereExpr("
193 (
194 CASE WHEN EXISTS (
195 SELECT ID
196 FROM {$tableName}
197 WHERE CONTRACT_ID = %s
198 AND PRODUCT_TYPE = '{$productType}'
199 AND {$whereExpression}
200 )
201 THEN 1
202 ELSE 0
203 END
204 ) = 1
205 ", ['ID']);
206 }
207
208 public static function onBeforeAdd(Event $event)
209 {
210 $result = new EventResult;
211 $data = $event->getParameter('fields');
212
213 if (empty($data['TITLE']))
214 {
215 $result->modifyFields([
216 'TITLE' => Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_TITLE_DEFAULT'),
217 ]);
218 }
219
220 return $result;
221 }
222
223 public static function onAfterAdd(Event $event)
224 {
225 $data = $event->getParameters();
226 $id = $event->getParameter('id');
227
228 if ($data['fields']['TITLE'] === Loc::getMessage('CATALOG_AGENT_CONTRACT_ENTITY_TITLE_DEFAULT'))
229 {
230 $title = Loc::getMessage(
231 'CATALOG_AGENT_CONTRACT_ENTITY_TITLE_DEFAULT',
232 [
233 '#' => $id,
234 ]
235 );
236
238 $id,
239 [
240 'TITLE' => $title,
241 ]
242 );
243 }
244 }
245
246 public static function onAfterDelete(Event $event)
247 {
248 $result = new EventResult();
249
250 $id = (int)$event->getParameter('primary')['ID'];
251
252 $contractorsProvider = Contractor\Provider\Manager::getActiveProvider(
253 Contractor\Provider\Manager::PROVIDER_AGENT_CONTRACT
254 );
255 if ($contractorsProvider)
256 {
257 $contractorsProvider::onAfterDocumentDelete($id);
258 }
259
260 // delete files
262
263 return $result;
264 }
265}
static withProductList(Query $query, array $productIds)
static withSectionList(Query $query, array $sectionIds)
static getConnection($name="")
getParameter($key)
Definition event.php:80
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static update($primary, array $data)