122 lines
4.5 KiB
PHP
122 lines
4.5 KiB
PHP
<?php
|
|
/**
|
|
* app/plugins/fileshare/controllers/fileshare.php
|
|
*
|
|
* This is the home controller for the fileshare plugin.
|
|
*
|
|
* @package TP FileShare
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Controllers;
|
|
|
|
use TheTempusProject\Houdini\Classes\Views;
|
|
use TheTempusProject\Houdini\Classes\Issues;
|
|
use TheTempusProject\Bedrock\Functions\Input;
|
|
use TheTempusProject\Bedrock\Functions\Upload;
|
|
use TheTempusProject\Classes\Controller;
|
|
use TheTempusProject\Models\Upload as UploadModel;
|
|
use TheTempusProject\TheTempusProject as App;
|
|
use TheTempusProject\Classes\Forms;
|
|
use TheTempusProject\Bedrock\Functions\Session;
|
|
use TheTempusProject\Hermes\Functions\Redirect;
|
|
|
|
class Fileshare extends Controller {
|
|
protected static $uploads;
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
self::$uploads = new UploadModel;
|
|
self::$title = 'FileShare - {SITENAME}';
|
|
self::$pageDescription = 'Your saved Files for the site.';
|
|
}
|
|
|
|
public function index() {
|
|
$uploads = self::$uploads->getByUser();
|
|
Views::view( 'fileshare.list', $uploads );
|
|
}
|
|
|
|
public function delete( $id = null ) {
|
|
$upload = self::$uploads->findById( $id );
|
|
if ( $upload == false ) {
|
|
Issues::add( 'error', 'Upload not found.' );
|
|
return $this->index();
|
|
}
|
|
if ( $upload->createdBy != App::$activeUser->ID ) {
|
|
Issues::add( 'error', 'You do not have permission to modify this upload.' );
|
|
return $this->index();
|
|
}
|
|
$result = self::$uploads->delete( $id );
|
|
if ( $result == true ) {
|
|
Issues::add( 'success', 'Upload deleted.' );
|
|
} else {
|
|
Issues::add( 'notice', 'There was an problem deleting your upload.' );
|
|
}
|
|
return $this->index();
|
|
}
|
|
|
|
public function edit( $id = null ) {
|
|
$upload = self::$uploads->findById( $id );
|
|
if ( $upload == false ) {
|
|
Issues::add( 'error', 'Upload not found.' );
|
|
return $this->index();
|
|
}
|
|
|
|
if ( ! Input::exists( 'submit' ) ) {
|
|
return Views::view( 'calendar.calendar.edit', $upload );
|
|
}
|
|
|
|
if ( ! Forms::check( 'editUpload' ) ) {
|
|
Issues::add( 'error', [ 'There was an error editing your upload.' => Check::userErrors() ] );
|
|
return Views::view( 'fileshare.edit', $upload );
|
|
}
|
|
|
|
if ( Input::exists( 'file' ) ) {
|
|
$folder = UPLOAD_DIRECTORY . App::$activeUser->username . DIRECTORY_SEPARATOR;
|
|
if ( ! Upload::image( 'file', $folder ) ) {
|
|
Issues::add( 'error', [ 'There was an error with your upload.' => Check::systemErrors() ] );
|
|
return Views::view( 'fileshare.edit', $upload );
|
|
} else {
|
|
$route = str_replace( APP_ROOT_DIRECTORY, '', $folder );
|
|
$location = $route . Upload::last();
|
|
}
|
|
} else {
|
|
$location = '';
|
|
}
|
|
|
|
$result = self::$uploads->update( $id, Input::post('name'), $location );
|
|
if ( ! $result ) {
|
|
Issues::add( 'error', [ 'There was an error updating your upload.' => Check::userErrors() ] );
|
|
return Views::view( 'fileshare.edit', $upload );
|
|
}
|
|
Session::flash( 'success', 'Your Upload has been updated.' );
|
|
Redirect::to( 'fileshare/index/');
|
|
}
|
|
|
|
public function upload( $id = null ) {
|
|
if ( ! Input::exists() ) {
|
|
return Views::view( 'fileshare.create' );
|
|
}
|
|
if ( ! Forms::check( 'newUpload' ) ) {
|
|
Issues::add( 'error', [ 'There was an error creating your upload.' => Check::userErrors() ] );
|
|
return Views::view( 'fileshare.create' );
|
|
}
|
|
$folder = UPLOAD_DIRECTORY . App::$activeUser->username . DIRECTORY_SEPARATOR;
|
|
if ( ! Upload::image( 'file', $folder ) ) {
|
|
Issues::add( 'error', [ 'There was an error with your upload.' => Check::systemErrors() ] );
|
|
return Views::view( 'fileshare.create' );
|
|
} else {
|
|
$route = str_replace( APP_ROOT_DIRECTORY, '', $folder );
|
|
$location = $route . Upload::last();
|
|
}
|
|
$upload = self::$uploads->create( Input::post('name'), $location );
|
|
if ( ! $upload ) {
|
|
return Views::view( 'fileshare.create' );
|
|
}
|
|
Session::flash( 'success', 'Your Upload has been saved.' );
|
|
Redirect::to( 'fileshare/index/');
|
|
}
|
|
}
|