ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  function ilMDKeyword($a_rbac_id = 0,$a_obj_id = 0,$a_obj_type = '')
36  {
37  parent::ilMDBase($a_rbac_id,
38  $a_obj_id,
39  $a_obj_type);
40  }
41 
42  // SET/GET
43  function setKeyword($a_keyword)
44  {
45  $this->keyword = $a_keyword;
46  }
47  function getKeyword()
48  {
49  return $this->keyword;
50  }
51  function setKeywordLanguage(&$lng_obj)
52  {
53  if(is_object($lng_obj))
54  {
55  $this->keyword_language = $lng_obj;
56  }
57  }
58  function &getKeywordLanguage()
59  {
60  return is_object($this->keyword_language) ? $this->keyword_language : false;
61  }
63  {
64  return is_object($this->keyword_language) ? $this->keyword_language->getLanguageCode() : false;
65  }
66 
67  function save()
68  {
69  global $ilDB;
70 
71  $fields = $this->__getFields();
72  $fields['meta_keyword_id'] = array('integer',$next_id = $ilDB->nextId('il_meta_keyword'));
73 
74  if($this->db->insert('il_meta_keyword',$fields))
75  {
76  $this->setMetaId($next_id);
77  return $this->getMetaId();
78  }
79  return false;
80  }
81 
82  function update()
83  {
84  global $ilDB;
85 
86  if($this->getMetaId())
87  {
88  if($this->db->update('il_meta_keyword',
89  $this->__getFields(),
90  array("meta_keyword_id" => array('integer',$this->getMetaId()))))
91  {
92  return true;
93  }
94  }
95  return false;
96  }
97 
98  function delete()
99  {
100  global $ilDB;
101 
102  if($this->getMetaId())
103  {
104  $query = "DELETE FROM il_meta_keyword ".
105  "WHERE meta_keyword_id = ".$ilDB->quote($this->getMetaId() ,'integer');
106  $res = $ilDB->manipulate($query);
107 
108  return true;
109  }
110  return false;
111  }
112 
113 
114  function __getFields()
115  {
116  return array('rbac_id' => array('integer',$this->getRBACId()),
117  'obj_id' => array('integer', $this->getObjId()),
118  'obj_type' => array('text', $this->getObjType()),
119  'parent_type' => array('text', $this->getParentType()),
120  'parent_id' => array('integer', $this->getParentId()),
121  'keyword' => array('text', $this->getKeyword()),
122  'keyword_language' => array('text', $this->getKeywordLanguageCode()));
123  }
124 
125  function read()
126  {
127  global $ilDB;
128 
129  include_once 'Services/MetaData/classes/class.ilMDLanguageItem.php';
130 
131  if($this->getMetaId())
132  {
133  $query = "SELECT * FROM il_meta_keyword ".
134  "WHERE meta_keyword_id = ".$ilDB->quote($this->getMetaId() ,'integer');
135 
136  $res = $this->db->query($query);
137  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
138  {
139  $this->setRBACId($row->rbac_id);
140  $this->setObjId($row->obj_id);
141  $this->setObjType($row->obj_type);
142  $this->setParentId($row->parent_id);
143  $this->setParentType($row->parent_type);
144  $this->setKeyword($row->keyword);
145  $this->setKeywordLanguage( new ilMDLanguageItem($row->keyword_language));
146  }
147  }
148  return true;
149  }
150 
151  /*
152  * XML Export of all meta data
153  * @param object (xml writer) see class.ilMD2XML.php
154  *
155  */
156  function toXML(&$writer)
157  {
158  $writer->xmlElement('Keyword',array('Language' => $this->getKeywordLanguageCode() ?
159  $this->getKeywordLanguageCode() :
160  'en'),
161  $this->getKeyword());
162  }
163 
164 
165  // STATIC
166  function _getIds($a_rbac_id,$a_obj_id,$a_parent_id,$a_parent_type)
167  {
168  global $ilDB;
169 
170  $query = "SELECT meta_keyword_id FROM il_meta_keyword ".
171  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id ,'integer')." ".
172  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
173  "AND parent_id = ".$ilDB->quote($a_parent_id ,'integer')." ".
174  "AND parent_type = ".$ilDB->quote($a_parent_type ,'text')." ".
175  "ORDER BY meta_keyword_id ";
176 
177  $res = $ilDB->query($query);
178  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
179  {
180  $ids[] = $row->meta_keyword_id;
181  }
182  return $ids ? $ids : array();
183  }
184 
195  public static function _getKeywordsByLanguage($a_rbac_id,$a_obj_id,$a_type)
196  {
197  global $ilDB,$ilObjDataCache;
198 
199  $query = "SELECT keyword,keyword_language ".
200  "FROM il_meta_keyword ".
201  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id ,'integer')." ".
202  "AND obj_id = ".$ilDB->quote($a_obj_id ,'integer')." ".
203  "AND obj_type = ".$ilDB->quote($a_type ,'text')." ";
204  $res = $ilDB->query($query);
205  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
206  {
207  if($row->keyword)
208  {
209  $keywords[$row->keyword_language][] = $row->keyword;
210  }
211  }
212  return $keywords ? $keywords : array();
213  }
224  public static function _getKeywordsByLanguageAsString($a_rbac_id,$a_obj_id,$a_type)
225  {
226  foreach(ilMDKeyword::_getKeywordsByLanguage($a_rbac_id,$a_obj_id,$a_type) as $lng_code => $keywords)
227  {
228  $key_string[$lng_code] = implode(",",$keywords);
229  }
230  return $key_string ? $key_string : array();
231  }
232 
240  public static function _searchKeywords($a_query,$a_type, $a_rbac_id = 0)
241  {
242  global $ilDB;
243 
244  $qs = 'AND ';
245  $counter = 0;
246  foreach((array) explode(' ',$a_query) as $part)
247  {
248  if($counter++)
249  {
250  $qs .= 'OR ';
251  }
252  $qs .= ($ilDB->like('keyword','text',$part).' ');
253  }
254 
255  if($a_rbac_id)
256  {
257  $query = "SELECT obj_id FROM il_meta_keyword ".
258  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id,'integer').' '.
259  'AND obj_type = '.$ilDB->quote($a_type,'text').' '.
260  $qs;
261  }
262  else
263  {
264  $query = "SELECT obj_id FROM il_meta_keyword ".
265  'WHERE obj_type = '.$ilDB->quote($a_type,'text').' '.
266  $qs;
267  }
268 
269  $res = $ilDB->query($query);
270  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
271  {
272  $obj_ids[] = $row->obj_id;
273  }
274 
275  return (array) $obj_ids;
276  }
277 
286  public static function _getMatchingKeywords($a_query,$a_type, $a_rbac_id = 0)
287  {
288  global $ilDB;
289 
290  $query = "SELECT DISTINCT keyword FROM il_meta_keyword ".
291  'WHERE obj_type = '.$ilDB->quote($a_type,'text').' '.
292  'AND '.$ilDB->like('keyword','text','%'.trim($a_query).'%').' ';
293 
294  if($a_rbac_id)
295  {
296  $query .= "AND rbac_id = ".$ilDB->quote($a_rbac_id,'integer').' ';
297  }
298 
299  $res = $ilDB->query($query);
300  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
301  {
302  $kws[] = $row->keyword;
303  }
304  return (array) $kws;
305  }
306 
314  public static function lookupKeywords($a_rbac_id, $a_obj_id, $a_return_ids = false)
315  {
316  global $ilDB;
317 
318  $query = "SELECT * FROM il_meta_keyword ".
319  "WHERE rbac_id = ".$ilDB->quote($a_rbac_id,'integer').' '.
320  "AND obj_id = ".$ilDB->quote($a_obj_id,'integer').' ';
321  $res = $ilDB->query($query);
322  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
323  {
324  if(!$a_return_ids)
325  {
326  if(strlen($row->keyword))
327  {
328  $kws[] = $row->keyword;
329  }
330  }
331  else
332  {
333  $kws[] = $row->meta_keyword_id;
334  }
335  }
336  return (array) $kws;
337  }
338 
344  public static function updateKeywords(ilMDGeneral $a_md_section, array $a_keywords)
345  {
346  // trim keywords
347  $new_keywords = array();
348  foreach($a_keywords as $lang => $keywords)
349  {
350  foreach($keywords as $keyword)
351  {
352  $keyword = trim($keyword);
353  if ($keyword != "" &&
354  !(is_array($new_keywords[$lang]) && in_array($keyword, $new_keywords[$lang])))
355  {
356  $new_keywords[$lang][] = $keyword;
357  }
358  }
359  }
360 
361  // update existing author entries (delete if not entered)
362  foreach($ids = $a_md_section->getKeywordIds() as $id)
363  {
364  $md_key = $a_md_section->getKeyword($id);
365  $lang = $md_key->getKeywordLanguageCode();
366 
367  // entered keyword already exists
368  if (is_array($new_keywords[$lang]) &&
369  in_array($md_key->getKeyword(), $new_keywords[$lang]))
370  {
371  unset($new_keywords[$lang]
372  [array_search($md_key->getKeyword(), $new_keywords[$lang])]);
373  }
374  else // existing keyword has not been entered again -> delete
375  {
376  $md_key->delete();
377  }
378  }
379 
380  // insert entered, but not existing keywords
381  foreach ($new_keywords as $lang => $key_arr)
382  {
383  foreach($key_arr as $keyword)
384  {
385  if ($keyword != "")
386  {
387  $md_key = $a_md_section->addKeyword();
388  $md_key->setKeyword(ilUtil::stripSlashes($keyword));
389  $md_key->setKeywordLanguage(new ilMDLanguageItem($lang));
390  $md_key->save();
391  }
392  }
393  }
394  }
395 }
396 ?>