2020-06-07 19:04:43 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\Web\Presenters ;
2023-05-14 23:49:33 +03:00
use openvk\Web\Models\Entities\ { Club , Photo , Post };
2022-12-12 02:23:42 +03:00
use Nette\InvalidStateException ;
2020-06-07 19:04:43 +03:00
use openvk\Web\Models\Entities\Notifications\ClubModeratorNotification ;
2021-12-15 01:27:17 +03:00
use openvk\Web\Models\Repositories\ { Clubs , Users , Albums , Managers , Topics };
2021-12-16 21:40:34 +03:00
use Chandler\Security\Authenticator ;
2020-06-07 19:04:43 +03:00
final class GroupPresenter extends OpenVKPresenter
{
private $clubs ;
2022-09-17 00:19:46 +03:00
protected $presenterName = " group " ;
2020-06-07 19:04:43 +03:00
function __construct ( Clubs $clubs )
{
$this -> clubs = $clubs ;
parent :: __construct ();
}
function renderView ( int $id ) : void
{
$club = $this -> clubs -> get ( $id );
if ( ! $club ) {
$this -> notFound ();
} else {
$this -> template -> albums = ( new Albums ) -> getClubAlbums ( $club , 1 , 3 );
$this -> template -> albumsCount = ( new Albums ) -> getClubAlbumsCount ( $club );
2021-12-15 01:27:17 +03:00
$this -> template -> topics = ( new Topics ) -> getLastTopics ( $club , 3 );
$this -> template -> topicsCount = ( new Topics ) -> getClubTopicsCount ( $club );
2022-07-11 19:07:49 +03:00
$this -> template -> club = $club ;
2020-06-07 19:04:43 +03:00
}
}
function renderCreate () : void
{
$this -> assertUserLoggedIn ();
2021-01-01 00:18:53 +03:00
$this -> willExecuteWriteAction ();
2020-06-07 19:04:43 +03:00
if ( $_SERVER [ " REQUEST_METHOD " ] === " POST " ) {
2023-08-09 15:50:04 +03:00
if ( ! empty ( $this -> postParam ( " name " )) && mb_strlen ( trim ( $this -> postParam ( " name " ))) > 0 )
2020-06-07 19:04:43 +03:00
{
$club = new Club ;
$club -> setName ( $this -> postParam ( " name " ));
$club -> setAbout ( empty ( $this -> postParam ( " about " )) ? NULL : $this -> postParam ( " about " ));
$club -> setOwner ( $this -> user -> id );
try {
$club -> save ();
} catch ( \PDOException $ex ) {
if ( $ex -> getCode () == 23000 )
$this -> flashFail ( " err " , " Ошибка " , " Произошла ошибка на стороне сервера. Обратитесь к системному администратору. " );
else
throw $ex ;
}
$club -> toggleSubscription ( $this -> user -> identity );
2022-08-09 08:52:36 +03:00
$this -> redirect ( " /club " . $club -> getId ());
2020-06-07 19:04:43 +03:00
} else {
$this -> flashFail ( " err " , " Ошибка " , " Вы не ввели название группы. " );
}
}
}
function renderSub () : void
{
$this -> assertUserLoggedIn ();
2021-01-01 00:18:53 +03:00
$this -> willExecuteWriteAction ();
2020-06-07 19:04:43 +03:00
if ( $_SERVER [ " REQUEST_METHOD " ] !== " POST " ) exit ( " Invalid state " );
$club = $this -> clubs -> get (( int ) $this -> postParam ( " id " ));
if ( ! $club ) exit ( " Invalid state " );
$club -> toggleSubscription ( $this -> user -> identity );
2022-08-09 08:52:36 +03:00
$this -> redirect ( $club -> getURL ());
2020-06-07 19:04:43 +03:00
}
function renderFollowers ( int $id ) : void
{
$this -> assertUserLoggedIn ();
2021-11-12 16:31:23 +03:00
$this -> template -> club = $this -> clubs -> get ( $id );
$this -> template -> onlyShowManagers = $this -> queryParam ( " onlyAdmins " ) == " 1 " ;
if ( $this -> template -> onlyShowManagers ) {
2022-05-08 13:06:26 +03:00
$this -> template -> followers = NULL ;
2021-11-12 16:31:23 +03:00
$this -> template -> managers = $this -> template -> club -> getManagers (( int ) ( $this -> queryParam ( " p " ) ? ? 1 ), ! $this -> template -> club -> canBeModifiedBy ( $this -> user -> identity ));
if ( $this -> template -> club -> canBeModifiedBy ( $this -> user -> identity ) || ! $this -> template -> club -> isOwnerHidden ()) {
$this -> template -> managers = array_merge ([ $this -> template -> club -> getOwner ()], iterator_to_array ( $this -> template -> managers ));
}
$this -> template -> count = $this -> template -> club -> getManagersCount ();
2021-11-14 00:39:00 +03:00
} else {
$this -> template -> followers = $this -> template -> club -> getFollowers (( int ) ( $this -> queryParam ( " p " ) ? ? 1 ));
2022-05-08 13:06:26 +03:00
$this -> template -> managers = NULL ;
2021-11-14 00:39:00 +03:00
$this -> template -> count = $this -> template -> club -> getFollowersCount ();
2021-11-12 16:31:23 +03:00
}
2021-11-14 00:39:00 +03:00
$this -> template -> paginatorConf = ( object ) [
" count " => $this -> template -> count ,
" page " => $this -> queryParam ( " p " ) ? ? 1 ,
" amount " => NULL ,
" perPage " => OPENVK_DEFAULT_PER_PAGE ,
];
2020-06-07 19:04:43 +03:00
}
function renderModifyAdmin ( int $id ) : void
{
$user = is_null ( $this -> queryParam ( " user " )) ? $this -> postParam ( " user " ) : $this -> queryParam ( " user " );
$comment = $this -> postParam ( " comment " );
2021-11-06 14:06:57 +03:00
$removeComment = $this -> postParam ( " removeComment " ) === " 1 " ;
2022-05-08 13:06:26 +03:00
$hidden = [ " 0 " => false , " 1 " => true ][ $this -> queryParam ( " hidden " )] ? ? NULL ;
2020-06-07 19:04:43 +03:00
//$index = $this->queryParam("index");
if ( ! $user )
$this -> badRequest ();
$club = $this -> clubs -> get ( $id );
$user = ( new Users ) -> get (( int ) $user );
if ( ! $user || ! $club )
$this -> notFound ();
2021-11-12 16:31:23 +03:00
if ( ! $club -> canBeModifiedBy ( $this -> user -> identity ? ? NULL ))
2020-06-07 19:04:43 +03:00
$this -> flashFail ( " err " , " Ошибка доступа " , " У вас недостаточно прав, чтобы изменять этот р е с у р с ." );
2021-11-12 16:31:23 +03:00
if ( ! is_null ( $hidden )) {
if ( $club -> getOwner () -> getId () == $user -> getId ()) {
$club -> setOwner_Hidden ( $hidden );
$club -> save ();
} else {
$manager = ( new Managers ) -> getByUserAndClub ( $user -> getId (), $club -> getId ());
$manager -> setHidden ( $hidden );
$manager -> save ();
}
if ( $club -> getManagersCount ( true ) == 0 ) {
$club -> setAdministrators_List_Display ( 2 );
$club -> save ();
}
if ( $hidden ) {
$this -> flashFail ( " succ " , " Операция успешна " , " Теперь " . $user -> getCanonicalName () . " будет показываться как обычный подписчик всем кроме других администраторов " );
} else {
$this -> flashFail ( " succ " , " Операция успешна " , " Теперь все будут знать про то что " . $user -> getCanonicalName () . " - администратор " );
}
} elseif ( $removeComment ) {
2021-11-06 14:06:57 +03:00
if ( $club -> getOwner () -> getId () == $user -> getId ()) {
$club -> setOwner_Comment ( null );
$club -> save ();
} else {
$manager = ( new Managers ) -> getByUserAndClub ( $user -> getId (), $club -> getId ());
$manager -> setComment ( null );
$manager -> save ();
}
$this -> flashFail ( " succ " , " Операция успешна " , " Комментарий к администратору удален " );
} elseif ( $comment ) {
2021-11-24 23:47:32 +03:00
if ( mb_strlen ( $comment ) > 36 ) {
$commentLength = ( string ) mb_strlen ( $comment );
2021-11-06 14:06:57 +03:00
$this -> flashFail ( " err " , " Ошибка " , " Комментарий слишком длинный ( $commentLength символов вместо 36 символов) " );
}
if ( $club -> getOwner () -> getId () == $user -> getId ()) {
$club -> setOwner_Comment ( $comment );
$club -> save ();
} else {
$manager = ( new Managers ) -> getByUserAndClub ( $user -> getId (), $club -> getId ());
$manager -> setComment ( $comment );
$manager -> save ();
}
2020-06-07 19:04:43 +03:00
$this -> flashFail ( " succ " , " Операция успешна " , " Комментарий к администратору изменён " );
} else {
if ( $club -> canBeModifiedBy ( $user )) {
$club -> removeManager ( $user );
$this -> flashFail ( " succ " , " Операция успешна " , $user -> getCanonicalName () . " более не администратор. " );
} else {
$club -> addManager ( $user );
( new ClubModeratorNotification ( $user , $club , $this -> user -> identity )) -> emit ();
$this -> flashFail ( " succ " , " Операция успешна " , $user -> getCanonicalName () . " назначен(а ) администратором. " );
}
}
}
function renderEdit ( int $id ) : void
{
$this -> assertUserLoggedIn ();
2021-01-01 00:18:53 +03:00
$this -> willExecuteWriteAction ();
2020-06-07 19:04:43 +03:00
$club = $this -> clubs -> get ( $id );
2022-12-12 02:23:42 +03:00
if ( ! $club || ! $club -> canBeModifiedBy ( $this -> user -> identity ))
2020-06-07 19:04:43 +03:00
$this -> notFound ();
else
$this -> template -> club = $club ;
if ( $_SERVER [ " REQUEST_METHOD " ] === " POST " ) {
2022-08-03 21:01:36 +03:00
if ( ! $club -> setShortcode ( empty ( $this -> postParam ( " shortcode " )) ? NULL : $this -> postParam ( " shortcode " ) ))
$this -> flashFail ( " err " , tr ( " error " ), tr ( " error_shorturl_incorrect " ));
2023-08-09 15:50:04 +03:00
$club -> setName (( empty ( $this -> postParam ( " name " )) || mb_strlen ( trim ( $this -> postParam ( " name " ))) === 0 ) ? $club -> getName () : $this -> postParam ( " name " ));
2020-06-07 19:04:43 +03:00
$club -> setAbout ( empty ( $this -> postParam ( " about " )) ? NULL : $this -> postParam ( " about " ));
2022-08-03 21:01:36 +03:00
$club -> setWall ( empty ( $this -> postParam ( " wall " )) ? 0 : 1 );
2021-11-05 12:44:50 +03:00
$club -> setAdministrators_List_Display ( empty ( $this -> postParam ( " administrators_list_display " )) ? 0 : $this -> postParam ( " administrators_list_display " ));
2022-01-25 18:25:49 +03:00
$club -> setEveryone_Can_Create_Topics ( empty ( $this -> postParam ( " everyone_can_create_topics " )) ? 0 : 1 );
2022-01-02 01:52:35 +03:00
$club -> setDisplay_Topics_Above_Wall ( empty ( $this -> postParam ( " display_topics_above_wall " )) ? 0 : 1 );
$club -> setHide_From_Global_Feed ( empty ( $this -> postParam ( " hide_from_global_feed " )) ? 0 : 1 );
2020-06-07 19:04:43 +03:00
2021-11-12 19:56:41 +03:00
$website = $this -> postParam ( " website " ) ? ? " " ;
if ( empty ( $website ))
2021-11-14 00:43:51 +03:00
$club -> setWebsite ( NULL );
2021-11-12 19:56:41 +03:00
else
2021-11-14 00:43:51 +03:00
$club -> setWebsite (( ! parse_url ( $website , PHP_URL_SCHEME ) ? " https:// " : " " ) . $website );
2021-11-12 19:56:41 +03:00
2020-06-07 19:04:43 +03:00
if ( $_FILES [ " ava " ][ " error " ] === UPLOAD_ERR_OK ) {
$photo = new Photo ;
try {
2021-12-26 17:16:37 +03:00
$anon = OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " wall " ][ " anonymousPosting " ][ " enable " ];
if ( $anon && $this -> user -> id === $club -> getOwner () -> getId ())
$anon = $club -> isOwnerHidden ();
else if ( $anon )
$anon = $club -> getManager ( $this -> user -> identity ) -> isHidden ();
2020-06-07 19:04:43 +03:00
$photo -> setOwner ( $this -> user -> id );
$photo -> setDescription ( " Profile image " );
$photo -> setFile ( $_FILES [ " ava " ]);
$photo -> setCreated ( time ());
2021-12-26 17:16:37 +03:00
$photo -> setAnonymous ( $anon );
2020-06-07 19:04:43 +03:00
$photo -> save ();
( new Albums ) -> getClubAvatarAlbum ( $club ) -> addPhoto ( $photo );
} catch ( ISE $ex ) {
$name = $album -> getName ();
$this -> flashFail ( " err " , " Неизвестная ошибка " , " Н е удалось сохранить фотографию." );
}
}
try {
$club -> save ();
} catch ( \PDOException $ex ) {
if ( $ex -> getCode () == 23000 )
$this -> flashFail ( " err " , " Ошибка " , " Произошла ошибка на стороне сервера. Обратитесь к системному администратору. " );
else
throw $ex ;
}
$this -> flash ( " succ " , " Изменения сохранены " , " Новые данные появятся в вашей группе. " );
}
}
2023-05-14 23:49:33 +03:00
function renderSetAvatar ( int $id )
{
$photo = new Photo ;
$club = $this -> clubs -> get ( $id );
if ( $_SERVER [ " REQUEST_METHOD " ] === " POST " && $_FILES [ " ava " ][ " error " ] === UPLOAD_ERR_OK ) {
try {
$anon = OPENVK_ROOT_CONF [ " openvk " ][ " preferences " ][ " wall " ][ " anonymousPosting " ][ " enable " ];
if ( $anon && $this -> user -> id === $club -> getOwner () -> getId ())
$anon = $club -> isOwnerHidden ();
else if ( $anon )
$anon = $club -> getManager ( $this -> user -> identity ) -> isHidden ();
$photo -> setOwner ( $this -> user -> id );
$photo -> setDescription ( " Club image " );
$photo -> setFile ( $_FILES [ " ava " ]);
$photo -> setCreated ( time ());
$photo -> setAnonymous ( $anon );
$photo -> save ();
( new Albums ) -> getClubAvatarAlbum ( $club ) -> addPhoto ( $photo );
$flags = 0 ;
$flags |= 0 b00010000 ;
$flags |= 0 b10000000 ;
$post = new Post ;
$post -> setOwner ( $this -> user -> id );
$post -> setWall ( $club -> getId () *- 1 );
$post -> setCreated ( time ());
$post -> setContent ( " " );
$post -> setFlags ( $flags );
$post -> save ();
$post -> attach ( $photo );
} catch ( ISE $ex ) {
$name = $album -> getName ();
$this -> flashFail ( " err " , " Неизвестная ошибка " , " Н е удалось сохранить фотографию." );
}
}
$this -> returnJson ([
" url " => $photo -> getURL (),
" id " => $photo -> getPrettyId ()
]);
}
2022-12-12 02:23:42 +03:00
function renderEditBackdrop ( int $id ) : void
{
$this -> assertUserLoggedIn ();
$this -> willExecuteWriteAction ();
$club = $this -> clubs -> get ( $id );
if ( ! $club || ! $club -> canBeModifiedBy ( $this -> user -> identity ))
$this -> notFound ();
else
$this -> template -> club = $club ;
if ( $_SERVER [ " REQUEST_METHOD " ] !== " POST " )
return ;
if ( $this -> postParam ( " subact " ) === " remove " ) {
$club -> unsetBackDropPictures ();
$club -> save ();
$this -> flashFail ( " succ " , tr ( " backdrop_succ_rem " ), tr ( " backdrop_succ_desc " )); # will exit
}
$pic1 = $pic2 = NULL ;
try {
if ( $_FILES [ " backdrop1 " ][ " error " ] !== UPLOAD_ERR_NO_FILE )
$pic1 = Photo :: fastMake ( $this -> user -> id , " Profile backdrop (system) " , $_FILES [ " backdrop1 " ]);
if ( $_FILES [ " backdrop2 " ][ " error " ] !== UPLOAD_ERR_NO_FILE )
$pic2 = Photo :: fastMake ( $this -> user -> id , " Profile backdrop (system) " , $_FILES [ " backdrop2 " ]);
} catch ( InvalidStateException $e ) {
$this -> flashFail ( " err " , tr ( " backdrop_error_title " ), tr ( " backdrop_error_no_media " ));
}
if ( $pic1 == $pic2 && is_null ( $pic1 ))
$this -> flashFail ( " err " , tr ( " backdrop_error_title " ), tr ( " backdrop_error_no_media " ));
$club -> setBackDropPictures ( $pic1 , $pic2 );
$club -> save ();
$this -> flashFail ( " succ " , tr ( " backdrop_succ " ), tr ( " backdrop_succ_desc " ));
}
2020-06-07 19:04:43 +03:00
function renderStatistics ( int $id ) : void
{
$this -> assertUserLoggedIn ();
if ( ! eventdb ())
$this -> flashFail ( " err " , " Ошибка подключения " , " Н е удалось подключится к службе телеметрии." );
$club = $this -> clubs -> get ( $id );
if ( ! $club -> canBeModifiedBy ( $this -> user -> identity ))
$this -> notFound ();
else
$this -> template -> club = $club ;
$this -> template -> reach = $club -> getPostViewStats ( true );
$this -> template -> views = $club -> getPostViewStats ( false );
}
function renderAdmin ( int $clb , int $id ) : void
{
$this -> assertUserLoggedIn ();
$manager = ( new Managers ) -> get ( $id );
if ( $manager -> getClub () -> canBeModifiedBy ( $this -> user -> identity )){
$this -> template -> manager = $manager ;
$this -> template -> club = $manager -> getClub ();
} else {
$this -> notFound ();
}
}
2021-12-16 21:40:34 +03:00
function renderChangeOwner ( int $id , int $newOwnerId ) : void
{
$this -> assertUserLoggedIn ();
$this -> willExecuteWriteAction ();
if ( $_SERVER [ 'REQUEST_METHOD' ] !== " POST " )
$this -> redirect ( " /groups " . $this -> user -> id );
if ( ! Authenticator :: verifyHash ( $this -> postParam ( " password " ), $this -> user -> identity -> getChandlerUser () -> getRaw () -> passwordHash ))
$this -> flashFail ( " err " , tr ( " error " ), tr ( " incorrect_password " ));
$club = $this -> clubs -> get ( $id );
$newOwner = ( new Users ) -> get ( $newOwnerId );
if ( $this -> user -> id !== $club -> getOwner () -> getId ())
$this -> flashFail ( " err " , tr ( " error " ), tr ( " forbidden " ));
$club -> setOwner ( $newOwnerId );
2021-12-16 22:23:28 +03:00
$club -> addManager ( $this -> user -> identity );
$oldOwnerManager = $club -> getManager ( $this -> user -> identity );
$oldOwnerManager -> setHidden ( $club -> isOwnerHidden ());
$oldOwnerManager -> setComment ( $club -> getOwnerComment ());
$oldOwnerManager -> save ();
$newOwnerManager = $club -> getManager ( $newOwner );
$club -> setOwner_Hidden ( $newOwnerManager -> isHidden ());
$club -> setOwner_Comment ( $newOwnerManager -> getComment ());
$club -> removeManager ( $newOwner );
2021-12-16 21:40:34 +03:00
$club -> save ();
$this -> flashFail ( " succ " , tr ( " information_-1 " ), tr ( " group_owner_setted " , $newOwner -> getCanonicalName (), $club -> getName ()));
}
2020-06-07 19:04:43 +03:00
}