Initial commit
This commit is contained in:
436
app/plugins/notes/controllers/notes.php
Normal file
436
app/plugins/notes/controllers/notes.php
Normal file
@ -0,0 +1,436 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/notes/controllers/notes.php
|
||||
*
|
||||
* This is the notes controller.
|
||||
*
|
||||
* @package TP Notes
|
||||
* @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\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
use TheTempusProject\Houdini\Classes\Forms as FormBuilder;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Classes\Config;
|
||||
|
||||
|
||||
|
||||
use TheTempusProject\Classes\Controller;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
use TheTempusProject\Bedrock\Functions\Session;
|
||||
use TheTempusProject\Hermes\Functions\Redirect;
|
||||
use TheTempusProject\Models\Notes as Note;
|
||||
use TheTempusProject\Models\Notebooks;
|
||||
use TheTempusProject\Houdini\Classes\Template;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
|
||||
class Notes extends Controller {
|
||||
protected static $notes;
|
||||
protected static $notebooks;
|
||||
protected static $defaultView;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
if ( !App::$isLoggedIn ) {
|
||||
Session::flash( 'notice', 'You must be logged in to use this feature.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
self::$notes = new Note;
|
||||
self::$notebooks = new Notebooks;
|
||||
self::$title = 'Notes - {SITENAME}';
|
||||
self::$pageDescription = 'On this page you can create and manage tasks and lists.';
|
||||
self::$defaultView = 'byNotebook';
|
||||
Components::set( 'notebookID', '' );
|
||||
Components::set( 'notebookName', 'All' );
|
||||
Components::set( 'notebookName', 'default' );
|
||||
Template::setTemplate( 'notes' );
|
||||
}
|
||||
|
||||
public function index() {
|
||||
$this->setupMenus();
|
||||
|
||||
$notebooks = Views::simpleView( 'notes.notebooks.list', self::$notebooks->byUser() );
|
||||
Components::set( 'notebookList', $notebooks );
|
||||
|
||||
$notes = Views::simpleView( 'notes.notes.list', self::$notes->byUser( 20 ) );
|
||||
Components::set( 'noteList', $notes );
|
||||
|
||||
Views::view( 'notes.dashboard' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes
|
||||
*/
|
||||
public function notes( $id = null ) {
|
||||
self::$defaultView = __FUNCTION__;
|
||||
Components::set( 'currentView', self::$defaultView );
|
||||
|
||||
$this->setupMenus( 0, $id );
|
||||
|
||||
Navigation::setCrumbComponent( 'NotebookBreadCrumbs', 'notes/byNotebook' );
|
||||
|
||||
$notes = self::$notes->findByNotebook( $id );
|
||||
Views::view( 'notes.notes.list', $notes );
|
||||
}
|
||||
public function createNote( $id = null ) {
|
||||
$this->setupMenus( 0, $id );
|
||||
$select = FormBuilder::getSelectHtml(
|
||||
'notebookID',
|
||||
self::$notebooks->simpleList(true),
|
||||
$id,
|
||||
);
|
||||
Components::set( 'notebookSelect', $select );
|
||||
if ( !Input::exists() ) {
|
||||
return Views::view( 'notes.notes.create' );
|
||||
}
|
||||
if ( !Forms::check( 'createNote' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notes.create' );
|
||||
}
|
||||
if ( !self::$notes->create( Input::post( 'title' ), Input::post( 'note' ), Input::post( 'color' ), Input::post( 'notebookID' ) ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your note.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notes.create' );
|
||||
}
|
||||
Session::flash( 'success', 'Your note has been created.' );
|
||||
if ( Input::exists( 'notebookID' ) ) {
|
||||
Redirect::to( 'notes/byNotebook/' . Input::post( 'notebookID' ) );
|
||||
} else {
|
||||
Redirect::to( 'notes/index' );
|
||||
}
|
||||
}
|
||||
public function editNote( $id = null ) {
|
||||
$note = self::$notes->findById( $id );
|
||||
if ( $note == false ) {
|
||||
Issues::add( 'error', 'Note not found.' );
|
||||
return $this->index();
|
||||
}
|
||||
if ( $note->createdBy != App::$activeUser->ID ) {
|
||||
Issues::add( 'error', 'You do not have permission to modify this note.' );
|
||||
return $this->index();
|
||||
}
|
||||
|
||||
$this->setupMenus( $id );
|
||||
$select = FormBuilder::getSelectHtml(
|
||||
'notebookID',
|
||||
self::$notebooks->simpleList(true),
|
||||
$note->notebookID,
|
||||
);
|
||||
Components::set( 'notebookSelect', $select );
|
||||
Components::set( 'color', $note->color );
|
||||
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'notes.notes.edit', $note );
|
||||
}
|
||||
if ( !Forms::check( 'editNote' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notes.edit', $note );
|
||||
}
|
||||
if ( self::$notes->update( $id, Input::post( 'title' ), Input::post( 'note' ), Input::post( 'color' ), Input::post( 'notebookID' ) ) ) {
|
||||
Session::flash( 'success', 'Note updated' );
|
||||
} else {
|
||||
return Views::view( 'notes.notes.edit', self::$notes->findById( $data ) );
|
||||
}
|
||||
if ( !empty( $note->notebookID ) ) {
|
||||
Redirect::to( 'notes/byNotebook/' . $note->notebookID );
|
||||
} else {
|
||||
Redirect::to( 'notes/index' );
|
||||
}
|
||||
}
|
||||
public function deleteNote( $id = null ) {
|
||||
$note = self::$notes->findById( $id );
|
||||
if ( $note == false ) {
|
||||
Issues::add( 'error', 'Note not found.' );
|
||||
return $this->index();
|
||||
}
|
||||
if ( $note->createdBy != App::$activeUser->ID ) {
|
||||
Issues::add( 'error', 'You do not have permission to modify this note.' );
|
||||
return $this->index();
|
||||
}
|
||||
$result = self::$notes->delete( $id );
|
||||
if ( !$result ) {
|
||||
Session::flash( 'error', 'There was an error deleting the note(s)' );
|
||||
} else {
|
||||
Session::flash( 'success', 'Note deleted' );
|
||||
}
|
||||
if ( !empty( $note->notebookID ) ) {
|
||||
Redirect::to( 'notes/byNotebook/' . $note->notebookID );
|
||||
} else {
|
||||
Redirect::to( 'notes/index' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notebooks
|
||||
*/
|
||||
public function notebook( $id = null ) {
|
||||
self::$defaultView = __FUNCTION__;
|
||||
Components::set( 'currentView', self::$defaultView );
|
||||
$notebook = self::$notebooks->findById( $id );
|
||||
if ( $notebook == false ) {
|
||||
Issues::add( 'error', 'Notebook not found.' );
|
||||
return $this->index();
|
||||
}
|
||||
Components::set( 'notebookID', $id );
|
||||
|
||||
$notes = self::$notes->findByNotebook( $notebook->ID );
|
||||
Components::set( 'notebookName', $notebook->title );
|
||||
Components::set( 'notebookIcon', $notebook->icon );
|
||||
Components::set( 'notebookColor', $notebook->color );
|
||||
Views::view( 'notes.notes.list', $notes );
|
||||
}
|
||||
public function createNotebook($id = null) {
|
||||
$this->setupMenus( 0, $id );
|
||||
$select = FormBuilder::getSelectHtml(
|
||||
'notebookID',
|
||||
self::$notebooks->simpleList(true),
|
||||
$id,
|
||||
);
|
||||
Components::set( 'notebookSelect', $select );
|
||||
if ( !Input::exists() ) {
|
||||
return Views::view( 'notes.notebooks.create' );
|
||||
}
|
||||
if ( !Forms::check( 'createNotebook' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error creating your notebook.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notebooks.create' );
|
||||
}
|
||||
if ( ! input::exists('notebookID') ) {
|
||||
$notebookID = 0;
|
||||
} else {
|
||||
$notebookID = Input::post( 'notebookID' );
|
||||
}
|
||||
if ( !self::$notebooks->create( Input::post( 'title' ), Input::post( 'description' ), Input::post( 'color' ), $notebookID, Input::post( 'icon' ) ) ) {
|
||||
Issues::add( 'error', [ 'There was an error creating your notebook.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notebooks.create' );
|
||||
}
|
||||
Session::flash( 'success', 'Your notebook has been created.' );
|
||||
Redirect::to( 'notes/index' );
|
||||
}
|
||||
public function editNotebook( $id = null ) {
|
||||
$notebook = self::$notebooks->findById( $id );
|
||||
$this->setupMenus( 0, $id );
|
||||
if ( $notebook == false ) {
|
||||
Issues::add( 'error', 'Notebook not found.' );
|
||||
return Views::view( 'notes.notebooks.list', self::$notebooks->byUser() );
|
||||
|
||||
}
|
||||
$select = FormBuilder::getSelectHtml(
|
||||
'notebookID',
|
||||
self::$notebooks->simpleList(true),
|
||||
$notebook->notebookID,
|
||||
);
|
||||
Components::set( 'notebookSelect', $select );
|
||||
Components::set( 'color', $notebook->color );
|
||||
|
||||
if ( !Input::exists() ) {
|
||||
return Views::view( 'notes.notebooks.edit', $notebook );
|
||||
}
|
||||
if ( !Forms::check( 'editNotebook' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your notebook.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notebooks.edit', $notebook );
|
||||
}
|
||||
if ( ! Input::exists('notebookID') || $id == Input::post( 'notebookID' ) ) {
|
||||
$notebookID = 0;
|
||||
} else {
|
||||
$notebookID = Input::post( 'notebookID' );
|
||||
}
|
||||
|
||||
if ( !self::$notebooks->update( $id, Input::post( 'title' ), Input::post( 'description' ), Input::post( 'color' ), $notebookID, Input::post( 'icon' ) )) {
|
||||
Issues::add( 'error', [ 'There was an error with your notebook.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notebooks.edit', $notebook );
|
||||
}
|
||||
Session::flash( 'success', 'Your Notebook has been saved.' );
|
||||
Redirect::to( 'notes/byNotebook/' . $id );
|
||||
}
|
||||
public function deleteNotebook( $id = null ) {
|
||||
$notebook = self::$notebooks->findById( $id );
|
||||
if ( $notebook == false ) {
|
||||
Issues::add( 'error', 'Notebook not found.' );
|
||||
return $this->index();
|
||||
}
|
||||
if ( $notebook->createdBy != App::$activeUser->ID ) {
|
||||
Issues::add( 'error', 'You do not have permission to modify this calendar.' );
|
||||
return $this->index();
|
||||
}
|
||||
$result = self::$notebooks->delete( $id );
|
||||
if ( !$result ) {
|
||||
Issues::add( 'error', 'There was an error deleting the notebook(s)' );
|
||||
} else {
|
||||
Issues::add( 'success', 'Notebook deleted' );
|
||||
}
|
||||
return $this->index();
|
||||
}
|
||||
|
||||
/**
|
||||
* Views
|
||||
*/
|
||||
public function byNotebook( $id = null ) {
|
||||
self::$defaultView = __FUNCTION__;
|
||||
Components::set( 'currentView', self::$defaultView );
|
||||
|
||||
$this->setupMenus( 0, $id );
|
||||
|
||||
Navigation::setCrumbComponent( 'NotebookBreadCrumbs', 'notes/byNotebook' );
|
||||
|
||||
$notes = self::$notes->findByNotebook( $id );
|
||||
Views::view( 'notes.notes.list', $notes );
|
||||
}
|
||||
public function byNote( $id = null ) {
|
||||
self::$defaultView = __FUNCTION__;
|
||||
Components::set( 'currentView', self::$defaultView );
|
||||
$note = self::$notes->findById( $id );
|
||||
if ( $note == false ) {
|
||||
Issues::add( 'error', 'Note not found.' );
|
||||
return $this->index();
|
||||
}
|
||||
if ( $note->createdBy != App::$activeUser->ID ) {
|
||||
Issues::add( 'error', 'You do not have permission to modify this note.' );
|
||||
return $this->index();
|
||||
}
|
||||
|
||||
$this->setupMenus( $id );
|
||||
$select = FormBuilder::getSelectHtml(
|
||||
'notebookID',
|
||||
self::$notebooks->simpleList(true),
|
||||
$note->notebookID,
|
||||
);
|
||||
Components::set( 'notebookSelect', $select );
|
||||
Components::set( 'noteID', $note->ID );
|
||||
Components::set( 'color', $note->color );
|
||||
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'notes.notes.edit', $note );
|
||||
}
|
||||
if ( !Forms::check( 'editNoteSimple' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
|
||||
return Views::view( 'notes.notes.edit', $note );
|
||||
}
|
||||
if ( self::$notes->update( $id, Input::post( 'title' ), Input::post( 'note' ), Input::post( 'color' ), Input::post( 'notebookID' ) ) ) {
|
||||
Issues::add( 'success', 'Note updated' );
|
||||
} else {
|
||||
Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
|
||||
}
|
||||
|
||||
return Views::view( 'notes.notes.edit', self::$notes->findById( $id ) );
|
||||
}
|
||||
public function duplicateNote( $id = null ) {
|
||||
$note = self::$notes->findById( $id );
|
||||
if ( $note == false ) {
|
||||
Issues::add( 'error', 'Note not found.' );
|
||||
return $this->index();
|
||||
}
|
||||
if ( $note->createdBy != App::$activeUser->ID ) {
|
||||
Issues::add( 'error', 'You do not have permission to modify this note.' );
|
||||
return $this->index();
|
||||
}
|
||||
if ( ! self::$notes->duplicate( $id ) ) {
|
||||
Session::flash( 'error', [ 'There was an error duplicating your note.'] );
|
||||
} else {
|
||||
Session::flash( 'success', 'Your Note has been duplicated.' );
|
||||
}
|
||||
if ( !empty( $note->notebookID ) ) {
|
||||
Redirect::to( 'notes/byNotebook/' . $note->notebookID );
|
||||
} else {
|
||||
Redirect::to( 'notes/index' );
|
||||
}
|
||||
}
|
||||
public function autoUpdate( $id = null ) {
|
||||
Template::setTemplate( 'api' );
|
||||
$note = self::$notes->findById( $id );
|
||||
if ( $note == false ) {
|
||||
return Views::view( 'api.response', ['response' => json_encode( [ 'error' => 'Note not found.' ], true )]);
|
||||
}
|
||||
if ( $note->createdBy != App::$activeUser->ID ) {
|
||||
return Views::view( 'api.response', ['response' => json_encode( [ 'error' => 'Note not found.' ], true )]);
|
||||
}
|
||||
if ( !Forms::check( 'autoUpdateNote' ) ) {
|
||||
return Views::view( 'api.response', ['response' => json_encode( [ 'error' => 'Error with your form.' ], true )]);
|
||||
}
|
||||
if ( ! self::$notes->update( $id, Input::post( 'title' ), Input::post( 'note' ), Input::post( 'color' ), Input::post( 'notebookID' ) ) ) {
|
||||
return Views::view( 'api.response', ['response' => json_encode( [ 'error' => 'Error updating your note.' ], true )]);
|
||||
}
|
||||
return Views::view( 'api.response', ['response' => json_encode( [ 'data' => 'Note Updated.' ], true )]);
|
||||
}
|
||||
private function setupMenus( $note_id = 0, $notebook_id = 0 ) {
|
||||
if ( ! empty( $note_id ) ) {
|
||||
$note = self::$notes->findById( $note_id );
|
||||
}
|
||||
|
||||
if ( ! empty( $note ) ) {
|
||||
$notebook_id = $note->notebookID;
|
||||
}
|
||||
|
||||
$tree = self::$notebooks->getTree();
|
||||
$trunk = [];
|
||||
|
||||
// Get the menu
|
||||
foreach( $tree as $id => $notebook ) {
|
||||
if ( ! isset( $notebook['notebook'] ) ) {
|
||||
continue;
|
||||
}
|
||||
Components::set( 'notebookID', $notebook['notebook']->ID );
|
||||
Components::set( 'notebookName', $notebook['notebook']->title );
|
||||
Components::set( 'notebookIcon', $notebook['notebook']->icon );
|
||||
Components::set( 'notebookColor', $notebook['notebook']->color );
|
||||
if ( !empty( $notebook['notebooks'] ) ) {
|
||||
$menuItem = Views::simpleView( 'notes.nav.menuItemList', $notebook['notebooks'] );
|
||||
} else {
|
||||
$menuItem = Views::simpleView( 'notes.nav.menuItem', $notebook['notebook'] );
|
||||
}
|
||||
|
||||
$obj = new \stdClass();
|
||||
$obj->menuItem = $menuItem;
|
||||
$obj->menuItem = $menuItem;
|
||||
$trunk[] = $obj;
|
||||
}
|
||||
|
||||
$notebook = self::$notebooks->findById( $notebook_id );
|
||||
if ( $notebook == false ) {
|
||||
$notebook_id = '';
|
||||
$notebook_title = 'All';
|
||||
$notebookIcon = '';
|
||||
$notebookColor = 'none';
|
||||
} else {
|
||||
$notebook_id = $notebook->ID;
|
||||
$notebook_title = $notebook->title;
|
||||
$notebookIcon = $notebook->icon;
|
||||
$notebookColor = $notebook->color;
|
||||
}
|
||||
Components::set( 'notebookID', $notebook_id );
|
||||
Components::set( 'notebookName', $notebook_title );
|
||||
Components::set( 'notebookIcon', $notebookIcon );
|
||||
Components::set( 'notebookColor', $notebookColor );
|
||||
|
||||
// Notebook Sidebar
|
||||
$notebookSidebar = Views::simpleView('notes.nav.notebookSidebar', $trunk );
|
||||
$notebookSidebarView = Navigation::activePageSelect( $notebookSidebar, '/notes/byNotebook/' . $notebook_id, false, true );
|
||||
Components::set( 'notebookSidebar', $notebookSidebarView );
|
||||
|
||||
// Notes Sidebar
|
||||
$noteSidebar = Views::simpleView('notes.nav.noteSidebar', self::$notes->simpleObjectByNotebook( $notebook_id ) );
|
||||
$noteSidebarView = Navigation::activePageSelect( $noteSidebar, '/notes/byNote/' . $note_id, false, true );
|
||||
Components::set( 'notesSidebar', $noteSidebarView );
|
||||
|
||||
// Page Crumbs
|
||||
Navigation::setCrumbComponent( 'NotebookBreadCrumbs', 'notes/byNotebook' );
|
||||
|
||||
// Top Tabs
|
||||
$notesTabs = Views::simpleView('notes.nav.topTabs');
|
||||
$tabsView = Navigation::activePageSelect( $notesTabs, Input::get( 'url' ), false, true );
|
||||
|
||||
// Notebook Dropdown
|
||||
$notebookDropdown = Views::simpleView('notes.nav.notebookDropdown', self::$notebooks->simpleObjectByUser() );
|
||||
$notebookDropdownView = Navigation::activePageSelect( $notebookDropdown, $notebook_id, false, true );
|
||||
Components::set( 'notebookDropdown', $notebookDropdownView );
|
||||
|
||||
// Re- Parse the top tabs
|
||||
Components::set( 'NotesNav', Template::parse( $tabsView ) );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user