ILIAS  release_8 Revision v8.24
class.ilDownloadFilesBackgroundTask.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
8
9/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
10
16{
18 protected ilLanguage $lng;
19 protected ?ilObjUser $user;
20 protected TaskFactory $task_factory;
21
22 private int $user_id;
23 private array $events = [];
24 private string $bucket_title;
25 private bool $has_files = false;
26 private \ilGlobalTemplateInterface $main_tpl;
27
33 public function __construct($a_usr_id)
34 {
35 global $DIC;
36 $this->main_tpl = $DIC->ui()->mainTemplate();
37
38 $this->logger = $DIC->logger()->cal();
39 $this->user_id = $a_usr_id;
40 $this->task_factory = $DIC->backgroundTasks()->taskFactory();
41 $this->lng = $DIC->language();
42
44 if (!$user instanceof ilObjUser) {
45 throw new DomainException('Invalid or deleted user id given: ' . $this->user_id);
46 }
47 $this->user = $user;
48 }
49
50 public function setEvents(array $a_events): void
51 {
52 $this->events = $a_events;
53 }
54
55 public function getEvents(): array
56 {
57 return $this->events;
58 }
59
60 public function setBucketTitle(string $a_title): void
61 {
62 $this->bucket_title = $a_title;
63 }
64
69 public function getBucketTitle(): string
70 {
71 //TODO: fix ilUtil zip stuff
72 // Error If name starts "-"
73 // error massage from ilUtil->execQuoted = ["","zip error: Invalid command arguments (short option 'a' not supported)"]
74 if (substr($this->bucket_title, 0, 1) === "-") {
75 $this->bucket_title = ltrim($this->bucket_title, "-");
76 }
78 }
79
80 public function run(): bool
81 {
82 $definition = new ilCalendarCopyDefinition();
83 $normalized_name = ilFileUtils::getASCIIFilename($this->getBucketTitle());
84 $definition->setTempDir($normalized_name);
85
86 $this->collectFiles($definition);
87
88 if (!$this->has_files) {
89 $this->main_tpl->setOnScreenMessage('info', $this->lng->txt("cal_down_no_files"), true);
90 return false;
91 }
92
93 $bucket = new BasicBucket();
94 $bucket->setUserId($this->user_id);
95
96 // move files from source dir to target directory
97 $copy_job = $this->task_factory->createTask(ilCalendarCopyFilesToTempDirectoryJob::class, [$definition]);
98 $zip_job = $this->task_factory->createTask(ilCalendarZipJob::class, [$copy_job]);
99
100 $download_name = new StringValue();
101
102 $this->logger->debug("Normalized name = " . $normalized_name);
103 $download_name->setValue($normalized_name . '.zip');
104
105 $download_interaction = $this->task_factory->createTask(
106 ilCalendarDownloadZipInteraction::class,
107 [$zip_job, $download_name]
108 );
109
110 // last task to bucket
111 $bucket->setTask($download_interaction);
112
113 $bucket->setTitle($this->getBucketTitle());
114
115 $task_manager = $GLOBALS['DIC']->backgroundTasks()->taskManager();
116 $task_manager->run($bucket);
117 return true;
118 }
119
120 private function collectFiles(ilCalendarCopyDefinition $def): void
121 {
122 //filter here the objects, don't repeat the object Id
123 $object_ids = [];
124 foreach ($this->getEvents() as $event) {
125 $start = new ilDateTime($event['dstart'], IL_CAL_UNIX);
126 $cat = ilCalendarCategory::getInstanceByCategoryId($event['category_id']);
127 $obj_id = $cat->getObjId();
128
129 $this->logger->debug('Handling event: ' . $event['event']->getPresentationTitle());
130 //22295 If the object type is exc then we need all the assignments.Otherwise we will get only one.
131 if (
132 $cat->getType() != \ilCalendarCategory::TYPE_OBJ ||
133 $cat->getObjType() == 'exc' ||
134 !in_array($obj_id, $object_ids)
135 ) {
136 $this->logger->debug('New obj_id..');
137 $object_ids[] = $obj_id;
138
139 $folder_date = $start->get(IL_CAL_FKT_DATE, 'Y-m-d', $this->user->getTimeZone());
140
141 if ($event['fullday']) {
142 $folder_app = ilFileUtils::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
143 } else {
144 $start_time = $start->get(IL_CAL_FKT_DATE, 'H.i', $this->user->getTimeZone());
145
146 $end = new ilDateTime($event['dend'], IL_CAL_UNIX);
147 $end_time = $end->get(IL_CAL_FKT_DATE, 'H.i', $this->user->getTimeZone());
148
149 if ($start_time != $end_time) {
150 $start_time .= (' - ' . $end_time);
151 }
152 $folder_app = $start_time . ' ' .
153 ilFileUtils::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
154 }
155
156 $this->logger->debug("collecting files...event title = " . $folder_app);
157 $file_handler = ilAppointmentFileHandlerFactory::getInstance($event);
158 $this->logger->debug('Current file handler: ' . get_class($file_handler));
159
160 if ($files = $file_handler->getFiles()) {
161 $this->has_files = true;
162 }
163
164 $this->logger->dump($files);
165 foreach ($files as $idx => $file_property) {
166 $this->logger->debug('Filename:' . $file_property->getFileName());
167 $this->logger->debug('Absolute path: ' . $file_property->getAbsolutePath());
168
169 $def->addCopyDefinition(
170 $file_property->getAbsolutePath(),
171 $folder_date . '/' . $folder_app . '/' . $file_property->getFileName()
172 );
173 $this->logger->debug(
174 'Added new copy definition: ' .
175 $folder_date . '/' . $folder_app . '/' . $file_property->getFileName() . ' => ' .
176 $file_property->getAbsolutePath()
177 );
178 }
179 } else {
180 $this->logger->info('Ignoring obj_id: ' . $obj_id . ' already processed.');
181 }
182 }
183 }
184}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
static getInstanceByCategoryId(int $a_cat_id)
addCopyDefinition(string $a_source, string $a_target)
Add copy definition.
@classDescription Date and time handling
__construct($a_usr_id)
ilDownloadFilesBackgroundTask constructor.
static getASCIIFilename(string $a_filename)
language handling
Component logger with individual log levels by component id.
User class.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
global $DIC
Definition: feed.php:28