Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entityusagetable.php
1<?php
2
4
13
31{
32 use Data\Internal\DeleteByFilterTrait;
33
37 public static function getTableName()
38 {
39 return "b_entity_usage";
40 }
41
45 public static function getMap()
46 {
47 $connection = Application::getConnection();
48 $helper = $connection->getSqlHelper();
49
50 return [
51 new Fields\IntegerField("USER_ID", [
52 "primary" => true,
53 "required" => true
54 ]),
55 new Reference(
56 "USER",
57 UserTable::class,
58 Join::on("this.USER_ID", "ref.ID"),
59 ["join_type" => "INNER"]
60 ),
61 new Fields\StringField("ITEM_ID", [
62 "primary" => true,
63 "required" => true
64 ]),
65 new Fields\StringField("ENTITY_ID", [
66 "primary" => true,
67 "required" => true
68 ]),
69 new Fields\StringField("CONTEXT", [
70 "primary" => true,
71 "required" => true
72 ]),
73 new Fields\IntegerField("ITEM_ID_INT", [
74 "required" => true,
75 "default_value" => 0
76 ]),
77 new Fields\StringField("PREFIX", [
78 "default_value" => ''
79 ]),
80 new Fields\DatetimeField("LAST_USE_DATE", [
81 "required" => true,
82 "default_value" => function() {
83 return new DateTime();
84 }
85 ]),
86
87 //Compatible Fields for FinderDestTable
88 new Fields\ExpressionField(
89 'CODE',
90 'CASE WHEN %s=\'department\' THEN REPLACE(' . $helper->getConcatFunction('%s', '%s') . ', \':F\', \'\') ELSE ' . $helper->getConcatFunction('%s', '%s') . ' END',
91 ['ENTITY_ID', 'PREFIX', 'ITEM_ID', 'PREFIX', 'ITEM_ID']
92 ),
93 new Fields\ExpressionField(
94 'CODE_TYPE',
95 'CASE
96 WHEN %s = \'user\' THEN \'U\'
97 WHEN %s = \'project\' THEN \'SG\'
98 WHEN %s = \'department\' THEN \'D\'
99 WHEN %s IN(
100 \'crm-contact\',
101 \'crm-company\',
102 \'crm-lead\',
103 \'crm-quote\',
104 \'crm-deal\',
105 \'crm-order\',
106 \'crm-product\'
107 ) THEN \'CRM\'
108 END',
109 ['ENTITY_ID', 'ENTITY_ID', 'ENTITY_ID', 'ENTITY_ID']
110 ),
111
112 new Fields\ExpressionField(
113 'CODE_USER_ID',
114 'CASE WHEN %s = \'user\' THEN %s END',
115 ['ENTITY_ID', 'ITEM_ID_INT']
116 ),
117
118 new Fields\ExpressionField(
119 'MAX_LAST_USE_DATE',
120 'MAX(%s)', ['LAST_USE_DATE']
121 ),
122
123 new Reference(
124 "CODE_USER",
125 UserTable::class,
126 Join::on("this.ITEM_ID_INT", "ref.ID")->where('this.ENTITY_ID', 'user')
127 ),
128
129 new Reference(
130 'CODE_USER_CURRENT',
131 UserTable::class,
132 Join::on("this.ITEM_ID_INT", "ref.ID")
133 ->where('this.ENTITY_ID', 'user')
134 ->where('this.USER_ID', $GLOBALS['USER']->getId())
135 ),
136 ];
137 }
138
139 public static function getCompatEntities()
140 {
142 }
143
144 public static function merge(array $data)
145 {
146 $userId = (
147 isset($data['USER_ID']) && intval($data['USER_ID']) > 0
148 ? intval($data['USER_ID'])
149 : (is_object($GLOBALS['USER']) ? $GLOBALS['USER']->getId() : 0)
150 );
151
152 if ($userId <= 0)
153 {
154 return false;
155 }
156
157 if (empty($data['CONTEXT']) || !is_string($data['CONTEXT']))
158 {
159 return false;
160 }
161
162 if (empty($data['ENTITY_ID']) || !is_string($data['ENTITY_ID']))
163 {
164 return false;
165 }
166 $entityId = strtolower($data['ENTITY_ID']);
167
168 if (empty($data['ITEM_ID']) || (!is_string($data['ITEM_ID']) && !is_int($data['ITEM_ID'])))
169 {
170 return false;
171 }
172
173 $itemIdInteger = 0;
174 if (isset($data['ITEM_ID_INT']) && is_int($data['ITEM_ID_INT']))
175 {
176 $itemIdInteger = $data['ITEM_ID_INT'];
177 }
178 else if (preg_match('/(?<id>[0-9]+)/', (string)$data['ITEM_ID'], $matches))
179 {
180 $itemIdInteger = (int)$matches['id'];
181 }
182
183 $prefix = '';
184 if (!empty($data['PREFIX']) && is_string($data['PREFIX']))
185 {
186 $prefix = $data['PREFIX'];
187 }
188 else
189 {
190 $compatEntities = Converter::getCompatEntities();
191 if (isset($compatEntities[$entityId]))
192 {
193 $prefix = $compatEntities[$entityId]['prefix'];
194 if (is_callable($prefix))
195 {
196 $prefix = $prefix($data['ITEM_ID']);
197 }
198 }
199 }
200
201 $sqlHelper = Application::getConnection()->getSqlHelper();
202 $merge = $sqlHelper->prepareMerge(
203 static::getTableName(),
204 ['USER_ID', 'ITEM_ID', 'ENTITY_ID', 'CONTEXT'],
205 [
206 'USER_ID' => $userId,
207 'CONTEXT' => mb_strtoupper($data['CONTEXT']),
208 'ENTITY_ID' => $entityId,
209 'ITEM_ID' => $data['ITEM_ID'],
210 'ITEM_ID_INT' => $itemIdInteger,
211 'PREFIX' => $prefix,
212 'LAST_USE_DATE' => new Main\Type\DateTime(),
213 ],
214 [
215 'LAST_USE_DATE' => new Main\Type\DateTime()
216 ]
217 );
218
219 if ($merge[0] !== "")
220 {
221 Application::getConnection()->query($merge[0]);
222 }
223 else
224 {
225 return false;
226 }
227
228 return true;
229 }
230}
static getConnection($name="")
$GLOBALS['____1444769544']
Definition license.php:1