ILIAS  Release_4_1_x_branch Revision 61804
 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 = $_POST["new_type"] ? $_POST["new_type"] : $_GET["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,$tree;
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('read','',(int) $_REQUEST['target_id']))
122  {
123  ilUtil::sendFailure($this->lng->txt('permission_denied'));
124  $this->createObject();
125  return false;
126  }
127 
128  $ref = parent::saveObject();
129 
130  $target_obj_id = ilObject::_lookupObjId((int) $_REQUEST['target_id']);
131  $ref->setTargetId($target_obj_id);
132  $ref->update();
133 
134  $ref_id = $ref->getRefId();
135  $parent = $tree->getParentId($ref_id);
136  ilUtil::redirect('repository.php?ref_id='.$parent);
137  }
138 
146  public function editObject()
147  {
148  global $ilUser,$ilSetting,$ilTabs;
149 
150  $ilTabs->setTabActive('edit');
151 
152  $this->initTargetSelection(self::MODE_EDIT);
153 
154  /*
155  $max_entries = $ilSetting->get('search_max_hits',10);
156  if(!count($this->existing_objects = ilUtil::_getObjectsByOperations($this->getTargetType(),'read',$ilUser->getId(),$max_entries)))
157  {
158  // TODO: No Objects with read permission found => send error message
159  return false;
160  }
161 
162  $this->initFormEditSelection();
163  $this->tpl->setContent($this->form->getHTML());
164  return true;
165 
166  */
167  }
168 
176  public function updateObject()
177  {
178  global $ilAccess;
179 
180  if(!(int) $_REQUEST['target_id'])
181  {
182  ilUtil::sendFailure($this->lng->txt('select_one'));
183  $this->editObject();
184  return false;
185  }
186  if(!$ilAccess->checkAccess('read','',(int) $_REQUEST['target_id']))
187  {
188  ilUtil::sendFailure($this->lng->txt('permission_denied'));
189  $this->editObject();
190  return false;
191  }
192  $this->checkPermission('write');
193 
194  $target_obj_id = ilObject::_lookupObjId((int) $_REQUEST['target_id']);
195  $this->object->setTargetId($target_obj_id);
196  $this->object->update();
197 
198  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
199  $this->ctrl->redirect($this,'edit');
200  }
201 
202 
209  protected function showSelection()
210  {
211  $this->initFormSelection();
212  $this->tpl->setContent($this->form->getHTML());
213  return true;
214  }
215 
222  protected function initFormSelection()
223  {
224  if(is_object($this->form))
225  {
226  return true;
227  }
228  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
229  $this->form = new ilPropertyFormGUI();
230  $this->ctrl->setParameter($this,'new_type',$this->getReferenceType());
231  $this->form->setFormAction($this->ctrl->getFormAction($this));
232  $this->form->setTitle($this->lng->txt($this->getReferenceType().'_new'));
233  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getReferenceType().'.gif'));
234 
235  // Show selection
236  $select = new ilSelectInputGUI($this->lng->txt('objs_'.$this->getTargetType()),'target_id');
237  $select->setOptions(self::_prepareSelection($this->existing_objects,$this->getTargetType()));
238  $select->setInfo($this->lng->txt($_POST['new_type'].'_edit_info'));
239  $this->form->addItem($select);
240 
241  $this->form->addCommandButton('save',$this->lng->txt('save'));
242  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
243  }
244 
251  protected function initFormEditSelection()
252  {
253  if(is_object($this->form))
254  {
255  return true;
256  }
257  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
258  $this->form = new ilPropertyFormGUI();
259  $this->form->setFormAction($this->ctrl->getFormAction($this));
260  $this->form->setTitle($this->lng->txt($this->getReferenceType().'_edit'));
261  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getReferenceType().'.gif'));
262 
263  // Show selection
264  $select = new ilSelectInputGUI($this->lng->txt('objs_'.$this->getTargetType()),'target_id');
265  $select->setValue($this->object->getTargetRefId());
266  $select->setOptions(self::_prepareSelection($this->existing_objects,$this->getTargetType()));
267  $select->setInfo($this->lng->txt($this->object->getType().'_edit_info'));
268  $this->form->addItem($select);
269 
270  $this->form->addCommandButton('update',$this->lng->txt('save'));
271  #$this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
272  }
273 
274 
282  public function getTargetType()
283  {
284  return $this->target_type;
285  }
286 
294  public function getReferenceType()
295  {
296  return $this->reference_type;
297  }
298 
307  public static function _prepareSelection($a_ref_ids,$a_target_type)
308  {
309  global $ilDB,$lng;
310 
311  $query = "SELECT obj_data.title obj_title,path_data.title path_title,child FROM tree ".
312  "JOIN object_reference obj_ref ON child = obj_ref.ref_id ".
313  "JOIN object_data obj_data ON obj_ref.obj_id = obj_data.obj_id ".
314  "JOIN object_reference path_ref ON parent = path_ref.ref_id ".
315  "JOIN object_data path_data ON path_ref.obj_id = path_data.obj_id ".
316  "WHERE ".$ilDB->in('child',$a_ref_ids,false,'integer').' '.
317  "ORDER BY obj_data.title ";
318  $res = $ilDB->query($query);
319 
320  $options[0] = $lng->txt('obj_'.$a_target_type.'_select');
321  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
322  {
323  if(strlen($title = $row->obj_title) > 40)
324  {
325  $title = substr($title,0,40).'...';
326  }
327  if(strlen($path = $row->path_title) > 40)
328  {
329  $path = substr($path,0,40).'...';
330  }
331  $options[$row->child] = ($title.' ('.$lng->txt('path').': '.$path.')');
332  }
333  return $options ? $options : array();
334  }
335 
340  protected function initTargetSelection($a_mode = self::MODE_CREATE)
341  {
342  global $ilCtrl, $tree;
343 
344  // empty session on init
345  $_SESSION['ref_repexpand'] = array();
346 
347  // copy opened nodes from repository explorer
348  $_SESSION['ref_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
349 
350  // open current position
351 
352  if($a_mode == self::MODE_CREATE)
353  {
354  $target = (int) $_GET['ref_id'];
355  }
356  else
357  {
358  $target = (int) $this->object->getTargetRefId();
359  }
360 
361  $path = $tree->getPathId($target);
362  foreach((array) $path as $node_id)
363  {
364  if(!in_array($node_id, $_SESSION['ref_repexpand']))
365  {
366  $_SESSION['ref_repexpand'][] = $node_id;
367  }
368  }
369 
370  $_SESSION['ref_mode'] = $a_mode;
371 
373  }
374 
380  {
381  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
382 
383  include_once './Services/ContainerReference/classes/class.ilContainerSelectionExplorer.php';
384 
385  ilUtil::sendInfo($this->lng->txt($this->getReferenceType().'_edit_info'));
386  if($_SESSION['ref_mode'] == self::MODE_CREATE)
387  {
388  $ilToolbar->addButton($this->lng->txt('back'), $ilCtrl->getLinkTarget($this,'cancel'));
389  $this->ctrl->setParameter($this,'new_type',$this->getReferenceType());
390  $cmd = 'save';
391  }
392  else
393  {
394  $ilTabs->setTabActive('edit');
395  $cmd = 'update';
396  }
397  $explorer = new ilContainerSelectionExplorer($this->ctrl->getLinkTarget($this,$cmd));
398 
399  if(isset($_GET['ref_repexpand']))
400  {
401  $explorer->setExpand((int) $_GET['ref_repexpand']);
402  }
403  else
404  {
405  $explorer->setExpand(ROOT_FOLDER_ID);
406  }
407  $explorer->setFrameTarget('_self');
408  $explorer->setExpandTarget($this->ctrl->getLinkTarget($this,'showTargetSelectionTree'));
409  $explorer->setTargetGet('target_id');
410  $explorer->setTargetType($this->getTargetType());
411  $explorer->setOutput(0);
412  $this->tpl->setContent($explorer->getOutput());
413  }
414 }
415 ?>