1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-08 01:14:24 +03:00
ifhub.club/application/classes/actions/ActionStream.class.php

332 lines
9.5 KiB
PHP
Raw Normal View History

2011-06-06 14:58:19 +03:00
<?php
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
2011-06-06 14:58:19 +03:00
/**
* Экшен обработки ленты активности
*
* @package actions
* @since 1.0
*/
class ActionStream extends Action {
/**
* Текущий пользователь
*
* @var ModuleUser_EntityUser|null
*/
protected $oUserCurrent;
2014-07-18 18:45:16 +03:00
/**
* Какое меню активно
*
* @var string
*/
protected $sMenuItemSelect='user';
2011-06-06 14:58:19 +03:00
/**
* Инициализация
*/
public function Init() {
$this->oUserCurrent = $this->User_getUserCurrent();
2014-07-18 18:45:16 +03:00
// Личная лента доступна только для авторизованных, гостям показываем общую ленту
if ($this->oUserCurrent) {
2014-07-18 18:45:16 +03:00
$this->SetDefaultEvent('personal');
} else {
$this->SetDefaultEvent('all');
}
2011-06-06 14:58:19 +03:00
$this->Viewer_Assign('sMenuHeadItemSelect', 'stream');
2014-07-18 18:45:16 +03:00
/**
* Загружаем в шаблон JS текстовки
*/
$this->Lang_AddLangJs(array(
2014-07-18 18:45:16 +03:00
'activity.notices.error_already_subscribed', 'error'
));
}
2014-07-18 18:45:16 +03:00
/**
* Регистрация евентов
*/
protected function RegisterEvent() {
2014-07-18 18:45:16 +03:00
$this->AddEvent('personal', 'EventPersonal');
$this->AddEvent('all', 'EventAll');
2014-07-18 18:45:16 +03:00
2014-02-07 12:26:28 +02:00
$this->AddEvent('subscribe', 'EventSubscribe'); // TODO: возможно нужно удалить
$this->AddEvent('ajaxadduser', 'EventAjaxAddUser');
$this->AddEvent('ajaxremoveuser', 'EventAjaxRemoveUser');
$this->AddEvent('switchEventType', 'EventSwitchEventType');
2014-07-18 18:45:16 +03:00
$this->AddEvent('get_more_all', 'EventGetMoreAll');
2014-07-18 18:45:16 +03:00
$this->AddEvent('get_more_personal', 'EventGetMore');
$this->AddEvent('get_more_user', 'EventGetMoreUser');
}
2011-06-06 14:58:19 +03:00
/**
2014-07-18 18:45:16 +03:00
* Персональная активность
*/
2014-07-18 18:45:16 +03:00
protected function EventPersonal() {
if ( ! $this->oUserCurrent ) {
return parent::EventNotFound();
}
2014-07-18 18:45:16 +03:00
$this->Viewer_AddBlock('right', 'activitySettings');
$this->Viewer_AddBlock('right', 'activityUsers');
$this->Viewer_Assign('activityEvents', $this->Stream_Read());
$this->Viewer_Assign('activityEventsAllCount', $this->Stream_GetCountByReaderId( $this->oUserCurrent->getId()) );
}
2014-07-18 18:45:16 +03:00
/**
2014-07-18 18:45:16 +03:00
* Общая активность
*/
protected function EventAll() {
2014-07-18 18:45:16 +03:00
$this->sMenuItemSelect = 'all';
$this->Viewer_Assign('activityEvents', $this->Stream_ReadAll());
$this->Viewer_Assign('activityEventsAllCount', $this->Stream_GetCountAll());
}
2014-07-18 18:45:16 +03:00
/**
* Активаци/деактивация типа события
*/
protected function EventSwitchEventType() {
$this->Viewer_SetResponseAjax('json');
2014-07-18 18:45:16 +03:00
if ( ! $this->oUserCurrent ) {
return parent::EventNotFound();
}
2014-07-18 18:45:16 +03:00
if ( ! getRequest('type') ) {
$this->Message_AddError($this->Lang_Get('system_error'), $this->Lang_Get('error'));
}
2014-07-18 18:45:16 +03:00
/**
* Активируем/деактивируем тип
*/
$this->Stream_switchUserEventType($this->oUserCurrent->getId(), getRequestStr('type'));
2014-07-18 18:45:16 +03:00
$this->Message_AddNotice($this->Lang_Get('common.success.save'), $this->Lang_Get('attention'));
}
2014-07-18 18:45:16 +03:00
/**
2014-07-18 18:45:16 +03:00
* Подгрузка событий (замена постраничности)
*/
protected function EventGetMore() {
2014-07-18 18:45:16 +03:00
if ( ! $this->oUserCurrent ) {
return parent::EventNotFound();
}
2014-07-18 18:45:16 +03:00
$this->GetMore(function($lastId) {
return $this->Stream_Read(null, $lastId);
});
}
2014-07-18 18:45:16 +03:00
/**
2014-07-18 18:45:16 +03:00
* Подгрузка событий для всего сайта
*/
protected function EventGetMoreAll() {
2014-07-18 18:45:16 +03:00
$this->GetMore(function($lastId) {
return $this->Stream_ReadAll(null, $lastId);
});
}
2014-07-18 18:45:16 +03:00
/**
* Подгрузка событий для пользователя
*/
protected function EventGetMoreUser() {
$this->GetMore(function($lastId) {
if ( ! ( $oUser = $this->User_GetUserById(getRequestStr('target_id')) ) ) {
return false;
}
2014-07-18 18:45:16 +03:00
return $this->Stream_ReadByUserId($oUser->getId(), null, $lastId);
});
}
2014-07-18 18:45:16 +03:00
/**
2014-07-18 18:45:16 +03:00
* Общий метод подгрузки событий
*
2014-07-18 18:45:16 +03:00
* @param callback $getEvents Метод возвращающий список событий
*/
2014-07-18 18:45:16 +03:00
protected function GetMore( $getEvents ) {
$this->Viewer_SetResponseAjax('json');
2014-07-18 18:45:16 +03:00
// Необходимо передать последний просмотренный ID событий
$iLastId = getRequestStr('last_id');
if ( ! $iLastId ) {
$this->Message_AddError($this->Lang_Get('system_error'), $this->Lang_Get('error'));
return;
}
2014-07-18 18:45:16 +03:00
// Получаем события
$aEvents = $getEvents( $iLastId );
if ( $aEvents === false ) {
return $this->EventErrorDebug();
}
2014-07-18 18:45:16 +03:00
$oViewer = $this->Viewer_GetLocalViewer();
$oViewer->Assign('events', $aEvents, true);
$oViewer->Assign('dateLast', getRequestStr('date_last'), true);
2014-07-18 18:45:16 +03:00
if ( count($aEvents) ) {
$this->Viewer_AssignAjax('last_id', end($aEvents)->getId(), true);
}
2014-07-18 18:45:16 +03:00
$this->Viewer_AssignAjax('count_loaded', count($aEvents));
$this->Viewer_AssignAjax('html', $oViewer->Fetch('components/activity/event-list.tpl'));
}
2014-07-18 18:45:16 +03:00
/**
* Подписка на пользователя по ID
*
*/
protected function EventSubscribe() {
/**
* Устанавливаем формат Ajax ответа
*/
$this->Viewer_SetResponseAjax('json');
/**
* Пользователь авторизован?
*/
if (!$this->oUserCurrent) {
return parent::EventNotFound();
}
/**
* Проверяем существование пользователя
*/
if (!$this->User_getUserById(getRequestStr('id'))) {
$this->Message_AddError($this->Lang_Get('system_error'),$this->Lang_Get('error'));
}
if ($this->oUserCurrent->getId() == getRequestStr('id')) {
2014-04-08 14:44:28 +03:00
$this->Message_AddError($this->Lang_Get('user_list_add.notices.error_self'),$this->Lang_Get('error'));
return;
}
/**
* Подписываем на пользователя
*/
$this->Stream_subscribeUser($this->oUserCurrent->getId(), getRequestStr('id'));
$this->Message_AddNotice($this->Lang_Get('stream_subscribes_updated'), $this->Lang_Get('attention'));
}
2014-07-18 18:45:16 +03:00
/**
* Подписка на пользователя по логину
*/
2014-02-07 12:26:28 +02:00
protected function EventAjaxAddUser() {
/**
* Устанавливаем формат Ajax ответа
*/
$this->Viewer_SetResponseAjax('json');
$aUsers=getRequest('aUserList',null,'post');
2014-07-18 18:45:16 +03:00
/**
* Валидация
*/
if ( ! is_array($aUsers) ) {
return $this->EventErrorDebug();
}
2014-07-18 18:45:16 +03:00
/**
2014-02-07 12:26:28 +02:00
* Если пользователь не авторизирован, возвращаем ошибку
*/
2014-02-07 12:26:28 +02:00
if (!$this->User_IsAuthorization()) {
$this->Message_AddErrorSingle($this->Lang_Get('need_authorization'),$this->Lang_Get('error'));
return;
}
2014-02-07 12:26:28 +02:00
$aResult=array();
/**
* Обрабатываем добавление по каждому из переданных логинов
*/
foreach ($aUsers as $sUser) {
$sUser=trim($sUser);
if ($sUser=='') {
continue;
}
/**
* Если пользователь не найден или неактивен, возвращаем ошибку
*/
if ($oUser=$this->User_GetUserByLogin($sUser) and $oUser->getActivate()==1) {
$this->Stream_subscribeUser($this->oUserCurrent->getId(),$oUser->getId());
$oViewer = $this->Viewer_GetLocalViewer();
$oViewer->Assign('oUser', $oUser);
$oViewer->Assign('bUserListSmallShowActions', true);
$aResult[]=array(
'bStateError'=>false,
'sMsgTitle'=>$this->Lang_Get('attention'),
'sMsg'=>$this->Lang_Get('common.success.add',array('login'=>htmlspecialchars($sUser))),
'sUserId'=>$oUser->getId(),
'sUserLogin'=>htmlspecialchars($sUser),
'sUserWebPath'=>$oUser->getUserWebPath(),
'sUserAvatar48'=>$oUser->getProfileAvatarPath(48),
2014-04-28 13:32:22 +03:00
'sHtml'=>$oViewer->Fetch("components/user_list_small/user_list_small_item.tpl")
2014-02-07 12:26:28 +02:00
);
} else {
$aResult[]=array(
'bStateError'=>true,
'sMsgTitle'=>$this->Lang_Get('error'),
'sMsg'=>$this->Lang_Get('user.notices.not_found',array('login'=>htmlspecialchars($sUser))),
2014-02-07 12:26:28 +02:00
'sUserLogin'=>htmlspecialchars($sUser)
);
}
}
/**
2014-02-07 12:26:28 +02:00
* Передаем во вьевер массив с результатами обработки по каждому пользователю
*/
$this->Viewer_AssignAjax('aUserList',$aResult);
}
/**
* Отписка от пользователя
*/
2014-02-07 12:26:28 +02:00
protected function EventAjaxRemoveUser() {
/**
* Устанавливаем формат Ajax ответа
*/
$this->Viewer_SetResponseAjax('json');
/**
* Пользователь авторизован?
*/
if (!$this->oUserCurrent) {
2014-02-07 12:26:28 +02:00
return $this->EventErrorDebug();
}
/**
* Пользователь с таким ID существует?
*/
2014-02-07 12:26:28 +02:00
if (!$this->User_GetUserById(getRequestStr('iUserId'))) {
return $this->EventErrorDebug();
}
/**
* Отписываем
*/
2014-02-07 12:46:33 +02:00
$this->Stream_unsubscribeUser($this->oUserCurrent->getId(), getRequestStr('iUserId'));
$this->Message_AddNotice($this->Lang_Get('stream_subscribes_updated'), $this->Lang_Get('attention'));
}
2014-07-18 18:45:16 +03:00
/**
* Выполняется при завершении работы экшена
*/
public function EventShutdown() {
/**
* Загружаем в шаблон необходимые переменные
*/
$this->Viewer_Assign('sMenuItemSelect',$this->sMenuItemSelect);
}
}