2016-04-21 22:58:49 +02:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
class WarninglistsController extends AppController {
public $components = array ( 'Session' , 'RequestHandler' );
public $paginate = array (
'limit' => 60 ,
'maxLimit' => 9999 , // LATER we will bump here on a problem once we have more than 9999 events <- no we won't, this is the max a user van view/page.
'contain' => array (
'WarninglistType'
),
'order' => array (
'Warninglist.id' => 'DESC'
),
);
public function index () {
$this -> paginate [ 'recursive' ] = - 1 ;
$warninglists = $this -> paginate ();
foreach ( $warninglists as & $warninglist ) {
$warninglist [ 'Warninglist' ][ 'valid_attributes' ] = array ();
foreach ( $warninglist [ 'WarninglistType' ] as $type ) $warninglist [ 'Warninglist' ][ 'valid_attributes' ][] = $type [ 'type' ];
$warninglist [ 'Warninglist' ][ 'valid_attributes' ] = implode ( ', ' , $warninglist [ 'Warninglist' ][ 'valid_attributes' ]);
unset ( $warninglist [ 'WarninglistType' ]);
}
2016-04-22 01:31:13 +02:00
if ( $this -> _isRest ()) {
$this -> set ( 'Warninglists' , $warninglists );
$this -> set ( '_serialize' , array ( 'Warninglists' ));
} else {
$this -> set ( 'warninglists' , $warninglists );
}
2016-04-21 22:58:49 +02:00
}
2016-06-04 01:08:16 +02:00
2016-04-21 22:58:49 +02:00
public function update () {
2016-04-22 01:31:13 +02:00
if ( ! $this -> request -> is ( 'post' )) throw new MethodNotAllowedException ( 'This action is only accessible via POST requests.' );
2016-04-21 22:58:49 +02:00
$result = $this -> Warninglist -> update ();
$this -> Log = ClassRegistry :: init ( 'Log' );
$fails = 0 ;
$successes = 0 ;
if ( ! empty ( $result )) {
if ( isset ( $result [ 'success' ])) {
2016-09-04 23:31:24 +02:00
foreach ( $result [ 'success' ] as $id => $success ) {
2016-04-21 22:58:49 +02:00
if ( isset ( $success [ 'old' ])) $change = $success [ 'name' ] . ': updated from v' . $success [ 'old' ] . ' to v' . $success [ 'new' ];
else $change = $success [ 'name' ] . ' v' . $success [ 'new' ] . ' installed' ;
$this -> Log -> create ();
$this -> Log -> save ( array (
'org' => $this -> Auth -> user ( 'Organisation' )[ 'name' ],
'model' => 'Warninglist' ,
'model_id' => $id ,
'email' => $this -> Auth -> user ( 'email' ),
'action' => 'update' ,
'user_id' => $this -> Auth -> user ( 'id' ),
'title' => 'Warning list updated' ,
'change' => $change ,
));
$successes ++ ;
}
}
if ( isset ( $result [ 'fails' ])) {
2016-09-04 23:31:24 +02:00
foreach ( $result [ 'fails' ] as $id => $fail ) {
2016-04-21 22:58:49 +02:00
$this -> Log -> create ();
$this -> Log -> save ( array (
'org' => $this -> Auth -> user ( 'Organisation' )[ 'name' ],
'model' => 'Warninglist' ,
'model_id' => $id ,
'email' => $this -> Auth -> user ( 'email' ),
'action' => 'update' ,
'user_id' => $this -> Auth -> user ( 'id' ),
'title' => 'Warning list failed to update' ,
'change' => $fail [ 'name' ] . ' could not be installed/updated. Error: ' . $fail [ 'fail' ],
));
$fails ++ ;
}
}
} else {
$this -> Log -> create ();
$this -> Log -> save ( array (
'org' => $this -> Auth -> user ( 'Organisation' )[ 'name' ],
'model' => 'Warninglist' ,
'model_id' => 0 ,
'email' => $this -> Auth -> user ( 'email' ),
'action' => 'update' ,
'user_id' => $this -> Auth -> user ( 'id' ),
'title' => 'Warninglist update (nothing to update)' ,
'change' => 'Executed an update of the warning lists, but there was nothing to update.' ,
));
}
2017-05-09 09:00:01 +02:00
if ( $successes == 0 && $fails == 0 ) $message = 'All warninglists are up to date already.' ;
else if ( $successes == 0 ) $message = 'Could not update any of the warning lists' ;
2016-04-21 22:58:49 +02:00
else {
$message = 'Successfully updated ' . $successes . ' warninglists.' ;
if ( $fails != 0 ) $message . ' However, could not update ' . $fails . ' warning list.' ;
2017-05-09 09:00:01 +02:00
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'Warninglist' , 'update' , false , $this -> response -> type (), $message );
} else {
2016-04-21 22:58:49 +02:00
$this -> Session -> setFlash ( $message );
2017-05-09 09:00:01 +02:00
$this -> redirect ( array ( 'controller' => 'warninglists' , 'action' => 'index' ));
2016-04-21 22:58:49 +02:00
}
}
2016-06-04 01:08:16 +02:00
2016-04-22 01:31:13 +02:00
public function toggleEnable () {
$id = $this -> request -> data [ 'Warninglist' ][ 'data' ];
2017-08-08 21:37:03 +02:00
if ( ! is_numeric ( $id )) return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => 'Warninglist not found.' )), 'status' => 200 , 'type' => 'json' ));
2016-04-21 22:58:49 +02:00
$currentState = $this -> Warninglist -> find ( 'first' , array ( 'conditions' => array ( 'id' => $id ), 'recursive' => - 1 ));
2017-08-08 21:37:03 +02:00
if ( empty ( $currentState )) return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => 'Warninglist not found.' )), 'status' => 200 , 'type' => 'json' ));
2016-04-21 22:58:49 +02:00
if ( $currentState [ 'Warninglist' ][ 'enabled' ]) {
2016-08-17 03:15:57 +02:00
$currentState [ 'Warninglist' ][ 'enabled' ] = 0 ;
2016-04-21 22:58:49 +02:00
$message = 'disabled' ;
} else {
2016-08-17 03:15:57 +02:00
$currentState [ 'Warninglist' ][ 'enabled' ] = 1 ;
2016-04-21 22:58:49 +02:00
$message = 'enabled' ;
}
if ( $this -> Warninglist -> save ( $currentState )) {
2017-06-09 15:38:45 +02:00
$this -> Warninglist -> regenerateWarninglistCaches ( $id );
2017-08-08 21:37:03 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => true , 'success' => 'Warninglist ' . $message )), 'status' => 200 , 'type' => 'json' ));
2016-04-21 22:58:49 +02:00
} else {
2017-08-08 21:37:03 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => 'Warninglist could not be enabled.' )), 'status' => 200 , 'type' => 'json' ));
2016-04-21 22:58:49 +02:00
}
}
2016-06-04 01:08:16 +02:00
2016-04-22 01:31:13 +02:00
public function enableWarninglist ( $id , $enable = false ) {
$this -> Warninglist -> id = $id ;
if ( ! $this -> Warninglist -> exists ()) throw new NotFoundException ( 'Invalid Warninglist.' );
2016-08-17 03:15:57 +02:00
// DBMS interoperability: convert boolean false to integer 0 so cakephp doesn't try to insert an empty string into the database
if ( $enable === false ) $enable = 0 ;
2016-04-22 01:31:13 +02:00
$this -> Warninglist -> saveField ( 'enabled' , $enable );
2017-06-09 15:38:45 +02:00
$this -> Warninglist -> regenerateWarninglistCaches ( $id );
2016-04-22 01:31:13 +02:00
$this -> Session -> setFlash ( 'Warninglist enabled' );
$this -> redirect ( array ( 'controller' => 'warninglists' , 'action' => 'view' , $id ));
}
2016-06-04 01:08:16 +02:00
2016-04-22 01:31:13 +02:00
public function getToggleField () {
2016-04-21 22:58:49 +02:00
if ( ! $this -> request -> is ( 'ajax' )) throw new MethodNotAllowedException ( 'This action is available via AJAX only.' );
$this -> layout = 'ajax' ;
$this -> render ( 'ajax/getToggleField' );
}
2016-06-04 01:08:16 +02:00
2016-04-22 01:31:13 +02:00
public function view ( $id ) {
if ( ! is_numeric ( $id )) throw new NotFoundException ( 'Invalid ID.' );
$warninglist = $this -> Warninglist -> find ( 'first' , array ( 'contain' => array ( 'WarninglistEntry' , 'WarninglistType' ), 'conditions' => array ( 'id' => $id )));
if ( empty ( $warninglist )) throw new NotFoundException ( 'Warninglist not found.' );
if ( $this -> _isRest ()) {
$warninglist [ 'Warninglist' ][ 'WarninglistEntry' ] = $warninglist [ 'WarninglistEntry' ];
$warninglist [ 'Warninglist' ][ 'WarninglistType' ] = $warninglist [ 'WarninglistType' ];
$this -> set ( 'Warninglist' , $warninglist [ 'Warninglist' ]);
$this -> set ( '_serialize' , array ( 'Warninglist' ));
} else {
$this -> set ( 'warninglist' , $warninglist );
}
}
2016-06-06 10:09:55 +02:00
}