diff --git a/web/api/app/Controller/TagsController.php b/web/api/app/Controller/TagsController.php new file mode 100644 index 000000000..72925eac1 --- /dev/null +++ b/web/api/app/Controller/TagsController.php @@ -0,0 +1,215 @@ +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 diff --git a/web/api/app/Model/Event.php b/web/api/app/Model/Event.php index 89afebf5d..ba16c785a 100644 --- a/web/api/app/Model/Event.php +++ b/web/api/app/Model/Event.php @@ -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( diff --git a/web/api/app/Model/Tag.php b/web/api/app/Model/Tag.php new file mode 100644 index 000000000..eb70f721f --- /dev/null +++ b/web/api/app/Model/Tag.php @@ -0,0 +1,79 @@ + 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' ); +} diff --git a/web/api/app/View/Tags/json/edit.ctp b/web/api/app/View/Tags/json/edit.ctp new file mode 100644 index 000000000..43af40e17 --- /dev/null +++ b/web/api/app/View/Tags/json/edit.ctp @@ -0,0 +1,2 @@ +echo json_encode($message); +echo json_encode($tag); diff --git a/web/api/app/View/Tags/json/index.ctp b/web/api/app/View/Tags/json/index.ctp new file mode 100644 index 000000000..c45ec12f2 --- /dev/null +++ b/web/api/app/View/Tags/json/index.ctp @@ -0,0 +1 @@ +echo json_encode($tags); diff --git a/web/api/app/View/Tags/json/view.ctp b/web/api/app/View/Tags/json/view.ctp new file mode 100644 index 000000000..c3c4e7b23 --- /dev/null +++ b/web/api/app/View/Tags/json/view.ctp @@ -0,0 +1 @@ +echo json_encode($tag); diff --git a/web/api/app/View/Tags/xml/edit.ctp b/web/api/app/View/Tags/xml/edit.ctp new file mode 100644 index 000000000..09fb8979a --- /dev/null +++ b/web/api/app/View/Tags/xml/edit.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $message)); +echo $xml->asXML(); diff --git a/web/api/app/View/Tags/xml/index.ctp b/web/api/app/View/Tags/xml/index.ctp new file mode 100644 index 000000000..8f45dfd14 --- /dev/null +++ b/web/api/app/View/Tags/xml/index.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $groups)); +echo $xml->asXML(); diff --git a/web/api/app/View/Tags/xml/view.ctp b/web/api/app/View/Tags/xml/view.ctp new file mode 100644 index 000000000..b54cad5ca --- /dev/null +++ b/web/api/app/View/Tags/xml/view.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $group)); +echo $xml->asXML();