2014-04-24 03:53:43 +08:00
|
|
|
<?php
|
|
|
|
App::uses('AppController', 'Controller');
|
|
|
|
/**
|
|
|
|
* Zones Controller
|
|
|
|
*
|
|
|
|
* @property Zone $Zone
|
|
|
|
*/
|
|
|
|
class ZonesController extends AppController {
|
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
/**
|
|
|
|
* Components
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $components = array('RequestHandler');
|
|
|
|
|
|
|
|
public function beforeFilter() {
|
|
|
|
parent::beforeFilter();
|
2018-08-08 21:59:46 +08:00
|
|
|
|
|
|
|
global $user;
|
2019-03-21 02:26:35 +08:00
|
|
|
$canView = (!$user) || ($user['Monitors'] != 'None');
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( !$canView ) {
|
2018-07-25 04:41:09 +08:00
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 02:51:24 +08:00
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
// Find all zones which belong to a MonitorId
|
|
|
|
public function forMonitor($id = null) {
|
2016-09-04 03:02:32 +08:00
|
|
|
$this->loadModel('Monitor');
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( !$this->Monitor->exists($id) ) {
|
|
|
|
throw new NotFoundException(__('Invalid monitor'));
|
2016-09-04 03:02:32 +08:00
|
|
|
}
|
|
|
|
$this->Zone->recursive = -1;
|
|
|
|
$zones = $this->Zone->find('all', array(
|
2018-07-25 04:41:09 +08:00
|
|
|
'conditions' => array('MonitorId' => $id)
|
2016-09-04 03:02:32 +08:00
|
|
|
));
|
|
|
|
$this->set(array(
|
2018-07-25 04:41:09 +08:00
|
|
|
'zones' => $zones,
|
|
|
|
'_serialize' => array('zones')
|
2016-09-04 03:02:32 +08:00
|
|
|
));
|
2018-07-25 04:41:09 +08:00
|
|
|
}
|
2020-07-21 04:24:49 +08:00
|
|
|
|
2018-07-25 04:41:09 +08:00
|
|
|
public function index() {
|
2016-09-04 03:02:32 +08:00
|
|
|
$this->Zone->recursive = -1;
|
2018-07-25 04:41:09 +08:00
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$allowedMonitors = $user ? preg_split('@,@', $user['MonitorIds'],NULL, PREG_SPLIT_NO_EMPTY) : null;
|
|
|
|
if ( $allowedMonitors ) {
|
2018-07-25 04:41:09 +08:00
|
|
|
$mon_options = array('Zones.MonitorId' => $allowedMonitors);
|
|
|
|
} else {
|
|
|
|
$mon_options = '';
|
2016-09-04 03:02:32 +08:00
|
|
|
}
|
|
|
|
$zones = $this->Zone->find('all',$mon_options);
|
|
|
|
$this->set(array(
|
2018-07-25 04:41:09 +08:00
|
|
|
'zones' => $zones,
|
|
|
|
'_serialize' => array('zones')
|
2016-09-04 03:02:32 +08:00
|
|
|
));
|
2018-07-25 04:41:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add() {
|
2018-08-08 21:59:46 +08:00
|
|
|
|
2020-07-21 04:24:49 +08:00
|
|
|
if ( !$this->request->is('post') ) {
|
|
|
|
throw new BadRequestException(__('Invalid method. Should be post'));
|
|
|
|
return;
|
|
|
|
}
|
2018-08-08 21:59:46 +08:00
|
|
|
|
2020-07-21 04:24:49 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || $user['Monitors'] == 'Edit';
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$zone = null;
|
|
|
|
|
|
|
|
$this->Zone->create();
|
|
|
|
$zone = $this->Zone->save($this->request->data);
|
|
|
|
if ( $zone ) {
|
|
|
|
require_once __DIR__ .'/../../../includes/Monitor.php';
|
|
|
|
$monitor = new ZM\Monitor($zone['Zone']['MonitorId']);
|
|
|
|
$monitor->zmaControl('restart');
|
|
|
|
$message = 'Saved';
|
|
|
|
//$zone = $this->Zone->find('first', array('conditions' => array( array('Zone.' . $this->Zone->primaryKey => $this->Zone),
|
|
|
|
} else {
|
|
|
|
$message = 'Error: ';
|
|
|
|
// if there is a validation message, use it
|
|
|
|
if ( !$this->Zone->validates() ) {
|
|
|
|
$message = $this->Zone->validationErrors;
|
2018-07-25 04:41:09 +08:00
|
|
|
}
|
2016-09-04 03:02:32 +08:00
|
|
|
}
|
2020-07-21 04:24:49 +08:00
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'message' => $message,
|
|
|
|
'zone' => $zone,
|
|
|
|
'_serialize' => array('message','zone')
|
|
|
|
));
|
|
|
|
} // end function add()
|
2018-07-25 04:41:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* edit method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function edit($id = null) {
|
|
|
|
$this->Zone->id = $id;
|
|
|
|
|
|
|
|
if ( !$this->Zone->exists($id) ) {
|
|
|
|
throw new NotFoundException(__('Invalid zone'));
|
2016-09-04 03:02:32 +08:00
|
|
|
}
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( $this->request->is(array('post', 'put')) ) {
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || $user['Monitors'] == 'Edit';
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( $this->Zone->save($this->request->data) ) {
|
|
|
|
return $this->flash(__('The zone has been saved.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$options = array('conditions' => array('Zone.' . $this->Zone->primaryKey => $id));
|
|
|
|
$this->request->data = $this->Zone->find('first', $options);
|
2016-09-04 03:02:32 +08:00
|
|
|
}
|
2018-07-25 04:41:09 +08:00
|
|
|
$monitors = $this->Zone->Monitor->find('list');
|
|
|
|
$this->set(compact('monitors'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($id = null) {
|
|
|
|
$this->Zone->id = $id;
|
|
|
|
if ( !$this->Zone->exists() ) {
|
|
|
|
throw new NotFoundException(__('Invalid zone'));
|
|
|
|
}
|
|
|
|
$this->request->allowMethod('post', 'delete');
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || $user['Monitors'] == 'Edit';
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
2018-07-25 04:41:09 +08:00
|
|
|
if ( $this->Zone->delete() ) {
|
|
|
|
return $this->flash(__('The zone has been deleted.'), array('action' => 'index'));
|
|
|
|
} else {
|
|
|
|
return $this->flash(__('The zone could not be deleted. Please, try again.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 21:59:46 +08:00
|
|
|
} // end class
|