mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
831 lines
24 KiB
PHP
831 lines
24 KiB
PHP
<?php
|
|
// Copyright (C) 2011 Combodo SARL
|
|
//
|
|
// This program is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; version 3 of the License.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program; if not, write to the Free Software
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
class DOMFormatException extends Exception
|
|
{
|
|
}
|
|
|
|
class CompilerEchoPage
|
|
{
|
|
public function p($s)
|
|
{
|
|
echo "<p>\n$s</p>\n";
|
|
}
|
|
|
|
public function add($s)
|
|
{
|
|
//echo $s;
|
|
}
|
|
|
|
public function output()
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Compiler class
|
|
*/
|
|
class MFCompiler
|
|
{
|
|
protected $oFactory;
|
|
protected $sSourceDir;
|
|
|
|
public function __construct($oModelFactory, $sSourceDir)
|
|
{
|
|
$this->oFactory = $oModelFactory;
|
|
$this->sSourceDir = $sSourceDir;
|
|
}
|
|
|
|
public function Compile($sTargetDir, $oP = null)
|
|
{
|
|
if (is_null($oP))
|
|
{
|
|
$oP = new CompilerEchoPage();
|
|
}
|
|
|
|
|
|
$aMenuNodes = array();
|
|
$aMenusByModule = array();
|
|
foreach ($this->oFactory->ListActiveChildNodes('menus', 'menu') as $oMenuNode)
|
|
{
|
|
$sMenuId = $oMenuNode->getAttribute('id');
|
|
$aMenuNodes[$sMenuId] = $oMenuNode;
|
|
|
|
$sModuleMenu = $oMenuNode->getAttribute('_created_in');
|
|
$aMenusByModule[$sModuleMenu][] = $sMenuId;
|
|
}
|
|
|
|
|
|
$aResultFiles = array();
|
|
|
|
$aModules = $this->oFactory->GetLoadedModules();
|
|
foreach($aModules as $foo => $oModule)
|
|
{
|
|
$sModuleName = $oModule->GetName();
|
|
$sModuleVersion = $oModule->GetVersion();
|
|
|
|
$sModuleRootDir = realpath($oModule->GetRootDir());
|
|
$sRelativeDir = substr($sModuleRootDir, strlen($this->sSourceDir) + 1);
|
|
|
|
// Push the other module files
|
|
$this->CopyDirectory($sModuleRootDir, $sTargetDir.'/'.$sRelativeDir);
|
|
|
|
$oClasses = $this->oFactory->ListClasses($sModuleName);
|
|
$iClassCount = $oClasses->length;
|
|
if ($iClassCount == 0)
|
|
{
|
|
$oP->p("Found module without classes declared: $sModuleName");
|
|
}
|
|
else
|
|
{
|
|
$sResultFile = $sTargetDir.'/'.$sRelativeDir.'/model.'.$sModuleName.'.php';
|
|
if (is_file($sResultFile))
|
|
{
|
|
$oP->p("Updating <a href=\"#$sModuleName\">$sResultFile</a> for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
|
|
}
|
|
else
|
|
{
|
|
$sResultDir = dirname($sResultFile);
|
|
if (!is_dir($sResultDir))
|
|
{
|
|
$oP->p("Creating directory $sResultDir");
|
|
mkdir($sResultDir, 0777, true);
|
|
}
|
|
$oP->p("Creating <a href=\"#$sModuleName\">$sResultFile</a> for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
|
|
}
|
|
|
|
// Compile the module into a single file
|
|
//
|
|
$sId = $sModuleName;
|
|
$aResultFiles[$sId] = $sResultFile;
|
|
$sCurrDate = date(DATE_ISO8601);
|
|
$sAuthor = 'Combodo compiler';
|
|
$sLicence = 'http://www.opensource.org/licenses/gpl-3.0.html LGPL';
|
|
$sFileHeader =
|
|
<<<EOF
|
|
<?php
|
|
//
|
|
// File generated by ... on the $sCurrDate
|
|
// Please do not edit manually
|
|
//
|
|
//
|
|
// Copyright (C) 2010 Combodo SARL
|
|
//
|
|
// ben on met quoi ici ?
|
|
// Signé: Romain
|
|
//
|
|
// This program is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; version 3 of the License.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program; if not, write to the Free Software
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
/**
|
|
* Persistent classes for $sModuleName (version $sModuleVersion)
|
|
*
|
|
* @author $sAuthor
|
|
* @license $sLicence
|
|
*/
|
|
|
|
EOF;
|
|
|
|
file_put_contents($sResultFile, $sFileHeader);
|
|
|
|
foreach($oClasses as $oClass)
|
|
{
|
|
try
|
|
{
|
|
$this->CompileClass($oClass, $sResultFile, $sRelativeDir, $oP);
|
|
}
|
|
catch (ssDOMFormatException $e)
|
|
{
|
|
$sClass = $oClass->getAttribute("id");
|
|
throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!array_key_exists($sModuleName, $aMenusByModule))
|
|
{
|
|
$oP->p("Found module without menus declared: $sModuleName");
|
|
}
|
|
else
|
|
{
|
|
$sMenusHeader =
|
|
<<<EOF
|
|
//
|
|
// Menus
|
|
//
|
|
|
|
EOF;
|
|
file_put_contents($sResultFile, $sMenusHeader, FILE_APPEND);
|
|
|
|
// Preliminary: determine parent menus not defined within the current module
|
|
$aMenusToLoad = array();
|
|
|
|
foreach($aMenusByModule[$sModuleName] as $sMenuId)
|
|
{
|
|
$oMenuNode = $aMenuNodes[$sMenuId];
|
|
if ($sParent = $oMenuNode->GetChildText('parent', null))
|
|
{
|
|
$aMenusToLoad[] = $sParent;
|
|
}
|
|
// Note: the order matters: the parents must be defined BEFORE
|
|
$aMenusToLoad[] = $sMenuId;
|
|
}
|
|
$aMenusToLoad = array_unique($aMenusToLoad);
|
|
foreach($aMenusToLoad as $sMenuId)
|
|
{
|
|
$oMenuNode = $aMenuNodes[$sMenuId];
|
|
try
|
|
{
|
|
$this->CompileMenu($oMenuNode, $sResultFile, $sRelativeDir, $oP);
|
|
}
|
|
catch (ssDOMFormatException $e)
|
|
{
|
|
throw new Exception("Failed to process menu '$sMenuId', from '$sModuleRootDir': ".$e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count($aResultFiles))
|
|
{
|
|
$oP->add('<h2>Files</h2>');
|
|
foreach ($aResultFiles as $sModuleName => $sFile)
|
|
{
|
|
$oP->add('<h3>'.$sFile.'</h3>');
|
|
$oP->add('<a name="'.$sModuleName.'"/><div style="border:1;">');
|
|
$oP->add(highlight_file($sFile, true));
|
|
$oP->add('</div style="">');
|
|
}
|
|
}
|
|
|
|
$oP->output();
|
|
//$this->oFactory->Dump();
|
|
}
|
|
|
|
/**
|
|
* Helper to copy the module files to the exploitation environment
|
|
* Returns true if successfull
|
|
*/
|
|
protected function CopyDirectory($sSource, $sDest)
|
|
{
|
|
if (is_dir($sSource))
|
|
{
|
|
if (!is_dir($sDest))
|
|
{
|
|
mkdir($sDest);
|
|
}
|
|
$aFiles = scandir($sSource);
|
|
if(sizeof($aFiles) > 0 )
|
|
{
|
|
foreach($aFiles as $sFile)
|
|
{
|
|
if ($sFile == '.' || $sFile == '..' || $sFile == '.svn')
|
|
{
|
|
// Skip
|
|
continue;
|
|
}
|
|
|
|
if (is_dir($sSource.'/'.$sFile))
|
|
{
|
|
$this->CopyDirectory($sSource.'/'.$sFile, $sDest.'/'.$sFile);
|
|
}
|
|
else
|
|
{
|
|
copy($sSource.'/'.$sFile, $sDest.'/'.$sFile);
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
elseif (is_file($sSource))
|
|
{
|
|
return copy($sSource, $sDest);
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper to format the flags for an attribute, in a given state
|
|
* @param object $oAttNode DOM node containing the information to build the flags
|
|
* Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
|
|
*/
|
|
protected function FlagsToPHP($oAttNode)
|
|
{
|
|
static $aNodeAttributeToFlag = array(
|
|
'mandatory' => 'OPT_ATT_MANDATORY',
|
|
'read_only' => 'OPT_ATT_READONLY',
|
|
'must_prompt' => 'OPT_ATT_MUSTPROMPT',
|
|
'must_change' => 'OPT_ATT_MUSTCHANGE',
|
|
'hidden' => 'OPT_ATT_HIDDEN',
|
|
);
|
|
|
|
$aFlags = array();
|
|
foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
|
|
{
|
|
$bFlag = ($oAttNode->GetOptionalElement($sNodeAttribute) != null);
|
|
if ($bFlag)
|
|
{
|
|
$aFlags[] = $sFlag;
|
|
}
|
|
}
|
|
$sRes = implode(' | ', $aFlags);
|
|
return $sRes;
|
|
}
|
|
|
|
|
|
/**
|
|
* Format a path (file or url) as an absolute path or relative to the module or the app
|
|
*/
|
|
protected function PathToPHP($sPath, $sModuleRelativeDir, $bIsUrl = false)
|
|
{
|
|
if ($sPath == '')
|
|
{
|
|
$sPHP = "''";
|
|
}
|
|
elseif (substr($sPath, 0, 2) == '$$')
|
|
{
|
|
// Absolute
|
|
$sPHP = "'".addslashes(substr($sPath, 2))."'";
|
|
}
|
|
elseif (substr($sPath, 0, 1) == '$')
|
|
{
|
|
// Relative to the application
|
|
if ($bIsUrl)
|
|
{
|
|
$sPHP = "utils::GetAbsoluteUrlAppRoot().'".addslashes(substr($sPath, 1))."'";
|
|
}
|
|
else
|
|
{
|
|
$sPHP = "APPROOT.'".addslashes(substr($sPath, 1))."'";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Relative to the module
|
|
if ($bIsUrl)
|
|
{
|
|
$sPHP = "utils::GetAbsoluteUrlAppRoot().'".addslashes($sModuleRelativeDir.''.$sPath)."'";
|
|
}
|
|
else
|
|
{
|
|
$sPHP = "dirname(__FILE__).'/$sPath'";
|
|
}
|
|
}
|
|
return $sPHP;
|
|
}
|
|
|
|
protected function CompileClass($oClass, $sResFile, $sModuleRelativeDir, $oP)
|
|
{
|
|
$sClass = $oClass->getAttribute('id');
|
|
$oProperties = $oClass->GetUniqueElement('properties');
|
|
|
|
// Class caracteristics
|
|
//
|
|
$aClassParams = array();
|
|
$aClassParams['category'] = "'".$oProperties->GetChildText('category')."'";
|
|
$aClassParams['key_type'] = "'autoincrement'";
|
|
|
|
$oNaming = $oProperties->GetUniqueElement('naming');
|
|
$oNameAttributes = $oNaming->GetUniqueElement('attributes');
|
|
$oAttributes = $oNameAttributes->getElementsByTagName('attribute');
|
|
$aNameAttCodes = array();
|
|
foreach($oAttributes as $oAttribute)
|
|
{
|
|
$aNameAttCodes[] = $oAttribute->getAttribute('id');
|
|
}
|
|
if (count($aNameAttCodes) > 1)
|
|
{
|
|
// New style...
|
|
$sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
|
|
}
|
|
elseif (count($aNameAttCodes) == 1)
|
|
{
|
|
// New style...
|
|
$sNameAttCode = "'$aNameAttCodes[0]'";
|
|
}
|
|
else
|
|
{
|
|
$sNameAttCode = "''";
|
|
}
|
|
$aClassParams['name_attcode'] = $sNameAttCode;
|
|
|
|
$oLifecycle = $oClass->GetOptionalElement('lifecycle');
|
|
if ($oLifecycle)
|
|
{
|
|
$sStateAttCode = $oLifecycle->GetChildText('attribute');
|
|
}
|
|
else
|
|
{
|
|
$sStateAttCode = "";
|
|
}
|
|
$aClassParams['state_attcode'] = "'$sStateAttCode'";
|
|
|
|
$oReconciliation = $oProperties->GetUniqueElement('reconciliation');
|
|
$oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
|
|
$aReconcAttCodes = array();
|
|
foreach($oReconcAttributes as $oAttribute)
|
|
{
|
|
$aReconcAttCodes[] = $oAttribute->getAttribute('id');
|
|
}
|
|
$sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
|
|
$aClassParams['reconc_keys'] = $sReconcKeys;
|
|
|
|
$aClassParams['db_table'] = "'".$oProperties->GetChildText('db_table')."'";
|
|
|
|
$sKeyField = $oProperties->GetChildText('db_key_field');
|
|
if ($sKeyField == '')
|
|
{
|
|
$sKeyField = 'id';
|
|
}
|
|
$aClassParams['db_key_field'] = "'".$sKeyField."'";
|
|
|
|
// TODO: faire automatiquement (pas dans le XML !!!)
|
|
$aClassParams['db_finalclass_field'] = "'".$oProperties->GetChildText('db_final_class_field')."'";
|
|
|
|
if (($sDisplayTemplate = $oProperties->GetChildText('display_template')) && (strlen($sDisplayTemplate) > 0))
|
|
{
|
|
$sDisplayTemplate = $sModuleRelativeDir.'/'.$sDisplayTemplate;
|
|
$aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
|
|
}
|
|
|
|
if (($sIcon = $oProperties->GetChildText('icon')) && (strlen($sIcon) > 0))
|
|
{
|
|
$sIcon = $sModuleRelativeDir.'/'.$sIcon;
|
|
$aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
|
|
}
|
|
|
|
$oOrder = $oProperties->GetOptionalElement('order');
|
|
if ($oOrder)
|
|
{
|
|
$oColumnsNode = $oOrder->GetUniqueElement('columns');
|
|
$oColumns = $oColumnsNode->getElementsByTagName('column');
|
|
$aSortColumns = array();
|
|
foreach($oColumns as $oColumn)
|
|
{
|
|
$aSortColumns[] = "'".$oColumn->getAttribute('id')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
|
|
}
|
|
if (count($aSortColumns) > 0)
|
|
{
|
|
$aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
|
|
}
|
|
}
|
|
|
|
|
|
// Finalize class params declaration
|
|
//
|
|
$aClassParamsPHP = array();
|
|
foreach($aClassParams as $sKey => $sPHPValue)
|
|
{
|
|
$aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
|
|
}
|
|
$sClassParams = implode("\n", $aClassParamsPHP);
|
|
|
|
// Comment on top of the class declaration
|
|
//
|
|
$sCodeComment = $oProperties->GetChildText('comment');
|
|
|
|
// Fields
|
|
//
|
|
$sAttributes = '';
|
|
foreach($this->oFactory->ListFields($oClass) as $oField)
|
|
{
|
|
// $oField
|
|
$sAttCode = $oField->getAttribute('id');
|
|
$sAttType = $oField->getAttribute('xsi:type');
|
|
|
|
$aDependencies = array();
|
|
$oDependencies = $oField->GetOptionalElement('dependencies');
|
|
if (!is_null($oDependencies))
|
|
{
|
|
$oDepNodes = $oDependencies->getElementsByTagName('attribute');
|
|
foreach($oDepNodes as $oDepAttribute)
|
|
{
|
|
$aDependencies[] = "'".$oDepAttribute->getAttribute('id')."'";
|
|
}
|
|
}
|
|
$sDependencies = 'array('.implode(', ', $aDependencies).')';
|
|
|
|
$aParameters = array();
|
|
|
|
if ($sAttType == 'AttributeLinkedSetIndirect')
|
|
{
|
|
$aParameters['linked_class'] = "'".$oField->GetChildText('linked_class')."'";
|
|
$aParameters['ext_key_to_me'] = "'".$oField->GetChildText('ext_key_to_me')."'";
|
|
$aParameters['ext_key_to_remote'] = "'".$oField->GetChildText('ext_key_to_remote')."'";
|
|
// todo - utile ?
|
|
$aParameters['allowed_values'] = 'null';
|
|
$aParameters['count_min'] = $oField->GetChildText('count_min');
|
|
$aParameters['count_max'] = $oField->GetChildText('count_max');
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
elseif ($sAttType == 'AttributeLinkedSet')
|
|
{
|
|
$aParameters['linked_class'] = "'".$oField->GetChildText('linked_class')."'";
|
|
$aParameters['ext_key_to_me'] = "'".$oField->GetChildText('ext_key_to_me')."'";
|
|
// todo - utile ?
|
|
$aParameters['allowed_values'] = 'null';
|
|
$aParameters['count_min'] = $oField->GetChildText('count_min');
|
|
$aParameters['count_max'] = $oField->GetChildText('count_max');
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
elseif ($sAttType == 'AttributeExternalKey')
|
|
{
|
|
$aParameters['targetclass'] = "'".$oField->GetChildText('target_class')."'";
|
|
$aParameters['jointype'] = 'null';
|
|
if ($sOql = $oField->GetChildText('filter'))
|
|
{
|
|
$sEscapedOql = addslashes($sOql);
|
|
$aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
|
|
}
|
|
else
|
|
{
|
|
$aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
|
|
}
|
|
$aParameters['sql'] = "'".$oField->GetChildText('sql')."'";
|
|
$aParameters['is_null_allowed'] = $oField->GetChildText('is_null_allowed') == 'true' ? 'true' : 'false';
|
|
$aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
elseif ($sAttType == 'AttributeHierarchicalKey')
|
|
{
|
|
if ($sOql = $oField->GetChildText('filter'))
|
|
{
|
|
$sEscapedOql = addslashes($sOql);
|
|
$aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
|
|
}
|
|
else
|
|
{
|
|
$aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
|
|
}
|
|
$aParameters['sql'] = "'".$oField->GetChildText('sql')."'";
|
|
$aParameters['is_null_allowed'] = $oField->GetChildText('is_null_allowed') == 'true' ? 'true' : 'false';
|
|
$aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
elseif ($sAttType == 'AttributeExternalField')
|
|
{
|
|
$aParameters['allowed_values'] = 'null';
|
|
$aParameters['extkey_attcode'] = "'".$oField->GetChildText('extkey_attcode')."'";
|
|
$aParameters['target_attcode'] = "'".$oField->GetChildText('target_attcode')."'";
|
|
}
|
|
elseif ($sAttType == 'AttributeURL')
|
|
{
|
|
$aParameters['target'] = "'".$oField->GetChildText('target')."'";
|
|
$aParameters['allowed_values'] = 'null';
|
|
$aParameters['sql'] = "'".$oField->GetChildText('sql')."'";
|
|
$aParameters['default_value'] = "'".$oField->GetChildText('default_value')."'";
|
|
$aParameters['is_null_allowed'] = $oField->GetChildText('is_null_allowed') == 'true' ? 'true' : 'false';
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
elseif ($sAttType == 'AttributeEnum')
|
|
{
|
|
$oValues = $oField->GetUniqueElement('values');
|
|
$oValueNodes = $oValues->getElementsByTagName('value');
|
|
$aValues = array();
|
|
foreach($oValueNodes as $oValue)
|
|
{
|
|
// new style... $aValues[] = "'".addslashes($oValue->textContent)."'";
|
|
$aValues[] = $oValue->textContent;
|
|
}
|
|
// new style... $sValues = 'array('.implode(', ', $aValues).')';
|
|
$sValues = '"'.implode(',', $aValues).'"';
|
|
$aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
|
|
$aParameters['sql'] = "'".$oField->GetChildText('sql')."'";
|
|
$aParameters['default_value'] = "'".$oField->GetChildText('default_value')."'";
|
|
$aParameters['is_null_allowed'] = $oField->GetChildText('is_null_allowed') == 'true' ? 'true' : 'false';
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
elseif ($sAttType == 'AttributeBlob')
|
|
{
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
}
|
|
else
|
|
{
|
|
$aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
|
|
$aParameters['sql'] = "'".$oField->GetChildText('sql')."'";
|
|
$aParameters['default_value'] = "'".$oField->GetChildText('default_value')."'";
|
|
$aParameters['is_null_allowed'] = $oField->GetChildText('is_null_allowed') == 'true' ? 'true' : 'false';
|
|
$aParameters['depends_on'] = $sDependencies;
|
|
|
|
if ($sValidationPattern = $oField->GetChildText('validation_pattern'))
|
|
{
|
|
$aParameters['validation_pattern'] = '"'.addslashes($sValidationPattern).'"';
|
|
}
|
|
}
|
|
|
|
$aParams = array();
|
|
foreach($aParameters as $sKey => $sValue)
|
|
{
|
|
$aParams[] = '"'.$sKey.'"=>'.$sValue;
|
|
}
|
|
$sParams = implode(', ', $aParams);
|
|
$sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
|
|
}
|
|
|
|
// Lifecycle
|
|
//
|
|
$sLifecycle = '';
|
|
if ($oLifecycle)
|
|
{
|
|
$sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
|
|
$sLifecycle .= "\t\t//\n";
|
|
|
|
$oStimuli = $oLifecycle->GetUniqueElement('stimuli');
|
|
foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
|
|
{
|
|
$sStimulus = $oStimulus->getAttribute('id');
|
|
$sStimulusClass = $oStimulus->getAttribute('xsi:type');
|
|
|
|
$sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
|
|
}
|
|
|
|
$oStates = $oLifecycle->GetUniqueElement('states');
|
|
foreach ($oStates->getElementsByTagName('state') as $oState)
|
|
{
|
|
$sState = $oState->getAttribute('id');
|
|
|
|
$sLifecycle .= " MetaModel::Init_DefineState(\n";
|
|
$sLifecycle .= " \"".$sState."\",\n";
|
|
$sLifecycle .= " array(\n";
|
|
$sLifecycle .= " \"attribute_inherit\" => '',\n";
|
|
$sLifecycle .= " \"attribute_list\" => array(\n";
|
|
|
|
$oFlags = $oState->GetUniqueElement('flags');
|
|
foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
|
|
{
|
|
$sFlags = $this->FlagsToPHP($oAttributeNode);
|
|
if (strlen($sFlags) > 0)
|
|
{
|
|
$sAttCode = $oAttributeNode->GetAttribute('id');
|
|
$sLifecycle .= " '$sAttCode' => $sFlags,\n";
|
|
}
|
|
}
|
|
|
|
$sLifecycle .= " ),\n";
|
|
$sLifecycle .= " )\n";
|
|
$sLifecycle .= " );\n";
|
|
|
|
$oTransitions = $oState->GetUniqueElement('transitions');
|
|
foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
|
|
{
|
|
$sStimulus = $oTransition->GetChildText('stimulus');
|
|
$sTargetState = $oTransition->GetChildText('target');
|
|
|
|
$oActions = $oTransition->GetUniqueElement('actions');
|
|
$aVerbs = array();
|
|
foreach ($oActions->getElementsByTagName('action') as $oAction)
|
|
{
|
|
$sVerb = $oAction->GetChildText('verb');
|
|
$aVerbs[] = "'$sVerb'";
|
|
}
|
|
$sActions = implode(', ', $aVerbs);
|
|
$sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
// ZLists
|
|
//
|
|
$aListRef = array(
|
|
'details' => 'details',
|
|
'standard_search' => 'search',
|
|
'list' => 'list'
|
|
);
|
|
|
|
$oPresentation = $oClass->GetUniqueElement('presentation');
|
|
$sZlists = '';
|
|
foreach ($aListRef as $sListCode => $sListTag)
|
|
{
|
|
$oListNode = $oPresentation->GetOptionalElement($sListTag);
|
|
if ($oListNode)
|
|
{
|
|
$aAttributes = $oListNode->GetNodeAsArrayOfItems();
|
|
|
|
$sZAttributes = var_export($aAttributes, true);
|
|
$sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
|
|
}
|
|
}
|
|
|
|
// Methods
|
|
$sMethods = "";
|
|
$oMethods = $oClass->GetUniqueElement('methods');
|
|
foreach($oMethods->getElementsByTagName('method') as $oMethod)
|
|
{
|
|
$sMethodCode = $oMethod->GetChildText('code');
|
|
if ($sMethodComment = $oMethod->GetChildText('comment', null))
|
|
{
|
|
$sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
|
|
}
|
|
else
|
|
{
|
|
$sMethods .= "\n\n".$sMethodCode."\n";
|
|
}
|
|
}
|
|
|
|
// Let's make the whole class declaration
|
|
//
|
|
$sPHP = "\n\n$sCodeComment\n";
|
|
if ($oProperties->GetChildText('abstract') == 'true')
|
|
{
|
|
$sPHP .= 'abstract class '.$oClass->getAttribute('id');
|
|
}
|
|
else
|
|
{
|
|
$sPHP .= 'class '.$oClass->getAttribute('id');
|
|
}
|
|
$sPHP .= " extends ".$oClass->GetChildText('parent', 'DBObject')."\n";
|
|
$sPHP .=
|
|
<<<EOF
|
|
{
|
|
public static function Init()
|
|
{
|
|
\$aParams = array
|
|
(
|
|
$sClassParams
|
|
);
|
|
MetaModel::Init_Params(\$aParams);
|
|
MetaModel::Init_InheritAttributes();
|
|
$sAttributes
|
|
$sLifecycle
|
|
$sZlists
|
|
}
|
|
|
|
$sMethods
|
|
}
|
|
EOF;
|
|
file_put_contents($sResFile, $sPHP, FILE_APPEND);
|
|
}// function CompileClass()
|
|
|
|
|
|
protected function CompileMenu($oMenu, $sResFile, $sModuleRelativeDir, $oP)
|
|
{
|
|
$sMenuId = $oMenu->getAttribute("id");
|
|
$sMenuClass = $oMenu->getAttribute("xsi:type");
|
|
|
|
$sParent = $oMenu->GetChildText('parent', null);
|
|
if ($sParent)
|
|
{
|
|
$sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
|
|
}
|
|
else
|
|
{
|
|
$sParentSpec = '-1';
|
|
}
|
|
|
|
$fRank = $oMenu->GetChildText('rank');
|
|
switch($sMenuClass)
|
|
{
|
|
case 'WebPageMenuNode':
|
|
$sUrl = $oMenu->GetChildText('url');
|
|
$sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
|
|
$sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
|
|
break;
|
|
|
|
case 'TemplateMenuNode':
|
|
$sTemplateFile = $oMenu->GetChildText('template_file');
|
|
$sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
|
|
$sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
|
|
break;
|
|
|
|
case 'OQLMenuNode':
|
|
$sOQL = $oMenu->GetChildText('oql');
|
|
$bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
|
|
$sNewMenu = "new OQLMenuNode('$sMenuId', '$sOQL', $sParentSpec, $fRank, $bSearch);";
|
|
break;
|
|
|
|
case 'NewObjectMenuNode':
|
|
$sClass = $oMenu->GetChildText('class');
|
|
$sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
|
|
break;
|
|
|
|
case 'SearchMenuNode':
|
|
$sClass = $oMenu->GetChildText('class');
|
|
$sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
|
|
break;
|
|
|
|
case 'MenuGroup':
|
|
default:
|
|
if ($sEnableClass = $oMenu->GetChildText('enable_class'))
|
|
{
|
|
$sEnableAction = $oMenu->GetChildText('enable_action');
|
|
$sEnablePermission = $oMenu->GetChildText('enable_permission');
|
|
$sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
|
|
if (strlen($sEnableStimulus) > 0)
|
|
{
|
|
$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
|
|
}
|
|
else
|
|
{
|
|
$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
|
|
}
|
|
//$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
|
|
}
|
|
else
|
|
{
|
|
$sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
|
|
}
|
|
}
|
|
|
|
$sIndent = '';
|
|
$aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
|
|
if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
|
|
{
|
|
$sAutoReload = addslashes($sAutoReload);
|
|
$aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => '$sAutoReload'));";
|
|
}
|
|
|
|
$sAdminOnly = $oMenu->GetChildText('enable_admin_only');
|
|
if ($sAdminOnly && ($sAdminOnly == '1'))
|
|
{
|
|
$sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
|
|
$sPHP .= $sIndent."{\n";
|
|
foreach($aPHPMenu as $sPHPLine)
|
|
{
|
|
$sPHP .= $sIndent." $sPHPLine\n";
|
|
}
|
|
$sPHP .= $sIndent."}\n";
|
|
}
|
|
else
|
|
{
|
|
$sPHP = '';
|
|
foreach($aPHPMenu as $sPHPLine)
|
|
{
|
|
$sPHP .= $sIndent.$sPHPLine."\n";
|
|
}
|
|
}
|
|
|
|
file_put_contents($sResFile, $sPHP, FILE_APPEND);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
?>
|