Files
luos/workflow/engine/classes/ReportTables.php

975 lines
47 KiB
PHP
Raw Normal View History

2010-12-02 23:34:41 +00:00
<?php
/**
2017-08-14 10:56:14 -04:00
* ReportTables - Report tables
*/
class ReportTables
2010-12-02 23:34:41 +00:00
{
2017-12-04 13:25:35 +00:00
private $aDef = array(
'mysql' => array(
'number' => 'DOUBLE',
'char' => 'VARCHAR(255)',
'text' => 'TEXT',
'date' => 'DATETIME'
),
'pgsql' => array(
'number' => 'DOUBLE',
'char' => 'VARCHAR(255)',
'text' => 'TEXT',
'date' => 'DATETIME'
),
'mssql' => array(
'number' => 'FLOAT',
'char' => 'NVARCHAR(255)',
'text' => 'TEXT',
'date' => 'CHAR(19)'
) /* Changed DATETIME CHAR(19) for compatibility issues. */
);
2012-10-09 13:21:00 -04:00
private $sPrefix = '';
2012-10-09 13:21:00 -04:00
/**
* Function deleteAllReportVars
* This function delete all reports
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $$sRepTabUid
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-08-14 10:56:14 -04:00
public function deleteAllReportVars($sRepTabUid = '')
2012-10-09 13:21:00 -04:00
{
try {
2017-08-14 10:56:14 -04:00
$oCriteria = new Criteria('workflow');
$oCriteria->add(ReportVarPeer::REP_TAB_UID, $sRepTabUid);
ReportVarPeer::doDelete($oCriteria);
2012-10-09 13:21:00 -04:00
} catch (Exception $oError) {
throw ($oError);
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
/**
* Function prepareQuery
* This function removes the table
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $sTableName Table name
* @param string $sConnection Conexion
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-08-14 10:56:14 -04:00
public function dropTable($sTableName, $sConnection = 'report')
2012-10-09 13:21:00 -04:00
{
$sTableName = $this->sPrefix . $sTableName;
//we have to do the propel connection
2017-08-14 10:56:14 -04:00
$PropelDatabase = $this->chooseDB($sConnection);
$con = Propel::getConnection($PropelDatabase);
2012-10-09 13:21:00 -04:00
$stmt = $con->createStatement();
try {
switch (DB_ADAPTER) {
case 'mysql':
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery('DROP TABLE IF EXISTS `' . $sTableName . '`');
2010-12-02 23:34:41 +00:00
break;
2012-10-09 13:21:00 -04:00
case 'mssql':
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery("IF OBJECT_ID (N'" . $sTableName . "', N'U') IS NOT NULL
2017-12-04 13:25:35 +00:00
DROP TABLE [" . $sTableName . "]");
2010-12-02 23:34:41 +00:00
break;
}
2012-10-09 13:21:00 -04:00
} catch (Exception $oError) {
throw ($oError);
}
}
/**
* Function createTable
* This Function creates the table
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $sTableName Table name
* @param string $sConnection Connection name
* @param string $sType
* @param array $aFields
* @param string $bDefaultFields
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-12-04 13:25:35 +00:00
public function createTable(
$sTableName,
$sConnection = 'report',
$sType = 'NORMAL',
$aFields = array(),
$bDefaultFields = true
)
2012-10-09 13:21:00 -04:00
{
$sTableName = $this->sPrefix . $sTableName;
//we have to do the propel connection
2017-08-14 10:56:14 -04:00
$PropelDatabase = $this->chooseDB($sConnection);
$con = Propel::getConnection($PropelDatabase);
2012-10-09 13:21:00 -04:00
$stmt = $con->createStatement();
try {
switch (DB_ADAPTER) {
case 'mysql':
$sQuery = 'CREATE TABLE IF NOT EXISTS `' . $sTableName . '` (';
if ($bDefaultFields) {
$sQuery .= "`APP_UID` VARCHAR(32) NOT NULL DEFAULT '',`APP_NUMBER` INT NOT NULL,";
if ($sType == 'GRID') {
$sQuery .= "`ROW` INT NOT NULL,";
2010-12-02 23:34:41 +00:00
}
}
foreach ($aFields as $aField) {
2012-10-09 13:21:00 -04:00
switch ($aField['sType']) {
case 'number':
$sQuery .= '`' . $aField['sFieldName'] . '` ' . $this->aDef['mysql'][$aField['sType']] . " NOT NULL DEFAULT '0',";
break;
case 'char':
$sQuery .= '`' . $aField['sFieldName'] . '` ' . $this->aDef['mysql'][$aField['sType']] . " NOT NULL DEFAULT '',";
break;
case 'text':
$sQuery .= '`' . $aField['sFieldName'] . '` ' . $this->aDef['mysql'][$aField['sType']] . " ,";
break;
case 'date':
$sQuery .= '`' . $aField['sFieldName'] . '` ' . $this->aDef['mysql'][$aField['sType']] . " NULL,";
break;
}
}
if ($bDefaultFields) {
2017-12-04 13:25:35 +00:00
$sQuery .= 'PRIMARY KEY (APP_UID' . ($sType === 'GRID' ? ',ROW' : '') . ')) ';
2012-10-09 13:21:00 -04:00
}
$sQuery .= ' DEFAULT CHARSET=utf8;';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
break;
case 'mssql':
$sQuery = 'CREATE TABLE [' . $sTableName . '] (';
if ($bDefaultFields) {
$sQuery .= "[APP_UID] VARCHAR(32) NOT NULL DEFAULT '', [APP_NUMBER] INT NOT NULL,";
if ($sType == 'GRID') {
$sQuery .= "[ROW] INT NOT NULL,";
}
}
foreach ($aFields as $aField) {
2012-10-09 13:21:00 -04:00
switch ($aField['sType']) {
case 'number':
$sQuery .= '[' . $aField['sFieldName'] . '] ' . $this->aDef['mssql'][$aField['sType']] . " NOT NULL DEFAULT '0',";
break;
case 'char':
$sQuery .= '[' . $aField['sFieldName'] . '] ' . $this->aDef['mssql'][$aField['sType']] . " NOT NULL DEFAULT '',";
break;
case 'text':
$sQuery .= '[' . $aField['sFieldName'] . '] ' . $this->aDef['mssql'][$aField['sType']] . " NOT NULL DEFAULT '',";
break;
case 'date':
$sQuery .= '[' . $aField['sFieldName'] . '] ' . $this->aDef['mssql'][$aField['sType']] . " NULL,";
break;
}
}
if ($bDefaultFields) {
$sQuery .= 'PRIMARY KEY (APP_UID' . ($sType == 'GRID' ? ',ROW' : '') . ')) ';
} else {
$sQuery .= ' ';
}
2012-10-09 13:21:00 -04:00
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
break;
}
2012-10-09 13:21:00 -04:00
} catch (Exception $oError) {
throw ($oError);
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
/**
* Function populateTable
* This Function fills the table
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $sTableName Table name
* @param string $sConnection Connection name
* @param string $sType
* @param array $aFields
* @param string $sProcessUid
* @param string $sGrid
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-12-04 13:25:35 +00:00
public function populateTable(
$sTableName,
$sConnection = 'report',
$sType = 'NORMAL',
$aFields = array(),
$sProcessUid = '',
$sGrid = ''
)
2012-10-09 13:21:00 -04:00
{
$sTableName = $this->sPrefix . $sTableName;
//we have to do the propel connection
2017-08-14 10:56:14 -04:00
$PropelDatabase = $this->chooseDB($sConnection);
$con = Propel::getConnection($PropelDatabase);
2012-10-09 13:21:00 -04:00
$stmt = $con->createStatement();
if ($sType == 'GRID') {
2017-08-14 10:56:14 -04:00
$aAux = explode('-', $sGrid);
2012-10-09 13:21:00 -04:00
$sGrid = $aAux[0];
}
try {
switch (DB_ADAPTER) {
case 'mysql':
//select cases for this Process, ordered by APP_NUMBER
2017-08-14 10:56:14 -04:00
$oCriteria = new Criteria('workflow');
$oCriteria->add(ApplicationPeer::PRO_UID, $sProcessUid);
$oCriteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER);
$oDataset = ApplicationPeer::doSelectRS($oCriteria);
$oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
2012-10-09 13:21:00 -04:00
$oDataset->next();
while ($aRow = $oDataset->getRow()) {
2017-08-14 10:56:14 -04:00
$aData = unserialize($aRow['APP_DATA']);
2012-10-09 13:21:00 -04:00
//delete previous record from this report table ( previous records in case this is a grid )
$deleteSql = 'DELETE FROM `' . $sTableName . "` WHERE APP_UID = '" . $aRow['APP_UID'] . "'";
2017-08-14 10:56:14 -04:00
$rsDel = $stmt->executeQuery($deleteSql);
2012-10-09 13:21:00 -04:00
if ($sType == 'NORMAL') {
$sQuery = 'INSERT INTO `' . $sTableName . '` (';
$sQuery .= '`APP_UID`,`APP_NUMBER`';
foreach ($aFields as $aField) {
$sQuery .= ',`' . $aField['sFieldName'] . '`';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER'];
2012-10-09 13:21:00 -04:00
foreach ($aFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aData[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aData[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aData[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aData[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aData[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aData[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$value = (isset($aData[$aField['sFieldName']]) && trim($aData[$aField['sFieldName']])) != '' ? "'" . $aData[$aField['sFieldName']] . "'" : 'NULL';
2012-10-09 13:21:00 -04:00
$sQuery .= "," . $value;
break;
}
}
$sQuery .= ')';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
} else {
2017-08-14 10:56:14 -04:00
if (isset($aData[$sGrid])) {
2012-10-09 13:21:00 -04:00
foreach ($aData[$sGrid] as $iRow => $aGridRow) {
$sQuery = 'INSERT INTO `' . $sTableName . '` (';
$sQuery .= '`APP_UID`,`APP_NUMBER`,`ROW`';
foreach ($aFields as $aField) {
$sQuery .= ',`' . $aField['sFieldName'] . '`';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER'] . ',' . $iRow;
2012-10-09 13:21:00 -04:00
foreach ($aFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aGridRow[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aGridRow[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aGridRow[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aGridRow[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aGridRow[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$value = (isset($aGridRow[$aField['sFieldName']]) && trim($aGridRow[$aField['sFieldName']])) != '' ? "'" . $aGridRow[$aField['sFieldName']] . "'" : 'NULL';
2012-10-09 13:21:00 -04:00
$sQuery .= "," . $value;
break;
}
}
$sQuery .= ')';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
}
}
}
$oDataset->next();
}
break;
/**
* For SQLServer code
*/
case 'mssql':
2017-08-14 10:56:14 -04:00
$oCriteria = new Criteria('workflow');
$oCriteria->add(ApplicationPeer::PRO_UID, $sProcessUid);
$oCriteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER);
$oDataset = ApplicationPeer::doSelectRS($oCriteria);
$oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
2012-10-09 13:21:00 -04:00
$oDataset->next();
while ($aRow = $oDataset->getRow()) {
2017-08-14 10:56:14 -04:00
$aData = unserialize($aRow['APP_DATA']);
2017-12-04 13:25:35 +00:00
//verify use mssql
mysqli_query(
$con->getResource(),
'DELETE FROM [' . $sTableName . "] WHERE APP_UID = '" . $aRow['APP_UID'] . "'"
);
2012-10-09 13:21:00 -04:00
if ($sType == 'NORMAL') {
$sQuery = 'INSERT INTO [' . $sTableName . '] (';
$sQuery .= '[APP_UID],[APP_NUMBER]';
foreach ($aFields as $aField) {
$sQuery .= ',[' . $aField['sFieldName'] . ']';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER'];
2012-10-09 13:21:00 -04:00
foreach ($aFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aData[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aData[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aData[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aData[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aData[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aData[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$sQuery .= ",'" . (isset($aData[$aField['sFieldName']]) ? $aData[$aField['sFieldName']] : '') . "'";
2012-10-09 13:21:00 -04:00
break;
}
}
$sQuery .= ')';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
} else {
2017-08-14 10:56:14 -04:00
if (isset($aData[$sGrid])) {
2012-10-09 13:21:00 -04:00
foreach ($aData[$sGrid] as $iRow => $aGridRow) {
$sQuery = 'INSERT INTO [' . $sTableName . '] (';
$sQuery .= '`APP_UID`,`APP_NUMBER`,`ROW`';
foreach ($aFields as $aField) {
$sQuery .= ',[' . $aField['sFieldName'] . ']';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $aRow['APP_UID'] . "'," . (int)$aRow['APP_NUMBER'] . ',' . $iRow;
2012-10-09 13:21:00 -04:00
foreach ($aFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aGridRow[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aGridRow[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aGridRow[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aGridRow[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aGridRow[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? $aGridRow[$aField['sFieldName']] : '') . "'";
2012-10-09 13:21:00 -04:00
break;
}
}
$sQuery .= ')';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
}
}
}
$oDataset->next();
}
break;
}
} catch (Exception $oError) {
throw ($oError);
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
/**
* Function getTableVars
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $sRepTabUid
* @param boolean $bWhitType
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-08-14 10:56:14 -04:00
public function getTableVars($sRepTabUid, $bWhitType = false)
2012-10-09 13:21:00 -04:00
{
try {
2017-08-14 10:56:14 -04:00
$oCriteria = new Criteria('workflow');
$oCriteria->add(ReportVarPeer::REP_TAB_UID, $sRepTabUid);
$oDataset = ReportVarPeer::doSelectRS($oCriteria);
$oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
2012-10-09 13:21:00 -04:00
$oDataset->next();
2017-08-14 10:56:14 -04:00
$aVars = array();
$aImportedVars = array(); //This array will help to control if the variable already exist
2012-10-09 13:21:00 -04:00
while ($aRow = $oDataset->getRow()) {
if ($bWhitType) {
2017-08-14 10:56:14 -04:00
if (!in_array($aRow['REP_VAR_NAME'], $aImportedVars)) {
2012-10-09 13:21:00 -04:00
$aImportedVars[] = $aRow['REP_VAR_NAME'];
2017-12-04 13:25:35 +00:00
$aVars[] = array(
'sFieldName' => $aRow['REP_VAR_NAME'],
'sType' => $aRow['REP_VAR_TYPE']
2012-10-09 13:21:00 -04:00
);
}
} else {
$aVars[] = $aRow['REP_VAR_NAME'];
}
2012-10-09 13:21:00 -04:00
$oDataset->next();
}
2012-10-09 13:21:00 -04:00
return $aVars;
} catch (Exception $oError) {
throw ($oError);
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
/**
* Function deleteReportTable
* This Function deletes report table
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $sRepTabUid
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-08-14 10:56:14 -04:00
public function deleteReportTable($sRepTabUid)
2012-10-09 13:21:00 -04:00
{
try {
$oReportTable = new ReportTable();
2017-08-14 10:56:14 -04:00
$aFields = $oReportTable->load($sRepTabUid);
if (!(empty($aFields))) {
$this->dropTable($aFields['REP_TAB_NAME'], $aFields['REP_TAB_CONNECTION']);
$oCriteria = new Criteria('workflow');
$oCriteria->add(ReportVarPeer::REP_TAB_UID, $sRepTabUid);
$oDataset = ReportVarPeer::doDelete($oCriteria);
$oReportTable->remove($sRepTabUid);
2012-10-09 13:21:00 -04:00
}
} catch (Exception $oError) {
throw ($oError);
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
/**
* Function getSplitDate
* This function gets the split date
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param date $date
* @param string $mask
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return array
*/
2017-08-14 10:56:14 -04:00
public function getSplitDate($date, $mask)
2012-10-09 13:21:00 -04:00
{
$sw1 = false;
2017-08-14 10:56:14 -04:00
for ($i = 0; $i < 3; $i++) {
$item = substr($mask, $i * 2, 1);
2012-10-09 13:21:00 -04:00
switch ($item) {
case 'Y':
switch ($i) {
case 0:
2017-08-14 10:56:14 -04:00
$d1 = substr($date, 0, 4);
2012-10-09 13:21:00 -04:00
break;
case 1:
2017-08-14 10:56:14 -04:00
$d1 = substr($date, 3, 4);
2012-10-09 13:21:00 -04:00
break;
case 2:
2017-08-14 10:56:14 -04:00
$d1 = substr($date, 6, 4);
2012-10-09 13:21:00 -04:00
break;
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
$sw1 = true;
break;
case 'y':
switch ($i) {
case 0:
2017-08-14 10:56:14 -04:00
$d1 = substr($date, 0, 2);
2012-10-09 13:21:00 -04:00
break;
case 1:
2017-08-14 10:56:14 -04:00
$d1 = substr($date, 3, 2);
2012-10-09 13:21:00 -04:00
break;
case 2:
2017-08-14 10:56:14 -04:00
$d1 = substr($date, 6, 2);
2012-10-09 13:21:00 -04:00
break;
}
2010-12-02 23:34:41 +00:00
break;
2012-10-09 13:21:00 -04:00
case 'm':
switch ($i) {
case 0:
2017-08-14 10:56:14 -04:00
$d2 = substr($date, 0, 2);
2012-10-09 13:21:00 -04:00
break;
case 1:
2017-08-14 10:56:14 -04:00
$d2 = ($sw1) ? substr($date, 5, 2) : substr($date, 3, 2);
2012-10-09 13:21:00 -04:00
break;
case 2:
2017-08-14 10:56:14 -04:00
$d2 = ($sw1) ? substr($date, 8, 2) : substr($date, 5, 2);
2012-10-09 13:21:00 -04:00
break;
2010-12-02 23:34:41 +00:00
}
break;
2012-10-09 13:21:00 -04:00
case 'd':
switch ($i) {
case 0:
2017-08-14 10:56:14 -04:00
$d3 = substr($date, 0, 2);
2012-10-09 13:21:00 -04:00
break;
case 1:
2017-08-14 10:56:14 -04:00
$d3 = ($sw1) ? substr($date, 5, 2) : substr($date, 3, 2);
2012-10-09 13:21:00 -04:00
break;
case 2:
2017-08-14 10:56:14 -04:00
$d3 = ($sw1) ? substr($date, 8, 2) : substr($date, 5, 2);
2012-10-09 13:21:00 -04:00
break;
}
2010-12-02 23:34:41 +00:00
break;
}
2012-10-09 13:21:00 -04:00
}
2017-12-04 13:25:35 +00:00
return array(
isset($d1) ? $d1 : '',
isset($d2) ? $d2 : '',
isset($d3) ? $d3 : ''
2012-10-09 13:21:00 -04:00
);
}
2010-12-02 23:34:41 +00:00
2012-10-09 13:21:00 -04:00
/**
* Function getFormatDate
* This function returns the date formated
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param date $sDate
* @param date $sMask
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return date
*/
2017-08-14 10:56:14 -04:00
public function getFormatDate($sDate, $sMask)
2012-10-09 13:21:00 -04:00
{
2017-08-14 10:56:14 -04:00
$dateTime = explode(" ", $sDate); //To accept the Hour part
$aDate = explode('-', str_replace("/", "-", $dateTime[0]));
2012-10-09 13:21:00 -04:00
$bResult = true;
foreach ($aDate as $sDate) {
2017-08-14 10:56:14 -04:00
if (!is_numeric($sDate)) {
2012-10-09 13:21:00 -04:00
$bResult = false;
break;
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
}
if ($sMask != '') {
2017-08-14 10:56:14 -04:00
$aDate = $this->getSplitDate($dateTime[0], $sMask);
$aDate[0] = ($aDate[0] == '') ? date('Y') : $aDate[0];
$aDate[1] = ($aDate[1] == '') ? date('m') : $aDate[1];
$aDate[2] = ($aDate[2] == '') ? date('d') : $aDate[2];
if (checkdate($aDate[1], $aDate[2], $aDate[0])) {
2012-10-09 13:21:00 -04:00
} else {
return false;
}
2012-10-09 13:21:00 -04:00
}
$sDateC = '';
2017-08-14 10:56:14 -04:00
for ($i = 0; $i < count($aDate); $i++) {
2012-10-09 13:21:00 -04:00
$sDateC .= (($i == 0) ? "" : "-") . $aDate[$i];
}
return ($sDateC);
}
/**
* Function updateTables
* This function updated the Report Tables
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $sProcessUid
* @param string $sApplicationUid
* @param string $iApplicationNumber
* @param string $aFields
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return void
*/
2017-08-14 10:56:14 -04:00
public function updateTables($sProcessUid, $sApplicationUid, $iApplicationNumber, $aFields)
2012-10-09 13:21:00 -04:00
{
try {
2015-12-02 16:14:54 -04:00
$c = new Criteria('workflow');
$c->addSelectColumn(BpmnProjectPeer::PRJ_UID);
$c->add(BpmnProjectPeer::PRJ_UID, $sProcessUid, Criteria::EQUAL);
$ds = ProcessPeer::doSelectRS($c);
$ds->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$ds->next();
$row = $ds->getRow();
$isBpmn = isset($row['PRJ_UID']);
if (!class_exists('ReportTablePeer')) {
2017-08-14 10:56:14 -04:00
require_once 'classes/model/ReportTablePeer.php';
}
2012-10-09 13:21:00 -04:00
//get all Active Report Tables
2017-08-14 10:56:14 -04:00
$oCriteria = new Criteria('workflow');
$oCriteria->add(ReportTablePeer::PRO_UID, $sProcessUid);
$oCriteria->add(ReportTablePeer::REP_TAB_STATUS, 'ACTIVE');
$oDataset = ReportTablePeer::doSelectRS($oCriteria);
$oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
2012-10-09 13:21:00 -04:00
$oDataset->next();
2017-08-14 10:56:14 -04:00
$aVars = array();
2012-10-09 13:21:00 -04:00
while ($aRow = $oDataset->getRow()) {
$aRow['REP_TAB_NAME'] = $this->sPrefix . $aRow['REP_TAB_NAME'];
2017-08-14 10:56:14 -04:00
$PropelDatabase = $this->chooseDB($aRow['REP_TAB_CONNECTION']);
$con = Propel::getConnection($PropelDatabase);
2017-12-04 13:25:35 +00:00
$con->getResource();
2012-10-09 13:21:00 -04:00
$stmt = $con->createStatement();
switch (DB_ADAPTER) {
case 'mysql':
2017-08-14 10:56:14 -04:00
$aTableFields = $this->getTableVars($aRow['REP_TAB_UID'], true);
2012-10-09 13:21:00 -04:00
if ($aRow['REP_TAB_TYPE'] == 'NORMAL') {
$sqlExists = "SELECT * FROM `" . $aRow['REP_TAB_NAME'] . "` WHERE APP_UID = '" . $sApplicationUid . "'";
2017-08-14 10:56:14 -04:00
$rsExists = $stmt->executeQuery($sqlExists, ResultSet::FETCHMODE_ASSOC);
2012-10-09 13:21:00 -04:00
$rsExists->next();
$aRow2 = $rsExists->getRow();
2017-08-14 10:56:14 -04:00
if (is_array($aRow2)) {
2012-10-09 13:21:00 -04:00
$sQuery = 'UPDATE `' . $aRow['REP_TAB_NAME'] . '` SET ';
foreach ($aTableFields as $aField) {
$sQuery .= '`' . $aField['sFieldName'] . '` = ';
2017-08-14 10:56:14 -04:00
if (!$isBpmn && !isset($aFields[$aField['sFieldName']])) {
foreach ($aFields as $row) {
if (is_array($row) && isset($row[count($row)])) {
$aFields = $row[count($row)];
}
}
}
2012-10-09 13:21:00 -04:00
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= (isset($aFields[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aFields[$aField['sFieldName']]
) : '0') . ',';
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aFields[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aFields[$aField['sFieldName']] = '';
2015-11-20 14:10:46 -04:00
}
2017-08-14 10:56:14 -04:00
if (!isset($aFields[$aField['sFieldName'] . '_label'])) {
2015-11-20 14:10:46 -04:00
$aFields[$aField['sFieldName'] . '_label'] = '';
}
2017-08-14 10:56:14 -04:00
if (is_array($aFields[$aField['sFieldName']])) {
$sQuery .= "'" . (isset($aFields[$aField['sFieldName']]) ? $aFields[$aField['sFieldName']][0] : '') . "',";
} else {
2017-12-04 13:25:35 +00:00
$sQuery .= '\'' . ((isset($aFields[$aField['sFieldName']])) ? mysqli_real_escape_string(
$con->getResource(),
$aFields[$aField['sFieldName']]
) : '') . '\',';
}
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$mysqlDate = (isset($aFields[$aField['sFieldName']]) ? $aFields[$aField['sFieldName']] : '');
2012-10-09 13:21:00 -04:00
if ($mysqlDate != '') {
2017-08-14 10:56:14 -04:00
$mysqlDate = str_replace('/', '-', $mysqlDate);
$mysqlDate = date('Y-m-d', strtotime($mysqlDate));
2012-10-09 13:21:00 -04:00
}
2017-08-14 10:56:14 -04:00
$value = trim($mysqlDate) != '' ? "'" . $mysqlDate . "'" : 'NULL';
2012-10-09 13:21:00 -04:00
$sQuery .= $value . ",";
break;
}
}
2017-08-14 10:56:14 -04:00
$sQuery = substr($sQuery, 0, -1);
2012-10-09 13:21:00 -04:00
$sQuery .= " WHERE APP_UID = '" . $sApplicationUid . "'";
2017-09-21 13:40:29 -04:00
//Only we will to executeQuery if we have additional field
if (count($aTableFields) > 0) {
try {
$rs = $stmt->executeQuery($sQuery);
} catch (Exception $e) {
2017-12-04 13:25:35 +00:00
Bootstrap::registerMonolog(
'sqlExecution',
400,
'Sql Execution',
['sql' => $sQuery, 'error' => $e->getMessage()],
config("system.workspace"),
'processmaker.log'
);
2017-09-21 13:40:29 -04:00
}
}
2012-10-09 13:21:00 -04:00
} else {
$sQuery = 'INSERT INTO `' . $aRow['REP_TAB_NAME'] . '` (';
$sQuery .= '`APP_UID`,`APP_NUMBER`';
foreach ($aTableFields as $aField) {
$sQuery .= ',`' . $aField['sFieldName'] . '`';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $sApplicationUid . "'," . (int)$iApplicationNumber;
2012-10-09 13:21:00 -04:00
foreach ($aTableFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aFields[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aFields[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aFields[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aFields[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aFields[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aFields[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$mysqlDate = (isset($aFields[$aField['sFieldName']]) ? $aFields[$aField['sFieldName']] : '');
2012-10-09 13:21:00 -04:00
if ($mysqlDate != '') {
2017-08-14 10:56:14 -04:00
$mysqlDate = str_replace('/', '-', $mysqlDate);
$mysqlDate = date('Y-m-d', strtotime($mysqlDate));
2012-10-09 13:21:00 -04:00
}
2017-08-14 10:56:14 -04:00
$value = trim($mysqlDate) != '' ? "'" . $mysqlDate . "'" : 'NULL';
2012-10-09 13:21:00 -04:00
$sQuery .= "," . $value;
break;
}
}
$sQuery .= ')';
2017-09-21 13:40:29 -04:00
try {
$rs = $stmt->executeQuery($sQuery);
} catch (Exception $e) {
2017-12-04 13:25:35 +00:00
Bootstrap::registerMonolog(
'sqlExecution',
400,
'Sql Execution',
['sql' => $sQuery, 'error' => $e->getMessage()],
config("system.workspace"),
'processmaker.log'
);
2017-09-21 13:40:29 -04:00
}
2012-10-09 13:21:00 -04:00
}
} else {
//remove old rows from database
$sqlDelete = 'DELETE FROM `' . $aRow['REP_TAB_NAME'] . "` WHERE APP_UID = '" . $sApplicationUid . "'";
2017-08-14 10:56:14 -04:00
$rsDelete = $stmt->executeQuery($sqlDelete);
2012-10-09 13:21:00 -04:00
2017-08-14 10:56:14 -04:00
$aAux = explode('-', $aRow['REP_TAB_GRID']);
if (isset($aFields[$aAux[0]])) {
if (is_array($aFields[$aAux[0]])) {
foreach ($aFields[$aAux[0]] as $iRow => $aGridRow) {
$sQuery = 'INSERT INTO `' . $aRow['REP_TAB_NAME'] . '` (';
$sQuery .= '`APP_UID`,`APP_NUMBER`,`ROW`';
foreach ($aTableFields as $aField) {
$sQuery .= ',`' . $aField['sFieldName'] . '`';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $sApplicationUid . "'," . (int)$iApplicationNumber . ',' . $iRow;
foreach ($aTableFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aGridRow[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aGridRow[$aField['sFieldName']]
) : '0');
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aGridRow[$aField['sFieldName']])) {
$aGridRow[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aGridRow[$aField['sFieldName']]
) : '') . "'";
break;
case 'date':
2017-08-14 10:56:14 -04:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? $aGridRow[$aField['sFieldName']] : '') . "'";
break;
}
2012-10-09 13:21:00 -04:00
}
$sQuery .= ')';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
}
}
}
}
2012-10-09 13:21:00 -04:00
break;
/**
* For SQLServer code
*/
case 'mssql':
2017-08-14 10:56:14 -04:00
$aTableFields = $this->getTableVars($aRow['REP_TAB_UID'], true);
2012-10-09 13:21:00 -04:00
if ($aRow['REP_TAB_TYPE'] == 'NORMAL') {
2017-08-14 10:56:14 -04:00
$oDataset2 = mssql_query("SELECT * FROM [" . $aRow['REP_TAB_NAME'] . "] WHERE APP_UID = '" . $sApplicationUid . "'");
if ($aRow2 = mssql_fetch_row($oDataset2)) {
2012-10-09 13:21:00 -04:00
$sQuery = 'UPDATE [' . $aRow['REP_TAB_NAME'] . '] SET ';
foreach ($aTableFields as $aField) {
$sQuery .= '[' . $aField['sFieldName'] . '] = ';
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= (isset($aFields[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aFields[$aField['sFieldName']]
) : '0') . ',';
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aFields[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aFields[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= "'" . (isset($aFields[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aFields[$aField['sFieldName']]
) : '') . "',";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$sQuery .= "'" . (isset($aFields[$aField['sFieldName']]) ? $aFields[$aField['sFieldName']] : '') . "',";
2012-10-09 13:21:00 -04:00
break;
}
}
2017-08-14 10:56:14 -04:00
$sQuery = substr($sQuery, 0, -1);
2012-10-09 13:21:00 -04:00
$sQuery .= " WHERE APP_UID = '" . $sApplicationUid . "'";
} else {
$sQuery = 'INSERT INTO [' . $aRow['REP_TAB_NAME'] . '] (';
$sQuery .= '[APP_UID],[APP_NUMBER]';
foreach ($aTableFields as $aField) {
$sQuery .= ',[' . $aField['sFieldName'] . ']';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $sApplicationUid . "'," . (int)$iApplicationNumber;
2012-10-09 13:21:00 -04:00
foreach ($aTableFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aFields[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aFields[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aFields[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aFields[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aFields[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aFields[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$sQuery .= ",'" . (isset($aFields[$aField['sFieldName']]) ? $aFields[$aField['sFieldName']] : '') . "'";
2012-10-09 13:21:00 -04:00
break;
}
}
$sQuery .= ')';
}
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
} else {
2017-12-04 13:25:35 +00:00
//Verify use in mssql
mysqli_query(
$con->getResource(),
'DELETE FROM [' . $aRow['REP_TAB_NAME'] . "] WHERE APP_UID = '" . $sApplicationUid . "'"
);
2017-08-14 10:56:14 -04:00
$aAux = explode('-', $aRow['REP_TAB_GRID']);
if (isset($aFields[$aAux[0]])) {
2012-10-09 13:21:00 -04:00
foreach ($aFields[$aAux[0]] as $iRow => $aGridRow) {
$sQuery = 'INSERT INTO [' . $aRow['REP_TAB_NAME'] . '] (';
$sQuery .= '[APP_UID],[APP_NUMBER],[ROW]';
foreach ($aTableFields as $aField) {
$sQuery .= ',[' . $aField['sFieldName'] . ']';
}
2017-08-14 10:56:14 -04:00
$sQuery .= ") VALUES ('" . $sApplicationUid . "'," . (int)$iApplicationNumber . ',' . $iRow;
2012-10-09 13:21:00 -04:00
foreach ($aTableFields as $aField) {
switch ($aField['sType']) {
case 'number':
2017-12-04 13:25:35 +00:00
$sQuery .= ',' . (isset($aGridRow[$aField['sFieldName']]) ? (float)str_replace(
',',
'',
$aGridRow[$aField['sFieldName']]
) : '0');
2012-10-09 13:21:00 -04:00
break;
case 'char':
case 'text':
2017-08-14 10:56:14 -04:00
if (!isset($aGridRow[$aField['sFieldName']])) {
2012-10-09 13:21:00 -04:00
$aGridRow[$aField['sFieldName']] = '';
}
2017-12-04 13:25:35 +00:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? mysqli_real_escape_string(
$con->getResource(),
$aGridRow[$aField['sFieldName']]
) : '') . "'";
2012-10-09 13:21:00 -04:00
break;
case 'date':
2017-08-14 10:56:14 -04:00
$sQuery .= ",'" . (isset($aGridRow[$aField['sFieldName']]) ? $aGridRow[$aField['sFieldName']] : '') . "'";
2012-10-09 13:21:00 -04:00
break;
}
}
$sQuery .= ')';
2017-08-14 10:56:14 -04:00
$rs = $stmt->executeQuery($sQuery);
2012-10-09 13:21:00 -04:00
}
}
}
break;
}
2012-10-09 13:21:00 -04:00
$oDataset->next();
}
2012-10-09 13:21:00 -04:00
} catch (Exception $oError) {
throw ($oError);
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:21:00 -04:00
/**
* Function tableExist
* Check if table exists
*
* @access public
* @return boolean
*/
2017-08-14 10:56:14 -04:00
public function tableExist()
2012-10-09 13:21:00 -04:00
{
$bExists = true;
2017-08-14 10:56:14 -04:00
$sDataBase = 'database_' . strtolower(DB_ADAPTER);
$oDataBase = new database();
$bExists = $oDataBase->reportTableExist();
2012-10-09 13:21:00 -04:00
return $bExists;
}
/**
* Function chooseDB
* Choose the database to connect
*
* @access public
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @param string $TabConnectionk
2017-12-04 13:25:35 +00:00
*
2012-10-09 13:21:00 -04:00
* @return string
*/
2017-08-14 10:56:14 -04:00
public function chooseDB($TabConnectionk)
2012-10-09 13:21:00 -04:00
{
2017-08-14 10:56:14 -04:00
$repTabConnection = trim(strtoupper($TabConnectionk));
2012-10-09 13:21:00 -04:00
$PropelDatabase = 'rp';
if ($repTabConnection == '' || $repTabConnection == 'REPORT') {
2012-10-09 13:21:00 -04:00
$PropelDatabase = 'rp';
}
if ($repTabConnection == 'RBAC') {
2012-10-09 13:21:00 -04:00
$PropelDatabase = 'rbac';
}
if ($repTabConnection == 'WF') {
2012-10-09 13:21:00 -04:00
$PropelDatabase = 'workflow';
}
2012-10-09 13:21:00 -04:00
return ($PropelDatabase);
}
}