ILIAS  release_8 Revision v8.24
class.ilBiblFieldFactory.php
Go to the documentation of this file.
1<?php
2
25{
26 protected \ilBiblTypeInterface $type;
27
28
33 {
34 $this->type = $type;
35 }
36
37
41 public function getType(): ilBiblTypeInterface
42 {
43 return $this->type;
44 }
45
46
50 public function findById(int $id): ilBiblFieldInterface
51 {
56 if ($this->type->isStandardField($inst->getIdentifier()) !== $inst->isStandardField()) {
57 $inst->setIsStandardField($this->type->isStandardField($inst->getIdentifier()));
58 $inst->update();
59 }
60
61 return $inst;
62 }
63
64
68 public function getFieldByTypeAndIdentifier(int $type, string $identifier): ilBiblFieldInterface
69 {
70 $inst = $this->getARInstance($type, $identifier);
71 if (!$inst) {
72 throw new ilException("bibliografic identifier {$identifier} not found");
73 }
74
76 return $inst;
77 }
78
79
83 public function findOrCreateFieldByTypeAndIdentifier(int $type, string $identifier): ilBiblFieldInterface
84 {
85 $inst = $this->getARInstance($type, $identifier);
86 if ($inst === null) {
87 $inst = new ilBiblField();
88 $inst->setIdentifier($identifier);
89 $inst->setDataType($type);
90 $inst->setIsStandardField($this->getType()->isStandardField($identifier));
91 $inst->create();
92 }
93 $inst->setDataType($type);
94 $inst->setIdentifier($identifier);
95 $inst->setIsStandardField($this->getType()->isStandardField($identifier));
96 $inst->update();
97
98 return $inst;
99 }
100
101
105 public function getAvailableFieldsForObjId(int $obj_id): array
106 {
107 global $DIC;
108 $sql
109 = "SELECT DISTINCT(il_bibl_attribute.name), il_bibl_data.file_type FROM il_bibl_data
110 JOIN il_bibl_entry ON il_bibl_entry.data_id = il_bibl_data.id
111 JOIN il_bibl_attribute ON il_bibl_attribute.entry_id = il_bibl_entry.id
112 WHERE il_bibl_data.id = %s;";
113
114 $result = $DIC->database()->queryF($sql, ['integer'], [$obj_id]);
115
116 $data = [];
117 while ($d = $DIC->database()->fetchObject($result)) {
118 $data[] = $this->findOrCreateFieldByTypeAndIdentifier($d->file_type, $d->name);
119 }
120
121 return $data;
122 }
123
124
129 {
130 return $this->getCollectionForFilter($type, $queryInfo)->get();
131 }
132
133
138 {
139 return $this->getCollectionForFilter($type, $queryInfo)->getArray();
140 }
141
142
147 {
148 $field = ilBiblField::where(['identifier' => $attribute->getName()])->first();
149 if ($field === null) {
150 $field = new ilBiblField();
151 $field->setIdentifier($attribute->getName());
152 $field->setDataType($this->type->getId());
153 $field->setIsStandardField($this->type->isStandardField($attribute->getName()));
154 $field->create();
155 } else {
156 $field->setDataType($this->type->getId());
157 $field->update();
158 }
159
160 return $field;
161 }
162
163
167 public function forcePosition(ilBiblFieldInterface $field): int
168 {
169 global $DIC;
170 $tablename = ilBiblField::TABLE_NAME;
171 $q = "UPDATE {$tablename} SET position = position + 1 WHERE data_type = %s AND position >= %s;";
172 $DIC->database()->manipulateF(
173 $q,
174 ['integer', 'integer'],
175 [
176 $field->getDataType(),
177 $field->getPosition(),
178 ]
179 );
180 $field->store();
181 $DIC->database()->query("SET @i=0");
182 $DIC->database()->manipulateF(
183 "UPDATE {$tablename} SET position = (@i := @i + 1) WHERE data_type = %s ORDER BY position",
184 ['integer'],
185 [
186 $field->getDataType(),
187 ]
188 );
189
190 return (int) $field->getPosition();
191 }
192
193 // Internal Methods
194
195
202 {
203 global $DIC;
204 $tablename = ilBiblField::TABLE_NAME;
205 $q = "SELECT MAX(position) + 1 as next_position FROM {$tablename} WHERE data_type = %s;";
206 $res = $DIC->database()->queryF($q, ['integer'], [$field->getDataType()]);
207 $data = $DIC->database()->fetchObject($res);
208
209 return (int) $data->next_position;
210 }
211
212
213 private function getARInstance(int $type, string $identifier): ?\ilBiblField
214 {
215 return ilBiblField::where(["identifier" => $identifier, "data_type" => $type])->first();
216 }
217
218
220 {
221 $collection = ilBiblField::getCollection();
222
223 $collection->where(array('data_type' => $type->getId()));
224
225 if ($queryInfo) {
226 $sorting_column = $queryInfo->getSortingColumn() ? $queryInfo->getSortingColumn() : null;
227 $offset = $queryInfo->getOffset() ? $queryInfo->getOffset() : 0;
228 $sorting_direction = $queryInfo->getSortingDirection();
229 $limit = $queryInfo->getLimit();
230 if ($sorting_column) {
231 $collection->orderBy($sorting_column, $sorting_direction);
232 }
233 $collection->limit($offset, $limit);
234
235 foreach ($queryInfo->getFilters() as $queryFilter) {
236 switch ($queryFilter->getFieldName()) {
237 default:
238 $collection->where(array($queryFilter->getFieldName() => $queryFilter->getFieldValue()), $queryFilter->getOperator());
239 break;
240 }
241 }
242 }
243
244 return $collection;
245 }
246}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
@noinspection NullPointerExceptionInspection
static where($where, $operator=null)
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getNextFreePosition(ilBiblFieldInterface $field)
ilBiblTypeInterface $type
getARInstance(int $type, string $identifier)
filterAllFieldsForType(ilBiblTypeInterface $type, ilBiblTableQueryInfoInterface $queryInfo=null)
@inheritDoc
getCollectionForFilter(ilBiblTypeInterface $type, ilBiblTableQueryInfoInterface $queryInfo=null)
getAvailableFieldsForObjId(int $obj_id)
@inheritDoc
getFieldByTypeAndIdentifier(int $type, string $identifier)
ilException if a wrong $type is passed or field is not found
findOrCreateFieldOfAttribute(ilBiblAttributeInterface $attribute)
@inheritDoc
findOrCreateFieldByTypeAndIdentifier(int $type, string $identifier)
ilException if a wrong $type is passed
filterAllFieldsForTypeAsArray(ilBiblTypeInterface $type, ilBiblTableQueryInfoInterface $queryInfo=null)
@inheritDoc
forcePosition(ilBiblFieldInterface $field)
@inheritDoc
__construct(\ilBiblTypeInterface $type)
ilBiblFieldFactory constructor.
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...
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
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...
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$res
Definition: ltiservices.php:69