· 6 years ago · Apr 16, 2020, 06:24 PM
1<?php
2
3class DocumentTypes
4{
5 public const TYPE_1 = 1;
6 public const TYPE_2 = 2;
7 public const TYPE_3 = 3;
8
9 public const TYPE_1_NAME = 'name 1';
10 public const TYPE_2_NAME = 'name 1';
11 public const TYPE_3_NAME = 'name 3';
12
13 public static function getTypes(): array
14 {
15 return [
16 self::TYPE_1 => self::TYPE_1_NAME,
17 self::TYPE_2 => self::TYPE_2_NAME,
18 self::TYPE_3 => self::TYPE_3_NAME,
19 ];
20 }
21
22 public static function isType1(string $type): bool
23 {
24 return $type === self::TYPE_1_NAME;
25 }
26
27 public static function isType2(string $type): bool
28 {
29 return $type === self::TYPE_2;
30 }
31
32 public static function isType3(string $type): bool
33 {
34 return $type === self::TYPE_3;
35 }
36
37}
38
39
40class Documents
41{
42 /** @var string Токен для запроса api.mrt.nark.ru/admin/quizzes/ */
43 public const TOKEN = 'JBxoyJADeeUPonyNpKijBoGhkYOrnDsJRrlgvlvsYe';
44
45 public $getURL = [
46 "http://document.dev-lab.site/charts/bar.php?id=%d&ids=%d",
47 "http://document.dev-lab.site/charts/maps.php?id=%d&ids=%d",
48 "http://document.dev-lab.site/charts/chartbar.php?id=%d&ids=%d",
49 "http://document.dev-lab.site/charts/data.php?id=%d&ids=%d",
50 "http://document.dev-lab.site/charts/doughnut.php?id=%d&ids=%d",
51 ];
52
53 public $getURLJson = [
54 "http://document.dev-lab.site/charts/bar.php?id=%d&ids=%d",
55 "http://document.dev-lab.site/charts/maps.php?id=%d&ids=%d",
56 "http://doc.test/charts/chartbar.php?id=%d&ids=%d&json=1",
57 "http://document.dev-lab.site/charts/data.php?id=%d&ids=%d",
58 "http://document.dev-lab.site/charts/doughnut.php?id=%d&ids=%d",
59 ];
60
61 /**
62 * @param $type // Тип запроса
63 * @param $apiID // ID запроса
64 *
65 * @return mixed
66 */
67 public function SetAPI($type, $apiID)
68 {
69 if (empty($apiID)) {
70 return null;
71 }
72
73 $headers = [];
74
75 switch ($type) {
76 case 2:
77 $token = self::TOKEN;
78 $url = "https://api.mrt.nark.ru/admin/quizzes/".$apiID;
79 $headers[] = "X-Auth:".$token;
80 break;
81 case 3:
82 $url = "https://api.mrt.nark.ru/analyze_report/".$apiID;
83 break;
84 default:
85 $url = "https://api.mrt.nark.ru/admin/reports/".$apiID;
86 break;
87 }
88
89 $state_ch = curl_init();
90 curl_setopt($state_ch, CURLOPT_URL, $url);
91 curl_setopt($state_ch, CURLOPT_RETodeURNTRANSFER, true);
92 curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
93 $state_result = curl_exec($state_ch);
94 $state_result = json_decode($state_result, true);
95
96 if ($state_result['code'] === 200) {
97 return $state_result;
98 }
99
100 return null;
101 }
102
103 /**
104 * @param string $type
105 * @param string $typeChart
106 * @param array $data
107 *
108 * @return mixed
109 */
110 public function isType(string $type, string $typeChart, array $data): bool
111 {
112 return ($data['data']['result_chart_type'] === $type)
113 && ($data['data']['result_type'] === $typeChart);
114 }
115
116 /**
117 * @param $id
118 * @param $type
119 * @param $chart
120 *
121 * @return bool
122 */
123 public function createDir($id, $type, $chart, $rowsid): bool
124 {
125 $isCreated = false;
126
127 if (!file_exists('../pictures/'.$id.'/'.$rowsid.'_'.$type.'_'.$chart.'.png')
128 && !file_exists('../pictures/'.$id.'')
129 ) {
130 $isCreated = mkdir('../pictures/'.$id.'', 0777);
131 }
132
133 return $isCreated;
134 }
135
136 /**
137 * @param $chartType
138 * @param $resultId
139 *
140 * @return string
141 */
142 public function getChartURL($chartType, $resultId, $rowsid)
143 {
144 return sprintf($this->getURL[$chartType], $resultId, $rowsid);
145 }
146
147 public function getChartLegend($chartType, $resultId, $rowsid)
148 {
149 return sprintf($this->getURLJson[$chartType], $resultId, $rowsid);
150 }
151
152 /**
153 * @param $mpdf
154 */
155 public function getTitleChartPDF($mpdf)
156 {
157 if (!empty($this->setType->data->title)) {
158 $formatSizeTitle = "<div class='header_style'><h1>%s</h1>%s</div>";
159 $mpdf->WriteHTML(sprintf($formatSizeTitle, $this->setType->data->title, $this->setType->data->description));
160 //$mpdf->AddPage();
161
162 return $mpdf;
163 }
164
165 return null;
166 }
167
168 /**
169 * @param $mpdf
170 * @param $title
171 *
172 * @return mixed
173 */
174 public function getTitleChartPDFTable($mpdf, $title)
175 {
176 if (!empty($title)) {
177 $formatSizeTitle = "<div class='header_style'><h1>%s</h1></div>";
178 $mpdf->WriteHTML(sprintf($formatSizeTitle, $title));
179 //$mpdf->AddPage();
180
181 return $mpdf;
182 }
183 }
184
185 /**
186 * @param $section
187 */
188 public function getTitleChartWord($section)
189 {
190 if (!empty($this->setType->data->title)) {
191 //$section->addTitle(htmlspecialchars($this->setType->data->title), 1);
192 // $section->addTextBreak(1);
193 $params1 = ['name' => 'Times New Roman',
194 'size' => 20,
195 'bold' => true,
196 'align' => 'center',
197 ];
198 $section->addText(
199 htmlspecialchars($this->setType->data->title),
200 $params1,
201 [
202 'spaceAfter' => 250,
203 '
204 spaceBefore' => 250,
205 ]
206 );
207 }
208 }
209
210 public function getTitleChartPoint($objPHPPresentation, $color, $colorCopy, $key)
211 {
212 if (!empty($this->setType->data->title)) {
213 $currentSlide = $objPHPPresentation->setActiveSlideIndex($key);
214 $shape = $currentSlide->createRichTextShape()
215 ->setHeight(800)
216 ->setWidth(950)
217 ->setOffsetX(0)
218 ->setOffsetY(0);
219 $textRun = $shape->createTextRun($this->setType->data->title);
220 $textRun->getFont()
221 ->setBold(true)
222 ->setSize(20)
223 ->setColor($color);
224 $textRun = $shape->createParagraph()->createTextRun($this->setType->data->description);
225 $textRun->getFont()
226 ->setBold(false)
227 ->setSize(12)
228 ->setColor($colorCopy);
229 }
230 }
231
232 /**
233 * @param $chartType
234 * @param $API
235 * @param $type
236 * @param $chart
237 * @param $rowsid
238 *
239 * @return void
240 */
241 public function generateImage($chartType, $API, $type, $chart, $rowsid)
242 {
243 $formatSizeBuffer = 'xvfb-run --auto-servernum --server-num=1 /usr/bin/cutycapt --delay=7000 --url="%s" --out=%s/pictures/%d/%d_%s_%s.png --min-width=1900 --min-height=1000';
244 shell_exec(sprintf($formatSizeBuffer,
245 $this->getChartURL($chartType, $API, $rowsid),
246 $_SERVER['DOCUMENT_ROOT'],
247 $API, $rowsid,
248 $this->setType->data->result_chart_type,
249 $this->setType->data->result_type)
250 );
251 $this->createDir($API, $type, $chart, $rowsid);
252 }
253
254 public function getLenend($chartType, $API, $rowsid)
255 {
256 $headers = [];
257 $json = curl_init();
258 curl_setopt($json, CURLOPT_URL, $this->getChartLegend($chartType, $API, $rowsid));
259 curl_setopt($json, CURLOPT_RETURNTRANSFER, true);
260 curl_setopt($json, CURLOPT_HTTPHEADER, $headers);
261 $jsonResult = curl_exec($json);
262 $jsonResult = json_decode($jsonResult);
263 return $jsonResult;
264 }
265
266}