mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-04-16 03:48:20 -04:00
Rough in api support for snapshots
This commit is contained in:
215
web/api/app/Controller/SnapshotsController.php
Normal file
215
web/api/app/Controller/SnapshotsController.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
App::uses('AppController', 'Controller');
|
||||
/**
|
||||
* Snapshots Controller
|
||||
*
|
||||
* @property Snapshot $Snapshot
|
||||
* @property PaginatorComponent $Paginator
|
||||
*/
|
||||
class SnapshotsController extends AppController {
|
||||
/**
|
||||
* Components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $components = array('Paginator', 'RequestHandler');
|
||||
|
||||
public function beforeFilter() {
|
||||
parent::beforeFilter();
|
||||
global $user;
|
||||
# We already tested for auth in appController, so we just need to test for specific permission
|
||||
$canView = (!$user) || ($user['Snapshots'] != 'None');
|
||||
if ( !$canView ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* index method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function index() {
|
||||
$this->Snapshot->recursive = 0;
|
||||
|
||||
if ( $this->request->params['named'] ) {
|
||||
$this->FilterComponent = $this->Components->load('Filter');
|
||||
$conditions = $this->FilterComponent->buildFilter($this->request->params['named']);
|
||||
} else {
|
||||
$conditions = array();
|
||||
}
|
||||
|
||||
$find_array = array(
|
||||
'conditions' => &$conditions,
|
||||
'contain' => array('Event'),
|
||||
'joins' => array(
|
||||
array(
|
||||
'table' => 'Snapshots_Events',
|
||||
'type' => 'left',
|
||||
'conditions' => array(
|
||||
'Snapshots_Events.SnapshotId = Snapshot.Id',
|
||||
),
|
||||
),
|
||||
),
|
||||
'snapshot' => '`Snapshot`.`Id`',
|
||||
);
|
||||
|
||||
$snapshots = $this->Snapshot->find('all', $find_array);
|
||||
$this->set(array(
|
||||
'snapshots' => $snapshots,
|
||||
'_serialize' => array('snapshots')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* view method
|
||||
*
|
||||
* @throws NotFoundException
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function view($id = null) {
|
||||
$this->Snapshot->recursive = -1;
|
||||
if (!$this->Snapshot->exists($id)) {
|
||||
throw new NotFoundException(__('Invalid snapshot'));
|
||||
}
|
||||
$options = array('conditions' => array('Snapshot.' . $this->Snapshot->primaryKey => $id));
|
||||
$snapshot = $this->Snapshot->find('first', $options);
|
||||
$this->set(array(
|
||||
'snapshot' => $snapshot,
|
||||
'_serialize' => array('snapshot')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* add method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add() {
|
||||
if ( $this->request->is('post') ) {
|
||||
|
||||
global $user;
|
||||
# We already tested for auth in appController,
|
||||
# so we just need to test for specific permission
|
||||
$canEdit = (!$user) || ($user['Snapshots'] == 'Edit');
|
||||
if ( !$canEdit ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->Snapshot->create();
|
||||
|
||||
if ( $this->request->data['Snapshot']['EventIds'] and ! isset($this->request->data['Event']) ) {
|
||||
$this->request->data['Event'] = explode(',', $this->request->data['Snapshot']['EventIds']);
|
||||
unset($this->request->data['Snapshot']['EventIds']);
|
||||
}
|
||||
if ( $this->Snapshot->saveAssociated($this->request->data, array('atomic'=>true)) ) {
|
||||
return $this->flash(
|
||||
__('The snapshot has been saved.'),
|
||||
array('action' => 'index')
|
||||
);
|
||||
} else {
|
||||
ZM\Error("Failed to save Snapshot");
|
||||
debug($this->Snapshot->invalidFields());
|
||||
}
|
||||
} # end if post
|
||||
$monitors = $this->Snapshot->Event->find('list');
|
||||
$this->set(compact('monitors'));
|
||||
} # end add
|
||||
|
||||
/**
|
||||
* edit method
|
||||
*
|
||||
* @throws NotFoundException
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function edit( $id = null ) {
|
||||
if ( !$this->Snapshot->exists($id) ) {
|
||||
throw new NotFoundException(__('Invalid snapshot'));
|
||||
}
|
||||
if ( $this->request->is(array('post', 'put'))) {
|
||||
global $user;
|
||||
# We already tested for auth in appController,
|
||||
# so we just need to test for specific permission
|
||||
$canEdit = (!$user) || ($user['Snapshots'] == 'Edit');
|
||||
if ( !$canEdit ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
$this->Snapshot->id = $id;
|
||||
if ( $this->Snapshot->save($this->request->data) ) {
|
||||
$message = 'Saved';
|
||||
} else {
|
||||
$message = 'Error';
|
||||
// if there is a validation message, use it
|
||||
if ( !$this->snapshot->validates() ) {
|
||||
$message .= ': '.$this->Snapshot->validationErrors;
|
||||
}
|
||||
}
|
||||
} # end if post/put
|
||||
|
||||
$snapshot = $this->Snapshot->findById($id);
|
||||
$this->set(array(
|
||||
'message' => $message,
|
||||
'snapshot' => $snapshot,
|
||||
'_serialize' => array('snapshot')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* delete method
|
||||
*
|
||||
* @throws NotFoundException
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id = null) {
|
||||
$this->Snapshot->id = $id;
|
||||
if ( !$this->Snapshot->exists() ) {
|
||||
throw new NotFoundException(__('Invalid snapshot'));
|
||||
}
|
||||
$this->request->allowMethod('post', 'delete');
|
||||
|
||||
global $user;
|
||||
# We already tested for auth in appController,
|
||||
# so we just need to test for specific permission
|
||||
$canEdit = (!$user) || ($user['Snapshots'] == 'Edit');
|
||||
if ( !$canEdit ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->Snapshot->delete() ) {
|
||||
return $this->flash(
|
||||
__('The snapshot has been deleted.'),
|
||||
array('action' => 'index')
|
||||
);
|
||||
} else {
|
||||
return $this->flash(
|
||||
__('The snapshot could not be deleted. Please, try again.'),
|
||||
array('action' => 'index')
|
||||
);
|
||||
}
|
||||
} // end function delete
|
||||
|
||||
// returns monitor associations
|
||||
public function associations() {
|
||||
$this->Snapshot->recursive = -1;
|
||||
$snapshots = $this->Snapshot->find('all', array(
|
||||
'contain'=> array(
|
||||
'Event' => array(
|
||||
'fields'=>array('Id','Name')
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->set(array(
|
||||
'snapshots' => $snapshots,
|
||||
'_serialize' => array('snapshots')
|
||||
));
|
||||
} // end associations
|
||||
|
||||
} // end class SnapshotController
|
||||
77
web/api/app/Model/Snapshot.php
Normal file
77
web/api/app/Model/Snapshot.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
App::uses('AppModel', 'Model');
|
||||
/**
|
||||
* Snapshot Model
|
||||
*
|
||||
* @property Event $Event
|
||||
* @property Zone $Zone
|
||||
*/
|
||||
class Snapshot extends AppModel {
|
||||
|
||||
/**
|
||||
* Use table
|
||||
*
|
||||
* @var mixed False or table name
|
||||
*/
|
||||
public $useTable = 'Snapshots';
|
||||
|
||||
/**
|
||||
* Primary key field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $primaryKey = 'Id';
|
||||
|
||||
/**
|
||||
* Display field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $displayField = 'Name';
|
||||
|
||||
public $recursive = -1;
|
||||
|
||||
/**
|
||||
* Validation rules
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $validate = array(
|
||||
'Id' => array(
|
||||
'numeric' => array(
|
||||
'rule' => array('numeric'),
|
||||
//'message' => 'Your custom message here',
|
||||
//'allowEmpty' => false,
|
||||
//'required' => false,
|
||||
//'last' => false, // Stop validation after this rule
|
||||
//'on' => 'create', // Limit validation to 'create' or 'update' operations
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
//The Associations below have been created with all possible keys, those that are not needed can be removed
|
||||
|
||||
/**
|
||||
* hasAndBelongsToMany associations
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $hasAndBelongsToMany = array(
|
||||
'Event' => array(
|
||||
'className' => 'Snapshot',
|
||||
'joinTable' => 'Snapshots_Events',
|
||||
'foreignKey' => 'EventId',
|
||||
'associationForeignKey' => 'SnapshotId',
|
||||
'unique'=>true,
|
||||
'dependent' => false,
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => '',
|
||||
'limit' => '',
|
||||
'offset' => '',
|
||||
'exclusive' => '',
|
||||
'finderQuery' => '',
|
||||
'counterQuery' => ''
|
||||
),
|
||||
);
|
||||
}
|
||||
2
web/api/app/View/Snapshots/json/edit.ctp
Normal file
2
web/api/app/View/Snapshots/json/edit.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
echo json_encode($message);
|
||||
echo json_encode($snapshot);
|
||||
1
web/api/app/View/Snapshots/json/index.ctp
Normal file
1
web/api/app/View/Snapshots/json/index.ctp
Normal file
@@ -0,0 +1 @@
|
||||
echo json_encode($snapshots);
|
||||
1
web/api/app/View/Snapshots/json/view.ctp
Normal file
1
web/api/app/View/Snapshots/json/view.ctp
Normal file
@@ -0,0 +1 @@
|
||||
echo json_encode($snapshot);
|
||||
2
web/api/app/View/Snapshots/xml/edit.ctp
Normal file
2
web/api/app/View/Snapshots/xml/edit.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
$xml = Xml::fromArray(array('response' => $message));
|
||||
echo $xml->asXML();
|
||||
2
web/api/app/View/Snapshots/xml/index.ctp
Normal file
2
web/api/app/View/Snapshots/xml/index.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
$xml = Xml::fromArray(array('response' => $groups));
|
||||
echo $xml->asXML();
|
||||
2
web/api/app/View/Snapshots/xml/view.ctp
Normal file
2
web/api/app/View/Snapshots/xml/view.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
$xml = Xml::fromArray(array('response' => $group));
|
||||
echo $xml->asXML();
|
||||
Reference in New Issue
Block a user