ILIAS  release_8 Revision v8.23
class.ilBiblAttributeFactory.php
Go to the documentation of this file.
1 <?php
23 {
24  protected \ilBiblFieldFactoryInterface $field_factory;
25  protected ilDBInterface $db;
26 
27  public function __construct(ilBiblFieldFactoryInterface $field_factory)
28  {
29  global $DIC;
30  $this->field_factory = $field_factory;
31  $this->db = $DIC->database();
32  }
33 
37  public function getPossibleValuesForFieldAndObject(ilBiblFieldInterface $field, int $object_id): array
38  {
39  $q = "SELECT DISTINCT(a.value) FROM il_bibl_data AS d
40 JOIN il_bibl_entry AS e ON e.data_id = d.id
41 JOIN il_bibl_attribute AS a on a.entry_id = e.id
42 WHERE a.name = %s AND d.id = %s";
43 
44  $res = $this->db->queryF($q, ['text', 'integer'], [
45  $field->getIdentifier(),
46  $object_id,
47  ]);
48  $result = [];
49  while ($data = $this->db->fetchObject($res)) {
50  $result[$data->value] = $data->value;
51  }
52 
53  return $result;
54  }
55 
59  public function getAttributesForEntry(ilBiblEntryInterface $entry): array
60  {
61  return ilBiblAttribute::where(['entry_id' => $entry->getId()])->get();
62  }
63 
67  public function sortAttributes(array $attributes): array
68  {
69  $sorted = [];
70  $type_id = $this->field_factory->getType()->getId();
71  $max = 0;
72  foreach ($attributes as $attribute) {
73  if (!$attribute->getName()) {
74  continue;
75  }
76  $field = $this->field_factory->findOrCreateFieldByTypeAndIdentifier($type_id, $attribute->getName());
77  $position = $field->getPosition();
78  $position = $position ?: $max + 1;
79 
80  $max = (max($position, $max));
81  $sorted[$position] = $attribute;
82  }
83 
84  ksort($sorted);
85 
86  return $sorted;
87  }
88 
92  public function createAttribute(string $name, string $value, int $entry_id): bool
93  {
94  $ilBiblAttribute = new ilBiblAttribute();
95  $ilBiblAttribute->setName($name);
96  $ilBiblAttribute->setValue($value);
97  $ilBiblAttribute->setEntryId($entry_id);
98  $ilBiblAttribute->store();
99 
100  $this->field_factory->findOrCreateFieldOfAttribute($ilBiblAttribute);
101 
102  return true;
103  }
104 }
$attributes
Definition: metadata.php:248
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getPossibleValuesForFieldAndObject(ilBiblFieldInterface $field, int $object_id)
static where($where, $operator=null)
createAttribute(string $name, string $value, int $entry_id)
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilBiblFieldFactoryInterface $field_factory
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...
getAttributesForEntry(ilBiblEntryInterface $entry)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilBiblFieldFactoryInterface $field_factory)