ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BlogHtmlExport.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ilFileUtils;
25
27{
28 protected \ILIAS\components\Export\HTML\ExportCollector $collector;
29 protected \ilObjBlog $blog;
30 protected \ilObjBlogGUI $blog_gui;
31 protected string $export_dir;
32 protected string $sub_dir;
33 protected string $target_dir;
34 protected \ILIAS\GlobalScreen\Services $global_screen;
35 protected Util $export_util;
36 protected \ilCOPageHTMLExport $co_page_html_export;
37 protected \ilLanguage $lng;
38 protected \ilTabsGUI $tabs;
39 protected array $items;
40 protected static array $keyword_export_map;
41 protected array $keywords;
42 protected bool $include_comments = false;
43 protected bool $print_version = false;
44 protected static bool $export_key_set = false;
45 protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
46
47 public function __construct(
49 string $exp_dir,
50 string $sub_dir,
51 bool $set_export_key = true
52 ) {
53 global $DIC;
54
55 $this->blog_gui = $blog_gui;
58 $this->collector = $DIC->export()->domain()->html()->collector($blog->getId());
59 $this->collector->init();
60
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 Util("", "", $this->collector);
68 $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir, null, 0, $this->collector);
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(): void
117 {
118 $this->initDirectories();
119
120 $this->export_util->exportSystemStyle(
121 [
122 "icon_blog.svg"
123 ]
124 );
125
126 $this->export_util->exportCOPageFiles(
127 $this->content_style_domain->getEffectiveStyleId(),
128 "blog"
129 );
130 /*
131 \ilObjUser::copyProfilePicturesToDirectory($this->blog->getOwner(), $this->target_dir);
132 */
133 // export pages
134 if ($this->print_version) {
135 $this->exportHTMLPagesPrint();
136 } else {
137 $this->exportHTMLPages();
138 }
139 /*
140 // export comments user images
141 $this->exportUserImages();
142 */
143 $this->export_util->exportResourceFiles();
144 $this->co_page_html_export->exportPageElements();
145 }
146
147 protected function exportUserImages(): void
148 {
149 if ($this->include_comments) {
150 $user_export = new \ILIAS\Notes\Export\UserImageExporter();
151 $user_export->exportUserImagesForRepObjId($this->target_dir, $this->blog->getId());
152 }
153 }
154
160 public function exportHTMLPages(
161 ?string $a_link_template = null,
162 ?\Closure $a_tpl_callback = null,
163 ?\ilCOPageHTMLExport $a_co_page_html_export = null,
164 string $a_index_name = "index.html"
165 ): void {
166 if (!$a_link_template) {
167 $a_link_template = "bl{TYPE}_{ID}.html";
168 }
169
170 if ($a_co_page_html_export) {
171 $this->co_page_html_export = $a_co_page_html_export;
172 }
173
174 // lists
175
176 // global nav
177 $nav = $this->blog_gui->renderNavigation("", "", $a_link_template);
178
179 // month list
180 $has_index = false;
181 foreach (array_keys($this->items) as $month) {
182 $list = $this->blog_gui->renderList($this->items[$month], "render", $a_link_template, false, $this->target_dir);
183
184 if (!$list) {
185 continue;
186 }
187
188 if (!$a_tpl_callback) {
189 $tpl = $this->getInitialisedTemplate();
190 } else {
191 $tpl = $a_tpl_callback();
192 }
193
194 $file = self::buildExportLink($a_link_template, "list", $month, $this->keywords);
195 $file = $this->writeExportFile($file, $tpl, $list, $nav);
196
197 if (!$has_index) {
198 $file = $this->writeExportFile($a_index_name, $tpl, $list, $nav);
199 $has_index = true;
200 }
201 }
202
203 // keywords
204 foreach (array_keys($this->blog_gui->getKeywords(false)) as $keyword) {
205 $list_items = $this->blog_gui->filterItemsByKeyword($this->items, $keyword);
206 $list = $this->blog_gui->renderList($list_items, "render", $a_link_template, false, $this->target_dir);
207
208 if (!$list) {
209 continue;
210 }
211
212 if (!$a_tpl_callback) {
213 $tpl = $this->getInitialisedTemplate();
214 } else {
215 $tpl = $a_tpl_callback();
216 }
217
218 $file = self::buildExportLink($a_link_template, "keyword", $keyword, $this->keywords);
219 $file = $this->writeExportFile($file, $tpl, $list, $nav);
220 }
221
222
223 // single postings
224
225 $pages = \ilBlogPosting::getAllPostings($this->blog->getId(), 0);
226 foreach ($pages as $page) {
227 if (\ilBlogPosting::_exists("blp", $page["id"])) {
228 $blp_gui = new \ilBlogPostingGUI(0, null, $page["id"]);
229 $blp_gui->setOutputMode("offline");
230 $blp_gui->setFullscreenLink("fullscreen.html"); // #12930 - see page.xsl
231 $blp_gui->add_date = true;
232 $page_content = $blp_gui->showPage();
233
234 $back = self::buildExportLink(
235 $a_link_template,
236 "list",
237 substr($page["created"]->get(IL_CAL_DATE), 0, 7),
238 $this->keywords
239 );
240
241 $file = self::buildExportLink($a_link_template, "posting", (string) $page["id"], $this->keywords);
242
243 if (!$a_tpl_callback) {
244 $tpl = $this->getInitialisedTemplate();
245 } else {
246 $tpl = $a_tpl_callback();
247 }
248
249 $comments = ($this->include_comments)
250 ? $blp_gui->getCommentsHTMLExport()
251 : "";
252
253 // posting nav
254 $nav = $this->blog_gui->renderNavigation(
255 "",
256 "",
257 $a_link_template,
258 false,
259 $page["id"]
260 );
261
262 $this->writeExportFile($file, $tpl, $page_content, $nav, (bool) $back, $comments);
263
264 $this->co_page_html_export->collectPageElements("blp:pg", $page["id"]);
265 }
266 }
267
268 if (!$has_index) {
269 if (!$a_tpl_callback) {
270 $tpl = $this->getInitialisedTemplate();
271 } else {
272 $tpl = $a_tpl_callback();
273 }
274 $file = $this->writeExportFile($a_index_name, $tpl, "", $nav);
275 }
276 }
277
281 public function exportHTMLPagesPrint(): void
282 {
283 $this->collectAllPagesPageElements($this->co_page_html_export);
284
285 // render print view
286 $print_view = $this->blog_gui->getPrintView();
287 $print_view->setOffline(true);
288 $html = $print_view->renderPrintView();
289 $this->collector->addString($html, "index.html");
290 }
291
292
293 public function collectAllPagesPageElements(\ilCOPageHTMLExport $co_page_html_export): void
294 {
295 $pages = \ilBlogPosting::getAllPostings($this->blog->getId(), 0);
296 foreach ($pages as $page) {
297 if (\ilBlogPosting::_exists("blp", $page["id"])) {
298 $co_page_html_export->collectPageElements("blp:pg", $page["id"]);
299 }
300 }
301 }
302
306 public static function buildExportLink(
307 string $a_template,
308 string $a_type,
309 string $a_id,
310 array $keywords
311 ): string {
312 switch ($a_type) {
313 case "list":
314 $a_type = "m";
315 break;
316
317 case "keyword":
318 if (!isset(self::$keyword_export_map)) {
319 self::$keyword_export_map = array_flip(array_keys($keywords));
320 }
321 $a_id = (string) (self::$keyword_export_map[$a_id] ?? "");
322 $a_type = "k";
323 break;
324
325 default:
326 $a_type = "p";
327 break;
328 }
329
330 return str_replace(array("{TYPE}", "{ID}"), array($a_type, $a_id), $a_template);
331 }
332
336 protected function getInitialisedTemplate(
337 string $a_back_url = ""
339 global $DIC;
340
341 $this->global_screen->layout()->meta()->reset();
342
343 $location_stylesheet = \ilUtil::getStyleSheetLocation();
344 $this->global_screen->layout()->meta()->addCss($location_stylesheet);
345 $this->global_screen->layout()->meta()->addCss(
347 );
349
350 $tabs = $DIC->tabs();
351 $tabs->clearTargets();
352 $tabs->clearSubTabs();
353 if ($a_back_url) {
354 $tabs->setBackTarget($this->lng->txt("back"), $a_back_url);
355 }
356 $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
357
358 $this->co_page_html_export->getPreparedMainTemplate($tpl);
359
360 $this->blog_gui->renderFullscreenHeader($tpl, $this->blog->getOwner(), true);
361
362 return $tpl;
363 }
364
369 protected function writeExportFile(
370 string $a_file,
371 \ilGlobalPageTemplate $a_tpl,
372 string $a_content,
373 string $a_right_content = "",
374 bool $a_back = false,
375 string $comments = ""
376 ): string {
377 $file = $this->target_dir . "/" . $a_file;
378
379 // export template: page content
380 $ep_tpl = new \ilTemplate(
381 "tpl.export_page.html",
382 true,
383 true,
384 "components/ILIAS/Blog"
385 );
386 if ($a_back) {
387 $ep_tpl->setVariable("PAGE_CONTENT", $a_content);
388 $ep_tpl->setVariable("COMMENTS", $comments);
389 } else {
390 $ep_tpl->setVariable("LIST", $a_content);
391 }
392 $a_tpl->setContent($ep_tpl->get());
393 unset($ep_tpl);
394
395 // template: right content
396 if ($a_right_content) {
397 $a_tpl->setRightContent($a_right_content);
398 }
399
400 $content = $a_tpl->printToString();
401
402 // open file
403 $this->collector->addString($content, $a_file);
404
405 return $file;
406 }
407
408 public function delete(): void
409 {
410 $this->collector->delete();
411 }
412
413 public function getFilePath(): string
414 {
415 return $this->collector->getFilePath();
416 }
417
418}
includeComments(bool $a_include_comments)
ILIAS components Export HTML ExportCollector $collector
ILIAS Style Content Object ObjectFacade $content_style_domain
exportHTMLPagesPrint()
Export all pages as one print version.
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!)
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
Util This class is an interim solution for the HTML export handling with 6.0.
Definition: class.Util.php:33
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
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)
Class ilFileUtils.
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.
@ilCtrl_Calls ilObjBlogGUI: ilBlogPostingGUI, ilWorkspaceAccessGUI @ilCtrl_Calls ilObjBlogGUI: ilInfo...
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="")
get full style sheet file name (path inclusive) of current user
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$comments