ILIAS  Release_4_0_x_branch Revision 61816
 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 {
37  protected $existing_objects = array();
38 
44  public function __construct($a_data, $a_id, $a_call_by_reference = true, $a_prepare_output = true)
45  {
46  parent::__construct($a_data, $a_id,$a_call_by_reference,$a_prepare_output);
47  }
48 
54  public function redirectObject()
55  {
56  ilUtil::redirect('repository.php?ref_id='.$this->object->getTargetRefId());
57  }
58 
64  public function createObject()
65  {
66  global $ilUser,$ilAccess,$ilErr,$ilSetting;
67 
68  $new_type = $_POST["new_type"] ? $_POST["new_type"] : $_GET["new_type"];
69  if(!$ilAccess->checkAccess("create_".$this->getReferenceType(),'',$_GET["ref_id"], $new_type))
70  {
71  $ilErr->raiseError($this->lng->txt("permission_denied"),$ilErr->MESSAGE);
72  }
73 
74  $max_entries = $ilSetting->get('search_max_hits',100);
75  $max_entries = 10000;
76 
77  if(!count($this->existing_objects = ilUtil::_getObjectsByOperations($this->getTargetType(),'read',$ilUser->getId(),$max_entries)))
78  {
79  // TODO: No Objects with read permission found => send error message
80  return false;
81  }
82 
83  if(count($this->existing_objs) >= $max_entries)
84  {
85  return $this->showSearchSelection();
86  }
87  else
88  {
89  return $this->showSelection();
90  }
91  }
92 
93 
101  public function saveObject()
102  {
103  global $ilAccess,$tree;
104 
105  if(!(int) $_POST['target_id'])
106  {
107  ilUtil::sendFailure($this->lng->txt('select_one'));
108  $this->createObject();
109  return false;
110  }
111  if(!$ilAccess->checkAccess('read','',(int) $_POST['target_id']))
112  {
113  ilUtil::sendFailure($this->lng->txt('permission_denied'));
114  $this->createObject();
115  return false;
116  }
117 
118  $ref = parent::saveObject();
119 
120  $target_obj_id = ilObject::_lookupObjId((int) $_POST['target_id']);
121  $ref->setTargetId($target_obj_id);
122  $ref->update();
123 
124  $ref_id = $ref->getRefId();
125  $parent = $tree->getParentId($ref_id);
126  ilUtil::redirect('repository.php?ref_id='.$parent);
127  }
128 
136  public function editObject()
137  {
138  global $ilUser,$ilSetting,$ilTabs;
139 
140  $ilTabs->setTabActive('edit');
141 
142  $max_entries = $ilSetting->get('search_max_hits',10000);
143  $max_entries = 10000;
144  if(!count($this->existing_objects = ilUtil::_getObjectsByOperations($this->getTargetType(),'read',$ilUser->getId(),$max_entries)))
145  {
146  // TODO: No Objects with read permission found => send error message
147  return false;
148  }
149 
150  $this->initFormEditSelection();
151  $this->tpl->setContent($this->form->getHTML());
152  return true;
153  }
154 
162  public function updateObject()
163  {
164  global $ilAccess;
165 
166  if(!(int) $_POST['target_id'])
167  {
168  ilUtil::sendFailure($this->lng->txt('select_one'));
169  $this->editObject();
170  return false;
171  }
172  if(!$ilAccess->checkAccess('edit','',(int) $_POST['target_id']))
173  {
174  ilUtil::sendFailure($this->lng->txt('permission_denied'));
175  $this->editObject();
176  return false;
177  }
178 
179  $target_obj_id = ilObject::_lookupObjId((int) $_POST['target_id']);
180  $this->object->setTargetId($target_obj_id);
181  $this->object->update();
182 
183  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
184  $this->editObject();
185  }
186 
187 
194  protected function showSelection()
195  {
196  $this->initFormSelection();
197  $this->tpl->setContent($this->form->getHTML());
198  return true;
199  }
200 
207  protected function initFormSelection()
208  {
209  if(is_object($this->form))
210  {
211  return true;
212  }
213  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
214  $this->form = new ilPropertyFormGUI();
215  $this->ctrl->setParameter($this,'new_type',$this->getReferenceType());
216  $this->form->setFormAction($this->ctrl->getFormAction($this));
217  $this->form->setTitle($this->lng->txt($this->getReferenceType().'_new'));
218  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getReferenceType().'.gif'));
219 
220  // Show selection
221  $select = new ilSelectInputGUI($this->lng->txt('objs_'.$this->getTargetType()),'target_id');
222  $select->setOptions(self::_prepareSelection($this->existing_objects,$this->getTargetType()));
223  $select->setInfo($this->lng->txt($_POST['new_type'].'_edit_info'));
224  $this->form->addItem($select);
225 
226  $this->form->addCommandButton('save',$this->lng->txt('save'));
227  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
228  }
229 
236  protected function initFormEditSelection()
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->form->setFormAction($this->ctrl->getFormAction($this));
245  $this->form->setTitle($this->lng->txt($this->getReferenceType().'_edit'));
246  $this->form->setTitleIcon(ilUtil::getImagePath('icon_'.$this->getReferenceType().'.gif'));
247 
248  // Show selection
249  $select = new ilSelectInputGUI($this->lng->txt('objs_'.$this->getTargetType()),'target_id');
250  $select->setValue($this->object->getTargetRefId());
251  $select->setOptions(self::_prepareSelection($this->existing_objects,$this->getTargetType()));
252  $select->setInfo($this->lng->txt($this->object->getType().'_edit_info'));
253  $this->form->addItem($select);
254 
255  $this->form->addCommandButton('update',$this->lng->txt('save'));
256  #$this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
257  }
258 
266  public function getTargetType()
267  {
268  return $this->target_type;
269  }
270 
278  public function getReferenceType()
279  {
280  return $this->reference_type;
281  }
282 
291  public static function _prepareSelection($a_ref_ids,$a_target_type)
292  {
293  global $ilDB,$lng;
294 
295  $query = "SELECT obj_data.title obj_title,path_data.title path_title,child FROM tree ".
296  "JOIN object_reference obj_ref ON child = obj_ref.ref_id ".
297  "JOIN object_data obj_data ON obj_ref.obj_id = obj_data.obj_id ".
298  "JOIN object_reference path_ref ON parent = path_ref.ref_id ".
299  "JOIN object_data path_data ON path_ref.obj_id = path_data.obj_id ".
300  "WHERE ".$ilDB->in('child',$a_ref_ids,false,'integer').' '.
301  "ORDER BY obj_data.title ";
302  $res = $ilDB->query($query);
303 
304  $options[0] = $lng->txt('obj_'.$a_target_type.'_select');
305  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
306  {
307  if(strlen($title = $row->obj_title) > 40)
308  {
309  $title = substr($title,0,40).'...';
310  }
311  if(strlen($path = $row->path_title) > 40)
312  {
313  $path = substr($path,0,40).'...';
314  }
315  $options[$row->child] = ($title.' ('.$lng->txt('path').': '.$path.')');
316  }
317  return $options ? $options : array();
318  }
319 
320 }
321 ?>