control.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * The control file of screen module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Mengyi Liu <liumengyi@cnezsoft.com>
  8. * @package task
  9. * @version $Id: control.php 5106 2022-11-18 17:15:54Z $
  10. * @link https://www.zentao.net
  11. */
  12. class screen extends control
  13. {
  14. /**
  15. * @param string $moduleName
  16. * @param string $methodName
  17. * @param string $appName
  18. */
  19. public function __construct($moduleName = '', $methodName = '', $appName = '')
  20. {
  21. parent::__construct($moduleName, $methodName, $appName);
  22. $this->dao->exec("SET @@sql_mode=''");
  23. }
  24. /**
  25. * 浏览一个维度下的所有大屏。
  26. * Browse screens of a dimension.
  27. *
  28. * @param int $dimensionID
  29. * @access public
  30. * @return void
  31. */
  32. public function browse($dimensionID = 0)
  33. {
  34. //$this->checkShowGuide();
  35. $this->view->showGuide = false;
  36. $dimensionID = $this->loadModel('dimension')->getDimension($dimensionID);
  37. $screens = $this->screen->getList($dimensionID);
  38. $screens = $this->screen->getThumbnail($screens);
  39. $screens = $this->screen->removeScheme($screens);
  40. $screens = $this->screenZen->prepareCardList($screens);
  41. $this->view->title = $this->lang->screen->common;
  42. $this->view->screens = $screens;
  43. $this->view->dimensionID = $dimensionID;
  44. $this->display();
  45. }
  46. /**
  47. * 检查是否需要显示 BI 新功能的引导页面。
  48. * Check if need to show the guide of BI new features.
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. public function checkShowGuide()
  54. {
  55. $this->app->loadLang('admin');
  56. $isUpdate = $this->loadModel('setting')->getItem("owner=system&module=bi&key=update2BI");
  57. if(empty($isUpdate))
  58. {
  59. $this->view->showGuide = false;
  60. return;
  61. }
  62. $lang = (strpos($this->app->getClientLang(), 'zh') !== false) ? 'zh' : 'en';
  63. $version = ($this->config->edition == 'biz' || $this->config->edition == 'max') ? 'biz' : 'pms';
  64. $imageURL = "static/images/bi_guide_{$version}_{$lang}.png";
  65. $moduleKey = $version . 'Guide';
  66. $guides = $this->setting->getItem("owner=system&module=bi&key={$moduleKey}");
  67. $haveSeen = explode(',', $guides);
  68. $afterSeen = array_merge($haveSeen, array($this->app->user->account));
  69. $this->setting->setItem("system.bi.{$moduleKey}", implode(',', array_unique($afterSeen)));
  70. $this->view->showGuide = in_array($this->app->user->account, $haveSeen) ? false : true;
  71. $this->view->imageURL = $imageURL;
  72. $this->view->version = $version;
  73. }
  74. /**
  75. * 查看一个大屏。
  76. * View a screen.
  77. *
  78. * @param int $screenID
  79. * @param int $year
  80. * @param int $dept
  81. * @param string $account
  82. * @access public
  83. * @return void
  84. * @param int $month
  85. */
  86. public function view($screenID, $year = 0, $month = 0, $dept = 0, $account = '')
  87. {
  88. $this->screen->checkAccess($screenID);
  89. if(empty($year)) $year = date('Y');
  90. if(empty($month)) $month = date('m');
  91. if($screenID == 3)
  92. {
  93. echo $this->fetch('report', 'annualData', "year={$year}&dept={$dept}&account={$account}");
  94. return;
  95. }
  96. $screen = $this->screen->getByID($screenID, $year, $month, $dept, $account, false);
  97. $this->view->title = $screen->name;
  98. $this->view->screenID = $screenID;
  99. if($screenID == 5)
  100. {
  101. $this->loadModel('execution');
  102. $this->view->executions = $this->screen->getBurnData();
  103. $this->view->date = date('Y-m-d H:i:s');
  104. $this->view->screen = $screen;
  105. $this->display('screen', 'burn');
  106. return;
  107. }
  108. $this->view->year = $year;
  109. $this->view->month = $month;
  110. $this->view->dept = $dept;
  111. $this->view->account = $account;
  112. $this->display();
  113. }
  114. /**
  115. * 查看一个大屏(旧页面方式)。
  116. * View a screen(old page).
  117. *
  118. * @param int $screenID
  119. * @param int $year
  120. * @param int $dept
  121. * @param string $account
  122. * @access public
  123. * @return void
  124. * @param int $month
  125. */
  126. public function viewOld($screenID, $year = 0, $month = 0, $dept = 0, $account = '')
  127. {
  128. if(empty($year)) $year = date('Y');
  129. if(empty($month)) $month = date('m');
  130. $screen = $this->screen->getByID($screenID, $year, $month, $dept, $account, false);
  131. $this->view->title = $screen->name;
  132. $this->view->screen = $screen;
  133. $this->view->year = $year;
  134. $this->view->month = $month;
  135. $this->view->dept = $dept;
  136. $this->view->account = $account;
  137. $this->display();
  138. }
  139. /**
  140. * 静态数据大屏(旧页面)。
  141. * Static data screen (old page).
  142. *
  143. * @access public
  144. * @return void
  145. */
  146. public function staticDataOld()
  147. {
  148. $this->display();
  149. }
  150. /**
  151. * @param int $screenID
  152. * @param int $year
  153. * @param int $month
  154. * @param int $dept
  155. * @param string $account
  156. */
  157. public function ajaxGetScreenScheme($screenID, $year = 0, $month = 0, $dept = 0, $account = '')
  158. {
  159. $screen = $this->screen->getByID($screenID, $year, $month, $dept, $account);
  160. echo(json_encode($screen));
  161. }
  162. /**
  163. * 通过ajax获取图表数据。
  164. * Ajax get chart.
  165. *
  166. * @access public
  167. * @return void
  168. * @param int $year
  169. * @param int $month
  170. * @param int $dept
  171. * @param string $account
  172. */
  173. public function ajaxGetChart($year = 0, $month = 0, $dept = 0, $account = '')
  174. {
  175. if(!empty($_POST))
  176. {
  177. $sourceID = $this->post->sourceID;
  178. $type = $this->post->type;
  179. if($type == 'Filters')
  180. {
  181. $filterComponent = $this->screen->genFilterComponent($sourceID);
  182. return print(json_encode($filterComponent));
  183. }
  184. $queryType = isset($_POST['queryType']) ? $this->post->queryType : 'filter';
  185. $component = isset($_POST['component']) ? json_decode($this->post->component) : null;
  186. $filterParams = isset($_POST['filters']) ? json_decode($this->post->filters, true) : array();
  187. $selectFilter = isset($_POST['selectFilter']) ? json_decode($this->post->selectFilter, true) : array();
  188. $this->screen->filter->year = $year;
  189. $this->screen->filter->month = $month;
  190. $this->screen->filter->dept = $dept;
  191. $this->screen->filter->account = $account;
  192. $this->screen->filter->charts = $this->screenZen->setSelectFilter($sourceID, $selectFilter);
  193. $type = $this->screen->getChartType($type);
  194. if($type == 'metric')
  195. {
  196. $metric = $this->loadModel('metric')->getByID($sourceID);
  197. $metricData = $this->screen->genMetricComponent($metric, $component, $filterParams);
  198. return print(json_encode($metricData));
  199. }
  200. if($type == 'pivot')
  201. {
  202. $chartOrPivot = $this->loadModel('pivot')->getPivotDataByID($sourceID);
  203. }
  204. else
  205. {
  206. $table = $this->config->objectTables[$type];
  207. $chartOrPivot = $this->dao->select('*')->from($table)->where('id')->eq($sourceID)->fetch();
  208. }
  209. $filterFormat = array();
  210. if($queryType == 'filter') list($chartOrPivot, $filterFormat) = $this->screen->mergeChartAndPivotFilters($type, $chartOrPivot, $sourceID, $filterParams);
  211. $component = $this->screen->genComponentData($chartOrPivot, $type, $component, $filterFormat);
  212. print(json_encode($component));
  213. }
  214. }
  215. /**
  216. * 获取度量数据。
  217. * Ajax get metric data.
  218. *
  219. * @access public
  220. * @return void
  221. */
  222. public function ajaxGetMetricData()
  223. {
  224. $metricID = $this->post->metricID;
  225. list($pager, $pagination) = $this->screen->preparePaginationBeforeFetchRecords($_POST['pagination']);
  226. $metric = $this->loadModel('metric')->getByID($metricID);
  227. $result = $this->metric->getLatestResultByCode($metric->code, $_POST, $pager);
  228. $pagination['total'] = $pager->recTotal;
  229. $pagination['pageTotal'] = $pager->pageTotal;
  230. $metricData = new stdclass();
  231. $metricData->header = $this->metric->getViewTableHeader($metric);
  232. $metricData->data = $this->metric->getViewTableData($metric, $result);
  233. $metricData->pagination = $pagination;
  234. echo(json_encode($metricData));
  235. }
  236. public function ajaxGetFilterOptions()
  237. {
  238. $this->loadModel('metric');
  239. $type = $_POST['type'];
  240. $params = json_decode($_POST['params']);
  241. $query = $_POST['query'];
  242. $defaultValue = $_POST['defaultValue'];
  243. $type = $this->screen->getChartType($type);
  244. if($type == 'metric')
  245. {
  246. $scope = $params->typeOption;
  247. $objectPairs = $this->metric->getPairsByScope($scope, true);
  248. }
  249. else
  250. {
  251. if(isset($params->typeOption))
  252. {
  253. $objectPairs = $this->screen->getSysOptions($params->typeOption);
  254. }
  255. else
  256. {
  257. $sourceID = $params->sourceID;
  258. $field = $params->field;
  259. $saveAs = isset($params->saveAs) ? $params->saveAs : '';
  260. if($type == 'pivot')
  261. {
  262. $chart = $this->loadModel('pivot')->getPivotDataByID($sourceID);
  263. }
  264. else
  265. {
  266. $table = $this->config->objectTables[$type];
  267. $chart = $this->dao->select('*')->from($table)->where('id')->eq($sourceID)->fetch();
  268. }
  269. $fields = json_decode($chart->fields, true);
  270. $fieldObj = zget($fields, $field);
  271. $fieldType = $fieldObj['type'];
  272. $field = $fieldType != 'options' && $fieldType != 'object' ? $field : $fieldObj['field'];
  273. $objectPairs = $this->loadModel('pivot')->getSysOptions($fieldType, $fieldObj['object'], $field, $chart->sql, $saveAs);
  274. }
  275. }
  276. $options = array_map(function($objectID, $objectName)
  277. {
  278. return array(
  279. 'label' => $objectName,
  280. 'value' => "$objectID"
  281. );
  282. }, array_keys($objectPairs), array_values($objectPairs));
  283. if(empty($query))
  284. {
  285. if(!empty($defaultValue))
  286. {
  287. $defaultValue = is_array($defaultValue) ? $defaultValue : explode(',', $defaultValue);
  288. $defaultOptions = array_filter($options, function($option) use($defaultValue)
  289. {
  290. return in_array($option['value'], $defaultValue);
  291. });
  292. }
  293. // return limit 10 options.
  294. $options = array_slice($options, 0, 10);
  295. if(!empty($defaultOptions))
  296. {
  297. $uniqueOptions = array();
  298. foreach($options as $option)
  299. {
  300. $findInDefault = array_filter($defaultOptions, function($defaultOption) use($option)
  301. {
  302. return $defaultOption['value'] == $option['value'];
  303. });
  304. if(empty($findInDefault))
  305. {
  306. $uniqueOptions[] = $option;
  307. }
  308. }
  309. $options = array_merge($defaultOptions, $uniqueOptions);
  310. }
  311. echo(json_encode($options));
  312. return;
  313. }
  314. $options = array_filter($options, function($option) use($query)
  315. {
  316. return strpos(strtolower($option['label']), strtolower($query)) !== false;
  317. });
  318. $options = array_values($options);
  319. echo(json_encode($options));
  320. }
  321. }