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