zen.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * The zen file of file module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yidong Wang<yidong@easycorp.ltd>
  8. * @package file
  9. * @link https://www.zentao.net
  10. */
  11. class fileZen extends file
  12. {
  13. /**
  14. * 获取下载方式,是下载文件还是直接在页面打开查看。
  15. * Get download mode
  16. *
  17. * @param object $file
  18. * @param string $mouse
  19. * @access protected
  20. * @return string down|open
  21. */
  22. protected function getDownloadMode($file, $mouse)
  23. {
  24. $mode = 'down';
  25. $fileTypes = 'txt|jpg|jpeg|gif|png|bmp|xml|html|mp4';
  26. if(stripos($fileTypes, $file->extension) !== false && $mouse == 'left') $mode = 'open';
  27. return $mode;
  28. }
  29. /**
  30. * 构建下载列表表格。
  31. * Build download table.
  32. *
  33. * @param array $fields
  34. * @param array $rows
  35. * @param string $kind
  36. * @param array $rowspans
  37. * @param array $colspans
  38. * @access protected
  39. * @return string
  40. */
  41. protected function buildDownloadTable($fields, $rows, $kind, $rowspans = array(), $colspans = array())
  42. {
  43. $rows = array_values($rows);
  44. $host = common::getSysURL();
  45. $fileModel = $this->file;
  46. $output = "<table><tr>";
  47. $output .= implode("\n", array_map(function($fieldLabel){return "<th><nobr>$fieldLabel</nobr></th>";}, $fields));
  48. $output .= "</tr>";
  49. foreach($rows as $i => $row)
  50. {
  51. if(in_array($kind, array('story', 'bug', 'testcase'))) $row->title = html::a($host . $this->createLink($kind, 'view', "{$kind}ID=$row->id"), $row->title, '_blank');
  52. if($kind == 'task') $row->name = html::a($host . $this->createLink('task', 'view', "taskID=$row->id"), $row->name, '_blank');
  53. $output .= "<tr valign='top'>\n";
  54. $col = 0;
  55. $endColspan = 0;
  56. foreach($fields as $fieldName => $fieldLabel)
  57. {
  58. $col ++;
  59. if(!empty($endColspan) && $col < $endColspan) continue;
  60. if(isset($endRowspan[$fieldName]) && $i < $endRowspan[$fieldName]) continue;
  61. $fieldValue = zget($row, $fieldName, '');
  62. $rowspan = '';
  63. if(isset($rowspans[$i]) && isset($rowspans[$i]['rows'][$fieldName]))
  64. {
  65. $rowspan = "rowspan='{$rowspans[$i]['rows'][$fieldName]}'";
  66. $endRowspan[$fieldName] = $i + $rowspans[$i]['rows'][$fieldName];
  67. }
  68. $colspan = '';
  69. if(isset($colspans[$i]) && isset($colspans[$i]['cols'][$fieldName]))
  70. {
  71. $colspan = "colspan='{$colspans[$i]['cols'][$fieldName]}'";
  72. $endColspan = $col + $colspans[$i]['cols'][$fieldName];
  73. }
  74. if($fieldValue && is_string($fieldValue))
  75. {
  76. $fieldValue = preg_replace_callback('/ src="{([0-9]+)(\.(\w+))?}" /', function($matches) use($host, $fileModel)
  77. {
  78. $file = $fileModel->getById((int)$matches[1]);
  79. return $file ? ' src="' . $host . $file->webPath . '" ' : $matches[0];
  80. }, $fieldValue);
  81. }
  82. $output .= "<td $rowspan $colspan><nobr>$fieldValue</nobr></td>\n";
  83. }
  84. $output .= "</tr>\n";
  85. }
  86. $output .= "</table>";
  87. return $output;
  88. }
  89. /**
  90. * 删除真实文件。
  91. * Unlink real file.
  92. *
  93. * @param object $file
  94. * @access protected
  95. * @return void
  96. */
  97. protected function unlinkRealFile($file)
  98. {
  99. $fileRecord = $this->dao->select('id')->from(TABLE_FILE)->where('pathname')->eq($file->pathname)->fetch();
  100. if(empty($fileRecord)) $this->file->unlinkFile($file);
  101. }
  102. /**
  103. * 更新 fileName 字段。
  104. * Update fileName field.
  105. *
  106. * @param int $fileID
  107. * @access protected
  108. * @return array
  109. */
  110. protected function updateFileName($fileID)
  111. {
  112. $file = $this->file->getByID($fileID);
  113. if(!$file) return array('result' => 'fail', 'code' => 404, 'message' => $this->lang->file->fileNotFound);
  114. $data = fixer::input('post')->get();
  115. if(validater::checkLength($data->fileName, 80, 1) == false) return array('result' => 'fail', 'message' => sprintf($this->lang->error->length[1], $this->lang->file->title, 80, 1));
  116. $fileName = $data->fileName;
  117. if(isset($data->extension))
  118. {
  119. $extension = $data->extension;
  120. }
  121. else
  122. {
  123. $extension = strpos($fileName, '.') !== false ? end(explode('.', $fileName)) : $file->extension;
  124. }
  125. $this->dao->update(TABLE_FILE)
  126. ->set('title')->eq($fileName)
  127. ->beginIF($extension)->set('extension')->eq($extension)->fi()
  128. ->where('id')->eq($fileID)
  129. ->exec();
  130. if($file->objectType) $actionID = $this->loadModel('action')->create($file->objectType, $file->objectID, 'editfile', '', $fileName);
  131. $changes[] = array('field' => 'fileName', 'old' => $file->title, 'new' => $fileName, 'diff' => '');
  132. $this->action->logHistory($actionID, $changes);
  133. /* Update test case version for test case synchronization. */
  134. if($file->objectType == 'testcase' and $file->title != $fileName) $this->file->updateTestcaseVersion($file);
  135. return array('result' => 'success');
  136. }
  137. }