metaparser.php
来自「SugarCRM5.1 开源PHP客户关系管理系统」· PHP 代码 · 共 919 行 · 第 1/2 页
PHP
919 行
}
/**
* isCustomField
* This method checks the mixed variable $elementNames to see if it is a custom field. A custom
* field is simply defined as a field that ends with "_c". If $elementNames is an Array
* any matching custom field value will result in a true evaluation
* @param $elementNames Array or String value of form element name(s).
* @return String name of custom field; null if none found
*/
function getCustomField($elementNames) {
if(!isset($elementNames) || (!is_string($elementNames) && !is_array($elementNames))) {
return null;
}
if(is_string($elementNames)) {
if(preg_match('/(.+_c)(_basic)?(\[\])?$/', $elementNames, $matches)) {
return count($matches) == 1 ? $matches[0] : $matches[1];
}
return null;
}
foreach($elementNames as $name) {
if(preg_match('/(.+_c)(_basic)?(\[\])?$/', $name, $matches)) {
return count($matches) == 1 ? $matches[0] : $matches[1];
}
}
return null;
}
function applyPreRules($moduleDir, $panels) {
if(file_exists("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php")) {
require_once("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php");
$class = $moduleDir."ParseRule";
$parseRule = new $class();
$panels = $parseRule->preParse($panels, $this->mView);
}
return $panels;
}
function applyRules($moduleDir, $panels) {
return $this->applyPostRules($moduleDir, $panels);
}
function applyPostRules($moduleDir, $panels) {
//Run module specific rules
if(file_exists("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php")) {
require_once("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php");
$class = $moduleDir."ParseRule";
$parseRule = new $class();
$panels = $parseRule->parsePanels($panels, $this->mView);
}
//Now run defined rules
require_once("include/SugarFields/Parsers/Rules/ParseRules.php");
$rules = ParseRules::getRules();
foreach($rules as $rule) {
if(!file_exists($rule['file'])) {
$GLOBALS['log']->error("Cannot run rule for " . $rule['file']);
continue;
} //if
require_once($rule['file']);
$runRule = new $rule['class'];
$panels = $runRule->parsePanels($panels, $this->mView);
} //foreach
return $panels;
}
function createFileContents($moduleDir, $panels, $templateMeta=array(), $htmlFilePath) {
$header = "<?php\n\n";
if(empty($templateMeta)) {
$header .= "\$viewdefs['$moduleDir']['$this->mView'] = array(
'templateMeta' => array('maxColumns' => '2',
'widths' => array(
array('label' => '10', 'field' => '30'),
array('label' => '10', 'field' => '30')
),
),";
} else {
$header .= "\$viewdefs['$moduleDir']['$this->mView'] = array(
'templateMeta' =>" . var_export($templateMeta, true) . ",";
}
/*
$contents = file_get_contents($htmlFilePath);
$javascript = $this->getJavascript($contents, true);
if(!empty($javascript)) {
$javascript = str_replace("'", "\\'", $javascript);
$header .= "\n 'javascript' => '" . $javascript . "',\n";
} //if
*/
$header .= "\n 'panels' =>";
$footer = "
\n
);
?>";
$metadata = '';
$body = var_export($panels, true);
$metadata = $header . $body . $footer;
$metadata = preg_replace('/(\d+)[\s]=>[\s]?/',"",$metadata);
return $metadata;
}
/**
* mergePanels
* This function merges the $panels Array against the $masterCopy's meta data definition
* @param $panels meta data Array to merge
* @param $moduleDir Directory name of the module
* @param $masterCopy file path to the meta data master copy
* @return Array of merged $panel definition
*/
function mergePanels($panels, $vardefs, $moduleDir, $masterCopy) {
require($masterCopy);
$masterpanels = $viewdefs[$moduleDir][$this->mView]['panels'];
$hasMultiplePanels = $this->hasMultiplePanels($masterpanels);
if(!$hasMultiplePanels) {
$keys = array_keys($viewdefs[$moduleDir][$this->mView]['panels']);
if(!empty($keys) && count($keys) == 1) {
if(strtolower($keys[0]) == 'default') {
$masterpanels = array('default'=>$viewdefs[$moduleDir][$this->mView]['panels'][$keys[0]]);
} else {
$firstPanel = array_values($viewdefs[$moduleDir][$this->mView]['panels']);
$masterpanels = array('default'=> $firstPanel[0]);
}
} else {
$masterpanels = array('default'=>$viewdefs[$moduleDir][$this->mView]['panels']);
}
}
foreach($masterpanels as $name=>$masterpanel) {
if(isset($panels[$name])) {
// Get all the names in the panel
$existingElements = array();
$existingLocation = array();
foreach($panels[$name] as $rowKey=>$row) {
foreach($row as $colKey=>$column) {
if(is_array($column) && !empty($column['name'])) {
$existingElements[$column['name']] = $column['name'];
$existingLocation[$column['name']] = array("panel"=>$name, "row"=>$rowKey, "col"=>$colKey);
} else if(!is_array($column) && !empty($column)) {
$existingElements[$column] = $column;
$existingLocation[$column] = array("panel"=>$name, "row"=>$rowKey, "col"=>$colKey);
}
} //foreach
} //foreach
// Now check against the $masterCopy
foreach($masterpanel as $rowKey=>$row) {
$addRow = array();
foreach($row as $colKey=>$column) {
if(is_array($column) && isset($column['name'])) {
$id = $column['name'];
} else if(!is_array($column) && !empty($column)) {
$id = $column;
} else {
continue;
}
if(empty($existingElements[$id])) {
//Only add if
// 1) if it is a required field (as defined in metadata)
// 2) or if it has a customLabel and customCode (a very deep customization)
if((is_array($column) && !empty($column['displayParams']['required'])) ||
(is_array($column) && !empty($column['customCode']) && !empty($column['customLabel']))) {
$addRow[] = $column;
}
} else {
//Use definition from master copy instead
$panels[$existingLocation[$id]['panel']][$existingLocation[$id]['row']][$existingLocation[$id]['col']] = $column;
}
} //foreach
// Add it to the $panels
if(!empty($addRow)) {
$panels[$name][] = $addRow;
}
} //foreach
} else {
$panels[$name] = $masterpanel;
}
} //foreach
// We're not done yet... go through the $panels Array now and try to remove duplicate
// or empty panels
foreach($panels as $name=>$panel) {
if(count($panel) == 0 || !isset($masterpanels[$name])) {
unset($panels[$name]);
}
} //foreach
return $panels;
}
/**
* mergeTemplateMeta
* This function merges the $templateMeta Array against the $masterCopy's meta data definition
* @param $templateMeta meta data Array to merge
* @param $moduleDir Directory name of the module
* @param $masterCopy file path to the meta data master copy
* @return Array of merged $templateMeta definition
*/
function mergeTemplateMeta($templateMeta, $moduleDir, $masterCopy) {
require($masterCopy);
$masterTemplateMeta = $viewdefs[$moduleDir][$this->mView]['templateMeta'];
return $masterTemplateMeta;
}
function hasRequiredSpanLabel($html) {
if(empty($html)) {
return false;
}
return preg_match('/\<(div|span) class=(\")?required(\")?\s?>\*<\/(div|span)>/si', $html);
}
function hasMultiplePanels($panels) {
if(!isset($panels) || empty($panels) || !is_array($panels)) {
return false;
}
if(is_array($panels) && (count($panels) == 0 || count($panels) == 1)) {
return false;
}
foreach($panels as $panel) {
if(!empty($panel) && !is_array($panel)) {
return false;
} else {
foreach($panel as $row) {
if(!empty($row) && !is_array($row)) {
return false;
} //if
} //foreach
} //if-else
} //foreach
return true;
}
function getRelateFieldName($mixed='') {
if(!is_array($mixed)) {
return '';
} else if(count($mixed) == 2){
$id = '';
$name = '';
foreach($mixed as $el) {
if(preg_match('/_id$/', $el)) {
$id = $el;
} else if(preg_match('/_name$/', $el)) {
$name = $el;
}
}
return (!empty($id) && !empty($name)) ? $name : '';
}
return '';
}
function getCustomPanels() {
return $this->mCustomPanels;
}
/**
* fixTablesWithMissingTr
* This is a very crude function to fix instances where files declared a table as
* <table...><td> instead of <table...><tr><td>. Without this helper function, the
* parsing could messed up.
*
*/
function fixTablesWithMissingTr($tableContents) {
if(preg_match('/(<table[^>]*?[\/]?>\s*?<td)/i', $tableContents, $matches)) {
return preg_replace('/(<table[^>]*?[\/]?>\s*?<td)/i', '<table><tr><td', $tableContents);
}
return $tableContents;
}
/**
* findSingleVardefElement
* Scans array of form elements to see if just one is a vardef element and, if so,
* return that vardef name
*/
function findSingleVardefElement($formElements=array(), $vardefs=array()) {
if(empty($formElements) || !is_array($formElements)) {
return '';
}
$found = array();
foreach($formElements as $el) {
if(isset($vardefs[$el])) {
$found[] = $el;
}
}
return count($found) == 1 ? $found[0] : '';
}
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?