2016-04-21 22:58:49 +02:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
2018-07-19 11:48:22 +02:00
class WarninglistsController extends AppController
{
public $components = array ( 'Session' , 'RequestHandler' );
2016-04-21 22:58:49 +02:00
2018-07-19 11:48:22 +02:00
public $paginate = array (
'limit' => 60 ,
2019-04-03 03:40:49 +02:00
'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 can view/page.
2018-07-19 11:48:22 +02:00
'contain' => array (
'WarninglistType'
),
'order' => array (
'Warninglist.id' => 'DESC'
),
);
2016-04-21 22:58:49 +02:00
2018-07-19 11:48:22 +02:00
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' ]);
}
if ( $this -> _isRest ()) {
$this -> set ( 'Warninglists' , $warninglists );
$this -> set ( '_serialize' , array ( 'Warninglists' ));
} else {
$this -> set ( 'warninglists' , $warninglists );
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function update ()
{
if ( ! $this -> request -> is ( 'post' )) {
2019-04-03 03:52:18 +02:00
throw new MethodNotAllowedException ( __ ( 'This action is only accessible via POST requests.' ));
2018-07-19 11:48:22 +02:00
}
$result = $this -> Warninglist -> update ();
$this -> Log = ClassRegistry :: init ( 'Log' );
$fails = 0 ;
$successes = 0 ;
if ( ! empty ( $result )) {
if ( isset ( $result [ 'success' ])) {
foreach ( $result [ 'success' ] as $id => $success ) {
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' ),
2019-04-03 03:52:18 +02:00
'title' => __ ( 'Warning list updated' ),
2018-07-19 11:48:22 +02:00
'change' => $change ,
));
$successes ++ ;
}
}
if ( isset ( $result [ 'fails' ])) {
foreach ( $result [ 'fails' ] as $id => $fail ) {
$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' ),
2019-04-03 03:52:18 +02:00
'title' => __ ( 'Warning list failed to update' ),
'change' => $fail [ 'name' ] . __ ( ' could not be installed/updated. Error: ' ) . $fail [ 'fail' ], // TODO: needs to be optimized for non-SVO languages
2018-07-19 11:48:22 +02:00
));
$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' ),
2019-04-03 03:52:18 +02:00
'title' => __ ( 'Warninglist update (nothing to update)' ),
'change' => __ ( 'Executed an update of the warning lists, but there was nothing to update.' ),
2018-07-19 11:48:22 +02:00
));
}
if ( $successes == 0 && $fails == 0 ) {
$flashType = 'info' ;
2019-04-03 03:52:18 +02:00
$message = __ ( 'All warninglists are up to date already.' );
2018-07-19 11:48:22 +02:00
} elseif ( $successes == 0 ) {
$flashType = 'error' ;
2019-04-03 03:52:18 +02:00
$message = __ ( 'Could not update any of the warning lists' );
2018-07-19 11:48:22 +02:00
} else {
$flashType = 'success' ;
2019-04-03 03:52:18 +02:00
$message = __ ( 'Successfully updated ' . $successes . ' warninglists.' );
2018-07-19 11:48:22 +02:00
if ( $fails != 0 ) {
2019-04-03 03:52:18 +02:00
$message . __ ( ' However, could not update ' ) . $fails . ' warning list.' ; // TODO: non-SVO languages need to be considered
2018-07-19 11:48:22 +02:00
}
}
if ( $this -> _isRest ()) {
return $this -> RestResponse -> saveSuccessResponse ( 'Warninglist' , 'update' , false , $this -> response -> type (), $message );
} else {
$this -> Flash -> { $flashType }( $message );
$this -> redirect ( array ( 'controller' => 'warninglists' , 'action' => 'index' ));
}
}
2016-06-04 01:08:16 +02:00
2018-11-23 14:11:33 +01:00
/*
* toggle warninglists on or offset
* Simply POST an ID or a list of IDs to toggle the current state
* To control what state the warninglists should have after execution instead of just blindly toggling them , simply pass the enabled flag
* Example :
* { " id " : [ 5 , 8 ], " enabled " : 1 }
2018-09-24 23:10:40 +02:00
* Alternatively search by a substring in the warninglist ' s named , such as :
* { " name " : [ " %alexa% " , " %iana% " ], " enabled " : 1 }
2018-11-23 14:11:33 +01:00
*/
2018-07-19 11:48:22 +02:00
public function toggleEnable ()
{
2018-11-23 14:11:33 +01:00
if ( ! $this -> request -> is ( 'post' )) {
2019-04-03 03:52:18 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => __ ( 'This function only accepts POST requests.' ))), 'status' => 200 , 'type' => 'json' ));
2018-11-23 14:11:33 +01:00
}
if ( isset ( $this -> request -> data [ 'Warninglist' ][ 'data' ])) {
$id = $this -> request -> data [ 'Warninglist' ][ 'data' ];
} else {
if ( ! empty ( $this -> request -> data [ 'id' ])) {
$id = $this -> request -> data [ 'id' ];
} elseif ( ! empty ( $this -> request -> data [ 'name' ])) {
if ( ! is_array ( $this -> request -> data [ 'name' ])) {
$names = array ( $this -> request -> data [ 'name' ]);
} else {
$names = $this -> request -> data [ 'name' ];
}
$conditions = array ();
foreach ( $names as $k => $name ) {
$conditions [ 'OR' ][] = array ( 'LOWER(Warninglist.name) LIKE' => strtolower ( $name ));
}
$id = $this -> Warninglist -> find ( 'list' , array (
'conditions' => $conditions ,
'recursive' => - 1 ,
'fields' => array ( 'Warninglist.id' , 'Warninglist.id' )
));
}
}
if ( isset ( $this -> request -> data [ 'enabled' ])) {
$enabled = $this -> request -> data [ 'enabled' ];
}
2018-09-19 12:20:59 +02:00
if ( empty ( $id )) {
2019-04-03 03:52:18 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => __ ( 'Warninglist not found.' ))), 'status' => 200 , 'type' => 'json' ));
2018-07-19 11:48:22 +02:00
}
2018-09-19 12:20:59 +02:00
$currentState = $this -> Warninglist -> find ( 'all' , array ( 'conditions' => array ( 'id' => $id ), 'recursive' => - 1 ));
2018-07-19 11:48:22 +02:00
if ( empty ( $currentState )) {
2019-04-03 03:52:18 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => __ ( 'Warninglist(s) not found.' ))), 'status' => 200 , 'type' => 'json' ));
2018-07-19 11:48:22 +02:00
}
2018-11-23 14:11:33 +01:00
$success = 0 ;
foreach ( $currentState as $warningList ) {
if ( isset ( $enabled )) {
$warningList [ 'Warninglist' ][ 'enabled' ] = $enabled ;
$message = $enabled ? 'enabled' : 'disabled' ;
} else {
if ( $warningList [ 'Warninglist' ][ 'enabled' ]) {
$warningList [ 'Warninglist' ][ 'enabled' ] = 0 ;
$message = 'disabled' ;
} else {
$warningList [ 'Warninglist' ][ 'enabled' ] = 1 ;
$message = 'enabled' ;
}
if ( ! isset ( $enabled ) && count ( $currentState ) > 1 ) {
$message = 'toggled' ;
}
}
if ( $this -> Warninglist -> save ( $warningList )) {
$success += 1 ;
}
$this -> Warninglist -> regenerateWarninglistCaches ( $warningList [ 'Warninglist' ][ 'id' ]);
}
2018-09-19 12:20:59 +02:00
if ( $success ) {
2018-07-19 11:48:22 +02:00
$this -> Warninglist -> regenerateWarninglistCaches ( $id );
2019-04-03 03:52:18 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => true , 'success' => $success . __ ( ' warninglist(s) ' ) . $message )), 'status' => 200 , 'type' => 'json' )); // TODO: non-SVO lang considerations
2018-07-19 11:48:22 +02:00
} else {
2019-04-03 03:52:18 +02:00
return new CakeResponse ( array ( 'body' => json_encode ( array ( 'saved' => false , 'errors' => __ ( 'Warninglist(s) could not be toggled.' ))), 'status' => 200 , 'type' => 'json' ));
2018-07-19 11:48:22 +02:00
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function enableWarninglist ( $id , $enable = false )
{
$this -> Warninglist -> id = $id ;
if ( ! $this -> Warninglist -> exists ()) {
2019-04-03 03:52:18 +02:00
throw new NotFoundException ( __ ( 'Invalid Warninglist.' ));
2018-07-19 11:48:22 +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 ;
}
$this -> Warninglist -> saveField ( 'enabled' , $enable );
$this -> Warninglist -> regenerateWarninglistCaches ( $id );
2019-04-02 15:57:41 +02:00
if ( $enable === 0 ) {
$this -> Flash -> success ( __ ( 'Warninglist disabled' ));
}
else {
$this -> Flash -> success ( __ ( 'Warninglist enabled' ));
}
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'controller' => 'warninglists' , 'action' => 'view' , $id ));
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function getToggleField ()
{
if ( ! $this -> request -> is ( 'ajax' )) {
2019-04-03 03:52:18 +02:00
throw new MethodNotAllowedException ( __ ( 'This action is available via AJAX only.' ));
2018-07-19 11:48:22 +02:00
}
$this -> layout = 'ajax' ;
$this -> render ( 'ajax/getToggleField' );
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function view ( $id )
{
if ( ! is_numeric ( $id )) {
2019-04-03 03:52:18 +02:00
throw new NotFoundException ( __ ( 'Invalid ID.' ));
2018-07-19 11:48:22 +02:00
}
$warninglist = $this -> Warninglist -> find ( 'first' , array ( 'contain' => array ( 'WarninglistEntry' , 'WarninglistType' ), 'conditions' => array ( 'id' => $id )));
if ( empty ( $warninglist )) {
2019-04-03 03:52:18 +02:00
throw new NotFoundException ( __ ( 'Warninglist not found.' ));
2018-07-19 11:48:22 +02:00
}
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 );
}
}
2017-12-22 13:15:22 +01:00
2018-07-19 11:48:22 +02:00
public function delete ( $id )
{
if ( $this -> request -> is ( 'post' )) {
$id = intval ( $id );
$result = $this -> Warninglist -> quickDelete ( $id );
if ( $result ) {
2019-04-02 15:57:41 +02:00
$this -> Flash -> success ( __ ( 'Warninglist successfuly deleted.' ));
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'controller' => 'warninglists' , 'action' => 'index' ));
} else {
2019-04-02 15:57:41 +02:00
$this -> Flash -> error ( __ ( 'Warninglists could not be deleted.' ));
2018-07-19 11:48:22 +02:00
$this -> redirect ( array ( 'controller' => 'warninglists' , 'action' => 'index' ));
}
} else {
if ( $this -> request -> is ( 'ajax' )) {
$this -> set ( 'id' , $id );
$this -> render ( 'ajax/delete_confirmation' );
} else {
2019-04-03 03:52:18 +02:00
throw new MethodNotAllowedException ( __ ( 'This function can only be reached via AJAX.' ));
2018-07-19 11:48:22 +02:00
}
}
}
2018-10-16 17:57:14 +02:00
2018-11-23 14:11:33 +01:00
public function checkValue ()
{
if ( $this -> request -> is ( 'post' )) {
$warninglists = $this -> Warninglist -> getWarninglists ( array ());
if ( empty ( $this -> request -> data )) {
2019-04-03 03:52:18 +02:00
throw new NotFoundException ( __ ( 'No valid data received.' ));
2018-11-23 14:11:33 +01:00
}
$data = $this -> request -> data ;
if ( ! is_array ( $data )) {
$data = array ( $data );
}
2018-11-06 16:54:17 +01:00
if ( array_key_exists ( '[]' , $data )) {
$data = $data [ '[]' ];
}
2018-11-23 14:11:33 +01:00
$hits = array ();
foreach ( $data as $dataPoint ) {
foreach ( $warninglists as $warninglist ) {
$listValues = $this -> Warninglist -> getWarninglistEntries ( $warninglist [ 'Warninglist' ][ 'id' ]);
$listValues = array_combine ( $listValues , $listValues );
$result = $this -> Warninglist -> quickCheckValue ( $listValues , $dataPoint , $warninglist [ 'Warninglist' ][ 'type' ]);
if ( $result ) {
$hits [ $dataPoint ][] = array ( 'id' => $warninglist [ 'Warninglist' ][ 'id' ], 'name' => $warninglist [ 'Warninglist' ][ 'name' ]);
}
}
}
return $this -> RestResponse -> viewData ( $hits , $this -> response -> type ());
2018-10-16 17:57:14 +02:00
} else {
2018-11-23 14:11:33 +01:00
return $this -> RestResponse -> describe ( 'Warninglists' , 'checkValue' , false , $this -> response -> type ());
2018-10-16 17:57:14 +02:00
}
2018-11-23 14:11:33 +01:00
}
2016-06-06 10:09:55 +02:00
}