<?php
/*=======================================================================
 *
 * inc_asset.inc.php
 *
 * Autor: Marc Vollmann
 *
 * Functions for asset handling
 *
 =======================================================================*/   


include_once ("../include/inc_dbdata.inc.php");


// Inserts an asset
// Possible array components: 
// ["as_obj_type" (e.g. => "cr")] and ["as_obj_id" (e.g. => $cr_id_new)] or ["as_key" (e.g. => "xxxx"), "as_value" (e.g. => "yyyy")] have to be set.
// ["as_location" => "22844", "as_weekdays" => "255"] are optional.
// ["asr_type" (e.g. => "owner"), "ref_type" (e.g. => "cr"), "ref_id" (e.g. => $cr_id_parent)] are optional, too. Additional insert into "assetrelation"
// Returns triple ([new asset ID or 0], [new asset relation ID or error ID], ["" or error text if error])
function insertAsset ($asArr, $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retArray = array(0,0,"");
	if (is_array($asArr)) :
	   // Check for existence in "asset"
	   if ($asArr["as_obj_type"] != "" && $asArr["as_obj_id"] != "" && is_numeric($asArr["as_obj_id"]) && !existsEntry("asset",array("as_obj_type", $asArr["as_obj_type"], "as_obj_id", $asArr["as_obj_id"]))) :
        insertStmt("asset", array("as_obj_type", $asArr["as_obj_type"], "as_obj_id", $asArr["as_obj_id"], "as_location", $asArr["as_location"], "as_weekdays", $asArr["as_weekdays"]), $dbConnection);
        $retArray[0] = getLastInsertId();
        // Insert asset relation if requested
        if ($retArray[0] > 0) :
           // If at least one component is set for inserting a relation then try it
           if ($asArr["ref_type"] != "" || $asArr["ref_id"] != "" || $asArr["asr_type"] != "") :
              if ($asArr["asr_type"] != "") :
                 if (($asArr["ref_type"] != "" && $asArr["ref_id"] != "" && is_numeric($asArr["ref_id"]))) :
                    // Check existence of asset to be referenced
                    $asIdRef = getFieldValueFromClause("asset", "as_id", "as_obj_type = '" . $asArr["ref_type"] . "' AND as_obj_id = '" . $asArr["ref_id"] . "'");
                    if ($asIdRef > 0) :
                       // insertStmt("assetrelation", array("as_id", $retArray[0], "as_id_ref", $asIdRef, "asr_type", $asArr["asr_type"], "asr_context", $asArr["asr_context"]), $dbConnection);
                       $asrArr = array("as_id" => $retArray[0], "as_id_ref" => $asIdRef, "asr_type" => $asArr["asr_type"], "asr_context" => $asArr["asr_context"]);
                       $retArray[1] = insertAssetrelation($asrArr, $dbConnection);
                    else :
                       $retArray[1] = -105; $retArray[2]= "Reference asset does not exist! Relation could not be inserted!";
                    endif;
                 else :
                    $retArray[1] = -104; $retArray[2]= "Reference type and reference ID of the object have to be specified both!";
                 endif;
              else :
                 $retArray[1] = -103; $retArray[2]= "Role (asset relation type) has to be specified!";
              endif;
           endif;
        else :
           $retArray[1] = -102; $retArray[2]= "Asset insertion failed! Critical error!";
        endif;
     else :
        $retArray[1] = -101; $retArray[2]= "Data to be inserted missing or asset does exist!";
     endif;
  else :
     $retArray[1] = -100; $retArray[2]= "No data specified!";
  endif;
  return $retArray;
}

// Removes an asset
function deleteAsset ($asArr, $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retArray = array(0,"");
	if (is_array($asArr)) :
	   $asId = $asArr["as_id"];
	   if (($asId == "" || !is_numeric($asId) || $asId <= "0") && $asArr["as_obj_type"] != "" && $asArr["as_obj_id"] != "" && is_numeric($asArr["as_obj_id"])) :
	      $asId = getFieldValueFromClause("asset", "as_id", "as_obj_type = '" . $asArr["as_obj_type"] . "' AND as_obj_id = '" . $asArr["as_obj_id"] . "'");
	   endif;
	   if ($asId != "" && is_numeric($asId) && $asId > "0") :
        // Check existence of assets being related to the asset to be deleted. In case of existence do NOT remove
        if (!existsEntry("assetrelation",array("as_id_ref", $asId))) :
           // Check existence of running or planned jobs
           if (!existsEntry("assetdisposition",array("as_id", $asId))) :
              deleteStmt("assetdisposition", "as_id = '" . $asId . "'", $dbConnection);
              deleteStmt("assetrelation", "as_id = '" . $asId . "'", $dbConnection);
              deleteStmt("asset", "as_id = '" . $asId . "'", $dbConnection);
           else :
              $retArray = array(-123, "Asset cannot be removed because at least one running or planned order does exist!");
           endif;
        else :
           $retArray = array(-122, "Asset cannot be removed because at least one asset relation does exist!");
        endif;
     else :
        $retArray = array(-121, "Asset to be removed is not specified!");
     endif;
  else :
     $retArray = array(-120, "No data specified!");
  endif;
  return $retArray;
}

// Removes an asset by ID
function deleteAssetByID ($asId, $dbConnection = "") {
  return deleteAsset(array("as_id" => $asId), $dbConnection);
}

// Removes an asset by object
function deleteAssetByObject ($objType, $objId, $dbConnection = "") {
  return deleteAsset(array("as_obj_type" => $objType, "objId" => $objId), $dbConnection);
}

// Gets an asset
function getAsset ($asArr, $opArr = array("asset"), $dbConnection = "") {
	global $db, $PHP_SELF, $dbData;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
  $retArray = array(0,"");
  if (is_array($asArr)) :
	   $asId = $asArr["as_id"];
	   if (($asId == "" || !is_numeric($asId) || $asId <= "0") && $asArr["as_obj_type"] != "" && $asArr["as_obj_id"] != "" && is_numeric($asArr["as_obj_id"])) :
	      $asId = getFieldValueFromClause("asset", "as_id", "as_obj_type = '" . $asArr["as_obj_type"] . "' AND as_obj_id = '" . $asArr["as_obj_id"] . "'");
	   endif;
     if ($asId != "" && is_numeric($asId) && $asId > "0") :
        $opArrLen = count($opArr);
        if ($opArrLen > 0) :
           for ($a = 0; $a < $opArrLen; $a++) :
               getDBData($opArr[$a], $asId, $dbConnection);
           endfor;
        endif;
        $retArray = array($asId, "");
     else :
        $retArray = array(-111, "Asset to be requested is not specified!");
     endif;
  else :
     $retArray = array(-110, "No data specified!");
  endif;
  return $retArray;
}

// Gets an asset by ID
function getAssetByID ($asId, $opArr, $dbConnection = "") {
  return getAsset(array("as_id" => $asId), $opArr, $dbConnection);
}

// Gets an asset by object
function getAssetByObject ($objType, $objId, $opArr, $dbConnection = "") {
  return getAsset(array("as_obj_type" => $objType, "objId" => $objId), $opArr, $dbConnection);
}

// Inserts an asset relation
function insertAssetRelation ($asrArr, $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retId = 0;
	if (is_array($asrArr)) :
	   if ($asrArr["as_id"] != "" && is_numeric($asrArr["as_id"]) && $asrArr["as_id_ref"] != "" && is_numeric($asrArr["as_id_ref"]) && $asrArr["asr_type"] != "") :
        // Check existence of asset IDs regarding reference to be inserted
        $asId    = getFieldValueFromId("asset","as_id",$asrArr["as_id"],"as_id");
        $asIdRef = getFieldValueFromId("asset","as_id",$asrArr["as_id_ref"],"as_id");
        if ($asId > 0 && $asIdRef > 0) :
           // Check for existence in "assetrelation"
           if (!existsEntry("assetrelation",array("as_id", $asId, "as_id_ref", $asIdRef, "asr_type", $asrArr["asr_type"]))) :
              insertStmt("assetrelation", array("as_id", $asId, "as_id_ref", $asIdRef, "asr_type", $asrArr["asr_type"], "asr_context", $asrArr["asr_context"]), $dbConnection);
           else :
              $retId = -203;  // Asset relation does exist!
           endif;
        else :
           $retId = -202;  // Reference asset does not exist! Relation could not be inserted!
        endif;
	   else :
	      $retId = -201;  // At least one information needed does not exist (asset ID, asset reference ID, relation type)!
	   endif;
  else :
     $retId = -200;  // No data specified!
  endif;
  return $retId;
}

// Removes an asset with all relations or a single relation
function deleteAssetRelation ($asId, $asIdRef, $asrType = "", $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retId = 0;
	if ($asId != "" && is_numeric($asId) && $asId > "0" && $asIdRef != "" && is_numeric($asIdRef) && $asIdRef > "0") :
     // Check existence of data triple ($asId, $asIdRef, $asrType)
     $tmpArr = array("as_id", $asId, "as_id_ref", $asIdRef);
     $whereClause = "as_id = '" . $asId . "' AND as_id_ref = '" . $asId . "'";
     if ($asrType != "") :
        array_push($tmpArr, "asr_type", $asrType);
        $whereClause .= " AND asr_type = '" . $asrType . "'";
     endif;
     if (existsEntry("assetrelation",$tmpArr)) :
        deleteStmt("assetrelation", $whereClause, $dbConnection);
     else :
        $retId = -221;  // Asset relation regarding data triple does not exist!
     endif;
  else :
     $retId = -220;  // No data specified!
  endif;
  return $retId;
}

// Removes a single asset relation
function deleteAssetSingleRelation ($asId, $asIdRef, $asrType, $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retId = 0;
	if ($asId != "" && is_numeric($asId) && $asId > "0" && $asIdRef != "" && is_numeric($asIdRef) && $asIdRef > "0" && $asrType != "") :
     $retId = deleteAssetRelation($asId, $asIdRef, $asrType, $dbConnection);  // Call with $asrType not being empty (!!!!)
  else :
     $retId = -220;  // Missing data or no data specified!
  endif;
  return $retId;
}

// Inserts an asset disposition entry
function insertAssetDisposition ($asdArr, $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retId = 0;
	if (is_array($asdArr)) :
	   if ($asdArr["as_id"] != "" && is_numeric($asdArr["as_id"]) && $asdArr["as_id"] > "0" && $asdArr["asd_time_from"] != "" && $asdArr["asd_time_to"] != "" && $asdArr["jb_id"] != "") :
        // Check existence of asset ID
        $asId = getFieldValueFromId("asset","as_id",$asdArr["as_id"],"as_id");
        if ($asId > 0) :
           // Check for identically(!) existence in "assetdisposition"
           if (!existsEntry("assetdisposition",array("as_id", $asdArr["as_id"], "asd_time_from", $asdArr["asd_time_from"], "asd_time_to", $asdArr["asd_time_to"], "jb_id", $asdArr["jb_id"]))) :
              insertStmt("assetdisposition", array("as_id", $asdArr["as_id"], "asd_time_from", $asdArr["asd_time_from"], "asd_time_to", $asdArr["asd_time_to"], "jb_id", $asdArr["jb_id"]), $dbConnection);
           else :
              $retId = -303;  // Identical asset disposition entry does exist!
           endif;
        else :
           $retId = -302;  // Specified asset does not exist! Disposition entry could not be inserted!
        endif;
	   else :
	      $retId = -301;  // At least one information needed does not exist (asset ID, time from, time to, location, order ID)!
	   endif;
  else :
     $retId = -300;  // No data specified!
  endif;
  return $retId;
}

// Removes an asset disposition entry
function deleteAssetDisposition ($asdArr, $dbConnection = "") {
	global $db, $PHP_SELF;
	if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
	$retId = 0;
	if (is_array($asdArr)) :
     $doRemove = false;
     $whereClause = "";
     if ($asdArr["asd_id"] != "" && is_numeric($asdArr["asd_id"]) && $asdArr["asd_id"] > "0") :
        // Check existence of asset IDs to be referenced
        $asdId = getFieldValueFromId("assetdisposition","asd_id",$asdArr["asd_id"],"asd_id");
        if ($asdId > 0) :
           $doRemove = true;
           $whereClause = "asd_id = '" . $asdId . "'";
        else :
           $retId = -321;  // Specified asset does not exist! Disposition entry could not be inserted!
        endif;
     endif;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	   if (!$doRemove && $asdArr["as_id"] != "" && is_numeric($asdArr["as_id"]) && $asdArr["as_id"] > "0" && $asdArr["asd_time_from"] != "" && $asdArr["asd_time_to"] != "") :
        // Check existence of asset ID
        $asId = getFieldValueFromId("asset","as_id",$asdArr["as_id"],"as_id");
        if ($asId > 0) :
           // Check for identically(!) existence in "assetdisposition"
           if (!existsEntry("assetdisposition",array("as_id", $asdArr["as_id"], "asd_time_from", $asdArr["asd_time_from"], "asd_time_to", $asdArr["asd_time_to"], "jb_id", $asdArr["jb_id"]))) :
              insertStmt("assetdisposition", array("as_id", $asdArr["as_id"], "asd_time_from", $asdArr["asd_time_from"], "asd_time_to", $asdArr["asd_time_to"], "jb_id", $asdArr["jb_id"]), $dbConnection);
           else :
              $retId = -324;  // Identical asset disposition entry does exist!
           endif;
        else :
           $retId = -323;  // Specified asset does not exist! Disposition entry could not be inserted!
        endif;
	   else :
	      $retId = -322;  // At least one information needed does not exist (asset ID, time from, time to, location, order ID)!
	   endif;
  else :
     $retId = -320;  // No data specified!
  endif;
  return $retId;
}

/*
CREATE TABLE asset (
  as_id int(10) NOT NULL auto_increment,
  as_obj_type varchar(10) default NULL,
  as_obj_id int(10) NOT NULL,
  as_key varchar(50) NOT NULL,
  as_value text NOT NULL,
  as_context varchar(50) NOT NULL,
  as_location varchar(10) NOT NULL,
  as_weekdays varchar(7) NOT NULL,
  as_modify timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (as_id),
  KEY obj_type_id (as_obj_type,as_obj_id)
) ENGINE=InnoDB;

CREATE TABLE assetrelation (
  as_id int(10) NOT NULL auto_increment,
  as_id_ref int(10) NOT NULL,
  asr_type varchar(20) NOT NULL,
  asr_context varchar(50) NOT NULL,
  asr_modify timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  KEY as_id (as_id),
  KEY as_id_ref (as_id_ref)
) ENGINE=InnoDB;

CREATE TABLE assetdisposition (
  asd_id int(10) NOT NULL auto_increment,
  as_id int(10) NOT NULL,
  asd_time_from datetime NOT NULL,
  asd_time_to datetime NOT NULL,
  asd_location varchar(20) NOT NULL,
  jb_id int(10) NOT NULL,
  PRIMARY KEY  (asd_id)
) ENGINE=InnoDB;
*/
?>