ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 require_once "Services/Object/classes/class.ilObject2.php";
5 require_once "Modules/Bibliographic/classes/class.ilBibliographicEntry.php";
6 require_once('./Modules/Bibliographic/classes/Types/Ris/class.ilRis.php');
7 require_once('./Modules/Bibliographic/classes/Types/BibTex/class.ilBibTex.php');
8 
19 
20  const FILETYPE_RIS = "ris";
21  const FILETYPE_BIB = "bib";
33  protected $filename;
39  protected $entries;
45  protected $overviewModels;
51  protected $is_online;
52 
53 
59  public function initType() {
60  $this->type = "bibl";
61  }
62 
63 
71  public function __construct($existant_bibl_id = 0) {
72  if ($existant_bibl_id) {
73  $this->setId($existant_bibl_id);
74  $this->doRead();
75  }
76  parent::__construct($existant_bibl_id, false);
77  }
78 
79 
85  protected function doCreate() {
86  global $DIC;
87  $ilDB = $DIC['ilDB'];
88  $ilDB->manipulate("INSERT INTO il_bibl_data " . "(id, filename, is_online) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . // id
89  $ilDB->quote($this->getFilename(), "text") . "," . // filename
90  $ilDB->quote($this->getOnline(), "integer") . // is_online
91  ")");
92  }
93 
94 
95  protected function doRead() {
96  global $DIC;
97  $ilDB = $DIC['ilDB'];
98  $set = $ilDB->query("SELECT * FROM il_bibl_data " . " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
99  while ($rec = $ilDB->fetchAssoc($set)) {
100  if (!$this->getFilename()) {
101  $this->setFilename($rec["filename"]);
102  }
103  $this->setOnline($rec['is_online']);
104  }
105  }
106 
107 
108  public function doUpdate() {
109  global $DIC;
110  $ilDB = $DIC['ilDB'];
111  $file_changed = !empty($_FILES['bibliographic_file']['name']);
112  if ($file_changed) {
113  $this->deleteFile();
114  $this->moveFile();
115  }
116  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
117  $this->doDelete(true, true);
118  $ilDB->manipulate("UPDATE il_bibl_data SET " . "filename = " . $ilDB->quote($this->getFilename(), "text") . ", " . // filename
119  "is_online = " . $ilDB->quote($this->getOnline(), "integer") . // is_online
120  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
122  }
123 
124 
129  protected function doDelete($leave_out_il_bibl_data = false, $leave_out_delete_file = false) {
130  global $DIC;
131  $ilDB = $DIC['ilDB'];
132  if (!$leave_out_delete_file) {
133  $this->deleteFile();
134  }
135  //il_bibl_attribute
136  $ilDB->manipulate("DELETE FROM il_bibl_attribute WHERE il_bibl_attribute.entry_id IN "
137  . "(SELECT il_bibl_entry.id FROM il_bibl_entry WHERE il_bibl_entry.data_id = " . $ilDB->quote($this->getId(), "integer")
138  . ")");
139  //il_bibl_entry
140  $ilDB->manipulate("DELETE FROM il_bibl_entry WHERE data_id = " . $ilDB->quote($this->getId(), "integer"));
141  if (!$leave_out_il_bibl_data) {
142  //il_bibl_data
143  $ilDB->manipulate("DELETE FROM il_bibl_data WHERE id = " . $ilDB->quote($this->getId(), "integer"));
144  }
145  // delete history entries
146  require_once("./Services/History/classes/class.ilHistory.php");
148  }
149 
150 
154  public function getFileDirectory() {
155  return ilUtil::getDataDir() . DIRECTORY_SEPARATOR . $this->getType() . DIRECTORY_SEPARATOR . $this->getId();
156  }
157 
158 
164  public function moveFile($file_to_copy = false) {
165  $target_dir = $this->getFileDirectory();
166  if (!is_dir($target_dir)) {
167  ilUtil::makeDirParents($target_dir);
168  }
169  if ($_FILES['bibliographic_file']['name']) {
170  $filename = $_FILES['bibliographic_file']['name'];
171  } elseif ($file_to_copy) {
172  //file is not uploaded, but a clone is made out of another bibl
173  $split_path = explode(DIRECTORY_SEPARATOR, $file_to_copy);
174  $filename = $split_path[sizeof($split_path) - 1];
175  } else {
176  throw new Exception("Either a file must be delivered via \$_POST/\$_FILE or the file must be delivered via the method argument file_to_copy");
177  }
178  $target_full_filename = $target_dir . DIRECTORY_SEPARATOR . $filename;
179  //If there is no file_to_copy (which is used for clones), copy the file from the temporary upload directory (new creation of object).
180  //Therefore, a warning predicates nothing and can be suppressed.
181  if (@!copy($file_to_copy, $target_full_filename)) {
182  if (!empty($_FILES['bibliographic_file']['tmp_name'])) {
183  ilUtil::moveUploadedFile($_FILES['bibliographic_file']['tmp_name'], $_FILES['bibliographic_file']['name'], $target_full_filename);
184  } else {
185  throw new Exception("The file delivered via the method argument file_to_copy could not be copied. The file '{$file_to_copy}' does probably not exist.");
186  }
187  }
188  $this->setFilename($filename);
189  ilUtil::sendSuccess($this->lng->txt("object_added"), true);
190  }
191 
192 
193  function deleteFile() {
194  $path = $this->getFilePath(true);
195  self::__force_rmdir($path);
196  }
197 
198 
204  public function getFilePath($without_filename = false) {
205  global $DIC;
206  $ilDB = $DIC['ilDB'];
207  $set = $ilDB->query("SELECT filename FROM il_bibl_data " . " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
208  $rec = $ilDB->fetchAssoc($set);
209  {
210  if ($without_filename) {
211  return substr($rec['filename'], 0, strrpos($rec['filename'], DIRECTORY_SEPARATOR));
212  } else {
213  return $rec['filename'];
214  }
215  }
216  }
217 
218 
222  public function setFilename($filename) {
223  $this->filename = $filename;
224  }
225 
226 
230  public function getFilename() {
231  return $this->filename;
232  }
233 
234 
238  public function getFileAbsolutePath() {
239  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
240  }
241 
242 
246  public function getFiletype() {
247  //return bib for filetype .bibtex:
248  if (strtolower(substr($this->getFilename(), - 6)) == "bibtex") {
249  return self::FILETYPE_BIB;
250  }
251 
252  //else return its true filetype
253  return strtolower(substr($this->getFilename(), - 3));
254  }
255 
256 
260  public static function getAllOverviewModels() {
261  global $DIC;
262  $ilDB = $DIC['ilDB'];
264  $set = $ilDB->query('SELECT * FROM il_bibl_overview_model');
265  while ($rec = $ilDB->fetchAssoc($set)) {
266  if ($rec['literature_type']) {
267  $overviewModels[$rec['filetype']][$rec['literature_type']] = $rec['pattern'];
268  } else {
269  $overviewModels[$rec['filetype']] = $rec['pattern'];
270  }
271  }
272 
273  return $overviewModels;
274  }
275 
276 
284  protected static function __force_rmdir($path) {
285  if (!file_exists($path)) {
286  return false;
287  }
288  if (is_file($path) || is_link($path)) {
289  return unlink($path);
290  }
291  if (is_dir($path)) {
292  $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
293  $result = true;
294  $dir = new DirectoryIterator($path);
295  foreach ($dir as $file) {
296  if (!$file->isDot()) {
297  $result &= self::__force_rmdir($path . $file->getFilename(), false);
298  }
299  }
300  $result &= rmdir($path);
301 
302  return $result;
303  }
304  }
305 
306 
316  public function doCloneObject($new_obj, $a_target_id, $a_copy_id = null, $a_omit_tree = false) {
317  assert($new_obj instanceof ilObjBibliographic);
318  //copy online status if object is not the root copy object
319  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
320 
321  if (!$cp_options->isRootNode($this->getRefId())) {
322  $new_obj->setOnline($this->getOnline());
323  }
324 
325  $new_obj->cloneStructure($this->getId());
326 
327  return $new_obj;
328  }
329 
330 
339  public function cloneStructure($original_id) {
340  $original = new ilObjBibliographic($original_id);
341  $this->moveFile($original->getFileAbsolutePath());
342  $this->setDescription($original->getDescription());
343  $this->setTitle($original->getTitle());
344  $this->setType($original->getType());
345  $this->doUpdate();
346  }
347 
348 
355  protected static function __removeSpacesAndDashesAtBeginning($input) {
356  for ($i = 0; $i < strlen($input); $i ++) {
357  if ($input[$i] != " " && $input[$i] != "-") {
358  return substr($input, $i);
359  }
360  }
361  }
362 
363 
369  public function writeSourcefileEntriesToDb() {
370  //Read File
371  $entries_from_file = array();
372  switch ($this->getFiletype()) {
373  case(self::FILETYPE_RIS):
374  $ilRis = new ilRis();
375  $ilRis->readContent($this->getFileAbsolutePath());
376 
377  $entries_from_file = $ilRis->parseContent();
378  break;
379  case(self::FILETYPE_BIB):
380  $bib = new ilBibTex();
381  $bib->readContent($this->getFileAbsolutePath());
382 
383  $entries_from_file = $bib->parseContent();
384  break;
385  }
386  //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
387  foreach ($entries_from_file as $file_entry) {
388  $type = null;
389  $x = 0;
390  $parsed_entry = array();
391  foreach ($file_entry as $key => $attribute) {
392  // if the attribute is an array, make a comma separated string out of it
393  if (is_array($attribute)) {
394  $attribute = implode(", ", $attribute);
395  }
396  // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
397  //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
398  if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
399  // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
400  $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3) . '...';
401  }
402  // ty (RIS) or entryType (BIB) is the type and is treated seperately
403  if (strtolower($key) == 'ty' || strtolower($key) == 'entrytype') {
404  $type = $attribute;
405  continue;
406  }
407  //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
408  //change array structure (name not as the key, but under the key "name")
409  $parsed_entry[$x]['name'] = $key;
410  $parsed_entry[$x ++]['value'] = $attribute;
411  }
412  //create the entry and fill data into database by executing doCreate()
413  $entry_model = ilBibliographicEntry::getInstance($this->getFiletype());
414  $entry_model->setType($type);
415  $entry_model->setAttributes($parsed_entry);
416  $entry_model->setBibliographicObjId($this->getId());
417  $entry_model->doCreate();
418  }
419  }
420 
421 
425  public function setOnline($a_online) {
426  $this->is_online = $a_online;
427  }
428 
429 
433  public function getOnline() {
434  return $this->is_online;
435  }
436 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
moveFile($file_to_copy=false)
$path
Definition: aliased.php:25
static strLen($a_string)
Definition: class.ilStr.php:91
setType($a_type)
setDescription($a_desc)
$result
$x
Definition: example_009.php:98
Class ilBibTex.
doCloneObject($new_obj, $a_target_id, $a_copy_id=null, $a_omit_tree=false)
Clone BIBL.
Class ilRis.
Definition: class.ilRis.php:11
doDelete($leave_out_il_bibl_data=false, $leave_out_delete_file=false)
static __force_rmdir($path)
remove a directory recursively
__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.
writeSourcefileEntriesToDb()
Reads out the source file and writes all entries to the database.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static __removeSpacesAndDashesAtBeginning($input)
setTitle($a_title)
static getInstance($file_type, $entry_id=null)
Create styles array
The data for the language used.
cloneStructure($original_id)
Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollec...
static getDataDir()
get data directory (outside webspace)
global $ilDB
static _removeEntriesForObject($a_obj_id)
remove all history entries for an object
Class ilObject2 This is an intermediate progress of ilObject class.
global $DIC
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
Class ilObjBibliographic.
static subStr($a_str, $a_start, $a_length=NULL)
Definition: class.ilStr.php:15
getFilePath($without_filename=false)