ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilContainerReferenceGUI.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 
24 include_once('./classes/class.ilObjectGUI.php');
25 
36 {
38 
39  const MODE_CREATE = 1;
40  const MODE_EDIT = 2;
41 
42  protected $existing_objects = array();
43 
49  public function __construct($a_data, $a_id, $a_call_by_reference = true, $a_prepare_output = true)
50  {
51  parent::__construct($a_data, $a_id,$a_call_by_reference,$a_prepare_output);
52  }
53 
59  public function redirectObject()
60  {
61  ilUtil::redirect('repository.php?ref_id='.$this->object->getTargetRefId());
62  }
63 
69  public function createObject()
70  {
71  global $ilUser,$ilAccess,$ilErr,$ilSetting;
72 
73  $new_type = $_REQUEST["new_type"];
74  if(!$ilAccess->checkAccess("create_".$this->getReferenceType(),'',$_GET["ref_id"], $new_type))
75  {
76  $ilErr->raiseError($this->lng->txt("permission_denied"),$ilErr->MESSAGE);
77  }
78 
79  return $this->initTargetSelection(self::MODE_CREATE);
80 
81  /*
82  if(!count($this->existing_objects = ilUtil::_getObjectsByOperations(
83  $this->getTargetType(),
84  'read',
85  $ilUser->getId(),
86  self::MAX_SELECTION_ENTRIES)))
87  {
88  // TODO: No Objects with read permission found => send error message
89  return false;
90  }
91 
92  if(count($this->existing_objs) >= $max_entries)
93  {
94  return $this->initTargetSelection();
95  }
96  else
97  {
98  return $this->showSelection();
99  }
100  */
101  }
102 
103 
111  public function saveObject()
112  {
113  global $ilAccess;
114 
115  if(!(int) $_REQUEST['target_id'])
116  {
117  ilUtil::sendFailure($this->lng->txt('select_one'));
118  $this->createObject();
119  return false;
120  }
121  if(!$ilAccess->checkAccess('visible','',(int) $_REQUEST['target_id']))
122  {
123  ilUtil::sendFailure($this->lng->txt('permission_denied'));
124  $this->createObject();
125  return false;
126  }
127 
129  }
130 
131  protected function initCreateForm($a_new_type)
132  {
133  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
134  $form = new ilPropertyFormGUI();
135  return $form;
136  }
137 
138  protected function afterSave(ilObject $a_new_object)
139  {
140  $target_obj_id = ilObject::_lookupObjId((int) $_REQUEST['target_id']);
141  $a_new_object->setTargetId($target_obj_id);
142  $a_new_object->update();
143 
144  ilUtil::sendSuccess($this->lng->txt("object_added"), true);
145  $this->ctrl->returnToParent($this);
146  }
147 
155  public function editObject()
156  {
157  global $ilUser,$ilSetting,$ilTabs;
158 
159  $ilTabs->setTabActive('edit');
160 
161  $this->initTargetSelection(self::MODE_EDIT);
162 
163  /*
164  $max_entries = $ilSetting->get('search_max_hits',10);
165  if(!count($this->existing_objects = ilUtil::_getObjectsByOperations($this->getTargetType(),'read',$ilUser->getId(),$max_entries)))
166  {
167  // TODO: No Objects with read permission found => send error message
168  return false;
169  }
170 
171  $this->initFormEditSelection();
172  $this->tpl->setContent($this->form->getHTML());
173  return true;
174 
175  */
176  }
177 
178  public function editReferenceObject()
179  {
180  $this->editObject();
181  }
182 
190  public function updateObject()
191  {
192  global $ilAccess;
193 
194  if(!(int) $_REQUEST['target_id'])
195  {
196  ilUtil::sendFailure($this->lng->txt('select_one'));
197  $this->editObject();
198  return false;
199  }
200  if(!$ilAccess->checkAccess('visible','',(int) $_REQUEST['target_id']))
201  {
202  ilUtil::sendFailure($this->lng->txt('permission_denied'));
203  $this->editObject();
204  return false;
205  }
206  $this->checkPermission('write');
207 
208  $target_obj_id = ilObject::_lookupObjId((int) $_REQUEST['target_id']);
209  $this->object->setTargetId($target_obj_id);
210  $this->object->update();
211 
212  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
213  $this->ctrl->redirect($this,'edit');
214  }
215 
216 
223  protected function showSelection()
224  {
225  $this->initFormSelection();
226  $this->tpl->setContent($this->form->getHTML());
227  return true;
228  }
229 
236  protected function initFormSelection()
237  {
238  if(is_object($this->form))
239  {
240  return true;
241  }
242  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
243  $this->form = new ilPropertyFormGUI();
244  $this->ctrl->setParameter($this,'new_type',$this->getReferenceType());
245  $this->form->setFormAction($this->ctrl->getFormAction($this));
246  $this->form->setTitle($this->lng->txt($this->getReferenceType().'_new'));
247  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getReferenceType().'.gif'));
248 
249  // Show selection
250  $select = new ilSelectInputGUI($this->lng->txt('objs_'.$this->getTargetType()),'target_id');
251  $select->setOptions(self::_prepareSelection($this->existing_objects,$this->getTargetType()));
252  $select->setInfo($this->lng->txt($_POST['new_type'].'_edit_info'));
253  $this->form->addItem($select);
254 
255  $this->form->addCommandButton('save',$this->lng->txt('save'));
256  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
257  }
258 
265  protected function initFormEditSelection()
266  {
267  if(is_object($this->form))
268  {
269  return true;
270  }
271  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
272  $this->form = new ilPropertyFormGUI();
273  $this->form->setFormAction($this->ctrl->getFormAction($this));
274  $this->form->setTitle($this->lng->txt($this->getReferenceType().'_edit'));
275  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getReferenceType().'.gif'));
276 
277  // Show selection
278  $select = new ilSelectInputGUI($this->lng->txt('objs_'.$this->getTargetType()),'target_id');
279  $select->setValue($this->object->getTargetRefId());
280  $select->setOptions(self::_prepareSelection($this->existing_objects,$this->getTargetType()));
281  $select->setInfo($this->lng->txt($this->object->getType().'_edit_info'));
282  $this->form->addItem($select);
283 
284  $this->form->addCommandButton('update',$this->lng->txt('save'));
285  #$this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
286  }
287 
288 
296  public function getTargetType()
297  {
298  return $this->target_type;
299  }
300 
308  public function getReferenceType()
309  {
310  return $this->reference_type;
311  }
312 
321  public static function _prepareSelection($a_ref_ids,$a_target_type)
322  {
323  global $ilDB,$lng;
324 
325  $query = "SELECT obj_data.title obj_title,path_data.title path_title,child FROM tree ".
326  "JOIN object_reference obj_ref ON child = obj_ref.ref_id ".
327  "JOIN object_data obj_data ON obj_ref.obj_id = obj_data.obj_id ".
328  "JOIN object_reference path_ref ON parent = path_ref.ref_id ".
329  "JOIN object_data path_data ON path_ref.obj_id = path_data.obj_id ".
330  "WHERE ".$ilDB->in('child',$a_ref_ids,false,'integer').' '.
331  "ORDER BY obj_data.title ";
332  $res = $ilDB->query($query);
333 
334  $options[0] = $lng->txt('obj_'.$a_target_type.'_select');
335  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
336  {
337  if(strlen($title = $row->obj_title) > 40)
338  {
339  $title = substr($title,0,40).'...';
340  }
341  if(strlen($path = $row->path_title) > 40)
342  {
343  $path = substr($path,0,40).'...';
344  }
345  $options[$row->child] = ($title.' ('.$lng->txt('path').': '.$path.')');
346  }
347  return $options ? $options : array();
348  }
349 
354  protected function initTargetSelection($a_mode = self::MODE_CREATE)
355  {
356  global $ilCtrl, $tree;
357 
358  // empty session on init
359  $_SESSION['ref_repexpand'] = array();
360 
361  // copy opened nodes from repository explorer
362  $_SESSION['ref_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
363 
364  // open current position
365 
366  if($a_mode == self::MODE_CREATE)
367  {
368  $target = (int) $_GET['ref_id'];
369  }
370  else
371  {
372  $target = (int) $this->object->getTargetRefId();
373  }
374 
375  $path = $tree->getPathId($target);
376  foreach((array) $path as $node_id)
377  {
378  if(!in_array($node_id, $_SESSION['ref_repexpand']))
379  {
380  $_SESSION['ref_repexpand'][] = $node_id;
381  }
382  }
383 
384  $_SESSION['ref_mode'] = $a_mode;
385 
387  }
388 
394  {
395  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
396 
397  include_once './Services/ContainerReference/classes/class.ilContainerSelectionExplorer.php';
398 
399  ilUtil::sendInfo($this->lng->txt($this->getReferenceType().'_edit_info'));
400  if($_SESSION['ref_mode'] == self::MODE_CREATE)
401  {
402  $ilToolbar->addButton($this->lng->txt('back'), $ilCtrl->getLinkTarget($this,'cancel'));
403  $this->ctrl->setParameter($this,'new_type',$this->getReferenceType());
404  $cmd = 'save';
405  }
406  else
407  {
408  $ilTabs->setTabActive('edit');
409  $cmd = 'update';
410  }
411  $explorer = new ilContainerSelectionExplorer($this->ctrl->getLinkTarget($this,$cmd));
412 
413  if(isset($_GET['ref_repexpand']))
414  {
415  $explorer->setExpand((int) $_GET['ref_repexpand']);
416  }
417  else
418  {
419  $explorer->setExpand(ROOT_FOLDER_ID);
420  }
421  $explorer->setFrameTarget('_self');
422  $explorer->setExpandTarget($this->ctrl->getLinkTarget($this,'showTargetSelectionTree'));
423  $explorer->setTargetGet('target_id');
424  $explorer->setTargetType($this->getTargetType());
425  $explorer->setOutput(0);
426  $this->tpl->setContent($explorer->getOutput());
427  }
428 }
429 ?>