2018-10-16 16:28:42 +00:00
< ? php
defined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Posts extends MY_Controller
{
public function __construct ()
{
parent :: __construct ( 'profile' , 'language_names' , 'country_names' );
$this -> load -> model ( 'UserModel' , '' , TRUE );
$this -> load -> model ( 'PostsModel' , '' , TRUE );
}
public function index ()
{
if ( isset ( $_SESSION [ 'user' ]) && ! empty ( $_SESSION [ 'user' ]))
redirect ( base_url ( 'posts/feed' ));
redirect ( base_url ( 'posts/popular' ));
}
public function feed ()
{
if ( ! isset ( $_SESSION [ 'user' ]) || empty ( $_SESSION [ 'user' ]))
redirect ( base_url ( 'posts' ));
$this -> load -> view ( 'header' , [ 'active' => 'feed' , 'title' => 'Dein Feed' , 'additionalStyles' => [ 'posts_list.css' ]]);
$this -> load -> view ( 'network/posts/posts_list' , [ 'active' , 'feed' ]);
$this -> load -> view ( 'footer' , [ 'additionalScripts' => [ 'post_feed.js' , 'post_search.js' ]]);
}
public function getFeedPosts ()
{
if ( ! isset ( $_SESSION [ 'user' ]) || empty ( $_SESSION [ 'user' ]))
redirect ( base_url ( 'posts' ));
$amount = ( int ) $this -> input -> get ( 'amount' );
$offset = ( int ) $this -> input -> get ( 'offset' ) * $amount ;
$posts = $this -> PostsModel -> getFeedPosts ( $_SESSION [ 'user' ][ 'ID' ], $amount , $offset );
foreach ( $posts as $post ) {
$this -> load -> view ( 'network/posts/post_item' , $post );
}
}
public function popular ()
{
$this -> load -> view ( 'header' , [ 'active' => 'popular' , 'title' => 'Beliebte Posts' , 'additionalStyles' => [ 'posts_list.css' ]]);
$this -> load -> view ( 'network/posts/posts_list' , [ 'active' => 'popular' ]);
$this -> load -> view ( 'footer' , [ 'additionalScripts' => [ 'post_feed.js' , 'post_search.js' ]]);
}
public function getPopularPosts ()
{
$amount = ( int ) $this -> input -> get ( 'amount' );
$offset = ( int ) $this -> input -> get ( 'offset' ) * $amount ;
$posts = $this -> PostsModel -> getPopularPosts ( $amount , $offset );
foreach ( $posts as $post ) {
$this -> load -> view ( 'network/posts/post_item' , $post );
}
}
public function addPostLike ()
{
header ( 'Content-Type: application/json' );
if ( ! isset ( $_SESSION [ 'user' ]) || empty ( $_SESSION [ 'user' ])) {
echo json_encode ([
'success' => false ,
'message' => lang ( 'post_like_account_missing' )
]);
exit ;
}
$postUUID = $this -> input -> post ( 'postUUID' );
$isLiked = $this -> PostsModel -> addPostLikeByUUID ( $postUUID , $_SESSION [ 'user' ][ 'ID' ]);
$likeCount = $this -> PostsModel -> getPostLikeCountByUUID ( $postUUID );
echo json_encode ([
'success' => true ,
'message' => 'Du hast den Post erfolgreich bewertet.' ,
'isLiked' => $isLiked ,
'likeCount' => $likeCount
]);
}
public function search ()
{
$type = $this -> input -> get ( 'type' );
$query = $this -> input -> get ( 'q' );
$rank = $this -> input -> get ( 'rank' );
$this -> load -> view ( 'header' , [ 'active' => 'search' , 'title' => 'Suche' , 'additionalStyles' => [ 'posts_list.css' ]]);
$this -> load -> view ( 'network/posts/posts_list' , [ 'active' => 'search' , 'search' => [ 'query' => $query , 'type' => $type , 'rank' => $rank ]]);
$this -> load -> view ( 'footer' , [ 'additionalScripts' => [ 'post_search.js' ]]);
}
public function getSearchPosts ()
{
$type = $this -> input -> get ( 'type' );
$query = $this -> input -> get ( 'query' );
$rank = $type == 'type-users' ? $this -> input -> get ( 'rank' ) : '' ;
$lang = $type == 'type-users' ? $this -> input -> get ( 'lang' ) : '' ;
$country = $type == 'type-users' ? $this -> input -> get ( 'country' ) : '' ;
$amount = ( int ) $this -> input -> get ( 'amount' );
$offset = ( int ) $this -> input -> get ( 'offset' ) * $amount ;
if ( $type !== 'type-users' && $rank !== '' && $lang !== '' && strlen ( $query ) < 4 ) {
exit ;
}
if (( $type == 'type-all' && $offset == 0 ) || $type == 'type-users' ) {
$userAmount = $type == 'type-users' ? $amount : 3 ;
$userOffset = $type == 'type-users' ? $offset : 0 ;
$users = $this -> UserModel -> searchUsers ( $query , $rank , $country , $lang , $userAmount , $userOffset );
if ( ! empty ( $users )) {
if ( $offset == 0 ) {
echo '<h2>Nutzer (' . sizeof ( $users ) . ')</h2>' ;
}
echo '<div class="row">' ;
foreach ( $users as $user ) {
$user [ 'about' ] = strlen ( $user [ 'about' ]) > 60 ? substr ( $user [ 'about' ], 0 , 60 ) . '...' : $user [ 'about' ];
echo $this -> load -> view ( 'network/user/user_overview_card' , $user , true );
}
echo '</div>' ;
}
}
if ( $type == 'type-all' || $type == 'type-posts' ) {
$posts = $this -> PostsModel -> searchPosts ( $query , $amount , $offset );
if ( ! empty ( $posts ) && $offset == 0 ) {
echo '<h2>Posts</h2>' ;
}
foreach ( $posts as $post ) {
$this -> load -> view ( 'network/posts/post_item' , $post );
}
}
}
public function getAvailableCountries () {
$countries = $this -> UserModel -> getAvailableCountries ();
foreach ( $countries as $i => $country ) {
$countries [ $i ][ 'name' ] = lang ( 'country_' . $country [ 'country' ]);
}
header ( 'Content-Type: application/json' );
echo json_encode ([ 'countries' => $countries ]);
}
public function getAvailableLanguages () {
$languages = $this -> UserModel -> getAvailableLanguages ();
foreach ( $languages as $i => $language ) {
$languages [ $i ][ 'name' ] = lang ( 'lang_' . strtolower ( $language [ 'language' ]));
}
header ( 'Content-Type: application/json' );
echo json_encode ([ 'languages' => $languages ]);
}
public function getReportModal () {
$this -> load -> view ( 'network/posts/report_modal' );
}
public function reportPost () {
header ( 'Content-Type: application/json' );
$uuid = $this -> input -> post ( 'uuid' );
$reason = $this -> input -> post ( 'reason' );
$reasonText = $this -> input -> post ( 'explanation' );
if ( $reason == '' ) {
echo json_encode ([ 'success' => false , 'message' => 'Bitte wähle einen Grund für deine Meldung aus.' ]);
exit ;
}
$allowedReasons = [ 'hatespeech' , 'racism' , 'terrorism' , 'abuse' , 'violence' , 'copyright' , 'spam' , 'technical-issue' ];
if ( ! array_search ( $reason , $allowedReasons )) {
echo json_encode ([ 'success' => false , 'message' => 'Bitte wähle einen standardmäßig vorhandenen und validen Grund für die Meldung aus.' ]);
exit ;
}
if ( ! $this -> PostsModel -> isUUIDValid ( $uuid )) {
echo json_encode ([ 'success' => true , 'message' => 'Der ausgewählte Post ist nicht (mehr) vorhanden. Sollte es sich hierbei um ein Irrtum handeln, verfasse bitte über den Button unten rechts ein Feedback.' ]);
exit ;
}
$this -> PostsModel -> reportPost ( $uuid , $reason , $reasonText );
echo json_encode ([ 'success' => true , 'message' => 'Vielen Dank für das Melden dieses Posts. Wir werden schnellstmöglich angemessene Aktionen unternehmen.' ]);
}
public function getDeleteModal () {
header ( 'Content-Type: application/json' );
if ( ! isset ( $_SESSION [ 'user' ])) {
echo json_encode ([ 'success' => false , 'message' => 'Du musst eingeloggt sein, um die Posts deines Accounts zu löschen' ]);
exit ;
}
$uuid = $this -> input -> post ( 'uuid' );
$post = $this -> PostsModel -> getPostByUUID ( $uuid );
if ( empty ( $post )) {
echo json_encode ([ 'success' => false , 'message' => 'Der angegebene Post existiert nicht.' ]);
exit ;
}
2018-10-17 11:56:22 +00:00
if ( $post [ 0 ][ 'user_id' ] != $_SESSION [ 'user' ][ 'ID' ]) {
echo json_encode ([ 'success' => false , 'message' => 'Du kannst keine Posts löschen, die dir nicht gehören.' ]);
exit ;
}
$post = $this -> PostsModel -> preparePostList ( $post );
$body = $this -> load -> view ( 'network/posts/delete_modal' , [ 'post' => $post [ 0 ]], true );
echo json_encode ([ 'success' => true , 'title' => 'Post löschen' , 'body' => $body ]);
}
public function deletePost () {
header ( 'Content-Type: application/json' );
if ( ! isset ( $_SESSION [ 'user' ])) {
echo json_encode ([ 'success' => false , 'message' => 'Du musst eingeloggt sein, um die Posts deines Accounts zu löschen' ]);
exit ;
}
$uuid = $this -> input -> post ( 'uuid' );
$post = $this -> PostsModel -> getPostByUUID ( $uuid );
if ( empty ( $post )) {
echo json_encode ([ 'success' => false , 'message' => 'Der angegebene Post existiert nicht.' ]);
exit ;
}
if ( $post [ 0 ][ 'user_id' ] != $_SESSION [ 'user' ][ 'ID' ]) {
echo json_encode ([ 'success' => false , 'message' => 'Du kannst keine Posts löschen, die dir nicht gehören.' ]);
exit ;
}
$this -> PostsModel -> deletePost ( $_SESSION [ 'user' ][ 'ID' ], $uuid );
2018-10-16 16:28:42 +00:00
2018-10-17 11:56:22 +00:00
echo json_encode ([ 'success' => true , 'message' => 'Der Post wurde erfolgreich gelöscht.' ]);
2018-10-16 16:28:42 +00:00
}
}