ILIAS  release_7 Revision v7.30-3-g800a261c036
BlogHtmlExport.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
6
13{
17 protected $blog;
18
22 protected $blog_gui;
23
27 protected $export_dir;
28
32 protected $sub_dir;
33
37 protected $target_dir;
38
42 protected $global_screen;
43
47 protected $export_util;
48
53
57 protected $lng;
58
62 protected $tabs;
63
67 protected $items;
68
72 protected static $keyword_export_map;
73
77 protected $keywords;
78
82 protected $include_comments = false;
83
90 public function __construct(\ilObjBlogGUI $blog_gui, string $exp_dir, string $sub_dir, bool $set_export_key = true)
91 {
92 global $DIC;
93
94 $this->blog_gui = $blog_gui;
95 $this->blog = $blog_gui->object;
96 $this->export_dir = $exp_dir;
97 $this->sub_dir = $sub_dir;
98 $this->target_dir = $exp_dir . "/" . $sub_dir;
99
100 $this->global_screen = $DIC->globalScreen();
101 $this->export_util = new \ILIAS\Services\Export\HTML\Util($exp_dir, $sub_dir);
102 $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir);
103 $this->tabs = $DIC->tabs();
104 $this->lng = $DIC->language();
105
106 $this->items = $this->blog_gui->getItems();
107 $this->keywords = $this->blog_gui->getKeywords(false);
108 if ($set_export_key) {
109 $this->global_screen->tool()->context()->current()->addAdditionalData(
111 true
112 );
113 }
114 }
115
120 public function includeComments($a_include_comments)
121 {
122 $this->include_comments = $a_include_comments;
123 }
124
128 protected function initDirectories()
129 {
130 // initialize temporary target directory
131 \ilUtil::delDir($this->target_dir);
132 \ilUtil::makeDir($this->target_dir);
133 }
134
142 public function exportHTML()
143 {
144 $this->initDirectories();
145 $this->export_util->exportSystemStyle();
146 $this->export_util->exportCOPageFiles($this->blog->getStyleSheetId(), "blog");
147
148 // export banner
149 $this->exportBanner();
150
151 // export pages
152 $this->exportHTMLPages();
153
154 // export comments user images
155 $this->exportUserImages();
156
157 $this->export_util->exportResourceFiles();
158 $this->co_page_html_export->exportPageElements();
159
160 return $this->zipPackage();
161 }
162
166 protected function exportUserImages()
167 {
168 if ($this->include_comments) {
169 $user_export = new \ILIAS\Notes\Export\UserImageExporter();
170 $user_export->exportUserImagesForRepObjId($this->target_dir, $this->blog->getId());
171 }
172 }
173
177 protected function exportBanner()
178 {
179 // banner / profile picture
180 $blga_set = new \ilSetting("blga");
181 if ($blga_set->get("banner")) {
182 $banner = $this->blog->getImageFullPath();
183 if ($banner) {
184 copy($banner, $this->target_dir . "/" . basename($banner));
185 }
186 }
187 // page element: profile picture
188 \ilObjUser::copyProfilePicturesToDirectory($this->blog->getOwner(), $this->target_dir);
189 }
190
196 public function zipPackage()
197 {
198 // zip it all
199 $date = time();
201 ? "html_comments"
202 : "html";
203 $zip_file = \ilExport::_getExportDirectory($this->blog->getId(), $type, "blog") .
204 "/" . $date . "__" . IL_INST_ID . "__" .
205 $this->blog->getType() . "_" . $this->blog->getId() . ".zip";
206 \ilUtil::zip($this->target_dir, $zip_file);
207 \ilUtil::delDir($this->target_dir);
208 return $zip_file;
209 }
210
220 public function exportHTMLPages($a_link_template = null, $a_tpl_callback = null, $a_co_page_html_export = null, $a_index_name = "index.html")
221 {
222 if (!$a_link_template) {
223 $a_link_template = "bl{TYPE}_{ID}.html";
224 }
225
226 if ($a_co_page_html_export) {
227 $this->co_page_html_export = $a_co_page_html_export;
228 }
229
230 // lists
231
232 // global nav
233 $nav = $this->blog_gui->renderNavigation("", "", $a_link_template);
234
235 // month list
236 $has_index = false;
237
238 foreach (array_keys($this->items) as $month) {
239 $list = $this->blog_gui->renderList($this->items[$month], "render", $a_link_template, false, $this->target_dir);
240
241 if (!$list) {
242 continue;
243 }
244
245 if (!$a_tpl_callback) {
246 $tpl = $this->getInitialisedTemplate();
247 } else {
248 $tpl = call_user_func($a_tpl_callback);
249 }
250
251 $file = self::buildExportLink($a_link_template, "list", $month, $this->keywords);
252 $file = $this->writeExportFile($file, $tpl, $list, $nav);
253
254 if (!$has_index) {
255 copy($file, $this->target_dir . "/" . $a_index_name);
256 $has_index = true;
257 }
258 }
259
260 // keywords
261 foreach (array_keys($this->blog_gui->getKeywords(false)) as $keyword) {
262 $this->keyword = $keyword;
263 $list_items = $this->blog_gui->filterItemsByKeyword($this->items, $keyword);
264 $list = $this->blog_gui->renderList($list_items, "render", $a_link_template, false, $this->target_dir);
265
266 if (!$list) {
267 continue;
268 }
269
270 if (!$a_tpl_callback) {
271 $tpl = $this->getInitialisedTemplate();
272 } else {
273 $tpl = call_user_func($a_tpl_callback);
274 }
275
276 $file = self::buildExportLink($a_link_template, "keyword", $keyword, $this->keywords);
277 $file = $this->writeExportFile($file, $tpl, $list, $nav);
278 }
279
280
281 // single postings
282
283 $pages = \ilBlogPosting::getAllPostings($this->blog->getId(), 0);
284 foreach ($pages as $page) {
285 if (\ilBlogPosting::_exists("blp", $page["id"])) {
286 $blp_gui = new \ilBlogPostingGUI(0, null, $page["id"]);
287 $blp_gui->setOutputMode("offline");
288 $blp_gui->setFullscreenLink("fullscreen.html"); // #12930 - see page.xsl
289 $blp_gui->add_date = true;
290 $page_content = $blp_gui->showPage();
291
292 $back = self::buildExportLink(
293 $a_link_template,
294 "list",
295 substr($page["created"]->get(IL_CAL_DATE), 0, 7),
296 $this->keywords
297 );
298
299 $file = self::buildExportLink($a_link_template, "posting", $page["id"], $this->keywords);
300
301 if (!$a_tpl_callback) {
302 $tpl = $this->getInitialisedTemplate();
303 } else {
304 $tpl = call_user_func($a_tpl_callback);
305 }
306
307 $comments = ($this->include_comments)
308 ? $blp_gui->getCommentsHTMLExport()
309 : "";
310
311 // posting nav
312 $nav = $this->blog_gui->renderNavigation(
313 "",
314 "",
315 $a_link_template,
316 false,
317 $page["id"]
318 );
319
320 $this->writeExportFile($file, $tpl, $page_content, $nav, $back, $comments);
321
322 $this->co_page_html_export->collectPageElements("blp:pg", $page["id"]);
323 }
324 }
325 }
326
335 public static function buildExportLink($a_template, $a_type, $a_id, $keywords)
336 {
337 switch ($a_type) {
338 case "list":
339 $a_type = "m";
340 break;
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 = self::$keyword_export_map[$a_id];
348 $a_type = "k";
349 break;
350
351 default:
352 $a_type = "p";
353 break;
354 }
355
356 $link = str_replace("{TYPE}", $a_type, $a_template);
357 return str_replace("{ID}", $a_id, $link);
358 }
359
364 protected function getInitialisedTemplate($a_back_url = "") : \ilGlobalPageTemplate
365 {
366 global $DIC;
367
368 $this->global_screen->layout()->meta()->reset();
369
370 $location_stylesheet = \ilUtil::getStyleSheetLocation();
371 $this->global_screen->layout()->meta()->addCss($location_stylesheet);
372 $this->global_screen->layout()->meta()->addCss(
373 \ilObjStyleSheet::getContentStylePath($this->blog->getStyleSheetId())
374 );
376
377 $tabs = $DIC->tabs();
378 $tabs->clearTargets();
379 $tabs->clearSubTabs();
380 if ($a_back_url) {
381 $tabs->setBackTarget($this->lng->txt("back"), $a_back_url);
382 }
383 $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
384
385 $this->co_page_html_export->getPreparedMainTemplate($tpl);
386
387 $this->blog_gui->renderFullscreenHeader($tpl, $this->blog->getOwner(), true);
388
389 return $tpl;
390 }
391
392
404 protected function writeExportFile(string $a_file, \ilGlobalPageTemplate $a_tpl, string $a_content, string $a_right_content = "", bool $a_back = false, $comments = "")
405 {
406 $file = $this->target_dir . "/" . $a_file;
407 // return if file is already existing
408 if (@is_file($file)) {
409 return;
410 }
411
412 // export template: page content
413 $ep_tpl = new \ilTemplate(
414 "tpl.export_page.html",
415 true,
416 true,
417 "Modules/Blog"
418 );
419 if ($a_back) {
420 $ep_tpl->setVariable("PAGE_CONTENT", $a_content);
421 $ep_tpl->setVariable("COMMENTS", $comments);
422 } else {
423 $ep_tpl->setVariable("LIST", $a_content);
424 }
425 unset($a_content);
426 $a_tpl->setContent($ep_tpl->get());
427 unset($ep_tpl);
428
429 // template: right content
430 if ($a_right_content) {
431 $a_tpl->setRightContent($a_right_content);
432 unset($a_right_content);
433 }
434
435 $content = $a_tpl->printToString();
436
437 // open file
438 file_put_contents($file, $content);
439
440 return $file;
441 }
442}
An exception for terminatinating execution or to throw for unit testing.
writeExportFile(string $a_file, \ilGlobalPageTemplate $a_tpl, string $a_content, string $a_right_content="", bool $a_back=false, $comments="")
Write HTML to file.
exportHTMLPages($a_link_template=null, $a_tpl_callback=null, $a_co_page_html_export=null, $a_index_name="index.html")
Export all pages (note: this one is called from the portfolio html export!)
exportUserImages()
Export user images.
__construct(\ilObjBlogGUI $blog_gui, string $exp_dir, string $sub_dir, bool $set_export_key=true)
constructor
static buildExportLink($a_template, $a_type, $a_id, $keywords)
Build static export link.
initDirectories()
Initialize directories.
includeComments($a_include_comments)
Include comments.
getInitialisedTemplate($a_back_url="")
Get initialised template.
const IL_CAL_DATE
static getAllPostings($a_blog_id, $a_limit=1000, $a_offset=0)
Get all postings of blog.
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
Class ilGlobalPageTemplate.
setRightContent($a_html)
@inheritDoc
Class ilObjBlogGUI.
static getContentStylePath($a_style_id, $add_random=true, $add_token=true)
get content style path
static copyProfilePicturesToDirectory($a_user_id, $a_dir)
Get profile picture direcotory.
static resetInitialState()
Reset initial state (for exports)
static _exists($a_parent_type, $a_id, $a_lang="", $a_no_cache=false)
Checks whether page exists.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getStyleSheetLocation($mode="output", $a_css_name="", $a_css_location="")
get full style sheet file name (path inclusive) of current user
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
const IL_INST_ID
Definition: constants.php:38
global $DIC
Definition: goto.php:24
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$type