ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilPDFGeneratorUtils.php
Go to the documentation of this file.
1 <?php
2 
7 {
16  public static function prepareGenerationRequest($service, $purpose)
17  {
18  try {
19  $map = self::getRendererMapForPurpose($service, $purpose);
20  $renderer = self::getRendererInstance($map['selected']);
21  $renderer->prepareGenerationRequest($service, $purpose);
22  } catch (Exception $e) {
23  return;
24  }
25  }
26 
27  public static function getTestPdfDir()
28  {
29  $iliasPDFTestPath = 'data/' . CLIENT_ID . '/pdf_service/';
30  if (!file_exists($iliasPDFTestPath)) {
31  mkdir($iliasPDFTestPath);
32  }
33  return $iliasPDFTestPath;
34  }
35 
40  {
41  foreach (glob($path . '*.css') as $filename) {
42  $content = file_get_contents($filename);
43  $content = preg_replace('/@media[\s]* print/', '@media nothing', $content);
44  file_put_contents($filename, $content);
45  }
46  }
47 
51  public static function removeWrongPathFromStyleFiles($path)
52  {
53  foreach (glob($path . '*.css') as $filename) {
54  $content = file_get_contents($filename);
55  $content = preg_replace('/src:\surl\([\',\"](..\/)*(\S)/', "src: url(./$2", $content);
56  file_put_contents($filename, $content);
57  }
58  }
59 
63  public static function setCheckedIfTrue(\ilPropertyFormGUI $form)
64  {
65  foreach ($form->getItems() as $item) {
66  if ($item instanceof ilCheckboxInputGUI) {
67  if ($item->getChecked() != null && ($item->getValue() == true || $item->getValue() == '1')) {
68  $item->setChecked(true);
69  }
70  }
71  }
72  }
73 
74  public static function getPurposeMap()
75  {
76  global $DIC;
77  $ilDB = $DIC['ilDB'];
78 
79  $purposes = array();
80 
81  $result = $ilDB->query('SELECT service, purpose FROM pdfgen_purposes ORDER BY service, purpose');
82 
83  while ($row = $ilDB->fetchAssoc($result)) {
84  $purposes[$row['service']][] = $row['purpose'];
85  }
86 
87  return $purposes;
88  }
89 
90  public static function getSelectionMap()
91  {
92  global $DIC;
93  $ilDB = $DIC['ilDB'];
94 
95  $mappings = array();
96  $result = $ilDB->query('SELECT service, purpose, preferred, selected FROM pdfgen_map');
97 
98  while ($row = $ilDB->fetchAssoc($result)) {
99  $mappings[$row['service']][$row['purpose']]['selected'] = $row['selected'];
100  $mappings[$row['service']][$row['purpose']]['preferred'] = $row['preferred'];
101  }
102 
103  return $mappings;
104  }
105 
106  public static function getRenderers()
107  {
108  global $DIC;
109  $ilDB = $DIC['ilDB'];
110 
111  $renderers = array();
112  $result = $ilDB->query('SELECT renderer, service, purpose FROM pdfgen_renderer_avail');
113 
114  while ($row = $ilDB->fetchAssoc($result)) {
115  $renderers[$row['service']][$row['purpose']][] = $row['renderer'];
116  }
117 
118  return $renderers;
119  }
120 
126  public static function updateRendererSelection($service, $purpose, $renderer)
127  {
128  global $DIC;
129  $ilDB = $DIC['ilDB'];
130 
131  $ilDB->update(
132  'pdfgen_map',
133  array( 'selected' => array('text', $renderer) ),
134  array(
135  'service' => array('text', $service),
136  'purpose' => array('text', $purpose)
137  )
138  );
139  }
140 
147  public static function getRendererConfig($service, $purpose, $renderer)
148  {
149  global $DIC;
151  $ilDB = $DIC['ilDB'];
152 
153  $query = 'SELECT config FROM pdfgen_conf WHERE renderer = ' . $ilDB->quote($renderer, 'text') .
154  ' AND service = ' . $ilDB->quote($service, 'text') . ' AND purpose = ' . $ilDB->quote($purpose, 'text');
155 
156  $result = $ilDB->query($query);
157  if ($ilDB->numRows($result) == 0) {
158  return self::getRendererDefaultConfig($service, $purpose, $renderer);
159  } else {
160  $row = $ilDB->fetchAssoc($result);
161  return json_decode($row['config'], true);
162  }
163  }
164 
171  public static function getRendererDefaultConfig($service, $purpose, $renderer)
172  {
174  $class_instance = self::getRendererInstance($renderer);
175 
176  return $class_instance->getDefaultConfig($service, $purpose);
177  }
178 
184  public static function removeRendererConfig($service, $purpose, $renderer)
185  {
186  global $DIC;
187  $ilDB = $DIC['ilDB'];
188 
189  $query = 'DELETE FROM pdfgen_conf WHERE renderer = ' . $ilDB->quote($renderer, 'text') .
190  ' AND service = ' . $ilDB->quote($service, 'text') . ' AND purpose = ' . $ilDB->quote($purpose, 'text');
191 
192  $ilDB->manipulate($query);
193  }
194 
200  public static function getRendererInstance($renderer)
201  {
202  global $DIC;
204  $ilDB = $DIC['ilDB'];
205 
206  $result = $ilDB->query('SELECT path FROM pdfgen_renderer WHERE renderer = ' . $ilDB->quote($renderer, 'text'));
207 
208  if ($ilDB->numRows($result) == 0) {
209  throw new Exception('No such renderer - given: ' . $renderer);
210  }
211  $row = $ilDB->fetchAssoc($result);
212 
213  include_once $row['path'];
214  if (self::isRendererPlugin($row['path'])) {
215  $classname = 'il' . $renderer . 'RendererPlugin';
216  } else {
217  $classname = 'il' . $renderer . 'Renderer';
218  }
219 
220  if (false && !class_exists($classname)) {
221  throw new Exception(
222  'Class failed loading, including path ' . $row['path']
223  . ' did not lead to class definition ' . $classname . ' being available.'
224  );
225  }
226 
227  $class_instance = new $classname;
228  return $class_instance;
229  }
230 
235  protected static function isRendererPlugin($path)
236  {
237  $needle = 'Plugin.php';
238  $length = strlen($needle);
239  return (substr($path, -$length) === $needle);
240  }
241 
248  public static function saveRendererPurposeConfig($service, $purpose, $renderer, $config)
249  {
250  global $DIC;
252  $ilDB = $DIC['ilDB'];
253 
254  $query = 'DELETE FROM pdfgen_conf WHERE renderer = ' . $ilDB->quote($renderer, 'text') .
255  ' AND service = ' . $ilDB->quote($service, 'text') . ' AND purpose = ' . $ilDB->quote($purpose, 'text');
256 
257  $ilDB->manipulate($query);
258 
259  $ilDB->insert(
260  'pdfgen_conf',
261  array(
262  'conf_id' => array('integer', $ilDB->nextId('pdfgen_conf')),
263  'renderer' => array('text', $renderer),
264  'service' => array('text', $service),
265  'purpose' => array('text', $purpose),
266  'config' => array('clob', json_encode($config))
267  )
268  );
269  }
270 
276  public static function getRendererMapForPurpose($service, $purpose)
277  {
278  global $DIC;
280  $ilDB = $DIC['ilDB'];
281 
282  $result = $ilDB->query('SELECT preferred, selected FROM pdfgen_map WHERE
283  service = ' . $ilDB->quote($service, 'text') . ' AND purpose=' . $ilDB->quote($purpose, 'text'));
284 
285  if ($ilDB->numRows($result) == 0) {
286  return array('selected' => 'TCPDF', 'preferred' => 'TCPDF');
287  }
288  $row = $ilDB->fetchAssoc($result);
289 
290  return $row;
291  }
292 }
$path
Definition: aliased.php:25
static removePrintMediaDefinitionsFromStyleFile($path)
$config
Definition: bootstrap.php:15
$result
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
This class represents a checkbox property in a property form.
static removeWrongPathFromStyleFiles($path)
static setCheckedIfTrue(\ilPropertyFormGUI $form)
if(isset($_POST['submit'])) $form
static updateRendererSelection($service, $purpose, $renderer)
$query
$filename
Definition: buildRTE.php:89
$row
static removeRendererConfig($service, $purpose, $renderer)
global $ilDB
Class ilPDFGeneratorUtils.
static prepareGenerationRequest($service, $purpose)
Prepare the content processing for a PDF generation request This function should be called as in a re...