ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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  }
118 
119  $DIC->database()->insert(
120  "il_bibl_data",
121  [
122  "id" => ["integer", $this->getId()],
123  "filename" => ["text", $this->getFilename()],
124  "is_online" => ["integer", $this->getOnline()],
125  "file_type" => ["integer", $this->getFilename() ? $this->determineFileTypeByFileName($this->getFilename()) : ""],
126  ]
127  );
128  }
129 
130 
131  protected function doRead()
132  {
133  global $DIC;
134  $ilDB = $DIC['ilDB'];
135  $ilBiblData = ilBiblData::where(array('id' => $this->getId()))->first();
136  if (!$this->getFilename()) {
137  $this->setFilename($ilBiblData->getFilename());
138  }
139  $this->setFileType($ilBiblData->getFileType());
140  $this->setOnline($ilBiblData->getIsOnline());
141  }
142 
143 
144  public function doUpdate()
145  {
146  global $DIC;
147 
148  $upload = $DIC->upload();
149  $has_valid_upload = $upload->hasUploads() && !$upload->hasBeenProcessed();
150  if ($_POST['override_entries'] && $has_valid_upload) {
151  $upload->process();
152  $this->deleteFile();
153  $this->moveUploadedFile($upload);
154  }
155  if ($has_valid_upload) {
156  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
157  $this->doDelete(true, true);
158  $this->parseFileToDatabase();
159  }
160 
161  $DIC->database()->update(
162  "il_bibl_data",
163  [
164  "filename" => ["text", $this->getFilename()],
165  "is_online" => ["integer", $this->getOnline()],
166  "file_type" => ["integer", $this->getFileType()],
167  ],
168  ["id" => ["integer", $this->getId()]]
169  );
170  }
171 
172 
177  protected function doDelete($leave_out_il_bibl_data = false, $leave_out_delete_file = false)
178  {
179  global $DIC;
180  $ilDB = $DIC['ilDB'];
181  if (!$leave_out_delete_file) {
182  $this->deleteFile();
183  }
184  //il_bibl_attribute
185  $ilDB->manipulate(
186  "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(
187  $this->getId(),
188  "integer"
189  ) . ")"
190  );
191  //il_bibl_entry
192  $this->bib_entry_factory->deleteEntryById($this->getId());
193 
194  if (!$leave_out_il_bibl_data) {
195  //il_bibl_data
196  $ilDB->manipulate(
197  "DELETE FROM il_bibl_data WHERE id = " . $ilDB->quote($this->getId(), "integer")
198  );
199  }
200  // delete history entries
202  }
203 
204 
208  public function getFileDirectory()
209  {
210  return "{$this->getType()}/{$this->getId()}";
211  }
212 
213 
217  protected function moveUploadedFile(\ILIAS\FileUpload\FileUpload $upload)
218  {
222  $result = array_values($upload->getResults())[0];
224  $this->deleteFile();
225  $upload->moveFilesTo($this->getFileDirectory(), \ILIAS\FileUpload\Location::STORAGE);
226  $this->setFilename($result->getName());
227  }
228  }
229 
230 
234  private function copyFile($file_to_copy)
235  {
236  $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
237  $this->getFileSystem()->copy($file_to_copy, $target);
238  }
239 
240 
244  protected function deleteFile()
245  {
246  $path = $this->getFileDirectory();
247  try {
248  $this->getFileSystem()->deleteDir($path);
249  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
250  return false;
251  }
252 
253  return true;
254  }
255 
256 
260  private function getFileSystem()
261  {
262  global $DIC;
263 
264  return $DIC["filesystem"]->storage();
265  }
266 
267 
273  public function getFilePath($without_filename = false)
274  {
275  $file_name = $this->getFilename();
276 
277  if ($without_filename) {
278  return substr($file_name, 0, strrpos($file_name, DIRECTORY_SEPARATOR));
279  } else {
280  return $file_name;
281  }
282  }
283 
284 
288  public function setFilename($filename)
289  {
290  $this->filename = $filename;
291  }
292 
293 
297  public function getFilename()
298  {
299  return $this->filename;
300  }
301 
302 
307  public function getFileAbsolutePath()
308  {
309  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
310  }
311 
312 
313  public function getLegacyAbsolutePath()
314  {
315  $stream = $this->getFileSystem()->readStream($this->getFileAbsolutePath());
316 
317  return $stream->getMetadata('uri');
318  }
319 
320 
325  public function getFileTypeAsString()
326  {
327  $type = $this->getFileType();
328 
329  return $this->bib_type_factory->getInstanceForType($type)->getStringRepresentation();
330  }
331 
332 
336  public function getFileType()
337  {
338  $filename = $this->getFilename();
339  if ($filename === null) {
341  }
342  $instance = $this->bib_type_factory->getInstanceForFileName($filename);
343 
344  return $instance->getId();
345  }
346 
347 
357  public function doCloneObject($new_obj, $a_target_id, $a_copy_id = null, $a_omit_tree = false)
358  {
359  assert($new_obj instanceof ilObjBibliographic);
360  //copy online status if object is not the root copy object
361  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
362 
363  if (!$cp_options->isRootNode($this->getRefId())) {
364  $new_obj->setOnline($this->getOnline());
365  }
366 
367  $new_obj->cloneStructure($this->getId());
368 
369  $new_obj->parseFileToDatabase();
370 
371  return $new_obj;
372  }
373 
374 
384  public function cloneStructure($original_id)
385  {
386  $original = new ilObjBibliographic($original_id);
387  $this->setFilename($original->getFilename());
388  $this->copyFile($original->getFileAbsolutePath());
389  $this->setDescription($original->getDescription());
390  $this->setTitle($original->getTitle());
391  $this->setType($original->getType());
392  $this->doUpdate();
393  }
394 
395 
401  public function parseFileToDatabase()
402  {
403  //Read File
404  $type = $this->getFileType();
405  $reader = $this->bib_filereader_factory->getByType($type, $this->bib_entry_factory, $this->bib_field_factory, $this->bib_attribute_factory);
406  $reader->readContent($this->getFileAbsolutePath());
407  $this->entries = $reader->parseContentToEntries($this);
408  }
409 
410 
414  public function setFileType($file_type)
415  {
416  $this->file_type = $file_type;
417  }
418 
419 
423  public function setOnline($a_online)
424  {
425  $this->is_online = $a_online;
426  }
427 
428 
432  public function getOnline()
433  {
434  return $this->is_online;
435  }
436 
437 
444  {
445  return $this->bib_type_factory->getInstanceForFileName($filename)->getId();
446  }
447 }
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.
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
$DIC
Definition: xapitoken.php:46
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.
parseFileToDatabase()
Reads out the source file and writes all entries to the database.