From 1b1b93f8111e50dfcc5ce9f614009a299cfccf68 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 1 Jan 2018 14:43:02 -0500 Subject: [PATCH] use CakePHP-Enum-Behavior to add support for ENUMs to the Monitor model. This should fix #48 --- .gitmodules | 3 +++ web/api/app/Config/bootstrap.php.in | 1 + web/api/app/Controller/MonitorsController.php | 22 +++++++++++++------ web/api/app/Model/Monitor.php | 19 ++++++++++++---- web/api/app/Plugin/CakePHP-Enum-Behavior | 1 + web/api/app/View/Monitors/json/add.ctp | 2 ++ web/api/app/View/Monitors/xml/add.ctp | 2 ++ 7 files changed, 39 insertions(+), 11 deletions(-) create mode 160000 web/api/app/Plugin/CakePHP-Enum-Behavior create mode 100644 web/api/app/View/Monitors/json/add.ctp create mode 100644 web/api/app/View/Monitors/xml/add.ctp diff --git a/.gitmodules b/.gitmodules index 0fa8df21a..fc8ea09d6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,6 @@ path = web/api/app/Plugin/Crud url = https://github.com/FriendsOfCake/crud.git branch = 3.0 +[submodule "web/api/app/Plugin/CakePHP-Enum-Behavior"] + path = web/api/app/Plugin/CakePHP-Enum-Behavior + url = https://github.com/asper/CakePHP-Enum-Behavior.git diff --git a/web/api/app/Config/bootstrap.php.in b/web/api/app/Config/bootstrap.php.in index d1cd7857d..4e2d99c63 100644 --- a/web/api/app/Config/bootstrap.php.in +++ b/web/api/app/Config/bootstrap.php.in @@ -70,6 +70,7 @@ Cache::config('default', array('engine' => 'Apc')); * */ CakePlugin::load('Crud'); +CakePlugin::load('CakePHP-Enum-Behavior'); /** * You can attach event listeners to the request lifecycle as Dispatcher Filter. By default CakePHP bundles two filters: diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index c24cd12f2..0880cc68e 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -15,10 +15,13 @@ class MonitorsController extends AppController { */ public $components = array('Paginator', 'RequestHandler'); + public function beforeRender() { + $this->set($this->Monitor->enumValues()); + } public function beforeFilter() { parent::beforeFilter(); $canView = $this->Session->Read('monitorPermission'); - if ($canView =='None') { + if ($canView == 'None') { throw new UnauthorizedException(__('Insufficient Privileges')); return; } @@ -109,10 +112,9 @@ class MonitorsController extends AppController { * @return void */ public function add() { - if ($this->request->is('post')) { + if ( $this->request->is('post') ) { - if ($this->Session->Read('systemPermission') != 'Edit') - { + if ( $this->Session->Read('systemPermission') != 'Edit' ) { throw new UnauthorizedException(__('Insufficient privileges')); return; } @@ -120,8 +122,15 @@ class MonitorsController extends AppController { $this->Monitor->create(); if ($this->Monitor->save($this->request->data)) { $this->daemonControl($this->Monitor->id, 'start'); - return $this->flash(__('The monitor has been saved.'), array('action' => 'index')); + //return $this->flash(__('The monitor has been saved.'), array('action' => 'index')); + $message = 'Saved'; + } else { + $message = 'Error'; } + $this->set(array( + 'message' => $message, + '_serialize' => array('message') + )); } } @@ -138,8 +147,7 @@ class MonitorsController extends AppController { if (!$this->Monitor->exists($id)) { throw new NotFoundException(__('Invalid monitor')); } - if ($this->Session->Read('monitorPermission') != 'Edit') - { + if ($this->Session->Read('monitorPermission') != 'Edit') { throw new UnauthorizedException(__('Insufficient privileges')); return; } diff --git a/web/api/app/Model/Monitor.php b/web/api/app/Model/Monitor.php index 0504dd22a..c9697b694 100644 --- a/web/api/app/Model/Monitor.php +++ b/web/api/app/Model/Monitor.php @@ -86,10 +86,10 @@ class Monitor extends AppModel { ); /** - * * hasMany associations - * * - * * @var array - * */ + * hasMany associations + * + * @var array + */ public $hasAndBelongsToMany = array( 'Group' => array( 'className' => 'Group', @@ -108,5 +108,16 @@ class Monitor extends AppModel { 'counterQuery' => '' ), ); + public $actsAs = array( + 'CakePHP-Enum-Behavior.Enum' => array( + 'Type' => array('Local','Remote','File','Ffmpeg','Libvlc','cURL'), + 'Function' => array('None','Monitor','Modect','Record','Mocord','Nodect'), + 'Orientation' => array('0','90','180','270','hori','vert'), + 'OutputCodec' => array('h264','mjpeg','mpeg1','mpeg2'), + 'OutputContainer' => array('auto','mp4','mkv'), + 'DefaultView' => array('Events','Control'), + 'Status' => array('Unknown','NotRunning','Running','NoSignal','Signal'), + ) + ); } diff --git a/web/api/app/Plugin/CakePHP-Enum-Behavior b/web/api/app/Plugin/CakePHP-Enum-Behavior new file mode 160000 index 000000000..7108489f2 --- /dev/null +++ b/web/api/app/Plugin/CakePHP-Enum-Behavior @@ -0,0 +1 @@ +Subproject commit 7108489f218c54d36d235d3af91d6da2f8311237 diff --git a/web/api/app/View/Monitors/json/add.ctp b/web/api/app/View/Monitors/json/add.ctp new file mode 100644 index 000000000..77d2dd08b --- /dev/null +++ b/web/api/app/View/Monitors/json/add.ctp @@ -0,0 +1,2 @@ +echo json_encode($message); +echo json_encode($monitor); diff --git a/web/api/app/View/Monitors/xml/add.ctp b/web/api/app/View/Monitors/xml/add.ctp new file mode 100644 index 000000000..09fb8979a --- /dev/null +++ b/web/api/app/View/Monitors/xml/add.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $message)); +echo $xml->asXML();