From 44681ca7a7dc3e1e7dda6472228899221745b73b Mon Sep 17 00:00:00 2001 From: samilliken Date: Wed, 15 Apr 2015 07:30:36 -0400 Subject: [PATCH] Touch all the things. #2 --- repo/api/index.php | 383 +++++++++++++++++++++++++++++++++------- repo/create.sql | 51 +++--- repo/repo.inc.php | 426 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 727 insertions(+), 133 deletions(-) diff --git a/repo/api/index.php b/repo/api/index.php index 072a0df..03d20e7 100644 --- a/repo/api/index.php +++ b/repo/api/index.php @@ -22,9 +22,12 @@ $app->get( '/manufacturer/pending/byid/:requestid', 'authenticate', 'getPendingManufacturerByID' ); $app->put( '/manufacturer', 'authenticate', 'queueManufacturer' ); - $app->put( '/manufacturer/approve', 'authenticate', 'approveManufacturer' ); + + $app->post( '/manufacturer/approve', 'authenticate', 'approveManufacturer' ); + $app->post( '/template/approve', 'authenticate', 'approveTemplate' ); $app->put( '/template', 'authenticate', 'queueTemplate' ); + $app->post( '/template/pending/delete/:requestid', 'authenticate', 'deletePendingTemplate' ); $app->post( '/template/addpictures/:requestid', 'authenticate', 'queuePictures' ); /** @@ -156,23 +159,83 @@ function getTemplate() { echoRespnse( 200, $response ); } - function getTemplateById( $templateid ) { - $dt = new DeviceTemplates(); - $dtList = $dt->getDeviceTemplateById( $templateid ); + function getTemplateByID( $templateid ) { + // This is the "meat" of the templates, so any ancillary objects need to be returned as well + $dt = new DeviceTemplates(); + $ct = new CDUTemplates(); + $sen = new SensorTemplates(); + $ts = new ChassisSlots(); + $tp = new TemplatePorts(); + $tpp = new TemplatePowerPorts(); + + $dtList = $dt->getDeviceTemplate( $templateid ); $response['error'] = false; $response['errorcode'] = 200; $response['templates'] = array(); - foreach ( $dtList as $devtmp ) { - $tmp = array(); - foreach ( $devtmp as $prop=>$value ) { - $tmp[$prop] = $value; + foreach( $dtList as $tmp ) { + $tmpl = array(); + foreach( $tmp as $prop=>$value ) { + $tmpl[$prop] = $value; + } + if ( $tmp->FrontPictureFile != "" ) { + $tmpl["FrontPictureFile"] = "https://repository.opendcim.org/images/approved/" . $tmp->TemplateID . "." . $tmp->FrontPictureFile; + ] + if ( $tmp->RearPictureFile != "" ) { + $tmpl["RearPictureFile"] = "https://repository.opendcim.org/images/approved/" . $tmp->TemplateID . "." . $tmp->RearPictureFile; } - array_push( $response['templates'], $tmp ); - } + if ( $tmp->DeviceType == "Chassis" ) { + $sList = $ts->getSlots( $templateid ); + error_log( print_r( $sList, true )); + $tmpl['slots'] = array(); + foreach ( $sList as $slot ) { + $tmpSlot = array(); + foreach ( $slot as $prop=>$value ) { + $tmpSlot[$prop] = $value; + } + $tmpl['slots'][] = $tmpSlot; + } + } + if ( $tmp->DeviceType == "CDU" ) { + $ct->getTemplate( $templateid ); + + $tmpl['cdutemplate'] = array(); + foreach ( $ct as $prop=>$value ) { + $tmpl['cdutemplate'][$prop] = $value; + } + } + if ( $tmp->DeviceType == "Sensor" ) { + $tmpl['sensortemplate'] = array(); + + $sen->getTemplate( $templateid ); + foreach ( $sen as $prop=>$val ) { + $tmpl["sensortemplate"][$prop] = $val; + } + } + $pList = $tp->getPorts( $templateid ); + $tmpl['ports'] = array(); + foreach( $pList as $port ) { + $tmpPort = array(); + foreach( $port as $prop=>$value ) { + $tmpPort[$prop] = $value; + } + $tmpl['ports'][] = $tmpPort; + } + + $ppList = $tpp->getPorts( $templateid ); + $tmpl['powerports'] = array(); + foreach ( $ppList as $pp ) { + $tmpPwr = array(); + foreach ( $pp as $prop=>$value ) { + $tmpPwr[$prop] = $value; + } + $tmpl['powerports'][] = $tmpPwr; + } + + array_push( $response['templates'], $tmpl ); + } echoRespnse( 200, $response ); - } function getTemplateByManufacturer( $manufacturerid ) { @@ -194,6 +257,7 @@ function getTemplateByManufacturer( $manufacturerid ) { } function getPendingTemplate() { + // This is more of a high level list, for picking what to drill down to, so none of the ancillary objects are returned $dt = new DeviceTemplatesQueue(); $dtList = $dt->viewStatus(); @@ -212,7 +276,14 @@ function getPendingTemplate() { } function getPendingTemplateByID( $requestid ) { + // This is the "meat" of the templates, so any ancillary objects need to be returned as well $dt = new DeviceTemplatesQueue(); + $ct = new CDUTemplatesQueue(); + $sen = new SensorTemplatesQueue(); + $ts = new ChassisSlotsQueue(); + $tp = new TemplatePortsQueue(); + $tpp = new TemplatePowerPortsQueue(); + $dtList = $dt->viewStatus( $requestid ); $response['error'] = false; @@ -223,6 +294,53 @@ function getPendingTemplateByID( $requestid ) { foreach( $tmp as $prop=>$value ) { $tmpl[$prop] = $value; } + if ( $tmp->DeviceType == "Chassis" ) { + $sList = $ts->getSlots( $requestid ); + $tmpl['slots'] = array(); + foreach ( $sList as $slot ) { + $tmpSlot = array(); + foreach ( $slot as $prop=>$value ) { + $tmpSlot[$prop] = $value; + } + $tmpl['slots'][] = $tmpSlot; + } + } + if ( $tmp->DeviceType == "CDU" ) { + $ct->getTemplate( $requestid ); + + $tmpl['cdutemplate'] = array(); + foreach ( $ct as $prop=>$value ) { + $tmpl['cdutemplate'][$prop] = $value; + } + } + if ( $tmp->DeviceType == "Sensor" ) { + $tmpl['sensortemplate'] = array(); + + $sen->getTemplate( $requestid ); + foreach ( $sen as $prop=>$val ) { + $tmpl["sensortemplate"][$prop] = $val; + } + } + $pList = $tp->getPorts( $requestid ); + $tmpl['ports'] = array(); + foreach( $pList as $port ) { + $tmpPort = array(); + foreach( $port as $prop=>$value ) { + $tmpPort[$prop] = $value; + } + $tmpl['ports'][] = $tmpPort; + } + + $ppList = $tpp->getPorts( $requestid ); + $tmpl['powerports'] = array(); + foreach ( $ppList as $pp ) { + $tmpPwr = array(); + foreach ( $pp as $prop=>$value ) { + $tmpPwr[$prop] = $value; + } + $tmpl['powerports'][] = $tmpPwr; + } + array_push( $response['templatequeue'], $tmpl ); } @@ -313,29 +431,8 @@ function getManufacturerByID($ManufacturerID) { } // -// URL: /api/manufacturer -// Method: PUT -// Params: JSON array manufacturer of all attributes defined in database table -// Returns: 200 if successful +// POST Operations (Updates to existing data) // - function queueManufacturer() { - $app = \Slim\Slim::getInstance(); - $response = array(); - $m = new ManufacturersQueue(); - $m->Name = $app->request->put('Name'); - if ( $m->queueManufacturer() ) { - $response['error'] = false; - $response['errorcode'] = 200; - $response['message'] = 'Manufacturer addition has been submitted for approval.'; - $response['manufacturer'] = array( 'RequestID'=>$m->RequestID, 'Name'=>$m->Name ); - echoRespnse( 200, $response ); - } else { - $response['error'] = true; - $response['errorcode'] = 403; - $response['message'] = 'Manufacturer name already in pending submission queue.'; - echoRespnse( 403, $response ); - } - } function approveManufacturer() { global $currUser; @@ -359,53 +456,102 @@ function approveManufacturer() { } } - function queueTemplate() { + function approveTemplate() { global $currUser; - $app = \Slim\Slim::getInstance(); - $vars = json_decode( $app->request()->getBody() ); - $dType = @$vars->template->DeviceType; + $app = \Slim\Slim::getInstance(); $response = array(); $response['error'] = false; $response['errorcode'] = 200; + $response['message'] = "Template has been approved."; - $t = new DeviceTemplatesQueue(); - $tp = new TemplatePortsQueue(); - // $sc = new SlotCoordinatesQueue(); - // $pp = new PowerPortsQueue(); + $dt = new DeviceTemplatesQueue(); + $vars = json_decode( $app->request->getBody() ); + foreach ( $dt as $prop=>$value ) { + $dt->$prop = @$vars->$prop; + } - foreach ( $t as $prop => $value ) { - $t->$prop = isset( $vars->template->$prop ) ? $vars->template->$prop : ''; + if ( ! $dt->approveRequest( $currUser ) ) { + $response['error'] = true; + $response['errorcode'] = 400; + $response['message'] = 'Error processing request.'; } - if ( $dType == "Chassis" ) { - if ( is_array( $vars->slotcoordinates ) ) { - $sc->queueCoords( $vars->slotcoordinates ); + if ( is_array( @$vars->powerports ) ) { + $pp = new TemplatePowerPortsQueue(); + $tpp = new TemplatePowerPorts; + $tpp->flushPorts( $dt->TemplateID ); + foreach ( $vars->powerports as $pPort ) { + foreach ( $pp as $prop=>$value ) { + $pp->$prop = @$pPort->$prop; + } + $pp->TemplateID = $dt->TemplateID; + $pp->approveRequest(); } } - - if ( $dType == "CDU" ) { - if ( is_array( $vars->powerports ) ) { - $pp->queuePorts( $vars->powerports ); + if ( is_array( @$vars->ports ) ) { + // Flush out any existing ports + $tp = new TemplatePorts(); + $tp->flushPorts( $dt->TemplateID ); + + $p = new TemplatePortsQueue(); + foreach ( $vars->ports as $Port ) { + foreach ( $p as $prop=>$value ) { + $p->$prop = @$Port->$prop; + } + $p->TemplateID = $dt->TemplateID; + $p->approveRequest(); + } + } + if ( is_array( @$vars->slots ) ) { + $tsc = new ChassisSlots(); + $tsc->flushSlots( $dt->TemplateID ); + + $s = new ChassisSlotsQueue(); + foreach( $vars->slots as $slot ) { + foreach ( $s as $prop=>$value ) { + $s->$prop = @$slot->$prop; + } + $s->TemplateID = $dt->TemplateID; + $s->approveRequest(); } } + if ( $dt->DeviceType == "CDU" && is_object( $vars->cdutemplate ) ) { + $ct = new CDUTemplatesQueue(); + foreach ( $vars->cdutemplate as $prop=>$val ) { + $ct->$prop = $val; + } + $ct->TemplateID = $dt->TemplateID; - $t->SubmittedBy = $currUser->UserID; - if ( $t->queueDeviceTemplate() ) { - $response['error'] = false; - $response['errorcode'] = 200; - $response['message'] = 'Device template queued for approval.'; - $response['template'] = array( "RequestID" => $t->RequestID ); - } else { - $response['error'] = true; - $response['errorcode'] = 403; - $response['message'] = 'Error processing request.'; + $ct->approveRequest(); } + if ( $dt->DeviceType == "Sensor" && is_object( $vars->sensortemplate ) ) { + $sen = new SensorTemplatesQueue(); + foreach ( $vars->sensortemplate as $prop=>$val ) { + $sen->$prop = $val; + } + $sen->TemplateID = $dt->TemplateID; - $tp->RequestID = $t->RequestID; - $tp->TemplateID = $t->TemplateID; - if ( is_array( $vars->templateports ) ) { - $tp->queuePorts( $vars->templateports ); + $sen->approveRequest(); + } + + echoRespnse( $response['errorcode'], $response ); + } + + function deletePendingTemplate( $RequestID ) { + global $currUser; + + $response = array(); + $response['error'] = false; + $response['errorcode'] = 200; + + $t = new DeviceTemplatesQueue(); + if ( $currUser->Administrator ) { + if ( ! $t->deleteRequest( $RequestID ) ) { + $response['error'] = true; + $response['errorcode'] = 403; + $response['message'] = 'Error processing request.'; + } } echoRespnse( $response['errorcode'], $response ); @@ -426,6 +572,7 @@ function queuePictures( $RequestID ) { $response['error'] = true; $response['errorcode'] = 400; $response['message'] = 'Unable to relocate temporary file.'; + error_log( "Error saving file submission " . $_FILES["front"]["name"] ); } } @@ -436,6 +583,7 @@ function queuePictures( $RequestID ) { $response['error'] = true; $response['errorcode'] = 400; $response['message'] = 'Unable to relocate temporary file.'; + error_log( "Error saving file submission " . $_FILES["rear"]["name"] ); } } @@ -443,5 +591,112 @@ function queuePictures( $RequestID ) { } +// +// PUT Operations (Creation of New Data) +// + function queueManufacturer() { + $app = \Slim\Slim::getInstance(); + $response = array(); + $m = new ManufacturersQueue(); + $m->Name = $app->request->put('Name'); + if ( $m->queueManufacturer() ) { + $response['error'] = false; + $response['errorcode'] = 200; + $response['message'] = 'Manufacturer addition has been submitted for approval.'; + $response['manufacturer'] = array( 'RequestID'=>$m->RequestID, 'Name'=>$m->Name ); + echoRespnse( 200, $response ); + } else { + $response['error'] = true; + $response['errorcode'] = 403; + $response['message'] = 'Manufacturer name already in pending submission queue.'; + echoRespnse( 403, $response ); + } + } + + function queueTemplate() { + global $currUser; + $app = \Slim\Slim::getInstance(); + $vars = json_decode( $app->request()->getBody() ); + $dType = @$vars->template->DeviceType; + + $response = array(); + $response['error'] = false; + $response['errorcode'] = 200; + + $t = new DeviceTemplatesQueue(); + $tp = new TemplatePortsQueue(); + $sc = new ChassisSlotsQueue(); + $pp = new TemplatePowerPortsQueue(); + + foreach ( $t as $prop => $value ) { + $t->$prop = isset( $vars->template->$prop ) ? $vars->template->$prop : ''; + } + + $t->TemplateID = @$vars->template->GlobalID; + + $t->SubmittedBy = $currUser->UserID; + + if ( $t->queueDeviceTemplate() ) { + $response['error'] = false; + $response['errorcode'] = 200; + $response['message'] = 'Device template queued for approval.'; + $response['template'] = array( "RequestID" => $t->RequestID ); + } else { + $response['error'] = true; + $response['errorcode'] = 403; + $response['message'] = 'Error processing request.'; + } + + $sc->RequestID = $t->RequestID; + $sc->TemplateID = $t->TemplateID; + if ( $dType == "Chassis" ) { + if ( is_array( @$vars->slots ) ) { + $sc->queueSlots( $vars->slots ); + } + } + + if ( $dType == "CDU" ) { + $ct = new CDUTemplatesQueue(); + if ( @is_object( $vars->cdutemplate ) ) { + foreach ( $vars->cdutemplate as $prop=>$value ) { + $ct->$prop = $value; + } + + $ct->RequestID = $t->RequestID; + $ct->TemplateID = $t->TemplateID; + + $ct->queueTemplate(); + } + } + + if ( $dType == "Sensor" ) { + $sen = new SensorTemplatesQueue(); + if ( @is_object( $vars->sensortemplate ) ) { + foreach ( $vars->sensortemplate as $prop=>$val ) { + $sen->$prop = $val; + } + + $sen->RequestID = $t->RequestID; + $sen->TemplateID = $t->TemplateID; + + $sen->queueTemplate(); + } + } + + $tp->RequestID = $t->RequestID; + $tp->TemplateID = $t->TemplateID; + if ( @is_array( $vars->templateports ) ) { + $tp->queuePorts( $vars->templateports ); + } + + $pp->RequestID = $t->RequestID; + $pp->TemplateID = $t->TemplateID; + if ( @is_array( $vars->templatepowerports ) ) { + $pp->queuePorts( $vars->templatepowerports ); + } + + echoRespnse( $response['errorcode'], $response ); + } + $app->run(); ?> diff --git a/repo/create.sql b/repo/create.sql index aba0cbf..1b6f4f2 100644 --- a/repo/create.sql +++ b/repo/create.sql @@ -55,7 +55,6 @@ CREATE TABLE DeviceTemplatesQueue ( DeviceType varchar(23) NOT NULL default 'Server', PSCount int(11) NOT NULL, NumPorts int(11) NOT NULL, - Notes text NOT NULL, FrontPictureFile VARCHAR(45) NOT NULL, RearPictureFile VARCHAR(45) NOT NULL, ChassisSlots SMALLINT(6) NOT NULL, @@ -78,7 +77,6 @@ CREATE TABLE DeviceTemplates ( DeviceType varchar(23) NOT NULL default 'Server', PSCount int(11) NOT NULL, NumPorts int(11) NOT NULL, - Notes text NOT NULL, FrontPictureFile VARCHAR(45) NOT NULL, RearPictureFile VARCHAR(45) NOT NULL, ChassisSlots SMALLINT(6) NOT NULL, @@ -110,8 +108,7 @@ CREATE TABLE ChassisSlots ( Y INT(11) NULL, W INT(11) NULL, H INT(11) NULL, - LastModified DATETIME NOT NULL, - PRIMARY KEY (TemlateID) + PRIMARY KEY (TemplateID) ) ENGINE = InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS TemplatePortsQueue; @@ -121,7 +118,7 @@ CREATE TABLE TemplatePortsQueue ( PortNumber INT(11) NOT NULL, Label VARCHAR(40) NOT NULL, PRIMARY KEY(RequestID,PortNumber) -) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS TemplatePorts; CREATE TABLE TemplatePorts ( @@ -150,10 +147,8 @@ CREATE TABLE TemplatePowerPorts ( DROP TABLE IF EXISTS CDUTemplatesQueue; CREATE TABLE CDUTemplateQueue ( - RequestID INT(11) NOT NULL AUTO_INCREMENT, + RequestID INT(11) NOT NULL, TemplateID int(11) NOT NULL, - ManufacturerID int(11) NOT NULL, - Model varchar(80) NOT NULL, Managed int(1) NOT NULL, ATS int(1) NOT NULL, SNMPVersion varchar(2) NOT NULL DEFAULT '2c', @@ -167,17 +162,13 @@ CREATE TABLE CDUTemplateQueue ( ProcessingProfile enum('SingleOIDWatts','SingleOIDAmperes','Combine3OIDWatts','Combine3OIDAmperes','Convert3PhAmperes'), Voltage int(11) NOT NULL, Amperage int(11) NOT NULL, - NumOutlets int(11) NOT NULL, - PRIMARY KEY (RequestID), - KEY ManufacturerID (ManufacturerID), -) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + PRIMARY KEY (RequestID) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS CDUTemplates; CREATE TABLE CDUTemplates ( - TemplateID int(11) NOT NULL AUTO_INCREMENT, - ManufacturerID int(11) NOT NULL, - Model varchar(80) NOT NULL, + TemplateID int(11) NOT NULL, Managed int(1) NOT NULL, ATS int(1) NOT NULL, SNMPVersion varchar(2) NOT NULL DEFAULT '2c', @@ -191,12 +182,32 @@ CREATE TABLE CDUTemplates ( ProcessingProfile enum('SingleOIDWatts','SingleOIDAmperes','Combine3OIDWatts','Combine3OIDAmperes','Convert3PhAmperes'), Voltage int(11) NOT NULL, Amperage int(11) NOT NULL, - NumOutlets int(11) NOT NULL, LastModified DATETIME NOT NULL, - PRIMARY KEY (TemplateID), - KEY ManufacturerID (ManufacturerID), - UNIQUE KEY (ManufacturerID, Model) -) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + PRIMARY KEY (TemplateID) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +DROP TABLE IF EXISTS SensorTemplatesQueue; +CREATE TABLE SensorTemplatesQueue ( + RequestID int(11) NOT NULL, + TemplateID int(11) NOT NULL, + SNMPVersion varchar(2) NOT NULL, + TemperatureOID varchar(256) NOT NULL, + HumidityOID varchar(256) NOT NULL, + TempMultiplier float, + HumidityMultiplier float, + mUnits enum('english','metric'), + PRIMARY KEY(RequestID) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + DROP TABLE IF EXISTS SensorTemplates; +CREATE TABLE SensorTemplates ( + TemplateID int(11) NOT NULL, + SNMPVersion varchar(2) NOT NULL, + TemperatureOID varchar(256) NOT NULL, + HumidityOID varchar(256) NOT NULL, + TempMultiplier float, + HumidityMultiplier float, + mUnits enum('english','metric'), + PRIMARY KEY(TemplateID) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/repo/repo.inc.php b/repo/repo.inc.php index a0b1e6e..f63b620 100644 --- a/repo/repo.inc.php +++ b/repo/repo.inc.php @@ -157,7 +157,6 @@ class DeviceTemplates { var $DeviceType; var $PSCount; var $NumPorts; - var $Notes; var $FrontPictureFile; var $RearPictureFile; var $ChassisSlots; @@ -187,19 +186,6 @@ function getDeviceTemplate( $TemplateID = null ) { return $templateList; } - function getDeviceTemplatebyId( $templateid ) { - $st = $this->prepare( "select * from DeviceTemplates where TemplateID=:TemplateID" ); - $st->execute( array( ":TemplateID"=>$templateid ) ); - - $st->setFetchMode( PDO::FETCH_CLASS, "DeviceTemplates" ); - $templateList = array(); - while ( $t = $st->fetch() ) { - $templateList[] = $t; - } - - return $templateList; - } - function getDeviceTemplateByMFG( $manufacturerid ) { $st = $this->prepare( "select * from DeviceTemplates where ManufacturerID=:ManufacturerID order by Model ASC" ); $st->execute( array( ":ManufacturerID"=>$manufacturerid ) ); @@ -213,10 +199,10 @@ function getDeviceTemplateByMFG( $manufacturerid ) { return $templateList; } - } class DeviceTemplatesQueue { + var $RequestID; var $TemplateID; var $ManufacturerID; var $Model; @@ -292,6 +278,111 @@ function viewStatus( $RequestID = null ) { return $tmpList; } + function deleteRequest( $RequestID ) { + $sql = array( "delete from CDUTemplatesQueue where RequestID=:RequestID", + "delete from ChassisSlotsQueue where RequestID=:RequestID", + "delete from TemplatePortsQueue where RequestID=:RequestID", + "delete from TemplatePowerPortsQueue where RequestID=:RequestID", + "delete from DeviceTemplatesQueue where RequestID=:RequestID" ); + + foreach ( $sql as $s ) { + $st = $this->prepare( $s ); + $st->execute( array( ":RequestID"=>$RequestID ) ); + } + + array_map( "unlink", glob( "/home/dcim/repo/repo/images/submitted/$RequestID.*" ) ); + + return true; + } + + function approveRequest( $currUser ) { + // Check for an existing Make & Model combination, and if so, turn this into an update + $st = $this->prepare( "select TemplateID,count(*) as Total from DeviceTemplates where ManufacturerID=:ManufacturerID and ucase(Model)=ucase(:Model)" ); + $st->execute( array( ":ManufacturerID"=>$this->ManufacturerID, ":Model"=>$this->Model ) ); + $res = $st->fetch(); + + if ( $res['Total'] > 0 ) { + $this->TemplateID = $res['TemplateID']; + } + + // Grab the original file names before we get started + $st = $this->prepare( "select * from DeviceTemplatesQueue where RequestID=:RequestID" ); + $st->execute( array( ":RequestID" => $this->RequestID ) ); + $st->setFetchMode( PDO::FETCH_CLASS, "DeviceTemplatesQueue" ); + if ( $req = $st->fetch() ) { + $srcFront = sprintf( "/home/dcim/repo/repo/images/submitted/%d.%s", $this->RequestID, $req->FrontPictureFile ); + $srcRear = sprintf( "/home/dcim/repo/repo/images/submitted/%d.%s", $this->RequestID, $req->RearPictureFile ); + // If the TemplateID is set in the request, this is an update + if ( $this->TemplateID > 0 ) { + $st = $this->prepare( "update DeviceTemplates set ManufacturerID=:ManufacturerID, + Model=:Model, Height=:Height, Weight=:Weight, Wattage=:Wattage, DeviceType=:DeviceType, + PSCount=:PSCount, NumPorts=:NumPorts, FrontPictureFile=:FrontPictureFile, + RearPictureFile=:RearPictureFile, ChassisSlots=:ChassisSlots, RearChassisSlots=:RearChassisSlots, + LastModified=now() where TemplateID=:TemplateID" ); + $st->execute( array( ":ManufacturerID"=>$this->ManufacturerID, + ":Model"=>$this->Model, + ":Height"=>$this->Height, + ":Weight"=>$this->Weight, + ":Wattage"=>$this->Wattage, + ":DeviceType"=>$this->DeviceType, + ":PSCount"=>$this->PSCount, + ":NumPorts"=>$this->NumPorts, + ":FrontPictureFile"=>$this->FrontPictureFile, + ":RearPictureFile"=>$this->RearPictureFile, + ":ChassisSlots"=>$this->ChassisSlots, + ":RearChassisSlots"=>$this->RearChassisSlots, + ":TemplateID"=>$this->TemplateID ) ); + } else { + $st = $this->prepare( "insert into DeviceTemplates set ManufacturerID=:ManufacturerID, + Model=:Model, Height=:Height, Weight=:Weight, Wattage=:Wattage, DeviceType=:DeviceType, + PSCount=:PSCount, NumPorts=:NumPorts, FrontPictureFile=:FrontPictureFile, + RearPictureFile=:RearPictureFile, ChassisSlots=:ChassisSlots, RearChassisSlots=:RearChassisSlots, + LastModified=now()" ); + $st->execute( array( ":ManufacturerID"=>$this->ManufacturerID, + ":Model"=>$this->Model, + ":Height"=>$this->Height, + ":Weight"=>$this->Weight, + ":Wattage"=>$this->Wattage, + ":DeviceType"=>$this->DeviceType, + ":PSCount"=>$this->PSCount, + ":NumPorts"=>$this->NumPorts, + ":FrontPictureFile"=>$this->FrontPictureFile, + ":RearPictureFile"=>$this->RearPictureFile, + ":ChassisSlots"=>$this->ChassisSlots, + ":RearChassisSlots"=>$this->RearChassisSlots ) ); + $this->TemplateID=$this->lastInsertId(); + + // We just assigned a TemplateID, so propagate that to all the other tables depending on it + $tables = array( "TemplatePowerPortsQueue", "TemplatePortsQueue", "SlotsQueue", "CDUTemplatesQueue", "SensorTemlatesQueue" ); + foreach ( $tables as $table ) { + $st = $this->prepare( "update $table set TemplateID=:TemplateID where RequestID=:RequestID" ); + $st->execute( array( ":TemplateID"=>$this->TemplateID, ":RequestID"=>$this->RequestID ) ); + } + } + + // Remove any previous images for this TemplateID + $targetPattern = "images/approved/" . $this->TemplateID . "*"; + array_map( "unlink", glob( $targetPattern )); + + + $tgtFront = sprintf( "/home/dcim/repo/repo/images/approved/%d.%s", $this->TemplateID, $this->FrontPictureFile ); + $tgtRear = sprintf( "/home/dcim/repo/repo/images/approved/%d.%s", $this->TemplateID, $this->RearPictureFile ); + + @rename( $srcFront, $tgtFront ); + @rename( $srcRear, $tgtRear ); + + $this->ApprovedBy = $currUser->UserID; + + $st = $this->prepare( "update DeviceTemplatesQueue set ApprovedBy=:UserID, + TemplateID=:TemplateID, ApprovedDate=now() where RequestID=:RequestID" ); + $st->execute( array( ":UserID"=>$currUser->UserID, + ":TemplateID"=>$this->TemplateID, ":RequestID"=>$this->RequestID ) ); + } else { + error_log( "Fetch failed for request=" . $this->RequestID ); + } + + return true; + } function queueDeviceTemplate() { $this->makeSafe(); @@ -299,12 +390,12 @@ function queueDeviceTemplate() { // Make sure that we don't violate unique keys // If the TemplateID > 0, this is an update to a record if ( $this->TemplateID == 0 ) { - $st = $this->prepare( "select count(*) as Total from DeviceTemplates where ManufacturerID=:ManufacturerID and ucase(Model)=ucase(:Model)" ); + $st = $this->prepare( "select TemplateID, count(*) as Total from DeviceTemplates where ManufacturerID=:ManufacturerID and ucase(Model)=ucase(:Model)" ); $st->execute( array( ":ManufacturerID"=>$this->ManufacturerID, ":Model"=>$this->Model ) ); $row = $st->fetch(); if ( $row["Total"] > 0 ) { - return false; + $this->TemplateID = $row["TemplateID"]; } } @@ -313,7 +404,7 @@ function queueDeviceTemplate() { $st = $this->prepare( "insert into DeviceTemplatesQueue set TemplateID=:TemplateID, ManufacturerID=:ManufacturerID, Model=:Model, Height=:Height, Weight=:Weight, Wattage=:Wattage, DeviceType=:DeviceType, - PSCount=:PSCount, NumPorts=:NumPorts, Notes=:Notes, + PSCount=:PSCount, NumPorts=:NumPorts, FrontPictureFile=:FrontPictureFile, RearPictureFile=:RearPictureFile, ChassisSlots=:ChassisSlots, RearChassisSlots=:RearChassisSlots, SubmittedBy=:SubmittedBy, SubmissionDate=now()"); @@ -326,7 +417,6 @@ function queueDeviceTemplate() { ":DeviceType"=>$this->DeviceType, ":PSCount"=>$this->PSCount, ":NumPorts"=>$this->NumPorts, - ":Notes"=>$this->Notes, ":FrontPictureFile"=>$this->FrontPictureFile, ":RearPictureFile"=>$this->RearPictureFile, ":ChassisSlots"=>$this->ChassisSlots, @@ -334,7 +424,6 @@ function queueDeviceTemplate() { ":SubmittedBy"=>$this->SubmittedBy ) ); $this->RequestID = $this->lastInsertId(); - return $this->RequestID; } } @@ -342,8 +431,6 @@ function queueDeviceTemplate() { class CDUTemplatesQueue { var $RequestID; var $TemplateID; - var $ManufacturerID; - var $Model; var $Managed; var $ATS; var $SNMPVersion; @@ -357,21 +444,20 @@ class CDUTemplatesQueue { var $ProcessingProfile; var $Voltage; var $Amperage; - var $NumOutlets; function prepare( $sql ) { global $dbh; return $dbh->prepare( $sql ); } - function getTemplate() { + function getTemplate( $RequestID ) { $st = $this->prepare( "select * from CDUTemplatesQueue where RequestID=:RequestID" ); - if ( ! $st->execute( array( ":RequestID"=>$this->RequestID ) ) ) { + if ( ! $st->execute( array( ":RequestID"=>$RequestID ) ) ) { return false; } - $st->setFetchMode( PDO::FETCH_CLASS, "CDUTemlatesQueue" ); + $st->setFetchMode( PDO::FETCH_CLASS, "CDUTemplatesQueue" ); if ( $row = $st->fetch() ) { foreach ( $row as $prop=>$val ) { $this->$prop = $val; @@ -381,14 +467,11 @@ function getTemplate() { function queueTemplate() { $st = $this->prepare( "insert into CDUTemplatesQueue set RequestID=:RequestID, TemplateID=:TemplateID, - ManufacturerID=:ManufacturerID, Model=:Model, Managed=:Managed, ATS=:ATS, SNMPVersion=:SNMPVersion, + Managed=:Managed, ATS=:ATS, SNMPVersion=:SNMPVersion, VersionOID=:VersionOID, Multiplier=:Multiplier, OID1=:OID1, OID2=:OID2, OID3=:OID3, ATSStatusOID=:ATSStatusOID, - ATSDesiredResule=:ATSDesiredResult, ProcessingProfile=:ProcessingProfile, Voltage=:Voltage, Amperage=:Amperage, - NumOutlets=:NumOutlets" ); + ATSDesiredResult=:ATSDesiredResult, ProcessingProfile=:ProcessingProfile, Voltage=:Voltage, Amperage=:Amperage" ); return $st->execute( array( ":RequestID"=>$this->RequestID, ":TemplateID"=>$this->TemplateID, - ":ManufacturerID"=>$this->ManufacturerID, - ":Model"=>$this->Model, ":Managed"=>$this->Managed, ":ATS"=>$this->ATS, ":SNMPVersion"=>$this->SNMPVersion, @@ -401,15 +484,61 @@ function queueTemplate() { ":ATSDesiredResult"=>$this->ATSDesiredResult, ":ProcessingProfile"=>$this->ProcessingProfile, ":Voltage"=>$this->Voltage, - ":Amperage"=>$this->Amperage, - ":NumOutlets"=>$this->NumOutlets ) ); + ":Amperage"=>$this->Amperage ) ); + } + + function approveRequest() { + $st = $this->prepare( "select count(*) as Total from CDUTemplates where TemplateID=:TemplateID" ); + $st->execute( array( ":TemplateID" => $this->TemplateID ) ); + $r = $st->fetch(); + if ( $r['Total'] > 0 ) { + $st = $this->prepare( "update CDUTemplates set + Managed=:Managed, ATS=:ATS, SNMPVersion=:SNMPVersion, VersionOID=:VersionOID, + Multiplier=:Multiplier, OID1=:OID1, OID2=:OID2, OID3=:OID3, ATSStatusOID=:ATSStatusOID, + ATSDesiredResult=:ATSDesiredResult, ProcessingProfile=:ProcessingProfile, Voltage=:Voltage, + Amperage=:Amperage where TemplateID=:TemplateID" ); + $st->execute( array( ":Managed"=>$this->Managed, + ":ATS"=>$this->ATS, + ":SNMPVersion"=>$this->SNMPVersion, + ":VersionOID"=>$this->VersionOID, + ":Multiplier"=>$this->Multiplier, + ":OID1"=>$this->OID1, + ":OID2"=>$this->OID2, + ":OID3"=>$this->OID3, + ":ATSStatusOID"=>$this->ATSStatusOID, + ":ATSDesiredResult"=>$this->ATSDesiredResult, + ":ProcessingProfile"=>$this->ProcessingProfile, + ":Voltage"=>$this->Voltage, + ":Amperage"=>$this->Amperage, + ":TemplateID"=>$this->TemplateID ) ); + } else { + $st = $this->prepare( "insert into CDUTemplates set TemplateID=:TemplateID, + Managed=:Managed, ATS=:ATS, SNMPVersion=:SNMPVersion, VersionOID=:VersionOID, + Multiplier=:Multiplier, OID1=:OID1, OID2=:OID2, OID3=:OID3, ATSStatusOID=:ATSStatusOID, + ATSDesiredResult=:ATSDesiredResult, ProcessingProfile=:ProcessingProfile, Voltage=:Voltage, + Amperage=:Amperage" ); + $st->execute( array( ":TemplateID"=>$this->TemplateID, + ":Managed"=>$this->Managed, + ":ATS"=>$this->ATS, + ":SNMPVersion"=>$this->SNMPVersion, + ":VersionOID"=>$this->VersionOID, + ":Multiplier"=>$this->Multiplier, + ":OID1"=>$this->OID1, + ":OID2"=>$this->OID2, + ":OID3"=>$this->OID3, + ":ATSStatusOID"=>$this->ATSStatusOID, + ":ATSDesiredResult"=>$this->ATSDesiredResult, + ":ProcessingProfile"=>$this->ProcessingProfile, + ":Voltage"=>$this->Voltage, + ":Amperage"=>$this->Amperage ) ); + } + + return true; } } class CDUTemplates { var $TemplateID; - var $ManufacturerID; - var $Model; var $Managed; var $ATS; var $SNMPVersion; @@ -423,7 +552,6 @@ class CDUTemplates { var $ProcessingProfile; var $Voltage; var $Amperage; - var $NumOutlets; function prepare( $sql ) { @@ -431,10 +559,19 @@ function prepare( $sql ) { return $dbh->prepare( $sql ); } - function queueTemplate() { + function getTemplate( $TemplateID ) { + $st = $this->prepare( "select * from CDUTemplates where TemplateID=:TemplateID" ); + $st->execute( array( ":TemplateID"=>$TemplateID ) ); - } + $st->setFetchMode( PDO::FETCH_CLASS, "CDUTemplates" ); + if ( $row = $st->fetch() ) { + foreach ( $row as $prop=>$val ) { + $this->$prop = $val; + } + } + return; + } } @@ -454,9 +591,9 @@ function prepare( $sql ) { return $dbh->prepare( $sql ); } - function getSlots() { - $st = $this->prepare( "select * from SlotsQueue where RequestID=:RequestID order by Position ASC" ); - $st->execute( array( ":RequestID"=>$this->RequestID ) ); + function getSlots( $RequestID ) { + $st = $this->prepare( "select * from ChassisSlotsQueue where RequestID=:RequestID order by Position ASC" ); + $st->execute( array( ":RequestID"=>$RequestID ) ); $st->setFetchMode( PDO::FETCH_CLASS, "ChassisSlotsQueue" ); $sList = array(); while ( $row = $st->fetch() ) { @@ -467,13 +604,53 @@ function getSlots() { } function queueSlots( $sList ) { - $st = $this->prepare( "insert into SlotsQueue set RequestID=:RequestID, TemplateID=:TemplateID, Position=:Position, BackSide=:BackSide, X=:x, Y=:Y, W=:W, H=:H" ); + $st = $this->prepare( "insert into ChassisSlotsQueue set RequestID=:RequestID, TemplateID=:TemplateID, Position=:Position, BackSide=:BackSide, X=:X, Y=:Y, W=:W, H=:H" ); - foreach ( $sList as $s ) { - $st->execute( array( ":RequestID"=>$this->RequestID, ":TemplateID"=>$this->TemplateID, ":Position"=>$s->Position, - ":BackSide"=>$s->BackSide, ":X", $s->X, ":Y"=>$s->Y, ":W"=>$s->W, ":H"=>$s->H ) ); + foreach ( $sList as $slot ) { + $st->execute( array( ":RequestID"=>$this->RequestID, ":TemplateID"=>$this->TemplateID, ":Position"=>$slot->Position, + ":BackSide"=>$slot->BackSide, ":X"=>$slot->X, ":Y"=>$slot->Y, ":W"=>$slot->W, ":H"=>$slot->H ) ); } } + + function approveRequest() { + $st = $this->prepare( "insert into ChassisSlots set TemplateID=:TemplateID, Position=:Position, BackSide=:BackSide, X=:X, Y=:Y, W=:W, H=:H" ); + + $st->execute( array( ":TemplateID"=>$this->TemplateID, ":Position"=>$this->Position, ":BackSide"=>$this->BackSide, ":X"=>$this->X, ":Y"=>$this->Y, ":W"=>$this->W, ":H"=>$this->H ) ); + } +} + +class ChassisSlots { + var $TemplateID; + var $Position; + var $BackSide; + var $X; + var $Y; + var $W; + var $H; + + function prepare( $sql ) { + global $dbh; + return $dbh->prepare( $sql ); + } + + function flushSlots( $TemplateID ) { + $st = $this->prepare( "delete from ChassisSlots where TemplateID=:TemplateID" ); + return $st->execute( array( ":TemplateID"=>$TemplateID ) ); + } + + function getSlots( $TemplateID ) { + // Searching for slots without a valid TemplateID will simply return an empty set + $st = $this->prepare( "select * from ChassisSlots where TemplateID=:TemplateID order by BackSide ASC, Position ASC" ); + $st->execute( array( ":TemplateID"=>$TemplateID ) ); + + $sList = array(); + $st->setFetchMode( PDO::FETCH_CLASS, "ChassisSlots" ); + while ( $row = $st->fetch() ) { + $sList[] = $row; + } + + return $sList; + } } class TemplatePortsQueue { @@ -488,9 +665,9 @@ function prepare( $sql ) { return $dbh->prepare( $sql ); } - function getPorts() { + function getPorts( $RequestID ) { $st = $this->prepare( "select * from TemplatePortsQueue where RequestID=:RequestID order by PortNumber ASC" ); - $st->execute( array( ":RequestID"=>$this->RequestID ) ); + $st->execute( array( ":RequestID"=>$RequestID ) ); $st->setFetchMode( PDO::FETCH_CLASS, "TemplatePortsQueue" ); $tpList = array(); while ( $row = $st->fetch() ) { @@ -500,6 +677,14 @@ function getPorts() { return $tpList; } + function approveRequest() { + // This is a child of a main template, assume that TemplatePorts::FlushPorts was called first + $st = $this->prepare( "insert into TemplatePorts set TemplateID=:TemplateID, PortNumber=:PortNumber, Label=:Label" ); + $st->execute( array( ":TemplateID"=>$this->TemplateID, ":PortNumber"=>$this->PortNumber, ":Label"=>$this->Label ) ); + + return true; + } + function queuePorts( $tpList ) { $st = $this->prepare( "insert into TemplatePortsQueue set RequestID=:RequestID, TemplateID=:TemplateID, PortNumber=:PortNumber, Label=:Label" ); @@ -520,7 +705,26 @@ function prepare( $sql ) { return $dbh->prepare( $sql ); } + function flushPorts( $TemplateID ) { + $st = $this->prepare( "delete from TemplatePorts where TemplateID=:TemplateID" ); + $st->execute( array( ":TemplateID"=>$TemplateID ) ); + + return; + } + function getPorts( $TemplateID ) { + // Passing an invalid port will simply return an empty set + $st = $this->prepare( "select * from TemplatePorts where TemplateID=:TemplateID order by PortNumber ASC" ); + $st->execute( array( ":TemplateID"=>$TemplateID ) ); + $st->setFetchMode( PDO::FETCH_CLASS, "TemplatePorts" ); + + $pList = array(); + while ( $row = $st->fetch() ) { + $pList[] = $row; + } + + return $pList; + } } class TemplatePowerPortsQueue { @@ -535,9 +739,9 @@ function prepare( $sql ) { return $dbh->prepare( $sql ); } - function getPorts() { + function getPorts( $RequestID ) { $st = $this->prepare( "select * from TemplatePowerPortsQueue where RequestID=:RequestID order by PortNumber ASC" ); - $st->execute( array( ":RequestID"=>$this->RequestID ) ); + $st->execute( array( ":RequestID"=>$RequestID ) ); $st->setFetchMode( PDO::FETCH_CLASS, "TemplatePowerPortsQueue" ); $ppList = array(); while ( $row = $st->fetch() ) { @@ -547,6 +751,14 @@ function getPorts() { return $ppList; } + function approveRequest() { + $st = $this->prepare( "insert into TemplatePowerPorts set TemplateID=:TemplateID, PortNumber=:PortNumber, Label=:Label" ); + $st->execute( array( ":TemplateID"=>$this->TemplateID, ":PortNumber"=>$this->PortNumber, ":Label"=>$this->Label ) ); + + return true; + } + + function queuePorts( $ppList ) { $st = $this->prepare( "insert into TemplatePowerPortsQueue set RequestID=:RequestID, TemplateID=:TemplateID, PortNumber=:PortNumber, Label=:Label" ); @@ -567,6 +779,122 @@ function prepare( $sql ) { return $dbh->prepare( $sql ); } + function flushPorts( $TemplateID ) { + $st = $this->prepare( "delete from TemplatePowerPorts where TemplateID=:TemplateID" ); + return $st->execute( array( ":TemplateID"=>$TemplateID ) ); + } + + function getPorts( $TemplateID ) { + $st = $this->prepare( "select * from TemplatePowerPorts where TemplateID=:TemplateID order by PortNumber ASC" ); + $st->execute( array( ":TemplateID"=>$TemplateID ) ); + $st->setFetchMode( PDO::FETCH_CLASS, "TemplatePowerPorts" ); + + $ppList = array(); + while ( $row = $st->fetch() ) { + $ppList[] = $row; + } + + return $ppList; + } +} + +class SensorTemplatesQueue { + var $RequestID; + var $TemplateID; + var $SNMPVersion; + var $TemperatureOID; + var $HumidityOID; + var $TempMultiplier; + var $HumidityMultiplier; + var $mUnits; + + function prepare( $sql ) { + global $dbh; + return $dbh->prepare( $sql ); + } + + function getTemplate( $requestid ) { + $st = $this->prepare( "select * from SensorTemplatesQueue where RequestID=:RequestID" ); + $st->execute( array( ":RequestID"=>$requestid ) ); + $st->setFetchMode( PDO::FETCH_CLASS, "SensorTemplatesQueue" ); + + if ( $row = $st->fetch() ) { + foreach ( $row as $prop=>$val ) { + $this->$prop = $val; + } + return true; + } else { + return false; + } + } + + function queueTemplate() { + $st = $this->prepare( "insert into SensorTemplatesQueue set RequestID=:RequestID, TemplateID=:TemplateID, + SNMPVersion=:SNMPVersion, TemperatureOID=:TemperatureOID, HumidityOID=:HumidityOID, + TempMultiplier=:TempMultiplier, HumidityMultiplier=:HumidityMultiplier, mUnits=:mUnits" ); + return $st->execute( array( ":RequestID"=>$this->RequestID, + ":TemplateID"=>$this->TemplateID, + ":SNMPVersion"=>$this->SNMPVersion, + ":TemperatureOID"=>$this->TemperatureOID, + ":HumidityOID"=>$this->HumidityOID, + ":TempMultiplier"=>$this->TempMultiplier, + ":HumidityMultiplier"=>$this->HumidityMultiplier, + ":mUnits"=>$this->mUnits ) ); + } + + function approveRequest() { + $st = $this->prepare( "select TemplateID, count(*) as Total from SensorTemplates where TemplateID=:TemplateID" ); + $st->execute( array( ":TemplateID"=>$this->TemplateID ) ); + $row = $st->fetch(); + + if ( $row['Total'] > 0 ) { + $st = $this->prepare( "update SensorTemplates set SNMPVersion=:SNMPVersion, TemperatureOID=:TemperatureOID, + HumidityOID=:HumidityOID, TempMultiplier=:TempMultiplier, HumidityMultiplier=:HumidityMultiplier, + mUnits=:mUnits where TemplateID=:TemplateID" ); + } else { + $st = $this->prepare( "insert into SensorTemplates set SNMPVersion=:SNMPVersion, TemperatureOID=:TemperatureOID, + HumidityOID=:HumidityOID, TempMultiplier=:TempMultiplier, HumidityMultiplier=:HumidityMultiplier, + mUnits=:mUnits, TemplateID=:TemplateID" ); + } + + return $st->execute( array( ":TemplateID"=>$this->TemplateID, + ":SNMPVersion"=>$this->SNMPVersion, + ":TemperatureOID"=>$this->TemperatureOID, + ":HumidityOID"=>$this->HumidityOID, + ":TempMultiplier"=>$this->TempMultiplier, + ":HumidityMultiplier"=>$this->HumidityMultiplier, + ":mUnits"=>$this->mUnits ) ); + } +} + +class SensorTemplates { + var $TemplateID; + var $SNMPVersion; + var $TemperatureOID; + var $HumidityOID; + var $TempMultiplier; + var $HumidityMultiplier; + var $mUnits; + + function prepare( $sql ) { + global $dbh; + return $dbh->prepare( $sql ); + } + + function getTemplate( $templateid ) { + $st = $this->prepare( "select * from SensorTemplates where TemplateID=:TemplateID" ); + $st->execute( array( ":TemplateID"=>$templateid ) ); + $st->setFetchMode( PDO::FETCH_CLASS, "SensorTemplates" ); + + if ( $row = $st->fetch() ) { + foreach ( $row as $prop=>$val ) { + $this->$prop = $val; + } + return true; + } else { + return false; + } + } } class Moderators {