ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBiblFieldFactory.php
Go to the documentation of this file.
1 <?php
2 
9 {
10 
14  protected $type;
15 
16 
23  {
24  $this->type = $type;
25  }
26 
27 
31  public function getType() : ilBiblTypeInterface
32  {
33  return $this->type;
34  }
35 
36 
40  public function findById(int $id) : ilBiblFieldInterface
41  {
45  $inst = ilBiblField::findOrFail($id);
46  if ($this->type->isStandardField($inst->getIdentifier()) != $inst->getisStandardField()) {
47  $inst->setIsStandardField($this->type->isStandardField($inst->getIdentifier()));
48  $inst->update();
49  }
50 
51  return $inst;
52  }
53 
54 
58  public function getFieldByTypeAndIdentifier(int $type, string $identifier) : ilBiblFieldInterface
59  {
60  $this->checkType($type);
61  $inst = $this->getARInstance($type, $identifier);
62  if (!$inst) {
63  throw new ilException("bibliografic identifier {$identifier} not found");
64  }
65 
66  return $inst;
67  }
68 
69 
73  public function findOrCreateFieldByTypeAndIdentifier(int $type, string $identifier) : ilBiblFieldInterface
74  {
75  $this->checkType($type);
76  $inst = $this->getARInstance($type, $identifier);
77  if (!$inst) {
78  $inst = new ilBiblField();
79  $inst->setIdentifier($identifier);
80  $inst->setDataType($type);
81  $inst->setIsStandardField((bool) $this->getType()->isStandardField($identifier));
82  $inst->create();
83  }
84  $inst->setDataType($type);
85  $inst->setIdentifier($identifier);
86  $inst->setIsStandardField((bool) $this->getType()->isStandardField($identifier));
87  $inst->update();
88 
89  return $inst;
90  }
91 
92 
96  public function getAvailableFieldsForObjId(int $obj_id) : array
97  {
98  global $DIC;
99  $sql
100  = "SELECT DISTINCT(il_bibl_attribute.name), il_bibl_data.file_type FROM il_bibl_data
101  JOIN il_bibl_entry ON il_bibl_entry.data_id = il_bibl_data.id
102  JOIN il_bibl_attribute ON il_bibl_attribute.entry_id = il_bibl_entry.id
103  WHERE il_bibl_data.id = %s;";
104 
105  $result = $DIC->database()->queryF($sql, ['integer'], [$obj_id]);
106 
107  $data = [];
108  while ($d = $DIC->database()->fetchObject($result)) {
109  $data[] = $this->findOrCreateFieldByTypeAndIdentifier($d->file_type, $d->name);
110  }
111 
112  return $data;
113  }
114 
115 
120  {
121  return $this->getCollectionForFilter($type, $queryInfo)->get();
122  }
123 
124 
129  {
130  return $this->getCollectionForFilter($type, $queryInfo)->getArray();
131  }
132 
133 
138  {
139  $field = ilBiblField::where(['identifier' => $attribute->getName()])->first();
140  if ($field === null) {
141  $field = new ilBiblField();
142  $field->setIdentifier($attribute->getName());
143  $field->setDataType($this->type->getId());
144  $field->setIsStandardField($this->type->isStandardField($attribute->getName()));
145  $field->create();
146  } else {
147  $field->setDataType($this->type->getId());
148  $field->update();
149  }
150 
151  return $field;
152  }
153 
154 
158  public function forcePosition(ilBiblFieldInterface $field) : int
159  {
160  global $DIC;
161  $tablename = ilBiblField::TABLE_NAME;
162  $q = "UPDATE {$tablename} SET position = position + 1 WHERE data_type = %s AND position >= %s;";
163  $DIC->database()->manipulateF(
164  $q,
165  ['integer', 'integer'],
166  [
167  $field->getDataType(), $field->getPosition(),
168  ]
169  );
170  $field->store();
171  $DIC->database()->query("SET @i=0");
172  $DIC->database()->manipulateF(
173  "UPDATE {$tablename} SET position = (@i := @i + 1) WHERE data_type = %s ORDER BY position",
174  ['integer'],
175  [
176  $field->getDataType(),
177  ]
178  );
179 
180  return (int) $field->getPosition();
181  }
182 
183  // Internal Methods
184 
185 
191  private function getNextFreePosition(ilBiblFieldInterface $field) : int
192  {
193  global $DIC;
194  $tablename = ilBiblField::TABLE_NAME;
195  $q = "SELECT MAX(position) + 1 as next_position FROM {$tablename} WHERE data_type = %s;";
196  $res = $DIC->database()->queryF($q, ['integer'], [$field->getDataType()]);
197  $data = $DIC->database()->fetchObject($res);
198 
199  return (int) $data->next_position;
200  }
201 
202 
209  private function getARInstance($type, $identifier)
210  {
211  return ilBiblField::where(["identifier" => $identifier, "data_type" => $type])->first();
212  }
213 
214 
220  private function checkType($type)
221  {
222  switch ($type) {
225  break;
226  default:
227  throw new ilException("bibliografic type not found");
228  }
229  }
230 
231 
239  {
240  $collection = ilBiblField::getCollection();
241 
242  $collection->where(array('data_type' => $type->getId()));
243 
244  if ($queryInfo) {
245  $sorting_column = $queryInfo->getSortingColumn() ? $queryInfo->getSortingColumn() : null;
246  $offset = $queryInfo->getOffset() ? $queryInfo->getOffset() : 0;
247  $sorting_direction = $queryInfo->getSortingDirection();
248  $limit = $queryInfo->getLimit();
249  if ($sorting_column) {
250  $collection->orderBy($sorting_column, $sorting_direction);
251  }
252  $collection->limit($offset, $limit);
253 
254  foreach ($queryInfo->getFilters() as $queryFilter) {
255  switch ($queryFilter->getFieldName()) {
256  default:
257  $collection->where(array($queryFilter->getFieldName() => $queryFilter->getFieldValue()), $queryFilter->getOperator());
258  break;
259  }
260  }
261  }
262 
263  return $collection;
264  }
265 }
__construct(\ilBiblTypeInterface $type)
ilBiblFieldFactory constructor.
Class ilField.
store()
Stores the Object, creates a newone in Db if non existing or updates an existing. ...
Interface ilBiblEntryInterface.
$result
global $DIC
Definition: saml.php:7
Interface ilBiblAttributeInterface.
forcePosition(ilBiblFieldInterface $field)
if(!array_key_exists('StateId', $_REQUEST)) $id
static where($where, $operator=null)
getFieldByTypeAndIdentifier(int $type, string $identifier)
MUST be ilBiblTypeFactoryInterface::DATA_TYPE_RIS or ilBiblTypeFactoryInterface::DATA_TYPE_BIBTEX ...
foreach($_POST as $key=> $value) $res
Interface ilBiblTypeInterface.
static findOrFail($primary_key, array $add_constructor_args=array())
Tries to find the object and throws an Exception if object is not found, instead of returning null...
filterAllFieldsForType(ilBiblTypeInterface $type, ilBiblTableQueryInfoInterface $queryInfo=null)
getAvailableFieldsForObjId(int $obj_id)
Interface ilBiblFieldFactoryInterface.
getNextFreePosition(ilBiblFieldInterface $field)
findOrCreateFieldOfAttribute(ilBiblAttributeInterface $attribute)
getARInstance($type, $identifier)
filterAllFieldsForTypeAsArray(ilBiblTypeInterface $type, ilBiblTableQueryInfoInterface $queryInfo=null)
findOrCreateFieldByTypeAndIdentifier(int $type, string $identifier)
MUST be ilBiblTypeFactoryInterface::DATA_TYPE_RIS or ilBiblTypeFactoryInterface::DATA_TYPE_BIBTEX ...
Class ilBiblFieldFactory.
Interface ilBiblTableQueryInfoInterface.
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
getCollectionForFilter(ilBiblTypeInterface $type, ilBiblTableQueryInfoInterface $queryInfo=null)
$data
Definition: bench.php:6