ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjBibliographic.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
15 {
16 
24  protected $bib_type_factory;
28  protected $bib_entry_factory;
32  protected $bib_field_factory;
36  protected $bib_data_factory;
50  protected $filename;
56  protected $entries;
62  protected $is_online;
66  protected $file_type = 0;
67 
68 
74  public function initType()
75  {
76  $this->type = "bibl";
77  }
78 
79 
87  public function __construct($existant_bibl_id = 0)
88  {
89  if ($existant_bibl_id) {
90  $this->setId($existant_bibl_id);
91  $this->doRead();
92  }
93  parent::__construct($existant_bibl_id, false);
94 
95  $this->bib_type_factory = new ilBiblTypeFactory();
96  $this->bib_field_factory = new ilBiblFieldFactory($this->bib_type_factory->getInstanceForType($this->getFileType()));
97  $this->bib_overview_factory = new ilBiblOverviewModelFactory();
98  $this->bib_entry_factory = new ilBiblEntryFactory($this->bib_field_factory, $this->bib_type_factory->getInstanceForType($this->getFileType()), $this->bib_overview_factory);
99  $this->bib_filereader_factory = new ilBiblFileReaderFactory();
100  $this->bib_attribute_factory = new ilBiblAttributeFactory($this->bib_field_factory);
101  }
102 
103 
109  protected function doCreate()
110  {
111  global $DIC;
112 
113  $upload = $DIC->upload();
114  if ($upload->hasUploads() && !$upload->hasBeenProcessed()) {
115  $upload->process();
116  $this->moveUploadedFile($upload);
117  $this->parseFileToDatabase();
118  }
119 
120  $DIC->database()->insert(
121  "il_bibl_data",
122  [
123  "id" => ["integer", $this->getId()],
124  "filename" => ["text", $this->getFilename()],
125  "is_online" => ["integer", $this->getOnline()],
126  "file_type" => ["integer", $this->getFilename() ? $this->determineFileTypeByFileName($this->getFilename()) : ""],
127  ]
128  );
129  }
130 
131 
132  protected function doRead()
133  {
134  global $DIC;
135  $ilDB = $DIC['ilDB'];
136  $ilBiblData = ilBiblData::where(array('id' => $this->getId()))->first();
137  if (!$this->getFilename()) {
138  $this->setFilename($ilBiblData->getFilename());
139  }
140  $this->setFileType($ilBiblData->getFileType());
141  $this->setOnline($ilBiblData->getIsOnline());
142  }
143 
144 
145  public function doUpdate()
146  {
147  global $DIC;
148 
149  $upload = $DIC->upload();
150  $has_valid_upload = $upload->hasUploads() && !$upload->hasBeenProcessed();
151  if ($_POST['override_entries'] && $has_valid_upload) {
152  $upload->process();
153  $this->deleteFile();
154  $this->moveUploadedFile($upload);
155  }
156  if ($has_valid_upload) {
157  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
158  $this->doDelete(true, true);
159  $this->parseFileToDatabase();
160  }
161 
162  $DIC->database()->update(
163  "il_bibl_data",
164  [
165  "filename" => ["text", $this->getFilename()],
166  "is_online" => ["integer", $this->getOnline()],
167  "file_type" => ["integer", $this->getFileType()],
168  ],
169  ["id" => ["integer", $this->getId()]]
170  );
171  }
172 
173 
178  protected function doDelete($leave_out_il_bibl_data = false, $leave_out_delete_file = false)
179  {
180  global $DIC;
181  $ilDB = $DIC['ilDB'];
182  if (!$leave_out_delete_file) {
183  $this->deleteFile();
184  }
185  //il_bibl_attribute
186  $ilDB->manipulate(
187  "DELETE FROM il_bibl_attribute WHERE il_bibl_attribute.entry_id IN " . "(SELECT il_bibl_entry.id FROM il_bibl_entry WHERE il_bibl_entry.data_id = " . $ilDB->quote(
188  $this->getId(),
189  "integer"
190  ) . ")"
191  );
192  //il_bibl_entry
193  $this->bib_entry_factory->deleteEntryById($this->getId());
194 
195  if (!$leave_out_il_bibl_data) {
196  //il_bibl_data
197  $ilDB->manipulate(
198  "DELETE FROM il_bibl_data WHERE id = " . $ilDB->quote($this->getId(), "integer")
199  );
200  }
201  // delete history entries
203  }
204 
205 
209  public function getFileDirectory()
210  {
211  return "{$this->getType()}/{$this->getId()}";
212  }
213 
214 
218  protected function moveUploadedFile(\ILIAS\FileUpload\FileUpload $upload)
219  {
223  $result = array_values($upload->getResults())[0];
225  $this->deleteFile();
226  $upload->moveFilesTo($this->getFileDirectory(), \ILIAS\FileUpload\Location::STORAGE);
227  $this->setFilename($result->getName());
228  }
229  }
230 
231 
235  private function copyFile($file_to_copy)
236  {
237  $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
238  $this->getFileSystem()->copy($file_to_copy, $target);
239  }
240 
241 
245  protected function deleteFile()
246  {
247  $path = $this->getFileDirectory();
248  try {
249  $this->getFileSystem()->deleteDir($path);
250  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
251  return false;
252  }
253 
254  return true;
255  }
256 
257 
261  private function getFileSystem()
262  {
263  global $DIC;
264 
265  return $DIC["filesystem"]->storage();
266  }
267 
268 
274  public function getFilePath($without_filename = false)
275  {
276  $file_name = $this->getFilename();
277 
278  if ($without_filename) {
279  return substr($file_name, 0, strrpos($file_name, DIRECTORY_SEPARATOR));
280  } else {
281  return $file_name;
282  }
283  }
284 
285 
289  public function setFilename($filename)
290  {
291  $this->filename = $filename;
292  }
293 
294 
298  public function getFilename()
299  {
300  return $this->filename;
301  }
302 
303 
308  public function getFileAbsolutePath()
309  {
310  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
311  }
312 
313 
314  public function getLegacyAbsolutePath()
315  {
316  $stream = $this->getFileSystem()->readStream($this->getFileAbsolutePath());
317 
318  return $stream->getMetadata('uri');
319  }
320 
321 
326  public function getFileTypeAsString()
327  {
328  $type = $this->getFileType();
329 
330  return $this->bib_type_factory->getInstanceForType($type)->getStringRepresentation();
331  }
332 
333 
337  public function getFileType()
338  {
339  $filename = $this->getFilename();
340  if ($filename === null) {
342  }
343  $instance = $this->bib_type_factory->getInstanceForFileName($filename);
344 
345  return $instance->getId();
346  }
347 
348 
358  public function doCloneObject($new_obj, $a_target_id, $a_copy_id = null, $a_omit_tree = false)
359  {
360  assert($new_obj instanceof ilObjBibliographic);
361  //copy online status if object is not the root copy object
362  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
363 
364  if (!$cp_options->isRootNode($this->getRefId())) {
365  $new_obj->setOnline($this->getOnline());
366  }
367 
368  $new_obj->cloneStructure($this->getId());
369 
370  $new_obj->parseFileToDatabase();
371 
372  return $new_obj;
373  }
374 
375 
385  public function cloneStructure($original_id)
386  {
387  $original = new ilObjBibliographic($original_id);
388  $this->setFilename($original->getFilename());
389  $this->copyFile($original->getFileAbsolutePath());
390  $this->setDescription($original->getDescription());
391  $this->setTitle($original->getTitle());
392  $this->setType($original->getType());
393  $this->doUpdate();
394  }
395 
396 
402  public function parseFileToDatabase()
403  {
404  //Read File
405  $type = $this->getFileType();
406  $reader = $this->bib_filereader_factory->getByType($type, $this->bib_entry_factory, $this->bib_field_factory, $this->bib_attribute_factory);
407  $reader->readContent($this->getFileAbsolutePath());
408  $this->entries = $reader->parseContentToEntries($this);
409  }
410 
411 
415  public function setFileType($file_type)
416  {
417  $this->file_type = $file_type;
418  }
419 
420 
424  public function setOnline($a_online)
425  {
426  $this->is_online = $a_online;
427  }
428 
429 
433  public function getOnline()
434  {
435  return $this->is_online;
436  }
437 
438 
445  {
446  return $this->bib_type_factory->getInstanceForFileName($filename)->getId();
447  }
448 }
Class ilBiblTypeFactory.
setType($a_type)
setDescription($a_desc)
$result
Class ChatMainBarProvider .
setId($a_id)
set object id public
doCloneObject($new_obj, $a_target_id, $a_copy_id=null, $a_omit_tree=false)
Clone BIBL.
static where($where, $operator=null)
doDelete($leave_out_il_bibl_data=false, $leave_out_delete_file=false)
__construct($existant_bibl_id=0)
If bibliographic object exists, read it&#39;s data from database, otherwise create it.
static _getInstance($a_copy_id)
Get instance of copy wizard options.
getId()
get object id public
Class ilBiblEntryFactory.
global $DIC
Definition: goto.php:24
setTitle($a_title)
cloneStructure($original_id)
Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollec...
Class ilBiblAttributeFactory.
__construct(Container $dic, ilPlugin $plugin)
Class ilBiblFieldFactory.
global $ilDB
static _removeEntriesForObject($a_obj_id)
remove all history entries for an object
Class ilObjBibliographic.
getFilePath($without_filename=false)
$_POST["username"]
Class ilBiblFileReaderFactory.
Class ilBiblOverviewModelFactory.
Class FlySystemFileAccessTest disabled disabled disabled.
parseFileToDatabase()
Reads out the source file and writes all entries to the database.