ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilWorkflowEngineDefinitionsGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
16  protected $parent_gui;
17 
21  protected $dic;
22 
29  public function __construct(ilObjWorkflowEngineGUI $parent_gui, \ILIAS\DI\Container $dic = null)
30  {
31  $this->parent_gui = $parent_gui;
32 
33  if ($dic === null) {
34  $dic = $GLOBALS['DIC'];
35  }
36  $this->dic = $dic;
37  }
38 
46  public function handle($command)
47  {
48  switch (strtolower($command)) {
49  case 'uploadform':
50  return $this->showUploadForm();
51  break;
52 
53  case 'upload':
54  return $this->handleUploadSubmit();
55  break;
56 
57  case 'applyfilter':
58  return $this->applyFilter();
59  break;
60 
61  case 'resetfilter':
62  return $this->resetFilter();
63  break;
64 
65  case 'start':
66  return $this->startProcess();
67  break;
68 
69  case 'delete':
70  return $this->deleteDefinition();
71  break;
72 
73  case 'confirmdelete':
74  return $this->confirmDeleteDefinition();
75  break;
76 
77  case 'startlistening':
78  return $this->startListening();
79  break;
80 
81  case'stoplistening':
82  return $this->stopListening();
83  break;
84 
85  case 'view':
86  default:
87  return $this->showDefinitionsTable();
88  }
89  }
90 
94  public function showDefinitionsTable()
95  {
96  $this->initToolbar();
97  require_once './Services/WorkflowEngine/classes/administration/class.ilWorkflowEngineDefinitionsTableGUI.php';
98  $table_gui = new ilWorkflowEngineDefinitionsTableGUI($this->parent_gui, 'definitions.view');
99  $table_gui->setFilterCommand("definitions.applyfilter");
100  $table_gui->setResetCommand("definitions.resetFilter");
101  $table_gui->setDisableFilterHiding(false);
102 
103  return $table_gui->getHTML();
104  }
105 
109  public function applyFilter()
110  {
111  require_once './Services/WorkflowEngine/classes/administration/class.ilWorkflowEngineDefinitionsTableGUI.php';
112  $table_gui = new ilWorkflowEngineDefinitionsTableGUI($this->parent_gui, 'definitions.view');
113  $table_gui->writeFilterToSession();
114  $table_gui->resetOffset();
115 
116  return $this->showDefinitionsTable();
117  }
118 
122  public function resetFilter()
123  {
124  require_once './Services/WorkflowEngine/classes/administration/class.ilWorkflowEngineDefinitionsTableGUI.php';
125  $table_gui = new ilWorkflowEngineDefinitionsTableGUI($this->parent_gui, 'definitions.view');
126  $table_gui->resetOffset();
127  $table_gui->resetFilter();
128 
129  return $this->showDefinitionsTable();
130  }
131 
135  public function showUploadForm()
136  {
137  require_once './Services/WorkflowEngine/classes/administration/class.ilUploadDefinitionForm.php';
138  $form_definition = new ilUploadDefinitionForm();
139  $form = $form_definition->getForm(
140  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.upload')
141  );
142 
143  return $form->getHTML();
144  }
145 
152  public function handleUploadSubmit()
153  {
155 
156  require_once './Services/WorkflowEngine/classes/administration/class.ilUploadDefinitionForm.php';
157  $form_definition = new ilUploadDefinitionForm();
158  $form = $form_definition->getForm(
159  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.upload')
160  );
161 
162  if (!$form->checkInput()) {
163  $form->setValuesByPost();
164  return $form->getHTML();
165  }
166 
167  $fs = $this->dic->filesystem()->storage();
168  $upload = $this->dic->upload();
169 
170  $repositoryDirectory = ilObjWorkflowEngine::getRepositoryDir(true);
171  if (!$fs->hasDir($repositoryDirectory)) {
172  $fs->createDir($repositoryDirectory);
173  }
174 
175  $tmpDirectory = ilObjWorkflowEngine::getTempDir(true);
176  if (!$fs->hasDir($tmpDirectory)) {
177  $fs->createDir($tmpDirectory);
178  }
179 
180  if (!$upload->hasUploads() || $upload->hasBeenProcessed()) {
181  $form->setValuesByPost();
182  return $form->getHTML();
183  }
184 
185  $upload->process();
186 
188  $uploadResult = array_values($upload->getResults())[0];
189  if (!$uploadResult || $uploadResult->getStatus() != \ILIAS\FileUpload\DTO\ProcessingStatus::OK) {
190  $form->setValuesByPost();
191  return $form->getHTML();
192  }
193 
194  $upload->moveOneFileTo(
195  $uploadResult,
196  $tmpDirectory,
197  \ILIAS\FileUpload\Location::STORAGE,
198  $uploadResult->getName(),
199  true
200  );
201 
202  $repo_base_name = 'il' . substr($uploadResult->getName(), 0, strpos($uploadResult->getName(), '.'));
203  $wf_base_name = 'wfd.' . $repo_base_name . '_v';
204  $version = 0;
205 
206  $fileList = $fs->listContents($repositoryDirectory, true);
207 
208  foreach ($fileList as $file) {
209  if ($file->isDir()) {
210  continue;
211  }
212 
213  $fileBaseName = basename($file->getPath());
214 
215  if (
216  substr(strtolower($fileBaseName), 0, strlen($wf_base_name)) == strtolower($wf_base_name) &&
217  substr($fileBaseName, -4) == '.php'
218  ) {
219  $number = substr($fileBaseName, strlen($wf_base_name), -4);
220  if ($number > $version) {
221  $version = $number;
222  }
223  }
224  }
225  $version++;
226 
227  $repo_name = $repo_base_name . '_v' . $version . '.php';
228 
229  require_once './Services/WorkflowEngine/classes/parser/class.ilBPMN2Parser.php';
230  $parser = new ilBPMN2Parser();
231  $bpmn = $fs->read($tmpDirectory . $uploadResult->getName());
232  $code = $parser->parseBPMN2XML($bpmn, $repo_name);
233 
234  $fs->put($repositoryDirectory . 'wfd.' . $repo_name, $code);
235  $fs->put($repositoryDirectory . 'wfd.' . $repo_base_name . '_v' . $version . '.bpmn2', $bpmn);
236  $fs->delete($tmpDirectory . $uploadResult->getName());
237 
238  // TODO: Workaround because of file extension whitelist. You currently cannot create/put '.php' files
239  $absRepositoryDirectory = ilObjWorkflowEngine::getRepositoryDir();
240  $sourceFile = $absRepositoryDirectory . str_replace('.', '', 'wfd.' . $repo_name) . '.sec';
241  $targetFile = $absRepositoryDirectory . 'wfd.' . $repo_name;
242  if (file_exists($sourceFile)) {
243  rename($sourceFile, $targetFile);
244  }
245 
246  ilUtil::sendSuccess($this->parent_gui->lng->txt('upload_parse_success'), true);
248  html_entity_decode($this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view'))
249  );
250  }
251 
255  public function initToolbar()
256  {
257  require_once './Services/UIComponent/Button/classes/class.ilLinkButton.php';
258  $upload_wizard_button = ilLinkButton::getInstance();
259  $upload_wizard_button->setCaption($this->parent_gui->lng->txt('upload_process'), false);
260  $upload_wizard_button->setUrl(
261  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.uploadform')
262  );
263  $this->parent_gui->ilToolbar->addButtonInstance($upload_wizard_button);
264  }
265 
269  protected function processUploadFormCancellation()
270  {
271  if (isset($_POST['cmd']['cancel'])) {
272  ilUtil::sendInfo($this->parent_gui->lng->txt('action_aborted'), true);
274  html_entity_decode(
275  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view')
276  )
277  );
278  }
279  }
280 
284  public function startListening()
285  {
286  $identifier = basename($_GET['process_id']);
287 
288  require_once ilObjWorkflowEngine::getRepositoryDir() . $identifier . '.php';
289  $class = substr($identifier, 4);
291  $workflow_instance = new $class;
292 
293  $workflow_instance->setWorkflowClass('wfd.' . $class . '.php');
294  $workflow_instance->setWorkflowLocation(ilObjWorkflowEngine::getRepositoryDir());
295 
296  $show_armer_form = true;
297  switch (true) {
298  case isset($_POST['process_id']):
299  case isset($_POST['se_type']):
300  case isset($_POST['se_content']):
301  case isset($_POST['se_subject_type']):
302  case isset($_POST['se_context_type']):
303  $show_armer_form = false;
304  break;
305  default:
306  $show_armer_form = true;
307  }
308 
309  // Check for Event definitions
310  require_once './Services/WorkflowEngine/classes/administration/class.ilWorkflowArmerGUI.php';
311  $this->parent_gui->ilCtrl->saveParameter($this->parent_gui, 'process_id', $identifier);
312  $action = $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.start');
313  $armer = new ilWorkflowArmerGUI($action, $identifier);
314 
315  $form = $armer->getForm($workflow_instance->getInputVars(), $workflow_instance->getStartEventInfo());
316 
317  if ($show_armer_form) {
318  return $form->getHTML();
319  }
320 
321  $event_data = array(
322  'type' => stripslashes($_POST['se_type']),
323  'content' => stripslashes($_POST['se_content']),
324  'subject_type' => stripslashes($_POST['se_subject_type']),
325  'subject_id' => (int) $_POST['se_subject_id'],
326  'context_type' => stripslashes($_POST['se_context_type']),
327  'context_id' => (int) $_POST['se_context_id']
328  );
329  $process_id = stripslashes($_POST['process_id']);
330 
331  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
332  $event_id = ilWorkflowDbHelper::writeStartEventData($event_data, $process_id);
333 
334  foreach ($workflow_instance->getInputVars() as $input_var) {
335  ilWorkflowDbHelper::writeStaticInput($input_var['name'], stripslashes($_POST[$input_var['name']]), $event_id);
336  }
337 
338  ilUtil::sendSuccess($this->parent_gui->lng->txt('wfe_started_listening'), true);
340  html_entity_decode(
341  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view')
342  )
343  );
344  }
345 
346  public function stopListening()
347  {
348  $process_id = ilUtil::stripSlashes($_GET['process_id']);
349 
350  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
351  ilWorkflowDbHelper::deleteStartEventData($process_id);
352 
353  ilUtil::sendSuccess($this->parent_gui->lng->txt('wfe_stopped_listening'), true);
355  html_entity_decode(
356  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view')
357  )
358  );
359  }
360 
366  public function startProcess()
367  {
368  if (isset($_POST['cmd']['cancel'])) {
369  ilUtil::sendInfo($this->parent_gui->lng->txt('action_aborted'), true);
371  html_entity_decode(
372  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view')
373  )
374  );
375  }
376 
377  $identifier = basename($_GET['process_id']);
378 
379  require_once ilObjWorkflowEngine::getRepositoryDir() . $identifier . '.php';
380  $class = substr($identifier, 4);
382  $workflow_instance = new $class;
383 
384  $workflow_instance->setWorkflowClass('wfd.' . $class . '.php');
385  $workflow_instance->setWorkflowLocation(ilObjWorkflowEngine::getRepositoryDir());
386 
387  if (count($workflow_instance->getInputVars())) {
388  $show_launcher_form = false;
389  foreach ($workflow_instance->getInputVars() as $input_var) {
390  if (!isset($_POST[$input_var['name']])) {
391  $show_launcher_form = true;
392  } else {
393  $workflow_instance->setInstanceVarById($input_var['name'], $_POST[$input_var['name']]);
394  }
395  }
396 
397  require_once './Services/WorkflowEngine/classes/administration/class.ilWorkflowLauncherGUI.php';
398  $this->parent_gui->ilCtrl->saveParameter($this->parent_gui, 'process_id', $identifier);
399  $action = $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.start');
400  $launcher = new ilWorkflowLauncherGUI($action, $identifier);
401  $form = $launcher->getForm($workflow_instance->getInputVars());
402 
403  if ($show_launcher_form || $form->checkInput() == false) {
404  $form->setValuesByPost();
405  return $form->getHTML();
406  }
407  }
408 
409  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowDbHelper.php';
410  ilWorkflowDbHelper::writeWorkflow($workflow_instance);
411 
412  $workflow_instance->startWorkflow();
413  $workflow_instance->handleEvent(
414  array(
415  'time_passed',
416  'time_passed',
417  'none',
418  0,
419  'none',
420  0
421  )
422  );
423 
424  ilWorkflowDbHelper::writeWorkflow($workflow_instance);
425 
426  ilUtil::sendSuccess($this->parent_gui->lng->txt('process_started'), true);
428  html_entity_decode(
429  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view')
430  )
431  );
432  }
433 
437  private function ensureProcessIdInRequest()
438  {
439  if (!isset($this->dic->http()->request()->getQueryParams()['process_id'])) {
440  ilUtil::sendInfo($this->parent_gui->lng->txt('wfe_request_missing_process_id'));
441  $this->parent_gui->ilCtrl->redirect($this->parent_gui, 'definitions.view');
442  }
443  }
444 
448  private function getProcessIdFromRequest()
449  {
450  $processId = str_replace(['\\', '/'], '', stripslashes($this->dic->http()->request()->getQueryParams()['process_id']));
451 
452  return basename($processId);
453  }
454 
458  public function deleteDefinition()
459  {
460  $this->ensureProcessIdInRequest();
461 
462  $processId = $this->getProcessIdFromRequest();
463 
464  $pathToProcessPhpFile = ilObjWorkflowEngine::getRepositoryDir() . '/' . $processId . '.php';
465  $pathToProcessBpmn2File = ilObjWorkflowEngine::getRepositoryDir() . '/' . $processId . '.bpmn2';
466 
467  if (file_exists($pathToProcessPhpFile)) {
468  unlink($pathToProcessPhpFile);
469  }
470  if (file_exists($pathToProcessBpmn2File)) {
471  unlink($pathToProcessBpmn2File);
472  }
473 
474  ilUtil::sendSuccess($this->parent_gui->lng->txt('definition_deleted'), true);
476  html_entity_decode(
477  $this->parent_gui->ilCtrl->getLinkTarget($this->parent_gui, 'definitions.view')
478  )
479  );
480  }
481 
485  public function confirmDeleteDefinition()
486  {
487  $this->ensureProcessIdInRequest();
488 
489  $processId = $this->getProcessIdFromRequest();
490 
491  require_once 'Services/WorkflowEngine/classes/administration/class.ilWorkflowDefinitionRepository.php';
492  $repository = new ilWorkflowDefinitionRepository(
493  $this->dic->database(),
494  $this->dic->filesystem(),
496  );
497  $processDefinition = $repository->getById($processId);
498 
499  require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
500  $confirmation = new ilConfirmationGUI();
501  $confirmation->addItem('process_id[]', $processDefinition['id'], $processDefinition['title']);
502  $this->parent_gui->ilCtrl->setParameter($this->parent_gui, 'process_id', $processDefinition['id']);
503  $confirmation->setFormAction($this->parent_gui->ilCtrl->getFormAction($this->parent_gui, 'definitions.view'));
504  $confirmation->setHeaderText($this->parent_gui->lng->txt('wfe_sure_to_delete_process_def'));
505  $confirmation->setConfirm($this->parent_gui->lng->txt('confirm'), 'definitions.delete');
506  $confirmation->setCancel($this->parent_gui->lng->txt('cancel'), 'definitions.view');
507 
508  return $confirmation->getHTML();
509  }
510 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
$action
Class ilWorkflowEngineDefinitionsGUI.
$_GET["client_id"]
Class BaseForm.
$code
Definition: example_050.php:99
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static getRepositoryDir($relative=false)
PhpIncludeInspection
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
if(isset($_POST['submit'])) $form
Class HTTPServicesTest.
Class ilWorkflowDefinitionRepository.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
Create styles array
The data for the language used.
__construct(ilObjWorkflowEngineGUI $parent_gui, \ILIAS\DI\Container $dic=null)
ilWorkflowEngineDefinitionsGUI constructor.
$parser
Definition: BPMN2Parser.php:23
static getTempDir($relative=false)
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
handle($command)
Handle the command given.
static redirect($a_script)
$_POST["username"]
Class ilBPMN2Parser.
Confirmation screen class.