Here is the code!
==============================================================
<?php
include_once("services/view.php");
class DBView extends View
{
var $m_nID;
var $m_vtrFields;
var $m_vtrTables;
var $m_sWhereClause;
var $m_sGroupClause;
var $m_vtrResultTable;
var $m_sFileName;
var $m_sForcedAction;
var $m_vtrCleanUp;
function DBView()
{
parent::View();
}
function reset()
{
$this->m_nID = "";
$this->m_vtrResultTable = null;
$this->m_sFileName = "";
for ($nIndex = 0; $nIndex < count($this->m_vtrCleanUp); ++$nIndex)
{
unset($this->m_vtrCleanUp[$nIndex], $GLOBALS["Variables"]);
}
$GLOBALS["System"]->dumpVariables();
$this->m_vtrCleanUp = array();
}
function run()
{
$this->parseID();
$sAction = getValue("action", $GLOBALS["Variables"]);
if ($this->m_sForcedAction != "")
{
$sAction = $this->m_sForcedAction;
}
switch ($sAction)
{
case "delete":
$this->delete();
break;
case "update":
$this->update();
break;
case "save":
$this->save();
break;
case "send":
$this->select();
$this->send();
break;
default:
$this->select();
}
unset($GLOBALS["Variables"]["process_ids"]);
unset($GLOBALS["Variables"]["action"]);
unset($GLOBALS["Variables"]["order_by"]);
unset($GLOBALS["Variables"]["send_template"]);
$this->cleanUpBeforeView();
if (($sAction == 'delete') || ($sAction == 'update'))
{
$this->m_nID = "";
$this->restoreTableHeader();
$this->select();
}
$sResult = parent::run();
$this->reset();
return $sResult;
}
function send()
{
$sEMail = $this->getTag("email");
$objTemplateManager = new TemplateManager();
$objTemplateManager->init($GLOBALS["Config"]);
$objTemplateManager->preloadTemplates();
$sMessage = $objTemplateManager->renderTemplate(getValue("send_template", $GLOBALS["Variables"]), $this);
//$GLOBALS["Logger"]->write("EMail: $sEMail");
//$GLOBALS["Logger"]->write("Message: $sMessage");
if (mail($sEMail, "Automatic message from Solaris system", $sMessage) == TRUE)
{
$GLOBALS["Logger"]->write("E-mail has been sent");
}
else
{
$GLOBALS["Logger"]->write("Errors while sending E-mail");
}
}
function delete()
{
if ($this->m_nID != "")
{
$objDB = createDataBase();
$objDB->execute("delete from ".($this->m_vtrTables[0])." where id=".$this->m_nID);
}
}
function update()
{
$objDB = createDataBase();
if ($this->m_nID == "")
{
$this->m_vtrResultTable = $objDB->execute("select max(id) from ".$this->m_vtrTables[0]);
$this->m_nID = $this->m_vtrResultTable[0][0];
$this->m_nID++;
}
$objReplace = new ReplaceQuery($this->m_vtrTables[0]);
for ($nCount = 0; $nCount < count($this->m_vtrFields); ++$nCount)
{
if ($this->m_vtrFields[$nCount][2] == 0)
{
$objReplace->addNumericValue($this->m_vtrFields[$nCount][1], getValue($this->m_vtrFields[$nCount][0], $GLOBALS["Variables"], 0));
}
if ($this->m_vtrFields[$nCount][2] == 1)
{
$objReplace->addQuotedValue($this->m_vtrFields[$nCount][1], getValue($this->m_vtrFields[$nCount][0], $GLOBALS["Variables"]));
}
unset($GLOBALS["Variables"][$this->m_vtrFields[$nCount][0]]);
}
$objReplace->addNumericValue("id", $this->m_nID);
$this->onUpdate($objReplace);
$objDB->execute($objReplace->getQuery());
}
function cleanUpBeforeView()
{
//should be implemented in derived classes
}
function onUpdate(&$objReplace)
{
//any additional field updates can be added here
}
function onSelect()
{
//any additional field updates can be added here
}
function restoreTableHeader()
{
//should be implemented in derived classes
}
function select()
{
$this->onSelect();
$objDB = createDataBase();
$sOrderBy = getValue("order_by", $GLOBALS["Variables"]);
if ($sOrderBy != "")
{
$vtrFieldArray = getValue($sOrderBy, $this->m_vtrFields);
if (gettype($vtrFieldArray) == 'array')
{
$sOrderBy = " order by ".($vtrFieldArray[1])." asc";
}
else
{
$sOrderBy = "";
}
}
$sRequest = "select ";
for ($nCount = 0; $nCount < count($this->m_vtrFields); ++$nCount)
{
if ($nCount != 0)
{
$sRequest .= " , ";
}
$sRequest .= $this->m_vtrFields[$nCount][1];
}
$sRequest .= " from ";
for ($nCount = 0; $nCount < count($this->m_vtrTables); ++$nCount)
{
if ($nCount != 0)
{
$sRequest .= " , ";
}
$sRequest .= $this->m_vtrTables[$nCount];
}
if (($this->m_nID != "") || ($this->m_sWhereClause != ""))
{
$sRequest .= " where ";
}
if ($this->m_nID != "")
{
$sRequest .= " ".$this->m_vtrTables[0].".id=".$this->m_nID;
if ($this->m_sWhereClause != "")
{
$sRequest .= " and ";
}
}
if ($this->m_sGroupClause != "")
{
$sRequest .= $this->m_sGroupClause;
}
$sRequest .= $this->m_sWhereClause;
$sRequest .= $sOrderBy;
$this->m_vtrResultTable = $objDB->execute($sRequest);
}
function save()
{
$this->select();
$this->m_sFileName = getValue("temp", $GLOBALS["Variables"])."/save_file_".time().".csv";
$fp = fopen($this->m_sFileName, "w+");
for ($nCount = -1; $nCount < count($this->m_vtrResultTable); ++$nCount)
{
$bWasOutputted = false;
for ($nFieldCount = 0; $nFieldCount < count($this->m_vtrResultTable[0]); ++$nFieldCount)
{
if ($this->m_vtrFields[$nFieldCount][3] == "")
{
continue;
}
if ($bWasOutputted == true)
{
fputs($fp, ", ");
}
$bWasOutputted = true;
if ($nCount == -1)
{
fputs($fp, $this->m_vtrFields[$nFieldCount][3]);
}
else
{
fputs($fp, $this->m_vtrResultTable[$nCount][$nFieldCount]);
}
}
fputs($fp, "\n");
}
fclose($fp);
}
function parseID()
{
$vtrIDs = explode(",", getValue("process_ids", $GLOBALS["Variables"]));
$this->m_nID = trim($vtrIDs[0], "a..z ");
}
function getTag($sTagName)
{
if (count($this->m_vtrResultTable) == 0)
{
return "";
}
$sResult = "";
if (strpos($sTagName, "##") !== FALSE)
{
list($nRow, $nCol) = explode("##", $sTagName, 2);
$sResult = $this->m_vtrResultTable[$nRow][$nCol];
}
else if ($sTagName == "FileLink")
{
$sResult = $this->m_sFileName;
}
else if ($sTagName == "RowTotal")
{
$sResult = $this->getRowCount();
}
else
{
for ($nCount = 0; $nCount < count($this->m_vtrFields); ++$nCount)
{
if ($this->m_vtrFields[$nCount][0] == $sTagName)
{
$sResult = $this->m_vtrResultTable[0][$nCount];
}
}
}
return $sResult;
}
function getRowCount()
{
return count($this->m_vtrResultTable);
}
function addWhereClause(&$sAdditionalVar, $sFieldName, $sName, $bPushToCleanUp = false, $bQuoted = false, $sOperation = "=", $bInsertAfter = true)
{
$sValue = getValue($sName, $GLOBALS["Variables"]);
if ($sValue == "")
{
return false;
}
$sCondition = " $sFieldName $sOperation ".(($bQuoted == true)?("'$sValue'"):($sValue));
if ($bInsertAfter)
{
$this->m_sWhereClause .= " and $sCondition ";
$sAdditionalVar .= " and $sCondition ";
}
else
{
$this->m_sWhereClause = " $sCondition and " .$this->m_sWhereClause;
$sAdditionalVar = " $sCondition and " .$sAdditionalVar;
}
if ($bPushToCleanUp)
{
$this->m_vtrCleanUp[] = $sName;
}
return true;
}
}