20 'USER_TYPE_ID' => static::USER_TYPE_ID,
21 'CLASS_NAME' => __CLASS__,
24 'VIEW_CALLBACK' => array(__CLASS__,
'getPublicView'),
25 'EDIT_CALLBACK' => array(__CLASS__,
'getPublicEdit'),
26 'onBeforeSave' => array(__CLASS__,
'onBeforeSave'),
27 'onDelete' => array(__CLASS__,
'onDelete'),
33 $connection = \Bitrix\Main\Application::getConnection();
34 $helper = $connection->getSqlHelper();
40 return static::getPublicHtml($userField, $params,
'view');
45 return static::getPublicHtml($userField, $params,
'edit');
48 protected static function getPublicHtml($userField, $params, $mode =
'view')
52 if (!in_array($mode, array(
'view',
'edit')))
59 foreach ((array) $userField[
'VALUE'] as $item)
61 $APPLICATION->includeComponent(
62 'bitrix:mail.uf.message',
'',
64 'USER_FIELD' => $userField,
65 'MESSAGE_ID' => (
int) $item,
69 array(
'HIDE_ICONS' =>
'Y')
73 return ob_get_clean();
78 $previousMailsIds = (array) $userField[
'VALUE'];
79 if ($previousMailsIds !==
false)
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'],
88 $newMailIdsToSave = array_diff($mailIds, $previousMailsIds);
89 foreach ($newMailIdsToSave as $mailMessageId)
91 static::onBeforeSave($userField, $mailMessageId, $userId);
95 public static function onBeforeSave($userField, $mailMessageId, $userId)
97 $previousMailsIds = (array) $userField[
'VALUE'];
98 if (!in_array($mailMessageId, $previousMailsIds))
100 $message = MailMessageTable::getList([
105 '=ID' => $mailMessageId,
111 if ($userField[
'VALUE'] !==
false && $userField[
'MULTIPLE'] ===
'N')
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'],
120 Internals\MessageAccessTable::add([
121 'TOKEN' => md5(sprintf(
124 $message[
'MAILBOX_ID'],
126 $userField[
'ENTITY_ID'],
128 $userField[
'VALUE_ID']
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)),
138 static::sendEntityCreatedEvents($message, $userField);
142 return $mailMessageId;
145 public static function onDelete($userField, $messageId)
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'],
155 private static function deleteList($filter)
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(
169 private static function sendEntityCreatedEvents($message, $userField)
171 if (Main\Loader::includeModule(
'pull'))
173 $bindingEntityLink =
'';
175 if ($userField[
'ENTITY_ID'] === MessageAccessTable::ENTITY_TYPE_CRM_ACTIVITY
176 && Loader::includeModule(
'crm'))
178 $bindingEntity = \Bitrix\Crm\ActivityTable::query()
179 ->addSelect(
'OWNER_TYPE_ID')
180 ->addSelect(
'OWNER_ID')
181 ->where(
'ID', $userField[
'VALUE_ID'])
187 $bindingEntityLink = \CCrmOwnerType::GetEntityShowPath($bindingEntity[
'OWNER_TYPE_ID'], $bindingEntity[
'OWNER_ID']);
191 if ($userField[
'ENTITY_ID'] === MessageAccessTable::ENTITY_TYPE_TASKS_TASK
192 && Loader::includeModule(
'tasks'))
194 $bindingEntity = \Bitrix\Tasks\Internals\TaskTable::getList([
197 '=ID' => $userField[
'VALUE_ID'],
205 $userPage = \Bitrix\Main\Config\Option::get(
'socialnetwork',
'user_page',
'/company/personal/', SITE_ID);
206 $bindingEntityLink = \CComponentEngine::makePathFromTemplate(
209 'paths_task_user_action',
210 $userPage .
'user/#user_id#/tasks/task/#action#/#task_id#/',
214 'user_id' => $USER->getId(),
216 'task_id' => $bindingEntity[
'ID'],
222 if ($userField[
'ENTITY_ID'] === MessageAccessTable::ENTITY_TYPE_BLOG_POST
223 && Loader::includeModule(
'blog'))
225 $bindingEntity = \Bitrix\Blog\PostTable::getList([
228 '=ID' => $userField[
'VALUE_ID'],
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#/',
240 'user_id' => $USER->getId(),
241 'post_id' => $bindingEntity[
'ID'],
247 \CPullWatch::addToStack(
248 'mail_mailbox_' . $message[
'MAILBOX_ID'],
250 'module_id' =>
'mail',
251 'command' =>
'messageBindingCreated',
253 'messageId' => $message[
'ID'],
254 'mailboxId' => $message[
'MAILBOX_ID'],
255 'entityType' => $userField[
'ENTITY_ID'],
256 'entityId' => $userField[
'VALUE_ID'],
257 'bindingEntityLink' => $bindingEntityLink,
262 $event =
new Main\Event(
264 'onBeforeUserFieldSave',
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'],
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)