From 914fc7fb4c25f1a8052c4071bc03627231f7cf6d Mon Sep 17 00:00:00 2001 From: samilliken Date: Fri, 13 Mar 2015 22:49:53 -0400 Subject: [PATCH] Adding more template import features - now with TemplatePorts. #2 --- repo/api/index.php | 105 ++++++++++++++++++++++++++++----------------- repo/create.sql | 14 +++++- repo/repo.inc.php | 44 ++++++++++++++----- 3 files changed, 111 insertions(+), 52 deletions(-) diff --git a/repo/api/index.php b/repo/api/index.php index e5dbd2c..7e825d6 100644 --- a/repo/api/index.php +++ b/repo/api/index.php @@ -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 @@ -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; } @@ -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 @@ -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" ) { @@ -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 ); } @@ -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 ); @@ -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." ); diff --git a/repo/create.sql b/repo/create.sql index 3687c3a..c650b7a 100644 --- a/repo/create.sql +++ b/repo/create.sql @@ -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, @@ -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, diff --git a/repo/repo.inc.php b/repo/repo.inc.php index fc024d4..cef59af 100644 --- a/repo/repo.inc.php +++ b/repo/repo.inc.php @@ -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 ) { @@ -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" ); @@ -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: