ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMDKeyword.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
26 class ilMDKeyword extends ilMDBase
27 {
28  private string $keyword = '';
30 
31  // SET/GET
32  public function setKeyword(string $a_keyword): void
33  {
34  $this->keyword = $a_keyword;
35  }
36 
37  public function getKeyword(): string
38  {
39  return $this->keyword;
40  }
41 
42  public function setKeywordLanguage(ilMDLanguageItem $lng_obj): void
43  {
44  $this->keyword_language = $lng_obj;
45  }
46 
48  {
49  return is_object($this->keyword_language) ? $this->keyword_language : null;
50  }
51 
52  public function getKeywordLanguageCode(): string
53  {
54  return is_object($this->keyword_language) ? $this->keyword_language->getLanguageCode() : '';
55  }
56 
57  public function save(): int
58  {
59  $fields = $this->__getFields();
60  $fields['meta_keyword_id'] = array('integer', $next_id = $this->db->nextId('il_meta_keyword'));
61 
62  if ($this->db->insert('il_meta_keyword', $fields)) {
63  $this->setMetaId($next_id);
64  return $this->getMetaId();
65  }
66  return 0;
67  }
68 
69  public function update(): bool
70  {
71  return $this->getMetaId() && $this->db->update(
72  'il_meta_keyword',
73  $this->__getFields(),
74  array("meta_keyword_id" => array('integer', $this->getMetaId()))
75  );
76  }
77 
78  public function delete(): bool
79  {
80  if ($this->getMetaId()) {
81  $query = "DELETE FROM il_meta_keyword " .
82  "WHERE meta_keyword_id = " . $this->db->quote($this->getMetaId(), 'integer');
83  $res = $this->db->manipulate($query);
84 
85  return true;
86  }
87  return false;
88  }
89 
93  public function __getFields(): array
94  {
95  return array(
96  'rbac_id' => array('integer', $this->getRBACId()),
97  'obj_id' => array('integer', $this->getObjId()),
98  'obj_type' => array('text', $this->getObjType()),
99  'parent_type' => array('text', $this->getParentType()),
100  'parent_id' => array('integer', $this->getParentId()),
101  'keyword' => array('text', $this->getKeyword()),
102  'keyword_language' => array('text', $this->getKeywordLanguageCode())
103  );
104  }
105 
106  public function read(): bool
107  {
108  if ($this->getMetaId()) {
109  $query = "SELECT * FROM il_meta_keyword " .
110  "WHERE meta_keyword_id = " . $this->db->quote($this->getMetaId(), 'integer');
111 
112  $res = $this->db->query($query);
113  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
114  $this->setRBACId((int) $row->rbac_id);
115  $this->setObjId((int) $row->obj_id);
116  $this->setObjType((string) $row->obj_type);
117  $this->setParentId((int) $row->parent_id);
118  $this->setParentType((string) $row->parent_type);
119  $this->setKeyword((string) $row->keyword);
120  $this->setKeywordLanguage(new ilMDLanguageItem($row->keyword_language ?? ''));
121  }
122  }
123  return true;
124  }
125 
126  public function toXML(ilXmlWriter $writer): void
127  {
128  $writer->xmlElement(
129  'Keyword',
130  array(
131  'Language' => $this->getKeywordLanguageCode() ?: 'en'
132  ),
133  $this->getKeyword()
134  );
135  }
136 
137  // STATIC
138 
142  public static function _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type): array
143  {
144  global $DIC;
145 
146  $ilDB = $DIC->database();
147 
148  $query = "SELECT meta_keyword_id FROM il_meta_keyword " .
149  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
150  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
151  "AND parent_id = " . $ilDB->quote($a_parent_id, 'integer') . " " .
152  "AND parent_type = " . $ilDB->quote($a_parent_type, 'text') . " " .
153  "ORDER BY meta_keyword_id ";
154 
155  $res = $ilDB->query($query);
156  $ids = [];
157  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
158  $ids[] = (int) $row->meta_keyword_id;
159  }
160  return $ids;
161  }
162 
166  public static function _getKeywordsByLanguage(int $a_rbac_id, int $a_obj_id, string $a_type): array
167  {
168  global $DIC;
169 
170  $ilDB = $DIC->database();
171  $ilObjDataCache = $DIC['ilObjDataCache'];
172 
173  $query = "SELECT keyword,keyword_language " .
174  "FROM il_meta_keyword " .
175  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
176  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
177  "AND obj_type = " . $ilDB->quote($a_type, 'text') . " ";
178  $res = $ilDB->query($query);
179  $keywords = [];
180  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
181  if ($row->keyword) {
182  $keywords[$row->keyword_language][] = $row->keyword;
183  }
184  }
185  return $keywords;
186  }
187 
191  public static function _getKeywordsByLanguageAsString(int $a_rbac_id, int $a_obj_id, string $a_type): array
192  {
193  $key_string = [];
194  foreach (self::_getKeywordsByLanguage($a_rbac_id, $a_obj_id, $a_type) as $lng_code => $keywords) {
195  $key_string[$lng_code] = implode(",", $keywords);
196  }
197  return $key_string;
198  }
199 
203  public static function _searchKeywords(string $a_query, string $a_type, int $a_rbac_id = 0): array
204  {
205  global $DIC;
206 
207  $ilDB = $DIC->database();
208 
209  $qs = 'AND ';
210  $counter = 0;
211  foreach ((array) explode(' ', $a_query) as $part) {
212  if ($counter++) {
213  $qs .= 'OR ';
214  }
215  $qs .= ($ilDB->like('keyword', 'text', $part) . ' ');
216  }
217 
218  if ($a_rbac_id) {
219  $query = "SELECT obj_id FROM il_meta_keyword " .
220  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . ' ' .
221  'AND obj_type = ' . $ilDB->quote($a_type, 'text') . ' ' .
222  $qs;
223  } else {
224  $query = "SELECT obj_id FROM il_meta_keyword " .
225  'WHERE obj_type = ' . $ilDB->quote($a_type, 'text') . ' ' .
226  $qs;
227  }
228 
229  $res = $ilDB->query($query);
230  $obj_ids = [];
231  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
232  $obj_ids[] = $row->obj_id;
233  }
234 
235  return $obj_ids;
236  }
237 
241  public static function _getMatchingKeywords(string $a_query, string $a_type, int $a_rbac_id = 0): array
242  {
243  global $DIC;
244 
245  $ilDB = $DIC->database();
246 
247  $query = "SELECT DISTINCT keyword FROM il_meta_keyword " .
248  'WHERE obj_type = ' . $ilDB->quote($a_type, 'text') . ' ' .
249  'AND ' . $ilDB->like('keyword', 'text', '%' . trim($a_query) . '%') . ' ';
250 
251  if ($a_rbac_id) {
252  $query .= "AND rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . ' ';
253  }
254 
255  $res = $ilDB->query($query);
256  $kws = [];
257  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
258  $kws[] = $row->keyword;
259  }
260  return $kws;
261  }
262 
263  public static function lookupKeywords(int $a_rbac_id, int $a_obj_id, bool $a_return_ids = false): array
264  {
265  global $DIC;
266 
267  $ilDB = $DIC->database();
268 
269  $query = "SELECT * FROM il_meta_keyword " .
270  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . ' ' .
271  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . ' ';
272  $res = $ilDB->query($query);
273  $kws = [];
274  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
275  if (!$a_return_ids) {
276  if (is_string($row->keyword) && $row->keyword !== '') {
277  $kws[] = $row->keyword;
278  }
279  } else {
280  $kws[] = $row->meta_keyword_id;
281  }
282  }
283  return $kws;
284  }
285 
286  public static function updateKeywords(ilMDGeneral $a_md_section, array $a_keywords): void
287  {
288  // trim keywords
289  $new_keywords = array();
290  foreach ($a_keywords as $lang => $keywords) {
291  foreach ((array) $keywords as $keyword) {
292  $keyword = trim($keyword);
293  if ($keyword !== "" && !(isset($new_keywords[$lang]) && in_array($keyword, $new_keywords[$lang], true))) {
294  $new_keywords[$lang][] = $keyword;
295  }
296  }
297  }
298 
299  // update existing author entries (delete if not entered)
300  foreach ($ids = $a_md_section->getKeywordIds() as $id) {
301  $md_key = $a_md_section->getKeyword($id);
302  $lang = $md_key->getKeywordLanguageCode();
303 
304  // entered keyword already exists
305  if (is_array($new_keywords[$lang] ?? false) && in_array($md_key->getKeyword(), $new_keywords[$lang], true)) {
306  unset($new_keywords[$lang][array_search($md_key->getKeyword(), $new_keywords[$lang], true)]);
307  } else { // existing keyword has not been entered again -> delete
308  $md_key->delete();
309  }
310  }
311 
312  // insert entered, but not existing keywords
313  foreach ($new_keywords as $lang => $key_arr) {
314  foreach ($key_arr as $keyword) {
315  if ($keyword !== "") {
316  $md_key = $a_md_section->addKeyword();
317  $md_key->setKeyword(ilUtil::stripSlashes($keyword));
318  $md_key->setKeywordLanguage(new ilMDLanguageItem($lang));
319  $md_key->save();
320  }
321  }
322  }
323  }
324 }
$res
Definition: ltiservices.php:69
static _getKeywordsByLanguage(int $a_rbac_id, int $a_obj_id, string $a_type)
static _getMatchingKeywords(string $a_query, string $a_type, int $a_rbac_id=0)
static _getKeywordsByLanguageAsString(int $a_rbac_id, int $a_obj_id, string $a_type)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
setKeyword(string $a_keyword)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setRBACId(int $a_id)
toXML(ilXmlWriter $writer)
static updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
global $DIC
Definition: feed.php:28
setObjId(int $a_id)
static lookupKeywords(int $a_rbac_id, int $a_obj_id, bool $a_return_ids=false)
$query
setParentId(int $a_id)
$lang
Definition: xapiexit.php:26
static _getIds(int $a_rbac_id, int $a_obj_id, int $a_parent_id, string $a_parent_type)
ilMDLanguageItem $keyword_language
getKeyword(int $a_keyword_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setParentType(string $a_parent_type)
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
setMetaId(int $a_meta_id, bool $a_read_data=true)
setKeywordLanguage(ilMDLanguageItem $lng_obj)
setObjType(string $a_type)
static _searchKeywords(string $a_query, string $a_type, int $a_rbac_id=0)