mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-02-02 18:41:17 -05:00
Add Tags support to API
This commit is contained in:
committed by
Charlie Root
parent
2d1c928ba2
commit
238aa0a542
215
web/api/app/Controller/TagsController.php
Normal file
215
web/api/app/Controller/TagsController.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
App::uses('AppController', 'Controller');
|
||||
/**
|
||||
* Tags Controller
|
||||
*
|
||||
* @property Tag $Tag
|
||||
* @property PaginatorComponent $Paginator
|
||||
*/
|
||||
class TagsController 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->Events() != 'None');
|
||||
if ( !$canView ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* index method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function index() {
|
||||
$this->Tag->recursive = -1;
|
||||
|
||||
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' => 'Events_Tags',
|
||||
'type' => 'left',
|
||||
'conditions' => array(
|
||||
'Events_Tags.TagId = Tag.Id',
|
||||
),
|
||||
),
|
||||
),
|
||||
'tag' => '`Tag`.`Id`',
|
||||
);
|
||||
|
||||
$tags = $this->Tag->find('all', $find_array);
|
||||
$this->set(array(
|
||||
'tags' => $tags,
|
||||
'_serialize' => array('tags')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* view method
|
||||
*
|
||||
* @throws NotFoundException
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function view($id = null) {
|
||||
$this->Tag->recursive = -1;
|
||||
if (!$this->Tag->exists($id)) {
|
||||
throw new NotFoundException(__('Invalid tag'));
|
||||
}
|
||||
$options = array('conditions' => array('Tag.' . $this->Tag->primaryKey => $id));
|
||||
$tag = $this->Tag->find('first', $options);
|
||||
$this->set(array(
|
||||
'tag' => $tag,
|
||||
'_serialize' => array('tag')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->Tags() == 'Edit');
|
||||
if ( !$canEdit ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->Tag->create();
|
||||
|
||||
if ( $this->request->data['Tag']['EventIds'] and ! isset($this->request->data['Event']) ) {
|
||||
$this->request->data['Event'] = explode(',', $this->request->data['Tag']['EventIds']);
|
||||
unset($this->request->data['Tag']['EventIds']);
|
||||
}
|
||||
if ( $this->Tag->saveAssociated($this->request->data, array('atomic'=>true)) ) {
|
||||
return $this->flash(
|
||||
__('The tag has been saved.'),
|
||||
array('action' => 'index')
|
||||
);
|
||||
} else {
|
||||
ZM\Error("Failed to save Tag");
|
||||
debug($this->Tag->invalidFields());
|
||||
}
|
||||
} # end if post
|
||||
$monitors = $this->Tag->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->Tag->exists($id) ) {
|
||||
throw new NotFoundException(__('Invalid tag'));
|
||||
}
|
||||
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->Tags() == 'Edit');
|
||||
if ( !$canEdit ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
$this->Tag->id = $id;
|
||||
if ( $this->Tag->save($this->request->data) ) {
|
||||
$message = 'Saved';
|
||||
} else {
|
||||
$message = 'Error';
|
||||
// if there is a validation message, use it
|
||||
if ( !$this->tag->validates() ) {
|
||||
$message .= ': '.$this->Tag->validationErrors;
|
||||
}
|
||||
}
|
||||
} # end if post/put
|
||||
|
||||
$tag = $this->Tag->findById($id);
|
||||
$this->set(array(
|
||||
'message' => $message,
|
||||
'tag' => $tag,
|
||||
'_serialize' => array('tag')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* delete method
|
||||
*
|
||||
* @throws NotFoundException
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id = null) {
|
||||
$this->Tag->id = $id;
|
||||
if ( !$this->Tag->exists() ) {
|
||||
throw new NotFoundException(__('Invalid tag'));
|
||||
}
|
||||
$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->Tags() == 'Edit');
|
||||
if ( !$canEdit ) {
|
||||
throw new UnauthorizedException(__('Insufficient Privileges'));
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->Tag->delete() ) {
|
||||
return $this->flash(
|
||||
__('The tag has been deleted.'),
|
||||
array('action' => 'index')
|
||||
);
|
||||
} else {
|
||||
return $this->flash(
|
||||
__('The tag could not be deleted. Please, try again.'),
|
||||
array('action' => 'index')
|
||||
);
|
||||
}
|
||||
} // end function delete
|
||||
|
||||
// returns monitor associations
|
||||
public function associations() {
|
||||
$this->Tag->recursive = -1;
|
||||
$tags = $this->Tag->find('all', array(
|
||||
'contain'=> array(
|
||||
'Event' => array(
|
||||
'fields'=>array('Id','Name')
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->set(array(
|
||||
'tags' => $tags,
|
||||
'_serialize' => array('tags')
|
||||
));
|
||||
} // end associations
|
||||
|
||||
} // end class TagController
|
||||
@@ -106,6 +106,22 @@ class Event extends AppModel {
|
||||
'finderQuery' => '',
|
||||
'counterQuery' => ''
|
||||
),
|
||||
'Tag' => array(
|
||||
'className' => 'Tag',
|
||||
'joinTable' => 'Events_Tags',
|
||||
'foreignKey' => 'EventId',
|
||||
'associationForeignKey' => 'TagId',
|
||||
'unique' => true,
|
||||
'dependent' => false,
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => '',
|
||||
'limit' => '',
|
||||
'offset' => '',
|
||||
'exclusive' => '',
|
||||
'finderQuery' => '',
|
||||
'counterQuery' => ''
|
||||
),
|
||||
);
|
||||
|
||||
public $actsAs = array(
|
||||
|
||||
79
web/api/app/Model/Tag.php
Normal file
79
web/api/app/Model/Tag.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
App::uses('AppModel', 'Model');
|
||||
/**
|
||||
* Tag Model
|
||||
*
|
||||
*/
|
||||
class Tag extends AppModel {
|
||||
|
||||
/**
|
||||
* Use table
|
||||
*
|
||||
* @var mixed False or table name
|
||||
*/
|
||||
public $useTable = 'Tags';
|
||||
|
||||
/**
|
||||
* 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(
|
||||
'Name' => array(
|
||||
'notBlank' => array(
|
||||
'rule' => array('notBlank'))),
|
||||
'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
|
||||
|
||||
/**
|
||||
* hasMany associations
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $hasAndBelongsToMany = array(
|
||||
'Event' => array(
|
||||
'className' => 'Event',
|
||||
'joinTable' => 'Events_Tags',
|
||||
'foreignKey' => 'TagId',
|
||||
'associationForeignKey' => 'EventId',
|
||||
'unique'=>true,
|
||||
'dependent' => false,
|
||||
'conditions' => '',
|
||||
'fields' => '',
|
||||
'order' => '',
|
||||
'limit' => '',
|
||||
'offset' => '',
|
||||
'exclusive' => '',
|
||||
'finderQuery' => '',
|
||||
'counterQuery' => ''
|
||||
),
|
||||
);
|
||||
var $actsAs = array( 'Containable' );
|
||||
}
|
||||
2
web/api/app/View/Tags/json/edit.ctp
Normal file
2
web/api/app/View/Tags/json/edit.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
echo json_encode($message);
|
||||
echo json_encode($tag);
|
||||
1
web/api/app/View/Tags/json/index.ctp
Normal file
1
web/api/app/View/Tags/json/index.ctp
Normal file
@@ -0,0 +1 @@
|
||||
echo json_encode($tags);
|
||||
1
web/api/app/View/Tags/json/view.ctp
Normal file
1
web/api/app/View/Tags/json/view.ctp
Normal file
@@ -0,0 +1 @@
|
||||
echo json_encode($tag);
|
||||
2
web/api/app/View/Tags/xml/edit.ctp
Normal file
2
web/api/app/View/Tags/xml/edit.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
$xml = Xml::fromArray(array('response' => $message));
|
||||
echo $xml->asXML();
|
||||
2
web/api/app/View/Tags/xml/index.ctp
Normal file
2
web/api/app/View/Tags/xml/index.ctp
Normal file
@@ -0,0 +1,2 @@
|
||||
$xml = Xml::fromArray(array('response' => $groups));
|
||||
echo $xml->asXML();
|
||||
2
web/api/app/View/Tags/xml/view.ctp
Normal file
2
web/api/app/View/Tags/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