1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
OptionService.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Collab\Control\Option;
6
7use Bitrix\Main\DI\ServiceLocator;
8use Bitrix\Main\Error;
9use Bitrix\Main\Result;
10use Bitrix\Main\Validation\ValidationService;
11use Bitrix\Socialnetwork\Collab\Control\Option\Command\DeleteOptionsCommand;
12use Bitrix\Socialnetwork\Collab\Control\Option\Command\SetOptionsCommand;
13use Bitrix\Socialnetwork\Collab\Internals\CollabOptionTable;
14use Exception;
15
17{
19
20 public function __construct()
21 {
22 $this->init();
23 }
24
25 public function set(SetOptionsCommand $command): Result
26 {
27 $validationResult = $this->validationService->validate($command);
28 if (!$validationResult->isSuccess())
29 {
30 return $validationResult;
31 }
32
33 // $deleteCommand = (new DeleteOptionsCommand())
34 // ->setCollabId($command->getCollabId());
35
36 // $deleteResult = $this->delete($deleteCommand);
37 // if (!$deleteResult->isSuccess())
38 // {
39 // return $deleteResult;
40 // }
41
42 $result = new Result();
43
44 $uniqueFields = ['COLLAB_ID', 'NAME'];
45
46 $options = $command->getOptions()->getValue();
47 foreach ($options as $option)
48 {
49 $insert = [
50 'COLLAB_ID' => $command->getCollabId(),
51 'NAME' => $option->getName(),
52 'VALUE' => $option->getValue(),
53 ];
54
55 $update = [
56 'VALUE' => $option->getValue(),
57 ];
58
59 try
60 {
61 CollabOptionTable::merge($insert, $update, $uniqueFields);
62 }
63 catch (Exception $e)
64 {
65 $result->addError(Error::createFromThrowable($e));
66
67 return $result;
68 }
69
70 $applyResult = $option->apply($command->getCollabId());
71
72 if (!$applyResult->isSuccess())
73 {
74 return $applyResult;
75 }
76 }
77
78 return $result;
79 }
80
81 public function delete(DeleteOptionsCommand $command): Result
82 {
83 $validationResult = $this->validationService->validate($command);
84 if (!$validationResult->isSuccess())
85 {
86 return $validationResult;
87 }
88
89 $result = new Result();
90
91 try
92 {
93 CollabOptionTable::deleteByFilter(['COLLAB_ID' => $command->getCollabId()]);
94 }
95 catch (Exception $e)
96 {
97 $result->addError(Error::createFromThrowable($e));
98 }
99
100 return $result;
101 }
102
103 protected function init(): void
104 {
105 $this->validationService = ServiceLocator::getInstance()->get('main.validation.service');
106 }
107}
$options
Определения commerceml2.php:49
$result
Определения get_property_values.php:14
$option
Определения options.php:1711