ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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->setExportable($original_record->getExportable());
59 
60  $this->doUpdate();
61  }
62 
63  public function getLocked(): bool
64  {
65  return true;
66  }
67 
68  public static function _getStandardFieldsAsArray(): array
69  {
70 
71  //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.
72  global $DIC;
73  $lng = $DIC->language();
74  return [
75  "id" => [
76  "id" => "id",
77  "title" => $lng->txt("dcl_id"),
78  "description" => $lng->txt("dcl_id_description"),
79  "datatype_id" => ilDclDatatype::INPUTFORMAT_NUMBER
80  ],
81  "create_date" => [
82  "id" => "create_date",
83  "title" => $lng->txt("dcl_creation_date"),
84  "description" => $lng->txt("dcl_creation_date_description"),
86  ],
87  "last_update" => [
88  "id" => "last_update",
89  "title" => $lng->txt("dcl_last_update"),
90  "description" => $lng->txt("dcl_last_update_description"),
92  ],
93  "owner" => [
94  "id" => "owner",
95  "title" => $lng->txt("dcl_owner"),
96  "description" => $lng->txt("dcl_owner_description"),
97  "datatype_id" => ilDclDatatype::INPUTFORMAT_TEXT
98  ],
99  "last_edit_by" => [
100  "id" => "last_edit_by",
101  "title" => $lng->txt("dcl_last_edited_by"),
102  "description" => $lng->txt("dcl_last_edited_by_description"),
103  "datatype_id" => ilDclDatatype::INPUTFORMAT_TEXT
104  ],
105  'comments' => [
106  'id' => 'comments',
107  'title' => $lng->txt('dcl_comments'),
108  'description' => $lng->txt('dcl_comments_desc'),
109  'datatype_id' => ilDclDatatype::INPUTFORMAT_TEXT
110  ],
111  ];
112  }
113 
114  public static function _getStandardFields(int $table_id): array
115  {
116  $stdFields = [];
117  foreach (self::_getStandardFieldsAsArray() as $array) {
118  $array["table_id"] = $table_id;
119  $field = new ilDclStandardField();
120  $field->buildFromDBRecord($array);
121  $stdFields[] = $field;
122  }
123 
124  return $stdFields;
125  }
126 
131  public static function _getNonImportableStandardFieldTitles(): array
132  {
133  global $DIC;
134  $ilDB = $DIC['ilDB'];
135  $identifiers = '';
136  foreach (
137  [
138  'dcl_id',
139  'dcl_creation_date',
140  'dcl_last_update',
141  'dcl_last_edited_by',
142  'dcl_comments',
143  ] as $id
144  ) {
145  $identifiers .= $ilDB->quote($id, 'text') . ',';
146  }
147  $identifiers = rtrim($identifiers, ',');
148  $sql = $ilDB->query(
149  'SELECT value FROM lng_data WHERE identifier IN (' . $identifiers
150  . ')'
151  );
152  $titles = [];
153  while ($rec = $ilDB->fetchAssoc($sql)) {
154  $titles[] = $rec['value'];
155  }
156 
157  return $titles;
158  }
159 
164  public static function _getImportableStandardFieldTitle(): array
165  {
166  global $DIC;
167  $ilDB = $DIC['ilDB'];
168  $identifiers = '';
169  $id = 'dcl_owner';
170  $identifiers .= $ilDB->quote($id, 'text') . ',';
171  $identifiers = rtrim($identifiers, ',');
172  $sql = $ilDB->query(
173  'SELECT value, identifier FROM lng_data WHERE identifier IN ('
174  . $identifiers . ')'
175  );
176  $titles = [];
177  while ($rec = $ilDB->fetchAssoc($sql)) {
178  $titles[$rec['identifier']][] = $rec['value'];
179  }
180 
181  return $titles;
182  }
183 
188  public static function _isStandardField($field_id): bool
189  {
190  $return = false;
191  foreach (self::_getStandardFieldsAsArray() as $field) {
192  if ($field["id"] == $field_id) {
193  $return = true;
194  }
195  }
196 
197  return $return;
198  }
199 
204  public static function _getDatatypeForId(string $id): ?int
205  {
206  return self::_getStandardFieldsAsArray()[$id]['datatype_id'];
207  }
208 
209  public function isStandardField(): bool
210  {
211  return true;
212  }
213 
217  public function getRecordQuerySortObject(
218  string $direction = "asc",
219  bool $sort_by_status = false
221  $sql_obj = new ilDclRecordQueryObject();
222 
223  $join_str = "";
224  if ($this->getId() == 'owner' || $this->getId() == 'last_edit_by') {
225  $join_str = "LEFT JOIN usr_data AS sort_usr_data_{$this->getId()} ON (sort_usr_data_{$this->getId()}.usr_id = record.{$this->getId()})";
226  $select_str = " sort_usr_data_{$this->getId()}.login AS field_{$this->getId()},";
227  } else {
228  $select_str = " record.{$this->getId()} AS field_{$this->getId()},";
229  }
230 
231  $sql_obj->setSelectStatement($select_str);
232  $sql_obj->setJoinStatement($join_str);
233 
234  if ($this->getId() !== "comments") {
235  $sql_obj->setOrderStatement("field_{$this->getId()} " . $direction);
236  }
237 
238  return $sql_obj;
239  }
240 
244  public function getRecordQueryFilterObject(
245  $filter_value = "",
246  ?ilDclBaseFieldModel $sort_field = null
248  global $DIC;
249  $ilDB = $DIC['ilDB'];
250 
251  $where_additions = "";
252  $join_str = "";
254  $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 "
255  . $ilDB->quote("%$filter_value%", 'text') . ") ";
256  } else {
258  $from = (isset($filter_value['from'])) ? $filter_value['from'] : null;
259  $to = (isset($filter_value['to'])) ? $filter_value['to'] : null;
260  if (is_numeric($from)) {
261  $where_additions .= " AND record.{$this->getId()} >= "
262  . $ilDB->quote($from, 'integer');
263  }
264  if (is_numeric($to)) {
265  $where_additions .= " AND record.{$this->getId()} <= "
266  . $ilDB->quote($to, 'integer');
267  }
268  } else {
270  $date_from = (isset($filter_value['from'])
271  && is_object($filter_value['from'])) ? $filter_value['from'] : null;
272  $date_to = (isset($filter_value['to'])
273  && is_object($filter_value['to'])) ? $filter_value['to'] : null;
274 
275  // db->quote(.. date) at some point invokes ilDate->_toString, which adds a <br /> to the string,
276  // that's why strip_tags is used
277  if ($date_from) {
278  $where_additions .= " AND (record.{$this->getId()} >= "
279  . strip_tags($ilDB->quote($date_from, 'date')) . ")";
280  }
281  if ($date_to) {
282  $where_additions .= " AND (record.{$this->getId()} <= "
283  . strip_tags($ilDB->quote($date_to, 'date')) . ")";
284  }
285  }
286  }
287  }
288 
289  $sql_obj = new ilDclRecordQueryObject();
290  $sql_obj->setJoinStatement($join_str);
291  $sql_obj->setWhereStatement($where_additions);
292 
293  return $sql_obj;
294  }
295 
296  public function getSortField(): string
297  {
298  if ($this->getId() == 'comments') {
299  return 'n_comments';
300  } else {
301  return $this->getTitle();
302  }
303  }
304 
305  public function hasNumericSorting(): bool
306  {
307  if ($this->getId() == 'comments') {
308  return true;
309  }
310 
311  return parent::hasNumericSorting();
312  }
313 
314  public function allowFilterInListView(): bool
315  {
316  //comments are filterable if they are enabled in the tables settings
317  return $this->id != 'comments'
318  || ilDclCache::getTableCache($this->getTableId())->getPublicCommentsEnabled();
319  }
320 
321  public function fillHeaderExcel(ilExcel $worksheet, int &$row, int &$col): void
322  {
323  parent::fillHeaderExcel($worksheet, $row, $col);
324  if ($this->getId() == 'owner') {
325  global $DIC;
326  $lng = $DIC['lng'];
327  $worksheet->setCell($row, $col, $lng->txt("dcl_owner_name"));
328  $col++;
329  }
330  }
331 
335  public function getValueFromExcel(ilExcel $excel, int $row, int $col)
336  {
337  $value = $excel->getCell($row, $col);
338  switch ($this->id) {
339  case 'owner':
340  return ilObjUser::_lookupId($value);
341  default:
342  return $value;
343  }
344  }
345 
349  public function afterClone($records)
350  {
351  }
352 }
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)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
getValueFromExcel(ilExcel $excel, int $row, int $col)
global $DIC
Definition: shib_login.php:26
ilGlobalTemplateInterface $main_tpl
static getTableCache(?int $table_id=null)
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)
$message
Definition: xapiexit.php:31
getDatatypeId()
Get datatype_id.