ILIAS  eassessment Revision 61809
 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 
19  private $mode = 0;
20 
21  private $lng;
22 
23  private $parent_obj = null;
24 
25  private $type = '';
26  private $source = 0;
27  private $target = 0;
28 
29 
34  public function __construct($a_parent_gui)
35  {
36  global $ilCtrl,$lng;
37 
38  $this->lng = $lng;
39  $this->lng->loadLanguageModule('search');
40 
41  $this->parent_obj = $a_parent_gui;
42  }
43 
48  public function executeCommand()
49  {
50  global $ilCtrl;
51 
52  $this->init();
53 
54  $next_class = $ilCtrl->getNextClass($this);
55  $cmd = $ilCtrl->getCmd();
56 
57 
58  switch($next_class)
59  {
60  default:
61  $this->$cmd();
62  break;
63  }
64  }
65 
70  protected function init()
71  {
72  global $ilCtrl;
73 
74  if($_REQUEST['new_type'])
75  {
76  $this->setMode(self::SOURCE_SELECTION);
77 
78  $ilCtrl->setParameter($this,'new_type',$this->getType());
79  $ilCtrl->setParameterByClass(get_class($this->parent_obj), 'new_type', $this->getType());
80  $ilCtrl->setReturnByClass(get_class($this->parent_obj), 'create');
81  }
82  elseif($_REQUEST['selectMode'] == self::SOURCE_SELECTION)
83  {
84  $ilCtrl->setParameterByClass(get_class($this->parent_obj), 'selectMode', self::SOURCE_SELECTION);
85  $this->setTarget((int) $_GET['ref_id']);
86  $ilCtrl->setReturnByClass(get_class($this->parent_obj), '');
87  }
88  else
89  {
90  $this->setMode(self::TARGET_SELECTION);
91  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
92 
93  $this->setType(
95  );
96  }
97  }
98 
103  protected function initTargetSelection()
104  {
105  global $ilCtrl, $tree;
106 
107  // empty session on init
108  $_SESSION['paste_copy_repexpand'] = array();
109 
110  // copy opened nodes from repository explorer
111  $_SESSION['paste_copy_repexpand'] = is_array($_SESSION['repexpand']) ? $_SESSION['repexpand'] : array();
112 
113  // open current position
114  $path = $tree->getPathId((int)$_GET['source_id']);
115  foreach((array)$path as $node_id)
116  {
117  if(!in_array($node_id, $_SESSION['paste_copy_repexpand']))
118  $_SESSION['paste_copy_repexpand'][] = $node_id;
119  }
120 
121  $this->setMode(self::TARGET_SELECTION);
122  $this->setSource((int) $_GET['source_id']);
123  $this->setType(
125  );
126 
127  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
128 
129  $this->showTargetSelectionTree();
130  }
131 
136  protected function initSourceSelection()
137  {
138  global $ilCtrl;
139 
140  $this->setMode(self::SOURCE_SELECTION);
141  $this->setTarget((int) $_GET['ref_id']);
142 
143  $ilCtrl->setReturnByClass(get_class($this->parent_obj),'');
144  $this->showSourceSelectionTree();
145  }
146 
151  public function showTargetSelectionTree()
152  {
153  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
154 
155  $this->tpl = $tpl;
156  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html');
157 
158  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard'));
159 
160  include_once 'classes/class.ilPasteIntoMultipleItemsExplorer.php';
163  'repository.php?cmd=goto', 'paste_copy_repexpand');
164  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showTargetSelectionTree'));
165  $exp->setTargetGet('ref_id');
166  $exp->setPostVar('target');
167  $exp->setCheckedItems(array((int) $_POST['target']));
168 
169  // Filter to container
170  foreach(array('cat','root','crs','grp','fold') as $container)
171  {
172  /*
173  if($this->getType() == 'crs' and $container == 'crs')
174  {
175  continue;
176  }
177  */
178  $sub = $objDefinition->getSubObjects($container);
179  if(!isset($sub[$this->getType()]))
180  {
181  $exp->removeFormItemForType($container);
182  }
183  }
184 
185  if($_GET['paste_copy_repexpand'] == '')
186  {
187  $expanded = $tree->readRootId();
188  }
189  else
190  {
191  $expanded = $_GET['paste_copy_repexpand'];
192  }
193 
194  $this->tpl->setVariable('FORM_TARGET', '_self');
195  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
196 
197  $exp->setExpand($expanded);
198  // build html-output
199  $exp->setOutput(0);
200  $output = $exp->getOutput();
201 
202  $this->tpl->setVariable('OBJECT_TREE', $output);
203 
204  $this->tpl->setVariable('CMD_SUBMIT', 'saveTarget');
205 
206  if($objDefinition->isContainer($this->getType()))
207  {
208  $this->tpl->setVariable('TXT_SUBMIT',$this->lng->txt('btn_next'));
209  }
210  else
211  {
212  $this->tpl->setVariable('TXT_SUBMIT', $this->lng->txt('obj_'.$this->getType().'_duplicate'));
213  }
214 
215  $ilToolbar->addButton($this->lng->txt('back'), $ilCtrl->getLinkTarget($this,'cancel'));
216  }
217 
222  public function showSourceSelectionTree()
223  {
224  global $ilTabs, $ilToolbar, $ilCtrl, $tree, $tpl, $objDefinition;
225 
226  $this->tpl = $tpl;
227  $this->tpl->addBlockfile('ADM_CONTENT', 'adm_content', 'tpl.paste_into_multiple_objects.html');
228 
229  ilUtil::sendInfo($this->lng->txt('msg_copy_clipboard_source'));
230 
231  include_once 'classes/class.ilPasteIntoMultipleItemsExplorer.php';
234  'repository.php?cmd=goto', 'paste_copy_repexpand');
235 
236  $ilCtrl->setParameter($this, 'selectMode', self::SOURCE_SELECTION);
237  $exp->setExpandTarget($ilCtrl->getLinkTarget($this, 'showSourceSelectionTree'));
238  $exp->setTargetGet('ref_id');
239  $exp->setPostVar('source');
240  $exp->setCheckedItems(array((int) $_POST['source']));
241 
242  // Filter to container
243  foreach(array('cat','root','grp','fold') as $container)
244  {
245  $exp->removeFormItemForType($container);
246  }
247 
248 
249  if($_GET['paste_copy_repexpand'] == '')
250  {
251  $expanded = $tree->readRootId();
252  }
253  else
254  {
255  $expanded = $_GET['paste_copy_repexpand'];
256  }
257 
258  $this->tpl->setVariable('FORM_TARGET', '_self');
259  $this->tpl->setVariable('FORM_ACTION', $ilCtrl->getFormAction($this, 'copySelection'));
260 
261  $exp->setExpand($expanded);
262  // build html-output
263  $exp->setOutput(0);
264  $output = $exp->getOutput();
265 
266  $this->tpl->setVariable('OBJECT_TREE', $output);
267 
268  $this->tpl->setVariable('CMD_SUBMIT', 'saveSource');
269  $this->tpl->setVariable('TXT_SUBMIT', $this->lng->txt('btn_next'));
270 
271  $ilToolbar->addButton($this->lng->txt('back'), $ilCtrl->getLinkTarget($this,'cancel'));
272  }
273 
278  protected function saveTarget()
279  {
280  global $objDefinition;
281 
282  if(isset($_POST['target']))
283  {
284  $this->setTarget((int) $_REQUEST['target']);
285  }
286  else
287  {
288  ilUtil::sendFailure($this->lng->txt('select_one'));
289  $this->showTargetSelectionTree();
290  return false;
291  }
292 
293  if($objDefinition->isContainer($this->getType()))
294  {
295  $this->showItemSelection();
296  }
297  else
298  {
299  $this->copySingleObject();
300  }
301  }
302 
308  public function setMode($a_mode)
309  {
310  $this->mode = $a_mode;
311  }
312 
317  public function getMode()
318  {
319  return $this->mode;
320  }
321 
326  public function getParentObject()
327  {
328  return $this->parent_obj;
329  }
330 
336  public function getType()
337  {
338  return $this->type;
339  }
340 
347  public function setType($type)
348  {
349  $this->type = $type;
350  }
351 
357  public function setSource($a_source_id)
358  {
359  $_SESSION['copy_source'] = $a_source_id;
360  }
361 
366  public function getSource()
367  {
368  return $_SESSION['copy_source'];
369  }
370 
376  public function setTarget($a_target)
377  {
378  $_SESSION['copy_target'] = $a_target;
379  }
380 
385  public function getTarget()
386  {
387  return $_SESSION['copy_target'];
388  }
389 
394  protected function cancel()
395  {
396  global $ilCtrl;
397  $ilCtrl->returnToParent($this);
398  }
399 
404  protected function searchSource()
405  {
406  global $tree,$ilObjDataCache,$lng,$ilCtrl,$tpl;
407 
408  if(isset($_POST['tit']))
409  {
410  ilUtil::sendInfo($this->lng->txt('wizard_search_list'));
411  $_SESSION['source_query'] = $_POST['tit'];
412  }
413  else
414  {
415  $_POST['tit'] = $_SESSION['source_query'];
416  }
417 
418  $this->initFormSearch();
419  $this->form->setValuesByPost();
420 
421  if(!$this->form->checkInput())
422  {
423  ilUtil::sendFailure($lng->txt('msg_no_search_string'),true);
424  $ilCtrl->returnToParent($this);
425  return false;
426  }
427 
428  include_once './Services/Search/classes/class.ilQueryParser.php';
429  $query_parser = new ilQueryParser($this->form->getInput('tit'));
430  $query_parser->setMinWordLength(1,true);
431  $query_parser->setCombination(QP_COMBINATION_AND);
432  $query_parser->parse();
433  if(!$query_parser->validate())
434  {
435  ilUtil::sendFailure($query_parser->getMessage(),true);
436  $ilCtrl->returnToParent($this);
437  }
438 
439  // only like search since fulltext does not support search with less than 3 characters
440  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
441  $object_search = new ilLikeObjectSearch($query_parser);
442  $object_search->setFilter(array($_REQUEST['new_type']));
443  $res = $object_search->performSearch();
444  $res->setRequiredPermission('copy');
445  $res->filter(ROOT_FOLDER_ID,true);
446 
447  if(!count($results = $res->getResultsByObjId()))
448  {
449  ilUtil::sendFailure($this->lng->txt('search_no_match'),true);
450  $ilCtrl->returnToParent($this);
451  }
452 
453  include_once './Services/Object/classes/class.ilObjectCopySearchResultTableGUI.php';
454  $table = new ilObjectCopySearchResultTableGUI($this,'searchSource',$_REQUEST['new_type']);
455  $table->setSelectedReference($this->getSource());
456  $table->parseSearchResults($results);
457  $tpl->setContent($table->getHTML());
458  }
459 
464  protected function saveSource()
465  {
466  global $objDefinition;
467 
468  if(isset($_POST['source']))
469  {
470  $this->setSource($_POST['source']);
471  }
472  else
473  {
474  ilUtil::sendFailure($this->lng->txt('select_one'));
475  $this->searchSource();
476  return false;
477  }
478 
479  if($objDefinition->isContainer($this->getType()))
480  {
481  $this->showItemSelection();
482  }
483  else
484  {
485  $this->copySingleObject();
486  }
487  }
488 
489 
490 
495  protected function showItemSelection()
496  {
497  global $tpl;
498 
499  if(!$this->getSource())
500  {
501  ilUtil::sendFailure($this->lng->txt('select_one'));
502  $this->searchSource();
503  return false;
504  }
505 
506  ilUtil::sendInfo($this->lng->txt($this->getType().'_copy_threads_info'));
507  include_once './Services/Object/classes/class.ilObjectCopySelectionTableGUI.php';
508 
509  $tpl->addJavaScript('./Services/CopyWizard/js/ilContainer.js');
510  $tpl->setVariable('BODY_ATTRIBUTES','onload="ilDisableChilds(\'cmd\');"');
511 
512  $back_cmd = $this->getMode() == self::SOURCE_SELECTION ? 'searchSource' : 'showTargetSelectionTree';
513  $table = new ilObjectCopySelectionTableGUI($this,'showItemSelection',$this->getType(),$back_cmd);
514  $table->parseSource($this->getSource());
515 
516  $tpl->setContent($table->getHTML());
517  }
518 
523  protected function copySingleObject()
524  {
525  include_once('classes/class.ilLink.php');
526  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
527 
528  global $ilAccess,$ilErr,$rbacsystem,$ilUser,$ilCtrl,$rbacreview;
529 
530  // Create permission
531  if(!$rbacsystem->checkAccess('create', $this->getTarget(), $this->getType()))
532  {
533  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
534  $ilCtrl->returnToParent($this);
535  }
536  // Source defined
537  if(!$this->getSource())
538  {
539  ilUtil::sendFailure($this->lng->txt('select_one'),true);
540  $ilCtrl->returnToParent($this);
541  }
542  // Copy permission
543  if(!$ilAccess->checkAccess('copy','',$this->getSource()))
544  {
545  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
546  $ilCtrl->returnToParent($this);
547  }
548 
549  // Save wizard options
551  $wizard_options = ilCopyWizardOptions::_getInstance($copy_id);
552  $wizard_options->saveOwner($ilUser->getId());
553  $wizard_options->saveRoot((int) $this->getSource());
554 
555  /*
556  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
557  foreach($options as $source_id => $option)
558  {
559  $wizard_options->addEntry($source_id,$option);
560  }
561  */
562 
563  $wizard_options->read();
564 
565  $orig = ilObjectFactory::getInstanceByRefId((int) $this->getSource());
566  $new_obj = $orig->cloneObject($this->getTarget(),$copy_id);
567 
568  // Delete wizard options
569  $wizard_options->deleteAll();
570 
571  // rbac log
572  include_once "Services/AccessControl/classes/class.ilRbacLog.php";
573  if(ilRbacLog::isActive())
574  {
575  $rbac_log_roles = $rbacreview->getParentRoleIds($new_obj->getRefId(), false);
576  $rbac_log = ilRbacLog::gatherFaPa($new_obj->getRefId(), array_keys($rbac_log_roles));
577  ilRbacLog::add(ilRbacLog::COPY_OBJECT, $new_obj->getRefId(), $rbac_log, (int)$this->getSource());
578  }
579 
580  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
581  ilUtil::redirect(ilLink::_getLink($new_obj->getRefId()));
582  }
583 
588  protected function copyContainer()
589  {
590  global $ilLog;
591 
592  include_once('classes/class.ilLink.php');
593  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
594 
595  global $ilAccess,$ilErr,$rbacsystem,$tree,$ilUser,$ilCtrl;
596 
597  // Workaround for course in course copy
598 
600  $source_type = ilObject::_lookupType(ilObject::_lookupObjId($this->getSource()));
601 
602  if($target_type != $source_type or $target_type != 'crs')
603  {
604  if(!$rbacsystem->checkAccess('create', $this->getTarget(),$this->getType()))
605  {
606  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
607  $ilCtrl->returnToParent($this);
608  }
609  }
610  if(!$this->getSource())
611  {
612  ilUtil::sendFailure($this->lng->txt('select_one'),true);
613  $ilCtrl->returnToParent($this);
614  return false;
615  }
616 
617  $options = $_POST['cp_options'] ? $_POST['cp_options'] : array();
619  $result = $orig->cloneAllObject($_COOKIE['PHPSESSID'], $_COOKIE['ilClientId'], $this->getType(), $this->getTarget(), $this->getSource(), $options);
620 
621  // Check if copy is in progress
622  if ($result == $this->getTarget())
623  {
624  ilUtil::sendInfo($this->lng->txt("object_copy_in_progress"),true);
625  ilUtil::redirect('repository.php?ref_id='.$this->getTarget());
626  }
627  else
628  {
629  ilUtil::sendSuccess($this->lng->txt("object_duplicated"),true);
630  ilUtil::redirect('repository.php?ref_id='.$result);
631  }
632  }
633 
634 
635 
643  public function showSourceSearch($a_tplvar)
644  {
645  global $tpl;
646 
647  if(!$this->sourceExists())
648  {
649  return false;
650  }
651 
652  $this->unsetSession();
653 
654  $this->initFormSearch();
655  $tpl->setVariable($a_tplvar,$this->form->getHTML());
656  }
657 
658 
663  protected function sourceExists()
664  {
665  global $ilUser;
666 
667  return (bool) ilUtil::_getObjectsByOperations($this->getType(),'copy',$ilUser->getId(),1);
668  }
669 
674  protected function initFormSearch()
675  {
676  global $lng,$ilCtrl;
677 
678  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
679  $this->form = new ilPropertyFormGUI();
680  $this->form->setTableWidth('600px');
681  $ilCtrl->setParameter($this,'new_type',$this->getType());
682  #$ilCtrl->setParameter($this, 'cp_mode', self::SOURCE_SELECTION);
683  $this->form->setFormAction($ilCtrl->getFormAction($this));
684  $this->form->setTitle($lng->txt($this->getType().'_copy'));
685 
686  $this->form->addCommandButton('searchSource', $lng->txt('btn_next'));
687 
688  $tit = new ilTextInputGUI($lng->txt('title'),'tit');
689  $tit->setSize(40);
690  $tit->setMaxLength(70);
691  $tit->setRequired(true);
692  $tit->setInfo($lng->txt('wizard_title_info'));
693  $this->form->addItem($tit);
694  }
695 
700  protected function unsetSession()
701  {
702  unset($_SESSION['source_query']);
703  $this->setSource(0);
704  }
705 }
706 ?>