2014-01-23 12:25:04 +01:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
/**
* Tags Controller
*
* @ property Tag $Tag
*/
class TagsController extends AppController {
public $components = array ( 'Security' , 'RequestHandler' );
public $paginate = array (
'limit' => 50 ,
'order' => array (
2015-06-05 15:34:21 +02:00
'Tag.name' => 'asc'
2014-01-23 12:25:04 +01:00
)
);
public $helpers = array ( 'TextColour' );
public function beforeFilter () { // TODO REMOVE
parent :: beforeFilter ();
}
public function index () {
2014-08-20 14:55:10 +02:00
$this -> loadModel ( 'Event' );
$this -> Event -> recursive = - 1 ;
$this -> paginate [ 'contain' ] = array ( 'EventTag' => array ( 'fields' => 'event_id' ));
$paginated = $this -> paginate ();
foreach ( $paginated as $k => & $tag ) {
$eventIDs = array ();
if ( empty ( $tag [ 'EventTag' ])) $tag [ 'Tag' ][ 'count' ] = 0 ;
else {
foreach ( $tag [ 'EventTag' ] as $eventTag ) {
$eventIDs [] = $eventTag [ 'event_id' ];
}
$conditions = array ( 'Event.id' => $eventIDs );
2015-02-05 17:16:40 +01:00
if ( ! $this -> _isSiteAdmin ()) $conditions = array_merge (
$conditions ,
array ( 'OR' => array (
array ( 'AND' => array (
array ( 'Event.distribution >' => 0 ),
array ( 'Event.published =' => 1 )
)),
array ( 'Event.orgc' => $this -> Auth -> user ( 'org' ))
)));
2014-08-20 14:55:10 +02:00
$events = $this -> Event -> find ( 'all' , array (
'fields' => array ( 'Event.id' , 'Event.distribution' , 'Event.orgc' ),
'conditions' => $conditions
));
$tag [ 'Tag' ][ 'count' ] = count ( $events );
}
2015-10-30 16:28:51 +01:00
unset ( $tag [ 'EventTag' ]);
2014-08-20 14:55:10 +02:00
}
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) {
foreach ( $paginated as & $tag ) {
$tag = $tag [ 'Tag' ];
}
$this -> set ( 'Tag' , $paginated );
$this -> set ( '_serialize' , array ( 'Tag' ));
} else {
$this -> set ( 'list' , $paginated );
}
2014-01-23 12:25:04 +01:00
// send perm_tagger to view for action buttons
}
public function add () {
2015-10-30 16:28:51 +01:00
if ( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tagger' ]) throw new NotFoundException ( 'You don\'t have permission to do that.' );
2014-01-23 12:25:04 +01:00
if ( $this -> request -> is ( 'post' )) {
2015-10-30 16:28:51 +01:00
if ( isset ( $this -> request -> data [ 'Tag' ][ 'request' ])) $this -> request -> data [ 'Tag' ] = $this -> request -> data [ 'Tag' ][ 'request' ];
if ( ! isset ( $this -> request -> data [ 'Tag' ][ 'colour' ])) $this -> request -> data [ 'Tag' ][ 'colour' ] = $this -> Tag -> random_color ();
if ( isset ( $this -> request -> data [ 'Tag' ][ 'id' ])) unset ( $this -> request -> data [ 'Tag' ][ 'id' ]);
2014-01-23 12:25:04 +01:00
if ( $this -> Tag -> save ( $this -> request -> data )) {
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) $this -> redirect ( array ( 'action' => 'view' , $this -> Tag -> id ));
2014-01-23 12:25:04 +01:00
$this -> Session -> setFlash ( 'The tag has been saved.' );
$this -> redirect ( array ( 'action' => 'index' ));
} else {
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) {
$error_message = '' ;
foreach ( $this -> Tag -> validationErrors as $k => $v ) $error_message .= '[' . $k . ']: ' . $v [ 0 ];
throw new MethodNotAllowedException ( 'Could not add the Tag. ' . $error_message );
} else {
$this -> Session -> setFlash ( 'The tag could not be saved. Please, try again.' );
}
2014-01-23 12:25:04 +01:00
}
}
}
2015-11-24 03:27:14 +01:00
public function quickAdd () {
if (( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tagger' ]) || ! $this -> request -> is ( 'post' )) throw new NotFoundException ( 'You don\'t have permission to do that.' );
if ( isset ( $this -> request -> data [ 'Tag' ][ 'request' ])) $this -> request -> data [ 'Tag' ] = $this -> request -> data [ 'Tag' ][ 'request' ];
if ( $this -> Tag -> quickAdd ( $this -> request -> data [ 'Tag' ][ 'name' ])) {
$this -> Session -> setFlash ( 'The tag has been saved.' );
} else {
$this -> Session -> setFlash ( 'The tag could not be saved. Please, try again.' );
}
$this -> redirect ( $this -> referer ());
}
2014-01-23 12:25:04 +01:00
public function edit ( $id ) {
if ( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tagger' ]) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
$this -> Tag -> id = $id ;
if ( ! $this -> Tag -> exists ()) {
throw new NotFoundException ( 'Invalid tag' );
}
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
2015-04-20 11:46:55 +02:00
$this -> request -> data [ 'Tag' ][ 'id' ] = $id ;
2015-10-30 16:28:51 +01:00
if ( isset ( $this -> request -> data [ 'Tag' ][ 'request' ])) $this -> request -> data [ 'Tag' ] = $this -> request -> data [ 'Tag' ][ 'request' ];
2015-11-08 23:26:19 +01:00
2014-01-23 12:25:04 +01:00
if ( $this -> Tag -> save ( $this -> request -> data )) {
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) $this -> redirect ( array ( 'action' => 'view' , $id ));
2014-01-23 12:25:04 +01:00
$this -> Session -> setFlash ( 'The Tag has been edited' );
$this -> redirect ( array ( 'action' => 'index' ));
} else {
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) {
$error_message = '' ;
foreach ( $this -> Tag -> validationErrors as $k => $v ) $error_message .= '[' . $k . ']: ' . $v [ 0 ];
throw new MethodNotAllowedException ( 'Could not add the Tag. ' . $error_message );
}
2014-01-23 12:25:04 +01:00
$this -> Session -> setFlash ( 'The Tag could not be saved. Please, try again.' );
}
}
$this -> request -> data = $this -> Tag -> read ( null , $id );
}
public function delete ( $id ) {
if ( ! $this -> _isSiteAdmin () && ! $this -> userRole [ 'perm_tagger' ]) {
throw new NotFoundException ( 'You don\'t have permission to do that.' );
}
if ( ! $this -> request -> is ( 'post' )) {
throw new MethodNotAllowedException ();
}
$this -> Tag -> id = $id ;
if ( ! $this -> Tag -> exists ()) {
throw new NotFoundException ( 'Invalid tag' );
}
2014-02-19 17:38:31 +01:00
if ( $this -> Tag -> delete ()) {
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) {
$this -> set ( 'name' , 'Tag deleted.' );
$this -> set ( 'message' , 'Tag deleted.' );
$this -> set ( 'url' , '/tags/delete/' . $id );
$this -> set ( '_serialize' , array ( 'name' , 'message' , 'url' ));
}
$this -> Session -> setFlash ( __ ( 'Tag deleted' ));
2014-01-23 12:25:04 +01:00
} else {
2015-10-30 16:28:51 +01:00
if ( $this -> _isRest ()) throw new MethodNotAllowedException ( 'Could not delete the tag, or tag doesn\'t exist.' );
$this -> Session -> setFlash ( __ ( 'Tag was not deleted' ));
2014-01-23 12:25:04 +01:00
}
2015-10-30 16:28:51 +01:00
if ( ! $this -> _isRest ()) $this -> redirect ( array ( 'action' => 'index' ));
}
public function view ( $id ) {
if ( $this -> _isRest ()) {
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array ( 'id' => $id ),
'recursive' => - 1 ,
'contain' => array ( 'EventTag' => array ( 'fields' => 'event_id' ))
));
if ( empty ( $tag )) throw MethodNotAllowedException ( 'Invalid Tag' );
$eventIDs = array ();
if ( empty ( $tag [ 'EventTag' ])) $tag [ 'Tag' ][ 'count' ] = 0 ;
else {
foreach ( $tag [ 'EventTag' ] as $eventTag ) {
$eventIDs [] = $eventTag [ 'event_id' ];
}
$conditions = array ( 'Event.id' => $eventIDs );
if ( ! $this -> _isSiteAdmin ()) $conditions = array_merge (
$conditions ,
array ( 'OR' => array (
array ( 'AND' => array (
array ( 'Event.distribution >' => 0 ),
array ( 'Event.published =' => 1 )
)),
array ( 'Event.orgc' => $this -> Auth -> user ( 'org' ))
)));
$events = $this -> Tag -> EventTag -> Event -> find ( 'all' , array (
'fields' => array ( 'Event.id' , 'Event.distribution' , 'Event.orgc' ),
'conditions' => $conditions
));
$tag [ 'Tag' ][ 'count' ] = count ( $events );
}
unset ( $tag [ 'EventTag' ]);
$this -> set ( 'Tag' , $tag [ 'Tag' ]);
$this -> set ( '_serialize' , 'Tag' );
} else throw new MethodNotAllowedException ( 'This action is only for REST users.' );
2014-01-23 12:25:04 +01:00
}
2014-04-24 15:10:08 +02:00
public function showEventTag ( $id ) {
$this -> helpers [] = 'TextColour' ;
$this -> loadModel ( 'EventTag' );
$tags = $this -> EventTag -> find ( 'all' , array (
'conditions' => array (
'event_id' => $id
),
'contain' => 'Tag' ,
'fields' => array ( 'Tag.id' , 'Tag.colour' , 'Tag.name' ),
));
$this -> set ( 'tags' , $tags );
2015-06-05 15:34:21 +02:00
$tags = $this -> Tag -> find ( 'all' , array ( 'recursive' => - 1 , 'order' => array ( 'Tag.name ASC' )));
2014-04-24 15:10:08 +02:00
$tagNames = array ( 'None' );
foreach ( $tags as $k => $v ) {
$tagNames [ $v [ 'Tag' ][ 'id' ]] = $v [ 'Tag' ][ 'name' ];
}
$this -> set ( 'allTags' , $tagNames );
$event [ 'Event' ][ 'id' ] = $id ;
$this -> set ( 'event' , $event );
$this -> layout = 'ajax' ;
$this -> render ( '/Events/ajax/ajaxTags' );
}
2014-06-12 13:59:54 +02:00
public function viewTag ( $id ) {
$tag = $this -> Tag -> find ( 'first' , array (
'conditions' => array (
'id' => $id
),
'recursive' => - 1 ,
));
$this -> layout = null ;
$this -> set ( 'tag' , $tag );
$this -> set ( 'id' , $id );
$this -> render ( 'ajax/view_tag' );
}
2015-01-27 10:10:22 +01:00
}