ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMobMultiSrtUpload.php
Go to the documentation of this file.
1 <?php
2 
25 {
27  protected ilLanguage $lng;
28 
32  public function __construct(
33  ilMobMultiSrtInt $a_multi_srt
34  ) {
35  global $DIC;
36 
37  $lng = $DIC->language();
38 
39  $this->lng = $lng;
40  $this->multi_srt = $a_multi_srt;
41  }
42 
46  public function getMultiSrtUploadDir(): string
47  {
48  return $this->multi_srt->getUploadDir();
49  }
50 
51 
59  public function uploadMultipleSubtitleFile(
60  array $a_file
61  ): void {
62  if (!is_file($a_file["tmp_name"])) {
63  throw new ilMobSrtUploadException($this->lng->txt("cont_srt_zip_file_could_not_be_uploaded"));
64  }
65 
66  $dir = $this->getMultiSrtUploadDir();
67  ilFileUtils::delDir($dir, true);
69  ilFileUtils::moveUploadedFile($a_file["tmp_name"], "multi_srt.zip", $dir . "/" . "multi_srt.zip");
70  ilFileUtils::unzip($dir . "/multi_srt.zip", true);
71  }
72 
76  public function clearMultiSrtDirectory(): void
77  {
79  }
80 
84  public function getMultiSrtFiles(): array
85  {
86  $items = array();
87 
89 
90  $dir = $this->getMultiSrtUploadDir();
91  $files = ilFileUtils::getDir($dir);
92  foreach ($files as $k => $i) {
93  // check directory
94  if ($i["type"] == "file" && !in_array($k, array(".", ".."))) {
95  if (pathinfo($k, PATHINFO_EXTENSION) == "srt") {
96  $lang = "";
97  if (substr($k, strlen($k) - 7, 1) == "_") {
98  $lang = substr($k, strlen($k) - 6, 2);
99  if (!in_array($lang, $lang_codes)) {
100  $lang = "";
101  }
102  }
103  $items[] = array("filename" => $k, "lang" => $lang);
104  }
105  }
106  }
107 
108  foreach ($this->multi_srt->getMobIds() as $mob) {
109  $m = new ilObjMediaObject($mob);
110  $mi = $m->getMediaItem("Standard");
111  if ($mi->getLocationType() == "LocalFile" && is_int(strpos($mi->getFormat(), "video"))) {
112  // $loc is e.g. "echo-hereweare.mp4", we not look for
113  // "echo-hereweare_<langcode>.srt" files
114  $loc = pathinfo($mi->getLocation(), PATHINFO_FILENAME);
115  foreach ($items as $i => $item) {
116  if (substr($item["filename"], 0, strlen($loc)) == $loc &&
117  substr($item["filename"], strlen($loc), 1) == "_" &&
118  pathinfo($item["filename"], PATHINFO_EXTENSION) == "srt") {
119  $l = substr($item["filename"], strlen($loc) + 1, 2);
120  if (in_array($l, $lang_codes)) {
121  $items[$i]["lang"] = $l;
122  $items[$i]["mob"] = $mob;
123  $items[$i]["mob_title"] = $m->getTitle();
124  }
125  }
126  }
127  }
128  }
129 
130  return $items;
131  }
132 
136  public function moveMultiSrtFiles(): int
137  {
138  $items = $this->getMultiSrtFiles();
139  $cnt = 0;
140  foreach ($items as $i) {
141  if ($i["mob"] > 0 && $i["lang"] != "") {
142  $mob = new ilObjMediaObject($i["mob"]);
143  $mob->uploadSrtFile($this->getMultiSrtUploadDir() . "/" . $i["filename"], $i["lang"], "rename");
144  $cnt++;
145  }
146  }
147  return $cnt;
148  }
149 }
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.
static unzip(string $path_to_zip_file, bool $overwrite_existing=false, bool $unpack_flat=false)
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$i
Definition: metadata.php:41
uploadMultipleSubtitleFile(array $a_file)
Upload multi srt file.