Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messageusertype.php
1<?php
2
3namespace Bitrix\Mail;
4
9
10Loc::loadMessages(__FILE__);
11
13{
14
15 const USER_TYPE_ID = 'mail_message';
16
17 public static function getUserTypeDescription()
18 {
19 return array(
20 'USER_TYPE_ID' => static::USER_TYPE_ID,
21 'CLASS_NAME' => __CLASS__,
22 'DESCRIPTION' => Loc::getMessage('MAIL_MESSAGE_USER_TYPE_NAME'),
23 'BASE_TYPE' => 'int',
24 'VIEW_CALLBACK' => array(__CLASS__, 'getPublicView'),
25 'EDIT_CALLBACK' => array(__CLASS__, 'getPublicEdit'),
26 'onBeforeSave' => array(__CLASS__, 'onBeforeSave'),
27 'onDelete' => array(__CLASS__, 'onDelete'),
28 );
29 }
30
31 public static function getDbColumnType($userField)
32 {
33 $connection = \Bitrix\Main\Application::getConnection();
34 $helper = $connection->getSqlHelper();
35 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
36 }
37
38 public static function getPublicView($userField, $params)
39 {
40 return static::getPublicHtml($userField, $params, 'view');
41 }
42
43 public static function getPublicEdit($userField, $params)
44 {
45 return static::getPublicHtml($userField, $params, 'edit');
46 }
47
48 protected static function getPublicHtml($userField, $params, $mode = 'view')
49 {
50 global $APPLICATION;
51
52 if (!in_array($mode, array('view', 'edit')))
53 {
54 $mode = 'view';
55 }
56
57 ob_start();
58
59 foreach ((array) $userField['VALUE'] as $item)
60 {
61 $APPLICATION->includeComponent(
62 'bitrix:mail.uf.message', '',
63 array(
64 'USER_FIELD' => $userField,
65 'MESSAGE_ID' => (int) $item,
66 'MODE' => $mode,
67 ),
68 null,
69 array('HIDE_ICONS' => 'Y')
70 );
71 }
72
73 return ob_get_clean();
74 }
75
76 public static function onBeforeSaveAll($userField, $mailIds, $userId)
77 {
78 $previousMailsIds = (array) $userField['VALUE'];
79 if ($previousMailsIds !== false)
80 {
81 $result = static::deleteList([
82 '!@MESSAGE_ID' => $mailIds,
83 '=ENTITY_UF_ID' => $userField['ID'],
84 '=ENTITY_TYPE' => $userField['ENTITY_ID'],
85 '=ENTITY_ID' => $userField['VALUE_ID'],
86 ]);
87 }
88 $newMailIdsToSave = array_diff($mailIds, $previousMailsIds);
89 foreach ($newMailIdsToSave as $mailMessageId)
90 {
91 static::onBeforeSave($userField, $mailMessageId, $userId);
92 }
93 }
94
95 public static function onBeforeSave($userField, $mailMessageId, $userId)
96 {
97 $previousMailsIds = (array) $userField['VALUE'];
98 if (!in_array($mailMessageId, $previousMailsIds))
99 {
100 $message = MailMessageTable::getList([
101 'select' => [
102 'ID', 'MAILBOX_ID',
103 ],
104 'filter' => [
105 '=ID' => $mailMessageId,
106 ],
107 ])->fetch();
108
109 if (MailboxTable::getUserMailbox($message['MAILBOX_ID'], $userId))
110 {
111 if ($userField['VALUE'] !== false && $userField['MULTIPLE'] === 'N')
112 {
113 $result = static::deleteList([
114 '!=MESSAGE_ID' => $mailMessageId,
115 '=ENTITY_UF_ID' => $userField['ID'],
116 '=ENTITY_TYPE' => $userField['ENTITY_ID'],
117 '=ENTITY_ID' => $userField['VALUE_ID'],
118 ]);
119 }
120 Internals\MessageAccessTable::add([
121 'TOKEN' => md5(sprintf(
122 '%u:%u:%u:%s:%s:%u',
123 time(),
124 $message['MAILBOX_ID'],
125 $mailMessageId,
126 $userField['ENTITY_ID'],
127 $userField['ID'],
128 $userField['VALUE_ID']
129 )),
130 'MAILBOX_ID' => $message['MAILBOX_ID'],
131 'MESSAGE_ID' => $mailMessageId,
132 'ENTITY_UF_ID' => $userField['ID'],
133 'ENTITY_TYPE' => $userField['ENTITY_ID'],
134 'ENTITY_ID' => $userField['VALUE_ID'],
135 'SECRET' => bin2hex(Main\Security\Random::getBytes(16)),
136 'OPTIONS' => [],
137 ]);
138 static::sendEntityCreatedEvents($message, $userField);
139 }
140 }
141
142 return $mailMessageId;
143 }
144
145 public static function onDelete($userField, $messageId)
146 {
147 $result = static::deleteList([
148 '=MESSAGE_ID' => $messageId,
149 '=ENTITY_UF_ID' => $userField['ID'],
150 '=ENTITY_TYPE' => $userField['ENTITY_ID'],
151 '=ENTITY_ID' => $userField['ENTITY_VALUE_ID'],
152 ]);
153 }
154
155 private static function deleteList($filter)
156 {
157 $entity = Internals\MessageAccessTable::getEntity();
158 $connection = $entity->getConnection();
159 return $connection->query(sprintf(
160 'DELETE FROM %s WHERE %s',
161 $connection->getSqlHelper()->quote($entity->getDbTableName()),
162 Main\Entity\Query::buildFilterSql(
163 $entity,
164 $filter
165 )
166 ));
167 }
168
169 private static function sendEntityCreatedEvents($message, $userField)
170 {
171 if (Main\Loader::includeModule('pull'))
172 {
173 $bindingEntityLink = '';
174
175 if ($userField['ENTITY_ID'] === MessageAccessTable::ENTITY_TYPE_CRM_ACTIVITY
176 && Loader::includeModule('crm'))
177 {
178 $bindingEntity = \Bitrix\Crm\ActivityTable::query()
179 ->addSelect('OWNER_TYPE_ID')
180 ->addSelect('OWNER_ID')
181 ->where('ID', $userField['VALUE_ID'])
182 ->exec()
183 ->fetch();
184
185 if ($bindingEntity)
186 {
187 $bindingEntityLink = \CCrmOwnerType::GetEntityShowPath($bindingEntity['OWNER_TYPE_ID'], $bindingEntity['OWNER_ID']);
188 }
189 }
190
191 if ($userField['ENTITY_ID'] === MessageAccessTable::ENTITY_TYPE_TASKS_TASK
192 && Loader::includeModule('tasks'))
193 {
194 $bindingEntity = \Bitrix\Tasks\Internals\TaskTable::getList([
195 'select' => ['ID'],
196 'filter' => [
197 '=ID' => $userField['VALUE_ID'],
198 ],
199 'limit' => 1,
200 ])->fetch();
201
202 if ($bindingEntity)
203 {
204 global $USER;
205 $userPage = \Bitrix\Main\Config\Option::get('socialnetwork', 'user_page', '/company/personal/', SITE_ID);
206 $bindingEntityLink = \CComponentEngine::makePathFromTemplate(
207 \Bitrix\Main\Config\Option::get(
208 'tasks',
209 'paths_task_user_action',
210 $userPage . 'user/#user_id#/tasks/task/#action#/#task_id#/',
211 SITE_ID
212 ),
213 [
214 'user_id' => $USER->getId(),
215 'action' => 'view',
216 'task_id' => $bindingEntity['ID'],
217 ]
218 );
219 }
220 }
221
222 if ($userField['ENTITY_ID'] === MessageAccessTable::ENTITY_TYPE_BLOG_POST
223 && Loader::includeModule('blog'))
224 {
225 $bindingEntity = \Bitrix\Blog\PostTable::getList([
226 'select' => ['ID'],
227 'filter' => [
228 '=ID' => $userField['VALUE_ID'],
229 ],
230 'limit' => 1,
231 ])->fetch();
232
233 if ($bindingEntity)
234 {
235 global $USER;
236 $userPage = \Bitrix\Main\Config\Option::get('socialnetwork', 'user_page', '/company/personal/', SITE_ID);
237 $bindingEntityLink = \CComponentEngine::makePathFromTemplate(
238 $userPage . 'user/#user_id#/blog/#post_id#/',
239 [
240 'user_id' => $USER->getId(),
241 'post_id' => $bindingEntity['ID'],
242 ]
243 );
244 }
245 }
246
247 \CPullWatch::addToStack(
248 'mail_mailbox_' . $message['MAILBOX_ID'],
249 [
250 'module_id' => 'mail',
251 'command' => 'messageBindingCreated',
252 'params' => [
253 'messageId' => $message['ID'],
254 'mailboxId' => $message['MAILBOX_ID'],
255 'entityType' => $userField['ENTITY_ID'],
256 'entityId' => $userField['VALUE_ID'],
257 'bindingEntityLink' => $bindingEntityLink,
258 ],
259 ]
260 );
261 }
262 $event = new Main\Event(
263 'mail',
264 'onBeforeUserFieldSave',
265 [
266 'mailbox_id' => $message['MAILBOX_ID'],
267 'message_id' => $message['ID'],
268 'entity_uf_id' => $userField['ID'],
269 'entity_type' => $userField['ENTITY_ID'],
270 'entity_id' => $userField['VALUE_ID'],
271 ]
272 );
273 $event->send();
274 }
275}
static getUserMailbox($mailboxId, $userId=null)
Definition mailbox.php:110
static onDelete($userField, $messageId)
static getPublicHtml($userField, $params, $mode='view')
static getDbColumnType($userField)
static getPublicView($userField, $params)
static onBeforeSave($userField, $mailMessageId, $userId)
static onBeforeSaveAll($userField, $mailIds, $userId)
static getPublicEdit($userField, $params)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29