* Move getGroupTable from metric-zen to metric-model.

This commit is contained in:
zhouxin
2023-12-06 14:33:07 +08:00
parent 12bdc09095
commit ad76cdf02f
4 changed files with 173 additions and 174 deletions
+3 -3
View File
@@ -78,7 +78,7 @@ class metric extends control
$allResultData = $this->metric->getViewTableData($metric, $allResult);
}
list($groupHeader, $groupData) = $this->metricZen->getGroupTable($resultHeader, $resultData);
list($groupHeader, $groupData) = $this->metric->getGroupTable($resultHeader, $resultData);
$this->view->groupHeader = $groupHeader;
$this->view->groupData = $groupData;
$this->view->dateType = $current ? $this->metric->getDateTypeByCode($current->code) : 'nodate';
@@ -289,7 +289,7 @@ class metric extends control
$resultData = $this->metric->getViewTableData($metric, $result);
$allResultData = $this->metric->getViewTableData($metric, $allResult);
list($groupHeader, $groupData) = $this->metricZen->getGroupTable($resultHeader, $resultData);
list($groupHeader, $groupData) = $this->metric->getGroupTable($resultHeader, $resultData);
$this->view->groupHeader = $groupHeader;
$this->view->groupData = $groupData;
$this->view->dateType = $this->metric->getDateTypeByCode($metric->code);
@@ -322,7 +322,7 @@ class metric extends control
$allResultData = $this->metric->getViewTableData($metric, $allResult);
if($usePager) $this->view->dtablePager = $pager;
list($groupHeader, $groupData) = $this->metricZen->getGroupTable($resultHeader, $resultData);
list($groupHeader, $groupData) = $this->metric->getGroupTable($resultHeader, $resultData);
$this->view->groupHeader = $groupHeader;
$this->view->groupData = $groupData;
$this->view->tableWidth = $this->metricZen->getViewTableWidth($groupHeader);
+166
View File
@@ -97,6 +97,172 @@ class metricModel extends model
return $tableData;
}
/**
* 对 headers 进行分组满足表头合并单元格, 返回经过分组的 headers 和 data。
* Return grouped headers and data for table headers merges cell.
*
* @param array $header
* @param array $data
* @param bool $withCalcTime
* @access public
* @return array
*/
public function getGroupTable($header, $data, $withCalcTime = true)
{
if(!$header or !$data) return array(array(), array());
$headerLength = count($header);
if($headerLength == 2)
{
return $this->getTimeTable($data, '', $withCalcTime);
}
elseif($headerLength == 3)
{
if($this->isObjectMetric($header))
{
return $this->getObjectTable($header, $data, '', $withCalcTime);
}
else
{
$dateType = current($data)->dateType;
return $this->getTimeTable($data, $dateType, $withCalcTime);
}
}
elseif($headerLength == 4)
{
$dateType = current($data)->dateType;
return $this->getObjectTable($header, $data, $dateType, $withCalcTime);
}
return array($header, $data);
}
public function getTimeTable($data, $dateType = 'day', $withCalcTime = true)
{
// 没有时间属性的度量项,也加上天的属性,此度量项数据的 year month week day 字段均为0, 此时使用 calcTime 来做计算
$dateField = ($dateType == 'day' and !isset(current($data)->dateString)) ? 'calcTime' : 'dateString';
usort($data, function($a, $b) use ($dateField)
{
$dateA = strtotime($a->$dateField);
$dateB = strtotime($b->$dateField);
if ($dateA == $dateB) {
return 0;
}
return ($dateA > $dateB) ? -1 : 1;
});
$groupHeader = array();
$groupHeader[] = array('name' => 'date', 'title' => $this->lang->metric->date, 'align' => 'center', 'width' => 96);
$groupHeader[] = array('name' => 'value', 'title' => $this->lang->metric->value, 'align' => 'center', 'width' => 80);
$groupData = array();
foreach($data as $dataInfo)
{
if($dateType == 'year')
{
$date = substr($dataInfo->$dateField, 0, 4);
}
elseif($dateType == 'month')
{
$year = substr($dataInfo->$dateField, 0, 4);
$month = substr($dataInfo->$dateField, 5, 2);
$date = "{$year}-{$month}";
}
elseif($dateType == 'week')
{
$year = substr($dataInfo->$dateField, 0, 4);
$week = sprintf($this->lang->metric->week, substr($dataInfo->dateString, 5, 2));
$date = "{$year}-{$week}";
}
elseif($dateType == 'day')
{
$date = substr($dataInfo->$dateField, 0, 10);
}
$value = $withCalcTime ? array($dataInfo->value, $dataInfo->calcTime) : $dataInfo->value;
$dataSeries = array('date' => $date, 'value' => $value);
$groupData[] = $dataSeries;
}
return array($groupHeader, $groupData);
}
public function getObjectTable($header, $data, $dateType = 'day', $withCalcTime = true)
{
$groupHeader = array();
$groupData = array();
$headerField = current($header)['name'];
$headerTitle = current($header)['title'];
$groupHeader[] = array('name' => $headerField, 'title' => $headerTitle, 'align' => 'center', 'width' => 160);
$dateField = ($dateType == 'day' and !isset(current($data)->dateString)) ? 'calcTime' : 'dateString';
usort($data, function($a, $b) use($dateField)
{
$dateA = strtotime($a->$dateField);
$dateB = strtotime($b->$dateField);
if ($dateA == $dateB) {
return 0;
}
return ($dateA > $dateB) ? -1 : 1;
});
$times = array();
$objects = array();
foreach($data as $dataInfo)
{
$time = substr($dataInfo->$dateField, 0, 10);
$calcTime = $dataInfo->calcTime;
$object = $dataInfo->scope;
$value = $dataInfo->value;
if(!isset($times[$time])) $times[$time] = $time;
if(!isset($objects[$object])) $objects[$object] = array();
$objects[$object][$time] = $withCalcTime ? array($value, $calcTime) : $value;
}
/* e.g $times = array('2023-10-14', '2023-10-15'), $objects = array('object1' => array('2023-10-14' => 2, '2023-10-15 => 3)) */
foreach($times as $time)
{
$year = substr($time, 0, 4) . $this->lang->year;
if($dateType == 'year')
{
$title = $year;
$groupHeader[] = array('name' => $time, 'title' => $title, 'align' => 'center', 'width' => 64);
}
elseif($dateType == 'month')
{
$month = substr($time, 5, 2) . $this->lang->month;
$groupHeader[] = array('name' => $time, 'title' => $month, 'headerGroup' => $year, 'align' => 'center', 'width' => 64);
}
elseif($dateType == 'week')
{
$week = sprintf($this->lang->metric->week, substr($time, 5, 2));
$groupHeader[] = array('name' => $time, 'title' => $week, 'headerGroup' => $year, 'align' => 'center', 'width' => 64);
}
elseif($dateType == 'day')
{
$day = substr($time, 5, 5);
$groupHeader[] = array('name' => $time, 'title' => $day, 'headerGroup' => $year, 'align' => 'center', 'width' => 64);
}
}
foreach($objects as $object => $datas)
{
$objectData = array($headerField => $object);
foreach($times as $time) $objectData[$time] = isset($datas[$time]) ? $datas[$time] : 0;
$groupData[] = $objectData;
}
return array($groupHeader, $groupData);
}
/**
* Get scope pairs.
*
+4 -4
View File
@@ -202,7 +202,7 @@ class metricTao extends metricModel
->where('deleted')->eq(0)
->andWhere('shadow')->eq(0)
->andWhere('id')->in($scopeObjects)
->page($pager)
->beginIF(!empty($pager))->page($pager)->fi()
->fetchPairs();
}
elseif($scope == 'project')
@@ -211,7 +211,7 @@ class metricTao extends metricModel
->where('deleted')->eq(0)
->andWhere('type')->eq('project')
->andWhere('id')->in($scopeObjects)
->page($pager)
->beginIF(!empty($pager))->page($pager)->fi()
->fetchPairs();
}
elseif($scope == 'execution')
@@ -220,7 +220,7 @@ class metricTao extends metricModel
->where('deleted')->eq(0)
->andWhere('type')->in('sprint,stage,kanban')
->andWhere('id')->in($scopeObjects)
->page($pager)
->beginIF(!empty($pager))->page($pager)->fi()
->fetchPairs();
}
elseif($scope == 'user')
@@ -228,7 +228,7 @@ class metricTao extends metricModel
$objects = $this->dao->select('account')->from(TABLE_USER)
->where('deleted')->eq('0')
->andWhere('account')->in($scopeObjects)
->page($pager)
->beginIF(!empty($pager))->page($pager)->fi()
->fetchPairs();
}
-167
View File
@@ -385,173 +385,6 @@ class metricZen extends metric
return $width;
}
/**
* 对 headers 进行分组满足表头合并单元格, 返回经过分组的 headers 和 data。
* Return grouped headers and data for table headers merges cell.
*
* @param array $header
* @param array $data
* @access protected
* @return array
*/
protected function getGroupTable($header, $data)
{
if(!$header or !$data) return array(array(), array());
$headerLength = count($header);
if($headerLength == 2)
{
return $this->getTimeTable($data);
}
elseif($headerLength == 3)
{
if($this->metric->isObjectMetric($header))
{
return $this->getObjectTable($header, $data);
}
else
{
$dateType = current($data)->dateType;
return $this->getTimeTable($data, $dateType);
}
}
elseif($headerLength == 4)
{
$dateType = current($data)->dateType;
return $this->getObjectTable($header, $data, $dateType);
}
return array($header, $data);
}
protected function getObjectTable($header, $data, $dateType = 'day')
{
$groupHeader = array();
$groupData = array();
$headerField = current($header)['name'];
$headerTitle = current($header)['title'];
$groupHeader[] = array('name' => $headerField, 'title' => $headerTitle, 'width' => 160);
$dateField = ($dateType == 'day' and !isset(current($data)->dateString)) ? 'calcTime' : 'dateString';
usort($data, function($a, $b) use($dateField)
{
$dateA = strtotime($a->$dateField);
$dateB = strtotime($b->$dateField);
if ($dateA == $dateB) {
return 0;
}
return ($dateA > $dateB) ? -1 : 1;
});
$times = array();
$objects = array();
foreach($data as $dataInfo)
{
$time = substr($dataInfo->$dateField, 0, 10);
$calcTime = $dataInfo->calcTime;
$object = $dataInfo->scope;
$value = $dataInfo->value;
if(!isset($times[$time])) $times[$time] = $time;
if(!isset($objects[$object])) $objects[$object] = array();
$objects[$object][$time] = array($value, $calcTime);
}
/* e.g $times = array('2023-10-14', '2023-10-15'), $objects = array('object1' => array('2023-10-14' => 2, '2023-10-15 => 3)) */
foreach($times as $time)
{
$year = substr($time, 0, 4) . $this->lang->year;
if($dateType == 'year')
{
$title = $year;
$groupHeader[] = array('name' => $time, 'title' => $title, 'align' => 'center', 'width' => 64);
}
elseif($dateType == 'month')
{
$month = substr($time, 5, 2) . $this->lang->month;
$groupHeader[] = array('name' => $time, 'title' => $month, 'headerGroup' => $year, 'align' => 'center', 'width' => 64);
}
elseif($dateType == 'week')
{
$week = sprintf($this->lang->metric->week, substr($time, 5, 2));
$groupHeader[] = array('name' => $time, 'title' => $week, 'headerGroup' => $year, 'align' => 'center', 'width' => 64);
}
elseif($dateType == 'day')
{
$day = substr($time, 5, 5);
$groupHeader[] = array('name' => $time, 'title' => $day, 'headerGroup' => $year, 'align' => 'center', 'width' => 64);
}
}
foreach($objects as $object => $datas)
{
$objectData = array($headerField => $object);
foreach($times as $time)
{
$objectData[$time] = isset($datas[$time]) ? $datas[$time] : 0;
}
$groupData[] = $objectData;
}
return array($groupHeader, $groupData);
}
protected function getTimeTable($data, $dateType = 'day')
{
// 没有时间属性的度量项,也加上天的属性,此度量项数据的 year month week day 字段均为0, 此时使用 calcTime 来做计算
$dateField = ($dateType == 'day' and !isset(current($data)->dateString)) ? 'calcTime' : 'dateString';
usort($data, function($a, $b) use ($dateField)
{
$dateA = strtotime($a->$dateField);
$dateB = strtotime($b->$dateField);
if ($dateA == $dateB) {
return 0;
}
return ($dateA > $dateB) ? -1 : 1;
});
$groupHeader = array();
$groupHeader[] = array('name' => 'date', 'title' => $this->lang->metric->date, 'align' => 'center', 'width' => 96);
$groupHeader[] = array('name' => 'value', 'title' => $this->lang->metric->value, 'align' => 'center', 'width' => 80);
$groupData = array();
foreach($data as $dataInfo)
{
if($dateType == 'year')
{
$date = substr($dataInfo->$dateField, 0, 4);
}
elseif($dateType == 'month')
{
$year = substr($dataInfo->$dateField, 0, 4);
$month = substr($dataInfo->$dateField, 5, 2);
$date = "{$year}-{$month}";
}
elseif($dateType == 'week')
{
$year = substr($dataInfo->$dateField, 0, 4);
$week = sprintf($this->lang->metric->week, substr($dataInfo->dateString, 5, 2));
$date = "{$year}-{$week}";
}
elseif($dateType == 'day')
{
$date = substr($dataInfo->$dateField, 0, 10);
}
$dataSeries = array('date' => $date, 'value' => array($dataInfo->value, $dataInfo->calcTime));
$groupData[] = $dataSeries;
}
return array($groupHeader, $groupData);
}
/**
* 根据后台配置的估算单位对列表赋值。
* Assign unitList['measure'] by custom hourPoint.