ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMobMultiSrtUpload.php
Go to the documentation of this file.
1 <?php
2 
20 
27 {
28  protected ZipAdapter $zip;
30  protected ilLanguage $lng;
31 
35  public function __construct(
36  ilMobMultiSrtInt $a_multi_srt
37  ) {
38  global $DIC;
39 
40  $lng = $DIC->language();
41  $this->zip = $DIC->mediaObjects()->internal()->domain()->resources()->zip();
42 
43  $this->lng = $lng;
44  $this->multi_srt = $a_multi_srt;
45  }
46 
50  public function getMultiSrtUploadDir(): string
51  {
52  return $this->multi_srt->getUploadDir();
53  }
54 
55 
63  public function uploadMultipleSubtitleFile(
64  array $a_file
65  ): void {
66  if (!is_file($a_file["tmp_name"])) {
67  throw new ilMobSrtUploadException($this->lng->txt("cont_srt_zip_file_could_not_be_uploaded"));
68  }
69 
70  $dir = $this->getMultiSrtUploadDir();
71  ilFileUtils::delDir($dir, true);
73  ilFileUtils::moveUploadedFile($a_file["tmp_name"], "multi_srt.zip", $dir . "/" . "multi_srt.zip");
74  $this->zip->unzipFile($dir . "/multi_srt.zip");
75  }
76 
80  public function clearMultiSrtDirectory(): void
81  {
83  }
84 
88  public function getMultiSrtFiles(): array
89  {
90  $items = array();
91 
93 
94  $dir = $this->getMultiSrtUploadDir();
95  $files = ilFileUtils::getDir($dir);
96  foreach ($files as $k => $i) {
97  // check directory
98  if ($i["type"] == "file" && !in_array($k, array(".", ".."))) {
99  if (pathinfo($k, PATHINFO_EXTENSION) == "srt") {
100  $lang = "";
101  if (substr($k, strlen($k) - 7, 1) == "_") {
102  $lang = substr($k, strlen($k) - 6, 2);
103  if (!in_array($lang, $lang_codes)) {
104  $lang = "";
105  }
106  }
107  $items[] = array("filename" => $k, "lang" => $lang);
108  }
109  }
110  }
111 
112  foreach ($this->multi_srt->getMobIds() as $mob) {
113  $m = new ilObjMediaObject($mob);
114  $mi = $m->getMediaItem("Standard");
115  if ($mi->getLocationType() == "LocalFile" && is_int(strpos($mi->getFormat(), "video"))) {
116  // $loc is e.g. "echo-hereweare.mp4", we not look for
117  // "echo-hereweare_<langcode>.srt" files
118  $loc = pathinfo($mi->getLocation(), PATHINFO_FILENAME);
119  foreach ($items as $i => $item) {
120  if (substr($item["filename"], 0, strlen($loc)) == $loc &&
121  substr($item["filename"], strlen($loc), 1) == "_" &&
122  pathinfo($item["filename"], PATHINFO_EXTENSION) == "srt") {
123  $l = substr($item["filename"], strlen($loc) + 1, 2);
124  if (in_array($l, $lang_codes)) {
125  $items[$i]["lang"] = $l;
126  $items[$i]["mob"] = $mob;
127  $items[$i]["mob_title"] = $m->getTitle();
128  }
129  }
130  }
131  }
132  }
133 
134  return $items;
135  }
136 
140  public function moveMultiSrtFiles(): int
141  {
142  $items = $this->getMultiSrtFiles();
143  $cnt = 0;
144  foreach ($items as $i) {
145  if ($i["mob"] > 0 && $i["lang"] != "") {
146  $mob = new ilObjMediaObject($i["mob"]);
147  $mob->uploadSrtFile($this->getMultiSrtUploadDir() . "/" . $i["filename"], $i["lang"], "rename");
148  $cnt++;
149  }
150  }
151  return $cnt;
152  }
153 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
getMultiSrtFiles()
Get all srt files of srt multi upload.
global $DIC
Definition: feed.php:28
__construct(ilMobMultiSrtInt $a_multi_srt)
getMultiSrtUploadDir()
Get directory for multi srt upload.
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
$lang
Definition: xapiexit.php:26
clearMultiSrtDirectory()
Clear multi feedback directory.
moveMultiSrtFiles()
Move all srt files that could be mapped to media objects.
Handler class for multi srt upload.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
uploadMultipleSubtitleFile(array $a_file)
Upload multi srt file.