ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBiblFieldFactory.php
Go to the documentation of this file.
1<?php
2
25{
29 public function __construct(protected \ilBiblTypeInterface $type)
30 {
31 }
32
33
37 public function getType(): ilBiblTypeInterface
38 {
39 return $this->type;
40 }
41
42
46 public function findById(int $id): ilBiblFieldInterface
47 {
52 if ($this->type->isStandardField($inst->getIdentifier()) !== $inst->isStandardField()) {
53 $inst->setIsStandardField($this->type->isStandardField($inst->getIdentifier()));
54 $inst->update();
55 }
56
57 return $inst;
58 }
59
60
64 public function getFieldByTypeAndIdentifier(int $type, string $identifier): ilBiblFieldInterface
65 {
66 $inst = $this->getARInstance($type, $identifier);
67 if ($inst === null) {
68 throw new ilException("bibliografic identifier {$identifier} not found");
69 }
70
72 return $inst;
73 }
74
75
79 public function findOrCreateFieldByTypeAndIdentifier(int $type, string $identifier): ilBiblFieldInterface
80 {
81 $inst = $this->getARInstance($type, $identifier);
82 if ($inst === null) {
83 $inst = new ilBiblField();
84 $inst->setIdentifier($identifier);
85 $inst->setDataType($type);
86 $inst->setIsStandardField($this->getType()->isStandardField($identifier));
87 $inst->create();
88 }
89 $inst->setDataType($type);
90 $inst->setIdentifier($identifier);
91 $inst->setIsStandardField($this->getType()->isStandardField($identifier));
92 $inst->update();
93
94 return $inst;
95 }
96
97
101 public function getAvailableFieldsForObjId(int $obj_id): array
102 {
103 global $DIC;
104 $sql
105 = "SELECT DISTINCT(il_bibl_attribute.name), il_bibl_data.file_type FROM il_bibl_data
106 JOIN il_bibl_entry ON il_bibl_entry.data_id = il_bibl_data.id
107 JOIN il_bibl_attribute ON il_bibl_attribute.entry_id = il_bibl_entry.id
108 WHERE il_bibl_data.id = %s;";
109
110 $result = $DIC->database()->queryF($sql, ['integer'], [$obj_id]);
111
112 $data = [];
113 while ($d = $DIC->database()->fetchObject($result)) {
114 $data[] = $this->findOrCreateFieldByTypeAndIdentifier($d->file_type, $d->name);
115 }
116
117 return $data;
118 }
119
120
124 public function filterAllFieldsForType(ilBiblTypeInterface $type, ?ilBiblTableQueryInfoInterface $queryInfo = null): array
125 {
126 return $this->getCollectionForFilter($type, $queryInfo)->get();
127 }
128
129
134 {
135 return $this->getCollectionForFilter($type, $queryInfo)->getArray();
136 }
137
138
143 {
144 $field = ilBiblField::where(['identifier' => $attribute->getName()])->first();
145 if ($field === null) {
146 $field = new ilBiblField();
147 $field->setIdentifier($attribute->getName());
148 $field->setDataType($this->type->getId());
149 $field->setIsStandardField($this->type->isStandardField($attribute->getName()));
150 $field->create();
151 } else {
152 $field->setDataType($this->type->getId());
153 $field->update();
154 }
155
156 return $field;
157 }
158
159
163 public function forcePosition(ilBiblFieldInterface $field): int
164 {
165 global $DIC;
166 $tablename = ilBiblField::TABLE_NAME;
167 $q = "UPDATE {$tablename} SET position = position + 1 WHERE data_type = %s AND position >= %s;";
168 $DIC->database()->manipulateF(
169 $q,
170 ['integer', 'integer'],
171 [
172 $field->getDataType(),
173 $field->getPosition(),
174 ]
175 );
176 $field->store();
177 $DIC->database()->query("SET @i=0");
178 $DIC->database()->manipulateF(
179 "UPDATE {$tablename} SET position = (@i := @i + 1) WHERE data_type = %s ORDER BY position",
180 ['integer'],
181 [
182 $field->getDataType(),
183 ]
184 );
185
186 return (int) $field->getPosition();
187 }
188
189 // Internal Methods
190
191
198 {
199 global $DIC;
200 $tablename = ilBiblField::TABLE_NAME;
201 $q = "SELECT MAX(position) + 1 as next_position FROM {$tablename} WHERE data_type = %s;";
202 $res = $DIC->database()->queryF($q, ['integer'], [$field->getDataType()]);
203 $data = $DIC->database()->fetchObject($res);
204
205 return (int) $data->next_position;
206 }
207
208
209 private function getARInstance(int $type, string $identifier): ?\ilBiblField
210 {
211 return ilBiblField::where(["identifier" => $identifier, "data_type" => $type])->first();
212 }
213
214
216 {
217 $collection = ilBiblField::getCollection();
218
219 $collection->where(['data_type' => $type->getId()]);
220
221 if ($queryInfo !== null) {
222 $sorting_column = $queryInfo->getSortingColumn() !== '' && $queryInfo->getSortingColumn() !== '0' ? $queryInfo->getSortingColumn() : null;
223 $offset = $queryInfo->getOffset();
224 $sorting_direction = $queryInfo->getSortingDirection();
225 $limit = $queryInfo->getLimit();
226 if ($sorting_column) {
227 $collection->orderBy($sorting_column, $sorting_direction);
228 }
229 $collection->limit($offset, $limit);
230
231 foreach ($queryInfo->getFilters() as $queryFilter) {
232 $collection->where([$queryFilter->getFieldName() => $queryFilter->getFieldValue()], $queryFilter->getOperator());
233 }
234 }
235
236 return $collection;
237 }
238}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static where($where, $operator=null)
static findOrFail($primary_key, array $add_constructor_args=[])
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)
getARInstance(int $type, string $identifier)
filterAllFieldsForTypeAsArray(ilBiblTypeInterface $type, ?ilBiblTableQueryInfoInterface $queryInfo=null)
@inheritDoc
__construct(protected \ilBiblTypeInterface $type)
ilBiblFieldFactory constructor.
filterAllFieldsForType(ilBiblTypeInterface $type, ?ilBiblTableQueryInfoInterface $queryInfo=null)
@inheritDoc
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
forcePosition(ilBiblFieldInterface $field)
@inheritDoc
getCollectionForFilter(ilBiblTypeInterface $type, ?ilBiblTableQueryInfoInterface $queryInfo=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Base class for ILIAS Exception handling.
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
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23