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