ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilMDKeyword.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
31 include_once 'class.ilMDBase.php';
32 
33 class ilMDKeyword extends ilMDBase
34 {
35  // SET/GET
36  public function setKeyword($a_keyword)
37  {
38  $this->keyword = $a_keyword;
39  }
40  public function getKeyword()
41  {
42  return $this->keyword;
43  }
44  public function setKeywordLanguage(&$lng_obj)
45  {
46  if (is_object($lng_obj)) {
47  $this->keyword_language = $lng_obj;
48  }
49  }
50  public function &getKeywordLanguage()
51  {
52  return is_object($this->keyword_language) ? $this->keyword_language : false;
53  }
54  public function getKeywordLanguageCode()
55  {
56  return is_object($this->keyword_language) ? $this->keyword_language->getLanguageCode() : false;
57  }
58 
59  public function save()
60  {
61  global $ilDB;
62 
63  $fields = $this->__getFields();
64  $fields['meta_keyword_id'] = array('integer',$next_id = $ilDB->nextId('il_meta_keyword'));
65 
66  if ($this->db->insert('il_meta_keyword', $fields)) {
67  $this->setMetaId($next_id);
68  return $this->getMetaId();
69  }
70  return false;
71  }
72 
73  public function update()
74  {
75  global $ilDB;
76 
77  if ($this->getMetaId()) {
78  if ($this->db->update(
79  'il_meta_keyword',
80  $this->__getFields(),
81  array("meta_keyword_id" => array('integer',$this->getMetaId()))
82  )) {
83  return true;
84  }
85  }
86  return false;
87  }
88 
89  public function delete()
90  {
91  global $ilDB;
92 
93  if ($this->getMetaId()) {
94  $query = "DELETE FROM il_meta_keyword " .
95  "WHERE meta_keyword_id = " . $ilDB->quote($this->getMetaId(), 'integer');
96  $res = $ilDB->manipulate($query);
97 
98  return true;
99  }
100  return false;
101  }
102 
103 
104  public function __getFields()
105  {
106  return array('rbac_id' => array('integer',$this->getRBACId()),
107  'obj_id' => array('integer', $this->getObjId()),
108  'obj_type' => array('text', $this->getObjType()),
109  'parent_type' => array('text', $this->getParentType()),
110  'parent_id' => array('integer', $this->getParentId()),
111  'keyword' => array('text', $this->getKeyword()),
112  'keyword_language' => array('text', $this->getKeywordLanguageCode()));
113  }
114 
115  public function read()
116  {
117  global $ilDB;
118 
119  include_once 'Services/MetaData/classes/class.ilMDLanguageItem.php';
120 
121  if ($this->getMetaId()) {
122  $query = "SELECT * FROM il_meta_keyword " .
123  "WHERE meta_keyword_id = " . $ilDB->quote($this->getMetaId(), 'integer');
124 
125  $res = $this->db->query($query);
126  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
127  $this->setRBACId($row->rbac_id);
128  $this->setObjId($row->obj_id);
129  $this->setObjType($row->obj_type);
130  $this->setParentId($row->parent_id);
131  $this->setParentType($row->parent_type);
132  $this->setKeyword($row->keyword);
133  $this->setKeywordLanguage(new ilMDLanguageItem($row->keyword_language));
134  }
135  }
136  return true;
137  }
138 
139  /*
140  * XML Export of all meta data
141  * @param object (xml writer) see class.ilMD2XML.php
142  *
143  */
144  public function toXML(&$writer)
145  {
146  $writer->xmlElement(
147  'Keyword',
148  array('Language' => $this->getKeywordLanguageCode() ?
149  $this->getKeywordLanguageCode() :
150  'en'),
151  $this->getKeyword()
152  );
153  }
154 
155 
156  // STATIC
157  public static function _getIds($a_rbac_id, $a_obj_id, $a_parent_id, $a_parent_type)
158  {
159  global $ilDB;
160 
161  $query = "SELECT meta_keyword_id FROM il_meta_keyword " .
162  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
163  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
164  "AND parent_id = " . $ilDB->quote($a_parent_id, 'integer') . " " .
165  "AND parent_type = " . $ilDB->quote($a_parent_type, 'text') . " " .
166  "ORDER BY meta_keyword_id ";
167 
168  $res = $ilDB->query($query);
169  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
170  $ids[] = $row->meta_keyword_id;
171  }
172  return $ids ? $ids : array();
173  }
174 
185  public static function _getKeywordsByLanguage($a_rbac_id, $a_obj_id, $a_type)
186  {
187  global $ilDB,$ilObjDataCache;
188 
189  $query = "SELECT keyword,keyword_language " .
190  "FROM il_meta_keyword " .
191  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . " " .
192  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
193  "AND obj_type = " . $ilDB->quote($a_type, 'text') . " ";
194  $res = $ilDB->query($query);
195  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
196  if ($row->keyword) {
197  $keywords[$row->keyword_language][] = $row->keyword;
198  }
199  }
200  return $keywords ? $keywords : array();
201  }
212  public static function _getKeywordsByLanguageAsString($a_rbac_id, $a_obj_id, $a_type)
213  {
214  foreach (ilMDKeyword::_getKeywordsByLanguage($a_rbac_id, $a_obj_id, $a_type) as $lng_code => $keywords) {
215  $key_string[$lng_code] = implode(",", $keywords);
216  }
217  return $key_string ? $key_string : array();
218  }
219 
227  public static function _searchKeywords($a_query, $a_type, $a_rbac_id = 0)
228  {
229  global $ilDB;
230 
231  $qs = 'AND ';
232  $counter = 0;
233  foreach ((array) explode(' ', $a_query) as $part) {
234  if ($counter++) {
235  $qs .= 'OR ';
236  }
237  $qs .= ($ilDB->like('keyword', 'text', $part) . ' ');
238  }
239 
240  if ($a_rbac_id) {
241  $query = "SELECT obj_id FROM il_meta_keyword " .
242  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . ' ' .
243  'AND obj_type = ' . $ilDB->quote($a_type, 'text') . ' ' .
244  $qs;
245  } else {
246  $query = "SELECT obj_id FROM il_meta_keyword " .
247  'WHERE obj_type = ' . $ilDB->quote($a_type, 'text') . ' ' .
248  $qs;
249  }
250 
251  $res = $ilDB->query($query);
252  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
253  $obj_ids[] = $row->obj_id;
254  }
255 
256  return (array) $obj_ids;
257  }
258 
267  public static function _getMatchingKeywords($a_query, $a_type, $a_rbac_id = 0)
268  {
269  global $ilDB;
270 
271  $query = "SELECT DISTINCT keyword FROM il_meta_keyword " .
272  'WHERE obj_type = ' . $ilDB->quote($a_type, 'text') . ' ' .
273  'AND ' . $ilDB->like('keyword', 'text', '%' . trim($a_query) . '%') . ' ';
274 
275  if ($a_rbac_id) {
276  $query .= "AND rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . ' ';
277  }
278 
279  $res = $ilDB->query($query);
280  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
281  $kws[] = $row->keyword;
282  }
283  return (array) $kws;
284  }
285 
293  public static function lookupKeywords($a_rbac_id, $a_obj_id, $a_return_ids = false)
294  {
295  global $ilDB;
296 
297  $query = "SELECT * FROM il_meta_keyword " .
298  "WHERE rbac_id = " . $ilDB->quote($a_rbac_id, 'integer') . ' ' .
299  "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . ' ';
300  $res = $ilDB->query($query);
301  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
302  if (!$a_return_ids) {
303  if (strlen($row->keyword)) {
304  $kws[] = $row->keyword;
305  }
306  } else {
307  $kws[] = $row->meta_keyword_id;
308  }
309  }
310  return (array) $kws;
311  }
312 
318  public static function updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
319  {
320  // trim keywords
321  $new_keywords = array();
322  foreach ($a_keywords as $lang => $keywords) {
323  foreach ((array) $keywords as $keyword) {
324  $keyword = trim($keyword);
325  if ($keyword != "" &&
326  !(is_array($new_keywords[$lang]) && in_array($keyword, $new_keywords[$lang]))) {
327  $new_keywords[$lang][] = $keyword;
328  }
329  }
330  }
331 
332  // update existing author entries (delete if not entered)
333  foreach ($ids = $a_md_section->getKeywordIds() as $id) {
334  $md_key = $a_md_section->getKeyword($id);
335  $lang = $md_key->getKeywordLanguageCode();
336 
337  // entered keyword already exists
338  if (is_array($new_keywords[$lang]) &&
339  in_array($md_key->getKeyword(), $new_keywords[$lang])) {
340  unset($new_keywords[$lang]
341  [array_search($md_key->getKeyword(), $new_keywords[$lang])]);
342  } else { // existing keyword has not been entered again -> delete
343  $md_key->delete();
344  }
345  }
346 
347  // insert entered, but not existing keywords
348  foreach ($new_keywords as $lang => $key_arr) {
349  foreach ($key_arr as $keyword) {
350  if ($keyword != "") {
351  $md_key = $a_md_section->addKeyword();
352  $md_key->setKeyword(ilUtil::stripSlashes($keyword));
353  $md_key->setKeywordLanguage(new ilMDLanguageItem($lang));
354  $md_key->save();
355  }
356  }
357  }
358  }
359 }
& getKeyword($a_keyword_id)
setObjType($a_type)
static _searchKeywords($a_query, $a_type, $a_rbac_id=0)
Search for objects by keywords.
if(!array_key_exists('StateId', $_REQUEST)) $id
setKeyword($a_keyword)
static updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
Update keywords from input array.
$counter
$a_type
Definition: workflow.php:92
setMetaId($a_meta_id, $a_read_data=true)
foreach($_POST as $key=> $value) $res
setKeywordLanguage(&$lng_obj)
setObjId($a_id)
setRBACId($a_id)
$query
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
Create styles array
The data for the language used.
setParentId($a_id)
static _getMatchingKeywords($a_query, $a_type, $a_rbac_id=0)
Search for keywords.
static _getKeywordsByLanguage($a_rbac_id, $a_obj_id, $a_type)
Get keywords by language.
static _getIds($a_rbac_id, $a_obj_id, $a_parent_id, $a_parent_type)
global $ilDB
setParentType($a_parent_type)
static lookupKeywords($a_rbac_id, $a_obj_id, $a_return_ids=false)
Lookup Keywords.
static _getKeywordsByLanguageAsString($a_rbac_id, $a_obj_id, $a_type)
Get keywords by language as string.