diff --git a/ajax/asynchronousdatas.php b/ajax/asynchronousdatas.php
new file mode 100644
index 0000000..5f1424c
--- /dev/null
+++ b/ajax/asynchronousdatas.php
@@ -0,0 +1,50 @@
+getFromDB( $datas['id'] ) && $asyncdata->fields['state'] == PluginProcessmakerCrontaskaction::WAITING_DATAS ) {
+ $initialdatas = json_decode($asyncdata->fields['postdatas'], true);
+ $initialdatas['form'] = array_merge( $initialdatas['form'], $datas['form'] ) ;
+ $asyncdata->update( array( 'id' => $datas['id'], 'state' => PluginProcessmakerCrontaskaction::DATAS_READY, 'postdatas' => json_encode($initialdatas, JSON_HEX_APOS | JSON_HEX_QUOT) ) ) ;
+ $ret = array( 'code' => '0', 'message' => 'Done' );
+ } else {
+ $ret = array( 'code' => '2', 'message' => 'Case is not existing, or state is not WAITING_DATAS' );
+ }
+
+ break;
+ default:
+ $ret = array( 'code' => '1', 'message' => 'Method not supported' ) ;
+ }
+
+ echo json_encode( $ret, JSON_HEX_APOS | JSON_HEX_QUOT ) ;
+
+}
diff --git a/ajax/dropdownProcesses.php b/ajax/dropdownProcesses.php
index 48cd90e..9877afa 100644
--- a/ajax/dropdownProcesses.php
+++ b/ajax/dropdownProcesses.php
@@ -32,6 +32,10 @@ if (!($item = getItemForItemtype($_REQUEST['itemtype']))) {
exit();
}
+$one_item = -1;
+if (isset($_POST['_one_id'])) {
+ $one_item = $_POST['_one_id'];
+}
// Count real items returned
$count = 0;
@@ -39,6 +43,7 @@ if (!isset($_REQUEST['emptylabel']) || ($_REQUEST['emptylabel'] == '')) {
$_REQUEST['emptylabel'] = Dropdown::EMPTY_VALUE;
}
+$search="";
if (!empty($_REQUEST['searchText'])) {
$search = Search::makeTextSearch($_REQUEST['searchText']);
}
@@ -46,14 +51,14 @@ if (!empty($_REQUEST['searchText'])) {
$processes = array();
// Empty search text : display first
-//if (empty($_REQUEST['searchText'])) {
+if (empty($_REQUEST['searchText'])) {
if ($_REQUEST['display_emptychoice']) {
if (($one_item < 0) || ($one_item == 0)) {
array_push($processes, array('id' => 0,
'text' => $_REQUEST['emptylabel']));
}
}
-//}
+}
$result = PluginProcessmakerProcess::getSqlSearchResult(false, $search);
diff --git a/front/cases.front.php b/front/cases.front.php
index 8e82daa..95b9956 100644
--- a/front/cases.front.php
+++ b/front/cases.front.php
@@ -1,5 +1,5 @@
login( ) ;
if( isset( $_REQUEST['form'] ) ) {
- // save the case variables
- //$resultSave = $myProcessMaker->sendVariables( $myCase->getID() , $_REQUEST['form'] ) ;
- $resultSave = $myProcessMaker->saveForm( $_REQUEST, $_SERVER['HTTP_COOKIE'] ) ;
- //$myCase->sendVariables( $_REQUEST['form'] ) ;
- // now derivate the case !!!
- $pmRouteCaseResponse = $myProcessMaker->routeCase( $myCase->getID(), $_REQUEST['DEL_INDEX']) ;
-
- // now tries to get some variables to setup content for new task and to append text to solved task
- $infoForTasks = $myProcessMaker->getVariables( $myCase->getID(), array( "GLPI_ITEM_TASK_CONTENT",
- "GLPI_ITEM_APPEND_TO_TASK",
- "GLPI_NEXT_GROUP_TO_BE_ASSIGNED",
- "GLPI_ITEM_TITLE",
- "GLPI_TICKET_FOLLOWUP_CONTENT",
- "GLPI_TICKET_FOLLOWUP_IS_PRIVATE",
- "GLPI_TICKET_FOLLOWUP_REQUESTTYPES_ID",
- "GLPI_ITEM_TASK_ENDDATE",
- "GLPI_ITEM_TASK_STARTDATE",
- "GLPI_ITEM_SET_STATUS"
- ) );
- $itemSetStatus = '';
- if( array_key_exists( 'GLPI_ITEM_SET_STATUS', $infoForTasks ) ) {
- $itemSetStatus = $infoForTasks[ 'GLPI_ITEM_SET_STATUS' ] ;
- }
-
- $txtItemTitle = '' ;
- if( array_key_exists( 'GLPI_ITEM_TITLE', $infoForTasks ) ) {
- $txtItemTitle = $infoForTasks[ 'GLPI_ITEM_TITLE' ] ;
- }
-
- $txtToAppendToTask = '' ;
- if( array_key_exists( 'GLPI_ITEM_APPEND_TO_TASK', $infoForTasks ) ) {
- $txtToAppendToTask = $infoForTasks[ 'GLPI_ITEM_APPEND_TO_TASK' ] ;
- }
-
- $txtTaskContent = '' ;
- if( array_key_exists( 'GLPI_ITEM_TASK_CONTENT', $infoForTasks ) ) {
- $txtTaskContent = $infoForTasks[ 'GLPI_ITEM_TASK_CONTENT' ] ;
- }
-
- $groupId = 0 ;
- if( array_key_exists( 'GLPI_NEXT_GROUP_TO_BE_ASSIGNED', $infoForTasks ) ) {
- $groupId = $infoForTasks[ 'GLPI_NEXT_GROUP_TO_BE_ASSIGNED' ] ;
- }
-
- $taskStartDate = '' ;
- $taskEndDate = '' ;
- if( array_key_exists( 'GLPI_ITEM_TASK_ENDDATE', $infoForTasks ) ) {
- $taskEndDate = $infoForTasks[ 'GLPI_ITEM_TASK_ENDDATE' ] ;
- }
- if( array_key_exists( 'GLPI_ITEM_TASK_STARTDATE', $infoForTasks ) ) {
- $taskStartDate = $infoForTasks[ 'GLPI_ITEM_TASK_STARTDATE' ] ;
- if( $taskEndDate == '' ) {
- // at least
- $taskEndDate = $taskStartDate ;
- }
- }
-
- $createFollowup = false ; // by default
- if( array_key_exists( 'GLPI_TICKET_FOLLOWUP_CONTENT', $infoForTasks ) && $infoForTasks[ 'GLPI_TICKET_FOLLOWUP_CONTENT' ] != '') {
- //&& array_key_exists( 'GLPI_TICKET_FOLLOWUP_IS_PRIVATE', $infoForTasks )
- //&& array_key_exists( 'GLPI_TICKET_FOLLOWUP_REQUESTTYPES_ID', $infoForTasks )
- $createFollowup = true ;
- }
- // reset those variables
- $resultSave = $myProcessMaker->sendVariables( $myCase->getID() , array( "GLPI_ITEM_APPEND_TO_TASK" => '',
- "GLPI_ITEM_TASK_CONTENT" => '',
- "GLPI_NEXT_GROUP_TO_BE_ASSIGNED" => '',
- "GLPI_TICKET_FOLLOWUP_CONTENT" => '',
- "GLPI_TICKET_FOLLOWUP_IS_PRIVATE" => '',
- "GLPI_TICKET_FOLLOWUP_REQUESTTYPES_ID" => '',
- "GLPI_ITEM_TASK_ENDDATE" => '',
- "GLPI_ITEM_TASK_STARTDATE" => '',
- 'GLPI_ITEM_TITLE' => '',
- "GLPI_ITEM_SET_STATUS" => ''
- ) ) ;
-
- // print_r( $pmRouteCaseResponse ) ;
- // die() ;
-
- // now manage tasks associated with item
- $itemType = $myCase->getField('itemtype');
- $itemId = $myCase->getField('items_id');
-
- // switch own task to 'done' and create a new one
- $myProcessMaker->solveTask( $myCase->getID(), $_REQUEST['DEL_INDEX'], $txtToAppendToTask ) ;
-
- // create a followup if requested
- if( $createFollowup && $itemType == 'Ticket' ) {
- $myProcessMaker->addTicketFollowup( $itemId, $infoForTasks ) ;
- }
- $caseInfo = $myProcessMaker->getCaseInfo( $myCase->getID(), $_REQUEST['DEL_INDEX']) ;
- if( property_exists( $pmRouteCaseResponse, 'routing' ) ) {
- foreach( $pmRouteCaseResponse->routing as $route ) {
- $myProcessMaker->addTask( $itemType, $itemId, $caseInfo, $route->delIndex, PluginProcessmakerUser::getGLPIUserId( $route->userId ), $groupId, $route->taskId, $txtTaskContent, $taskStartDate, $taskEndDate ) ;
- // if end date was specicied, then must change due date of the PM task
- if( $taskEndDate != '' ) {
- $PM_DB->query( "UPDATE APP_DELEGATION SET DEL_TASK_DUE_DATE='$taskEndDate' WHERE APP_UID='".$caseInfo->caseId."' AND DEL_INDEX=".$route->delIndex);
- }
- }
- }
- if( $txtItemTitle != '') {
- // we are going to change the title of current GLPI Item
- $item = new $itemType ;
- $item->getFromDB( $itemId ) ;
- $item->update( array('id' => $itemId, 'name' => $txtItemTitle) ) ;
- }
-
- if( $itemSetStatus != '' ) {
- $myProcessMaker->setItemStatus($itemType, $itemId, $itemSetStatus ) ;
- }
- // evolution of case status: DRAFT, TO_DO, COMPLETED, CANCELLED
- $myCase->update( array( 'id' => $myCase->getID(), 'case_status' => $caseInfo->caseStatus ) ) ;
+ $myProcessMaker->derivateCase( $myCase, $_REQUEST); //, $_SERVER['HTTP_COOKIE'] ) ;
}
}
diff --git a/front/processmaker.form.php b/front/processmaker.form.php
index 755c10d..ca818e4 100644
--- a/front/processmaker.form.php
+++ b/front/processmaker.form.php
@@ -27,13 +27,13 @@ switch( $_POST["action"] ) {
$myProcessMaker = new PluginProcessmakerProcessmaker() ;
$myProcessMaker->login() ;
- $resultCase = $myProcessMaker->newCase( $_POST['plugin_processmaker_process_id'], array( 'GLPI_ITEM_CAN_BE_SOLVED' => 0, 'GLPI_SELFSERVICE_CREATED' => '1') ) ;
+ $resultCase = $myProcessMaker->newCase( $_POST['plugin_processmaker_process_id'], array( 'GLPI_ITEM_CAN_BE_SOLVED' => 0, 'GLPI_SELFSERVICE_CREATED' => '1', 'GLPI_URL' => $CFG_GLPI['url_base'].$CFG_GLPI['root_doc']) ) ;
if ($resultCase->status_code == 0){
// case is created
// Must show it...
//
$rand = rand( ) ;
- Html::redirect($CFG_GLPI['root_doc']."/plugins/processmaker/front/processmaker.helpdesk.form.php?process_id=".$_POST['plugin_processmaker_process_id']."&case_id=".$resultCase->caseId."&rand=$rand&itilcategories_id=".$_POST["itilcategories_id"]."&type=".$_REQUEST["type"]);
+ Html::redirect($CFG_GLPI['root_doc']."/plugins/processmaker/front/processmaker.helpdesk.form.php?process_id=".$_POST['plugin_processmaker_process_id']."&case_id=".$resultCase->caseId."&rand=$rand&itilcategories_id=".$_POST["itilcategories_id"]."&type=".$_REQUEST["type"]."&entities_id=".$_REQUEST['entities_id']);
} else {
Session::addMessageAfterRedirect($LANG['processmaker']['item']['error'][$resultCase->status_code]."
$resultCase->message ($resultCase->status_code)", true, ERROR); //echo "Error creating case: $resultCase->message \n";
diff --git a/front/processmaker.helpdesk.form.php b/front/processmaker.helpdesk.form.php
index 3392ca4..d36a7f6 100644
--- a/front/processmaker.helpdesk.form.php
+++ b/front/processmaker.helpdesk.form.php
@@ -20,8 +20,10 @@ function processMakerShowProcessList ($ID, $from_helpdesk) {
echo "";
echo "";
echo "";
+ echo "";
+ echo "";
// Dropdown::show('PluginProcessmakerProcessmaker', array( 'name' => 'plugin_processmaker_process_id', 'condition' => "is_active=1 and is_helpdeskvisible=1")); // condition is used to prevent start of none-active and none-helpdesk-visible cases
- PluginProcessmakerProcess::dropdown( array( 'entity' => $_SESSION['glpiactive_entity'], 'name' => 'plugin_processmaker_process_id' ));
+ PluginProcessmakerProcess::dropdown( array( 'value' => 0, 'entity' => $_SESSION['glpiactive_entity'], 'name' => 'plugin_processmaker_process_id' ));
echo "
| ".$caseInfo->processName." | "; //echo " |
|---|---|
| ".__('Describe the incident or request')." | "; if (Session::isMultiEntitiesMode()) { - echo "(".Dropdown::getDropdownName("glpi_entities", $_SESSION["glpiactive_entity"]).")"; + echo "(".Dropdown::getDropdownName("glpi_entities", $_REQUEST['entities_id']).")"; } echo " | "; echo ""; echo " | "; @@ -648,7 +648,7 @@ class PluginProcessmakerCase extends CommonDBTM { */ public static function canSolve ($parm) { $myCase = new self; - if( $myCase->getCaseFromItemTypeAndItemId( $parm->getType(), $parm->getID() ) ) { + if( $myCase->getCaseFromItemTypeAndItemId( $parm['item']->getType(), $parm['item']->getID() ) ) { $pmVar = $myCase->getVariables( array( 'GLPI_ITEM_CAN_BE_SOLVED' ) ) ; if( $myCase->fields['case_status'] != 'COMPLETED' && $myCase->fields['case_status'] != 'CANCELLED' && (!isset($pmVar['GLPI_ITEM_CAN_BE_SOLVED']) || $pmVar['GLPI_ITEM_CAN_BE_SOLVED'] != 1) ) { // then item can't be solved diff --git a/inc/caselink.class.php b/inc/caselink.class.php new file mode 100644 index 0000000..7ef6e54 --- /dev/null +++ b/inc/caselink.class.php @@ -0,0 +1,12 @@ +status_code = $status ; -// $this->message = $message ; -// if( $time_st == '' ) -// $this->time_stamp = date() ; -// else -// $this->time_stamp = $time_st ; -// } - -//} - - $pmHideSolution = false ; +if( !function_exists('http_formdata_flat_hierarchy') ) { /** * Summary of http_formdata_flat_hierarchy * @param mixed $data @@ -72,6 +48,7 @@ class getVariableStruct { return $vars; } +} /** * PluginProcessmakerProcessmaker short summary. @@ -103,7 +80,6 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { * * @return string **/ - static function getTable() { return "glpi_plugin_processmaker_processes"; @@ -113,10 +89,12 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { * Summary of addTicketFollowup * @param mixed $itemId * @param mixed $txtForFollowup + * @param integer $users_id optional, if null will uses logged-in user */ - public function addTicketFollowup( $itemId, $txtForFollowup ) { + public function addTicketFollowup( $itemId, $txtForFollowup, $users_id=null ) { $fu = new TicketFollowup() ; - $input = array() ; + $fu->getEmpty() ; // to get default values + $input = $fu->fields ; if( isset( $txtForFollowup['GLPI_TICKET_FOLLOWUP_CONTENT'] ) ) { $input['content']=$txtForFollowup['GLPI_TICKET_FOLLOWUP_CONTENT']; } @@ -127,7 +105,7 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $input['requesttypes_id']=$txtForFollowup['GLPI_TICKET_FOLLOWUP_REQUESTTYPES_ID']; } $input['tickets_id']=$itemId; - $input['users_id']= Session::getLoginUserID( true ) ; // $this->taskWriter; + $input['users_id']= (isset($users_id) ? $users_id : Session::getLoginUserID( true )) ; // $this->taskWriter; $fu->add( $input ) ; } @@ -192,38 +170,10 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { // return $tab; //} - //function useSession( $session, $lang = "en" ) { - // try { - // if( $this->openSoap( $lang ) ) { - // $this->pmSession = $session ; - // } - - // return $this->pmSession ; - // } - // catch ( Exception $e ) { - // Toolbox::logDebug( $e ); - // } - //} - - - //function openSession($username = "glpi", $password = "md5:2b0a4830b22f1f5ef5f8c239f9c8f07e", $lang="en" ) { - // try { - // if( $this->openSoap( $lang ) ) { - // $locSession = $this->pmSoapClient->login( array( 'userid' => $username, 'password' => $password) ) ; - // if( $locSession->status_code == 0 ) - // $this->pmSession = $locSession->message ; - // } - - // return $this->pmSession ; - // } - // catch ( Exception $e ) { - // Toolbox::logDebug( $e ); - // } - //} /** * Summary of login - * + * @param mixed $admin_or_user if true will be admin, otherwise is user name (or user id), or current user * @return true if login has been correctly done with current GLPI user, or if a PM session was already open * false if an exception occured (like SOAP error or PM login error) */ @@ -253,7 +203,11 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { // get the password of the user $pmusr = new PluginProcessmakerUser ; $gusr = new User ; - $gusr->getFromDBbyName( ($admin_or_user !== false ? $admin_or_user : $_SESSION["glpiname"]) ) ; + if( is_numeric($admin_or_user) ) { + $gusr->getFromDB( $admin_or_user ) ; + } else { + $gusr->getFromDBbyName( $admin_or_user !== false ? $admin_or_user : $_SESSION["glpiname"]) ; + } $pmusr->getFromDB( $gusr->getID() ) ; if( !isset($pmusr->fields['password']) || $pmusr->fields['password'] == "" ) { $pass = md5(Toolbox::encrypt( $gusr->getID().$gusr->getName().time(), GLPIKEY) ) ; @@ -264,7 +218,7 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $PM_DB->query("UPDATE rbac_users SET USR_PASSWORD='".$pass."' WHERE USR_UID='".$pmusr->fields['pm_users_id']."' ") ; $PM_DB->query("UPDATE users SET USR_PASSWORD='".$pass."' WHERE USR_UID='".$pmusr->fields['pm_users_id']."' ") ; } - $locSession = $this->pmSoapClient->login( array( 'userid' => ($admin_or_user !== false ? $admin_or_user : $_SESSION["glpiname"]), 'password' => 'md5:'.$pmusr->fields['password']) ) ; + $locSession = $this->pmSoapClient->login( array( 'userid' => $gusr->fields['name'], 'password' => 'md5:'.$pmusr->fields['password']) ) ; if( is_object( $locSession ) && $locSession->status_code == 0 ) { $_SESSION["pluginprocessmaker"]["session"]["id"] = $locSession->message ; $_SESSION["pluginprocessmaker"]["session"]["date"] = $locSession->timestamp ; @@ -438,7 +392,7 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { * Embedded taskList() PM web service call (definition: http://wiki.processmaker.com/index.php/2.0/ProcessMaker_WSDL_Web_Services#taskList.28.29) * A session must be open before with login() * Normalizes output to an array, even when only one element is returned by PM - * @return an array of taskListStruct objects, or false when exception occured + * @return array of taskListStruct objects, or false when exception occured */ function taskCase( $caseId ) { try { @@ -739,7 +693,7 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { * @param string $caseId The uID of the case * @param array $vars an array of variable name that will be read from the case as case variables * Normalizes output to an array, even when only one element is returned by PM - * @return an associative array (variable_name => value), or false when exception occured. The return array can be empty if requested variables are not found. + * @return array: an associative array (variable_name => value), or false when exception occured. The return array can be empty if requested variables are not found. */ function getVariables( $caseId, $vars = array() ) { try { @@ -987,12 +941,95 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { // return array('description' => $LANG['processmaker']['cron']['pmnotifications'] ); case 'pmorphancases' : return array('description' => $LANG['processmaker']['cron']['pmorphancases']['description'], 'parameter' => $LANG['processmaker']['cron']['pmorphancases']['parameter'] ); + case 'pmtaskactions' : + return array('description' => $LANG['processmaker']['cron']['pmtaskactions'] ); } return array(); } /** - * summary of cronOrphanCases + * summary of cronPMTaskActions + * Execute 1 task managed by the plugin + * @param: $task CronTask class for log / stat + * @return integer + * >0 : done + * <0 : to be run again (not finished) + * 0 : nothing to do + */ + static function cronPMTaskActions($task) { + global $DB, $CFG_GLPI; + + $actionCode = 0; // by default + $error = false ; + $task->setVolume(0); // start with zero + + $pm = new self; + $existingpmsession = isset($_SESSION["pluginprocessmaker"]["session"]) ; + $formerusers_id = 0 ; + // get the list of taskactions to be done + foreach( $DB->request( getTableForItemType('PluginProcessmakerCrontaskaction'), ' `state` = '.PluginProcessmakerCrontaskaction::DATAS_READY ) as $taskaction ) { + + try{ + + if( $formerusers_id != $taskaction['users_id']) { + unset($_SESSION["pluginprocessmaker"]["session"]) ; // to reset previous user login if any + } + + $pm->login($taskaction['users_id']); + + $postdatas = json_decode($taskaction['postdatas'], true) ; + + if( $taskaction['toclaim'] ) { + // must do a claim before solving task + if( !$pm->claimCase( $postdatas['APP_UID'], $postdatas['DEL_INDEX'] ) ) + throw new Exception("Can't claim case"); + + $donotif = $CFG_GLPI["use_mailing"]; + $CFG_GLPI["use_mailing"] = false ; + + // now manage tasks associated with item + $pm->claimTask( $postdatas['APP_UID'], $postdatas['DEL_INDEX'], $taskaction['users_id'] ) ; + + $CFG_GLPI["use_mailing"] = $donotif ; + + } + $myCase = new PluginProcessmakerCase ; + if( $myCase->getFromDB( $postdatas['APP_UID'] )) { + + //$cookies = json_decode($taskaction['cookies'], true) ; + $pm->derivateCase( $myCase, $postdatas, $taskaction['users_id'] ) ; + } + + $tkaction = new PluginProcessmakerCrontaskaction; + $tkaction->update( array( 'id' => $taskaction['id'], 'state' => PluginProcessmakerCrontaskaction::DONE ) ) ; + + $task->addVolume(1); + $task->log( "Applied task action id: '".$taskaction['id']."'" ) ; + + } catch( Exception $e ) { + $task->log( "Can't apply task action id: '".$taskaction['id']."'" ) ; + $error = true ; + } + + $formerusers_id = $taskaction['users_id'] ; + } + + if( $existingpmsession ) { + unset($_SESSION["pluginprocessmaker"]["session"]) ; // reset the one created during the foreach + if (!Session::isCron()) { + $pm->login(); // re-log default user + } + } + + + if($error) + return -1 ; + else + return $actionCode; + + } + /** + * summary of cronPMOrphanCases * Execute 1 task managed by the plugin * @param: $task CronTask class for log / stat * @return integer @@ -1003,6 +1040,10 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { static function cronPMOrphanCases($task) { global $PM_DB, $DB ; + if (!isset($PM_DB)) { + $PM_DB = new PluginProcessmakerDB ; + } + // get list of case assigned to GLPi items $draftCases = array(0) ; $query = "SELECT case_num FROM glpi_plugin_processmaker_cases WHERE case_status = 'DRAFT';" ; @@ -1125,7 +1166,11 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { * 0 : nothing to do */ static function cronPMUsers($task) { - global $DB, $PM_DB, $GLOBALS ; + global $DB, $PM_DB ; + + if (!isset($PM_DB)) { + $PM_DB = new PluginProcessmakerDB ; + } $actionCode = 0; // by default $error = false ; @@ -1153,8 +1198,8 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { foreach($DB->request("SELECT glpi_users.id, glpi_users.name, glpi_users.realname, glpi_users.firstname, glpi_users.is_active, glpi_users.is_deleted, glpi_plugin_processmaker_users.pm_users_id as pmUserId FROM glpi_users LEFT JOIN glpi_plugin_processmaker_users on glpi_plugin_processmaker_users.id = glpi_users.id - WHERE name not like '*%'") as $dbuser) { - $glpiUserList[ strtolower($dbuser['name'])] = $dbuser ; + WHERE name not like '*%'") as $dbgroup) { + $glpiUserList[ strtolower($dbgroup['name'])] = $dbgroup ; } $arrayDiff = array_diff_key( $glpiUserList, $pmUserList ) ; @@ -1259,50 +1304,57 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { - // so now treat GLPI pseudo-groups - foreach($DB->request("SELECT id, name, realname, firstname, is_active, is_deleted FROM glpi_users WHERE name LIKE '*%'") as $dbuser) { - $glpiPseudoUserList[$dbuser['name']] = $dbuser ; + // so now treat GLPI groups + $glpiGroupList = array(); + foreach($DB->request("SELECT id, name, is_task, is_usergroup FROM glpi_groups WHERE is_task=1 AND is_usergroup=1") as $dbgroup) { + $glpiGroupList[$dbgroup['name']] = $dbgroup; } - foreach($PM_DB->request("SELECT * FROM content WHERE content.CON_CATEGORY='GRP_TITLE' AND content.CON_LANG='en'") as $dbuser){ - $pmGroupList[$dbuser['CON_VALUE']] = $dbuser ; - } + $pmGroupList = self::getPMGroups(); // array(); + //foreach($PM_DB->request("SELECT * FROM content WHERE content.CON_CATEGORY='GRP_TITLE' AND content.CON_LANG='en'") as $dbgroup){ + // $pmGroupList[$dbgroup['CON_VALUE']] = $dbgroup; + //} // here we can compare group lists like done for the users - $arrayDiff = array_diff_key( $glpiPseudoUserList, $pmGroupList ) ; + $arrayDiff = array_diff_key( $glpiGroupList, $pmGroupList ) ; - // then for each pseudo-group we must check if it exists, and if not create a real PM group - foreach( $arrayDiff as $pseudoGroup ) { + // then for each group we must check if it exists, and if not create a real PM group + foreach( $arrayDiff as $group ) { // it is not existing in PM // then create - $pmResult = $myProcessMaker->createGroup( $pseudoGroup['name'] ) ; + $pmResult = $myProcessMaker->createGroup( $group['name'] ) ; if( $pmResult->status_code == 0) { $task->addVolume(1); - $task->log( "Added group: '".$pseudoGroup['name']."'" ) ; + $task->log( "Added group: '".$group['name']."'" ) ; } } - // review and update all users in each pseudo-groups - $pmGroupList = array() ; // reset groups and get the new complete list from PM DB - foreach($PM_DB->request("SELECT * FROM content WHERE content.CON_CATEGORY='GRP_TITLE' AND content.CON_LANG='en'") as $dbuser){ - $pmGroupList[$dbuser['CON_VALUE']] = $dbuser ; - } + // review and update all users in each group + $pmGroupList = self::getPMGroups(); // array(); + //$pmGroupList = array() ; // reset groups and get the new complete list from PM DB + //foreach($PM_DB->request("SELECT * FROM content WHERE content.CON_CATEGORY='GRP_TITLE' AND content.CON_LANG='en'") as $dbgroup){ + // $pmGroupList[$dbgroup['CON_VALUE']] = $dbgroup ; + //} // now should refresh the existing users into groups - $arrayDiff = array_intersect_key( $glpiPseudoUserList, $pmGroupList ) ; - foreach( $arrayDiff as $pseudoGroup ){ - // for each pseudo group will delete users and re-create them + $arrayDiff = array_intersect_key( $glpiGroupList, $pmGroupList ) ; + foreach( $arrayDiff as $group ){ + // for each group will delete users and re-create them // not really optimized, but this way we are sure that groups are synchronized // must be redesigned - $query = "DELETE FROM group_user WHERE group_user.GRP_UID='".$pmGroupList[$pseudoGroup['name']]['CON_ID']."';"; + $query = "DELETE FROM group_user WHERE group_user.GRP_UID='".$pmGroupList[$group['name']]['CON_ID']."';"; $PM_DB->query( $query ) ; // and insert all users from real GLPI group - foreach( $DB->request("select glpi_groups_users.users_id, glpi_plugin_processmaker_users.pm_users_id from glpi_groups join glpi_groups_users on glpi_groups_users.groups_id=glpi_groups.id join glpi_plugin_processmaker_users on glpi_plugin_processmaker_users.id=glpi_groups_users.users_id where glpi_groups.name='".$pseudoGroup['name']."'") as $user ) { - $query = "INSERT INTO group_user (`GRP_UID`, `USR_UID`) VALUES ( '".$pmGroupList[$pseudoGroup['name']]['CON_ID']."', '".$user['pm_users_id']."' )" ; + foreach( $DB->request("SELECT glpi_groups_users.users_id, glpi_plugin_processmaker_users.pm_users_id + FROM glpi_groups + JOIN glpi_groups_users ON glpi_groups_users.groups_id=glpi_groups.id + JOIN glpi_plugin_processmaker_users ON glpi_plugin_processmaker_users.id=glpi_groups_users.users_id + WHERE glpi_groups.name='".$group['name']."'") as $user ) { + $query = "INSERT INTO group_user (`GRP_UID`, `USR_UID`) VALUES ( '".$pmGroupList[$group['name']]['CON_ID']."', '".$user['pm_users_id']."' )" ; $PM_DB->query( $query ) ; } $task->addVolume(1); - $task->log( "Updated users into PM group: '".$pseudoGroup['name']."'" ) ; + $task->log( "Updated users into PM group: '".$group['name']."'" ) ; } // now should renew the duedate of the users @@ -1327,10 +1379,17 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $caseInfo = $myProcessMaker->getCaseInfo( $parm->input['processmaker_caseid'], $parm->input['processmaker_delindex']) ; $parm->input['name'] = $PM_DB->escape($caseInfo->caseName ) ; - $caseInitialDueDate = $myProcessMaker->getVariables( $parm->input['processmaker_caseid'], array( 'GLPI_ITEM_INITIAL_DUE_DATE' )) ; + $caseInitialDueDate = $myProcessMaker->getVariables( $parm->input['processmaker_caseid'], array( 'GLPI_ITEM_TITLE', 'GLPI_ITEM_INITIAL_DUE_DATE' )) ; if( array_key_exists( 'GLPI_ITEM_INITIAL_DUE_DATE', $caseInitialDueDate ) ) { $parm->input['due_date'] = $caseInitialDueDate[ 'GLPI_ITEM_INITIAL_DUE_DATE' ]." 23:59:59" ; } + $txtItemTitle = $caseInfo->caseName; + if( array_key_exists( 'GLPI_ITEM_TITLE', $caseInitialDueDate ) ) { + $txtItemTitle = $caseInitialDueDate[ 'GLPI_ITEM_TITLE' ] ; + // reset those variables + $resultSave = $myProcessMaker->sendVariables( $parm->input['processmaker_caseid'] , array( "GLPI_ITEM_TITLE" => '') ) ; + } + $parm->input['name'] = $PM_DB->escape($txtItemTitle ) ; $procDef = new PluginProcessmakerProcess; $procDef->getFromDBbyExternalID( $caseInfo->processId ) ; @@ -1371,7 +1430,7 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { // here we create a fake task that will be used to store the creator of the case // this is due for traceability only - $myProcessMaker->add1stTask( $myCase->fields['itemtype'], $myCase->fields['items_id'], $caseInfo); + $myProcessMaker->add1stTask( $myCase->fields['itemtype'], $myCase->fields['items_id'], $caseInfo, array( 'notif' => false) ); // no notif // route case $pmRouteCaseResponse = $myProcessMaker->routeCase( $myCase->getID(), $parm->input['processmaker_delindex'] ) ; @@ -1397,11 +1456,16 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { "GLPI_ITEM_SET_STATUS" => '' ) ) ; // routing has been done, then solve 1st task - $myProcessMaker->solveTask( $myCase->getID(), $parm->input['processmaker_delindex'], $txtToAppendToTask ) ; + $myProcessMaker->solveTask( $myCase->getID(), $parm->input['processmaker_delindex'], array( 'txtToAppend' => $txtToAppendToTask, 'notif' => false) ) ; // and create GLPI tasks for the newly created PM tasks. foreach( $pmRouteCaseResponse->routing as $route ) { - $myProcessMaker->addTask( $myCase->fields['itemtype'], $myCase->fields['items_id'], $caseInfo, $route->delIndex, PluginProcessmakerUser::getGLPIUserId( $route->userId ), 0, $route->taskId ) ; + $myProcessMaker->addTask( $myCase->fields['itemtype'], + $myCase->fields['items_id'], + $caseInfo, $route->delIndex, + PluginProcessmakerUser::getGLPIUserId( $route->userId ), + 0, + $route->taskId ) ; } if( $itemSetStatus != '' ) { @@ -1453,54 +1517,78 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { } - //function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { - // global $LANG; - - // if (!$withtemplate) { - // switch ($item->getType()) { - // case 'Phone' : - // if ($_SESSION['glpishow_count_on_tabs']) { - // return self::createTabEntry('Example', - // countElementsInTable($this->getTable())); - // } - // return 'Example'; - // } - // } - // return ''; - //} - - ///** - // * Summary of displayTabContentForItem - // * @param CommonGLPI $item - // * @param integer $tabnum - // * @param integer $withtemplate - // * @return boolean - // */ - //static function displayTabContentForItem(CommonGLPI $item, integer $tabnum=1, integer $withtemplate=0) { - - // if ($item->getType()=='Phone') { - // echo "Plugin Example on Phone"; - // } - // return true; - //} + /** + * Summary of addWatcher + * add $techId as watcher to $glpi_item when techId has no rights on it + * @param string $itemType is the type of the CommonITILObject + * @param integer $itemId is the id of the ITIL object + * @param integer $techId is the users_id to be added + * @return boolean true if added, + */ + public static function addWatcher( $itemType, $itemId, $techId ) { + global $CFG_GLPI; // currently use $CFG_GLPI to disable notif + //$plug = new Plugin; + if( $techId && $itemType != '' && $itemId > 0 ) { //!$plug->isActivated('arbehaviours') && + $glpi_item = getItemForItemtype( $itemType ); + $glpi_item->getFromDB( $itemId ) ; + // then we should check if this user has rights on the item, if not then we must add it to the watcher list! + $glpi_item = getItemForItemtype( $itemType ); + $glpi_item->getFromDB( $itemId ) ; + $user_entities = Profile_User::getUserEntities( $techId, true, true ) ; + $user_can_view = in_array( $glpi_item->fields['entities_id'], $user_entities ); + if( !$glpi_item->isUser( CommonITILActor::REQUESTER, $techId ) + && !$glpi_item->isUser( CommonITILActor::OBSERVER, $techId ) + && !$glpi_item->isUser( CommonITILActor::ASSIGN, $techId ) + && !$user_can_view ) { + // then we must add this tech user to watcher list + $glpi_item_user = getItemForItemtype( $glpi_item->getType() . "_User" ); + $donotif = $CFG_GLPI["use_mailing"]; + $CFG_GLPI["use_mailing"] = false ; + $glpi_item_user->add( array( $glpi_item::getForeignKeyField() => $glpi_item->getId(), 'users_id' => $techId, 'type' => CommonITILActor::OBSERVER, '_disablenotif' => true ) ) ; // , '_no_notif' => true + $CFG_GLPI["use_mailing"]= $donotif; + return true; + } + } + return false; + } /** * Summary of addTask * adds a GLPI task to given item - * @param $itemType String item type to which a task must be added - * @param $iteId integer item# to which a task must be added - * @param $caseInfo getCaseInfoResponse object (see: getCaseInfo() function) - * @param $del_index integer index of the delegation - * @param $techId integer GLPI user id to which a task must be assigned, if == 0 then we should look-up in DB the pseudo-group to be assigned to the task + * @param $itemType string item type to which a task will be added + * @param $itemId integer item id to which a task will be added + * @param $caseInfo mixed getCaseInfoResponse object (see: getCaseInfo() function) + * @param $delIndex integer index of the delegation + * @param $techId integer GLPI user id to which a task must be assigned, if == 0 will use $groupId and/or $pmTaskId + * @param $groupId string PM group guid to assign to task, used when $techId == 0 + * @param $pmTaskId string PM task guid, used when $groupId == 0 AND $techID == 0 + * @param $options array of options, default values are + * 'txtTaskContent' => '', + * 'start_date' => '', + * 'end_date' => '', + * 'notif' => true * @return */ - public function addTask( $itemType, $itemId, $caseInfo, $delIndex, $techId, $groupId, $pmTaskId, $txtTaskContent = '', $start_date = '', $end_date = '' ) { - global $DB, $PM_DB, $LANG, $CFG_GLPI, $_SESSION ; + public function addTask( $itemType, $itemId, $caseInfo, $delIndex, $techId, $groupId, $pmTaskId, $options=array() ) { + global $DB, $PM_DB, $LANG, $CFG_GLPI, $_SESSION ; + + $default_options = array( + 'txtTaskContent' => '', + 'start_date' => '', + 'end_date' => '', + 'notif' => true + ) ; + foreach($default_options as $key => $opt){ + if(!isset($options[$key])){ + $options[$key] = $opt ; + } + } $glpi_task = getItemForItemtype( "{$itemType}Task" ); + $glpi_task->getEmpty(); $input = array() ; // will contain all data for the Task @@ -1514,96 +1602,67 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $pmProcess = new PluginProcessmakerProcess ; $pmProcess->getFromDB( $pmtaskcat->fields['processes_id'] ) ; - if( $start_date == '' ) { - $start_date = new DateTime( $_SESSION["glpi_currenttime"] ) ; + if( $options['start_date'] == '' ) { + $options['start_date'] = new DateTime( $_SESSION["glpi_currenttime"] ) ; } else { - $start_date = new DateTime( $start_date ) ; + $options['start_date'] = new DateTime( $options['start_date'] ) ; } - $input['begin'] = $start_date->format("Y-m-d H:i:s"); + $input['begin'] = $options['start_date']->format("Y-m-d H:i:s"); - if( $end_date == '' || $end_date <= $input['begin']) { - $end_date = $start_date ; - $end_date->add( new DateInterval('PT1M') ) ; + if( $options['end_date'] == '' || $options['end_date'] <= $input['begin']) { + $options['end_date'] = clone $options['start_date'] ; + $options['end_date']->add( new DateInterval('PT15M') ) ; } else { - $end_date = new DateTime( $end_date ) ; + $options['end_date'] = new DateTime( $options['end_date'] ) ; + } + $input['end'] = $options['end_date']->format("Y-m-d H:i:s") ; + $input['plan']['begin'] = $input['begin']; + $temp = $options['start_date']->diff( $options['end_date'] ); + $input['plan']['_duration'] = $temp->d * DAY_TIMESTAMP + $temp->h * HOUR_TIMESTAMP + $temp->i * MINUTE_TIMESTAMP + $temp->s; + if( $input['plan']['_duration'] == 0 ) { + $input['plan']['_duration'] = 60; // at least } - $input['end'] = $end_date->format("Y-m-d H:i:s") ; - //if( $end_date == '' ) { - // $loc_date = new DateTime( $_SESSION["glpi_currenttime"] ) ; - // $input['begin'] = $loc_date->format("Y-m-d H:i:s"); - // $loc_date->add( new DateInterval('PT1M') ) ; - // $input['end'] = $loc_date->format("Y-m-d H:i:s") ; - //} else { - // $loc_date = new DateTime( $end_date) ; - // $input['end'] = $loc_date->format("Y-m-d H:i:s") ; - // $loc_date->sub( new DateInterval('PT1M') ) ; - // $input['begin'] = $loc_date->format("Y-m-d H:i:s"); - //} - - //$input['date'] = $start_date->format("Y-m-d H:i:s"); $input['users_id'] = $this->taskWriter ; - $user_can_view = true ; // by default - $plug = new Plugin; - // manage groups if( $techId == 0 ) { // then we must look-up DB to get the pseudo-group that will be assigned to the task - $pseudogroup=''; + $groupname=''; if( $groupId == 0 ) { $query = "SELECT content.CON_VALUE FROM task_user JOIN content ON content.CON_ID=task_user.USR_UID AND content.CON_CATEGORY='GRP_TITLE' AND content.CON_LANG = 'en' WHERE task_user.tas_uid='$pmTaskId' AND task_user.tu_relation=2 LIMIT 1;" ; - //$query = "select glpi.glpi_users.id as glpi_user_id from wf_workflow.task_user - // join wf_workflow.content on wf_workflow.content.CON_ID=wf_workflow.task_user.USR_UID and wf_workflow.content.CON_CATEGORY='GRP_TITLE' and wf_workflow.content.CON_LANG = 'en' - // join glpi.glpi_users on glpi.glpi_users.name=wf_workflow.content.CON_VALUE COLLATE utf8_unicode_ci - // where wf_workflow.task_user.tas_uid='$pmTaskId' and wf_workflow.task_user.tu_relation=2 LIMIT 1;" ; } else { $query = "SELECT CON_VALUE FROM content WHERE content.CON_ID='$groupId' AND content.CON_CATEGORY='GRP_TITLE' AND content.CON_LANG = 'en' ;" ; - //$query = "select glpi.glpi_users.id as glpi_user_id from wf_workflow.content - // join glpi.glpi_users on glpi.glpi_users.name=wf_workflow.content.CON_VALUE COLLATE utf8_unicode_ci - // where wf_workflow.content.CON_ID='$groupId' and wf_workflow.content.CON_CATEGORY='GRP_TITLE' and wf_workflow.content.CON_LANG = 'en' ;" ; } // as there is a LIMIT of 1 // or // as there is only one group per guid // then we should have at maximun 1 record foreach( $PM_DB->request($query) as $onlyrec ) { - $pseudogroup = $onlyrec['CON_VALUE'] ; + $groupname = $onlyrec['CON_VALUE'] ; } - $query = "SELECT glpi.glpi_users.id AS glpi_user_id FROM glpi.glpi_users WHERE glpi.glpi_users.name LIKE '$pseudogroup';"; + $groups_id_tech = 0; + $query = "SELECT id AS glpi_group_id FROM glpi_groups WHERE name LIKE '$groupname';"; $res = $DB->query($query) ; if( $DB->numrows($res) > 0) { $row = $DB->fetch_array( $res ) ; - $techId = $row['glpi_user_id'] ; + $groups_id_tech = $row['glpi_group_id'] ; } - } elseif( !$plug->isActivated('arbehaviours') ) { // check is done during Task add in this plugin - // this is a real user not a pseudo-user - // then we should check if this user has rights on the item, if not then we must add it to the watcher list! - $glpi_item = getItemForItemtype( $itemType ); - $glpi_item->getFromDB( $itemId ) ; - //$glpi_tech = new User() ; - //$glpi_tech->getFromDB( $techId ) ; - // several possibilities - // $techId may be requester - $user_entities = Profile_User::getUserEntities( $techId, true, true ) ; - $user_can_view = in_array( $glpi_item->fields['entities_id'], $user_entities ); - if( !$glpi_item->isUser( CommonITILActor::REQUESTER, $techId ) && !$glpi_item->isUser( CommonITILActor::OBSERVER, $techId ) && !$glpi_item->isUser( CommonITILActor::ASSIGN, $techId ) && !$user_can_view ) { - // then we must add this tech user to watcher list - $glpi_item_user = getItemForItemtype( "{$itemType}_User" ); - $glpi_item_user->add( array( strtolower(getPlural( $itemType )).'_id' => $itemId, 'users_id' => $techId, 'type' => CommonITILActor::OBSERVER, '_no_notif' => true ) ) ; - } + } else { + // adds the user tech to ticket watcher if neccessary + self::addWatcher( $itemType, $itemId, $techId ); } // manage task description $input['content'] = "" ; // by default empty :) - if( $txtTaskContent != '' ) { - $input['content'] = $txtTaskContent ; + if( $options['txtTaskContent'] != '' ) { + $input['content'] = $options['txtTaskContent'] ; } elseif( !$pmProcess->fields["hide_case_num_title"] ) { $input['content'] = $LANG['processmaker']['item']['task']['case'].$caseInfo->caseName ; @@ -1618,25 +1677,23 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $input['is_private'] = 0 ; $input['actiontime'] = 0 ; - //$input['begin'] = $start_date->format("Y-m-d H:i:s"); - //$start_date->add( new DateInterval('PT1M') ) ; - //$input['end'] = $start_date->format("Y-m-d H:i:s") ; $input['state'] = 1 ; // == TO_DO - $input['users_id_tech'] = $techId ; // Session::getLoginUserID() ; //PluginProcessmakerUsers::getGLPIUserId( $pmInfo['caseInfo']->currentUsers[0]->userId ) ; + if( $techId ) { + $input['users_id_tech'] = $techId; + } elseif( $groups_id_tech ) { + $input['groups_id_tech'] = $groups_id_tech; + } + $donotif = $CFG_GLPI["use_mailing"]; + if( !$options['notif'] ) { + //$input['_no_notif'] = true; + $CFG_GLPI["use_mailing"] = false; + } $glpi_task->add( Toolbox::addslashes_deep( $input ) ) ; + $CFG_GLPI["use_mailing"] = $donotif; if($glpi_task->getId() > 0 ) { - // task has been created then send notifications for tech with no default rigths to the item entity - // now done in GLPI core - //if( !$user_can_view ) { - // // to cheat the entity rigths, passes default user_entity to raiseEvent(), to be sure that task_tech will receive a notification. - // // drawback: notifications that are entity based could be missing. - // // tip: $user_entities[0] is the user's default entity - // NotificationEvent::raiseEvent('add_task', $glpi_item, array( 'entities_id' => $user_entities[0], 'task_id' => $glpi_task->getId(), 'is_private' => 0 ) ) ; - //} - - // and store link to task in DB + // stores link to task in DB $query = "INSERT INTO glpi_plugin_processmaker_tasks (items_id, itemtype, case_id, del_index) VALUES (".$glpi_task->getId().", '".$glpi_task->getType()."', '".$caseInfo->caseId."', ".$delIndex.");" ; $DB->query( $query ) ; } @@ -1645,15 +1702,42 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { /** * Summary of add1stTask * adds a GLPI task to ticket - * @param $ticket_id integer ticket# to which a task must be added - * @param $caseInfo getCaseInfoResponse object (see: getCaseInfo() function) + * @param $intemType string itemtype of object (Ticket, Problem, ....) + * @param $itemId integer item id to which a task will be added + * @param $caseInfo mixed getCaseInfoResponse object (see: getCaseInfo() function) + * @param $options array of options, defaults are: + * 'txtTaskContent' => '', is content of the task + * 'userId' => false, is user id to be assigned to task + * 'notif' => true, if true notifications will be sent * @return */ - public function add1stTask ( $itemType, $itemId, $caseInfo, $txtTaskContent='', $userId=false ) { + public function add1stTask ( $itemType, $itemId, $caseInfo, $options=array() ) { + + $default_options = array( + 'txtTaskContent' => '', + 'userId' => false, + 'notif' => true + ) ; + foreach($default_options as $key => $opt){ + if(!isset($options[$key])){ + $options[$key] = $opt ; + } + } $start_date = new DateTime( $_SESSION["glpi_currenttime"] ) ; $official_date_time = $_SESSION["glpi_currenttime"] ; $_SESSION["glpi_currenttime"] = $start_date->sub(new DateInterval("PT1S"))->format("Y-m-d H:i:s") ; - $this->addTask( $itemType, $itemId, $caseInfo, $caseInfo->currentUsers[0]->delIndex, ($userId ? $userId : Session::getLoginUserID()), 0, $caseInfo->currentUsers[0]->taskId, $txtTaskContent ) ; + $userId = $options['userId'] ? $options['userId'] : Session::getLoginUserID() ; + unset($options['userId']); // unset it as it's not in the options of addTask + + $this->addTask( $itemType, + $itemId, + $caseInfo, + $caseInfo->currentUsers[0]->delIndex, + $userId, + 0, + $caseInfo->currentUsers[0]->taskId, + $options + ) ; $_SESSION["glpi_currenttime"] = $official_date_time ; } @@ -1704,7 +1788,7 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { * @param mixed $newTech */ public function reassignTask ( $caseId, $delIndex, $newDelIndex, $newTech) { - global $DB ; + global $DB, $CFG_GLPI ; // $CFG_GLPI is only used to _disablenotif $query = "SELECT * FROM glpi_plugin_processmaker_tasks WHERE case_id='$caseId' and del_index=$delIndex; "; $res = $DB->query($query) ; @@ -1714,21 +1798,28 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $glpi_task->getFromDB( $row['items_id'] ) ; $itemType = str_replace( 'Task', '', $row['itemtype'] ) ; - $glpi_item = getItemForItemtype( $itemType ); - $glpi_item->getFromDB( $glpi_task->fields[ getForeignKeyFieldForItemType( $itemType ) ] ) ; + $foreignkey = getForeignKeyFieldForItemType( $itemType ) ; - $plug = new Plugin; - if( !$plug->isActivated('arbehaviours') ) { // check is done during Task update in this plugin - $user_entities = Profile_User::getUserEntities( $newTech, true, true ) ; - $user_can_view = in_array( $glpi_item->fields['entities_id'], $user_entities ); - if( !$glpi_item->isUser( CommonITILActor::REQUESTER, $newTech ) && !$glpi_item->isUser( CommonITILActor::OBSERVER, $newTech ) && !$glpi_item->isUser( CommonITILActor::ASSIGN, $newTech ) && !$user_can_view ) { - // then we must add this tech user to watcher list - $glpi_item_user = getItemForItemtype( "{$itemType}_User" ); - $glpi_item_user->add( array( $glpi_item->getForeignKeyField() => $glpi_item->getId() , 'users_id' => $newTech, 'type' => CommonITILActor::OBSERVER, '_no_notif' => true ) ) ; - } - } + //$glpi_item = getItemForItemtype( $itemType ); + //$glpi_item->getFromDB( $glpi_task->fields[ getForeignKeyFieldForItemType( $itemType ) ] ) ; - $glpi_task->update( array( 'id' => $row['items_id'], $glpi_item->getForeignKeyField() => $glpi_item->getId(), 'users_id_tech' => $newTech )) ; + //$plug = new Plugin; + //if( !$plug->isActivated('arbehaviours') ) { // check is done during Task update in this plugin + // $user_entities = Profile_User::getUserEntities( $newTech, true, true ) ; + // $user_can_view = in_array( $glpi_item->fields['entities_id'], $user_entities ); + // if( !$glpi_item->isUser( CommonITILActor::REQUESTER, $newTech ) && !$glpi_item->isUser( CommonITILActor::OBSERVER, $newTech ) && !$glpi_item->isUser( CommonITILActor::ASSIGN, $newTech ) && !$user_can_view ) { + // // then we must add this tech user to watcher list + // $glpi_item_user = getItemForItemtype( "{$itemType}_User" ); + // $donotif = $CFG_GLPI["use_mailing"] ; + // $CFG_GLPI["use_mailing"] = false; + // $glpi_item_user->add( array( $glpi_item->getForeignKeyField() => $glpi_item->getId() , 'users_id' => $newTech, 'type' => CommonITILActor::OBSERVER ) ) ; // , '_no_notif' => true + // $CFG_GLPI["use_mailing"] = $donotif; + // } + //} + + self::addWatcher( $itemType, $glpi_task->fields[ $foreignkey ], $newTech ); + + $glpi_task->update( array( 'id' => $row['items_id'], $foreignkey => $glpi_task->fields[ $foreignkey ], 'users_id_tech' => $newTech )) ; // this is now done into GLPI core //if( !$user_can_view ) { @@ -1746,11 +1837,26 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { /** * Summary of solveTask - * @param mixed $caseId - * @param mixed $delIndex + * @param string $caseId + * @param int $delIndex + * @param array $options + * 'txtToAppend' => '' : text to append to solved task + * 'notif' => true : if true will send notifications + * 'users_id_tech' => is the users_id of the tech that solved the task */ - public function solveTask( $caseId, $delIndex, $txtToAppend = '' ) { - global $DB ; + public function solveTask( $caseId, $delIndex, $options=array() ) { + global $DB, $CFG_GLPI ; + + $default_options = array( + 'txtToAppend' => '', + 'notif' => true, + 'users_id_tech' => null + ) ; + foreach($default_options as $key => $opt){ + if(!isset($options[$key])){ + $options[$key] = $opt ; + } + } $query = "SELECT * FROM glpi_plugin_processmaker_tasks WHERE case_id='$caseId' and del_index=$delIndex; "; $res = $DB->query($query) ; @@ -1763,17 +1869,37 @@ class PluginProcessmakerProcessmaker extends CommonDBTM { $itemFKField = getForeignKeyFieldForItemType( $glpi_task->getItilObjectItemType() ) ; $hostItem->getFromDB( $glpi_task->fields[ $itemFKField ] ) ; $duration = $this->computeTaskDuration( $glpi_task, $hostItem->fields['entities_id'] ) ; - if( $txtToAppend <> "" ) $txtToAppend = "\n