ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDclStandardField.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
24 
25  public function __construct($a_id = 0)
26  {
27  parent::__construct($a_id);
28  global $DIC;
29  $this->main_tpl = $DIC->ui()->mainTemplate();
30  }
31 
32  public function doRead(): void
33  {
34  global $DIC;
35  $ilLog = $DIC['ilLog'];
36  $message = "Standard fields cannot be read from DB";
37  $this->main_tpl->setOnScreenMessage('failure', $message);
38  $ilLog->write("[ilDclStandardField] " . $message);
39  }
40 
41  public function doCreate(): void
42  {
43  global $DIC;
44  $ilLog = $DIC['ilLog'];
45  $message = "Standard fields cannot be written to DB";
46  $this->main_tpl->setOnScreenMessage('failure', $message);
47  $ilLog->write("[ilDclStandardField] " . $message);
48  }
49 
50  public function doUpdate(): void
51  {
52  $this->updateTableFieldSetting();
53  }
54 
55  public function clone(ilDclStandardField $original_record): void
56  {
57  $this->setOrder($original_record->getOrder());
58  $this->setUnique($original_record->isUnique());
59  $this->setExportable($original_record->getExportable());
60 
61  $this->doUpdate();
62  }
63 
64  public function getLocked(): bool
65  {
66  return true;
67  }
68 
69  public static function _getStandardFieldsAsArray(): array
70  {
71 
72  //TODO: this isn't particularly pretty especially as $lng is used in the model. On the long run the standard fields should be refactored into "normal" fields.
73  global $DIC;
74  $lng = $DIC->language();
75  return [
76  "id" => [
77  "id" => "id",
78  "title" => $lng->txt("dcl_id"),
79  "description" => $lng->txt("dcl_id_description"),
80  "datatype_id" => ilDclDatatype::INPUTFORMAT_NUMBER
81  ],
82  "create_date" => [
83  "id" => "create_date",
84  "title" => $lng->txt("dcl_creation_date"),
85  "description" => $lng->txt("dcl_creation_date_description"),
86  "datatype_id" => ilDclDatatype::INPUTFORMAT_DATE
87  ],
88  "last_update" => [
89  "id" => "last_update",
90  "title" => $lng->txt("dcl_last_update"),
91  "description" => $lng->txt("dcl_last_update_description"),
92  "datatype_id" => ilDclDatatype::INPUTFORMAT_DATE
93  ],
94  "owner" => [
95  "id" => "owner",
96  "title" => $lng->txt("dcl_owner"),
97  "description" => $lng->txt("dcl_owner_description"),
98  "datatype_id" => ilDclDatatype::INPUTFORMAT_TEXT
99  ],
100  "last_edit_by" => [
101  "id" => "last_edit_by",
102  "title" => $lng->txt("dcl_last_edited_by"),
103  "description" => $lng->txt("dcl_last_edited_by_description"),
104  "datatype_id" => ilDclDatatype::INPUTFORMAT_TEXT
105  ],
106  'comments' => [
107  'id' => 'comments',
108  'title' => $lng->txt('dcl_comments'),
109  'description' => $lng->txt('dcl_comments_desc'),
110  'datatype_id' => ilDclDatatype::INPUTFORMAT_TEXT
111  ],
112  ];
113  }
114 
115  public static function _getStandardFields(int $table_id): array
116  {
117  $stdFields = [];
118  foreach (self::_getStandardFieldsAsArray() as $array) {
119  $array["table_id"] = $table_id;
120  $field = new ilDclStandardField();
121  $field->buildFromDBRecord($array);
122  $stdFields[] = $field;
123  }
124 
125  return $stdFields;
126  }
127 
132  public static function _getNonImportableStandardFieldTitles(): array
133  {
134  global $DIC;
135  $ilDB = $DIC['ilDB'];
136  $identifiers = '';
137  foreach (
138  [
139  'dcl_id',
140  'dcl_creation_date',
141  'dcl_last_update',
142  'dcl_last_edited_by',
143  'dcl_comments',
144  ] as $id
145  ) {
146  $identifiers .= $ilDB->quote($id, 'text') . ',';
147  }
148  $identifiers = rtrim($identifiers, ',');
149  $sql = $ilDB->query(
150  'SELECT value FROM lng_data WHERE identifier IN (' . $identifiers
151  . ')'
152  );
153  $titles = [];
154  while ($rec = $ilDB->fetchAssoc($sql)) {
155  $titles[] = $rec['value'];
156  }
157 
158  return $titles;
159  }
160 
165  public static function _getImportableStandardFieldTitle(): array
166  {
167  global $DIC;
168  $ilDB = $DIC['ilDB'];
169  $identifiers = '';
170  $id = 'dcl_owner';
171  $identifiers .= $ilDB->quote($id, 'text') . ',';
172  $identifiers = rtrim($identifiers, ',');
173  $sql = $ilDB->query(
174  'SELECT value, identifier FROM lng_data WHERE identifier IN ('
175  . $identifiers . ')'
176  );
177  $titles = [];
178  while ($rec = $ilDB->fetchAssoc($sql)) {
179  $titles[$rec['identifier']][] = $rec['value'];
180  }
181 
182  return $titles;
183  }
184 
189  public static function _isStandardField($field_id): bool
190  {
191  $return = false;
192  foreach (self::_getStandardFieldsAsArray() as $field) {
193  if ($field["id"] == $field_id) {
194  $return = true;
195  }
196  }
197 
198  return $return;
199  }
200 
205  public static function _getDatatypeForId(string $id): ?int
206  {
207  return self::_getStandardFieldsAsArray()[$id]['datatype_id'];
208  }
209 
210  public function isStandardField(): bool
211  {
212  return true;
213  }
214 
215  public function isUnique(): bool
216  {
217  return false;
218  }
219 
223  public function getRecordQuerySortObject(
224  string $direction = "asc",
225  bool $sort_by_status = false
227  $sql_obj = new ilDclRecordQueryObject();
228 
229  $join_str = "";
230  if ($this->getId() == 'owner' || $this->getId() == 'last_edit_by') {
231  $join_str = "LEFT JOIN usr_data AS sort_usr_data_{$this->getId()} ON (sort_usr_data_{$this->getId()}.usr_id = record.{$this->getId()})";
232  $select_str = " sort_usr_data_{$this->getId()}.login AS field_{$this->getId()},";
233  } else {
234  $select_str = " record.{$this->getId()} AS field_{$this->getId()},";
235  }
236 
237  $sql_obj->setSelectStatement($select_str);
238  $sql_obj->setJoinStatement($join_str);
239 
240  if ($this->getId() !== "comments") {
241  $sql_obj->setOrderStatement("field_{$this->getId()} " . $direction);
242  }
243 
244  return $sql_obj;
245  }
246 
250  public function getRecordQueryFilterObject(
251  $filter_value = "",
252  ?ilDclBaseFieldModel $sort_field = null
254  global $DIC;
255  $ilDB = $DIC['ilDB'];
256 
257  $where_additions = "";
258  $join_str = "";
260  $join_str = "INNER JOIN usr_data AS filter_usr_data_{$this->getId()} ON (filter_usr_data_{$this->getId()}.usr_id = record.{$this->getId()} AND filter_usr_data_{$this->getId()}.login LIKE "
261  . $ilDB->quote("%$filter_value%", 'text') . ") ";
262  } else {
264  $from = (isset($filter_value['from'])) ? $filter_value['from'] : null;
265  $to = (isset($filter_value['to'])) ? $filter_value['to'] : null;
266  if (is_numeric($from)) {
267  $where_additions .= " AND record.{$this->getId()} >= "
268  . $ilDB->quote($from, 'integer');
269  }
270  if (is_numeric($to)) {
271  $where_additions .= " AND record.{$this->getId()} <= "
272  . $ilDB->quote($to, 'integer');
273  }
274  } else {
276  $date_from = (isset($filter_value['from'])
277  && is_object($filter_value['from'])) ? $filter_value['from'] : null;
278  $date_to = (isset($filter_value['to'])
279  && is_object($filter_value['to'])) ? $filter_value['to'] : null;
280 
281  // db->quote(.. date) at some point invokes ilDate->_toString, which adds a <br /> to the string,
282  // that's why strip_tags is used
283  if ($date_from) {
284  $where_additions .= " AND (record.{$this->getId()} >= "
285  . strip_tags($ilDB->quote($date_from, 'date')) . ")";
286  }
287  if ($date_to) {
288  $where_additions .= " AND (record.{$this->getId()} <= "
289  . strip_tags($ilDB->quote($date_to, 'date')) . ")";
290  }
291  }
292  }
293  }
294 
295  $sql_obj = new ilDclRecordQueryObject();
296  $sql_obj->setJoinStatement($join_str);
297  $sql_obj->setWhereStatement($where_additions);
298 
299  return $sql_obj;
300  }
301 
302  public function getSortField(): string
303  {
304  if ($this->getId() == 'comments') {
305  return 'n_comments';
306  } else {
307  return $this->getTitle();
308  }
309  }
310 
311  public function hasNumericSorting(): bool
312  {
313  if ($this->getId() == 'comments') {
314  return true;
315  }
316 
317  return parent::hasNumericSorting();
318  }
319 
320  public function allowFilterInListView(): bool
321  {
322  //comments are filterable if they are enabled in the tables settings
323  return $this->id != 'comments'
324  || ilDclCache::getTableCache($this->getTableId())->getPublicCommentsEnabled();
325  }
326 
327  public function fillHeaderExcel(ilExcel $worksheet, int &$row, int &$col): void
328  {
329  parent::fillHeaderExcel($worksheet, $row, $col);
330  if ($this->getId() == 'owner') {
331  global $DIC;
332  $lng = $DIC['lng'];
333  $worksheet->setCell($row, $col, $lng->txt("dcl_owner_name"));
334  $col++;
335  }
336  }
337 
341  public function getValueFromExcel(ilExcel $excel, int $row, int $col)
342  {
343  $value = $excel->getCell($row, $col);
344  switch ($this->id) {
345  case 'owner':
346  return ilObjUser::_lookupId($value);
347  default:
348  return $value;
349  }
350  }
351 
355  public function afterClone($records)
356  {
357  }
358 }
getRecordQueryFilterObject( $filter_value="", ?ilDclBaseFieldModel $sort_field=null)
Returns a query-object for building the record-loader-sql-query.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static _getStandardFields(int $table_id)
static _lookupId($a_user_str)
getRecordQuerySortObject(string $direction="asc", bool $sort_by_status=false)
Returns a query-object for building the record-loader-sql-query.
static _getDatatypeForId(string $id)
gives you the datatype id of a specified standard field.
clone(ilDclStandardField $original_record)
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
static getTableCache(int $table_id=null)
getValueFromExcel(ilExcel $excel, int $row, int $col)
global $DIC
Definition: shib_login.php:25
ilGlobalTemplateInterface $main_tpl
getCell(int $a_row, int $a_col)
Returns the value of a cell.
static _isStandardField($field_id)
updateTableFieldSetting()
update exportable and fieldorder
fillHeaderExcel(ilExcel $worksheet, int &$row, int &$col)
__construct(Container $dic, ilPlugin $plugin)
getDatatypeId()
Get datatype_id.