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