ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDclTextFieldModel.php
Go to the documentation of this file.
1 <?php
2 require_once("./Modules/DataCollection/classes/Fields/Base/class.ilDclBaseFieldModel.php");
3 require_once ('./Modules/DataCollection/classes/Fields/Text/class.ilDclTextRecordQueryObject.php');
4 require_once("./Modules/DataCollection/classes/Helpers/class.ilDclRecordQueryObject.php");
5 
13 
17  public function getRecordQueryFilterObject($filter_value = "", ilDclBaseFieldModel $sort_field = null) {
18  global $DIC;
19  $ilDB = $DIC['ilDB'];
20 
21  $join_str = "INNER JOIN il_dcl_record_field AS filter_record_field_{$this->getId()} ON (filter_record_field_{$this->getId()}.record_id = record.id AND filter_record_field_{$this->getId()}.field_id = "
22  . $ilDB->quote($this->getId(), 'integer') . ") ";
23  $join_str .= "INNER JOIN il_dcl_stloc{$this->getStorageLocation()}_value AS filter_stloc_{$this->getId()} ON (filter_stloc_{$this->getId()}.record_field_id = filter_record_field_{$this->getId()}.id AND filter_stloc_{$this->getId()}.value LIKE "
24  . $ilDB->quote("%$filter_value%", 'text') . ") ";
25 
26  $sql_obj = new ilDclRecordQueryObject();
27  $sql_obj->setJoinStatement($join_str);
28 
29  return $sql_obj;
30  }
31 
35  public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false) {
36  // use custom record sorting for url-fields
38  return new ilDclTextRecordQueryObject();
39  } else {
40  return parent::getRecordQuerySortObject($direction, $sort_by_status);
41  }
42  }
43 
44 
49  public function checkValidityFromForm(ilPropertyFormGUI &$form, $record_id = NULL) {
50  $has_url_property = $this->getProperty(ilDclBaseFieldModel::PROP_URL);
51  if ($has_url_property) {
52  $values = array(
53  'link' => $form->getInput("field_" . $this->getId()),
54  'title' => $form->getInput("field_" . $this->getId() . "_title")
55  );
56  $this->checkValidityOfURLField($values, $record_id);
57  } else {
58  parent::checkValidityFromForm($form, $record_id);
59  }
60  }
61 
62 
66  public function checkValidity($value, $record_id = NULL) {
67  $has_url_property = $this->getProperty(ilDclBaseFieldModel::PROP_URL);
68  if ($has_url_property) {
69  return $this->checkValidityOfURLField($value, $record_id);
70  }
71 
72  //Don't check empty values
73  if ($value == NULL) {
74  return true;
75  }
76 
77  $this->checkRegexAndLength($value);
78 
79  if ($this->isUnique()) {
80  $table = ilDclCache::getTableCache($this->getTableId());
81  foreach ($table->getRecords() as $record) {
82  //for text it has to be case insensitive.
83  $record_value = $record->getRecordFieldValue($this->getId());
84 
85  if (strtolower($this->normalizeValue($record_value)) == strtolower($this->normalizeValue(nl2br($value)))
86  && ($record->getId() != $record_id
87  || $record_id == 0)
88  ) {
90  }
91  }
92  }
93  }
94 
95 
103  protected function checkValidityOfURLField($value, $record_id) {
104  // TODO: value should always be an array with url fields, can we remove the check & json_decode?
105  if (!is_array($value)) {
106  $value = array( 'link' => $value, 'title' => '');
107  }
108 
109  //Don't check empty values
110  if (!$value['link']) {
111  return true;
112  }
113 
114  $this->checkRegexAndLength($value['link']);
115 
116  //check url/email
117  $link = (substr($value['link'], 0, 3) === 'www') ? 'http://' . $value['link'] : $value['link'];
118  if (! filter_var($link, FILTER_VALIDATE_URL) && ! filter_var($link, FILTER_VALIDATE_EMAIL) && $link != '') {
120  }
121 
122  if ($this->isUnique()) {
123  $table = ilDclCache::getTableCache($this->getTableId());
124  foreach ($table->getRecords() as $record) {
125  $record_value = $record->getRecordFieldValue($this->getId());
126 
127  if ($record_value == $value
128  && ($record->getId() != $record_id
129  || $record_id == 0)
130  ) {
132  }
133  }
134  }
135  }
136 
141  global $DIC;
142  $lng = $DIC['lng'];
143 
144  $return = true;
145  // Additional check for text fields: The length property should be max 200 if the textarea option is not set
146  if ((int)$form->getInput('prop_' . ilDclBaseFieldModel::PROP_LENGTH) > 200 && !$form->getInput('prop_' . ilDclBaseFieldModel::PROP_TEXTAREA)) {
147  $inputObj = $form->getItemByPostVar('prop_' . ilDclBaseFieldModel::PROP_LENGTH);
148  $inputObj->setAlert($lng->txt("form_msg_value_too_high"));
149  $return = false;
150  }
151 
152  return $return;
153  }
154 
155 
159  public function getValidFieldProperties() {
161  }
162 
163 
169  protected function checkRegexAndLength($value) {
171  if (substr($regex, 0, 1) != "/") {
172  $regex = "/" . $regex;
173  }
174  if (substr($regex, - 1) != "/") {
175  $regex .= "/";
176  }
177 
178  if ($this->getProperty(ilDclBaseFieldModel::PROP_LENGTH) < $this->strlen($value, 'UTF-8')
179  && is_numeric($this->getProperty(ilDclBaseFieldModel::PROP_LENGTH))
180  ) {
182  }
183 
184  if ($this->getProperty(ilDclBaseFieldModel::PROP_REGEX) != NULL) {
185  try {
186  $preg_match = preg_match($regex, $value);
187  } catch (ErrorException $e) {
189  }
190 
191  if ($preg_match == false) {
193  }
194  }
195  }
196 
197 
203  public function strlen($value, $encoding = 'UTF-8') {
204  switch (true) {
205  case function_exists('mb_strlen'):
206  return mb_strlen($value, $encoding);
207  case function_exists('iconv_strlen'):
208  return iconv_strlen($value, $encoding);
209  default:
210  return strlen($value);
211  }
212  }
213 
214  public function fillHeaderExcel(ilExcel $worksheet, &$row, &$col) {
215  parent::fillHeaderExcel($worksheet, $row, $col);
217  $worksheet->setCell($row, $col, $this->getTitle() . '_title');
218  $col++;
219  }
220  }
221 
226  public function checkTitlesForImport(array &$titles, array &$import_fields) {
227  foreach ($titles as $k => $title) {
228  if (!ilStr::isUtf8($title)) {
229  $title = utf8_encode($title);
230  }
231  if ($title == $this->getTitle()) {
232  $import_fields[$k] = $this;
233  if ($this->hasProperty(ilDclBaseFieldModel::PROP_URL) && $titles[$k+1] == $this->getTitle().'_title') {
234  unset($titles[$k+1]);
235  }
236  }
237  }
238 
239  }
240 }
Class ilDclBaseFieldModel.
fillHeaderExcel(ilExcel $worksheet, &$row, &$col)
$worksheet
getItemByPostVar($a_post_var)
Get Item by POST variable.
This class represents a property form user interface.
checkTitlesForImport(array &$titles, array &$import_fields)
Class ilDclRecordQueryObject.
Class ilDclBaseFieldModel.
checkValidity($value, $record_id=NULL)
checkFieldCreationInput(ilPropertyFormGUI $form)
static getTableCache($table_id=0)
hasProperty($key)
Checks if a certain property for a field is set.
const PROP_LENGTH
General properties.
getRecordQueryFilterObject($filter_value="", ilDclBaseFieldModel $sort_field=null)
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
Create styles array
The data for the language used.
setCell($a_row, $a_col, $a_value)
Set cell value.
getProperty($key)
Returns a certain property of a field.
checkValidityOfURLField($value, $record_id)
global $lng
Definition: privfeed.php:17
strlen($value, $encoding='UTF-8')
global $ilDB
Class ilDclTextRecordQueryObject.
global $DIC
checkValidityFromForm(ilPropertyFormGUI &$form, $record_id=NULL)
getRecordQuerySortObject($direction="asc", $sort_by_status=false)
Class ilDclTextFieldModel.
static isUtf8($a_str)
Check whether string is utf-8.