ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjLinkResource.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
28 
29  public function __construct(int $a_id = 0, bool $a_call_by_reference = true)
30  {
31  $this->type = "webr";
32  parent::__construct($a_id, $a_call_by_reference);
33  }
34 
35  protected function getWebLinkRepo(): ilWebLinkRepository
36  {
37  if (isset($this->repo)) {
38  return $this->repo;
39  }
40  return $this->repo = new ilWebLinkDatabaseRepository($this->getId());
41  }
42 
46  public function create($a_upload = false): int
47  {
48  $new_id = parent::create();
49  if (!$a_upload) {
50  $this->createMetaData();
51  }
52  return $new_id;
53  }
54 
55  public function update(): bool
56  {
57  $this->updateMetaData();
58  return parent::update();
59  }
60 
61  protected function doMDUpdateListener(string $a_element): void
62  {
63  $md = new ilMD($this->getId(), 0, $this->getType());
64  if (!is_object($md_gen = $md->getGeneral())) {
65  return;
66  }
67  $title = $md_gen->getTitle();
68  $description = '';
69  foreach ($md_gen->getDescriptionIds() as $id) {
70  $md_des = $md_gen->getDescription($id);
71  $description = $md_des->getDescription();
72  break;
73  }
74  if (
75  $a_element === 'General' &&
76  !$this->getWebLinkRepo()->doesListExist() &&
77  $this->getWebLinkRepo()->doesOnlyOneItemExist()
78  ) {
79  $item = $this->getWebLinkRepo()->getAllItemsAsContainer()->getFirstItem();
80  $draft = new ilWebLinkDraftItem(
81  $item->isInternal(),
82  $title,
83  $description,
84  $item->getTarget(),
85  $item->isActive(),
86  $item->getParameters()
87  );
88  $this->getWebLinkRepo()->updateItem($item, $draft);
89  }
90  if (
91  $a_element === 'General' &&
92  $this->getWebLinkRepo()->doesListExist()
93  ) {
94  $list = $this->getWebLinkRepo()->getList();
95  $draft = new ilWebLinkDraftList(
96  $title,
97  $description
98  );
99  $this->getWebLinkRepo()->updateList($list, $draft);
100  }
101  }
102 
103  public function delete(): bool
104  {
105  // always call parent delete function first!!
106  if (!parent::delete()) {
107  return false;
108  }
109 
110  // delete items and list
111  $this->getWebLinkRepo()->deleteAllItems();
112  if ($this->getWebLinkRepo()->doesListExist()) {
113  $this->getWebLinkRepo()->deleteList();
114  }
115 
116  // delete meta data
117  $this->deleteMetaData();
118 
119  return true;
120  }
121 
122  public function cloneObject(
123  int $target_id,
124  int $copy_id = 0,
125  bool $omit_tree = false
126  ): ?ilObject {
127  $new_obj = parent::cloneObject($target_id, $copy_id, $omit_tree);
128  $this->cloneMetaData($new_obj);
129 
130  // object created, now copy items and parameters
131  $items = $this->getWebLinkRepo()->getAllItemsAsContainer()->getItems();
133 
134  foreach ($items as $item) {
135  $draft = new ilWebLinkDraftItem(
136  $item->isInternal(),
137  $item->getTitle(),
138  $item->getDescription(),
139  $item->getTarget(),
140  $item->isActive(),
141  $item->getParameters()
142  );
143 
144  $container->addItem($draft);
145  }
146 
147  $new_web_link_repo = new ilWebLinkDatabaseRepository($new_obj->getId());
148  $new_web_link_repo->createAllItemsInDraftContainer($container);
149 
150  // append copy info weblink title
151  if ($new_web_link_repo->doesOnlyOneItemExist(true)) {
152  $item = ilObjLinkResourceAccess::_getFirstLink($new_obj->getId());
153  $draft = new ilWebLinkDraftItem(
154  $item->isInternal(),
155  $new_obj->getTitle(),
156  $new_obj->getDescription(),
157  $item->getTarget(),
158  $item->isActive(),
159  $item->getParameters()
160  );
161  $new_web_link_repo->updateItem($item, $draft);
162  }
163  return $new_obj;
164  }
165 
166  public function toXML(ilXmlWriter $writer): void
167  {
168  $attribs = array("obj_id" => "il_" . IL_INST_ID . "_webr_" . $this->getId(
169  )
170  );
171 
172  $writer->xmlStartTag('WebLinks', $attribs);
173 
174  // LOM MetaData
175  $md2xml = new ilMD2XML($this->getId(), $this->getId(), 'webr');
176  $md2xml->startExport();
177  $writer->appendXML($md2xml->getXML());
178 
179  // Sorting
182  $writer->xmlElement(
183  'Sorting',
184  array('type' => 'Manual')
185  );
186  break;
187 
189  default:
190  $writer->xmlElement(
191  'Sorting',
192  array('type' => 'Title')
193  );
194  break;
195  }
196 
197  if ($this->getWebLinkRepo()->doesListExist()) {
198  $writer->xmlStartTag('ListSettings');
199  $writer->xmlElement('ListTitle', [], $this->getTitle());
200  $writer->xmlElement('ListDescription', [], $this->getDescription());
201  $writer->xmlEndTag('ListSettings');
202  }
203 
204  // All items
205  $items = $this->getWebLinkRepo()->getAllItemsAsContainer()
206  ->sort()
207  ->getItems();
208 
209  $position = 0;
210  foreach ($items as $item) {
211  ++$position;
212  $item->toXML($writer, $position);
213  }
214 
215  $writer->xmlEndTag('WebLinks');
216  }
217 }
string $title
createAllItemsInDraftContainer(ilWebLinkDraftItemsContainer $container)
toXML(ilXmlWriter $writer)
const IL_INST_ID
Definition: constants.php:40
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...
$target_id
Definition: goto.php:52
appendXML(string $a_str)
append xml string to document
$container
Definition: wac.php:14
xmlEndTag(string $tag)
Writes an endtag.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
cloneObject(int $target_id, int $copy_id=0, bool $omit_tree=false)
cloneMetaData(ilObject $target_obj)
Copy meta data.
doMDUpdateListener(string $a_element)
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...
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
ilWebLinkDatabaseRepository $repo
Class ilObjLinkResource.
static _getFirstLink(int $a_webr_id)
Get first link item Check before with _isSingular() if there is more or less than one...
__construct(int $a_id=0, bool $a_call_by_reference=true)