ILIAS  trunk Revision v11.0_alpha-2645-g16283d3b3f8
class.ilADTTextSearchBridgeSingle.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  public const SQL_STRICT = 1;
24  public const SQL_LIKE = 2;
25  public const SQL_LIKE_END = 3;
26  public const SQL_LIKE_START = 4;
27 
28  protected function isValidADTDefinition(ilADTDefinition $a_adt_def): bool
29  {
30  return ($a_adt_def instanceof ilADTTextDefinition);
31  }
32 
33  // table2gui / filter
34 
35  public function loadFilter(): void
36  {
37  $value = $this->readFilter();
38  if ($value !== null) {
39  $this->getADT()->setText($value);
40  }
41  }
42 
43  // form
44 
45  public function addToForm(): void
46  {
47  $this->addTextInputToForm(true);
48  }
49 
50  public function addToFilterForm(): void
51  {
52  $this->addTextInputToForm(false);
53  }
54 
55  protected function addTextInputToForm(bool $submit_on_enter): void
56  {
57  $text = new ilTextInputGUI($this->getTitle(), $this->getElementId());
58  $text->setSize(20);
59  $text->setMaxLength(512);
60  $text->setSubmitFormOnEnter($submit_on_enter);
61 
62  $text->setValue($this->getADT()->getText());
63 
64  $this->addToParentElement($text);
65  }
66 
67  public function importFromPost(?array $a_post = null): bool
68  {
69  $post = $this->extractPostValues($a_post);
70 
71  if ($post && $this->shouldBeImportedFromPost($post)) {
72  if ($this->getForm() instanceof ilPropertyFormGUI) {
73  $item = $this->getForm()->getItemByPostVar($this->getElementId());
74  $item->setValue($post);
75  } elseif (array_key_exists($this->getElementId(), $this->table_filter_fields)) {
76  $this->table_filter_fields[$this->getElementId()]->setValue($post);
77  $this->writeFilter($post);
78  }
79 
80  $this->getADT()->setText($post);
81  } else {
82  $this->writeFilter();
83  $this->getADT()->setText();
84  }
85  return true;
86  }
87 
88  // db
89 
90  public function getSQLCondition(string $a_element_id, int $mode = self::SQL_LIKE, array $quotedWords = []): string
91  {
92  if (!$quotedWords) {
93  if ($this->isNull() || !$this->isValid()) {
94  return '';
95  }
96  $quotedWords = $this->getADT()->getText();
97  }
98 
99  switch ($mode) {
100  case self::SQL_STRICT:
101  if (!is_array($quotedWords)) {
102  return $a_element_id . " = " . $this->db->quote($quotedWords, "text");
103  } else {
104  return $this->db->in($a_element_id, $quotedWords, false, "text");
105  }
106 
107  // no break
108  case self::SQL_LIKE:
109  if (!is_array($quotedWords)) {
110  return $this->db->like($a_element_id, "text", "%" . $quotedWords . "%");
111  } else {
112  $tmp = array();
113  foreach ($quotedWords as $word) {
114  if ($word) {
115  $tmp[] = $this->db->like($a_element_id, "text", "%" . $word . "%");
116  }
117  }
118  if (count($tmp)) {
119  return "(" . implode(" OR ", $tmp) . ")";
120  }
121  }
122  break;
123 
124  case self::SQL_LIKE_END:
125  if (!is_array($quotedWords)) {
126  return $this->db->like($a_element_id, "text", $quotedWords . "%");
127  }
128  break;
129 
130  case self::SQL_LIKE_START:
131  if (!is_array($quotedWords)) {
132  return $this->db->like($a_element_id, "text", "%" . $quotedWords);
133  }
134  break;
135  }
136  return '';
137  }
138 
139  public function isInCondition(ilADT $a_adt): bool
140  {
141  if ($this->getADT()->getCopyOfDefinition()->isComparableTo($a_adt)) {
142  $search_term = strtolower(trim((string) $this->getADT()->getText()));
143  $text = strtolower(trim((string) $a_adt->getText()));
144  return str_contains($text, $search_term);
145  }
146  return false;
147  }
148 
149  // import/export
150 
151  public function getSerializedValue(): string
152  {
153  if (!$this->isNull() && $this->isValid()) {
154  return serialize(array($this->getADT()->getText()));
155  }
156  return '';
157  }
158 
159  public function setSerializedValue(string $a_value): void
160  {
161  $a_value = unserialize($a_value);
162  if (is_array($a_value)) {
163  $this->getADT()->setText($a_value[0]);
164  }
165  }
166 }
getSQLCondition(string $a_element_id, int $mode=self::SQL_LIKE, array $quotedWords=[])
isValidADTDefinition(ilADTDefinition $a_adt_def)
ADT base class.
Definition: class.ilADT.php:25
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
shouldBeImportedFromPost($a_post)
Check if incoming values should be imported at all.
readFilter()
Load value(s) from filter store (in session)
Class ilADTSearchBridgeSingle.
extractPostValues(?array $a_post=null)
Extract data from (post) values.
writeFilter($a_value=null)
Write value(s) to filter store (in session)
$post
Definition: ltitoken.php:46
ADT definition base class.
addToParentElement(ilFormPropertyGUI $a_field)
Add form field to parent element.