Skip to content

Commit

Permalink
Merge pull request #146 from tomolimo/4.0/bugfixes
Browse files Browse the repository at this point in the history
Fixed add solution that was not compliant with GLPI9.5
  • Loading branch information
tomolimo authored Jan 24, 2022
2 parents 3ba25a6 + e5ccb43 commit eeac86f
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 316 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ version 3.6.x is compatible with GLPI 9.4 and needs ProcessMaker 3.3.0-RE-1.x (h

version 4.0.x is compatible with GLPI 9.5 and needs ProcessMaker 3.3.0-RE-1.x (https://github.com/tomolimo/processmaker-server/releases/latest)

This plugin can run classic and bpmn processes
This plugin can run classic (ProcessMaker server v2) and BPMN (ProcessMaker server v3 and later) processes

An IRC channel is available: #processmaker-glpi on https://webchat.freenode.net/
68 changes: 46 additions & 22 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function plugin_pre_item_update_processmaker(CommonITILObject $parm) {
//global $DB;//, $PM_SOAP;

// look at previous status
if (isset($parm->input['status'])
if (isset($parm->input['status'])
&& $parm->input['status'] == CommonITILObject::SOLVED
&& !in_array($parm->fields['status'], [CommonITILObject::SOLVED, CommonITILObject::CLOSED])
&& !PluginProcessmakerCase::canSolve(['item' => $parm])) {
Expand Down Expand Up @@ -194,7 +194,7 @@ function plugin_item_update_processmaker_satisfaction($parm) {
global $PM_SOAP;
if (Session::isCron()) { // Task cron log with user admin
$PM_SOAP->login(true);
}
}
$cases = PluginProcessmakerCase::getIDsFromItem('Ticket', $parm->fields['tickets_id']);
foreach ($cases as $cases_id) {
$locCase = new PluginProcessmakerCase;
Expand Down Expand Up @@ -282,7 +282,6 @@ function plugin_item_update_processmaker_tasks($parm) {
$locCase = new PluginProcessmakerCase;
$locCase->getFromDB($pmTask->fields['plugin_processmaker_cases_id']);
$srccase_guid = $locCase->fields['case_guid'];

//$msg = Toolbox::backtrace(false);
//$msg .= ' $locCase: '.str_replace("\n", "\n ", print_r($locCase, true))."\n";
//$msg .= ' $task: '.str_replace("\n", "\n ", print_r($parm, true))."\n";
Expand Down Expand Up @@ -320,25 +319,34 @@ function plugin_item_update_processmaker_tasks($parm) {
$casevariables = array_merge( $casevariables, $matches[1] );
}
}
if (preg_match_all( "/@@(\w+)/u", $externalapplication['url'], $matches )) {
$casevariables = array_merge( $casevariables, $matches[1] );
}
if(isset($externalapplication['headers']) && $externalapplication['headers'] != "") {
if (preg_match_all( "/@@(\w+)/u", $externalapplication['headers'], $matches )) {
$casevariables = array_merge( $casevariables, $matches[1] );
}
}

}

// ask for those case variables
//$PM_SOAP = new PluginProcessmakerProcessmaker();
//$PM_SOAP->login( );
// now tries to get the variables to check condition
$infoForTasks = $locCase->getVariables($casevariables);
$infoForURL = [];
foreach ($infoForTasks as $casevar => $varval) {
$infoForTasks[ "@@$casevar" ] = "'$varval'";
$infoForURL[ "@@$casevar" ] = $varval;
unset( $infoForTasks[ $casevar ] );
}

//$msg .= " ***********\n";
//$msg .= ' $targetTask: '.str_replace("\n", "\n ", print_r($targetTask, true))."\n";

$targetTask['sourcecondition'] = str_replace( array_keys($infoForTasks), $infoForTasks, $targetTask['sourcecondition'] );

$eval = eval( "return (".$targetTask['sourcecondition']." ? 1 : 0);" );

//$msg .= ' $infoForTasks: '.str_replace("\n", "\n ", print_r($infoForTasks, true))."\n";
//$msg .= ' $targetTask[\'sourcecondition\']: '.str_replace("\n", "\n ", print_r($targetTask['sourcecondition'], true))."\n";
//$msg .= ' $result: '."$eval\n";
Expand Down Expand Up @@ -366,7 +374,8 @@ function plugin_item_update_processmaker_tasks($parm) {
}
$externalapplicationparams['callback'] = $CFG_GLPI["url_base"]."/plugins/processmaker/ajax/asynchronousdatas.php";
$ch = curl_init();
$externalapplication['url'] = eval( "return '".str_replace( array_keys($infoForTasks), $infoForTasks, $externalapplication['url'])."' ;" ); // '???

$externalapplication['url'] = str_replace( array_keys($infoForURL), $infoForURL, $externalapplication['url']);
curl_setopt($ch, CURLOPT_URL, $externalapplication['url'] );
if (isset($externalapplication['method']) && $externalapplication['method'] == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
Expand All @@ -391,18 +400,17 @@ function plugin_item_update_processmaker_tasks($parm) {
$pmconfig = $PM_SOAP->config; //PluginProcessmakerConfig::getInstance();

$cronaction = new PluginProcessmakerCrontaskaction;
$cronaction->add( [ 'plugin_processmaker_caselinks_id' => $targetTask['id'],
'plugin_processmaker_cases_id' => $locCase->getID(),
//'itemtype' => $itemtype,
//'items_id' => $parm->fields['tickets_id'],
'users_id' => $pmconfig->fields['users_id'],
'is_targettoclaim' => $targetTask['is_targettoclaim'],
'state' => ($targetTask['is_externaldata'] ? PluginProcessmakerCrontaskaction::WAITING_DATA : PluginProcessmakerCrontaskaction::DATA_READY),
'postdata' => json_encode( $postdata, JSON_HEX_APOS | JSON_HEX_QUOT),
'logs_out' => json_encode( $externalapplicationparams, JSON_HEX_APOS | JSON_HEX_QUOT)
],
null,
false);
$cronaction->add([
'plugin_processmaker_caselinks_id' => $targetTask['id'],
'plugin_processmaker_cases_id' => $locCase->getID(),
//'itemtype' => $itemtype,
//'items_id' => $parm->fields['tickets_id'],
'users_id' => $pmconfig->fields['users_id'],
'is_targettoclaim' => $targetTask['is_targettoclaim'],
'state' => ($targetTask['is_externaldata'] ? PluginProcessmakerCrontaskaction::WAITING_DATA : PluginProcessmakerCrontaskaction::DATA_READY),
'postdata' => json_encode( $postdata, JSON_HEX_APOS | JSON_HEX_QUOT),
'logs_out' => json_encode( $externalapplicationparams, JSON_HEX_APOS | JSON_HEX_QUOT)
], [], false);

if ($externalapplication) {
// must call external application in order to get the needed data asynchroneously
Expand All @@ -415,12 +423,26 @@ function plugin_item_update_processmaker_tasks($parm) {
$externalapplicationparams = json_encode( $externalapplicationparams, JSON_HEX_APOS | JSON_HEX_QUOT);

curl_setopt($ch, CURLOPT_POSTFIELDS, $externalapplicationparams);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Content-Length: ' . strlen($externalapplicationparams), 'Expect:']);
$headers = [
'Content-Type: application/json',
'Content-Length: ' . strlen($externalapplicationparams),
'Expect:'];
if(isset($externalapplication['headers']) && $externalapplication['headers'] != "") {
$externalapplication['headers'] = eval( "return ".str_replace( array_keys($infoForTasks), $infoForTasks, $externalapplication['headers'])." ;" ); // '???
//Can't add an assoicative array in curlopt_httpheader
foreach($externalapplication['headers'] as $key => $h) {
array_push($headers, $key.": ".$h);
}
}
//$headers = array_merge($headers, $externalapplication['headers']);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_VERBOSE, 1);

if (isset($externalapplication['ssl_verify'])) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $externalapplication['ssl_verify']);
if (isset($externalapplication['ssl_verify']) && $externalapplication['ssl_verify'] > 0) {
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $externalapplication['ssl_verify']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $externalapplication['ssl_verify']);
}

Expand All @@ -432,7 +454,9 @@ function plugin_item_update_processmaker_tasks($parm) {
$response = curl_exec ($ch);
if ($response === false) {
//throw new Exception(curl_error($ch), curl_errno($ch));
Toolbox::logDebug( curl_error($ch).":".curl_errno($ch) );
Toolbox::logDebug(curl_error($ch) . ":" . curl_errno($ch));
// Set 0 to the crontask action status
$cronaction->update(['id ' => $cronaction->getID(), 'state' => PluginProcessmakerCrontaskaction::CURL_ERROR]);
}

curl_close ($ch);
Expand Down
46 changes: 23 additions & 23 deletions inc/crontaskaction.class.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php

/**
* PluginProcessmakerCrontaskaction is used to manage actions between cases
*
* Allows actions: routing cases (called slaves) from another case (called master)
*
*
* @version 1.0
* @author MoronO
*/
class PluginProcessmakerCrontaskaction extends CommonDBTM {

// postdata are of the form:
// {"form":{"RELEASE_DONE":"0","btnGLPISendRequest":"submit"},"UID":"28421020557bffc5b374850018853291","__DynaformName__":"51126098657bd96b286ded7016691792_28421020557bffc5b374850018853291","__notValidateThisFields__":"[]","DynaformRequiredFields":"[]","APP_UID":"6077575685836f7d89cabe6013770123","DEL_INDEX":"4"}


const WAITING_DATA = 1;
const DATA_READY = 2;
const DONE = 3;
const NOT_DONE = 4;

}
<?php

/**
* PluginProcessmakerCrontaskaction is used to manage actions between cases
*
* Allows actions: routing cases (called slaves) from another case (called master)
*
*
* @version 1.0
* @author MoronO
*/
class PluginProcessmakerCrontaskaction extends CommonDBTM {

// postdata are of the form:
// {"form":{"RELEASE_DONE":"0","btnGLPISendRequest":"submit"},"UID":"28421020557bffc5b374850018853291","__DynaformName__":"51126098657bd96b286ded7016691792_28421020557bffc5b374850018853291","__notValidateThisFields__":"[]","DynaformRequiredFields":"[]","APP_UID":"6077575685836f7d89cabe6013770123","DEL_INDEX":"4"}

const CURL_ERROR = 0;
const WAITING_DATA = 1;
const DATA_READY = 2;
const DONE = 3;
const NOT_DONE = 4;

}
76 changes: 53 additions & 23 deletions inc/process.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ function maybeDeleted() {
}


// static function getIcon() {
//// return "fas fa-code-branch fa-rotate-90";
// return "fas fa-blog fa-flip-vertical";
//// return "fas fa-cogs fa-flip-vertical";
// static function getIcon() {
//// return "fas fa-code-branch fa-rotate-90";
// return "fas fa-blog fa-flip-vertical";
//// return "fas fa-cogs fa-flip-vertical";
// }


Expand Down Expand Up @@ -151,16 +151,16 @@ function refreshTasks($post) {
$countElt += $dbu->countElementsInTable( $dbu->getTableForItemType($obj), "taskcategories_id = ".$task['taskcategories_id'] );
if ($countElt != 0) {
// just set 'is_active' to 0
$pmtask->Update( [ 'id' => $task['id'], 'is_start' => 0, 'is_active' => 0 ] );
$pmtask->Update(['id' => $task['id'], 'is_start' => 0, 'is_active' => 0]);
break;
}
}
if ($countElt == 0) {
// purge this category as it is not used anywhere
$taskCat = new TaskCategory;
$taskCat->delete([ 'id' => $task['taskcategories_id'] ], 1);
$taskCat->delete(['id' => $task['taskcategories_id']], 1);
$pmTaskCat = new PluginProcessmakerTaskCategory;
$pmTaskCat->delete([ 'id' => $task['id'] ], 1);
$pmTaskCat->delete(['id' => $task['id']], 1);
}
}

Expand All @@ -169,32 +169,52 @@ function refreshTasks($post) {
$taskCat = new TaskCategory;
if ($pmTaskCat->getFromGUID( $taskGUID )) {
// got it then check names, and if != update
if ($taskCat->getFromDB( $pmTaskCat->fields['taskcategories_id'] )) {
if ($taskCat->getFromDB($pmTaskCat->fields['taskcategories_id'])) {
// found it must test if should be updated
if ($taskCat->fields['name'] != $task['TAS_TITLE'] || $taskCat->fields['comment'] != $task['TAS_DESCRIPTION']) {
$taskCat->update( [ 'id' => $taskCat->getID(), 'name' => $PM_DB->escape($task['TAS_TITLE']), 'comment' => $PM_DB->escape($task['TAS_DESCRIPTION']), 'taskcategories_id' => $this->fields['taskcategories_id'] ] );
if ($taskCat->fields['name'] != $task['TAS_TITLE']
|| $taskCat->fields['comment'] != $task['TAS_DESCRIPTION']) {
$taskCat->update([
'id' => $taskCat->getID(),
'name' => $PM_DB->escape($task['TAS_TITLE']),
'comment' => $PM_DB->escape($task['TAS_DESCRIPTION']),
'taskcategories_id' => $this->fields['taskcategories_id'],
'is_active' => 0 // to prevent use of this task cat in manual tasks
] );
}
if ($pmTaskCat->fields['is_start'] != $task['is_start']) {
$pmTaskCat->update( [ 'id' => $pmTaskCat->getID(), 'is_start' => $task['is_start'] ] );
$pmTaskCat->update(['id' => $pmTaskCat->getID(), 'is_start' => $task['is_start']]);
}
} else {
// taskcat must be created
$taskCat->add( [ 'is_recursive' => true, 'name' => $PM_DB->escape($task['TAS_TITLE']), 'comment' => $PM_DB->escape($task['TAS_DESCRIPTION']), 'taskcategories_id' => $this->fields['taskcategories_id'] ] );
$taskCat->add([
'is_recursive' => true,
'name' => $PM_DB->escape($task['TAS_TITLE']),
'comment' => $PM_DB->escape($task['TAS_DESCRIPTION']),
'taskcategories_id' => $this->fields['taskcategories_id'],
'is_active' => 0 // to prevent use of this task cat in manual tasks
] );
// update pmTaskCat
$pmTaskCat->update( [ 'id' => $pmTaskCat->getID(), 'taskcategories_id' => $taskCat->getID(), 'is_start' => $task['is_start'] ] );
$pmTaskCat->update(['id' => $pmTaskCat->getID(), 'taskcategories_id' => $taskCat->getID(), 'is_start' => $task['is_start']]);
}
} else {
// should create a new one
// taskcat must be created
$taskCat->add( [ 'is_recursive' => true, 'name' => $PM_DB->escape($task['TAS_TITLE']), 'comment' => $PM_DB->escape($task['TAS_DESCRIPTION']), 'taskcategories_id' => $this->fields['taskcategories_id'] ] );
$taskCat->add([
'is_recursive' => true,
'name' => $PM_DB->escape($task['TAS_TITLE']),
'comment' => $PM_DB->escape($task['TAS_DESCRIPTION']),
'taskcategories_id' => $this->fields['taskcategories_id'],
'is_active' => 0 // to prevent use of this task cat in manual tasks
] );
// pmTaskCat must be created too
$pmTaskCat->add( ['plugin_processmaker_processes_id' => $this->getID(),
'pm_task_guid' => $taskGUID,
'taskcategories_id' => $taskCat->getID(),
'is_start' => $task['is_start'],
'is_active' => 1,
'is_subprocess' => $task['is_subprocess']
] );
$pmTaskCat->add([
'plugin_processmaker_processes_id' => $this->getID(),
'pm_task_guid' => $taskGUID,
'taskcategories_id' => $taskCat->getID(),
'is_start' => $task['is_start'],
'is_active' => 1,
'is_subprocess' => $task['is_subprocess']
]);
}
// here we should take into account translations if any
if (isset($taskArray[ $taskGUID ])) {
Expand Down Expand Up @@ -371,7 +391,12 @@ function updateTaskCategory($pmMainTaskCat) {
global $PM_DB;
$taskCat = new TaskCategory;
if ($taskCat->getFromDB( $this->fields['taskcategories_id'] ) && $taskCat->fields['name'] != $this->fields['name']) {
return $taskCat->update( [ 'id' => $taskCat->getID(), 'taskcategories_id' => $pmMainTaskCat, 'name' => $PM_DB->escape($this->fields['name'])] );
return $taskCat->update([
'id' => $taskCat->getID(),
'taskcategories_id' => $pmMainTaskCat,
'name' => $PM_DB->escape($this->fields['name']),
'is_active' => 0 // to prevent use of this task cat in a manual task
] );
}
return false;
}
Expand All @@ -385,7 +410,12 @@ function updateTaskCategory($pmMainTaskCat) {
function addTaskCategory($pmMainTaskCat) {
global $PM_DB;
$taskCat = new TaskCategory;
if ($taskCat->add( [ 'is_recursive' => true, 'taskcategories_id' => $pmMainTaskCat, 'name' => $PM_DB->escape($this->fields['name'])] )) {
if ($taskCat->add([
'is_recursive' => true,
'taskcategories_id' => $pmMainTaskCat,
'name' => $PM_DB->escape($this->fields['name']),
'is_active' => 0 // to prevent use of this task cat in a manual task
])) {
return $this->update( [ 'id' => $this->getID(), 'taskcategories_id' => $taskCat->getID() ] );
}
return false;
Expand Down
Loading

0 comments on commit eeac86f

Please sign in to comment.