ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectCopyGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
15 {
16  const SOURCE_SELECTION = 1;
17  const TARGET_SELECTION = 2;
18  const SEARCH_SOURCE = 3;
19 
20  private $mode = 0;
21 
22  private $lng;
23 
24  private $parent_obj = null;
25 
26  private $type = '';
27  private $source = 0;
28  private $target = 0;
29 
30 
35  public function __construct($a_parent_gui)
36  {
37  global $ilCtrl,$lng;
38 
39  $this->lng = $lng;
40  $this->lng->loadLanguageModule('search');
41 
42  $this->parent_obj = $a_parent_gui;
43  }
44 
49  public function executeCommand()
50  {
51  global $ilCtrl;
52 
53  $this->init();
54 
55  $next_class = $ilCtrl->getNextClass($this);
56  $cmd = $ilCtrl->getCmd();
57 
58 
59  switch($next_class)
60  {
61  default:
62  $this->$cmd();
63  break;
64  }
65  }
66 
71  protected function init()
72  {
73  global $ilCtrl;
74 
75  if($_REQUEST['new_type'])
76  {
77  $this->setMode(self::SEARCH_SOURCE);
78 
79  $ilCtrl->setParameter($this,'new_type',$this->getType());
80  $ilCtrl->setParameterByClass(get_class($this->parent_obj), 'new_type', $this->getType());
81  $ilCtrl->setParameterByClass(get_class($this->parent_obj), 'cpfl', 1);
82  $ilCtrl->setReturnByClass(get_class($this->parent_obj), 'create');
83  }
84  elseif($_REQUEST['selectMode'] == self::SOURCE_SELECTION)
85  {
86  $this->setMode(self::SOURCE_SELECTION);
87 
88  $ilCtrl->setParameterByClass(get_class($this->parent_obj), 'selectMode', self::SOURCE_SELECTION);
89  $this->setTarget((int) $_GET['ref_id']);
90  $ilCtrl->setReturnByClass(get_class($this->parent_obj), '');
91  }
92  else
93  {
94  $this->setMode(self::TARGET_SELECTION);
95  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
96 
97  $this->setType(
99  );
100  }
101  }
102 
107  protected function initTargetSelection()
108  {
109  global $ilCtrl, $tree;
110 
111  // empty session on init
112  $_SESSION['paste_copy_repexpand'] = array();
113 
114  // copy opened nodes from repository explorer
115  $_SESSION['paste_copy_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
116 
117  // open current position
118  $path = $tree->getPathId((int)$_GET['source_id']);
119  foreach((array)$path as $node_id)
120  {
121  if(!in_array($node_id, $_SESSION['paste_copy_repexpand']))
122  $_SESSION['paste_copy_repexpand'][] = $node_id;
123  }
124 
125  $this->setMode(self::TARGET_SELECTION);
126  $this->setSource((int) $_GET['source_id']);
127  $this->setTarget(0);
128  $this->setType(
130  );
131 
132  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
133 
134  $this->showTargetSelectionTree();
135  }
136 
141  protected function initSourceSelection()
142  {
143  global $ilCtrl,$tree;
144 
145  // empty session on init
146  $_SESSION['paste_copy_repexpand'] = array();
147 
148  // copy opened nodes from repository explorer
149  $_SESSION['paste_copy_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
150 
151  $this->setMode(self::SOURCE_SELECTION);
152  $this->setSource(0);
153  $this->setTarget((int) $_GET['ref_id']);
154 
155  // open current position
156  $path = $tree->getPathId($this->getTarget());
157  foreach((array) $path as $node_id)
158  {
159  if(!in_array($node_id, $_SESSION['paste_copy_repexpand']))
160  $_SESSION['paste_copy_repexpand'][] = $node_id;
161  }
162 
163  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
164  $this->showSourceSelectionTree();
165  }
166 
171  public function showTargetSelectionTree()
172  {
173  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
174 
175  $this->tpl = $tpl;
176  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html',
177  "Services/Object");
178 
179  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard'));
180 
181  include_once './Services/Object/classes/class.ilPasteIntoMultipleItemsExplorer.php';
184  'ilias.php?baseClass=ilRepositoryGUI&amp;cmd=goto', 'paste_copy_repexpand');
185 
186  // Target selection should check for create permission
187  $required_perm = 'visible';
188  $create_perm = 'create_'.ilObject::_lookupType($this->getSource(), true);
189  if($create_perm)
190  {
191  $required_perm .= (','.$create_perm);
192  }
193  $exp->setRequiredFormItemPermission($required_perm);
194  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showTargetSelectionTree'));
195  $exp->setTargetGet('ref_id');
196  $exp->setPostVar('target');
197  $exp->highlightNode($_GET['ref_id']);
198  $exp->setCheckedItems(array((int) $_POST['target']));
199 
200  // Filter to container
201  foreach(array('cat','root','crs','grp','fold') as $container)
202  {
203  /*
204  if($this->getType() == 'crs' and $container == 'crs')
205  {
206  continue;
207  }
208  */
209  $sub = $objDefinition->getSubObjects($container);
210  if(!isset($sub[$this->getType()]))
211  {
212  $exp->removeFormItemForType($container);
213  }
214  }
215 
216  if($_GET['paste_copy_repexpand'] == '')
217  {
218  $expanded = $tree->readRootId();
219  }
220  else
221  {
222  $expanded = $_GET['paste_copy_repexpand'];
223  }
224 
225  $this->tpl->setVariable('FORM_TARGET', '_self');
226  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
227 
228  $exp->setExpand($expanded);
229  // build html-output
230  $exp->setOutput(0);
231  $output = $exp->getOutput();
232 
233  $this->tpl->setVariable('OBJECT_TREE', $output);
234 
235  $this->tpl->setVariable('CMD_SUBMIT', 'saveTarget');
236 
237  if($objDefinition->isContainer($this->getType()))
238  {
239  $this->tpl->setVariable('TXT_SUBMIT',$this->lng->txt('btn_next'));
240  }
241  else
242  {
243  if(!$objDefinition->isPlugin($this->getType()))
244  {
245  $submit = $this->lng->txt('obj_'.$this->getType().'_duplicate');
246  }
247  else
248  {
249  // get plugin instance
250  include_once "Services/Component/classes/class.ilPlugin.php";
251  $plugin = ilPlugin::getPluginObject(IL_COMP_SERVICE, "Repository", "robj",
252  ilPlugin::lookupNameForId(IL_COMP_SERVICE, "Repository", "robj", $this->getType()));
253  $submit = $plugin->txt('obj_'.$this->getType().'_duplicate');
254  }
255  $this->tpl->setVariable('TXT_SUBMIT', $submit);
256  }
257 
258  $ilToolbar->addButton($this->lng->txt('cancel'), $ilCtrl->getLinkTarget($this,'cancel'));
259  }
260 
265  public function showSourceSelectionTree()
266  {
267  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
268 
269  $this->tpl = $tpl;
270  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html',
271  "Services/Object");
272 
273  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard_source'));
274 
275  include_once './Services/Object/classes/class.ilPasteIntoMultipleItemsExplorer.php';
278  'ilias.php?baseClass=ilRepositoryGUI&amp;cmd=goto', 'paste_copy_repexpand');
279  $exp->setRequiredFormItemPermission('visible,read,copy');
280 
281  $ilCtrl->setParameter($this, 'selectMode', self::SOURCE_SELECTION);
282  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showSourceSelectionTree'));
283  $exp->setTargetGet('ref_id');
284  $exp->setPostVar('source');
285  $exp->setCheckedItems(array($this->getSource()));
286  #$exp->setNotSelectableItems(array($this->getTarget()));
287 
288  // Filter to container
289  foreach(array('cat','root','grp','fold') as $container)
290  {
291  $exp->removeFormItemForType($container);
292  }
293 
294 
295  if($_GET['paste_copy_repexpand'] == '')
296  {
297  $expanded = $tree->readRootId();
298  }
299  else
300  {
301  $expanded = $_GET['paste_copy_repexpand'];
302  }
303 
304  $this->tpl->setVariable('FORM_TARGET', '_self');
305  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
306 
307  $exp->setExpand($expanded);
308  // build html-output
309  $exp->setOutput(0);
310  $output = $exp->getOutput();
311 
312  $this->tpl->setVariable('OBJECT_TREE', $output);
313 
314  $this->tpl->setVariable('CMD_SUBMIT', 'saveSource');
315  $this->tpl->setVariable('TXT_SUBMIT', $this->lng->txt('btn_next'));
316 
317  $ilToolbar->addButton($this->lng->txt('cancel'), $ilCtrl->getLinkTarget($this,'cancel'));
318  }
319 
324  protected function saveTarget()
325  {
326  global $objDefinition;
327 
328  if(isset($_POST['target']))
329  {
330  $this->setTarget((int) $_REQUEST['target']);
331  }
332  else
333  {
334  ilUtil::sendFailure($this->lng->txt('select_one'));
335  $this->showTargetSelectionTree();
336  return false;
337  }
338 
339  if($objDefinition->isContainer($this->getType()))
340  {
341  $this->showItemSelection();
342  }
343  else
344  {
345  $this->copySingleObject();
346  }
347  }
348 
354  public function setMode($a_mode)
355  {
356  $this->mode = $a_mode;
357  }
358 
363  public function getMode()
364  {
365  return $this->mode;
366  }
367 
372  public function getParentObject()
373  {
374  return $this->parent_obj;
375  }
376 
382  public function getType()
383  {
384  return $this->type;
385  }
386 
393  public function setType($type)
394  {
395  $this->type = $type;
396  }
397 
403  public function setSource($a_source_id)
404  {
405  $_SESSION['copy_source'] = $a_source_id;
406  }
407 
412  public function getSource()
413  {
414  return $_SESSION['copy_source'];
415  }
416 
422  public function setTarget($a_target)
423  {
424  $_SESSION['copy_target'] = $a_target;
425  }
426 
431  public function getTarget()
432  {
433  return $_SESSION['copy_target'];
434  }
435 
440  protected function cancel()
441  {
442  global $ilCtrl;
443  $ilCtrl->returnToParent($this);
444  }
445 
450  protected function searchSource()
451  {
452  global $tree,$ilObjDataCache,$lng,$ilCtrl,$tpl;
453 
454  if(isset($_POST['tit']))
455  {
456  ilUtil::sendInfo($this->lng->txt('wizard_search_list'));
457  $_SESSION['source_query'] = $_POST['tit'];
458  }
459  else
460  {
461  $_POST['tit'] = $_SESSION['source_query'];
462  }
463 
464  $this->initFormSearch();
465  $this->form->setValuesByPost();
466 
467  if(!$this->form->checkInput())
468  {
469  ilUtil::sendFailure($lng->txt('msg_no_search_string'),true);
470  $ilCtrl->returnToParent($this);
471  return false;
472  }
473 
474  include_once './Services/Search/classes/class.ilQueryParser.php';
475  $query_parser = new ilQueryParser($this->form->getInput('tit'));
476  $query_parser->setMinWordLength(1,true);
477  $query_parser->setCombination(QP_COMBINATION_AND);
478  $query_parser->parse();
479  if(!$query_parser->validate())
480  {
481  ilUtil::sendFailure($query_parser->getMessage(),true);
482  $ilCtrl->returnToParent($this);
483  }
484 
485  // only like search since fulltext does not support search with less than 3 characters
486  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
487  $object_search = new ilLikeObjectSearch($query_parser);
488  $object_search->setFilter(array($_REQUEST['new_type']));
489  $res = $object_search->performSearch();
490  $res->setRequiredPermission('copy');
491  $res->filter(ROOT_FOLDER_ID,true);
492 
493  if(!count($results = $res->getResultsByObjId()))
494  {
495  ilUtil::sendFailure($this->lng->txt('search_no_match'),true);
496  $ilCtrl->returnToParent($this);
497  }
498 
499  include_once './Services/Object/classes/class.ilObjectCopySearchResultTableGUI.php';
500  $table = new ilObjectCopySearchResultTableGUI($this,'searchSource',$_REQUEST['new_type']);
501  $table->setSelectedReference($this->getSource());
502  $table->parseSearchResults($results);
503  $tpl->setContent($table->getHTML());
504  }
505 
510  protected function saveSource()
511  {
512  global $objDefinition;
513 
514  if(isset($_POST['source']))
515  {
516  $this->setSource($_POST['source']);
517  }
518  else
519  {
520  ilUtil::sendFailure($this->lng->txt('select_one'));
521  $this->searchSource();
522  return false;
523  }
524 
525  if($objDefinition->isContainer($this->getType()))
526  {
527  $this->showItemSelection();
528  }
529  else
530  {
531  $this->copySingleObject();
532  }
533  }
534 
535 
536 
541  protected function showItemSelection()
542  {
543  global $tpl;
544 
545  if(!$this->getSource())
546  {
547  ilUtil::sendFailure($this->lng->txt('select_one'));
548  $this->searchSource();
549  return false;
550  }
551 
552  ilUtil::sendInfo($this->lng->txt($this->getType().'_copy_threads_info'));
553  include_once './Services/Object/classes/class.ilObjectCopySelectionTableGUI.php';
554 
555  $tpl->addJavaScript('./Services/CopyWizard/js/ilContainer.js');
556  $tpl->setVariable('BODY_ATTRIBUTES','onload="ilDisableChilds(\'cmd\');"');
557 
558  switch($this->getMode())
559  {
560  case self::SOURCE_SELECTION:
561  $back_cmd = 'showSourceSelectionTree';
562  break;
563 
564  case self::TARGET_SELECTION:
565  $back_cmd = 'showTargetSelectionTree';
566  break;
567 
568  case self::SEARCH_SOURCE:
569  $back_cmd = 'searchSource';
570  break;
571  }
572 
573  $table = new ilObjectCopySelectionTableGUI($this,'showItemSelection',$this->getType(),$back_cmd);
574  $table->parseSource($this->getSource());
575 
576  $tpl->setContent($table->getHTML());
577  }
578 
583  protected function copySingleObject()
584  {
585  include_once('./Services/Link/classes/class.ilLink.php');
586  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
587 
588  global $ilAccess,$ilErr,$rbacsystem,$ilUser,$ilCtrl,$rbacreview;
589 
590  // Create permission
591  if(!$rbacsystem->checkAccess('create', $this->getTarget(), $this->getType()))
592  {
593  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
594  $ilCtrl->returnToParent($this);
595  }
596  // Source defined
597  if(!$this->getSource())
598  {
599  ilUtil::sendFailure($this->lng->txt('select_one'),true);
600  $ilCtrl->returnToParent($this);
601  }
602  // Copy permission
603  if(!$ilAccess->checkAccess('copy','',$this->getSource()))
604  {
605  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
606  $ilCtrl->returnToParent($this);
607  }
608 
609  // Save wizard options
611  $wizard_options = ilCopyWizardOptions::_getInstance($copy_id);
612  $wizard_options->saveOwner($ilUser->getId());
613  $wizard_options->saveRoot((int) $this->getSource());
614 
615  /*
616  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
617  foreach($options as $source_id => $option)
618  {
619  $wizard_options->addEntry($source_id,$option);
620  }
621  */
622 
623  $wizard_options->read();
624 
625  $orig = ilObjectFactory::getInstanceByRefId((int) $this->getSource());
626  $new_obj = $orig->cloneObject($this->getTarget(),$copy_id);
627 
628  // Delete wizard options
629  $wizard_options->deleteAll();
630 
631  // rbac log
632  include_once "Services/AccessControl/classes/class.ilRbacLog.php";
633  if(ilRbacLog::isActive())
634  {
635  $rbac_log_roles = $rbacreview->getParentRoleIds($new_obj->getRefId(), false);
636  $rbac_log = ilRbacLog::gatherFaPa($new_obj->getRefId(), array_keys($rbac_log_roles), true);
637  ilRbacLog::add(ilRbacLog::COPY_OBJECT, $new_obj->getRefId(), $rbac_log, (int)$this->getSource());
638  }
639 
640  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
641  ilUtil::redirect(ilLink::_getLink($new_obj->getRefId()));
642  }
643 
648  protected function copyContainer()
649  {
650  global $ilLog, $ilCtrl;
651 
652  include_once('./Services/Link/classes/class.ilLink.php');
653  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
654 
655  global $ilAccess,$ilErr,$rbacsystem,$tree,$ilUser,$ilCtrl;
656 
657  // Workaround for course in course copy
658 
660  $source_type = ilObject::_lookupType(ilObject::_lookupObjId($this->getSource()));
661 
662  if($target_type != $source_type or $target_type != 'crs')
663  {
664  if(!$rbacsystem->checkAccess('create', $this->getTarget(),$this->getType()))
665  {
666  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
667  $ilCtrl->returnToParent($this);
668  }
669  }
670  if(!$this->getSource())
671  {
672  ilUtil::sendFailure($this->lng->txt('select_one'),true);
673  $ilCtrl->returnToParent($this);
674  return false;
675  }
676 
677  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
679  $result = $orig->cloneAllObject($_COOKIE['PHPSESSID'], $_COOKIE['ilClientId'], $this->getType(), $this->getTarget(), $this->getSource(), $options);
680 
681  // Check if copy is in progress
682  if ($result == $this->getTarget())
683  {
684  ilUtil::sendInfo($this->lng->txt("object_copy_in_progress"),true);
685  $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id",
686  $this->getTarget());
687  $ilCtrl->redirectByClass("ilrepositorygui", "");
688  }
689  else
690  {
691  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
692  $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id",
693  $result);
694  $ilCtrl->redirectByClass("ilrepositorygui", "");
695  }
696  }
697 
698 
699 
707  public function showSourceSearch($a_tplvar)
708  {
709  global $tpl;
710 
711  if(!$this->sourceExists())
712  {
713  return false;
714  }
715 
716  $this->unsetSession();
717  $this->initFormSearch();
718 
719  if($a_tplvar)
720  {
721  $tpl->setVariable($a_tplvar,$this->form->getHTML());
722  }
723  else
724  {
725  return $this->form;
726  }
727  }
728 
729 
734  protected function sourceExists()
735  {
736  global $ilUser;
737 
738  return (bool) ilUtil::_getObjectsByOperations($this->getType(),'copy',$ilUser->getId(),1);
739  }
740 
745  protected function initFormSearch()
746  {
747  global $lng,$ilCtrl;
748 
749  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
750  $this->form = new ilPropertyFormGUI();
751  $this->form->setTableWidth('600px');
752  $ilCtrl->setParameter($this,'new_type',$this->getType());
753  #$ilCtrl->setParameter($this, 'cp_mode', self::SOURCE_SELECTION);
754  $this->form->setFormAction($ilCtrl->getFormAction($this));
755  $this->form->setTitle($lng->txt($this->getType().'_copy'));
756 
757  $this->form->addCommandButton('searchSource', $lng->txt('btn_next'));
758 
759  $tit = new ilTextInputGUI($lng->txt('title'),'tit');
760  $tit->setSize(40);
761  $tit->setMaxLength(70);
762  $tit->setRequired(true);
763  $tit->setInfo($lng->txt('wizard_title_info'));
764  $this->form->addItem($tit);
765  }
766 
771  protected function unsetSession()
772  {
773  unset($_SESSION['source_query']);
774  $this->setSource(0);
775  }
776 }
777 ?>