rough in api support for Models and Manufacturers

This commit is contained in:
Isaac Connor 2021-09-25 20:49:30 -04:00
parent d98d20958c
commit c66489fb30
10 changed files with 475 additions and 0 deletions

View File

@ -0,0 +1,156 @@
<?php
App::uses('AppController', 'Controller');
/**
* Models Controller
*
* @property Model $Model
* @property PaginatorComponent $Paginator
*/
class CameraModelsController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
/*
* A user needs the model data to calculate how to view a monitor, and there really isn't anything sensitive in this data.
* So it has been decided for now to just let everyone read it.
global $user;
$canView = (!$user) || ($user['System'] != 'None');
if ( !$canView ) {
throw new UnauthorizedException(__('Insufficient Privileges'));
return;
}
*/
}
/**
* index method
*
* @return void
*/
public function index() {
$this->CameraModel->recursive = 0;
$options = '';
$models = $this->CameraModel->find('all', $options);
$this->set(array(
'models' => $models,
'_serialize' => array('models')
));
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
$this->CameraModel->recursive = 0;
if ( !$this->CameraModel->exists($id) ) {
throw new NotFoundException(__('Invalid model'));
}
$restricted = '';
$options = array('conditions' => array(
array('CameraModel.'.$this->CameraModel->primaryKey => $id),
$restricted
)
);
$model = $this->CameraModel->find('first', $options);
$this->set(array(
'model' => $model,
'_serialize' => array('model')
));
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
global $user;
$canEdit = (!$user) || ($user['System'] == 'Edit');
if (!$canEdit) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
$this->CameraModel->create();
if ($this->CameraModel->save($this->request->data)) {
return $this->flash(__('The model has been saved.'), array('action' => 'index'));
}
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
$this->CameraModel->id = $id;
global $user;
$canEdit = (!$user) || ($user['System'] == 'Edit');
if (!$canEdit) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
if (!$this->CameraModel->exists($id)) {
throw new NotFoundException(__('Invalid model'));
}
if ($this->CameraModel->save($this->request->data)) {
$message = 'Saved';
} else {
$message = 'Error';
}
$this->set(array(
'message' => $message,
'_serialize' => array('message')
));
}
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
global $user;
$canEdit = (!$user) || ($user['System'] == 'Edit');
if (!$canEdit) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
$this->CameraModel->id = $id;
if (!$this->CameraModel->exists()) {
throw new NotFoundException(__('Invalid model'));
}
$this->request->allowMethod('post', 'delete');
if ($this->CameraModel->delete()) {
return $this->flash(__('The model has been deleted.'), array('action' => 'index'));
} else {
return $this->flash(__('The model could not be deleted. Please, try again.'), array('action' => 'index'));
}
}
}

View File

@ -0,0 +1,162 @@
<?php
App::uses('AppController', 'Controller');
/**
* Manufacturers Controller
*
* @property Manufacturer $Manufacturer
* @property PaginatorComponent $Paginator
*/
class ManufacturersController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
/*
* A user needs the manufacturer data to calculate how to view a monitor, and there really isn't anything sensitive in this data.
* So it has been decided for now to just let everyone read it.
global $user;
$canView = (!$user) || ($user['System'] != 'None');
if ( !$canView ) {
throw new UnauthorizedException(__('Insufficient Privileges'));
return;
}
*/
}
/**
* index method
*
* @return void
*/
public function index() {
$this->Manufacturer->recursive = 0;
$options = '';
$manufacturers = $this->Manufacturer->find('all', $options);
$this->set(array(
'manufacturers' => $manufacturers,
'_serialize' => array('manufacturers')
));
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
$this->Manufacturer->recursive = 0;
if ( !$this->Manufacturer->exists($id) ) {
throw new NotFoundException(__('Invalid manufacturer'));
}
$restricted = '';
$options = array('conditions' => array(
array('Manufacturer.'.$this->Manufacturer->primaryKey => $id),
$restricted
)
);
$manufacturer = $this->Manufacturer->find('first', $options);
$this->set(array(
'manufacturer' => $manufacturer,
'_serialize' => array('manufacturer')
));
}
/**
* add method
*
* @return void
*/
public function add() {
if ( $this->request->is('post') ) {
global $user;
$canEdit = (!$user) || ($user['System'] == 'Edit');
if ( !$canEdit ) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
$this->Manufacturer->create();
if ( $this->Manufacturer->save($this->request->data) ) {
# Might be nice to send it a start request
#$this->daemonControl($this->Manufacturer->id, 'start', $this->request->data);
return $this->flash(__('The manufacturer has been saved.'), array('action' => 'index'));
}
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
$this->Manufacturer->id = $id;
global $user;
$canEdit = (!$user) || ($user['System'] == 'Edit');
if ( !$canEdit ) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
if ( !$this->Manufacturer->exists($id) ) {
throw new NotFoundException(__('Invalid manufacturer'));
}
if ( $this->Manufacturer->save($this->request->data) ) {
$message = 'Saved';
} else {
$message = 'Error';
}
$this->set(array(
'message' => $message,
'_serialize' => array('message')
));
// - restart this manufacturer after change
#$this->daemonControl($this->Manufacturer->id, 'restart', $this->request->data);
}
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
global $user;
$canEdit = (!$user) || ($user['System'] == 'Edit');
if ( !$canEdit ) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
$this->Manufacturer->id = $id;
if ( !$this->Manufacturer->exists() ) {
throw new NotFoundException(__('Invalid manufacturer'));
}
$this->request->allowMethod('post', 'delete');
#$this->daemonControl($this->Manufacturer->id, 'stop');
if ( $this->Manufacturer->delete() ) {
return $this->flash(__('The manufacturer has been deleted.'), array('action' => 'index'));
} else {
return $this->flash(__('The manufacturer could not be deleted. Please, try again.'), array('action' => 'index'));
}
}
}

View File

@ -0,0 +1,70 @@
<?php
App::uses('AppModel', 'CameraModel');
/**
* Model CameraModel
*
* @property Name $Name
* @property ManufacturerId $ManufacturerId
*/
class CameraModel extends AppModel {
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'Models';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'Id';
/**
* Display field
*
* @var string
*/
public $displayField = 'Name';
public $recursive = -1;
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'Name' => array(
'notBlank' => array(
'rule' => array('notBlank'))),
'Id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasOne = array(
'Manufacturer' => array(
'className' => 'Manufacturer',
'joinTable' => 'Manufacturers',
'foreignKey' => 'Id',
),
);
//var $actsAs = array( 'Containable' );
}

View File

@ -0,0 +1,77 @@
<?php
App::uses('AppModel', 'Model');
/**
* Manufacturer Model
*
* @property Event $Event
* @property Zone $Zone
*/
class Manufacturer extends AppModel {
/**
* Use table
*
* @var mixed False or table name
*/
public $useTable = 'Manufacturers';
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'Id';
/**
* Display field
*
* @var string
*/
public $displayField = 'Name';
public $recursive = -1;
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'Id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'Name' => array(
'notBlank' => array(
'rule' => array('notBlank'))),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Model' => array(
'className' => 'Model',
'foreignKey' => 'ManufacturerId',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}

View File

@ -0,0 +1,2 @@
echo json_encode($message);
echo json_encode($manufacturer);

View File

@ -0,0 +1 @@
echo json_encode($manufacturers);

View File

@ -0,0 +1 @@
echo json_encode($manufacturer);

View File

@ -0,0 +1,2 @@
$xml = Xml::fromArray(array('response' => $message));
echo $xml->asXML();

View File

@ -0,0 +1,2 @@
$xml = Xml::fromArray(array('response' => $servers));
echo $xml->asXML();

View File

@ -0,0 +1,2 @@
$xml = Xml::fromArray(array('response' => $server));
echo $xml->asXML();