ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSearchFolder.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 
32 include_once "./Services/Tree/classes/class.ilTree.php";
33 
34 
36 {
37  // OBJECT VARIABLES
38  var $s_tree;
39  var $ilias;
40 
41  var $user_id;
42  var $root_id;
45  var $title;
46 
51  function ilSearchFolder($a_user_id,$a_folder_id = 0)
52  {
53  global $ilias;
54 
55  define("TABLE_SEARCH_TREE","search_tree");
56  define("TABLE_SEARCH_DATA","search_data");
57 
58  $this->ilias =& $ilias;
59 
60  $this->user_id = $a_user_id;
61 
62  $this->__readRootId();
63 
64  // IF NO FOLDER ID IS GIVEN DEFAULT TO ROOT ID
65  $this->setFolderId($a_folder_id ? $a_folder_id : $this->getRootId());
66 
67  $this->__initTreeObject();
68 
69  if(!$this->__treeExists())
70  {
71  $this->__createNewTree();
72  }
73  $this->__init();
74 
75  // CHECK USER TREE IF HAS BEEN CREATED
76  }
77 
78  // SET/GET
79  function getType()
80  {
81  return "seaf";
82  }
83 
84  function getRootId()
85  {
86  return $this->root_id;
87  }
88  function setRootId($a_root_id)
89  {
90  $this->root_id = $a_root_id;
91  }
92  function setFolderId($a_folder_id)
93  {
94  $this->folder_id = $a_folder_id;
95  }
96  function getFolderId()
97  {
98  return $this->folder_id;
99  }
100  function setUserId($a_user_id)
101  {
102  $this->user_id = $a_user_id;
103 
104  }
105  function getUserId()
106  {
107  return $this->user_id;
108  }
109  function setTitle($a_title)
110  {
111  $this->title = $a_title;
112  }
113  function getTitle()
114  {
115  return $this->title;
116  }
117 
118  function getChilds()
119  {
120  return $this->s_tree->getChilds($this->getFolderId(),"type","DESC");
121  }
122  function hasSubfolders()
123  {
124  $childs = $this->getChilds();
125 
126  return $childs[0]["type"] == "seaf" ? true : false;
127  }
128  function hasResults()
129  {
130  $childs = $this->getChilds();
131 
132  return $childs[count($childs)-1]["type"] == "sea" ? true : false;
133  }
134  function countFolders()
135  {
136  $childs = $this->s_tree->getChilds($this->getRootId(),"type","DESC");
137 
138  $counter = 0;
139  while(true)
140  {
141  if($childs[$counter]["type"] != "seaf")
142  {
143  break;
144  }
145  ++$counter;
146  }
147  return $counter;
148  }
149 
150  function getPath()
151  {
152  return $this->s_tree->getPathFull($this->getFolderId(),$this->getRootId());
153  }
154 
155  function setParentId($a_parent_id)
156  {
157  $this->parent_id = $a_parent_id;
158  }
159 
160  function getParentId()
161  {
162  return $this->parent_id;
163  }
164 
165  function delete($a_folder_id)
166  {
167  $subtree = $this->s_tree->getSubTree($this->s_tree->getNodeData($a_folder_id));
168 
169  foreach($subtree as $node)
170  {
171  // DELETE DATA ENTRIES
172  $query = "DELETE FROM ".TABLE_SEARCH_DATA." ".
173  "WHERE user_id = '".$this->getUserId()."' ".
174  "AND obj_id = '".$node["obj_id"]."'";
175 
176  $res = $this->ilias->db->query($query);
177  }
178  // FINALLY DELETE SUBTREE
179  $this->s_tree->deleteTree($this->s_tree->getNodeData($a_folder_id));
180 
181  return true;
182  }
183 
184  function assignResult(&$search_result)
185  {
186  if(!$this->__treeExists())
187  {
188  $this->__createNewTree();
189  }
190  // CREATE RESULT
191  $query = "INSERT INTO ".TABLE_SEARCH_DATA ." ".
192  "SET user_id = '".$this->getUserId()."', ".
193  "title = '".$search_result->getTitle()."', ".
194  "target = '".$search_result->getTarget()."', ".
195  "type = 'sea'";
196 
197  $res = $this->ilias->db->query($query);
198 
199  $this->s_tree->insertNode($this->ilias->db->getLastInsertId(),$this->getFolderId());
200 
201  return true;
202  }
203 
204  function updateTitle($a_title)
205  {
206  $query = "UPDATE ".TABLE_SEARCH_DATA." ".
207  "SET title = '".addslashes($a_title)."' ".
208  "WHERE obj_id = '".$this->getFolderId()."' ".
209  "AND user_id = '".$this->getUserId()."'";
210 
211  $res = $this->ilias->db->query($query);
212 
213  return true;
214  }
215 
216  function &create($a_title)
217  {
218  // CREATE FOLDER
219  $query = "INSERT INTO ".TABLE_SEARCH_DATA ." ".
220  "SET user_id = '".$this->getUserId()."', ".
221  "title = '".addslashes($a_title)."', ".
222  "type = 'seaf'";
223 
224  $res = $this->ilias->db->query($query);
225 
226  $this->s_tree->insertNode($this->ilias->db->getLastInsertId(),$this->getFolderId());
227 
228  $new_obj =& new ilSearchFolder($this->getUserId(),$this->getFolderId());
229  $new_obj->setTitle($a_title);
230 
231  return $new_obj;
232  }
233  function getTree()
234  {
235  $tmp_folder_id = $this->getFolderId();
236 
237  $this->setFolderId($this->getRootId());
238 
239  $tree_data = $this->getSubtree();
240  $this->setFolderId($tmp_folder_id);
241 
242  return $tree_data;
243  }
244 
245  function getSubtree()
246  {
247  $subtree = $this->s_tree->getSubtree($this->s_tree->getNodeData($this->getFolderId()));
248 
249  // FILTER FOLDERS
250  foreach($subtree as $node)
251  {
252  if($node["type"] == "seaf")
253  {
254  $filtered[] = $node;
255  }
256  }
257  return count($filtered) ? $filtered : array();
258  }
259  // PRIVATE METHODS
260  function __init()
261  {
262  $query = "SELECT * FROM ".TABLE_SEARCH_TREE.", ".TABLE_SEARCH_DATA." ".
263  "WHERE child = obj_id ".
264  "AND child = '".$this->getFolderId()."' ".
265  "AND tree = '".$this->getUserId()."'";
266 
267  $res = $this->ilias->db->query($query);
268 
269  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
270  {
271  $this->setTitle($row->title);
272  $this->setParentId($row->parent);
273  }
274  }
275 
276  function __initTreeObject()
277  {
278  $this->s_tree = new ilTree($this->getUserId(),$this->getRootId());
279  $this->s_tree->setTableNames(TABLE_SEARCH_TREE,TABLE_SEARCH_DATA);
280 
281  return true;
282  }
283 
284  function __treeExists()
285  {
286  $query = "SELECT tree FROM ".TABLE_SEARCH_TREE." ".
287  "WHERE tree = ".$this->getUserId();
288 
289  $res = $this->ilias->db->query($query);
290 
291  return $res->numRows() ? true : false;
292  }
293 
294  function __createNewTree()
295  {
296  // ADD ENTRY search_data
297  $query = "INSERT INTO ".TABLE_SEARCH_DATA." ".
298  "SET user_id = '".$this->getUserId()."', ".
299  "type = 'seaf'";
300 
301  $res = $this->ilias->db->query($query);
302  $root_id = $this->__getLastInsertId();
303 
304  $this->s_tree->addTree($this->getUserId(),$root_id);
305 
306  // SET MEMBER VARIABLES
307  $this->setFolderId($root_id);
308  $this->setRootId($root_id);
309 
310  return true;
311  }
312 
313  function __readRootId()
314  {
315  $query = "SELECT child FROM ".TABLE_SEARCH_TREE." ".
316  "WHERE tree = '".$this->getUserId()."' ".
317  "AND parent = '0'";
318 
319  $res = $this->ilias->db->query($query);
320  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
321  {
322  $this->setRootId($row->child);
323  }
324  return true;
325  }
326 
327  function __getLastInsertId()
328  {
329  return $this->ilias->db->getLastInsertId();
330  }
331 
332 } // END class.Search
333 ?>