Merge pull request '* Fix bug#34665.' (#13) from fix_bug_qxz into 18.4
Reviewed-on: https://git.zcorp.cc/easycorp/zentaopms/pulls/13
This commit is contained in:
@@ -696,5 +696,110 @@ class chartModel extends model
|
||||
|
||||
return $filterFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tables.
|
||||
*
|
||||
* @param string $sql
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($sql)
|
||||
{
|
||||
$processedSql = trim($sql, ';');
|
||||
$processedSql = str_replace(array("\r\n", "\n"), ' ', $processedSql);
|
||||
$processedSql = str_replace('`', '', $processedSql);
|
||||
preg_match_all('/^select (.+) from (.+)$/i', $processedSql, $selectAndFrom);
|
||||
if(empty($selectAndFrom[2][0])) return false;
|
||||
|
||||
$selectSql = $selectAndFrom[1][0];
|
||||
$tableSql = $fromSql = $selectAndFrom[2][0];
|
||||
if(stripos($fromSql, 'where') !== false) $tableSql = trim(substr($fromSql, 0, stripos($fromSql, 'where')));
|
||||
if(stripos($fromSql, 'limit') !== false) $tableSql = trim(substr($fromSql, 0, stripos($fromSql, 'limit')));
|
||||
if(stripos($fromSql, 'having') !== false) $tableSql = trim(substr($fromSql, 0, stripos($fromSql, 'having')));
|
||||
if(stripos($fromSql, 'group by') !== false) $tableSql = trim(substr($fromSql, 0, stripos($fromSql, 'group by')));
|
||||
|
||||
/* Remove such as "left join|right join|join", "on (t1.id=t2.id)", result like t1, t2 as t3. */
|
||||
$tableSql .= ' ';
|
||||
if(stripos($tableSql, 'join') !== false) $tableSql = preg_replace(array('/join\s+([A-Z]+_\w+ .*)on/Ui', '/,\s*on\s+[^,]+/i'), array(',$1,on', ''), $tableSql);
|
||||
|
||||
/* Match t2 as t3 */
|
||||
preg_match_all('/(\w+) +as +(\w+)/i', $tableSql, $out);
|
||||
|
||||
$fields = explode(',', $selectSql);
|
||||
foreach($fields as $i => $field)
|
||||
{
|
||||
if($field) $asField = '';
|
||||
if(strrpos($field, ' as ') !== false) list($field, $asField) = explode(' as ', $field);
|
||||
if(strrpos($field, ' AS ') !== false) list($field, $asField) = explode(' AS ', $field);
|
||||
|
||||
$field = trim($field);
|
||||
$asField = trim($asField);
|
||||
$fieldName = $field;
|
||||
if(strrpos($field, '.') !== false)
|
||||
{
|
||||
$table = substr($field, 0, strrpos($field, '.'));
|
||||
$fieldName = substr($field, strrpos($field, '.') + 1);
|
||||
if(!empty($out[0]) and in_array($table, $out[2]))
|
||||
{
|
||||
$realTable = $out[1][array_search($table, $out[2])];
|
||||
$tableFieldName = str_replace($table . '.', $realTable . '.', $field);
|
||||
|
||||
if(isset($fields[$fieldName]) && !$asField)
|
||||
{
|
||||
$fieldName = str_replace('.', '', $field);
|
||||
|
||||
$sql = preg_replace(array("/$field/", "/`$field`/"), array("$field AS $fieldName", "`$field` AS $fieldName"), $sql, 1);
|
||||
|
||||
$field = $tableFieldName;
|
||||
}
|
||||
}
|
||||
|
||||
if($fieldName == '*') $fieldName = $field;
|
||||
}
|
||||
|
||||
$fieldName = $asField ? $asField : $fieldName;
|
||||
|
||||
$fields[$fieldName] = $field;
|
||||
unset($fields[$i]);
|
||||
}
|
||||
|
||||
$tableSql = preg_replace('/as +\w+/i', ' ', $tableSql);
|
||||
$tableSql = trim(str_replace(array('(', ')', ','), ' ', $tableSql));
|
||||
$tableSql = preg_replace('/ +/', ' ', $tableSql);
|
||||
|
||||
$tables = explode(' ', $tableSql);
|
||||
|
||||
return array('sql' => $sql, 'tables' => $tables, 'fields' => $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse variables to null string in sql.
|
||||
*
|
||||
* @param string $sql
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function parseSqlVars($sql, $filters)
|
||||
{
|
||||
if($filters)
|
||||
{
|
||||
foreach($filters as $filter)
|
||||
{
|
||||
if(!isset($filter['default'])) continue;
|
||||
if(isset($filter['from']) and $filter['from'] == 'query')
|
||||
{
|
||||
$default = "'{$filter['default']}'";
|
||||
$sql = str_replace('$' . $filter['field'], $default, $sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(preg_match_all("/[\$]+[a-zA-Z0-9]+/", $sql, $out))
|
||||
{
|
||||
foreach($out[0] as $match) $sql = str_replace($match, "''", $sql);
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ class pivot extends control
|
||||
|
||||
$pivot->sql = $sql;
|
||||
|
||||
$processSqlData = $this->loadModel('dataview')->getTables($sql);
|
||||
$processSqlData = $this->loadModel('chart')->getTables($sql);
|
||||
$sql = $processSqlData['sql'];
|
||||
|
||||
list($data, $configs) = $this->pivot->genSheet(json_decode(json_encode($pivot->fieldSettings), true), $pivot->settings, $sql, $filterFormat, json_decode($pivot->langs, true));
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
<?php endif;?>
|
||||
</div>
|
||||
<div id='datagirdInfo' class='datagrid datagrid-padding'>
|
||||
<?php $sql = $this->loadModel('dataview')->parseSqlVars($pivot->sql, $pivot->filters);?>
|
||||
<?php $sql = $this->loadModel('chart')->parseSqlVars($pivot->sql, $pivot->filters);?>
|
||||
<?php $this->pivot->buildPivotTable($data, $configs, json_decode(json_encode($pivot->fieldSettings), true), $sql);?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user