ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilContainerReference.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 public const TITLE_TYPE_REUSE = 1;
27 public const TITLE_TYPE_CUSTOM = 2;
28
29 protected ilObjUser $user;
30 protected ?int $target_id = null;
31 protected ?int $target_ref_id = null;
33
34 public function __construct(
35 int $a_id = 0,
36 bool $a_call_by_reference = true
37 ) {
38 global $DIC;
39
40 $this->user = $DIC->user();
41 parent::__construct($a_id, $a_call_by_reference);
42 }
43
44 public static function _lookupTargetId(int $a_obj_id): int
45 {
46 global $DIC;
47
48 $ilDB = $DIC->database();
49
50 $query = "SELECT * FROM container_reference " .
51 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
52 $res = $ilDB->query($query);
53 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
54 return (int) $row->target_obj_id;
55 }
56 return $a_obj_id;
57 }
58
59 public static function _lookupTargetRefId(int $a_obj_id): ?int
60 {
61 global $DIC;
62
63 $ilDB = $DIC->database();
64
65 $query = "SELECT ref_id FROM object_reference obr " .
66 "JOIN container_reference cr ON obr.obj_id = cr.target_obj_id " .
67 "WHERE cr.obj_id = " . $ilDB->quote($a_obj_id, 'integer');
68 $res = $ilDB->query($query);
69 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
70 return (int) $row->ref_id;
71 }
72 return null;
73 }
74
75 public static function _lookupTitle(int $obj_id): string
76 {
77 global $DIC;
78
79 $ilDB = $DIC->database();
80
81 $query = 'SELECT title,title_type FROM container_reference cr ' .
82 'JOIN object_data od ON cr.obj_id = od.obj_id ' .
83 'WHERE cr.obj_id = ' . $ilDB->quote($obj_id, 'integer');
84 $res = $ilDB->query($query);
85 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
86 if ((int) $row->title_type === self::TITLE_TYPE_CUSTOM) {
87 return (string) $row->title;
88 }
89 }
90 return self::_lookupTargetTitle($obj_id);
91 }
92
93 public static function _lookupTargetTitle(int $a_obj_id): string
94 {
95 global $DIC;
96
97 $ilDB = $DIC->database();
98
99 $query = "SELECT title FROM object_data od " .
100 "JOIN container_reference cr ON target_obj_id = od.obj_id " .
101 "WHERE cr.obj_id = " . $ilDB->quote($a_obj_id, 'integer');
102 $res = $ilDB->query($query);
103 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
104 return (string) $row->title;
105 }
106 return '';
107 }
108
109 public static function _lookupSourceId(int $a_target_id): ?int
110 {
111 global $DIC;
112
113 $ilDB = $DIC->database();
114
115 $query = "SELECT * FROM container_reference " .
116 "WHERE target_obj_id = " . $ilDB->quote($a_target_id, 'integer') . " ";
117 $res = $ilDB->query($query);
118 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
119 return (int) $row->obj_id;
120 }
121 return null;
122 }
123
129 public static function _lookupSourceIds(int $a_target_id): array
130 {
131 global $DIC;
132
133 $ilDB = $DIC->database();
134
135 $query = "SELECT * FROM container_reference " .
136 "WHERE target_obj_id = " . $ilDB->quote($a_target_id, 'integer') . " ";
137 $res = $ilDB->query($query);
138 $ret = [];
139 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
140 $ret[] = (int) $row->obj_id;
141 }
142 return $ret;
143 }
144
145 public function getTargetId(): ?int
146 {
147 return $this->target_id;
148 }
149
150 public function setTargetId(int $a_target_id): void
151 {
152 $this->target_id = $a_target_id;
153 }
154
155 public function setTargetRefId(int $a_id): void
156 {
157 $this->target_ref_id = $a_id;
158 }
159
160 public function getTargetRefId(): ?int
161 {
163 }
164
165 public function getTitleType(): int
166 {
167 return $this->title_type;
168 }
169
170 public function setTitleType(int $type): void
171 {
172 $this->title_type = $type;
173 }
174
175 public function read(): void
176 {
178
179 parent::read();
180
181 $query = "SELECT * FROM container_reference " .
182 "WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " ";
183 $res = $ilDB->query($query);
184
185 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
186 $this->setTargetId((int) $row->target_obj_id);
187 $this->setTitleType((int) $row->title_type);
188 }
189 if ($this->getTargetId()) {// might be null...
190 $ref_ids = ilObject::_getAllReferences($this->getTargetId());
191 $ref_id = current($ref_ids); // might be null in deletion cases, see #35150
192
193 if ($ref_id) {
194 $this->setTargetRefId($ref_id);
195 }
196
197 if ($this->getTitleType() === self::TITLE_TYPE_REUSE) {
198 $this->title = ilObject::_lookupTitle($this->getTargetId());
199 }
200 }
201 }
202
203 public function getPresentationTitle(): string
204 {
205 if ($this->getTitleType() === self::TITLE_TYPE_CUSTOM) {
206 return $this->getTitle();
207 }
208
209 return $this->lng->txt('reference_of') . ' ' . $this->getTitle();
210 }
211
212 public function update(): bool
213 {
215
216 parent::update();
217
218 $query = "DELETE FROM container_reference " .
219 "WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " ";
220 $ilDB->manipulate($query);
221
222 $query = "INSERT INTO container_reference (obj_id, target_obj_id, title_type) " .
223 "VALUES( " .
224 $ilDB->quote($this->getId(), 'integer') . ", " .
225 $ilDB->quote($this->getTargetId(), 'integer') . ", " .
226 $ilDB->quote($this->getTitleType(), 'integer') . ' ' .
227 ")";
228 $ilDB->manipulate($query);
229 return true;
230 }
231
232 public function delete(): bool
233 {
235
236 if (!parent::delete()) {
237 return false;
238 }
239
240 $query = "DELETE FROM container_reference " .
241 "WHERE obj_id = " . $ilDB->quote($this->getId(), 'integer') . " ";
242 $ilDB->manipulate($query);
243
244 return true;
245 }
246
247 public function cloneObject(int $target_id, int $copy_id = 0, bool $omit_tree = false): ?ilObject
248 {
249 $new_obj = parent::cloneObject($target_id, $copy_id, $omit_tree);
250 $new_obj->setTargetId($this->getTargetId());
251 $new_obj->setTitleType($this->getTitleType());
252 $new_obj->update();
253 return $new_obj;
254 }
255}
static _lookupTitle(int $obj_id)
__construct(int $a_id=0, bool $a_call_by_reference=true)
static _lookupSourceIds(int $a_target_id)
Get ids of all container references that target the object with the given id.
static _lookupTargetId(int $a_obj_id)
static _lookupTargetRefId(int $a_obj_id)
cloneObject(int $target_id, int $copy_id=0, bool $omit_tree=false)
static _lookupTargetTitle(int $a_obj_id)
static _lookupSourceId(int $a_target_id)
getPresentationTitle()
get presentation title Normally same as title Overwritten for sessions
User class.
Class ilObject Basic functions for all objects.
static _getAllReferences(int $id)
get all reference ids for object ID
string $type
static _lookupTitle(int $obj_id)
ilDBInterface $db
$res
Definition: ltiservices.php:69
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26