ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilDclTableView.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
33  protected ?int $id;
41  protected int $table_id = 0;
49  protected string $title = "";
56  protected array $roles = [];
63  protected bool $role_limitation = false;
70  protected string $description = '';
77  protected int $tableview_order = 0;
81  protected array $visible_fields_cache = [];
82 
87  public static function returnDbTableName(): string
88  {
89  return "il_dcl_tableview";
90  }
91 
92  public function getId(): ?int
93  {
94  return $this->id;
95  }
96 
97  public function setId(int $id): void
98  {
99  $this->id = $id;
100  }
101 
102  public function getTableId(): int
103  {
104  return $this->table_id;
105  }
106 
107  public function setTableId(int $table_id): void
108  {
109  $this->table_id = $table_id;
110  }
111 
112  public function getTitle(): string
113  {
114  return $this->title;
115  }
116 
117  public function setTitle(string $title): void
118  {
119  $this->title = $title;
120  }
121 
122  public function getOrder(): int
123  {
124  return $this->tableview_order;
125  }
126 
127  public function setOrder(int $order): void
128  {
129  $this->tableview_order = $order;
130  }
131 
132  public function getDescription(): string
133  {
134  return $this->description;
135  }
136 
137  public function setDescription(string $description): void
138  {
139  $this->description = $description;
140  }
141 
142  public function getTableviewOrder(): int
143  {
144  return $this->tableview_order;
145  }
146 
147  public function setTableviewOrder(int $tableview_order): void
148  {
149  $this->tableview_order = $tableview_order;
150  }
151 
152  public function getRoles(): array
153  {
154  return $this->roles;
155  }
156 
157  public function setRoles(array $roles): void
158  {
159  $this->roles = $roles;
160  }
161 
162  public function getRoleLimitation(): bool
163  {
164  return $this->role_limitation;
165  }
166 
167  public function setRoleLimitation(bool $role_limitation): void
168  {
169  $this->role_limitation = $role_limitation;
170  }
171 
175  public function sleep($field_name): ?string
176  {
177  if ($field_name == 'roles') {
178  return json_encode($this->roles);
179  }
180 
181  return null;
182  }
183 
188  public function wakeUp($field_name, $field_value): ?array
189  {
190  if ($field_name == 'roles') {
191  return json_decode($field_value);
192  }
193 
194  return null;
195  }
196 
197  public function delete(): void
198  {
199  //Delete settings
200  foreach ($this->getFieldSettings() as $setting) {
201  $setting->delete();
202  }
203  parent::delete();
204  }
205 
206  public function getTable(): ilDclTable
207  {
208  return ilDclCache::getTableCache($this->table_id);
209  }
210 
214  public static function findOrGetInstance($primary_key, array $add_constructor_args = []): ActiveRecord
215  {
216  return parent::findOrGetInstance($primary_key, $add_constructor_args);
217  }
218 
224  public function getFilterableFieldSettings(): array
225  {
227  [
228  "tableview_id" => $this->id,
229  'in_filter' => 1,
230  'il_dcl_tfield_set.table_id' => $this->getTableId(),
231  ]
232  )->innerjoin('il_dcl_tfield_set', 'field', 'field', [])
233  ->orderBy('il_dcl_tfield_set.field_order')
234  ->get();
235  }
236 
241  public function getVisibleFields(): array
242  {
243  if (!$this->visible_fields_cache) {
245  [
246  "tableview_id" => $this->id,
247  'visible' => true,
248  'il_dcl_tfield_set.table_id' => $this->getTableId(),
249  ]
250  )->innerjoin(
251  'il_dcl_tfield_set',
252  'field',
253  'field',
254  []
255  )->orderBy('il_dcl_tfield_set.field_order')->get();
256  $fields = [];
257  foreach ($visible as $field_rec) {
258  $fields[] = $field_rec->getFieldObject();
259  }
260  $this->visible_fields_cache = $fields;
261  }
262 
264  }
265 
270  public function getFieldSettings(): array
271  {
273  [
274  'tableview_id' => $this->getId(),
275  'il_dcl_tfield_set.table_id' => $this->getTableId(),
276  ]
277  )->innerjoin('il_dcl_tfield_set', 'field', 'field', [])->orderBy('il_dcl_tfield_set.field_order')->get();
278 
279  $result = [];
280  foreach ($settings as $setting) {
281  $datatype = $setting->getFieldObject()->getDatatypeId();
282  if ($datatype === null || in_array($datatype, array_keys(ilDclDatatype::getAllDatatype()))) {
283  $result[] = $setting;
284  }
285  }
286 
287  return $result;
288  }
289 
294  public function getFieldSetting($field_id): ActiveRecord
295  {
297  'tableview_id' => $this->getId(),
298  'field' => $field_id
299  ])->first();
300  }
301 
302  public function create(bool $create_default_settings = true): void
303  {
304  parent::create();
305  if ($create_default_settings) {
306  $this->createDefaultSettings();
307  }
308  }
309 
313  public function createDefaultSettings(): void
314  {
315  $table = ilDclCache::getTableCache($this->table_id);
316 
317  foreach ($table->getFieldIds() as $field_id) {
318  $this->createFieldSetting($field_id);
319  }
320 
321  //ilDclTable->getFieldIds won't reuturn comments if they are disabled,
322  //still we have to create a fieldsetting for this field
323  if (!$table->getPublicCommentsEnabled()) {
324  $this->createFieldSetting('comments');
325  }
326  }
327 
332  public function createFieldSetting($field_id): void
333  {
335  [
336  'tableview_id' => $this->id,
337  'field' => $field_id,
338  ]
339  )->get()
340  ) {
341  $field_set = new ilDclTableViewFieldSetting();
342  $field_set->setTableviewId($this->id);
343  $field_set->setField($field_id);
344  $field_set->setVisible(!ilDclStandardField::_isStandardField($field_id));
345  $field_set->setFilterChangeable(true);
346  $field_set->setLockedCreate(false);
347  $field_set->setLockedEdit(false);
348  $field_set->setRequiredCreate(false);
349  $field_set->setRequiredEdit(false);
350  $field_set->setVisibleCreate(true);
351  $field_set->setVisibleEdit(true);
352  $field_set->create();
353  }
354  }
355 
360  public function cloneStructure(ilDclTableView $orig, array $new_fields): void
361  {
362  global $DIC;
363  //clone structure
364  $this->setTitle($orig->getTitle() . ' ' . $DIC->language()->txt('copy_of_suffix'));
365  $this->setOrder($orig->getOrder());
366  $this->setDescription($orig->getDescription());
367  $this->setRoles($orig->getRoles());
368  $this->setRoleLimitation($orig->getRoleLimitation());
369  $this->create(false); //create default setting, adjust them later
370 
371  //clone default values
373 
374  //clone fieldsettings
375  foreach ($orig->getFieldSettings() as $orig_fieldsetting) {
376  $new_fieldsetting = new ilDclTableViewFieldSetting();
377  $new_fieldsetting->setTableviewId($this->getId());
378  if ($new_fields[$orig_fieldsetting->getField()] ?? null) {
379  //normal fields
380  $new_fieldsetting->setField($new_fields[$orig_fieldsetting->getField()]->getId());
381  } else {
382  //standard fields
383  $new_fieldsetting->setField($orig_fieldsetting->getField());
384  }
385  $new_field_id = $new_fieldsetting->cloneStructure($orig_fieldsetting);
386 
387  //clone default value
388  $datatype = $orig_fieldsetting->getFieldObject()->getDatatypeId();
389  $match = ilDclTableViewBaseDefaultValue::findSingle($datatype, $orig_fieldsetting->getId());
390 
391  if (!is_null($match)) {
392  $new_default_value = $f->create($datatype);
393  $new_default_value->setTviewSetId($new_field_id);
394  $new_default_value->setValue($match->getValue());
395  $new_default_value->create();
396  }
397  }
398  $this->createFieldSetting('comments');
399 
400  //clone pageobject
402  $orig_pageobject = new ilDclDetailedViewDefinition($orig->getId());
403  $orig_pageobject->copy($this->getId());
404  }
405 
406  // mandatory for all cloning functions
408  }
409 
413  public static function getAllForTableId(int $table_id): array
414  {
415  return self::where(['table_id' => $table_id])->orderBy('title')->get();
416  }
417 
418  public static function getCountForTableId(int $table_id): int
419  {
420  return self::where(['table_id' => $table_id])->orderBy('tableview_order')->count();
421  }
422 
427  public static function createOrGetStandardView(int $table_id): ActiveRecord
428  {
429  if ($standardview = self::where(['table_id' => $table_id])->orderBy('tableview_order')->first()) {
430  return $standardview;
431  }
432 
433  global $DIC;
434  $rbacreview = $DIC['rbacreview'];
435  $http = $DIC->http();
436  $refinery = $DIC->refinery();
437 
438  $roles = [];
439 
440  $ref_id = $http->wrapper()->query()->retrieve('ref_id', $refinery->kindlyTo()->int());
441  foreach ($rbacreview->getParentRoleIds($ref_id) as $role_array) {
442  $roles[] = $role_array['obj_id'];
443  }
444 
445  $view = new self();
446 
447  $hasRefId = $http->wrapper()->query()->has('ref_id');
448 
449  if ($hasRefId) {
450  global $DIC;
451  $rbacreview = $DIC['rbacreview'];
452 
453  $ref_id = $http->wrapper()->query()->retrieve('ref_id', $refinery->kindlyTo()->int());
454 
455  $roles = [];
456  foreach ($rbacreview->getParentRoleIds($ref_id) as $role_array) {
457  $roles[] = $role_array['obj_id'];
458  }
459  $view->setRoles(array_merge($roles, $rbacreview->getLocalRoles($ref_id)));
460  }
461  $view->setTableId($table_id);
462  // bugfix mantis 0023307
463  $lng = $DIC['lng'];
464  $view->setTitle($lng->txt('dcl_title_standardview'));
465  $view->setTableviewOrder(10);
466  $view->create();
467 
468  return $view;
469  }
470 }
static createOrGetStandardView(int $table_id)
cloneStructure(ilDclTableView $orig, array $new_fields)
createFieldSetting($field_id)
create ilDclTableViewFieldSetting for this tableview and the given field id
wakeUp($field_name, $field_value)
static findOrGetInstance($primary_key, array $add_constructor_args=[])
setTitle(string $title)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static orderBy(string $orderBy, string $orderDirection='ASC')
getFilterableFieldSettings()
getFilterableFields Returns all fieldsetting-objects of this tableview which have set their filterabl...
const TYPE_TABLEVIEW
$http
Definition: deliver.php:30
static where($where, $operator=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setDescription(string $description)
$ref_id
Definition: ltiauth.php:65
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
createDefaultSettings()
create default ilDclTableViewFieldSetting entries
global $DIC
Definition: shib_login.php:26
static getCountForTableId(int $table_id)
static getTableCache(?int $table_id=null)
static setCloneOf(int $old, int $new, string $type)
static _isStandardField($field_id)
setRoles(array $roles)
create(bool $create_default_settings=true)
global $lng
Definition: privfeed.php:31
setRoleLimitation(bool $role_limitation)
copy(int $a_id, string $a_parent_type="", int $a_new_parent_id=0, bool $a_clone_mobs=false, int $obj_copy_id=0, bool $overwrite_existing=true)
Copy page.
getVisibleFields()
Returns all field-objects of this tableview which have set their visibility to true, including standard fields.
static getAllForTableId(int $table_id)
setTableId(int $table_id)
setTableviewOrder(int $tableview_order)