\
]*>\<\/p>/i' => '
',
'/\
/i' => '',
'/\<\/p>/i' => '
',
'/
'
];
$sNewMessage = preg_replace(array_keys($aReplacements), $aReplacements, $sMessage);
return preg_replace('/(?:(?:\s*)?
)+$/', '', $sNewMessage);
}
function actionGetGiphy()
{
if (!$this->isLogged())
return '';
$aContent = $this->_oTemplate->getGiphyItems(bx_get('action'), urlencode(bx_get('filter')), (float) bx_get('height'), (int) bx_get('start'));
if (isset($aContent['content']) && $aContent['content'])
return echoJson(array('code' => 0, 'html' => $aContent['content'], 'total' => isset($aContent['pagination']) ? $aContent['pagination']['total_count'] : (int) bx_get('start')));
return echoJson(array('code' => 1, 'message' => MsgBox(_t('_bx_messenger_giphy_gifs_nothing_found'))));
}
function serviceGetTalksList($aOptions = [])
{
$mixedGroup = isset($aOptions['group']) ? $aOptions['group'] : '';
$iId = isset($aOptions['id']) ? (int) $aOptions['id'] : 0;
$iLotId = isset($aOptions['lot']) ? (int) $aOptions['lot'] : 0;
$aParams = ['start' => isset($aOptions['count']) ? (int) $aOptions['count'] : 0, 'lot' => $iLotId];
$CNF = &$this->_oConfig->CNF;
$aLotInfo = $iLotId ? $this->_oDb->getLotInfoById($iLotId) : [];
if (!empty($aLotInfo) && ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId)))
return ['code' => 1, 'message' => _t('_bx_messenger_not_participant')];
$sTitle = _t('_bx_messenger_nav_menu_item_title_' . ($mixedGroup ? $mixedGroup : 'inbox'));
$aLotsList = [];
switch ($mixedGroup) {
case BX_MSG_TALK_TYPE_THREADS:
$aParams['threads'] = true;
$aLotsList = $this->_oDb->getMyLots($this->_iProfileId, $aParams);
break;
case 'public':
$aParams['type'] = array(BX_IM_TYPE_SETS, BX_IM_TYPE_PUBLIC);
$aLotsList = $this->_oDb->getMyLots($this->_iProfileId, $aParams);
break;
case BX_MSG_TALK_TYPE_FILES:
break;
case BX_MSG_TALK_TYPE_MR:
$aLotsList = $this->_oDb->getReactionsMentionsLots($this->_iProfileId, $aParams);
break;
case BX_MSG_TALK_TYPE_REPLIES:
$aLotsList = $this->_oDb->getLotsWithReplies($this->_iProfileId, $aParams);
break;
case BX_MSG_TALK_TYPE_SAVED:
$aLotsList = $this->_oDb->getSavedJotInLots($this->_iProfileId, $aParams);
break;
case BX_MSG_TALK_TYPE_FAVORITES:
// Talks marked with a star by the viewer, the same filter as the star toggle on the site.
$aLotsList = $this->_oDb->getMyLots($this->_iProfileId, array_merge($aParams, ['star' => true]));
break;
case BX_MSG_TALK_TYPE_GROUPS:
$aParams['group'] = $iId;
$aLotsList = $this->_oDb->getMyLots($this->_iProfileId, $aParams);
if (empty($aLotsList))
break;
$aGroupInfo = $this->_oDb->getGroupById($iId);
$sTitle = !empty($aGroupInfo) ? $aGroupInfo[$CNF['FMG_NAME']] : current($aLotsList)['title'];
break;
case BX_MSG_TALK_TYPE_DIRECT:
$aLotsList = $this->_oDb->getMyLots($this->_iProfileId, array_merge($aParams, ['type' => BX_IM_TYPE_PRIVATE]));
break;
default:
$aLotsList = $this->_oDb->getMyLots($this->_iProfileId, array_merge($aParams, ['inbox' => true]));
}
$bNext = false;
if (isset($aParams['per_page']) && count($aLotsList) === (int) $aParams['per_page'])
$bNext = true;
else if (count($aLotsList) === $CNF['MAX_LOTS_NUMBER'])
$bNext = true;
return ['title' => $sTitle, 'list' => $aLotsList, 'code' => 0, 'next' => $bNext];
}
function actionGetTalksList()
{
if (!$this->isLogged())
return echoJson(['code' => 1, 'reload' => 1]);
$aParams = ['group' => bx_get('group'), 'id' => (int) bx_get('id'), 'count' => (int) bx_get('count'), 'lot' => (int) bx_get('lot')];
$iExcludeConvo = (int) bx_get('exclude_convo');
$aData = $this->serviceGetTalksList($aParams);
if (isset($aData['code']) && (int) $aData['code'])
return echoJson(['code' => 1, 'msg' => $aData['message']]);
$sTitle = isset($aData['title']) ? $aData['title'] : '';
$aLotsList = isset($aData['list']) ? $aData['list'] : [];
if (!empty($aLotsList)) {
if ($iExcludeConvo) {
$mixedKey = array_search($iExcludeConvo, array_column($aLotsList, $this->_oConfig->CNF['FIELD_ID']));
if ($mixedKey !== false)
unset($aLotsList[$mixedKey]);
}
$sContent = $this->_oTemplate->getLotsPreview($this->_iProfileId, $aLotsList);
return echoJson(array(
'code' => 0,
'html' => $sContent,
'title' => _t($sTitle)
));
}
return echoJson(['code' => 1, 'html' => MsgBox(_t('_bx_messenger_txt_msg_no_results')), 'title' => _t($sTitle)]);
}
public function actionGetTopMenuItems()
{
$CNF = &$this->_oConfig->CNF;
$oLeftMainMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_NAV_LEFT_MENU']);
$oLeftGroupsMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_GROUPS_MENU']);
return ['top' => $oLeftMainMenu->getMenuItems(), 'groups' => $oLeftGroupsMenu->getMenuItems()];
}
public function serviceGetLotStat($mixedLotId = '')
{
if (is_numeric($mixedLotId) && (int) $mixedLotId)
$aLotInfo = $this->_oDb->getLotInfoById($mixedLotId);
else {
$sUrl = $mixedLotId ? $this->getPreparedUrl($mixedLotId) : $this->_oConfig->getPageIdent($mixedLotId);
$aLotInfo = $this->_oDb->getLotByUrl($sUrl);
}
if (empty($aLotInfo))
return $aLotInfo;
$CNF = &$this->_oConfig->CNF;
$aMessages = $this->_oDb->getLotJotsCount($aLotInfo[$CNF['FIELD_ID']]);
return array(
'title' => $aLotInfo[$CNF['FIELD_TITLE']],
'created' => $aLotInfo[$CNF['FIELD_ADDED']],
'participants' => $aLotInfo[$CNF['FIELD_PARTICIPANTS']] ? explode(',', $aLotInfo[$CNF['FIELD_PARTICIPANTS']]) : array(),
'author' => $aLotInfo[$CNF['FIELD_AUTHOR']],
'url' => $aLotInfo[$CNF['FIELD_URL']],
'type' => $aLotInfo[$CNF['FIELD_TYPE']],
'messages' => array_merge($aMessages, array(
'total' => array_sum($aMessages)
))
);
}
public function serviceGetUnreadLots($iProfileId)
{
if (!$iProfileId || !($oProfile = BxDolProfile::getInstance($iProfileId)))
return array();
$aLots = $this->_oDb->getLotsWithUnreadMessages($iProfileId);
$aLotsInfo = array();
foreach ($aLots as $iLotId => $iCount) {
$aLotsInfo[$iLotId] = array(
'count' => $iCount,
'participants' => $this->_oDb->getParticipantsList($iLotId),
'new_messages' => $this->_oDb->getUnreadMessages($iProfileId, $iLotId)
);
}
return $aLotsInfo;
}
public function actionGetJitsiConferenceForm($iLotId = 0)
{
if (!$this->_iProfileId) {
echo MsgBox(_t('_bx_messenger_jitsi_err_can_join_conference'));
exit;
}
header('Content-type: text/html; charset=utf-8');
if (!$iLotId) {
$sUrl = bx_get('url');
$sTitle = bx_get('title');
$aLot = $this->_oDb->getLotByUrl($sUrl);
if (empty($aLot)) {
$this->_oDb->createNewLot($this->_iProfileId, array('title' => $sTitle, 'type' => BX_IM_TYPE_PRIVATE, 'url' => $sUrl));
$this->onCreateLot($iLotId);
} else
$iLotId = $aLot[$this->_oConfig->CNF['FIELD_ID']];
}
$CNF = &$this->_oConfig->CNF;
$aLotInfo = $this->_oDb->getLotInfoById($iLotId);
$bIsParticipant = $this->_oDb->isParticipant($iLotId, $this->_iProfileId);
if (!$bIsParticipant) {
if ($aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE) {
echo MsgBox(_t('_bx_messenger_jitsi_err_can_join_conference'));
exit;
}
if (!empty($aLotInfo))
$this->_oDb->addMemberToParticipantsList($iLotId, $this->_iProfileId);
}
$aParams['audio_only'] = bx_get('startAudioOnly') ? 1 : 0;
$mixedAuthor = $this->_oDb->getActiveJVCItem($iLotId, $CNF['FJVCT_AUTHOR_ID']);
if (($mixedAuthor === false || ((int) $mixedAuthor == $this->_iProfileId)) && $this->_oConfig->isJitsiAllowed($aLotInfo[$CNF['FIELD_TYPE']])) {
echo $this->_oTemplate->getJitsi($iLotId, $this->_iProfileId, $aParams);
exit;
}
$sMessage = _t('_bx_messenger_jitsi_err_cant_type_use');
if ($mixedAuthor !== false && ((int) $mixedAuthor != $this->_iProfileId) && ($mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_TALK_VC))) {
if ($mixedResult !== true)
$sMessage = $mixedResult;
else {
echo $this->_oTemplate->getJitsi($iLotId, $this->_iProfileId, $aParams);
exit;
}
}
echo MsgBox($sMessage, 2.5);
exit;
}
public function onCreateVC($iLotId)
{
bx_alert($this->_oConfig->getObject('alert'), 'create_vc', $iLotId, $this->_iUserId);
}
public function onJoinVC($iLotId)
{
$aItem = $this->_oDb->getActiveJVCItem($iLotId);
bx_alert($this->_oConfig->getObject('alert'), 'join_vc', $iLotId, $this->_iUserId, array('object_author_id' => $aItem[$this->_oConfig->CNF['FJVCT_AUTHOR_ID']]));
}
public function actionCreateJitsiVideoConference()
{
$CNF = $this->_oConfig->CNF;
$iLotId = (int) bx_get('lot_id');
if (!$iLotId)
return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_found')));
if (!$this->isLogged())
return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_jitsi_err_can_join_conference')));
$aLotInfo = $this->_oDb->getLotInfoById($iLotId);
if (empty($aLotInfo))
return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_not_found')));
$bIsParticipant = $this->_oDb->isParticipant($iLotId, $this->_iProfileId);
if (!$bIsParticipant) {
if ($aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE)
return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_jitsi_err_can_join_conference')));
$this->_oDb->addMemberToParticipantsList($iLotId, $this->_iProfileId);
}
$mixedAuthor = $this->_oDb->getActiveJVCItem($iLotId, $CNF['FJVCT_AUTHOR_ID']);
if ($mixedAuthor != $this->_iProfileId && ($mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_TALK_VC))) {
if ($mixedResult !== true)
return echoJson(array('code' => 1, 'message' => $mixedResult));
}
$aJVC = $this->_oDb->getJVC($iLotId);
$iParticipants = count($this->_oDb->getParticipantsList($iLotId));
$aResult = array('code' => 0, 'parts' => $iParticipants);
if (!empty($aJVC) && (int) $aJVC[$CNF['FJVC_ACTIVE']]) {
if ($this->_oDb->joinToActiveJVC($iLotId, $this->_iProfileId)) {
$aResult['jot_id'] = $this->_oDb->getJotIdByJitsiItem((int) $aJVC[$CNF['FJVC_ACTIVE']]);
$aResult[$CNF['FJVC_ROOM']] = $aJVC[$CNF['FJVC_ROOM']];
$this->onJoinVC($iLotId);
return echoJson($aResult);
} else
$this->_oDb->closeJVC($iLotId);
}
$this->onCreateVC($iLotId);
return echoJson(
array_merge(
array('new' => 1),
$aResult,
$this->_oDb->createJVC($iLotId, $this->_iProfileId)
)
);
}
public function actionStopJvc()
{
$iLotId = (int) bx_get('lot_id');
if (!$iLotId)
return echoJson(array('code' => 1, 'msg' => _t('_bx_messenger_not_found')));
if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
return echoJson(array('code' => 1));
if ($this->_oDb->stopJVC($iLotId, $this->_iProfileId))
return echoJson(array('code' => 0));
return echoJson(array('code' => 1));
}
public function actionLeaveJvc()
{
if (!isLogged())
return '';
$iLotId = (int) bx_get('lot_id');
$this->_oDb->leaveJVC($iLotId, $this->_iProfileId);
}
function actionGetTalkFiles()
{
$iLotId = (int) bx_get('lot_id');
$iNumber = (int) bx_get('number');
$CNF = $this->_oConfig->CNF;
if (!$iLotId)
return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_found'))));
$aLotInfo = $this->_oDb->getLotInfoById($iLotId);
if (empty($aLotInfo) || ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE && !$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId)))
return echoJson(array('code' => 1, 'message' => '_bx_messenger_not_participant'));
if (!($iTotal = $this->_oDb->getLotFilesCount($iLotId)))
return echoJson(array('code' => 0, 'html' => MsgBox(_t('_bx_messenger_txt_msg_no_results'))));
$sContent = $this->_oTemplate->getTalkFiles($this->_iProfileId, $iLotId, $iNumber);
return echoJson(array('code' => 0, 'html' => ($iTotal !== $iNumber ? $sContent : ''), 'total' => $iTotal));
}
function actionGetCallPopup()
{
$iLotId = (int) bx_get('lot');
$aResult = array('code' => 1, 'msg' => _t('_bx_messenger_not_found'));
if (!$iLotId || !isLogged())
return echoJson($aResult);
if ($this->_oConfig->isAllowedAction(BX_MSG_ACTION_JOIN_TALK_VC) !== true)
$aResult = array('code' => 1);
else {
$mixedContent = $this->_oTemplate->getCallPopup($iLotId, $this->_iProfileId);
$aResult = array('code' => +($mixedContent === false), 'popup' => $mixedContent);
}
return echoJson($aResult);
}
function actionGetLotSettings()
{
$iLotId = (int) bx_get('lot');
$aResult = array('code' => 1, 'msg' => _t('_bx_messenger_not_found'));
if (!$iLotId || !isLogged())
return echoJson($aResult);
$aResult = array('code' => 1, 'msg' => _t('_bx_messenger_lot_can_change_settings'));
$bCheckAction = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true;
if (!($this->_oDb->isAuthor($iLotId, $this->_iProfileId) || $bCheckAction))
return echoJson($aResult);
$mixedContent = $this->_oTemplate->getLotSettingsForm($iLotId);
$aResult = array(
'code' => +($mixedContent === false),
'popup' => array(
'html' => $mixedContent,
'options' => array(
'closeOnOuterClick' => true,
'removeOnClose' => true
)
)
);
return echoJson($aResult);
}
function actionSaveLotSettings()
{
$iLotId = (int) bx_get('lot');
$aOptions = bx_get('options');
$aResult = ['code' => 1, 'msg' => _t('_bx_messenger_not_found')];
if (!$iLotId || !isLogged() || !($aLotInfo = $this->_oDb->getLotInfoById($iLotId)))
return echoJson($aResult);
$aResult = ['code' => 1, 'msg' => _t('_bx_messenger_lot_can_change_settings')];
$bCheckAction = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_ADMINISTRATE_TALKS, $this->_iProfileId) === true;
if (!($this->_oDb->isAuthor($iLotId, $this->_iProfileId) || $bCheckAction))
return echoJson($aResult);
$aResult = ['code' => 1, 'msg' => _t('_bx_messenger_nothing_has_been_saved')];
if ($this->_oDb->saveLotSettings($iLotId, $aOptions))
return echoJson(['code' => 0]);
return echoJson($aResult);
}
function serviceSearch($sText = '', $iStart = 0, $iLimit = 10, $sOrder = 'DESC', $iLotId = 0)
{
$CNF = $this->_oConfig->CNF;
if (($iLotId && !isLogged()) || (!isAdmin() && !$iLotId))
return _t('_bx_messenger_no_permissions');
$sResult = $this->_oDb->findInHistory($iLotId, $sText, $iStart, $iLimit, $sOrder);
if (empty($sResult))
return array();
$aMessages = array();
foreach ($sResult as &$aMessage) {
$iType = $aMessage[$CNF['FIELD_TYPE']];
$sTitle = $aMessage[$CNF['FIELD_TITLE']] ?
_t($aMessage[$CNF['FIELD_TITLE']]) : _t('_bx_messenger_lots_private_lot');
$aMessages[] = array_merge($aMessage, array(
$CNF['FIELD_TITLE'] => $sTitle,
'message_url' => $this->_oConfig->getRepostUrl($aMessage['message_id']),
$CNF['FIELD_PARTICIPANTS'] => explode(',', $aMessage[$CNF['FIELD_PARTICIPANTS']])
));
}
return $aMessages;
}
function serviceGetVideoConference($sRoomIdent, $iInitiator, $sTitle = '', $sId = '', $aInterfaceConfig = array())
{
if (!$iInitiator)
if (!$sRoomIdent)
$sRoomIdent = $this->_iProfileId;
return $this->_oTemplate->getPublicJitsi($iInitiator, $sRoomIdent, $sTitle, $sId, $aInterfaceConfig);
}
function serviceGetPublicRoomInfo($sRoomId)
{
return $this->_oDb->getPublicVideoRoom($sRoomId);
}
function serviceGenerateRoomName($sIdent)
{
return $this->_oConfig->getRoomId($sIdent);
}
function actionGetEmojiPicker()
{
if (!$this->_iProfileId)
return;
echo $this->_oTemplate->getEmojiCode();
exit;
}
function actionUpdateUploadedFiles()
{
$iJotId = bx_get('jot_id');
$aFiles = bx_get('files');
if (!$this->isLogged())
return echoJson(['code' => 1, 'message' => MsgBox(_t('_bx_messenger_not_logged')), 'reload' => 1]);
if (!$iJotId || empty($aFiles))
return echoJson(['code' => 1, 'message' => MsgBox(_t('_bx_messenger_files_can_not_be_uploaded'))]);
$mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_SEND_MESSAGE, $this->_iProfileId);
if ($mixedResult !== true)
return echoJson(array('code' => 1, 'message' => $mixedResult));
$CNF = &$this->_oConfig->CNF;
$aLotInfo = $this->_oDb->getLotByJotId($iJotId, false);
if (
empty($aLotInfo) || ($aLotInfo[$CNF['FIELD_TYPE']] === BX_IM_TYPE_PRIVATE &&
!$this->_oDb->isParticipant($aLotInfo[$CNF['FIELD_ID']], $this->_iProfileId))
)
return echoJson(array('code' => 1, 'message' => '_bx_messenger_not_participant'));
$aUploadedFiles = $this->_oDb->getJotFiles($iJotId);
$oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
$aSuccessfulFiles = array();
foreach ($aFiles as &$aFile) {
$sRealName = $aFile['realname'];
if (
!empty($aUploadedFiles) && array_filter($aUploadedFiles, function ($aF) use ($sRealName, $CNF) {
return $aF[$CNF['FIELD_ST_NAME']] == $sRealName;
})
)
continue;
if (!$this->_oConfig->isValidToUpload($aFile['name'])) {
$this->_oDb->deleteJot($iJotId, $this->_iProfileId, true);
return echoJson(array('code' => 1, 'message' => _t('_bx_messenger_bad_request')));
}
$sFile = BX_DIRECTORY_PATH_TMP . basename($aFile['name']);
$sExt = $oStorage->getFileExt($sFile);
$iFileId = $oStorage->storeFileFromPath($sFile, $aLotInfo[$CNF['FIELD_TYPE']] == BX_IM_TYPE_PRIVATE, $this->_iProfileId, (int) $iJotId);
$sFilename = $oStorage->getFileTitle($sRealName) . ".{$sExt}";
if ($iFileId) {
$oStorage->afterUploadCleanup($iFileId, $this->_iProfileId);
$this->_oDb->updateFiles($iFileId, array(
$CNF['FIELD_ST_JOT'] => $iJotId,
$CNF['FIELD_ST_NAME'] => $sFilename
));
$aSuccessfulFiles[] = $sFilename;
@unlink($sFile);
}
}
if (count($aSuccessfulFiles)) {
$aFilesData = [];
$aJotInfo = $this->_oDb->getJotById($iJotId);
if (!empty($aJotInfo[$CNF['FIELD_MESSAGE_AT']])) {
$aFilesData = @unserialize($aJotInfo[$CNF['FIELD_MESSAGE_AT']]);
if (!is_array($aFilesData))
$aFilesData = [];
if (isset($aFilesData[BX_ATT_TYPE_FILES_UPLOADING]))
unset($aFilesData[BX_ATT_TYPE_FILES_UPLOADING]);
}
$aStoredFiles = [];
if (!empty($aFilesData[BX_ATT_TYPE_FILES])) {
if (is_array($aFilesData[BX_ATT_TYPE_FILES]))
$aStoredFiles = $aFilesData[BX_ATT_TYPE_FILES];
elseif (is_string($aFilesData[BX_ATT_TYPE_FILES])) {
$aStoredFiles = @unserialize($aFilesData[BX_ATT_TYPE_FILES]);
if (!is_array($aStoredFiles))
$aStoredFiles = [];
}
}
$aFilesData[BX_ATT_TYPE_FILES] = $aStoredFiles + $aSuccessfulFiles;
$this->_oDb->updateJot($iJotId, $CNF['FIELD_MESSAGE_AT'], @serialize($aFilesData));
}
echoJson(array('code' => 0));
}
function serviceGetSearchOptions()
{
$aResult = array();
$CNF = &$this->_oConfig->CNF;
foreach ($CNF['SEARCH-CRITERIA'] as &$sItem)
$aResult[$sItem] = _t("_bx_messenger_search_item_{$sItem}");
return $aResult;
}
function serviceSetTalkAvatar($iLotId, $mixedFile)
{
if (!$mixedFile)
return false;
return $this->setLotAvatar($iLotId, $mixedFile);
}
function setLotAvatar($iLotId, $mixedFile = null)
{
if (!$this->isLogged() || $mixedFile === null)
return false;
if (!($aLotInfo = $this->_oDb->getLotInfoById($iLotId)))
return false;
$CNF = &$this->_oConfig->CNF;
$oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
if (is_array($mixedFile) && isset($mixedFile['name'])) {
$sFile = BX_DIRECTORY_PATH_TMP . basename($mixedFile['name']);
$iFileId = $oStorage->storeFileFromPath($sFile, false, $this->_iProfileId, $iLotId);
} elseif (filter_var($mixedFile, FILTER_VALIDATE_URL) !== false)
$iFileId = $oStorage->storeFileFromUrl($mixedFile, false, $this->_iProfileId, $iLotId);
elseif ((int) $mixedFile)
$iFileId = (int) $mixedFile;
if ($iFileId) {
$oStorage->afterUploadCleanup($iFileId, $this->_iProfileId);
$this->_oDb->saveLotSettings($iLotId, $iFileId, $CNF['FLS_ICON']);
}
}
function actionGetCreateGroupForm()
{
$iGroupId = (int) bx_get('group');
if (!$this->isLogged())
return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))));
$mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_GROUPS, $this->_iProfileId);
if ($mixedResult !== true)
return echoJson(array('code' => 1, 'msg' => _t($mixedResult)));
$aResult = array('code' => 0, 'popup' => $this->_oTemplate->getCreateGroupsForm($this->_iProfileId, $iGroupId));
echoJson($aResult);
}
function actionSaveGroup()
{
if (!$this->isLogged())
return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))));
$mixedResult = $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_GROUPS, $this->_iProfileId);
if ($mixedResult !== true)
return echoJson(array('code' => 1, 'msg' => _t($mixedResult)));
if ($this->_oDb->addGroup($this->_iProfileId, $_POST))
return echoJson(array('code' => 0));
choJson(array('code' => 1, 'msg' => _t('_bx_messenger_nothing_has_been_saved')));
}
function actionLoadGroupsList()
{
if (!$this->isLogged())
return echoJson(array('code' => 1, 'msg' => MsgBox(_t('_bx_messenger_not_logged'))));
echoJson(array('code' => 0, 'content' => $this->_oTemplate->getNavGroupsMenu($this->_iProfileId)));
}
function serviceIsBlockVersion()
{
return $this->_isBlockMessenger;
}
function actionAddMembersToGroups()
{
$CNF = &$this->_oConfig->CNF;
if (!$this->isLogged())
return false;
$aGroups = $this->_oDb->getAll("SELECT
`l`.*, `g`.`{$CNF['FMG_MODULE']}`, `g`.`{$CNF['FMG_NAME']}`, `gl`.`{$CNF['FMGL_GROUP_ID']}`,
IF (`l`.`{$CNF['FIELD_UPDATED']}` = 0, `l`.`{$CNF['FIELD_ADDED']}`, `l`.`{$CNF['FIELD_UPDATED']}`) as `order`
FROM `{$CNF['TABLE_ENTRIES']}` as `l`
LEFT JOIN `{$CNF['TABLE_GROUPS_LOTS']}` as `gl` ON `gl`.`{$CNF['FMGL_LOT_ID']}` = `l`.`{$CNF['FIELD_ID']}`
LEFT JOIN `{$CNF['TABLE_GROUPS']}` as `g` ON `g`.`{$CNF['FMG_ID']}` = `gl`.`{$CNF['FMGL_GROUP_ID']}`
WHERE `l`.`{$CNF['FIELD_TYPE']}` > 3
ORDER BY `g`.`{$CNF['FMG_MODULE']}`"); //AND (`gl`.`{$CNF['FMGL_GROUP_ID']}` IS NULL OR `gl`.`{$CNF['FMGL_GROUP_ID']}` = 0)
foreach ($aGroups as $aGroup) {
if ($aGroup[$CNF['FIELD_URL']])
$this->_oDb->registerGroup($aGroup[$CNF['FIELD_URL']], $aGroup[$CNF['FIELD_ID']]);
}
}
function actionUpdateGroups()
{
if (!isLogged())
return false;
$aModules = bx_srv('system', 'get_modules_by_type', ['context']);
bx_import('BxDolConnection');
foreach ($aModules as &$aModule)
if (!bx_srv($aModule['name'], 'act_as_profile')) {
$aMyItems = bx_srv($aModule['name'], 'get_participating_profiles', [$this->_iProfileId]);
foreach ($aMyItems as &$iItem) {
$oGroupProfile = BxDolProfile::getInstance($iItem);
if ($oGroupProfile) {
$aItemInfo = bx_srv($aModule['name'], 'get_info', array($oGroupProfile->getContentId(), false));
$oModule = BxDolModule::getInstance($aModule['name']);
$CNF = &$oModule->_oConfig->CNF;
$oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS']);
if (!$oConnection)
continue;
$aFans = $oConnection->getConnectedInitiators($iItem);
if (!empty($aFans)) {
$aObject = BxDolPageQuery::getPageObject($CNF['OBJECT_PAGE_VIEW_ENTRY']);
$sUrl = "i={$aObject['uri']}&id=" . $oGroupProfile->getContentId();
$aLotInfo = $this->_oDb->getLotByUrl($sUrl);
if (empty($aLotInfo)) {
$iLotId = $this->_oDb->createLot($aItemInfo[$CNF['FIELD_AUTHOR']], $sUrl, $aItemInfo[$CNF['FIELD_TITLE']], BX_IM_TYPE_GROUPS, $aFans);
$this->_oDb->registerGroup($sUrl, $iLotId);
}
}
}
}
}
}
function actionMediaAccordion()
{
$bShown = (bool) bx_get('hidden');
$iFileId = (int) bx_get('id');
if (!$this->isLogged() || !$iFileId)
return false;
$oStorage = BxDolStorage::getObjectInstance($this->_oConfig->CNF['OBJECT_STORAGE']);
if (!($aFile = $oStorage->getFile($iFileId)))
return false;
$iLotId = $this->_oDb->getLotByJotId($aFile[$this->_oConfig->CNF['FIELD_ST_JOT']]);
if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
return false;
if (!$bShown)
$this->_oDb->addMediaTrack($iFileId, $this->_iProfileId);
else
$this->_oDb->removeMediaTracker($iFileId, $this->_iProfileId);
}
function serviceConvertToReply()
{
$CNF = &$this->_oConfig->CNF;
$aJots = $this->_oDb->getAll("SELECT * FROM `{$CNF['TABLE_MESSAGES']}`
WHERE `{$CNF['FIELD_MESSAGE_AT_TYPE']}`=:type OR `{$CNF['FIELD_MESSAGE_AT']}` LIKE '%\"reply\"%'", array('type' => BX_ATT_TYPE_REPLY));
foreach ($aJots as &$aJot) {
if ($aJot[$CNF['FIELD_MESSAGE_AT_TYPE']] == BX_ATT_TYPE_REPLY && (int) $aJot[$CNF['FIELD_MESSAGE_AT']])
$this->_oDb->query(
"UPDATE `{$CNF['TABLE_MESSAGES']}` SET `{$CNF['FIELD_MESSAGE_REPLY']}`=:reply WHERE `{$CNF['FIELD_MESSAGE_ID']}`=:id",
array('id' => $aJot[$CNF['FIELD_MESSAGE_ID']], 'reply' => (int) $aJot[$CNF['FIELD_MESSAGE_AT']])
);
elseif (!$aJot[$CNF['FIELD_MESSAGE_AT_TYPE']] && $aJot[$CNF['FIELD_MESSAGE_AT']]) {
$aAttachment = @unserialize($aJot[$CNF['FIELD_MESSAGE_AT']]);
if (!empty($aAttachment[BX_ATT_TYPE_REPLY]))
$this->_oDb->query("UPDATE `{$CNF['TABLE_MESSAGES']}` SET `{$CNF['FIELD_MESSAGE_REPLY']}`=:reply
WHERE `{$CNF['FIELD_MESSAGE_ID']}`=:id", array('id' => $aJot[$CNF['FIELD_MESSAGE_ID']], 'reply' => $aAttachment[BX_ATT_TYPE_REPLY]));
}
}
}
function serviceUpdateMemberships()
{
$aMembershipLevels = BxDolAcl::getInstance()->getMemberships();
$sModuleName = $this->getName();
$iActionId = BxDolAcl::getInstance()->getMembershipActionId('administrate messages', $sModuleName);
$iActionEditId = BxDolAcl::getInstance()->getMembershipActionId('edit messages', $sModuleName);
$iActionDeleteId = BxDolAcl::getInstance()->getMembershipActionId('delete messages', $sModuleName);
if (!($iActionId && $iActionEditId && $iActionDeleteId) || empty($aMembershipLevels))
return false;
foreach ($aMembershipLevels as $iMembershipId => $sMembership) {
$aAction = BxDolAclQuery::getInstance()->getAction($iMembershipId, $iActionId);
$aActionEdit = BxDolAclQuery::getInstance()->getAction($iMembershipId, $iActionEditId);
$aActionDelete = BxDolAclQuery::getInstance()->getAction($iMembershipId, $iActionDeleteId);
if (is_null($aAction['id']) || !is_null($aActionEdit['id']) || !is_null($aActionDelete['id']))
continue;
$this->_oDb->query("INSERT INTO `sys_acl_matrix` SET `IDLevel`=:level, `IDAction`=:action", array('level' => $iMembershipId, 'action' => $iActionDeleteId));
$this->_oDb->query("INSERT INTO `sys_acl_matrix` SET `IDLevel`=:level, `IDAction`=:action", array('level' => $iMembershipId, 'action' => $iActionEditId));
$this->_oDb->query("DELETE FROM `sys_acl_matrix` WHERE `IDLevel`=:level AND `IDAction`=:action", array('level' => $iMembershipId, 'action' => $iActionId));
}
return $iActionId;
}
function servicePublicToPages()
{
$CNF = &$this->_oConfig->CNF;
$aLots = $this->_oDb->getAll("SELECT * FROM `{$CNF['TABLE_ENTRIES']}`
WHERE `{$CNF['FIELD_TYPE']}`!=:type AND `{$CNF['FIELD_URL']}` != ''", array('type' => BX_IM_TYPE_PRIVATE));
foreach ($aLots as &$aLot) {
$iGroupId = $this->_oDb->getGroupIdByLotId($aLot[$CNF['FIELD_ID']]);
if ($iGroupId)
continue;
$sUrl = $aLot[$CNF['FIELD_URL']];
if ($sUrl === 'index.php')
$sUrl = "i=index";
else
if (stripos($sUrl, 'i=') === FALSE)
$sUrl = "i=" . str_replace('r=', '', $aLot[$CNF['FIELD_URL']]);
$aGroup = $this->_oDb->getGroupByUrl($sUrl);
if (empty($aGroup))
$this->_oDb->registerGroup($sUrl, $aLot[$CNF['FIELD_ID']], true);
else
$this->_oDb->addLotToGroup($aLot[$CNF['FIELD_ID']], $aGroup[$CNF['FMG_ID']]);
}
}
public function serviceGetSafeServices()
{
return [
'FindConvo' => 'BxMessengerServices',
'LeaveConvo' => 'BxMessengerServices',
'DeleteConvo' => 'BxMessengerServices',
'ClearConvo' => 'BxMessengerServices',
'MuteConvo' => 'BxMessengerServices',
'StarConvo' => 'BxMessengerServices',
'SaveJot' => 'BxMessengerServices',
'SearchConvoMessages' => 'BxMessengerServices',
'GetConvoFiles' => 'BxMessengerServices',
'GetConvoSettings' => 'BxMessengerServices',
'SaveConvoSettings' => 'BxMessengerServices',
'GetConvo' => 'BxMessengerServices',
'GetConvoUrl' => 'BxMessengerServices',
'GetConvosList' => 'BxMessengerServices',
'GetConvoMessages' => 'BxMessengerServices',
'GetSendForm' => 'BxMessengerServices',
'RemoveJot' => 'BxMessengerServices',
'SearchUsers' => 'BxMessengerServices',
'SearchLots' => 'BxMessengerServices',
'GetPartsList' => 'BxMessengerServices',
'SavePartsList' => 'BxMessengerServices',
'GetBlockContacts' => 'BxMessengerServices',
//--- Aren't used in App for now.
'GetConvoMessage' => '',
'GetConferenceInfoByUid' => 'BxMessengerServices',
'GetConvoItem' => '',
'ClearGhost' => '',
];
}
/*
* Should be moved to Services if it's needed, otherwise removed.
*/
public function serviceClearGhost($sParams)
{
$aOptions = json_decode($sParams, true);
if (!isset($aOptions['id']))
return [];
$CNF = &$this->_oConfig->CNF;
$aJotInfo = $this->_oDb->getJotById($aOptions['id']);
if (empty($aJotInfo) || !$this->isAvailable($aJotInfo[$CNF['FIELD_MESSAGE_FK']]))
return [];
$oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
if (!($aFiles = $this->_oDb->getJotFiles($aOptions['id'])))
return [];
$aResultFiles = array_map(function ($aFile) use ($CNF) {
return $aFile[$CNF['FIELD_ST_ID']];
}, $aFiles);
$oStorage->afterUploadCleanup($aResultFiles, $this->_iProfileId);
}
/*
* Moved to Services
*/
public function serviceSavePartsList($sParams)
{
$aOptions = json_decode($sParams, true);
$aResult = ['code' => 1];
if (!$sParams || !isset($aOptions['parts']))
return $aResult;
$aResult = $this->saveParticipantsList($aOptions['parts'], (isset($aOptions['id']) ? $aOptions['id'] : 0));
if (isset($aResult['lot'])) {
$CNF = &$this->_oConfig->CNF;
$aLotInfo = $this->_oDb->getLotInfoById($aResult['lot']);
$aItem = $this->_oTemplate->getLotsPreview($this->_iProfileId, [$aLotInfo]);
if (!empty($aItem)) {
$aItem = current($aItem);
$sImageUrl = bx_api_get_relative_url($aItem['bx_if:user']['content']['icon']);
$aResult['convo'] = [
'author_data' => (int) $aItem[$CNF['FIELD_AUTHOR']] ? BxDolProfile::getInstance()->getData($aItem[$CNF['FIELD_AUTHOR']]) : [
'id' => 0,
'display_type' => 'unit',
'display_name' => $aItem['bx_if:user']['content']['talk_type'],
'url' => $sImageUrl,
'url_avatar' => $sImageUrl,
'module' => isset($aItem['author_module']) ? $aItem['author_module'] : 'bx_pages',
],
'title' => $aItem[$CNF['FIELD_TITLE']],
'message' => $aItem['bx_if:user']['content']['message'],
'date' => $aItem['bx_if:timer']['content']['time'],
'id' => $aLotInfo[$CNF['FIELD_HASH']],
'total_messages' => $this->_oDb->getJotsNumber($aItem[$CNF['FIELD_ID']], 0)
];
$aResult['lot'] = $aLotInfo[$CNF['FIELD_HASH']];
}
}
return $aResult;
}
/*
* Moved to Services
*/
public function serviceSearchUsers($sParams)
{
$aOptions = json_decode($sParams, true);
$aResult = ['code' => 1];
$aUsers = [];
if (!$sParams || !isset($aOptions['term']))
return $aResult;
$aFoundProfile = $this->searchProfiles($aOptions['term'], isset($aOptions['except']) ? $aOptions['except'] : []);
if (!empty($aFoundProfile)) {
foreach ($aFoundProfile as &$aProfile) {
$oModule = BxDolModule::getInstance($aProfile['module']);
$oPCNF = &$oModule->_oConfig->CNF;
$aData = $oModule->_oDb->getContentInfoById($aProfile['id']);
$oProfile = BxDolProfile::getInstanceByContentAndType($aProfile['id'], $aProfile['module']);
$aUsers[] = array_merge($aProfile, [
'id' => $oProfile->id(),
'image' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_PICTURE']]),
'cover' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_COVER']])
]);
}
}
return $aUsers;
}
/*
* Moved to Services.
*/
public function serviceFindConvo($sParams)
{
$aOptions = json_decode($sParams, true);
$aResult = ['code' => 1];
if (!isset($aOptions['param']))
return $aResult;
if ($aOptions['param']) {
$oProfile = null;
$aData = BxDolPageQuery::getSeoLink($this->_oConfig->getName(), 'messenger', ['param_name' => 'profile_id', 'uri' => $aOptions['param']]);
if (!empty($aData) && isset($aData['param_value']))
$oProfile = BxDolProfile::getInstance($aData['param_value']);
if (!$oProfile) {
$aPersonsData = BxDolPageQuery::getSeoLink('bx_persons', 'view-persons-profile', ['param_name' => 'id', 'uri' => $aOptions['param']]);
if (!empty($aPersonsData) && isset($aPersonsData['param_value']))
$oProfile = BxDolProfile::getInstanceByContentAndType($aPersonsData['param_value'], 'bx_persons');
}
if (!$oProfile) {
$aOrgData = BxDolPageQuery::getSeoLink('bx_organizations', 'view-organization-profile', ['param_name' => 'id', 'uri' => $aOptions['param']]);
if (!empty($aOrgData) && isset($aOrgData['param_value']))
$oProfile = BxDolProfile::getInstanceByContentAndType($aOrgData['param_value'], 'bx_organizations');
}
if ($oProfile) {
if ($aExistedTalk = $this->_oDb->getLotsByParticipantsList([$oProfile->id(), $this->_iProfileId], BX_IM_TYPE_PRIVATE))
return ['convo' => $aExistedTalk];
return ['profile' => BxDolProfile::getInstance()->getData($oProfile->id())];
}
}
return [];
}
/*
* Should be moved to Services if it's needed, otherwise removed.
*/
public function serviceGetConvoItem($sParams)
{
$aOptions = json_decode($sParams, true);
$aResult = array('code' => 1);
if (!isset($aOptions['id']) || !($aLotInfo = $this->_oDb->getLotInfoById($aOptions['id'])))
return $aResult;
$iLotId = $aOptions['id'];
if (!$this->_oDb->isParticipant($iLotId, $this->_iProfileId))
return ['code' => 1, _t('_bx_messenger_not_participant')];
$CNF = $this->_oConfig->CNF;
return array_merge($aLotInfo, ['author_data' => BxDolProfile::getInstance()->getData($aLotInfo[$CNF['FIELD_AUTHOR']])]);
}
/*
* Moved to Services
*/
function serviceRemoveJot($sParams = '')
{
$aOptions = json_decode($sParams, true);
$iJotId = isset($aOptions['jot_id']) ? (int) $aOptions['jot_id'] : 0;
$iLotId = isset($aOptions['lot_id']) ? $aOptions['lot_id'] : 0;
if (!$iJotId)
return [];
$this->pusherData('convo_' . $iLotId, ['convo' => $iLotId, 'action' => 'deleted', 'data' => $iJotId]);
return $this->serviceDeleteJot($iJotId, true);
}
/*
* Moved to Services
*/
function serviceGetConvosList($sParams = '')
{
$aOptions = json_decode($sParams, true);
$aData = $this->serviceGetTalksList($aOptions);
$aList = $aData['list'];
if (isset($aData['code']) && !(int) $aData['code'] && !empty($aData['list']))
$aList = $this->_oTemplate->getLotsPreview($this->_iProfileId, $aData['list']);
$CNF = &$this->_oConfig->CNF;
$aResult = [];
if (!empty($aList)) {
foreach ($aList as $iKey => $aItem) {
$sImageUrl = bx_api_get_relative_url($aItem['bx_if:user']['content']['icon']);
$aResult[] = [
'author_data' => (int) $aItem[$CNF['FIELD_AUTHOR']] ? BxDolProfile::getInstance()->getData($aItem[$CNF['FIELD_AUTHOR']]) : [
'id' => 0,
'display_type' => 'unit',
'display_name' => $aItem['bx_if:user']['content']['talk_type'],
'url' => $sImageUrl,
'url_avatar' => $sImageUrl,
'module' => isset($aItem['author_module']) ? $aItem['author_module'] : 'bx_pages',
],
'title' => $aItem[$CNF['FIELD_TITLE']],
'message' => $aItem['bx_if:user']['content']['message'],
'date' => $aItem['bx_if:timer']['content']['time'],
'id' => $aData['list'][$iKey][$CNF['FIELD_HASH']],
'id2' => $aItem[$CNF['FIELD_ID']],
'unread' => $aItem['count'],
'total_messages' => $this->_oDb->getJotsNumber($aItem[$CNF['FIELD_ID']], 0)
];
}
}
return $aResult;
}
public function checkAllowedDeleteAnyEntryForProfile($isPerformAction = false, $iProfileId = false)
{
return true;
}
public function checkAllowedEditAnyEntryForProfile($isPerformAction = false, $iProfileId = false)
{
return true;
}
/**
* Moved to Services
*/
public function unitAPI($iProfileId, $aParams = [])
{
$CNF = &$this->_oConfig->CNF;
$oProfile = BxDolProfile::getInstance($iProfileId);
if (!$oProfile)
return '';
$sModule = $oProfile->getModule();
$iContentId = $oProfile->getContentId();
$oModule = BxDolModule::getInstance($sModule);
$aData = $oModule->_oDb->getContentInfoById($iContentId);
$oPCNF = &$oModule->_oConfig->CNF;
// get profile's url
$sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $oPCNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId));
$aResult = [
'id' => $iContentId,
'module' => $sModule,
'added' => $aData[$oPCNF['FIELD_ADDED']],
'title' => $aData[$oPCNF['FIELD_TITLE']],
'url' => bx_api_get_relative_url($sUrl),
'image' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_PICTURE']]),
'cover' => bx_api_get_image($oPCNF['OBJECT_STORAGE'], $aData[$oPCNF['FIELD_COVER']])
];
$oPrivacy = BxDolPrivacy::getObjectInstance($oPCNF['OBJECT_PRIVACY_VIEW']);
$bPrivacy = $oPrivacy !== false;
$sKey = 'OBJECT_MENU_SNIPPET_META';
if (!empty($CNF[$sKey]) && ($oMetaMenu = BxDolMenu::getObjectInstance($CNF[$sKey], $oModule->_oTemplate)) !== false) {
$bPublic = !$bPrivacy || $oPrivacy->check($iContentId) || $oPrivacy->isPartiallyVisible($aData[$CNF['FIELD_ALLOW_VIEW_TO']]);
$oMetaMenu->setContentModule($sModule);
$oMetaMenu->setContentId($iContentId);
$oMetaMenu->setContentPublic($bPublic);
$aResult['meta'] = $oMetaMenu->getCodeAPI();
}
return $aResult;
}
function serviceGetBlockContactsMessenger($mixedParams = [])
{
if (bx_is_api())
return bx_srv($this->getName(), 'get_block_contacts', [$mixedParams], 'Services');
return $this->_oTemplate->getContacts($this->_iProfileId, $mixedParams);
}
public function serviceGetBroadcastFields($aInputsAdd = array())
{
$aFields = $this->_oTemplate->_getBroadcastFields();
$aResult = [];
foreach ($aFields as $sName => $aField) {
if (isset($aField['skip']) || !isset($aField['caption']))
continue;
$aResult[$sName] = $aField['caption'];
}
return $aResult;
}
private function queueBroadcastDelivery($iConvoId, $iJotId, $iAuthorId, $aNotificationsType = [], $iAfterUserId = 0)
{
if (!$iConvoId || !$iJotId || !$iAuthorId)
return false;
$sStamp = str_replace('.', '', sprintf('%.6F', microtime(true)));
$sJobName = sprintf('%s_broadcast_%d_%d_%s', $this->_oConfig->getName(), $iConvoId, $iAfterUserId, $sStamp);
return BxDolBackgroundJobs::getInstance()->add($sJobName, [
$this->_oConfig->getName(),
'process_broadcast_queue',
[$iConvoId, $iJotId, $iAuthorId, (array) $aNotificationsType, $iAfterUserId],
'Module'
], 5);
}
public function serviceProcessBroadcastQueue($iConvoId, $iJotId, $iAuthorId, $aNotificationsType = [], $iAfterUserId = 0)
{
if (!$iConvoId || !$iJotId || !$iAuthorId)
return false;
$CNF = &$this->_oConfig->CNF;
$aConvoInfo = $this->_oDb->getLotInfoById($iConvoId);
$aJotInfo = $this->_oDb->getJotById($iJotId);
if (empty($aConvoInfo) || empty($aJotInfo) || (int) $aConvoInfo[$CNF['FIELD_TYPE']] !== BX_IM_TYPE_BROADCAST)
return false;
$sModule = $this->_oConfig->getObject('alert');
for ($iBatch = 0; $iBatch < self::BROADCAST_BATCHES_PER_RUN; $iBatch++) {
$aParticipants = $this->_oDb->getBroadcastParticipants($iConvoId, self::BROADCAST_BATCH_SIZE + 1, $iAfterUserId);
if (empty($aParticipants))
return true;
$bHasMore = count($aParticipants) > self::BROADCAST_BATCH_SIZE;
if ($bHasMore)
$aParticipants = array_slice($aParticipants, 0, self::BROADCAST_BATCH_SIZE);
$this->_oDb->markAsNewJot($aParticipants, $iConvoId, $iJotId, false);
foreach ($aParticipants as $iPart) {
bx_alert($sModule, 'got_broadcast_ntfs', $iConvoId, $iAuthorId, [
'object_author_id' => $iPart,
'recipient_id' => $iPart,
'subobject_id' => $iJotId,
'source' => $sModule . '_' . $iJotId,
'source_mac' => $sModule . '_' . $iAuthorId . '_' . $iJotId,
] + (array) $aNotificationsType);
}
$iAfterUserId = (int) end($aParticipants);
if (!$bHasMore)
return true;
}
return $this->queueBroadcastDelivery($iConvoId, $iJotId, $iAuthorId, $aNotificationsType, $iAfterUserId);
}
private function getProfilesByCriteria($aData, $aOptions = [])
{
$bCountOnly = !empty($aOptions['count_only']);
$aExcludeProfiles = $aOptions['exclude_profiles'] ?? [];
$aIncludeProfiles = $aOptions['include_profiles'] ?? [];
if (isset($aData['connection_type'])) {
if ($oConnection = $this->_oConfig->getConnectionByType($aData['connection_type'])) {
if ($bCountOnly && empty($aExcludeProfiles) && empty($aIncludeProfiles))
return (int)$oConnection->getConnectedContentCount($this->_iProfileId, $aData['connection_type'] === 'friends');
$aProfiles = $oConnection->getConnectedContent($this->_iProfileId, $aData['connection_type'] === 'friends');
if (!empty($aExcludeProfiles))
$aProfiles = array_values(array_diff($aProfiles, $aExcludeProfiles));
if (!empty($aIncludeProfiles))
$aProfiles = array_unique(array_merge($aProfiles, $aIncludeProfiles));
return $bCountOnly ? count($aProfiles) : $aProfiles;
}
}
$CNF = &$this->_oConfig->CNF;
if (!($oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_FILTER'], $CNF['OBJECT_FORM_FILTER_DISPLAY'], $this->_oTemplate)))
return false;
$oForm->filteredForm();
$aFields = [];
foreach ($oForm->aInputs as &$aInput) {
if (!isset($aData[$aInput['name']]) || isset($aInput['skip']) || $aInput['name'] === 'convo_type')
continue;
if (!empty($aInput['values']))
$aFields[$aInput['name']] = array_filter($aData[$aInput['name']], function ($sVal) use ($aInput) {
return in_array($sVal, array_keys($aInput['values']));
});
else
$aFields[$aInput['name']] = $aData[$aInput['name']];
}
$aParams = [];
if (!empty($aExcludeProfiles))
$aParams['exclude_profiles'] = $aExcludeProfiles;
if (!empty($aIncludeProfiles))
$aParams['include_profiles'] = $aIncludeProfiles;
$aProfiles = $this->_oDb->getProfilesByCriteria($aFields, $aParams);
bx_alert($this->_oConfig->getObject('alert'), 'broadcast_profiles_list', 0, 0, [
'profiles' => &$aProfiles
]);
if ($bCountOnly)
return count($aProfiles);
return $aProfiles;
}
function actionCalculateProfiles()
{
$aData = bx_get('data');
$aManuall = array_values(array_unique(array_filter(array_map('intval', (array)bx_get('manually')), function ($iProfileId) {
return $iProfileId > 0;
})));
$iConvoType = isset($aData['convo_type']) ? $aData['convo_type'] : 0;
if (!$this->isLogged() || (empty($aData) && empty($aManuall)) || !$iConvoType)
return false;
$aParams['count_only'] = true;
if (!empty($aManuall))
$aParams['include_profiles'] = $aManuall;
$iTotal = (int)$this->getProfilesByCriteria($aData, $aParams);
echoJson(['msg' => _t('_bx_messenger_broadcast_total_profiles', $iTotal)]);
}
function serviceGetBroadcastCard($iJotId)
{
if (!$iJotId)
return '';
$aLotInfo = $this->_oDb->getLotByJotId($iJotId, false);
$aJotInfo = $this->_oDb->getJotById($iJotId);
return $this->_oTemplate->parseHtmlByName('broadcast-message.html', ['title' => $aLotInfo['title'], 'content' => $aJotInfo['message']]);
}
function actionGetFilterCriteria()
{
if (!$this->isLogged() && $this->_oConfig->isAllowedAction(BX_MSG_ACTION_CREATE_BROADCASTS) !== true)
return echoJson(['code' => 1]);
$CNF = &$this->_oConfig->CNF;
$sType = bx_get('type');
$sHtml = '';
switch ($sType) {
case BX_MSG_TALK_TYPE_BROADCAST:
$oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_FILTER'], $CNF['OBJECT_FORM_FILTER_DISPLAY'], $this->_oTemplate);
if ($oForm->filteredForm())
$sHtml = $oForm->getCode();
else
$sHtml = MsgBox(_t('_bx_messenger_no_criteria_available'));
break;
case 'followers':
case 'friends':
$sHtml = $this->_oTemplate->getConnectionsForm($sType, $this->_iProfileId);
break;
/*default:
$aForm = [
'form_attrs' => [
'method' => 'post',
'id' => $CNF['OBJECT_FORM_ENTRY'],
'class' => 'space-y-4 max-h-60 overflow-y-auto'
],
'inputs' => $this->_oTemplate->getNotificationFormData()
];
$oForm = new BxTemplFormView($aForm);
$sHtml = $oForm -> getCode();*/
}
echoJson(['code' => 0, 'html' => $sHtml]);
}
public function actionAjaxGetRecipients()
{
$sTerm = bx_get('term');
$a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
header('Content-Type:text/javascript; charset=utf-8');
echo (json_encode($a));
}
function actionGetTalkInfo()
{
$iLotId = (int) bx_get('lot_id');
if (!$iLotId)
return echoJson(array('code' => 1, 'html' => MsgBox(_t('_bx_messenger_not_found'))));
if (!$this->isAvailable($iLotId))
return echoJson(array('code' => 1, 'message' => '_bx_messenger_not_participant'));
$sContent = $this->_oTemplate->getInfoBlockContent($iLotId);
return echoJson(array('code' => 0, 'html' => $sContent));
}
public function broadcastNewJot($iLotId, $iJotId)
{
$iLotId = (int)$iLotId;
$iJotId = (int)$iJotId;
if (!$iLotId || !$iJotId)
return false;
$CNF = &$this->_oConfig->CNF;
$aLotInfo = $this->_oDb->getLotInfoById($iLotId);
if (empty($aLotInfo) || empty($aLotInfo[$CNF['FIELD_HASH']]))
return false;
$sHash = mb_strtolower($aLotInfo[$CNF['FIELD_HASH']]);
// NEO JotItem expects Services API shape (menu.items)
bx_import('Services', $this->_aModule);
$oServices = new BxMessengerServices();
$this->pusherData('convo_' . $sHash, [
'convo' => $iLotId,
'action' => 'added',
'data' => $oServices->serviceGetConvoMessages([
'load' => 'ids',
'lot' => $sHash,
'jot' => $iJotId,
]),
]);
$aParticipantsList = $this->_oDb->getParticipantsList($iLotId, true);
foreach ($aParticipantsList as $iProfile) {
$this->pusherData('profile_' . $iProfile, ['convo' => $iLotId]);
}
return true;
}
/**
* Moved to Services
*/
private function pusherData($sAction, $aData = [])
{
$oSockets = BxDolSockets::getInstance();
if (isset($aData['convo']) && (int) $aData['convo']) {
$CNF = &$this->_oConfig->CNF;
$aLotInfo = $this->_oDb->getLotInfoById($aData['convo']);
$aData['id'] = mb_strtolower($aLotInfo[$CNF['FIELD_HASH']]);
}
$aData['user_id'] = $this->_iProfileId;
if ($oSockets->isEnabled() && $sAction && !empty($aData)) {
$oSockets->sendEvent('bx', 'messenger', $sAction, $aData);
}
}
}
/** @} */