ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilContainerSorting.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
33 {
34  protected static $instances = array();
35 
36  protected $obj_id;
37  protected $db;
38 
39  protected $manual_sort_enabled = false;
40  protected $sorting_mode = 0;
41 
42  const ORDER_DEFAULT = 999999;
43 
51  private function __construct($a_obj_id)
52  {
53  global $ilDB;
54 
55  $this->db = $ilDB;
56  $this->obj_id = $a_obj_id;
57 
58  $this->read();
59  }
60 
68  public function getSortMode()
69  {
70  return $this->sorting_mode;
71  }
72 
73 
82  public static function _getInstance($a_obj_id)
83  {
84  if(isset(self::$instances[$a_obj_id]))
85  {
86  return self::$instances[$a_obj_id];
87  }
88  return self::$instances[$a_obj_id] = new ilContainerSorting($a_obj_id);
89  }
90 
96  public static function lookupPositions($a_obj_id)
97  {
98  global $ilDB;
99 
100  $query = "SELECT * FROM container_sorting WHERE ".
101  "obj_id = ".$ilDB->quote($a_obj_id,'integer');
102  $res = $ilDB->query($query);
103  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
104  {
105  $sorted[$row->child_id] = $row->position;
106  }
107  return $sorted ? $sorted : array();
108  }
109 
116  public function cloneSorting($a_target_id,$a_copy_id)
117  {
118  global $ilDB;
119  global $ilLog;
120 
121  $ilLog->write(__METHOD__.': Cloning container sorting.');
122 
123  $target_obj_id = ilObject::_lookupObjId($a_target_id);
124 
125  include_once('./Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
126  $mappings = ilCopyWizardOptions::_getInstance($a_copy_id)->getMappings();
127 
128  $query = "SELECT * FROM container_sorting ".
129  "WHERE obj_id = ".$ilDB->quote($this->obj_id, 'integer');
130 
131  $res = $ilDB->query($query);
132 
133  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
134  {
135  if(!isset($mappings[$row->child_id]) or !$mappings[$row->child_id])
136  {
137  #$ilLog->write(__METHOD__.': No mapping found for:'.$row->child_id);
138  continue;
139  }
140 
141  if($row->parent_id and (!isset($mappings[$row->parent_id]) or !$mappings[$row->parent_id]))
142  {
143  continue;
144  }
145 
146  $query = "DELETE FROM container_sorting ".
147  "WHERE obj_id = ".$ilDB->quote($target_obj_id,'integer')." ".
148  "AND child_id = ".$ilDB->quote($mappings[$row->child_id],'integer')." ".
149  "AND parent_type = ".$ilDB->quote($row->parent_type,'text').' '.
150  "AND parent_id = ".$ilDB->quote((int) $mappings[$row->parent_id],'integer');
151  $ilDB->manipulate($query);
152 
153  // Add new value
154  $query = "INSERT INTO container_sorting (obj_id,child_id,position,parent_type,parent_id) ".
155  "VALUES( ".
156  $ilDB->quote($target_obj_id ,'integer').", ".
157  $ilDB->quote($mappings[$row->child_id] ,'integer').", ".
158  $ilDB->quote($row->position,'integer').", ".
159  $ilDB->quote($row->parent_type,'text').", ".
160  $ilDB->quote((int) $mappings[$row->parent_id],'integer').
161  ")";
162  $ilDB->manipulate($query);
163  }
164  return true;
165  }
166 
167 
168 
176  public function sortItems($a_items)
177  {
178  $sorted = array();
179  if(!$this->manual_sort_enabled)
180  {
181  switch($this->getSortMode())
182  {
184  foreach((array) $a_items as $type => $data)
185  {
186 // $sorted[$type] = ilUtil::sortArray((array) $data,'title','asc',false);
187  $sorted[$type] = $data;
188  }
189  return $sorted ? $sorted : array();
190 
192  foreach((array) $a_items as $type => $data)
193  {
194  $sorted[$type] = ilUtil::sortArray((array) $data,'start','asc',true);
195  }
196  return $sorted ? $sorted : array();
197  }
198  return $a_items;
199  }
200  if(!count($a_items))
201  {
202  return $a_items;
203  }
204  foreach((array) $a_items as $type => $data)
205  {
206  // Add position
207  $items = array();
208  foreach((array) $data as $key => $item)
209  {
210  $items[$key] = $item;
211  if(is_array($this->sorting['all']) and isset($this->sorting['all'][$item['child']]))
212  {
213  $items[$key]['position'] = $this->sorting['all'][$item['child']];
214  }
215  else
216  {
217  $items[$key]['position'] = self::ORDER_DEFAULT;
218  }
219  }
220 
221  switch($type)
222  {
223  case '_all':
224  $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
225  break;
226 
227  case '_non_sess':
228  $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
229  break;
230 
231  default:
232  $sorted[$type] = ilUtil::sortArray((array) $items,'position','asc',true);
233  break;
234  }
235  }
236  return $sorted ? $sorted : array();
237  }
238 
246  public function sortSubItems($a_parent_type,$a_parent_id,$a_items)
247  {
248  switch($this->getSortMode())
249  {
251  // Add position
252  $items = array();
253  foreach($a_items as $key => $item)
254  {
255  $items[$key] = $item;
256  $items[$key]['position'] = isset($this->sorting[$a_parent_type][$a_parent_id][$item['child']]) ?
257  $this->sorting[$a_parent_type][$a_parent_id][$item['child']] : self::ORDER_DEFAULT;
258  }
259  return ilUtil::sortArray((array) $items,'position','asc',true);
260 
261 
263  default:
264  return ilUtil::sortArray((array) $a_items,'title','asc',true);
265  }
266 
267  }
268 
269 
270 
271 
278  public function isManualSortingEnabled()
279  {
280  return (bool) $this->manual_sort_enabled;
281  }
282 
290  public function savePost($a_type_positions)
291  {
292  global $ilLog;
293 
294  if(!is_array($a_type_positions))
295  {
296  return false;
297  }
298  foreach($a_type_positions as $key => $position)
299  {
300  if(!is_array($position))
301  {
302  $items[$key] = $position * 100;
303  }
304  else
305  {
306  $ilLog->write(__METHOD__.': Deprecated call');
307  foreach($position as $parent_id => $sub_items)
308  {
309  $this->saveSubItems($key,$parent_id,$sub_items ? $sub_items : array());
310  }
311  }
312  }
313  $this->saveItems($items ? $items : array());
314  }
315 
316 
326  protected function saveItems($a_items)
327  {
328  global $ilDB;
329 
330  foreach($a_items as $child_id => $position)
331  {
332  $ilDB->replace(
333  'container_sorting',
334  array(
335  'obj_id' => array('integer',$this->obj_id),
336  'child_id' => array('integer',$child_id),
337  'parent_id' => array('integer',0)
338  ),
339  array(
340  'parent_type' => array('text',''),
341  'position' => array('integer',$position)
342  )
343  );
344  }
345  return true;
346  }
347 
355  protected function saveSubItems($a_parent_type,$a_parent_id,$a_items)
356  {
357  global $ilDB;
358 
359  foreach($a_items as $child_id => $position)
360  {
361  $ilDB->replace(
362  'container_sorting',
363  array(
364  'obj_id' => array('integer',$this->obj_id),
365  'child_id' => array('integer',$child_id),
366  'parent_id' => array('integer',$a_parent_id)
367  ),
368  array(
369  'parent_type' => array('text',$a_parent_type),
370  'position' => array('integer',$position)
371  )
372  );
373  }
374  return true;
375 
376  }
377 
378 
385  private function read()
386  {
387  if(!$this->obj_id)
388  {
389  return true;
390  }
391 
392  include_once('Services/Container/classes/class.ilContainerSortingSettings.php');
393  $this->manual_sort_enabled = ilContainerSortingSettings::_isManualSortingEnabled($this->obj_id);
394  $this->sorting_mode = ilContainerSortingSettings::_lookupSortMode($this->obj_id);
395 
396  $query = "SELECT * FROM container_sorting ".
397  "WHERE obj_id = ".$this->db->quote($this->obj_id ,'integer')." ORDER BY position";
398  $res = $this->db->query($query);
399  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
400  {
401  if($row->parent_id)
402  {
403  $this->sorting[$row->parent_type][$row->parent_id][$row->child_id] = $row->position;
404  }
405  else
406  {
407  $this->sorting['all'][$row->child_id] = $row->position;
408  }
409  }
410  return true;
411  }
412 }
413 
414 
415 ?>