ILIAS  Release_4_1_x_branch Revision 61804
 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->setReturnByClass(get_class($this->parent_obj), 'create');
82  }
83  elseif($_REQUEST['selectMode'] == self::SOURCE_SELECTION)
84  {
85  $this->setMode(self::SOURCE_SELECTION);
86 
87  $ilCtrl->setParameterByClass(get_class($this->parent_obj), 'selectMode', self::SOURCE_SELECTION);
88  $this->setTarget((int) $_GET['ref_id']);
89  $ilCtrl->setReturnByClass(get_class($this->parent_obj), '');
90  }
91  else
92  {
93  $this->setMode(self::TARGET_SELECTION);
94  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
95 
96  $this->setType(
98  );
99  }
100  }
101 
106  protected function initTargetSelection()
107  {
108  global $ilCtrl, $tree;
109 
110  // empty session on init
111  $_SESSION['paste_copy_repexpand'] = array();
112 
113  // copy opened nodes from repository explorer
114  $_SESSION['paste_copy_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
115 
116  // open current position
117  $path = $tree->getPathId((int)$_GET['source_id']);
118  foreach((array)$path as $node_id)
119  {
120  if(!in_array($node_id, $_SESSION['paste_copy_repexpand']))
121  $_SESSION['paste_copy_repexpand'][] = $node_id;
122  }
123 
124  $this->setMode(self::TARGET_SELECTION);
125  $this->setSource((int) $_GET['source_id']);
126  $this->setTarget(0);
127  $this->setType(
129  );
130 
131  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
132 
133  $this->showTargetSelectionTree();
134  }
135 
140  protected function initSourceSelection()
141  {
142  global $ilCtrl,$tree;
143 
144  // empty session on init
145  $_SESSION['paste_copy_repexpand'] = array();
146 
147  // copy opened nodes from repository explorer
148  $_SESSION['paste_copy_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
149 
150  $this->setMode(self::SOURCE_SELECTION);
151  $this->setSource(0);
152  $this->setTarget((int) $_GET['ref_id']);
153 
154  // open current position
155  $path = $tree->getPathId($this->getTarget());
156  foreach((array) $path as $node_id)
157  {
158  if(!in_array($node_id, $_SESSION['paste_copy_repexpand']))
159  $_SESSION['paste_copy_repexpand'][] = $node_id;
160  }
161 
162  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
163  $this->showSourceSelectionTree();
164  }
165 
170  public function showTargetSelectionTree()
171  {
172  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
173 
174  $this->tpl = $tpl;
175  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html');
176 
177  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard'));
178 
179  include_once 'classes/class.ilPasteIntoMultipleItemsExplorer.php';
182  'repository.php?cmd=goto', 'paste_copy_repexpand');
183  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showTargetSelectionTree'));
184  $exp->setTargetGet('ref_id');
185  $exp->setPostVar('target');
186  $exp->setCheckedItems(array((int) $_POST['target']));
187 
188  // Filter to container
189  foreach(array('cat','root','crs','grp','fold') as $container)
190  {
191  /*
192  if($this->getType() == 'crs' and $container == 'crs')
193  {
194  continue;
195  }
196  */
197  $sub = $objDefinition->getSubObjects($container);
198  if(!isset($sub[$this->getType()]))
199  {
200  $exp->removeFormItemForType($container);
201  }
202  }
203 
204  if($_GET['paste_copy_repexpand'] == '')
205  {
206  $expanded = $tree->readRootId();
207  }
208  else
209  {
210  $expanded = $_GET['paste_copy_repexpand'];
211  }
212 
213  $this->tpl->setVariable('FORM_TARGET', '_self');
214  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
215 
216  $exp->setExpand($expanded);
217  // build html-output
218  $exp->setOutput(0);
219  $output = $exp->getOutput();
220 
221  $this->tpl->setVariable('OBJECT_TREE', $output);
222 
223  $this->tpl->setVariable('CMD_SUBMIT', 'saveTarget');
224 
225  if($objDefinition->isContainer($this->getType()))
226  {
227  $this->tpl->setVariable('TXT_SUBMIT',$this->lng->txt('btn_next'));
228  }
229  else
230  {
231  $this->tpl->setVariable('TXT_SUBMIT', $this->lng->txt('obj_'.$this->getType().'_duplicate'));
232  }
233 
234  $ilToolbar->addButton($this->lng->txt('back'), $ilCtrl->getLinkTarget($this,'cancel'));
235  }
236 
241  public function showSourceSelectionTree()
242  {
243  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
244 
245  $this->tpl = $tpl;
246  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html');
247 
248  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard_source'));
249 
250  include_once 'classes/class.ilPasteIntoMultipleItemsExplorer.php';
253  'repository.php?cmd=goto', 'paste_copy_repexpand');
254 
255  $ilCtrl->setParameter($this, 'selectMode', self::SOURCE_SELECTION);
256  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showSourceSelectionTree'));
257  $exp->setTargetGet('ref_id');
258  $exp->setPostVar('source');
259  $exp->setCheckedItems(array($this->getSource()));
260  $exp->setNotSelectableItems(array($this->getTarget()));
261 
262  // Filter to container
263  foreach(array('cat','root','grp','fold') as $container)
264  {
265  $exp->removeFormItemForType($container);
266  }
267 
268 
269  if($_GET['paste_copy_repexpand'] == '')
270  {
271  $expanded = $tree->readRootId();
272  }
273  else
274  {
275  $expanded = $_GET['paste_copy_repexpand'];
276  }
277 
278  $this->tpl->setVariable('FORM_TARGET', '_self');
279  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
280 
281  $exp->setExpand($expanded);
282  // build html-output
283  $exp->setOutput(0);
284  $output = $exp->getOutput();
285 
286  $this->tpl->setVariable('OBJECT_TREE', $output);
287 
288  $this->tpl->setVariable('CMD_SUBMIT', 'saveSource');
289  $this->tpl->setVariable('TXT_SUBMIT', $this->lng->txt('btn_next'));
290 
291  $ilToolbar->addButton($this->lng->txt('back'), $ilCtrl->getLinkTarget($this,'cancel'));
292  }
293 
298  protected function saveTarget()
299  {
300  global $objDefinition;
301 
302  if(isset($_POST['target']))
303  {
304  $this->setTarget((int) $_REQUEST['target']);
305  }
306  else
307  {
308  ilUtil::sendFailure($this->lng->txt('select_one'));
309  $this->showTargetSelectionTree();
310  return false;
311  }
312 
313  if($objDefinition->isContainer($this->getType()))
314  {
315  $this->showItemSelection();
316  }
317  else
318  {
319  $this->copySingleObject();
320  }
321  }
322 
328  public function setMode($a_mode)
329  {
330  $this->mode = $a_mode;
331  }
332 
337  public function getMode()
338  {
339  return $this->mode;
340  }
341 
346  public function getParentObject()
347  {
348  return $this->parent_obj;
349  }
350 
356  public function getType()
357  {
358  return $this->type;
359  }
360 
367  public function setType($type)
368  {
369  $this->type = $type;
370  }
371 
377  public function setSource($a_source_id)
378  {
379  $_SESSION['copy_source'] = $a_source_id;
380  }
381 
386  public function getSource()
387  {
388  return $_SESSION['copy_source'];
389  }
390 
396  public function setTarget($a_target)
397  {
398  $_SESSION['copy_target'] = $a_target;
399  }
400 
405  public function getTarget()
406  {
407  return $_SESSION['copy_target'];
408  }
409 
414  protected function cancel()
415  {
416  global $ilCtrl;
417  $ilCtrl->returnToParent($this);
418  }
419 
424  protected function searchSource()
425  {
426  global $tree,$ilObjDataCache,$lng,$ilCtrl,$tpl;
427 
428  if(isset($_POST['tit']))
429  {
430  ilUtil::sendInfo($this->lng->txt('wizard_search_list'));
431  $_SESSION['source_query'] = $_POST['tit'];
432  }
433  else
434  {
435  $_POST['tit'] = $_SESSION['source_query'];
436  }
437 
438  $this->initFormSearch();
439  $this->form->setValuesByPost();
440 
441  if(!$this->form->checkInput())
442  {
443  ilUtil::sendFailure($lng->txt('msg_no_search_string'),true);
444  $ilCtrl->returnToParent($this);
445  return false;
446  }
447 
448  include_once './Services/Search/classes/class.ilQueryParser.php';
449  $query_parser = new ilQueryParser($this->form->getInput('tit'));
450  $query_parser->setMinWordLength(1,true);
451  $query_parser->setCombination(QP_COMBINATION_AND);
452  $query_parser->parse();
453  if(!$query_parser->validate())
454  {
455  ilUtil::sendFailure($query_parser->getMessage(),true);
456  $ilCtrl->returnToParent($this);
457  }
458 
459  // only like search since fulltext does not support search with less than 3 characters
460  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
461  $object_search = new ilLikeObjectSearch($query_parser);
462  $object_search->setFilter(array($_REQUEST['new_type']));
463  $res = $object_search->performSearch();
464  $res->setRequiredPermission('copy');
465  $res->filter(ROOT_FOLDER_ID,true);
466 
467  if(!count($results = $res->getResultsByObjId()))
468  {
469  ilUtil::sendFailure($this->lng->txt('search_no_match'),true);
470  $ilCtrl->returnToParent($this);
471  }
472 
473  include_once './Services/Object/classes/class.ilObjectCopySearchResultTableGUI.php';
474  $table = new ilObjectCopySearchResultTableGUI($this,'searchSource',$_REQUEST['new_type']);
475  $table->setSelectedReference($this->getSource());
476  $table->parseSearchResults($results);
477  $tpl->setContent($table->getHTML());
478  }
479 
484  protected function saveSource()
485  {
486  global $objDefinition;
487 
488  if(isset($_POST['source']))
489  {
490  $this->setSource($_POST['source']);
491  }
492  else
493  {
494  ilUtil::sendFailure($this->lng->txt('select_one'));
495  $this->searchSource();
496  return false;
497  }
498 
499  if($objDefinition->isContainer($this->getType()))
500  {
501  $this->showItemSelection();
502  }
503  else
504  {
505  $this->copySingleObject();
506  }
507  }
508 
509 
510 
515  protected function showItemSelection()
516  {
517  global $tpl;
518 
519  if(!$this->getSource())
520  {
521  ilUtil::sendFailure($this->lng->txt('select_one'));
522  $this->searchSource();
523  return false;
524  }
525 
526  ilUtil::sendInfo($this->lng->txt($this->getType().'_copy_threads_info'));
527  include_once './Services/Object/classes/class.ilObjectCopySelectionTableGUI.php';
528 
529  $tpl->addJavaScript('./Services/CopyWizard/js/ilContainer.js');
530  $tpl->setVariable('BODY_ATTRIBUTES','onload="ilDisableChilds(\'cmd\');"');
531 
532  switch($this->getMode())
533  {
534  case self::SOURCE_SELECTION:
535  $back_cmd = 'showSourceSelectionTree';
536  break;
537 
538  case self::TARGET_SELECTION:
539  $back_cmd = 'showTargetSelectionTree';
540  break;
541 
542  case self::SEARCH_SOURCE:
543  $back_cmd = 'searchSource';
544  break;
545  }
546 
547  $table = new ilObjectCopySelectionTableGUI($this,'showItemSelection',$this->getType(),$back_cmd);
548  $table->parseSource($this->getSource());
549 
550  $tpl->setContent($table->getHTML());
551  }
552 
557  protected function copySingleObject()
558  {
559  include_once('classes/class.ilLink.php');
560  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
561 
562  global $ilAccess,$ilErr,$rbacsystem,$ilUser,$ilCtrl,$rbacreview;
563 
564  // Create permission
565  if(!$rbacsystem->checkAccess('create', $this->getTarget(), $this->getType()))
566  {
567  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
568  $ilCtrl->returnToParent($this);
569  }
570  // Source defined
571  if(!$this->getSource())
572  {
573  ilUtil::sendFailure($this->lng->txt('select_one'),true);
574  $ilCtrl->returnToParent($this);
575  }
576  // Copy permission
577  if(!$ilAccess->checkAccess('copy','',$this->getSource()))
578  {
579  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
580  $ilCtrl->returnToParent($this);
581  }
582 
583  // Save wizard options
585  $wizard_options = ilCopyWizardOptions::_getInstance($copy_id);
586  $wizard_options->saveOwner($ilUser->getId());
587  $wizard_options->saveRoot((int) $this->getSource());
588 
589  /*
590  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
591  foreach($options as $source_id => $option)
592  {
593  $wizard_options->addEntry($source_id,$option);
594  }
595  */
596 
597  $wizard_options->read();
598 
599  $orig = ilObjectFactory::getInstanceByRefId((int) $this->getSource());
600  $new_obj = $orig->cloneObject($this->getTarget(),$copy_id);
601 
602  // Delete wizard options
603  $wizard_options->deleteAll();
604 
605  // rbac log
606  include_once "Services/AccessControl/classes/class.ilRbacLog.php";
607  if(ilRbacLog::isActive())
608  {
609  $rbac_log_roles = $rbacreview->getParentRoleIds($new_obj->getRefId(), false);
610  $rbac_log = ilRbacLog::gatherFaPa($new_obj->getRefId(), array_keys($rbac_log_roles));
611  ilRbacLog::add(ilRbacLog::COPY_OBJECT, $new_obj->getRefId(), $rbac_log, (int)$this->getSource());
612  }
613 
614  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
615  ilUtil::redirect(ilLink::_getLink($new_obj->getRefId()));
616  }
617 
622  protected function copyContainer()
623  {
624  global $ilLog;
625 
626  include_once('classes/class.ilLink.php');
627  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
628 
629  global $ilAccess,$ilErr,$rbacsystem,$tree,$ilUser,$ilCtrl;
630 
631  // Workaround for course in course copy
632 
634  $source_type = ilObject::_lookupType(ilObject::_lookupObjId($this->getSource()));
635 
636  if($target_type != $source_type or $target_type != 'crs')
637  {
638  if(!$rbacsystem->checkAccess('create', $this->getTarget(),$this->getType()))
639  {
640  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
641  $ilCtrl->returnToParent($this);
642  }
643  }
644  if(!$this->getSource())
645  {
646  ilUtil::sendFailure($this->lng->txt('select_one'),true);
647  $ilCtrl->returnToParent($this);
648  return false;
649  }
650 
651  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
653  $result = $orig->cloneAllObject($_COOKIE['PHPSESSID'], $_COOKIE['ilClientId'], $this->getType(), $this->getTarget(), $this->getSource(), $options);
654 
655  // Check if copy is in progress
656  if ($result == $this->getTarget())
657  {
658  ilUtil::sendInfo($this->lng->txt("object_copy_in_progress"),true);
659  ilUtil::redirect('repository.php?ref_id='.$this->getTarget());
660  }
661  else
662  {
663  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
664  ilUtil::redirect('repository.php?ref_id='.$result);
665  }
666  }
667 
668 
669 
677  public function showSourceSearch($a_tplvar)
678  {
679  global $tpl;
680 
681  if(!$this->sourceExists())
682  {
683  return false;
684  }
685 
686  $this->unsetSession();
687 
688  $this->initFormSearch();
689  $tpl->setVariable($a_tplvar,$this->form->getHTML());
690  }
691 
692 
697  protected function sourceExists()
698  {
699  global $ilUser;
700 
701  return (bool) ilUtil::_getObjectsByOperations($this->getType(),'copy',$ilUser->getId(),1);
702  }
703 
708  protected function initFormSearch()
709  {
710  global $lng,$ilCtrl;
711 
712  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
713  $this->form = new ilPropertyFormGUI();
714  $this->form->setTableWidth('600px');
715  $ilCtrl->setParameter($this,'new_type',$this->getType());
716  #$ilCtrl->setParameter($this, 'cp_mode', self::SOURCE_SELECTION);
717  $this->form->setFormAction($ilCtrl->getFormAction($this));
718  $this->form->setTitle($lng->txt($this->getType().'_copy'));
719 
720  $this->form->addCommandButton('searchSource', $lng->txt('btn_next'));
721 
722  $tit = new ilTextInputGUI($lng->txt('title'),'tit');
723  $tit->setSize(40);
724  $tit->setMaxLength(70);
725  $tit->setRequired(true);
726  $tit->setInfo($lng->txt('wizard_title_info'));
727  $this->form->addItem($tit);
728  }
729 
734  protected function unsetSession()
735  {
736  unset($_SESSION['source_query']);
737  $this->setSource(0);
738  }
739 }
740 ?>