ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
PortfolioHtmlExport.php
Go to the documentation of this file.
1 <?php
2 
20 
22 use ilFileUtils;
24 
31 {
33  protected \ilObjPortfolio $portfolio;
34  protected \ilObjPortfolioBaseGUI $portfolio_gui;
35  protected string $export_dir = "";
36  protected string $sub_dir = "";
37  protected string $target_dir = "";
38  protected \ILIAS\GlobalScreen\Services $global_screen;
39  protected \ILIAS\Services\Export\HTML\Util $export_util;
40  protected \ilCOPageHTMLExport $co_page_html_export;
41  protected \ilLanguage $lng;
42  protected array $tabs = [];
43  protected array $export_material = [];
44  protected string $active_tab = "";
45  protected bool $include_comments = false;
46  protected bool $print_version = false;
47  protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
48 
49  public function __construct(
50  \ilObjPortfolioBaseGUI $portfolio_gui
51  ) {
52  global $DIC;
53 
54  $this->portfolio_gui = $portfolio_gui;
56  $portfolio = $portfolio_gui->getObject();
57  $this->portfolio = $portfolio;
58 
59 
60  $this->global_screen = $DIC->globalScreen();
61  $this->lng = $DIC->language();
62 
63  $this->global_screen->tool()->context()->current()->addAdditionalData(
65  true
66  );
67 
68  $this->content_style_domain = $DIC
69  ->contentStyle()
70  ->domain()->styleForObjId($this->portfolio->getId());
71  $this->domain = $DIC->portfolio()->internal()->domain();
72  }
73 
74  protected function init(): void
75  {
76  $this->export_dir = \ilExport::_getExportDirectory($this->portfolio->getId(), "html", "prtf");
77  $this->sub_dir = $this->portfolio->getType() . "_" . $this->portfolio->getId();
78  if ($this->print_version) {
79  $this->sub_dir .= "print";
80  }
81  $this->target_dir = $this->export_dir . "/" . $this->sub_dir;
82  $this->export_util = new \ILIAS\Services\Export\HTML\Util($this->export_dir, $this->sub_dir);
83  $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir);
84  }
85 
86  public function includeComments(bool $a_include_comments): void
87  {
88  $this->include_comments = $a_include_comments;
89  }
90 
91  public function setPrintVersion(bool $print_version): void
92  {
93  $this->print_version = $print_version;
94  }
95 
99  protected function initDirectories(): void
100  {
101  // create export file
102  \ilExport::_createExportDirectory($this->portfolio->getId(), "html", "prtf");
103 
104  // initialize temporary target directory
105  ilFileUtils::delDir($this->target_dir);
106  ilFileUtils::makeDir($this->target_dir);
107  }
108 
112  protected function exportBanner(): void
113  {
114  // banner
115  $prfa_set = new \ilSetting("prfa");
116  if ($prfa_set->get("banner")) {
117  $banner = $this->portfolio->getImageFullPath();
118  if ($banner) { // #16096
119  copy($banner, $this->target_dir . "/" . basename($banner));
120  }
121  }
122  // page element: profile picture
123  \ilObjUser::copyProfilePicturesToDirectory($this->portfolio->getOwner(), $this->target_dir);
124  /*
125  $ppic = \ilObjUser::_getPersonalPicturePath($this->portfolio->getOwner(), "big", true, true);
126  if ($ppic) {
127  $ppic = array_shift(explode("?", $ppic));
128  copy($ppic, $this->target_dir . "/" . basename($ppic));
129  }
130  // header image: profile picture
131  $ppic = \ilObjUser::_getPersonalPicturePath($this->portfolio->getOwner(), "xsmall", true, true);
132  if ($ppic) {
133  $ppic = array_shift(explode("?", $ppic));
134  copy($ppic, $this->target_dir . "/" . basename($ppic));
135  }*/
136  }
137 
138 
142  public function exportHtml(): string
143  {
144  $this->init();
145  $this->initDirectories();
146 
147  $this->export_util->exportSystemStyle();
148  $this->export_util->exportCOPageFiles(
149  $this->content_style_domain->getEffectiveStyleId(),
150  $this->portfolio->getType()
151  );
152 
153  $this->exportBanner();
154 
155  // export pages
156  if ($this->print_version) {
157  $this->exportHTMLPagesPrint();
158  } else {
159  $this->exportHTMLPages();
160  }
161 
162  $this->exportUserImages();
163 
164  // add js/images/file to zip
165  // note: only files are still used for certificate files
166  $images = $files = $js_files = [];
167  foreach ($this->export_material as $items) {
168  $images = array_merge($images, $items["images"]);
169  $files = array_merge($files, $items["files"]);
170  $js_files = array_merge($js_files, $items["js"]);
171  }
172  foreach (array_unique($files) as $file) {
173  if (is_file($file)) {
174  copy($file, $this->target_dir . "/files/" . basename($file));
175  }
176  }
177 
178  $this->export_util->exportResourceFiles();
179  $this->co_page_html_export->exportPageElements();
180 
181  return $this->zipPackage();
182  }
183 
187  protected function exportUserImages(): void
188  {
189  if ($this->include_comments) {
190  $user_export = new \ILIAS\Notes\Export\UserImageExporter();
191  $user_export->exportUserImagesForRepObjId($this->target_dir, $this->portfolio->getId());
192  }
193  }
194 
200  public function zipPackage(): string
201  {
202  // zip it all
203  $date = time();
204  $zip_file = \ilExport::_getExportDirectory($this->portfolio->getId(), "html", "prtf") .
205  "/" . $date . "__" . IL_INST_ID . "__" .
206  $this->portfolio->getType() . "_" . $this->portfolio->getId() . ".zip";
207  $this->domain->resources()->zip()->zipDirectoryToFile($this->target_dir, $zip_file);
208  ilFileUtils::delDir($this->target_dir);
209 
210  return $zip_file;
211  }
212 
213 
220  public function exportHTMLPages(): void
221  {
222  $pages = \ilPortfolioPage::getAllPortfolioPages($this->portfolio->getId());
223 
224  $this->tabs = [];
225  foreach ($pages as $page) {
226  // substitute blog id with title
227  if ($page["type"] == \ilPortfolioPage::TYPE_BLOG) {
228  $page["title"] = \ilObjBlog::_lookupTitle((int) $page["title"]);
229  }
230 
231  $this->tabs[$page["id"]] = $page["title"];
232  }
233 
234  // for sub-pages, e.g. blog postings
235  $tpl_callback = function (array $js_files = []): \ilGlobalPageTemplate {
236  return $this->getInitialisedTemplate($js_files);
237  };
238 
239  $has_index = false;
240  foreach ($pages as $page) {
241  if (\ilPortfolioPage::_exists("prtf", $page["id"])) {
242  $this->active_tab = "user_page_" . $page["id"];
243 
244  if ($page["type"] == \ilPortfolioPage::TYPE_BLOG) {
245  $link_template = "prtf_" . $page["id"] . "_bl{TYPE}_{ID}.html";
246 
247  $blog_gui = new \ilObjBlogGUI((int) $page["title"], \ilObject2GUI::WORKSPACE_OBJECT_ID);
248  $blog_export = new BlogHtmlExport($blog_gui, $this->export_dir, $this->sub_dir, false);
249  $blog_export->exportHTMLPages($link_template, $tpl_callback, $this->co_page_html_export, "prtf_" . $page["id"] . ".html");
250  } else {
251  $tpl = $this->getInitialisedTemplate();
252  $tpl->setContent($this->renderPage($page["id"]));
253  $this->writeExportFile("prtf_" . $page["id"] . ".html", $tpl->printToString());
254  $this->co_page_html_export->collectPageElements("prtf:pg", $page["id"]);
255  }
256 
257  if (!$has_index && is_file($this->target_dir . "/prtf_" . $page["id"] . ".html")) { // #20144
258  copy(
259  $this->target_dir . "/prtf_" . $page["id"] . ".html",
260  $this->target_dir . "/index.html"
261  );
262  $has_index = true;
263  }
264  }
265  }
266  }
267 
271  public function exportHTMLPagesPrint(): void
272  {
273  // collect page elements
274  $pages = \ilPortfolioPage::getAllPortfolioPages($this->portfolio->getId());
275  foreach ($pages as $page) {
276  if (\ilPortfolioPage::_exists("prtf", $page["id"])) {
277  if ($page["type"] == \ilPortfolioPage::TYPE_BLOG) {
278  $blog_gui = new \ilObjBlogGUI((int) $page["title"], \ilObject2GUI::WORKSPACE_OBJECT_ID);
279  $blog_export = new BlogHtmlExport($blog_gui, $this->export_dir, $this->sub_dir, false);
280  $blog_export->collectAllPagesPageElements($this->co_page_html_export);
281  } else {
282  $this->co_page_html_export->collectPageElements("prtf:pg", $page["id"]);
283  }
284  }
285  }
286 
287  // render print view
288  $print_view = $this->portfolio_gui->getPrintView();
289  $print_view->setOffline(true);
290  $html = $print_view->renderPrintView();
291  file_put_contents($this->target_dir . "/index.html", $html);
292  }
293 
297  public function getInitialisedTemplate(
298  array $a_js_files = []
300  global $DIC;
301 
302  $this->global_screen->layout()->meta()->reset();
303 
304  $location_stylesheet = \ilUtil::getStyleSheetLocation();
305  $this->global_screen->layout()->meta()->addCss($location_stylesheet);
306  $this->global_screen->layout()->meta()->addCss(
308  $this->content_style_domain->getEffectiveStyleId()
309  )
310  );
312 
313  // js files
314  foreach ($a_js_files as $js_file) {
315  $this->global_screen->layout()->meta()->addJs($js_file);
316  }
317 
318  $tabs = $DIC->tabs();
319  $tabs->clearTargets();
320  $tabs->clearSubTabs();
321  if (is_array($this->tabs)) {
322  foreach ($this->tabs as $id => $caption) {
323  $tabs->addTab("user_page_" . $id, $caption, "prtf_" . $id . ".html");
324  }
325 
326  $tabs->activateTab($this->active_tab);
327  }
328 
329 
330  $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
331 
332  $this->co_page_html_export->getPreparedMainTemplate($tpl);
333 
334  $tpl->setTitle($this->portfolio->getTitle());
335 
336  return $tpl;
337  }
338 
342  public function writeExportFile(
343  string $a_file,
344  string $content
345  ): string {
346  $file = $this->target_dir . "/" . $a_file;
347  // return if file is already existing
348  if (is_file($file)) {
349  return "";
350  }
351 
352  file_put_contents($file, $content);
353 
354  return $file;
355  }
356 
360  public function renderPage(
361  string $a_post_id
362  ): string {
363  // page
364  $pgui = new \ilPortfolioPageGUI($this->portfolio->getId(), $a_post_id);
365  $pgui->setOutputMode("offline");
366  $pgui->setFullscreenLink("fullscreen.html"); // #12930 - see page.xsl
367  $page_content = $pgui->showPage();
368 
369  $ep_tpl = new \ilTemplate(
370  "tpl.export_page.html",
371  true,
372  true,
373  "Modules/Portfolio"
374  );
375 
377  ? $pgui->getCommentsHTMLExport()
378  : "";
379  $ep_tpl->setVariable("PAGE_CONTENT", $page_content);
380  $ep_tpl->setVariable("COMMENTS", $comments);
381 
382  $material = $pgui->getExportMaterial();
383  $this->export_material[] = $material;
384 
385  return $ep_tpl->get();
386  }
387 }
static getAllPortfolioPages(int $a_portfolio_id)
Get pages of portfolio.
static copyProfilePicturesToDirectory(int $a_user_id, string $a_dir)
const IL_INST_ID
Definition: constants.php:40
writeExportFile(string $a_file, string $content)
Write export file.
renderPage(string $a_post_id)
Render page.
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.
ILIAS Style Content Object ObjectFacade $content_style_domain
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupTitle(int $obj_id)
__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 _createExportDirectory(int $a_obj_id, string $a_export_type="xml", string $a_obj_type="")
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
ILIAS Services Export HTML Util $export_util
$comments
static getContentStylePath(int $a_style_id, bool $add_random=true, bool $add_token=true)
get content style path static (to avoid full reading)
Portfolio view gui base class.
static resetInitialState()
Reset initial state (for exports)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
exportHTMLPagesPrint()
Export all pages as one print version.
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
getInitialisedTemplate(array $a_js_files=[])
Get initialised template.