ILIAS  Release_4_2_x_branch Revision 61807
 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 
178  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard'));
179 
180  include_once 'classes/class.ilPasteIntoMultipleItemsExplorer.php';
183  'repository.php?cmd=goto', 'paste_copy_repexpand');
184  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showTargetSelectionTree'));
185  $exp->setTargetGet('ref_id');
186  $exp->setPostVar('target');
187  $exp->setCheckedItems(array((int) $_POST['target']));
188 
189  // Filter to container
190  foreach(array('cat','root','crs','grp','fold') as $container)
191  {
192  /*
193  if($this->getType() == 'crs' and $container == 'crs')
194  {
195  continue;
196  }
197  */
198  $sub = $objDefinition->getSubObjects($container);
199  if(!isset($sub[$this->getType()]))
200  {
201  $exp->removeFormItemForType($container);
202  }
203  }
204 
205  if($_GET['paste_copy_repexpand'] == '')
206  {
207  $expanded = $tree->readRootId();
208  }
209  else
210  {
211  $expanded = $_GET['paste_copy_repexpand'];
212  }
213 
214  $this->tpl->setVariable('FORM_TARGET', '_self');
215  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
216 
217  $exp->setExpand($expanded);
218  // build html-output
219  $exp->setOutput(0);
220  $output = $exp->getOutput();
221 
222  $this->tpl->setVariable('OBJECT_TREE', $output);
223 
224  $this->tpl->setVariable('CMD_SUBMIT', 'saveTarget');
225 
226  if($objDefinition->isContainer($this->getType()))
227  {
228  $this->tpl->setVariable('TXT_SUBMIT',$this->lng->txt('btn_next'));
229  }
230  else
231  {
232  if(!$objDefinition->isPlugin($this->getType()))
233  {
234  $submit = $this->lng->txt('obj_'.$this->getType().'_duplicate');
235  }
236  else
237  {
238  // get plugin instance
239  include_once "Services/Component/classes/class.ilPlugin.php";
240  $plugin = ilPlugin::getPluginObject(IL_COMP_SERVICE, "Repository", "robj",
241  ilPlugin::lookupNameForId(IL_COMP_SERVICE, "Repository", "robj", $this->getType()));
242  $submit = $plugin->txt('obj_'.$this->getType().'_duplicate');
243  }
244  $this->tpl->setVariable('TXT_SUBMIT', $submit);
245  }
246 
247  $ilToolbar->addButton($this->lng->txt('cancel'), $ilCtrl->getLinkTarget($this,'cancel'));
248  }
249 
254  public function showSourceSelectionTree()
255  {
256  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
257 
258  $this->tpl = $tpl;
259  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html');
260 
261  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard_source'));
262 
263  include_once 'classes/class.ilPasteIntoMultipleItemsExplorer.php';
266  'repository.php?cmd=goto', 'paste_copy_repexpand');
267 
268  $ilCtrl->setParameter($this, 'selectMode', self::SOURCE_SELECTION);
269  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showSourceSelectionTree'));
270  $exp->setTargetGet('ref_id');
271  $exp->setPostVar('source');
272  $exp->setCheckedItems(array($this->getSource()));
273  #$exp->setNotSelectableItems(array($this->getTarget()));
274 
275  // Filter to container
276  foreach(array('cat','root','grp','fold') as $container)
277  {
278  $exp->removeFormItemForType($container);
279  }
280 
281 
282  if($_GET['paste_copy_repexpand'] == '')
283  {
284  $expanded = $tree->readRootId();
285  }
286  else
287  {
288  $expanded = $_GET['paste_copy_repexpand'];
289  }
290 
291  $this->tpl->setVariable('FORM_TARGET', '_self');
292  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
293 
294  $exp->setExpand($expanded);
295  // build html-output
296  $exp->setOutput(0);
297  $output = $exp->getOutput();
298 
299  $this->tpl->setVariable('OBJECT_TREE', $output);
300 
301  $this->tpl->setVariable('CMD_SUBMIT', 'saveSource');
302  $this->tpl->setVariable('TXT_SUBMIT', $this->lng->txt('btn_next'));
303 
304  $ilToolbar->addButton($this->lng->txt('cancel'), $ilCtrl->getLinkTarget($this,'cancel'));
305  }
306 
311  protected function saveTarget()
312  {
313  global $objDefinition;
314 
315  if(isset($_POST['target']))
316  {
317  $this->setTarget((int) $_REQUEST['target']);
318  }
319  else
320  {
321  ilUtil::sendFailure($this->lng->txt('select_one'));
322  $this->showTargetSelectionTree();
323  return false;
324  }
325 
326  if($objDefinition->isContainer($this->getType()))
327  {
328  $this->showItemSelection();
329  }
330  else
331  {
332  $this->copySingleObject();
333  }
334  }
335 
341  public function setMode($a_mode)
342  {
343  $this->mode = $a_mode;
344  }
345 
350  public function getMode()
351  {
352  return $this->mode;
353  }
354 
359  public function getParentObject()
360  {
361  return $this->parent_obj;
362  }
363 
369  public function getType()
370  {
371  return $this->type;
372  }
373 
380  public function setType($type)
381  {
382  $this->type = $type;
383  }
384 
390  public function setSource($a_source_id)
391  {
392  $_SESSION['copy_source'] = $a_source_id;
393  }
394 
399  public function getSource()
400  {
401  return $_SESSION['copy_source'];
402  }
403 
409  public function setTarget($a_target)
410  {
411  $_SESSION['copy_target'] = $a_target;
412  }
413 
418  public function getTarget()
419  {
420  return $_SESSION['copy_target'];
421  }
422 
427  protected function cancel()
428  {
429  global $ilCtrl;
430  $ilCtrl->returnToParent($this);
431  }
432 
437  protected function searchSource()
438  {
439  global $tree,$ilObjDataCache,$lng,$ilCtrl,$tpl;
440 
441  if(isset($_POST['tit']))
442  {
443  ilUtil::sendInfo($this->lng->txt('wizard_search_list'));
444  $_SESSION['source_query'] = $_POST['tit'];
445  }
446  else
447  {
448  $_POST['tit'] = $_SESSION['source_query'];
449  }
450 
451  $this->initFormSearch();
452  $this->form->setValuesByPost();
453 
454  if(!$this->form->checkInput())
455  {
456  ilUtil::sendFailure($lng->txt('msg_no_search_string'),true);
457  $ilCtrl->returnToParent($this);
458  return false;
459  }
460 
461  include_once './Services/Search/classes/class.ilQueryParser.php';
462  $query_parser = new ilQueryParser($this->form->getInput('tit'));
463  $query_parser->setMinWordLength(1,true);
464  $query_parser->setCombination(QP_COMBINATION_AND);
465  $query_parser->parse();
466  if(!$query_parser->validate())
467  {
468  ilUtil::sendFailure($query_parser->getMessage(),true);
469  $ilCtrl->returnToParent($this);
470  }
471 
472  // only like search since fulltext does not support search with less than 3 characters
473  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
474  $object_search = new ilLikeObjectSearch($query_parser);
475  $object_search->setFilter(array($_REQUEST['new_type']));
476  $res = $object_search->performSearch();
477  $res->setRequiredPermission('copy');
478  $res->filter(ROOT_FOLDER_ID,true);
479 
480  if(!count($results = $res->getResultsByObjId()))
481  {
482  ilUtil::sendFailure($this->lng->txt('search_no_match'),true);
483  $ilCtrl->returnToParent($this);
484  }
485 
486  include_once './Services/Object/classes/class.ilObjectCopySearchResultTableGUI.php';
487  $table = new ilObjectCopySearchResultTableGUI($this,'searchSource',$_REQUEST['new_type']);
488  $table->setSelectedReference($this->getSource());
489  $table->parseSearchResults($results);
490  $tpl->setContent($table->getHTML());
491  }
492 
497  protected function saveSource()
498  {
499  global $objDefinition;
500 
501  if(isset($_POST['source']))
502  {
503  $this->setSource($_POST['source']);
504  }
505  else
506  {
507  ilUtil::sendFailure($this->lng->txt('select_one'));
508  $this->searchSource();
509  return false;
510  }
511 
512  if($objDefinition->isContainer($this->getType()))
513  {
514  $this->showItemSelection();
515  }
516  else
517  {
518  $this->copySingleObject();
519  }
520  }
521 
522 
523 
528  protected function showItemSelection()
529  {
530  global $tpl;
531 
532  if(!$this->getSource())
533  {
534  ilUtil::sendFailure($this->lng->txt('select_one'));
535  $this->searchSource();
536  return false;
537  }
538 
539  ilUtil::sendInfo($this->lng->txt($this->getType().'_copy_threads_info'));
540  include_once './Services/Object/classes/class.ilObjectCopySelectionTableGUI.php';
541 
542  $tpl->addJavaScript('./Services/CopyWizard/js/ilContainer.js');
543  $tpl->setVariable('BODY_ATTRIBUTES','onload="ilDisableChilds(\'cmd\');"');
544 
545  switch($this->getMode())
546  {
547  case self::SOURCE_SELECTION:
548  $back_cmd = 'showSourceSelectionTree';
549  break;
550 
551  case self::TARGET_SELECTION:
552  $back_cmd = 'showTargetSelectionTree';
553  break;
554 
555  case self::SEARCH_SOURCE:
556  $back_cmd = 'searchSource';
557  break;
558  }
559 
560  $table = new ilObjectCopySelectionTableGUI($this,'showItemSelection',$this->getType(),$back_cmd);
561  $table->parseSource($this->getSource());
562 
563  $tpl->setContent($table->getHTML());
564  }
565 
570  protected function copySingleObject()
571  {
572  include_once('classes/class.ilLink.php');
573  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
574 
575  global $ilAccess,$ilErr,$rbacsystem,$ilUser,$ilCtrl,$rbacreview;
576 
577  // Create permission
578  if(!$rbacsystem->checkAccess('create', $this->getTarget(), $this->getType()))
579  {
580  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
581  $ilCtrl->returnToParent($this);
582  }
583  // Source defined
584  if(!$this->getSource())
585  {
586  ilUtil::sendFailure($this->lng->txt('select_one'),true);
587  $ilCtrl->returnToParent($this);
588  }
589  // Copy permission
590  if(!$ilAccess->checkAccess('copy','',$this->getSource()))
591  {
592  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
593  $ilCtrl->returnToParent($this);
594  }
595 
596  // Save wizard options
598  $wizard_options = ilCopyWizardOptions::_getInstance($copy_id);
599  $wizard_options->saveOwner($ilUser->getId());
600  $wizard_options->saveRoot((int) $this->getSource());
601 
602  /*
603  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
604  foreach($options as $source_id => $option)
605  {
606  $wizard_options->addEntry($source_id,$option);
607  }
608  */
609 
610  $wizard_options->read();
611 
612  $orig = ilObjectFactory::getInstanceByRefId((int) $this->getSource());
613  $new_obj = $orig->cloneObject($this->getTarget(),$copy_id);
614 
615  // Delete wizard options
616  $wizard_options->deleteAll();
617 
618  // rbac log
619  include_once "Services/AccessControl/classes/class.ilRbacLog.php";
620  if(ilRbacLog::isActive())
621  {
622  $rbac_log_roles = $rbacreview->getParentRoleIds($new_obj->getRefId(), false);
623  $rbac_log = ilRbacLog::gatherFaPa($new_obj->getRefId(), array_keys($rbac_log_roles), true);
624  ilRbacLog::add(ilRbacLog::COPY_OBJECT, $new_obj->getRefId(), $rbac_log, (int)$this->getSource());
625  }
626 
627  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
628  ilUtil::redirect(ilLink::_getLink($new_obj->getRefId()));
629  }
630 
635  protected function copyContainer()
636  {
637  global $ilLog;
638 
639  include_once('classes/class.ilLink.php');
640  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
641 
642  global $ilAccess,$ilErr,$rbacsystem,$tree,$ilUser,$ilCtrl;
643 
644  // Workaround for course in course copy
645 
647  $source_type = ilObject::_lookupType(ilObject::_lookupObjId($this->getSource()));
648 
649  if($target_type != $source_type or $target_type != 'crs')
650  {
651  if(!$rbacsystem->checkAccess('create', $this->getTarget(),$this->getType()))
652  {
653  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
654  $ilCtrl->returnToParent($this);
655  }
656  }
657  if(!$this->getSource())
658  {
659  ilUtil::sendFailure($this->lng->txt('select_one'),true);
660  $ilCtrl->returnToParent($this);
661  return false;
662  }
663 
664  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
666  $result = $orig->cloneAllObject($_COOKIE['PHPSESSID'], $_COOKIE['ilClientId'], $this->getType(), $this->getTarget(), $this->getSource(), $options);
667 
668  // Check if copy is in progress
669  if ($result == $this->getTarget())
670  {
671  ilUtil::sendInfo($this->lng->txt("object_copy_in_progress"),true);
672  ilUtil::redirect('repository.php?ref_id='.$this->getTarget());
673  }
674  else
675  {
676  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
677  ilUtil::redirect('repository.php?ref_id='.$result);
678  }
679  }
680 
681 
682 
690  public function showSourceSearch($a_tplvar)
691  {
692  global $tpl;
693 
694  if(!$this->sourceExists())
695  {
696  return false;
697  }
698 
699  $this->unsetSession();
700  $this->initFormSearch();
701 
702  if($a_tplvar)
703  {
704  $tpl->setVariable($a_tplvar,$this->form->getHTML());
705  }
706  else
707  {
708  return $this->form;
709  }
710  }
711 
712 
717  protected function sourceExists()
718  {
719  global $ilUser;
720 
721  return (bool) ilUtil::_getObjectsByOperations($this->getType(),'copy',$ilUser->getId(),1);
722  }
723 
728  protected function initFormSearch()
729  {
730  global $lng,$ilCtrl;
731 
732  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
733  $this->form = new ilPropertyFormGUI();
734  $this->form->setTableWidth('600px');
735  $ilCtrl->setParameter($this,'new_type',$this->getType());
736  #$ilCtrl->setParameter($this, 'cp_mode', self::SOURCE_SELECTION);
737  $this->form->setFormAction($ilCtrl->getFormAction($this));
738  $this->form->setTitle($lng->txt($this->getType().'_copy'));
739 
740  $this->form->addCommandButton('searchSource', $lng->txt('btn_next'));
741 
742  $tit = new ilTextInputGUI($lng->txt('title'),'tit');
743  $tit->setSize(40);
744  $tit->setMaxLength(70);
745  $tit->setRequired(true);
746  $tit->setInfo($lng->txt('wizard_title_info'));
747  $this->form->addItem($tit);
748  }
749 
754  protected function unsetSession()
755  {
756  unset($_SESSION['source_query']);
757  $this->setSource(0);
758  }
759 }
760 ?>