ILIAS  release_8 Revision v8.23
class.ilBiblEntryFactory.php
Go to the documentation of this file.
1 <?php
23 {
25 
26  protected int $bibliographic_obj_id;
27  protected int $entry_id;
28  protected string $type;
29  protected array $attributes;
30  protected \ilBiblTypeInterface $file_type;
31  protected \ilBiblFieldFactoryInterface $field_factory;
32  protected \ilBiblOverviewModelFactoryInterface $overview_factory;
33  protected ilDBInterface $db;
34 
35 
39  public function __construct(ilBiblFieldFactoryInterface $field_factory, \ilBiblTypeInterface $file_type, ilBiblOverviewModelFactoryInterface $overview_factory)
40  {
41  global $DIC;
42  $this->db = $DIC->database();
43  $this->file_type = $file_type;
44  $this->field_factory = $field_factory;
45  $this->overview_factory = $overview_factory;
46  }
47 
51  public function loadParsedAttributesByEntryId(int $entry_id): array
52  {
53  $ilBiblEntry = ilBiblEntry::where(array('id' => $entry_id))->first();
54  $attributes = $this->getAllAttributesByEntryId($entry_id);
55 
56  if ($this->file_type->getId() == ilBiblTypeFactoryInterface::DATA_TYPE_RIS) {
57  //for RIS-Files also add the type;
58  $type = $ilBiblEntry->getType();
59  } else {
60  $type = 'default';
61  }
62  $parsed_attributes = array();
63  foreach ($attributes as $attribute) {
64  $value = $this->secure($attribute->getValue());
65  // surround links with <a href="">
66  // Allowed signs in URL: a-z A-Z 0-9 . ? & _ / - ~ ! ' * ( ) + , : ; @ = $ # [ ] %
67  $value = preg_replace('!(http)(s)?:\/\/[a-zA-Z0-9.?&_/\-~\!\'\*()+,:;@=$#\[\]%]+!', "<a href=\"\\0\" target=\"_blank\">\\0</a>", $value);
68 
69 
70 
71  $attribute->setValue($value);
72  $parsed_attributes[strtolower($this->file_type->getStringRepresentation() . '_' . $type . '_' . $attribute->getName())] = $value;
73 
74  $this->field_factory->findOrCreateFieldOfAttribute($attribute);
75  }
76 
77  return $parsed_attributes;
78  }
79 
83  public function findByIdAndTypeString(int $id, string $type_string): ilBiblEntryInterface
84  {
86  return ilBiblEntry::where(array('id' => $id))->first();
87  }
88 
92  public function findOrCreateEntry(int $id, int $bibliographic_obj_id, string $entry_type): \ilBiblEntryInterface
93  {
94  $inst = $this->getARInstance($id);
95  if (!$inst) {
96  $inst = $this->createEntry($bibliographic_obj_id, $entry_type);
97  }
98  $inst->setDataId($bibliographic_obj_id);
99  $inst->setEntryType($entry_type);
100  $inst->update();
101 
102  return $inst;
103  }
104 
108  public function createEntry(int $bibliographic_obj_id, string $entry_type): \ilBiblEntryInterface
109  {
110  $inst = new ilBiblEntry();
111  $inst->setDataId($bibliographic_obj_id);
112  $inst->setEntryType($entry_type);
113  $inst->create();
114 
115  return $inst;
116  }
117 
118  public function getEmptyInstance(): \ilBiblEntry
119  {
120  return new ilBiblEntry();
121  }
122 
123  private function getARInstance(int $id): ?\ilBiblEntry
124  {
126  return ilBiblEntry::where(["ïd" => $id])->first();
127  }
128 
132  public function filterEntriesForTable(int $object_id, ilBiblTableQueryInfo $info = null): array
133  {
134  $entries = $this->filterEntryIdsForTableAsArray($object_id, $info);
135  $entry_objects = [];
136  foreach ($entries as $entry_id => $entry) {
137  $entry_objects[$entry_id] = $this->findByIdAndTypeString($entry['type'], $entry['id']);
138  }
139 
140  return $entry_objects;
141  }
142 
146  public function filterEntryIdsForTableAsArray(int $object_id, ?ilBiblTableQueryInfo $info = null): array
147  {
148  $types = ["integer"];
149  $values = [$object_id];
150 
151  $filters = $info->getFilters();
152  if (!empty($filters)) {
153  $q = "SELECT (e.id), e.type FROM il_bibl_entry AS e WHERE data_id = %s";
154  foreach ($filters as $filter) {
155  $value = $filter->getFieldValue();
156  if (!$value) {
157  continue;
158  }
159  if ($filter->getOperator() === "IN" && is_array($filter->getFieldValue())) {
160  $types[] = "text";
161  $values[] = $filter->getFieldName();
162  $q .= " AND e.id IN (SELECT a.entry_id FROM il_bibl_attribute AS a WHERE a.name = %s AND " . $this->db->in("a.value", $value, false, "text") . ")";
163  } else {
164  $types[] = "text";
165  $values[] = $filter->getFieldName();
166  $types[] = "text";
167  $values[] = "{$value}";
168  $q .= " AND e.id IN (SELECT a.entry_id FROM il_bibl_attribute AS a WHERE a.name = %s AND a.value {$filter->getOperator()} %s )";
169  }
170  }
171  } else {
172  $q = "SELECT DISTINCT (e.id), e.type FROM il_bibl_entry AS e
173  JOIN il_bibl_attribute AS a ON a.entry_id = e.id
174  WHERE data_id = %s";
175  }
176  $entries = [];
177  $set = $this->db->queryF($q, $types, $values);
178 
179  $i = 0;
180  while ($rec = $this->db->fetchAssoc($set)) {
181  $entries[$i]['entry_id'] = $rec['id'];
182  $entries[$i]['entry_type'] = $rec['type'];
183  $i++;
184  }
185 
186  return $entries;
187  }
188 
189  public function deleteEntryById(int $id): void
190  {
191  $entry = ilBiblEntry::where(array('id' => $id))->first();
192  if ($entry instanceof ilBiblEntry) {
193  $entry->delete();
194  }
195  }
196 
197  public function deleteEntriesById(int $object_id): void
198  {
199  $this->db->manipulateF("DELETE FROM il_bibl_entry WHERE data_id = %s", ['integer'], [$object_id]);
200  }
201 
205  public function getAllAttributesByEntryId(int $id): array
206  {
207  return ilBiblAttribute::where(array('entry_id' => $id))->get();
208  }
209 
210  public function getFileType(): \ilBiblTypeInterface
211  {
212  return $this->file_type;
213  }
214 
215  public function setFileType(string $file_type): void
216  {
217  $this->file_type = $file_type;
218  }
219 
223  public function setAttributes(array $attributes): void
224  {
225  $this->attributes = $attributes;
226  }
227 
232  public function getAttributes(): array
233  {
234  return $this->attributes;
235  }
236 }
setAttributes(array $attributes)
createEntry(int $bibliographic_obj_id, string $entry_type)
setFileType(string $file_type)
findOrCreateEntry(int $id, int $bibliographic_obj_id, string $entry_type)
ilBiblOverviewModelFactoryInterface $overview_factory
static where($where, $operator=null)
global $DIC
Definition: feed.php:28
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...
__construct(ilBiblFieldFactoryInterface $field_factory, \ilBiblTypeInterface $file_type, ilBiblOverviewModelFactoryInterface $overview_factory)
ilBiblEntryFactory constructor.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
loadParsedAttributesByEntryId(int $entry_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
filterEntriesForTable(int $object_id, ilBiblTableQueryInfo $info=null)
filterEntryIdsForTableAsArray(int $object_id, ?ilBiblTableQueryInfo $info=null)
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...
ilBiblTypeInterface $file_type
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
findByIdAndTypeString(int $id, string $type_string)
ilBiblFieldFactoryInterface $field_factory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$i
Definition: metadata.php:41