tomolimo / processmaker

GLPI plugin that provides an interface with ProcessMaker (http://www.processmaker.com/)
30 stars 11 forks source link

Add input and output documents from processmaker to glpi #134

Closed jorge-madrigal closed 9 months ago

jorge-madrigal commented 2 years ago

Hello:

We have modified your code to add input and output documents in glpi tickets. You can use it for yours proyects.

Code of processmaker.class.php modified:

// here we create a fake task that will be used to store the creator of the case // this is due for traceability only

     $PM_SOAP->add1stTask($myCase->getID(), $myCase->fields['itemtype'], $myCase->fields['items_id'], $caseInfo,[ 'notif' => false] ); // no notif

     //Debo añadir los documentos generados por el usuario. A la vez verifico que los documentos no se hayan incorpora ya en la base de datos
     //We must add the output documents. We verify that the documents do not exist in the GLPI database

     $PM_SOAP->addGeneratedDocuments($myCase->fields['case_guid'], $myCase->fields['itemtype'], $myCase->fields['items_id'],Session::getLoginUserID(),$myCase->fields['plugin_processmaker_processes_id']);

     //Debo añadir los documentos de entrada por el usuario.  A la vez verifico que los documentos no se hayan incorpora ya en la base de datos
     //We must add the input documents. We verify that the documents do not exist in the GLPI database         

     $PM_SOAP->addInputDocuments($myCase->fields['case_guid'], $myCase->fields['itemtype'], $myCase->fields['items_id'],Session::getLoginUserID(),$myCase->fields['plugin_processmaker_processes_id']);

In DerivaseCase;

// now manage tasks associated with item // switch own task to 'done' and create a new one $this->solveTask($myCase->getID(), $request['DEL_INDEX'], ['txtToAppend' => $txtToAppendToTask, 'users_id_tech' => $users_id, 'begin' => $solvedTaskStartDate, 'end' => $solvedTaskEndDate, 'toInformation' => $solvedTaskSetToInformation ] );

  //Despues de resolver la tarea debo revisar si hay nuevos documentos de entrada o generados. Si los hay los incorporo
  //After solving the task I must check if there are new input or generated documents. If there are, I incorporate them

  //Debo añadir los documentos generados por el usuario. A la vez verifico que los documentos no se hayan incorpora ya en la base de datos
  //I must add the user generated documents. At the same time I verify that the documents have not already been incorporated into the database

  $this->addGeneratedDocuments($myCase->fields['case_guid'], $myCase->fields['itemtype'], $myCase->fields['items_id'],Session::getLoginUserID(),$myCase->fields['plugin_processmaker_processes_id']);

  //Debo añadir los documentos de entrada por el usuario.  A la vez verifico que los documentos no se hayan incorpora ya en la base de datos
  //I must add the user generated documents. At the same time I verify that the documents have not already been incorporated into the database

  $this->addInputDocuments($myCase->fields['case_guid'], $myCase->fields['itemtype'], $myCase->fields['items_id'],Session::getLoginUserID(),$myCase->fields['plugin_processmaker_processes_id']);

The addGeneratedDocuments and addInputDocuments functions:

public function addGeneratedDocuments($caseGuid,$itemtype, $itemId, $users_id = null,$id_proceso_glpi) { global $DB; global $PM_SOAP;

  $config = $PM_SOAP->config;

 // Intento sacar todos los documentos generados del caso
 // I try to read all the generated documents of the case

 $doc = new document();//new document();
 $reldoc = new document_item(); // new relation document

  $pmOutputDocumentList = $this->outputDocumentList($caseGuid);
  $CaseNumber = $this->getCaseInfo($caseGuid)->caseNumber;

           if ($pmOutputDocumentList != (object) NULL)
           {
                if (is_array($pmOutputDocumentList)) {  // Si no es un array, no hay documentos. If it isn't an array, there are no documents.

                    foreach ($pmOutputDocumentList as $documentos) {

                                   //Añado los documentos
                                   //I add the documents

                                   $doc->getEmpty(); // to get default values

                                   $input = $doc->fields;
                                   $input['entities_id']=0;
                                   $input['is_recursive']=0;
                                   $input['name']=$documentos->filename;
                                   $input['users_id'] = (isset($users_id) ? $users_id : Session::getLoginUserID( true )); // $this->taskWriter;
                                   $input['link'] = $config->fields['pm_server_URL'] . "/sysworkflow/neoclassic/en/".$documentos->link;

                                   $input['sha1sum']=sha1_file($input['link']);

                                   // Busco el documento para ver si existe
                                   // Si existe no lo incorporo
                                   // Si no existe lo agrego

                                   // I look for the document to see if it exits
                                   // If it exits, i don't add it
                                   // If it don' exit, i add it

                                   // Para buscar, mando el enlace. La función calcula el sha1 del fichero y lo busca en la base de datos.
                                   // Si existe devuelve el id del documento, si no existe devuelve=false

                                   //To search for it, I send the link. The function calculates the sha1 of the file and looks for it in the database.
                                   //If it exists it returns the id of the document, if it does not exist it returns=false

                                   if ($doc->getFromDBbyContent($input['entities_id'],$input['link'])==false) {

                                               // Incorporo el documento en GLPI
                                               // I add the document to GLPI
                                               $id_document = $this->cargarDocumentoEnGLPI($input);

                                   }
                                   //Añado la relación entre el documento y el ticket
                                   //I add the relationship between document and ticket

                                   $reldoc->getEmpty();
                                   $input = $reldoc->fields;
                                   $input['documents_id']=$id_document;
                                   $input['entities_id']=0;
                                   $input['is_recursive']=0;
                                   $input['itemtype']="Ticket";
                                   $input['items_id']=$itemId;
                                   $input['users_id'] = (isset($users_id) ? $users_id : Session::getLoginUserID( true )); // $this->taskWriter;
                                   $date_mod = new DateTime( $_SESSION["glpi_currenttime"] );
                                   $input['date_mod'] =$date_mod->format("Y-m-d H:i:s");
                                   $reldoc->add ($input); //añado relación entre el documento y el ticket

                                   //Añado la relación entre el documento y el usuario
                                   //I add the relationship between document and user

                                   $input['itemtype']="User";
                                   $input['items_id']=(isset($users_id) ? $users_id : Session::getLoginUserID( true )); // Meto el usuario que relaciono
                                   $reldoc->add ($input); //añado la relación entre documento y usuario. I add the relationship between document and user

                    } //fin foreach
                 } //fin es array

           } //Fin no es nulo

} //fin funcion

public function addInputDocuments($caseGuid,$itemtype, $itemId, $users_id = null,$id_proceso_glpi) { global $DB; global $PM_SOAP;

  $config = $PM_SOAP->config;

  // Intento sacar todos los documentos de entrada del caso
  // I try to read all the generated documents of the case

 $doc = new document();//new document();
 $reldoc = new document_item(); // new relation document

  $pmInputDocumentList = $this->inputDocumentList($caseGuid);
  $CaseNumber = $this->getCaseInfo($caseGuid)->caseNumber;

           if ($pmInputDocumentList != (object) NULL)
           {

              if (is_array($pmInputDocumentList)) { // Si no es un array, no hay documentos. If it isn't an array, there are no documents.

                 foreach ($pmInputDocumentList as $documentos) {

                                //Añado los documentos
                                //I add the documents

                                $doc->getEmpty(); // to get default values

                                $input = $doc->fields;
                                $input['entities_id']=0;
                                $input['is_recursive']=0;
                                $input['name']=$documentos->filename;
                                $input['users_id'] = (isset($users_id) ? $users_id : Session::getLoginUserID( true )); // $this->taskWriter;
                                $input['link'] = $config->fields['pm_server_URL'] . "/sysworkflow/neoclassic/en/".$documentos->link;
                                $input['sha1sum']=sha1_file($input['link']);

                                // Busco el documento para ver si existe
                                // Si existe no lo incorporo
                                // Si no existe lo agrego
                                // I look for the document to see if it exits
                                // If it exits, i don't add it
                                // If it don' exit, i add it

                                // Para buscar, mando el enlace. La función calcula el sha1 del fichero y lo busca en la base de datos.
                                // Si existe devuelve el id del documento, si no existe devuelve false
                                //To search for it, I send the link. The function calculates the sha1 of the file and looks for it in the database.
                                //If it exists it returns the id of the document, if it does not exist it returns=false

                                if ($doc->getFromDBbyContent($input['entities_id'],$input['link'])==false) {

                                            // Incorporo el documento en GLPI
                                            // I add the document to GLPI

                                            $id_document = $this->cargarDocumentoEnGLPI($input);

                                 }
                                //Añado la relación entre el documento y el ticket
                                //I add the relationship between document and ticket

                                $reldoc->getEmpty();
                                $input = $reldoc->fields;
                                $input['documents_id']=$id_document;
                                $input['entities_id']=0;
                                $input['is_recursive']=0;
                                $input['itemtype']="Ticket";
                                $input['items_id']=$itemId;
                                $input['users_id'] = (isset($users_id) ? $users_id : Session::getLoginUserID( true )); // $this->taskWriter;
                                $date_mod = new DateTime( $_SESSION["glpi_currenttime"] );
                                $input['date_mod'] =$date_mod->format("Y-m-d H:i:s");

                                $reldoc->add ($input); //añado relación entre el documento y el ticket

                                //Añado la relación entre el documento y el usuario
                                //I add the relationship between document and user
                                $input['itemtype']="User";
                                $input['items_id']=(isset($users_id) ? $users_id : Session::getLoginUserID( true )); // Meto el usuario que relaciono
                                $reldoc->add ($input); //añado la relación entre documento y usuario.I add the relationship between document and user

                 } //fin foreach
              }  //fin es arrau

           } //Fin no es nulo

} //fin funcion

And function CargarDocumentoEnGLPI

public function cargarDocumentoEnGLPI($input) { global $DB;

 $doc = new document();//new document();
 $reldoc = new document_item(); // new relation document

  // Miro si puedo cargar el documento mediante: file_get_contents
  // I see if I can load the document using: file_get_contents

  $data = file_get_contents($input['link']);

  //check  it return data or not
  if ( $data === false )
     {

           // Error
            echo "Fallo en la lectura del fichero para cargar en GLPI";
     }
     else
     {
        // Descargo el fichero y lo grabo de forma temporal
        // I download the file and save it temporarily

        $fp = fopen(GLPI_TMP_DIR.'/'.$input['name'], 'w');

         fwrite($fp, $data);
         fclose($fp);

         // Tengo en fichero descargado
         // I have a downloaded file

        $input['filepath']=GLPI_TMP_DIR.'/'. $input['name'];

        $ext = pathinfo($input['filepath'], PATHINFO_EXTENSION);
        $input['name']= $input['name'];
        $input['filename']= $input['name'];
        $input['link']="";

        // Subo el fichero a GLPI
        // I upload the file to GLPI

        $FILEDESC = array (
              "name" => $input['name'],
              "tmp_name" => $input['filepath'],
              "type" => $ext,
        );

        $doc->uploadDocument($input,$FILEDESC);

        // Lo añado a la base de datos de GLPI
        // I add it to GLPI database
        $doc->add( $input);

        // borro el fichero descargado desde processmaker y metido antes en GLPI
        // I delete the temporary file from processmaker and inserted before in GLPI
        unlink(GLPI_TMP_DIR.'/'. $input['name']);

        return $doc->getId();
     }

} //fin funcion

tomolimo commented 2 years ago

Hello @jorge-madrigal, Thank you for your proposal Regards, Tomolimo

tomolimo commented 9 months ago

Fixed by https://github.com/tomolimo/processmaker/releases/tag/5.0.0