2014-04-25 10:00:50 +08:00
< ? php
App :: uses ( 'AppController' , 'Controller' );
2018-08-08 21:59:46 +08:00
2014-04-25 10:00:50 +08:00
/**
* Events Controller
*
* @ property Event $Event
*/
class EventsController extends AppController {
2018-07-25 04:41:09 +08:00
/**
* Components
*
* @ var array
*/
public $components = array ( 'RequestHandler' , 'Scaler' , 'Image' , 'Paginator' );
2014-04-25 10:00:50 +08:00
2017-12-05 04:52:16 +08:00
public function beforeFilter () {
parent :: beforeFilter ();
2018-08-08 21:59:46 +08:00
global $user ;
$canView = ( ! $user ) || ( $user [ 'Events' ] != 'None' );
if ( ! $canView ) {
2017-12-05 04:52:16 +08:00
throw new UnauthorizedException ( __ ( 'Insufficient Privileges' ));
return ;
}
}
2015-12-20 06:44:16 +08:00
2018-07-25 04:41:09 +08:00
/**
* index method
*
* @ return void
* This also creates a thumbnail for each event .
*/
public function index () {
$this -> Event -> recursive = - 1 ;
2018-08-08 21:59:46 +08:00
global $user ;
$allowedMonitors = $user ? preg_split ( '@,@' , $user [ 'MonitorIds' ], NULL , PREG_SPLIT_NO_EMPTY ) : null ;
2015-12-20 06:44:16 +08:00
2018-08-08 21:59:46 +08:00
if ( $allowedMonitors ) {
2017-11-14 02:27:24 +08:00
$mon_options = array ( 'Event.MonitorId' => $allowedMonitors );
} else {
2018-07-25 04:41:09 +08:00
$mon_options = '' ;
2017-11-14 02:27:24 +08:00
}
2015-06-11 10:58:58 +08:00
2018-08-08 21:59:46 +08:00
if ( $this -> request -> params [ 'named' ] ) {
2018-07-25 04:41:09 +08:00
//$this->FilterComponent = $this->Components->load('Filter');
//$conditions = $this->FilterComponent->buildFilter($this->request->params['named']);
2017-12-10 03:03:42 +08:00
$conditions = $this -> request -> params [ 'named' ];
2018-07-25 04:41:09 +08:00
} else {
$conditions = array ();
}
2017-12-05 04:52:16 +08:00
$settings = array (
2018-07-25 04:41:09 +08:00
// https://github.com/ZoneMinder/ZoneMinder/issues/995
// 'limit' => $limit['ZM_WEB_EVENTS_PER_PAGE'],
// 25 events per page which is what the above
// default is, is way too low for an API
// changing this to 100 so we don't kill ZM
// with many event APIs. In future, we can
// make a nice ZM_API_ITEMS_PER_PAGE for all pagination
// API
'limit' => '100' ,
'order' => array ( 'StartTime' ),
'paramType' => 'querystring' ,
2017-12-05 04:52:16 +08:00
);
2018-07-25 04:41:09 +08:00
if ( isset ( $conditions [ 'GroupId' ]) ) {
2017-12-05 04:52:16 +08:00
$settings [ 'joins' ] = array (
array (
'table' => 'Groups_Monitors' ,
'type' => 'inner' ,
'conditions' => array (
'Groups_Monitors.MonitorId = Event.MonitorId'
),
),
);
$settings [ 'contain' ] = array ( 'Group' );
2017-12-10 03:03:42 +08:00
}
2017-12-05 04:52:16 +08:00
$settings [ 'conditions' ] = array ( $conditions , $mon_options );
2018-07-25 04:41:09 +08:00
// How many events to return
$this -> loadModel ( 'Config' );
$limit = $this -> Config -> find ( 'list' , array (
'conditions' => array ( 'Name' => 'ZM_WEB_EVENTS_PER_PAGE' ),
'fields' => array ( 'Name' , 'Value' )
));
$this -> Paginator -> settings = $settings ;
$events = $this -> Paginator -> paginate ( 'Event' );
2015-06-11 10:58:58 +08:00
2018-07-25 04:41:09 +08:00
// For each event, get the frameID which has the largest score
2018-08-08 21:59:46 +08:00
foreach ( $events as $key => $value ) {
2018-07-25 04:41:09 +08:00
$maxScoreFrameId = $this -> getMaxScoreAlarmFrameId ( $value [ 'Event' ][ 'Id' ]);
$events [ $key ][ 'Event' ][ 'MaxScoreFrameId' ] = $maxScoreFrameId ;
}
2015-06-11 10:58:58 +08:00
2018-07-25 04:41:09 +08:00
$this -> set ( compact ( 'events' ));
2018-08-08 21:59:46 +08:00
} // end public function index()
2014-04-25 10:00:50 +08:00
2018-07-25 04:41:09 +08:00
/**
* view method
*
* @ throws NotFoundException
* @ param string $id
* @ return void
*/
public function view ( $id = null ) {
2017-12-05 04:52:16 +08:00
$this -> loadModel ( 'Config' );
$this -> Event -> recursive = 1 ;
2018-07-25 04:41:09 +08:00
if ( ! $this -> Event -> exists ( $id ) ) {
2017-12-05 04:52:16 +08:00
throw new NotFoundException ( __ ( 'Invalid event' ));
}
2015-12-20 06:44:16 +08:00
2018-08-08 21:59:46 +08:00
global $user ;
$allowedMonitors = $user ? preg_split ( '@,@' , $user [ 'MonitorIds' ], NULL , PREG_SPLIT_NO_EMPTY ) : null ;
2015-12-20 06:44:16 +08:00
2018-08-08 21:59:46 +08:00
if ( $allowedMonitors ) {
2017-12-05 04:52:16 +08:00
$mon_options = array ( 'Event.MonitorId' => $allowedMonitors );
2017-12-12 00:39:42 +08:00
} else {
2018-07-25 04:41:09 +08:00
$mon_options = '' ;
2017-12-05 04:52:16 +08:00
}
2014-04-26 02:20:55 +08:00
2017-12-05 04:52:16 +08:00
$options = array ( 'conditions' => array ( array ( 'Event.' . $this -> Event -> primaryKey => $id ), $mon_options ));
$event = $this -> Event -> find ( 'first' , $options );
# Get the previous and next events for any monitor
$this -> Event -> id = $id ;
$event_neighbors = $this -> Event -> find ( 'neighbors' );
$event [ 'Event' ][ 'Next' ] = $event_neighbors [ 'next' ][ 'Event' ][ 'Id' ];
$event [ 'Event' ][ 'Prev' ] = $event_neighbors [ 'prev' ][ 'Event' ][ 'Id' ];
2018-05-08 05:07:03 +08:00
$event [ 'Event' ][ 'fileExists' ] = $this -> Event -> fileExists ( $event [ 'Event' ]);
2018-05-09 00:22:20 +08:00
$event [ 'Event' ][ 'fileSize' ] = $this -> Event -> fileSize ( $event [ 'Event' ]);
2018-05-08 05:07:03 +08:00
2017-12-05 04:52:16 +08:00
# Also get the previous and next events for the same monitor
$event_monitor_neighbors = $this -> Event -> find ( 'neighbors' , array (
'conditions' => array ( 'Event.MonitorId' => $event [ 'Event' ][ 'MonitorId' ])
));
$event [ 'Event' ][ 'NextOfMonitor' ] = $event_monitor_neighbors [ 'next' ][ 'Event' ][ 'Id' ];
$event [ 'Event' ][ 'PrevOfMonitor' ] = $event_monitor_neighbors [ 'prev' ][ 'Event' ][ 'Id' ];
$this -> set ( array (
'event' => $event ,
'_serialize' => array ( 'event' )
));
}
/**
* add method
*
* @ return void
*/
public function add () {
2018-08-08 21:59:46 +08:00
global $user ;
$canEdit = ( ! $user ) || ( $user [ 'Events' ] == 'Edit' );
if ( ! $canEdit ) {
2017-12-05 04:52:16 +08:00
throw new UnauthorizedException ( __ ( 'Insufficient privileges' ));
return ;
}
2014-04-26 02:20:55 +08:00
2018-07-25 04:41:09 +08:00
if ( $this -> request -> is ( 'post' ) ) {
2017-12-05 04:52:16 +08:00
$this -> Event -> create ();
2018-07-25 04:41:09 +08:00
if ( $this -> Event -> save ( $this -> request -> data ) ) {
2017-12-05 04:52:16 +08:00
return $this -> flash ( __ ( 'The event has been saved.' ), array ( 'action' => 'index' ));
}
}
$monitors = $this -> Event -> Monitor -> find ( 'list' );
$this -> set ( compact ( 'monitors' ));
}
/**
* edit method
*
* @ throws NotFoundException
* @ param string $id
* @ return void
*/
public function edit ( $id = null ) {
2018-08-08 21:59:46 +08:00
global $user ;
$canEdit = ( ! $user ) || ( $user [ 'Events' ] == 'Edit' );
if ( ! $canEdit ) {
2017-12-05 04:52:16 +08:00
throw new UnauthorizedException ( __ ( 'Insufficient privileges' ));
return ;
}
2014-04-26 02:20:55 +08:00
2017-12-05 04:52:16 +08:00
$this -> Event -> id = $id ;
2014-04-25 10:00:50 +08:00
2018-07-25 04:41:09 +08:00
if ( ! $this -> Event -> exists ( $id ) ) {
2017-12-05 04:52:16 +08:00
throw new NotFoundException ( __ ( 'Invalid event' ));
}
2015-06-11 10:58:58 +08:00
2018-07-25 04:41:09 +08:00
if ( $this -> Event -> save ( $this -> request -> data ) ) {
2017-12-05 04:52:16 +08:00
$message = 'Saved' ;
} else {
$message = 'Error' ;
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$this -> set ( array (
'message' => $message ,
'_serialize' => array ( 'message' )
));
}
/**
* delete method
*
* @ throws NotFoundException
* @ param string $id
* @ return void
*/
public function delete ( $id = null ) {
2018-08-08 21:59:46 +08:00
global $user ;
$canEdit = ( ! $user ) || ( $user [ 'Events' ] == 'Edit' );
if ( ! $canEdit ) {
2017-12-05 04:52:16 +08:00
throw new UnauthorizedException ( __ ( 'Insufficient privileges' ));
return ;
}
$this -> Event -> id = $id ;
2018-07-25 04:41:09 +08:00
if ( ! $this -> Event -> exists () ) {
2017-12-05 04:52:16 +08:00
throw new NotFoundException ( __ ( 'Invalid event' ));
}
$this -> request -> allowMethod ( 'post' , 'delete' );
2018-07-25 04:41:09 +08:00
if ( $this -> Event -> delete () ) {
2017-12-05 04:52:16 +08:00
//$this->loadModel('Frame');
//$this->Event->Frame->delete();
return $this -> flash ( __ ( 'The event has been deleted.' ), array ( 'action' => 'index' ));
} else {
return $this -> flash ( __ ( 'The event could not be deleted. Please, try again.' ), array ( 'action' => 'index' ));
}
2017-12-12 00:39:42 +08:00
} // end public function delete
2017-12-05 04:52:16 +08:00
public function search () {
$this -> Event -> recursive = - 1 ;
$conditions = array ();
foreach ( $this -> params [ 'named' ] as $param_name => $value ) {
// Transform params into mysql
2018-07-25 04:41:09 +08:00
if ( preg_match ( '/interval/i' , $value , $matches ) ) {
2017-12-05 04:52:16 +08:00
$condition = array ( " $param_name >= (date_sub(now(), $value )) " );
} else {
$condition = array ( $param_name => $value );
}
array_push ( $conditions , $condition );
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$results = $this -> Event -> find ( 'all' , array (
'conditions' => $conditions
));
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$this -> set ( array (
'results' => $results ,
'_serialize' => array ( 'results' )
));
2017-12-12 00:39:42 +08:00
} // end public function search
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
// format expected:
// you can changed AlarmFrames to any other named params
// consoleEvents/1 hour/AlarmFrames >=: 1/AlarmFrames <=: 20.json
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
public function consoleEvents ( $interval = null ) {
$this -> Event -> recursive = - 1 ;
$results = array ();
2015-06-11 10:58:58 +08:00
2018-07-25 04:41:09 +08:00
$moreconditions = '' ;
2017-12-05 04:52:16 +08:00
foreach ( $this -> request -> params [ 'named' ] as $name => $param ) {
2018-07-25 04:41:09 +08:00
$moreconditions = $moreconditions . ' AND ' . $name . $param ;
2018-08-08 21:59:46 +08:00
}
2015-06-11 10:58:58 +08:00
2018-08-08 21:59:46 +08:00
$query = $this -> Event -> query ( " SELECT MonitorId, COUNT(*) AS Count FROM Events WHERE (StartTime >= (DATE_SUB(NOW(), interval $interval )) $moreconditions ) GROUP BY MonitorId; " );
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
foreach ( $query as $result ) {
$results [ $result [ 'Events' ][ 'MonitorId' ]] = $result [ 0 ][ 'Count' ];
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$this -> set ( array (
'results' => $results ,
'_serialize' => array ( 'results' )
));
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
// Create a thumbnail and return the thumbnail's data for a given event id.
public function createThumbnail ( $id = null ) {
$this -> Event -> recursive = - 1 ;
2015-06-11 10:58:58 +08:00
2018-07-25 04:41:09 +08:00
if ( ! $this -> Event -> exists ( $id ) ) {
2017-12-05 04:52:16 +08:00
throw new NotFoundException ( __ ( 'Invalid event' ));
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$event = $this -> Event -> find ( 'first' , array (
'conditions' => array ( 'Id' => $id )
));
// Find the max Frame for this Event. Error out otherwise.
$this -> loadModel ( 'Frame' );
2018-07-25 04:41:09 +08:00
if ( ! ( $frame = $this -> Frame -> find ( 'first' , array (
2017-12-05 04:52:16 +08:00
'conditions' => array (
'EventId' => $event [ 'Event' ][ 'Id' ],
'Score' => $event [ 'Event' ][ 'MaxScore' ]
)
2018-07-25 04:41:09 +08:00
))) ) {
throw new NotFoundException ( __ ( 'Can not find Frame for Event ' . $event [ 'Event' ][ 'Id' ]));
2017-12-05 04:52:16 +08:00
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$this -> loadModel ( 'Config' );
// Get the config options required for reScale and getImageSrc
// The $bw, $thumbs and unset() code is a workaround / temporary
// until I have a better way of handing per-bandwidth config options
$bw = ( isset ( $_COOKIE [ 'zmBandwidth' ]) ? strtoupper ( substr ( $_COOKIE [ 'zmBandwidth' ], 0 , 1 )) : 'L' );
$thumbs = " ZM_WEB_ ${ bw } _SCALE_THUMBS " ;
$config = $this -> Config -> find ( 'list' , array (
'conditions' => array ( 'OR' => array (
2018-07-25 04:41:09 +08:00
'Name' => array (
'ZM_WEB_LIST_THUMB_WIDTH' ,
'ZM_WEB_LIST_THUMB_HEIGHT' ,
'ZM_EVENT_IMAGE_DIGITS' ,
'ZM_DIR_IMAGES' ,
$thumbs ,
'ZM_DIR_EVENTS'
)
)),
2017-12-05 04:52:16 +08:00
'fields' => array ( 'Name' , 'Value' )
));
$config [ 'ZM_WEB_SCALE_THUMBS' ] = $config [ $thumbs ];
unset ( $config [ $thumbs ]);
// reScale based on either the width, or the hight, of the event.
if ( $config [ 'ZM_WEB_LIST_THUMB_WIDTH' ] ) {
$thumbWidth = $config [ 'ZM_WEB_LIST_THUMB_WIDTH' ];
$scale = ( 100 * $thumbWidth ) / $event [ 'Event' ][ 'Width' ];
$thumbHeight = $this -> Scaler -> reScale ( $event [ 'Event' ][ 'Height' ], $scale );
2017-12-12 00:39:42 +08:00
} elseif ( $config [ 'ZM_WEB_LIST_THUMB_HEIGHT' ] ) {
2017-12-05 04:52:16 +08:00
$thumbHeight = $config [ 'ZM_WEB_LIST_THUMB_HEIGHT' ];
$scale = ( 100 * $thumbHeight ) / $event [ 'Event' ][ 'Height' ];
$thumbWidth = $this -> Scaler -> reScale ( $event [ 'Event' ][ 'Width' ], $scale );
2017-12-12 00:39:42 +08:00
} else {
2017-12-05 04:52:16 +08:00
throw new NotFoundException ( __ ( 'No thumbnail width or height specified, please check in Options->Web' ));
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
$imageData = $this -> Image -> getImageSrc ( $event , $frame , $scale , $config );
$thumbData [ 'Path' ] = $imageData [ 'thumbPath' ];
$thumbData [ 'Width' ] = ( int ) $thumbWidth ;
$thumbData [ 'Height' ] = ( int ) $thumbHeight ;
2015-06-11 10:58:58 +08:00
2018-08-08 21:59:46 +08:00
return $thumbData ;
2017-12-05 04:52:16 +08:00
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
public function archive ( $id = null ) {
$this -> Event -> recursive = - 1 ;
2018-07-25 04:41:09 +08:00
if ( ! $this -> Event -> exists ( $id ) ) {
2017-12-05 04:52:16 +08:00
throw new NotFoundException ( __ ( 'Invalid event' ));
}
2015-06-11 10:58:58 +08:00
2017-12-05 04:52:16 +08:00
// Get the current value of Archive
$archived = $this -> Event -> find ( 'first' , array (
'fields' => array ( 'Event.Archived' ),
'conditions' => array ( 'Event.Id' => $id )
));
// If 0, 1, if 1, 0
$archiveVal = (( $archived [ 'Event' ][ 'Archived' ] == 0 ) ? 1 : 0 );
// Save the new value
$this -> Event -> id = $id ;
$this -> Event -> saveField ( 'Archived' , $archiveVal );
$this -> set ( array (
'archived' => $archiveVal ,
'_serialize' => array ( 'archived' )
));
}
2015-06-11 10:58:58 +08:00
2017-12-12 00:39:42 +08:00
public function getMaxScoreAlarmFrameId ( $id = null ) {
$this -> Event -> recursive = - 1 ;
2018-07-25 04:41:09 +08:00
if ( ! $this -> Event -> exists ( $id ) ) {
2017-12-12 00:39:42 +08:00
throw new NotFoundException ( __ ( 'Invalid event' ));
}
$event = $this -> Event -> find ( 'first' , array (
'conditions' => array ( 'Id' => $id )
));
// Find the max Frame for this Event. Error out otherwise.
$this -> loadModel ( 'Frame' );
if ( ! $frame = $this -> Frame -> find ( 'first' , array (
'conditions' => array (
'EventId' => $event [ 'Event' ][ 'Id' ],
'Score' => $event [ 'Event' ][ 'MaxScore' ]
)
))) {
2018-07-25 04:41:09 +08:00
throw new NotFoundException ( __ ( 'Can not find Frame for Event ' . $event [ 'Event' ][ 'Id' ]));
2017-12-12 00:39:42 +08:00
}
return $frame [ 'Frame' ][ 'Id' ];
}
} // end class EventsController