AutoGostController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Pockit\Controllers;
  3. // Контроллер автогоста
  4. use Pockit\Models\ReportModel;
  5. use Pockit\Models\SubjectModel;
  6. use Pockit\Models\WorkTypeModel;
  7. use Pockit\Models\TeacherModel;
  8. use Pockit\Views\AutoGostArchiveView;
  9. use Pockit\Views\AutoGostReportsView;
  10. use Pockit\Views\AutoGostEditView;
  11. use Pockit\Views\AutoGostNewReportView;
  12. use Pockit\Views\AutoGostPages\AutoGostPage;
  13. use Pockit\AutoGostSections\TitleSection;
  14. use Pockit\AutoGostSections\SubSection;
  15. class AutoGostController {
  16. // Загрузка изображений
  17. public static function uploadImage() {
  18. if (is_uploaded_file($_FILES['file']['tmp_name'])) {
  19. $mime_type = mime_content_type($_FILES['file']['tmp_name']);
  20. $filepath = tempnam(rootdir."/img/autogost", "rgnupload");
  21. if ($mime_type == "image/png") {
  22. // Конвертирование png в gif
  23. $png_image = imagecreatefrompng($_FILES['file']['tmp_name']);
  24. $gif_image = imagecreatetruecolor(imagesx($png_image), imagesy($png_image));
  25. imagecopy($gif_image, $png_image, 0, 0, 0, 0, imagesx($png_image), imagesy($png_image));
  26. imagegif($gif_image, $filepath);
  27. } else {
  28. // Просто перемещение файла
  29. $filepath = tempnam(rootdir."/img/autogost", "rgnupload");
  30. move_uploaded_file($_FILES['file']['tmp_name'], $filepath);
  31. }
  32. $output = ["ok"=>true, "filename"=>basename($filepath)];
  33. } else {
  34. $output = ["ok"=>false];
  35. }
  36. echo json_encode($output);
  37. }
  38. // Список отчётов по дисциплине
  39. public static function listReports($subject_id) {
  40. $reports = ReportModel::where("subject_id", $subject_id);
  41. $subject = SubjectModel::getById($subject_id);
  42. $view = new AutoGostReportsView([
  43. "page_title" => "Автогост: архив ".$subject['name'],
  44. "crumbs" => ["Главная" => "/", "Автогост: дисциплины" => "/autogost/archive", $subject['name'] => ""],
  45. "reports" => $reports,
  46. "subject" => $subject
  47. ]);
  48. $view->view();
  49. }
  50. // Архив отчётов
  51. public static function archive() {
  52. $subjects = SubjectModel::all();
  53. $view = new AutoGostArchiveView([
  54. "page_title" => "Автогост: архив",
  55. "crumbs" => ["Главная" => "/", "Автогост: дисциплины" => "/"],
  56. "subjects" => $subjects
  57. ]);
  58. $view->view();
  59. }
  60. // Редактирование отчёта
  61. public static function edit($report_id) {
  62. $report = ReportModel::getById($report_id);
  63. $subject = SubjectModel::getById($report['subject_id']);
  64. $filename = "Автогост - ".$subject['name']." #".$report['work_number']." - Королёв";
  65. $view = new AutoGostEditView([
  66. "page_title" => $filename,
  67. "crumbs" => ["Главная"=>"/", "Автогост: дисциплины" => "/autogost/archive/", $subject['name'] => "/autogost/archive/".$subject['id'], "Редактирование"=>"/"],
  68. "markup" => $report['markup'],
  69. "filename" => $filename,
  70. "report_id" => $report_id
  71. ]);
  72. $view->view();
  73. }
  74. // Валидация создания отчёта
  75. private static function validateCreation() {
  76. if (empty($_POST["number"])) {
  77. // Запрос на создание
  78. return 'Не указан номер работы';
  79. }
  80. return true;
  81. }
  82. // Создание отчёта
  83. public static function newReport() {
  84. $subjects = SubjectModel::all();
  85. $worktypes = WorkTypeModel::all();
  86. if (!empty($_POST)) {
  87. $response = self::validateCreation();
  88. if ($response === true) {
  89. // Валидация успешна
  90. // Создаём запись
  91. $work_type = WorkTypeModel::getById($_POST['work_type']);
  92. $report_id = ReportModel::create(
  93. $_POST["subject_id"],
  94. $_POST['work_type'],
  95. $_POST['number'],
  96. $_POST['notice'],
  97. "!-\n!\n#{$work_type['name_nom']} №{$_POST['number']}\n"
  98. );
  99. // Перенаправляем на предпросмотр этого отчёта
  100. header("Location: /autogost/edit/".$report_id);
  101. return;
  102. } else {
  103. // Валидация провалена
  104. $error_text = $response;
  105. }
  106. } else {
  107. $error_text = null;
  108. }
  109. $view = new AutoGostNewReportView([
  110. "page_title" => "Автогост: создание отчёта",
  111. 'subjects' => $subjects,
  112. 'worktypes' => $worktypes,
  113. 'error_text' => $error_text,
  114. "crumbs" => ["Главная"=>"/", "Автогост: создание отчёта" => "/"]
  115. ]);
  116. $view->view();
  117. }
  118. // Получение HTML
  119. public static function getHtml() {
  120. $report = ReportModel::getById($_POST['report_id']);
  121. $subject = SubjectModel::getById($report["subject_id"]);
  122. $work_type = WorkTypeModel::getById($report["work_type"]);
  123. $teacher = TeacherModel::getById($subject["teacher_id"]);
  124. $document = [];
  125. $current_page = 1;
  126. $current_img = 1; // Номер текущего рисунка
  127. $expr_is_raw_html = false; // Выражение - чистый HTML?
  128. $lines = explode("\n", $report['markup']);
  129. foreach ($lines as $expr) {
  130. if (mb_strlen($expr) == 0) {
  131. continue;
  132. }
  133. if ($expr[0] != "@") {
  134. // Выражение - обычный текст
  135. // FIXME: end($document) может быть false
  136. if ($expr_is_raw_html) {
  137. end($document)->addHTML($expr);
  138. } else {
  139. end($document)->addHTML("<p class='report-text'>".$expr."</p>");
  140. }
  141. continue;
  142. }
  143. $command = explode(":", $expr);
  144. $command_name = $command[0];
  145. switch ($command_name) {
  146. case "@titlepage":
  147. // Титульный лист
  148. $document[] = new TitleSection($current_page);
  149. $current_page++;
  150. break;
  151. case "@section":
  152. // Секция основной части
  153. $document[] = new SubSection($current_page, $command[1]);
  154. $current_page++;
  155. break;
  156. case "@\\":
  157. // Перенос строки
  158. end($document)->addHTML("<br/>");
  159. break;
  160. case "@img":
  161. // Изображение
  162. if (count($command) >= 4) {
  163. $imgwidth = "width='".$command[3]."'";
  164. } else {
  165. $imgwidth = "";
  166. }
  167. end($document)->addHTML(
  168. "<figure>
  169. <img ".$imgwidth." src='/img/autogost/".$command[1]."'>
  170. <figcaption>Рисунок ".$current_img." - ".$command[2]."</figcaption>
  171. </figure>"
  172. );
  173. $current_img++;
  174. break;
  175. case "@raw":
  176. // Чистый HTML
  177. $expr_is_raw_html = true;
  178. break;
  179. case "@endraw":
  180. // /Чистый HTML
  181. $expr_is_raw_html = false;
  182. break;
  183. case "@/":
  184. case "@-":
  185. // Разрыв страницы
  186. end($document)->pageBreak($current_page);
  187. $current_page++;
  188. break;
  189. default:
  190. throw new \Exception("Unknown command: ".$command_name);
  191. break;
  192. }
  193. }
  194. AutoGostPage::init(
  195. $subject,
  196. $teacher,
  197. $work_type,
  198. $current_page - 1,
  199. $report
  200. );
  201. echo "<!DOCTYPE html>";
  202. echo "<html>";
  203. echo "<head><link rel='stylesheet' href='/css/autogost-report.css'></head>";
  204. echo "<body><div id='preview'>";
  205. foreach ($document as $section) {
  206. $section->output();
  207. }
  208. echo "</div></body>";
  209. echo "</html>";
  210. }
  211. }