* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Controllers; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Functions\Input; use TheTempusProject\Hermes\Functions\Redirect; use TheTempusProject\Bedrock\Functions\Session; use TheTempusProject\Houdini\Classes\Issues; use TheTempusProject\Houdini\Classes\Views; use TheTempusProject\Classes\Controller; use TheTempusProject\Classes\Forms; use TheTempusProject\Models\Suggestions as SuggestionModel; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Models\Comments as CommentsModel; use TheTempusProject\Plugins\Comments; use TheTempusProject\Houdini\Classes\Components; class Suggestions extends Controller { protected static $suggestions; protected static $comments; public function __construct() { parent::__construct(); if ( !App::$isLoggedIn ) { Session::flash( 'notice', 'You must be logged in to view suggestions.' ); return Redirect::home(); } self::$suggestions = new SuggestionModel; self::$title = 'Suggestions - {SITENAME}'; self::$pageDescription = 'On this page you can view, submit, or comment-on suggestions for the site.'; } public function index() { Views::view( 'suggestions.list', self::$suggestions->recent() ); } public function view( $id = null ) { $data = self::$suggestions->findById( $id ); if ( $data == false ) { Issues::add( 'error', 'Suggestion not found.' ); return $this->index(); } if ( empty( self::$comments ) ) { self::$comments = new CommentsModel; } if ( Input::exists( 'contentId' ) ) { $this->comments( 'post', Input::post( 'contentId' ) ); } Components::set( 'CONTENT_ID', $id ); Components::set( 'COMMENT_TYPE', 'suggestions' ); Components::set( 'NEWCOMMENT', Views::simpleView( 'comments.create' ) ); Components::set( 'count', self::$comments->count( 'suggestion', $id ) ); Components::set( 'COMMENTS', Views::simpleView( 'comments.list', self::$comments->display( 10, self::$suggestions->tableName, $id ) ) ); Views::view( 'suggestions.view', $data ); } public function create() { if ( !Input::exists() ) { return Views::view( 'suggestions.create' ); } if ( !Forms::check( 'newSuggestion' ) ) { Issues::add( 'error', [ 'There was an error with your suggestion.' => Check::userErrors() ] ); return Views::view( 'suggestions.create' ); } if ( !self::$suggestions->create( Input::post( 'title' ), Input::post( 'suggestion' ) ) ) { Issues::add( 'error', [ 'There was an error with your suggestion.' => Check::userErrors() ] ); return Views::view( 'suggestions.create' ); } Session::flash( 'success', 'Your Suggestion has been received. We must verify all suggestions before they are published, but as long as it doesn\'t violate our code of conduct, it should be available to view and comment on publicly within 24 hours.' ); Redirect::to( 'suggestions/index' ); } public function edit( $data = null ) { if ( !Input::exists( 'submit' ) ) { return Views::view( 'suggestions.edit', self::$suggestions->findById( $data ) ); } if ( !Forms::check( 'editSuggestion' ) ) { Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] ); return Views::view( 'suggestions.edit', self::$suggestions->findById( $data ) ); } if ( self::$suggestions->update( $data, Input::post( 'title' ), Input::post( 'suggestion' ) ) ) { Issues::add( 'success', 'Suggestion updated' ); } else { return Views::view( 'suggestions.edit', self::$suggestions->findById( $data ) ); } $this->index(); } public function comments( $sub = null, $data = null ) { if ( empty( self::$comments ) ) { self::$comments = new CommentsModel; } $commentsPlugin = new Comments; if ( empty( $sub ) || empty( $data ) ) { Session::flash( 'error', 'Whoops, try again.' ); Redirect::to( 'suggestions' ); } switch ( $sub ) { case 'post': $content = self::$suggestions->findById( $data ); if ( empty( $content ) ) { Session::flash( 'error', 'Unknown Content.' ); Redirect::to( 'suggestions' ); } return $commentsPlugin->formPost( self::$suggestions->tableName, $content, 'suggestions/view/' ); case 'edit': $content = self::$comments->findById( $data ); if ( empty( $content ) ) { Session::flash( 'error', 'Unknown Comment.' ); Redirect::to( 'suggestions' ); } return $commentsPlugin->formEdit( self::$suggestions->tableName, $content, 'suggestions/view/' ); case 'delete': $content = self::$comments->findById( $data ); if ( empty( $content ) ) { Session::flash( 'error', 'Unknown Comment.' ); Redirect::to( 'suggestions' ); } return $commentsPlugin->formDelete( self::$suggestions->tableName, $content, 'suggestions/view/' ); } } }