1<?php
2
3use ILIAS\BackgroundTasks\Implementation\Bucket\BasicBucket;
4use ILIAS\BackgroundTasks\Implementation\Values\ScalarValues\StringValue;
5
6include_once('Services/BackgroundTasks/classes/class.ilCopyDefinition.php');
7include_once('Services/BackgroundTasks/classes/Jobs/class.ilCollectFilesJob.php');
8include_once('Services/BackgroundTasks/classes/Jobs/class.ilCheckSumOfFileSizesJob.php');
9include_once('Services/BackgroundTasks/classes/UserInteractions/class.ilSumOfFileSizesTooLargeInteraction.php');
10include_once('Services/BackgroundTasks/classes/Jobs/class.ilCopyFilesToTempDirectoryJob.php');
11include_once('Services/BackgroundTasks/classes/Jobs/class.ilZipJob.php');
12include_once('Services/BackgroundTasks/classes/UserInteractions/class.ilDownloadZipInteraction.php');
13
14/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
15
16/**
17 * Class ilDownloadContainerFilesBackgroundTask
18 *
19 * @author Lukas Zehnder <lz@studer-raimann.ch>
20 * @author Fabian Schmid <fs@studer-raimann.ch>
21 */
22class ilDownloadContainerFilesBackgroundTask
23{
24
25    /**
26     * @var ilLogger
27     */
28    private $logger = null;
29    /**
30     * @var int
31     */
32    protected $user_id;
33    /**
34     * @var int[]
35     */
36    protected $object_ref_ids;
37    /**
38     * determines whether the task has been initiated by a folder's action drop-down to prevent a folder duplicate inside the zip.
39     *
40     * @var bool
41     */
42    protected $initiated_by_folder_action = false;
43    /**
44     * @var \ILIAS\BackgroundTasks\Task\TaskFactory
45     */
46    protected $task_factory = null;
47    /**
48     * title of the task showed in the main menu.
49     *
50     * @var string
51     */
52    protected $bucket_title;
53    /**
54     * if the task has collected files to create the ZIP file.
55     *
56     * @var bool
57     */
58    protected $has_files = false;
59
60
61    /**
62     * Constructor
63     *
64     * @param type  $a_usr_id
65     * @param int[] $a_object_ref_ids
66     */
67    public function __construct($a_usr_id, $a_object_ref_ids, $a_initiated_by_folder_action)
68    {
69        global $DIC;
70        $this->logger = $DIC->logger()->cal();
71        $this->user_id = $a_usr_id;
72        $this->object_ref_ids = $a_object_ref_ids;
73        $this->initiated_by_folder_action = $a_initiated_by_folder_action;
74        $this->task_factory = $DIC->backgroundTasks()->taskFactory();
75        $this->lng = $DIC->language();
76    }
77
78
79    /**
80     * set bucket title.
81     *
82     * @param $a_title
83     */
84    public function setBucketTitle($a_title)
85    {
86        $this->bucket_title = $a_title;
87    }
88
89
90    /**
91     * return bucket title.
92     *
93     * @return string
94     */
95    public function getBucketTitle()
96    {
97        //TODO: fix ilUtil zip stuff
98        // Error If name starts "-"
99        // error massage from ilUtil->execQuoted = ["","zip error: Invalid command arguments (short option 'a' not supported)"]
100        if (substr($this->bucket_title, 0, 1) === "-") {
101            $this->bucket_title = ltrim($this->bucket_title, "-");
102        }
103
104        return $this->bucket_title;
105    }
106
107
108    /**
109     * Run task
110     *
111     * @return bool
112     */
113    public function run()
114    {
115        // This is our Bucket
116        $this->logger->info('Started download container files background task');
117        $bucket = new BasicBucket();
118        $bucket->setUserId($this->user_id);
119        $this->logger->debug('Created bucket and set the following user id: ' . $this->user_id);
120
121        // Copy Definition
122        $definition = new ilCopyDefinition();
123        $normalized_name = ilUtil::getASCIIFilename($this->getBucketTitle());
124        $definition->setTempDir($normalized_name);
125        $definition->setObjectRefIds($this->object_ref_ids);
126        $this->logger->debug('Created copy definition and added the following tempdir: ' . $normalized_name);
127
128        // Collect all files by the definition and prevent duplicates
129        $collect_job = $this->task_factory->createTask(ilCollectFilesJob::class, [$definition, $this->initiated_by_folder_action]);
130        $this->logger->debug('Collected files based on the following object ids: ');
131        $this->logger->dump($this->object_ref_ids);
132
133        // Check the FileSize
134        $file_size_job = $this->task_factory->createTask(ilCheckSumOfFileSizesJob::class, [$collect_job]);
135
136        // Show problems with file-limit
137        $file_size_interaction = $this->task_factory->createTask(ilSumOfFileSizesTooLargeInteraction::class, [$file_size_job]);
138        $this->logger->debug('Determined the sum of all file sizes');
139
140        // move files from source dir to target directory
141        $copy_job = $this->task_factory->createTask(ilCopyFilesToTempDirectoryJob::class, [$file_size_interaction]);
142
143        // Zip it
144        $zip_job = $this->task_factory->createTask(ilZipJob::class, [$copy_job]);
145        $this->logger->debug('Moved files from source- to target-directory');
146
147        // Download
148        $download_name = new StringValue();
149        $download_name->setValue($normalized_name . '.zip');
150        $download_interaction = $this->task_factory->createTask(ilDownloadZipInteraction::class, [$zip_job, $download_name]);
151        $this->logger->debug('Created a download interaction with the following download name: ' . $download_name->getValue());
152
153        // last task to bucket
154        $bucket->setTask($download_interaction);
155        $bucket->setTitle($this->getBucketTitle());
156        $this->logger->debug('Added last task to bucket and set the following title: ' . $this->getBucketTitle());
157
158        $task_manager = $GLOBALS['DIC']->backgroundTasks()->taskManager();
159        $task_manager->run($bucket);
160        $this->logger->debug('Ran bucket in task manager');
161
162        return true;
163    }
164}
165