Skip to content

Commit

Permalink
Adding more template import features - now with TemplatePorts. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
samilliken committed Mar 14, 2015
1 parent e09b5fd commit 914fc7f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 52 deletions.
105 changes: 65 additions & 40 deletions repo/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@

$app = new \Slim\Slim();

// $app->get( '/cdutemplate', 'getCDUTemplate' );
// $app->get( '/cdutemplate/byid/:cdutemplateid', 'getCDUTemplateByID' );
$app->get( '/devicetemplate', 'getDeviceTemplate' );
$app->get( '/devicetemplate/byid/:templateid', 'getDeviceTemplateByID' );
$app->get( '/devicetemplate/bymanufacturer/:manufacturerid', 'getDeviceTemplateByManufacturer' );
$app->get( '/devicetemplate/pending', 'authenticate', 'getPendingDeviceTemplate' );
$app->get( '/template', 'getTemplate' );
$app->get( '/template/:templateid', 'getTemplateByID' );
$app->get( '/template/byid/:templateid', 'getTemplateByID' );
$app->get( '/template/bymanufacturer/:manufacturerid', 'getTemplateByManufacturer' );
$app->get( '/template/pending/', 'authenticate', 'getPendingTemplate' );
$app->get( '/template/pending/:requestid', 'authenticate', 'getPendingTemplateByID' );

$app->get( '/manufacturer', 'getManufacturer' );
$app->get( '/manufacturer/:manufacturerid', 'getManufacturerByID' );
$app->get( '/manufacturer/byid/:manufacturerid', 'getManufacturerByID' );
$app->get( '/manufacturer/pending', 'authenticate', 'getPendingManufacturer' );
$app->get( '/manufacturer/pending/', 'authenticate', 'getPendingManufacturer' );
$app->get( '/manufacturer/pending/:requestid', 'authenticate', 'getPendingManufacturerByID' );
$app->get( '/manufacturer/pending/byid/:requestid', 'authenticate', 'getPendingManufacturerByID' );

$app->put( '/manufacturer', 'authenticate', 'queueManufacturer' );
$app->put( '/manufacturer/approve', 'authenticate', 'approveManufacturer' );

$app->put( '/devicetemplate', 'authenticate', 'queueDeviceTemplate' );
$app->post( '/devicetemplate/addpictures/:requestid', 'authenticate', 'queuePictures' );
$app->put( '/template', 'authenticate', 'queueTemplate' );
$app->post( '/template/addpictures/:requestid', 'authenticate', 'queuePictures' );

/**
* Need to accept all options requests for PUT calls to work via jquery
Expand Down Expand Up @@ -96,9 +98,9 @@ function authenticate(\Slim\Route $route) {
}

// If the Session variable 'userid' exists, this is an interactive session
// Rights are adminstered by the UI, rather than the API
if ( isset( $_SESSION['userid'] ) ) {
$currUser->UserID = $_SESSION['userid'];
$currUser->verifyLogin( $_SERVER["REMOTE_ADDR"] );
return;
}

Expand Down Expand Up @@ -136,76 +138,95 @@ function authenticate(\Slim\Route $route) {
}


function getCDUTemplate() {
}

function getCDUTemplateByID() {
}

function getDeviceTemplate() {
function getTemplate() {
$dt = new DeviceTemplates();
$dtList = $dt->getDeviceTemplate();

$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['templates'] = array();
foreach ( $dtList as $devtmp ) {
$tmp = array();
foreach ( $devtmp as $prop=>$value ) {
$tmp[$prop] = $value;
}
array_push( $response['devicetemplates'], $tmp );
array_push( $response['templates'], $tmp );
}

echoRespnse( 200, $response );
}

function getDeviceTemplateById( $templateid ) {
function getTemplateById( $templateid ) {
$dt = new DeviceTemplates();
$dtList = $dt->getDeviceTemplateById( $templateid );

$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['templates'] = array();
foreach ( $dtList as $devtmp ) {
$tmp = array();
foreach ( $devtmp as $prop=>$value ) {
$tmp[$prop] = $value;
}
array_push( $response['devicetemplates'], $tmp );
array_push( $response['templates'], $tmp );
}

echoRespnse( 200, $response );

}

function getDeviceTemplateByManufacturer( $manufacturerid ) {
function getTemplateByManufacturer( $manufacturerid ) {
$dt = new DeviceTemplates();
$dtList = $dt->getDeviceTemplateByMFG( $manufacturerid );

$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['templates'] = array();
foreach ( $dtList as $devtmp ) {
$tmp = array();
foreach ( $devtmp as $prop=>$value ) {
$tmp[$prop] = $value;
}
array_push( $response['devicetemplates'], $tmp );
array_push( $response['templates'], $tmp );
}

echoRespnse( 200, $response );
}

function getPendingDeviceTemplate() {
function getPendingTemplate() {
$dt = new DeviceTemplatesQueue();

$dtList = $dt->viewStatus();

$response['error'] = false;
$response['errorcode'] = 200;
$response['templatequeue'] = array();
foreach( $dtList as $tmp ) {
$tmpl = array();
foreach( $tmp as $prop=>$value ) {
$tmpl[$prop] = $value;
}
array_push( $response['templatequeue'], $tmpl );
}

echoRespnse( 200, $response );
}

function getPendingTemplateByID( $requestid ) {
$dt = new DeviceTemplatesQueue();
$dtList = $dt->viewStatus( $requestid );

$response['error'] = false;
$response['errorcode'] = 200;
$response['templatequeue'] = array();
foreach( $dtList as $tmp ) {
$tmpl = array();
foreach( $tmp as $prop=>$value ) {
$tmpl[$prop] = $value;
}
array_push( $response['templatequeue'], $tmpl );
}

echoRespnse( 200, $response );
}
//
// URL: /api/manufacturer/pending
Expand Down Expand Up @@ -338,23 +359,24 @@ function approveManufacturer() {
}
}

function queueDeviceTemplate() {
function queueTemplate() {
global $currUser;
$app = \Slim\Slim::getInstance();
$vars = $app->request()->put();
$dType = $vars["DeviceType"];
$vars = json_decode( $app->request()->getBody() );
$dType = @$vars->template->DeviceType;

$response = array();
$response['error'] = false;
$response['errorcode'] = 200;

$t = new DeviceTemplatesQueue();
$tp = new TemplatePortsQueue();

if ( $dType == "Sensor" ) {
} elseif ( $dType == "Sensor" ) {
} else {
foreach ( $t as $prop => $value ) {
$t->$prop = isset( $vars[$prop] ) ? $vars[$prop] : '';
$t->$prop = isset( $vars->template->$prop ) ? $vars->template->$prop : '';
}

if ( $dType == "Chassis" ) {
Expand All @@ -373,6 +395,12 @@ function queueDeviceTemplate() {
$response['message'] = 'Error processing request.';
}

$tp->RequestID = $t->RequestID;
$tp->TemplateID = $t->TemplateID;
if ( is_array( $vars->templateports ) ) {
$tp->queuePorts( $vars->templateports );
}

echoRespnse( $response['errorcode'], $response );
}

Expand All @@ -383,10 +411,8 @@ function queuePictures( $RequestID ) {
$response = array();
$response['error'] = false;
$response['errorcode'] = 200;
error_log( "Looking for pictures on RequestID=" . $RequestID );

if ( isset( $_FILES["front"] ) ) {
error_log( "Front picture received for RequestID=" . $RequestID );
$fn = '/home/dcim/repo/repo/images/submitted/' . $RequestID . "." . $_FILES["front"]["name"];
if ( ! move_uploaded_file($_FILES["front"]["tmp_name"], $fn ) ){
error_log( "Error moving file " . $fn );
Expand All @@ -397,7 +423,6 @@ function queuePictures( $RequestID ) {
}

if ( isset( $_FILES['rear'] ) ) {
error_log( "Rear picture received for RequestID=" . $RequestID );
$fn = '/home/dcim/repo/repo/images/submitted/' . $RequestID . '.' . $_FILES["rear"]["name"];
if ( ! move_uploaded_file($_FILES["rear"]["tmp_name"], $fn ) ) {
error_log( "Rear file " . $_FILES['rear']['name'] . " uploaded." );
Expand Down
14 changes: 12 additions & 2 deletions repo/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CREATE TABLE SlotsQueue (
) ENGINE = InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS TemplatePortsQueue;
CREATE TABLE TemplatePortQueues (
CREATE TABLE TemplatePortsQueue (
RequestID INT(11) NOT NULL AUTO_INCREMENT,
TemplateID INT(11) NOT NULL,
PortNumber INT(11) NOT NULL,
Expand All @@ -118,9 +118,19 @@ CREATE TABLE TemplatePortQueues (
SubmissionDate DATETIME DEFAULT NULL,
ApprovedBy VARCHAR(255) NOT NULL,
ApprovedDate DATETIME DEFAULT NULL,
PRIMARY KEY(RequestID)
PRIMARY KEY(RequestID,PortNumber)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS TemplatePowerPortsQueue;
CREATE TABLE TemplatePowerPortsQueue (
RequestID INT(11) NOT NULL,
TemplateID INT(11) NOT NULL,
PortNumber INT(11) NOT NULL,
Label VARCHAR(40) NOT NULL,
PortNotes VARCHAR(80) NOT NULL,
PRIMARY KEY(RequestID,PortNumber)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS CDUTemplates;
CREATE TABLE fac_CDUTemplate (
TemplateID int(11) NOT NULL AUTO_INCREMENT,
Expand Down
44 changes: 34 additions & 10 deletions repo/repo.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function queueManufacturer() {
$this->Name = sanitize( $this->Name );
$st = $this->prepare( "select * from Manufacturers where UCASE(Name)=UCASE(:Name)" );
$st->execute( array( ":Name" => $this->Name ) );
error_log( "Searching for existing Mfg = " . $this->Name );
$st->setFetchMode( PDO::FETCH_CLASS, "Manufacturers" );
$row = $st->fetch();
if ( @$row->ManufacturerID > 0 ) {
Expand Down Expand Up @@ -273,16 +272,15 @@ function viewStatus( $RequestID = null ) {
if ( isset( $RequestID ) ){
$st = $this->prepare( "select * from DeviceTemplatesQueue where RequestID=:RequestID" );
$st->execute( array( ":RequestID"=>$RequestID ) );
} elseif ( $this->ManufacturerID > 0 ) {
$m = new Moderators();
$m->UserID = $this->UserID;
$m->ManufacturerID = $this->ManufacturerID;

if ( $currUser->Administrator || $m->checkRights() ) {
$st = $this->prepare( "select * from DeviceTemplatesQueue where ApprovedBy='' order by Model ASC, RequestID ASC" );
$st->execute();
} else {
if ( ! $currUser->Administrator ) {
$st = $this->prepare( "select * from DeviceTemplatesQueue where ApprovedBy='' and ManufacturerID in (select ManufacturerID from Moderators where UserID=:UserID) order by Model ASC, RequestID ASC" );
$st->execute( array( ":UserID"=>$currUser->UserID ) );
} else {
$st = $this->prepare( "select * from DeviceTemplatesQueue where ApprovedBy='' order by Model ASC, RequestID ASC" );
$st->execute();
}
}
}

$tmpList = array();
$st->setFetchMode( PDO::FETCH_CLASS, "DeviceTemplatesQueue" );
Expand Down Expand Up @@ -340,6 +338,32 @@ function queueDeviceTemplate() {
}
}

class TemplatePortsQueue {
var $RequestID;
var $TemplateID;
var $PortNumber;
var $Label;
var $MediaID;
var $ColorID;
var $PortNotes;

function prepare( $sql ) {
global $dbh;

return $dbh->prepare( $sql );
}

function queuePorts( $tpList ) {
$st = $this->prepare( "insert into TemplatePortsQueue set RequestID=:RequestID, TemplateID=:TemplateID, PortNumber=:PortNumber,
Label=:Label, MediaID=:MediaID, ColorID=:ColorID, PortNotes=:PortNotes" );

foreach( $tpList as $tp ) {
$st->execute( array( ":RequestID"=>$this->RequestID, ":TemplateID"=>$this->TemplateID, ":PortNumber"=>$tp->PortNumber,
":Label"=>$tp->Label, ":MediaID"=>$tp->MediaID, ":ColorID"=>$tp->ColorID, ":PortNotes"=>$tp->PortNotes ) );
}
}
}


class Moderators {
/* Simple authorization schema:
Expand Down

0 comments on commit 914fc7f

Please sign in to comment.