ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
5 namespace ILIAS\Blog\Export;
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 
85  public function __construct(\ilObjBlogGUI $blog_gui, string $exp_dir, string $sub_dir, bool $set_export_key = true)
86  {
87  global $DIC;
88 
89  $this->blog_gui = $blog_gui;
90  $this->blog = $blog_gui->object;
91  $this->export_dir = $exp_dir;
92  $this->sub_dir = $sub_dir;
93  $this->target_dir = $exp_dir . "/" . $sub_dir;
94 
95  $this->global_screen = $DIC->globalScreen();
96  $this->export_util = new \ILIAS\Services\Export\HTML\Util($exp_dir, $sub_dir);
97  $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir);
98  $this->tabs = $DIC->tabs();
99  $this->lng = $DIC->language();
100 
101  $this->items = $this->blog_gui->getItems();
102  $this->keywords = $this->blog_gui->getKeywords(false);
103  if ($set_export_key) {
104  $this->global_screen->tool()->context()->current()->addAdditionalData(
106  true
107  );
108  }
109  }
110 
114  protected function initDirectories()
115  {
116  // initialize temporary target directory
117  \ilUtil::delDir($this->target_dir);
118  \ilUtil::makeDir($this->target_dir);
119  }
120 
128  public function exportHTML()
129  {
130  $this->initDirectories();
131  $this->export_util->exportSystemStyle();
132  $this->export_util->exportCOPageFiles($this->blog->getStyleSheetId(), "blog");
133 
134  // export banner
135  $this->exportBanner();
136 
137  // export pages
138  $this->exportHTMLPages();
139 
140  $this->export_util->exportResourceFiles();
141  $this->co_page_html_export->exportPageElements();
142 
143  return $this->zipPackage();
144  }
145 
149  protected function exportBanner()
150  {
151  // banner / profile picture
152  $blga_set = new \ilSetting("blga");
153  if ($blga_set->get("banner")) {
154  $banner = $this->blog->getImageFullPath();
155  if ($banner) {
156  copy($banner, $this->target_dir . "/" . basename($banner));
157  }
158  }
159  // page element: profile picture
161  }
162 
168  public function zipPackage()
169  {
170  // zip it all
171  $date = time();
172  $zip_file = \ilExport::_getExportDirectory($this->blog->getId(), "html", "blog") .
173  "/" . $date . "__" . IL_INST_ID . "__" .
174  $this->blog->getType() . "_" . $this->blog->getId() . ".zip";
175  \ilUtil::zip($this->target_dir, $zip_file);
176  \ilUtil::delDir($this->target_dir);
177 
178  return $zip_file;
179  }
180 
190  public function exportHTMLPages($a_link_template = null, $a_tpl_callback = null, $a_co_page_html_export = null, $a_index_name = "index.html")
191  {
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 
208  foreach (array_keys($this->items) as $month) {
209  $list = $this->blog_gui->renderList($this->items[$month], "render", $a_link_template, false, $this->target_dir);
210 
211  if (!$list) {
212  continue;
213  }
214 
215  if (!$a_tpl_callback) {
216  $tpl = $this->getInitialisedTemplate();
217  } else {
218  $tpl = call_user_func($a_tpl_callback);
219  }
220 
221  $file = self::buildExportLink($a_link_template, "list", $month, $this->keywords);
222  $file = $this->writeExportFile($file, $tpl, $list, $nav);
223 
224  if (!$has_index) {
225  copy($file, $this->target_dir . "/" . $a_index_name);
226  $has_index = true;
227  }
228  }
229 
230  // keywords
231  foreach (array_keys($this->blog_gui->getKeywords(false)) as $keyword) {
232  $this->keyword = $keyword;
233  $list_items = $this->blog_gui->filterItemsByKeyword($this->items, $keyword);
234  $list = $this->blog_gui->renderList($list_items, "render", $a_link_template, false, $this->target_dir);
235 
236  if (!$list) {
237  continue;
238  }
239 
240  if (!$a_tpl_callback) {
241  $tpl = $this->getInitialisedTemplate();
242  } else {
243  $tpl = call_user_func($a_tpl_callback);
244  }
245 
246  $file = self::buildExportLink($a_link_template, "keyword", $keyword, $this->keywords);
247  $file = $this->writeExportFile($file, $tpl, $list, $nav);
248  }
249 
250 
251  // single postings
252 
253  $pages = \ilBlogPosting::getAllPostings($this->blog->getId(), 0);
254  foreach ($pages as $page) {
255  if (\ilBlogPosting::_exists("blp", $page["id"])) {
256  $blp_gui = new \ilBlogPostingGUI(0, null, $page["id"]);
257  $blp_gui->setOutputMode("offline");
258  $blp_gui->setFullscreenLink("fullscreen.html"); // #12930 - see page.xsl
259  $blp_gui->add_date = true;
260  $page_content = $blp_gui->showPage();
261 
262  $back = self::buildExportLink(
263  $a_link_template,
264  "list",
265  substr($page["created"]->get(IL_CAL_DATE), 0, 7),
266  $this->keywords
267  );
268 
269  $file = self::buildExportLink($a_link_template, "posting", $page["id"], $this->keywords);
270 
271  if (!$a_tpl_callback) {
272  $tpl = $this->getInitialisedTemplate();
273  } else {
274  $tpl = call_user_func($a_tpl_callback);
275  }
276 
277  // posting nav
278  $nav = $this->blog_gui->renderNavigation(
279  "",
280  "",
281  $a_link_template,
282  false,
283  $page["id"]
284  );
285 
286  $this->writeExportFile($file, $tpl, $page_content, $nav, $back);
287 
288  $this->co_page_html_export->collectPageElements("blp:pg", $page["id"]);
289  }
290  }
291  }
292 
301  public static function buildExportLink($a_template, $a_type, $a_id, $keywords)
302  {
303  switch ($a_type) {
304  case "list":
305  $a_type = "m";
306  break;
307  break;
308 
309  case "keyword":
310  if (!isset(self::$keyword_export_map)) {
311  self::$keyword_export_map = array_flip(array_keys($keywords));
312  }
313  $a_id = self::$keyword_export_map[$a_id];
314  $a_type = "k";
315  break;
316 
317  default:
318  $a_type = "p";
319  break;
320  }
321 
322  $link = str_replace("{TYPE}", $a_type, $a_template);
323  return str_replace("{ID}", $a_id, $link);
324  }
325 
330  protected function getInitialisedTemplate($a_back_url = "") : \ilGlobalPageTemplate
331  {
332  global $DIC;
333 
334  $this->global_screen->layout()->meta()->reset();
335 
336  $location_stylesheet = \ilUtil::getStyleSheetLocation();
337  $this->global_screen->layout()->meta()->addCss($location_stylesheet);
338  $this->global_screen->layout()->meta()->addCss(
339  \ilObjStyleSheet::getContentStylePath($this->blog->getStyleSheetId())
340  );
342 
343  $tabs = $DIC->tabs();
344  $tabs->clearTargets();
345  $tabs->clearSubTabs();
346  if ($a_back_url) {
347  $tabs->setBackTarget($this->lng->txt("back"), $a_back_url);
348  }
349  $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
350 
351  $this->co_page_html_export->getPreparedMainTemplate($tpl);
352 
353  $this->blog_gui->renderFullscreenHeader($tpl, $this->blog->getOwner(), true);
354 
355  return $tpl;
356  }
357 
358 
370  protected function writeExportFile(string $a_file, \ilGlobalPageTemplate $a_tpl, string $a_content, string $a_right_content = "", bool $a_back = false)
371  {
372  $file = $this->target_dir . "/" . $a_file;
373  // return if file is already existing
374  if (@is_file($file)) {
375  return;
376  }
377 
378  // export template: page content
379  $ep_tpl = new \ilTemplate(
380  "tpl.export_page.html",
381  true,
382  true,
383  "Modules/Blog"
384  );
385  if ($a_back) {
386  $ep_tpl->setVariable("PAGE_CONTENT", $a_content);
387  } else {
388  $ep_tpl->setVariable("LIST", $a_content);
389  }
390  unset($a_content);
391  $a_tpl->setContent($ep_tpl->get());
392  unset($ep_tpl);
393 
394  // template: right content
395  if ($a_right_content) {
396  $a_tpl->setRightContent($a_right_content);
397  unset($a_right_content);
398  }
399 
400  $content = $a_tpl->printToString();
401 
402  // open file
403  file_put_contents($file, $content);
404 
405  return $file;
406  }
407 }
static getContentStylePath($a_style_id, $add_random=true, $add_token=true)
get content style path
static _exists($a_parent_type, $a_id, $a_lang="", $a_no_cache=false)
Checks whether page exists.
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!)
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
static getStyleSheetLocation($mode="output", $a_css_name="", $a_css_location="")
get full style sheet file name (path inclusive) of current user
static buildExportLink($a_template, $a_type, $a_id, $keywords)
Build static export link.
static getAllPostings($a_blog_id, $a_limit=1000, $a_offset=0)
Get all postings of blog.
$a_type
Definition: workflow.php:92
Class ilGlobalPageTemplate.
static copyProfilePicturesToDirectory($a_user_id, $a_dir)
Get profile picture direcotory.
$a_content
Definition: workflow.php:93
initDirectories()
Initialize directories.
Class ilObjBlogGUI.
writeExportFile(string $a_file, \ilGlobalPageTemplate $a_tpl, string $a_content, string $a_right_content="", bool $a_back=false)
Write HTML to file.
__construct(\ilObjBlogGUI $blog_gui, string $exp_dir, string $sub_dir, bool $set_export_key=true)
constructor
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 ...
static resetInitialState()
Reset initial state (for exports)
const IL_CAL_DATE
$DIC
Definition: xapitoken.php:46
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
getInitialisedTemplate($a_back_url="")
Get initialised template.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively