ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
BlogHtmlExport.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
23 use ilFileUtils;
24 
31 {
32  protected \ilObjBlog $blog;
33  protected \ilObjBlogGUI $blog_gui;
34  protected string $export_dir;
35  protected string $sub_dir;
36  protected string $target_dir;
37  protected \ILIAS\GlobalScreen\Services $global_screen;
38  protected \ILIAS\Services\Export\HTML\Util $export_util;
39  protected \ilCOPageHTMLExport $co_page_html_export;
40  protected \ilLanguage $lng;
41  protected \ilTabsGUI $tabs;
42  protected array $items;
43  protected static array $keyword_export_map;
44  protected array $keywords;
45  protected bool $include_comments = false;
46  protected bool $print_version = false;
47  protected static bool $export_key_set = false;
48  protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
49 
50  public function __construct(
51  \ilObjBlogGUI $blog_gui,
52  string $exp_dir,
53  string $sub_dir,
54  bool $set_export_key = true
55  ) {
56  global $DIC;
57 
58  $this->blog_gui = $blog_gui;
60  $blog = $blog_gui->getObject();
61  $this->blog = $blog;
62  $this->export_dir = $exp_dir;
63  $this->sub_dir = $sub_dir;
64  $this->target_dir = $exp_dir . "/" . $sub_dir;
65 
66  $this->global_screen = $DIC->globalScreen();
67  $this->export_util = new \ILIAS\Services\Export\HTML\Util($exp_dir, $sub_dir);
68  $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir);
69  $this->tabs = $DIC->tabs();
70  $this->lng = $DIC->language();
71 
72  $this->items = $this->blog_gui->getItems();
73  $this->keywords = $this->blog_gui->getKeywords(false);
74  if ($set_export_key && !self::$export_key_set) {
75  self::$export_key_set = true;
76  $this->global_screen->tool()->context()->current()->addAdditionalData(
78  true
79  );
80  }
81 
82  $cs = $DIC->contentStyle();
83  if ($this->blog_gui->getIdType() === \ilObject2GUI::REPOSITORY_NODE_ID) {
84  $this->content_style_domain = $cs->domain()->styleForRefId($this->blog->getRefId());
85  } else {
86  $this->content_style_domain = $cs->domain()->styleForObjId($this->blog->getId());
87  }
88  }
89  protected function init(): void
90  {
91  }
92 
93  public function setPrintVersion(bool $print_version): void
94  {
95  $this->print_version = $print_version;
96  }
97 
98  public function includeComments(
99  bool $a_include_comments
100  ): void {
101  $this->include_comments = $a_include_comments;
102  }
103 
104  protected function initDirectories(): void
105  {
106  // initialize temporary target directory
107  ilFileUtils::delDir($this->target_dir);
108  ilFileUtils::makeDir($this->target_dir);
109  }
110 
116  public function exportHTML(): string
117  {
118  $this->initDirectories();
119  $this->export_util->exportSystemStyle();
120  $this->export_util->exportCOPageFiles(
121  $this->content_style_domain->getEffectiveStyleId(),
122  "blog"
123  );
124 
125  // export banner
126  $this->exportBanner();
127 
128  // export pages
129  if ($this->print_version) {
130  $this->exportHTMLPagesPrint();
131  } else {
132  $this->exportHTMLPages();
133  }
134 
135  // export comments user images
136  $this->exportUserImages();
137 
138  $this->export_util->exportResourceFiles();
139  $this->co_page_html_export->exportPageElements();
140 
141  return $this->zipPackage();
142  }
143 
144  protected function exportUserImages(): void
145  {
146  if ($this->include_comments) {
147  $user_export = new \ILIAS\Notes\Export\UserImageExporter();
148  $user_export->exportUserImagesForRepObjId($this->target_dir, $this->blog->getId());
149  }
150  }
151 
152  protected function exportBanner(): void
153  {
154  // banner / profile picture
155  $blga_set = new \ilSetting("blga");
156  if ($blga_set->get("banner")) {
157  $banner = $this->blog->getImageFullPath();
158  if ($banner) {
159  copy($banner, $this->target_dir . "/" . basename($banner));
160  }
161  }
162  // page element: profile picture
164  }
165 
166  public function zipPackage(): string
167  {
168  // zip it all
169  $date = time();
170  $type = ($this->include_comments)
171  ? "html_comments"
172  : "html";
173  $zip_file = \ilExport::_getExportDirectory($this->blog->getId(), $type, "blog") .
174  "/" . $date . "__" . IL_INST_ID . "__" .
175  $this->blog->getType() . "_" . $this->blog->getId() . ".zip";
176  ilFileUtils::zip($this->target_dir, $zip_file);
177  ilFileUtils::delDir($this->target_dir);
178  return $zip_file;
179  }
180 
186  public function exportHTMLPages(
187  string $a_link_template = null,
188  ?\Closure $a_tpl_callback = null,
189  ?\ilCOPageHTMLExport $a_co_page_html_export = null,
190  string $a_index_name = "index.html"
191  ): void {
192  if (!$a_link_template) {
193  $a_link_template = "bl{TYPE}_{ID}.html";
194  }
195 
196  if ($a_co_page_html_export) {
197  $this->co_page_html_export = $a_co_page_html_export;
198  }
199 
200  // lists
201 
202  // global nav
203  $nav = $this->blog_gui->renderNavigation("", "", $a_link_template);
204 
205  // month list
206  $has_index = false;
207  foreach (array_keys($this->items) as $month) {
208  $list = $this->blog_gui->renderList($this->items[$month], "render", $a_link_template, false, $this->target_dir);
209 
210  if (!$list) {
211  continue;
212  }
213 
214  if (!$a_tpl_callback) {
215  $tpl = $this->getInitialisedTemplate();
216  } else {
217  $tpl = $a_tpl_callback();
218  }
219 
220  $file = self::buildExportLink($a_link_template, "list", $month, $this->keywords);
221  $file = $this->writeExportFile($file, $tpl, $list, $nav);
222 
223  if (!$has_index) {
224  copy($file, $this->target_dir . "/" . $a_index_name);
225  $has_index = true;
226  }
227  }
228 
229  // keywords
230  foreach (array_keys($this->blog_gui->getKeywords(false)) as $keyword) {
231  $list_items = $this->blog_gui->filterItemsByKeyword($this->items, $keyword);
232  $list = $this->blog_gui->renderList($list_items, "render", $a_link_template, false, $this->target_dir);
233 
234  if (!$list) {
235  continue;
236  }
237 
238  if (!$a_tpl_callback) {
239  $tpl = $this->getInitialisedTemplate();
240  } else {
241  $tpl = $a_tpl_callback();
242  }
243 
244  $file = self::buildExportLink($a_link_template, "keyword", $keyword, $this->keywords);
245  $file = $this->writeExportFile($file, $tpl, $list, $nav);
246  }
247 
248 
249  // single postings
250 
251  $pages = \ilBlogPosting::getAllPostings($this->blog->getId(), 0);
252  foreach ($pages as $page) {
253  if (\ilBlogPosting::_exists("blp", $page["id"])) {
254  $blp_gui = new \ilBlogPostingGUI(0, null, $page["id"]);
255  $blp_gui->setOutputMode("offline");
256  $blp_gui->setFullscreenLink("fullscreen.html"); // #12930 - see page.xsl
257  $blp_gui->add_date = true;
258  $page_content = $blp_gui->showPage();
259 
260  $back = self::buildExportLink(
261  $a_link_template,
262  "list",
263  substr($page["created"]->get(IL_CAL_DATE), 0, 7),
264  $this->keywords
265  );
266 
267  $file = self::buildExportLink($a_link_template, "posting", (string) $page["id"], $this->keywords);
268 
269  if (!$a_tpl_callback) {
270  $tpl = $this->getInitialisedTemplate();
271  } else {
272  $tpl = $a_tpl_callback();
273  }
274 
276  ? $blp_gui->getCommentsHTMLExport()
277  : "";
278 
279  // posting nav
280  $nav = $this->blog_gui->renderNavigation(
281  "",
282  "",
283  $a_link_template,
284  false,
285  $page["id"]
286  );
287 
288  $this->writeExportFile($file, $tpl, $page_content, $nav, (bool) $back, $comments);
289 
290  $this->co_page_html_export->collectPageElements("blp:pg", $page["id"]);
291  }
292  }
293 
294  if (!$has_index) {
295  if (!$a_tpl_callback) {
296  $tpl = $this->getInitialisedTemplate();
297  } else {
298  $tpl = $a_tpl_callback();
299  }
300  $file = $this->writeExportFile($a_index_name, $tpl, "", $nav);
301  }
302  }
303 
307  public function exportHTMLPagesPrint(): void
308  {
309  $this->collectAllPagesPageElements($this->co_page_html_export);
310 
311  // render print view
312  $print_view = $this->blog_gui->getPrintView();
313  $print_view->setOffline(true);
314  $html = $print_view->renderPrintView();
315  file_put_contents($this->target_dir . "/index.html", $html);
316  }
317 
318 
319  public function collectAllPagesPageElements(\ilCOPageHTMLExport $co_page_html_export): void
320  {
321  $pages = \ilBlogPosting::getAllPostings($this->blog->getId(), 0);
322  foreach ($pages as $page) {
323  if (\ilBlogPosting::_exists("blp", $page["id"])) {
324  $co_page_html_export->collectPageElements("blp:pg", $page["id"]);
325  }
326  }
327  }
328 
332  public static function buildExportLink(
333  string $a_template,
334  string $a_type,
335  string $a_id,
336  array $keywords
337  ): string {
338  switch ($a_type) {
339  case "list":
340  $a_type = "m";
341  break;
342 
343  case "keyword":
344  if (!isset(self::$keyword_export_map)) {
345  self::$keyword_export_map = array_flip(array_keys($keywords));
346  }
347  $a_id = (string) (self::$keyword_export_map[$a_id] ?? "");
348  $a_type = "k";
349  break;
350 
351  default:
352  $a_type = "p";
353  break;
354  }
355 
356  return str_replace(array("{TYPE}", "{ID}"), array($a_type, $a_id), $a_template);
357  }
358 
362  protected function getInitialisedTemplate(
363  string $a_back_url = ""
365  global $DIC;
366 
367  $this->global_screen->layout()->meta()->reset();
368 
369  $location_stylesheet = \ilUtil::getStyleSheetLocation();
370  $this->global_screen->layout()->meta()->addCss($location_stylesheet);
371  $this->global_screen->layout()->meta()->addCss(
372  \ilObjStyleSheet::getContentStylePath($this->content_style_domain->getEffectiveStyleId())
373  );
375 
376  $tabs = $DIC->tabs();
377  $tabs->clearTargets();
378  $tabs->clearSubTabs();
379  if ($a_back_url) {
380  $tabs->setBackTarget($this->lng->txt("back"), $a_back_url);
381  }
382  $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
383 
384  $this->co_page_html_export->getPreparedMainTemplate($tpl);
385 
386  $this->blog_gui->renderFullscreenHeader($tpl, $this->blog->getOwner(), true);
387 
388  return $tpl;
389  }
390 
395  protected function writeExportFile(
396  string $a_file,
397  \ilGlobalPageTemplate $a_tpl,
398  string $a_content,
399  string $a_right_content = "",
400  bool $a_back = false,
401  string $comments = ""
402  ): string {
403  $file = $this->target_dir . "/" . $a_file;
404  // return if file is already existing
405  if (is_file($file)) {
406  return "";
407  }
408 
409  // export template: page content
410  $ep_tpl = new \ilTemplate(
411  "tpl.export_page.html",
412  true,
413  true,
414  "Modules/Blog"
415  );
416  if ($a_back) {
417  $ep_tpl->setVariable("PAGE_CONTENT", $a_content);
418  $ep_tpl->setVariable("COMMENTS", $comments);
419  } else {
420  $ep_tpl->setVariable("LIST", $a_content);
421  }
422  $a_tpl->setContent($ep_tpl->get());
423  unset($ep_tpl);
424 
425  // template: right content
426  if ($a_right_content) {
427  $a_tpl->setRightContent($a_right_content);
428  }
429 
430  $content = $a_tpl->printToString();
431 
432  // open file
433  file_put_contents($file, $content);
434 
435  return $file;
436  }
437 }
collectAllPagesPageElements(\ilCOPageHTMLExport $co_page_html_export)
static copyProfilePicturesToDirectory(int $a_user_id, string $a_dir)
const IL_INST_ID
Definition: constants.php:40
static getAllPostings(int $a_blog_id, int $a_limit=1000, int $a_offset=0)
Get all postings of blog.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
collectPageElements(string $a_type, int $a_id, string $lang="")
Collect page elements (that need to be exported separately)
static getStyleSheetLocation(string $mode="output", string $a_css_name="", string $a_css_location="")
get full style sheet file name (path inclusive) of current user
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object.
ilCOPageHTMLExport $co_page_html_export
exportHTMLPages(string $a_link_template=null, ?\Closure $a_tpl_callback=null, ?\ilCOPageHTMLExport $a_co_page_html_export=null, string $a_index_name="index.html")
Export all pages (note: this one is called from the portfolio html export!)
global $DIC
Definition: feed.php:28
ILIAS Services Export HTML Util $export_util
includeComments(bool $a_include_comments)
getInitialisedTemplate(string $a_back_url="")
Get initialised template.
setRightContent(string $a_html)
Sets content of right column.
Class ilObjBlogGUI.
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:62
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
$comments
exportHTMLPagesPrint()
Export all pages as one print version.
printToString()
Use this method to get the finally rendered page as string.
static getContentStylePath(int $a_style_id, bool $add_random=true, bool $add_token=true)
get content style path static (to avoid full reading)
static resetInitialState()
Reset initial state (for exports)
const IL_CAL_DATE
static zip(string $a_dir, string $a_file, bool $compress_content=false)
writeExportFile(string $a_file, \ilGlobalPageTemplate $a_tpl, string $a_content, string $a_right_content="", bool $a_back=false, string $comments="")
Write HTML to file.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ILIAS GlobalScreen Services $global_screen
setPrintVersion(bool $print_version)
static buildExportLink(string $a_template, string $a_type, string $a_id, array $keywords)
Build static export link.
setContent(string $a_html)
Sets content for standard template.
ILIAS Style Content Object ObjectFacade $content_style_domain
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...