ILIAS  release_8 Revision v8.23
class.ilPDFGeneratorUtils.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
31  public static function prepareGenerationRequest(string $service, string $purpose): void
32  {
33  try {
34  $map = self::getRendererMapForPurpose($service, $purpose);
35  $renderer = self::getRendererInstance($map['selected']);
36  $renderer->prepareGenerationRequest($service, $purpose);
37  } catch (Exception $e) {
38  return;
39  }
40  }
41 
42  public static function getTestPdfDir(): string
43  {
44  $iliasPDFTestPath = 'data/' . CLIENT_ID . '/pdf_service/';
45  if (!is_dir($iliasPDFTestPath) && !mkdir($iliasPDFTestPath) && !is_dir($iliasPDFTestPath)) {
46  throw new RuntimeException(sprintf('Directory "%s" was not created', $iliasPDFTestPath));
47  }
48 
49  return $iliasPDFTestPath;
50  }
51 
52  public static function removePrintMediaDefinitionsFromStyleFile(string $path): void
53  {
54  foreach (glob($path . '*.css') as $filename) {
55  $content = file_get_contents($filename);
56  $content = preg_replace('/@media[\s]* print/', '@media nothing', $content);
57  file_put_contents($filename, $content);
58  }
59  }
60 
61  public static function removeWrongPathFromStyleFiles(string $path): void
62  {
63  foreach (glob($path . '*.css') as $filename) {
64  $content = file_get_contents($filename);
65  $content = preg_replace('/src:\surl\([\',\"](..\/)*(\S)/', "src: url(./$2", $content);
66  file_put_contents($filename, $content);
67  }
68  }
69 
73  public static function getPurposeMap(): array
74  {
75  global $DIC;
76  $ilDB = $DIC->database();
77 
78  $purposes = [];
79 
80  $result = $ilDB->query('SELECT service, purpose FROM pdfgen_purposes ORDER BY service, purpose');
81 
82  while ($row = $ilDB->fetchAssoc($result)) {
83  $purposes[$row['service']][] = $row['purpose'];
84  }
85 
86  return $purposes;
87  }
88 
92  public static function getSelectionMap(): array
93  {
94  global $DIC;
95  $ilDB = $DIC->database();
96 
97  $mappings = [];
98  $result = $ilDB->query('SELECT service, purpose, preferred, selected FROM pdfgen_map');
99 
100  while ($row = $ilDB->fetchAssoc($result)) {
101  $mappings[$row['service']][$row['purpose']]['selected'] = $row['selected'];
102  $mappings[$row['service']][$row['purpose']]['preferred'] = $row['preferred'];
103  }
104 
105  return $mappings;
106  }
107 
111  public static function getRenderers(): array
112  {
113  global $DIC;
114  $ilDB = $DIC->database();
115 
116  $renderers = [];
117  $result = $ilDB->query('SELECT renderer, service, purpose FROM pdfgen_renderer_avail');
118 
119  while ($row = $ilDB->fetchAssoc($result)) {
120  $renderers[$row['service']][$row['purpose']][] = $row['renderer'];
121  }
122 
123  return $renderers;
124  }
125 
126  public static function updateRendererSelection(string $service, string $purpose, string $renderer): void
127  {
128  global $DIC;
129  $ilDB = $DIC->database();
130 
131  $ilDB->update(
132  'pdfgen_map',
133  ['selected' => ['text', $renderer]],
134  [
135  'service' => ['text', $service],
136  'purpose' => ['text', $purpose]
137  ]
138  );
139  }
140 
141  public static function getRendererConfig(string $service, string $purpose, string $renderer)
142  {
143  global $DIC;
144  $ilDB = $DIC->database();
145 
146  $query = 'SELECT config FROM pdfgen_conf WHERE renderer = ' . $ilDB->quote($renderer, 'text') .
147  ' AND service = ' . $ilDB->quote($service, 'text') . ' AND purpose = ' . $ilDB->quote($purpose, 'text');
148 
149  $result = $ilDB->query($query);
150  if ($ilDB->numRows($result) === 0) {
151  return self::getRendererDefaultConfig($service, $purpose, $renderer);
152  }
153 
154  $row = $ilDB->fetchAssoc($result);
155  return json_decode($row['config'], true, 512, JSON_THROW_ON_ERROR);
156  }
157 
161  public static function getRendererDefaultConfig(string $service, string $purpose, string $renderer)
162  {
163  $class_instance = self::getRendererInstance($renderer);
164 
165  return $class_instance->getDefaultConfig($service, $purpose);
166  }
167 
168  public static function removeRendererConfig(string $service, string $purpose, string $renderer): void
169  {
170  global $DIC;
171  $ilDB = $DIC->database();
172 
173  $query = 'DELETE FROM pdfgen_conf WHERE renderer = ' . $ilDB->quote($renderer, 'text') .
174  ' AND service = ' . $ilDB->quote($service, 'text') . ' AND purpose = ' . $ilDB->quote($purpose, 'text');
175 
176  $ilDB->manipulate($query);
177  }
178 
183  public static function getRendererInstance(string $renderer)
184  {
185  global $DIC;
186 
187  $ilDB = $DIC->database();
188 
189  $result = $ilDB->query('SELECT path FROM pdfgen_renderer WHERE renderer = ' . $ilDB->quote($renderer, 'text'));
190 
191  if ($ilDB->numRows($result) === 0) {
192  throw new Exception('No such renderer - given: ' . $renderer);
193  }
194  $row = $ilDB->fetchAssoc($result);
195 
196  $classname = 'il' . $renderer . 'Renderer';
197  if (self::isRendererPlugin($row['path'])) {
198  $classname = 'il' . $renderer . 'RendererPlugin';
199  }
200 
201  return new $classname();
202  }
203 
204  protected static function isRendererPlugin(string $path): bool
205  {
206  $needle = 'Plugin.php';
207  $length = strlen($needle);
208  return (substr($path, -$length) === $needle);
209  }
210 
211  public static function saveRendererPurposeConfig(string $service, string $purpose, string $renderer, array $config): void
212  {
213  global $DIC;
214  $ilDB = $DIC->database();
215 
216  $query = 'DELETE FROM pdfgen_conf WHERE renderer = ' . $ilDB->quote($renderer, 'text') .
217  ' AND service = ' . $ilDB->quote($service, 'text') . ' AND purpose = ' . $ilDB->quote($purpose, 'text');
218 
219  $ilDB->manipulate($query);
220 
221  $ilDB->insert(
222  'pdfgen_conf',
223  [
224  'conf_id' => ['integer', $ilDB->nextId('pdfgen_conf')],
225  'renderer' => ['text', $renderer],
226  'service' => ['text', $service],
227  'purpose' => ['text', $purpose],
228  'config' => ['clob', json_encode($config, JSON_THROW_ON_ERROR)]
229  ]
230  );
231  }
232 
238  public static function getRendererMapForPurpose(string $service, string $purpose): array
239  {
240  global $DIC;
241  $ilDB = $DIC->database();
242 
243  $result = $ilDB->query(
244  'SELECT preferred, selected FROM pdfgen_map WHERE service = ' . $ilDB->quote($service, 'text') . ' ' .
245  'AND purpose=' . $ilDB->quote($purpose, 'text')
246  );
247 
248  if ($ilDB->numRows($result) === 0) {
249  return ['selected' => 'TCPDF', 'preferred' => 'TCPDF'];
250  }
251 
252  return $ilDB->fetchAssoc($result);
253  }
254 }
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
static saveRendererPurposeConfig(string $service, string $purpose, string $renderer, array $config)
static getRendererConfig(string $service, string $purpose, string $renderer)
static getRendererInstance(string $renderer)
$path
Definition: ltiservices.php:32
global $DIC
Definition: feed.php:28
static removeWrongPathFromStyleFiles(string $path)
static getRendererDefaultConfig(string $service, string $purpose, string $renderer)
const CLIENT_ID
Definition: constants.php:41
static removeRendererConfig(string $service, string $purpose, string $renderer)
static getRendererMapForPurpose(string $service, string $purpose)
$query
$filename
Definition: buildRTE.php:78
static isRendererPlugin(string $path)
static removePrintMediaDefinitionsFromStyleFile(string $path)
static updateRendererSelection(string $service, string $purpose, string $renderer)
static prepareGenerationRequest(string $service, string $purpose)
Prepare the content processing for a PDF generation request This function should be called as in a re...
$service
Definition: ltiservices.php:43