ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilMobMultiSrtUpload.php
Go to the documentation of this file.
1 <?php
2 
21 
28 {
29  protected ZipAdapter $zip;
31  protected ilLanguage $lng;
32  protected MetadataManager $md;
33 
37  public function __construct(
38  ilMobMultiSrtInt $a_multi_srt
39  ) {
40  global $DIC;
41 
42  $lng = $DIC->language();
43  $this->zip = $DIC->mediaObjects()->internal()->domain()->resources()->zip();
44  $this->md = $DIC->mediaObjects()->internal()->domain()->metadata();
45 
46  $this->lng = $lng;
47  $this->multi_srt = $a_multi_srt;
48  }
49 
53  public function getMultiSrtUploadDir(): string
54  {
55  return $this->multi_srt->getUploadDir();
56  }
57 
58 
66  public function uploadMultipleSubtitleFile(
67  array $a_file
68  ): void {
69  if (!is_file($a_file["tmp_name"])) {
70  throw new ilMobSrtUploadException($this->lng->txt("cont_srt_zip_file_could_not_be_uploaded"));
71  }
72 
73  $dir = $this->getMultiSrtUploadDir();
74  ilFileUtils::delDir($dir, true);
76  ilFileUtils::moveUploadedFile($a_file["tmp_name"], "multi_vtt.zip", $dir . "/" . "multi_vtt.zip");
77  $this->zip->unzipFile($dir . "/multi_vtt.zip");
78  }
79 
83  public function clearMultiSrtDirectory(): void
84  {
86  }
87 
91  public function getMultiSrtFiles(): array
92  {
93  $items = array();
94 
95  $lang_codes = $this->md->getLOMLanguageCodes();
96 
97  $dir = $this->getMultiSrtUploadDir();
98  $files = ilFileUtils::getDir($dir);
99  foreach ($files as $k => $i) {
100  // check directory
101  if ($i["type"] == "file" && !in_array($k, array(".", ".."))) {
102  if (pathinfo($k, PATHINFO_EXTENSION) == "vtt") {
103  $lang = "";
104  if (substr($k, strlen($k) - 7, 1) == "_") {
105  $lang = substr($k, strlen($k) - 6, 2);
106  if (!in_array($lang, $lang_codes)) {
107  $lang = "";
108  }
109  }
110  $items[] = array("filename" => $k, "lang" => $lang);
111  }
112  }
113  }
114 
115  foreach ($this->multi_srt->getMobIds() as $mob) {
116  $m = new ilObjMediaObject($mob);
117  $mi = $m->getMediaItem("Standard");
118  if ($mi->getLocationType() == "LocalFile" && is_int(strpos($mi->getFormat(), "video"))) {
119  // $loc is e.g. "echo-hereweare.mp4", we not look for
120  // "echo-hereweare_<langcode>.srt" files
121  $loc = pathinfo($mi->getLocation(), PATHINFO_FILENAME);
122  foreach ($items as $i => $item) {
123  if (substr($item["filename"], 0, strlen($loc)) == $loc &&
124  substr($item["filename"], strlen($loc), 1) == "_" &&
125  pathinfo($item["filename"], PATHINFO_EXTENSION) == "vtt") {
126  $l = substr($item["filename"], strlen($loc) + 1, 2);
127  if (in_array($l, $lang_codes)) {
128  $items[$i]["lang"] = $l;
129  $items[$i]["mob"] = $mob;
130  $items[$i]["mob_title"] = $m->getTitle();
131  }
132  }
133  }
134  }
135  }
136 
137  return $items;
138  }
139 
143  public function moveMultiSrtFiles(): int
144  {
145  $items = $this->getMultiSrtFiles();
146  $cnt = 0;
147  foreach ($items as $i) {
148  if ($i["mob"] > 0 && $i["lang"] != "") {
149  $mob = new ilObjMediaObject($i["mob"]);
150  $mob->uploadSrtFile($this->getMultiSrtUploadDir() . "/" . $i["filename"], $i["lang"], "rename");
151  $cnt++;
152  }
153  }
154  return $cnt;
155  }
156 }
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.
__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
global $DIC
Definition: shib_login.php:25
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
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.