Files
luos/workflow/engine/classes/model/AppNotes.php

346 lines
12 KiB
PHP
Raw Normal View History

2012-10-22 05:57:53 -04:00
<?php
2017-08-14 16:13:46 -04:00
use ProcessMaker\Core\System;
use ProcessMaker\Model\Documents;
2019-03-11 12:32:10 -04:00
use ProcessMaker\Util\DateTime;
2012-10-22 05:57:53 -04:00
/**
* Skeleton subclass for representing a row from the 'APP_NOTES' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
* @package classes.model
*/
2019-03-11 12:32:10 -04:00
2012-10-22 05:57:53 -04:00
class AppNotes extends BaseAppNotes
{
2019-03-11 12:32:10 -04:00
/**
* Get the existing case notes information from a case
*
* @param string $appUid
* @param string $usrUid
* @param string $start
* @param int $limit
* @param string $sort
* @param string $dir
* @param string $dateFrom
* @param string $dateTo
* @param string $search
*
* @return array
*
* @see \Cases->getCaseNotes()
* @see \AppProxy->getNotesList()
* @see \Home->getAppsData()
* @see workflow/engine/methods/cases/caseNotesAjax.php->getNotesList()
* @see \ProcessMaker\BusinessModel\Cases->getCaseNotes()
* @see \ProcessMaker\Services\Api\Light->doGetCaseNotes()
*
* @link https://wiki.processmaker.com/3.2/Case_Notes#Viewing_Existing_Case_Notes
*/
public function getNotesList(
2014-03-28 11:40:59 -04:00
$appUid,
$usrUid = '',
$start = '',
$limit = 25,
$sort = 'APP_NOTES.NOTE_DATE',
$dir = 'DESC',
$dateFrom = '',
$dateTo = '',
2019-03-11 12:32:10 -04:00
$search = ''
) {
$criteria = new Criteria('workflow');
$criteria->clearSelectColumns();
2012-10-22 05:57:53 -04:00
2019-03-11 12:32:10 -04:00
$criteria->addSelectColumn(AppNotesPeer::APP_UID);
$criteria->addSelectColumn(AppNotesPeer::USR_UID);
$criteria->addSelectColumn(AppNotesPeer::NOTE_DATE);
$criteria->addSelectColumn(AppNotesPeer::NOTE_CONTENT);
$criteria->addSelectColumn(AppNotesPeer::NOTE_TYPE);
$criteria->addSelectColumn(AppNotesPeer::NOTE_AVAILABILITY);
$criteria->addSelectColumn(AppNotesPeer::NOTE_ORIGIN_OBJ);
$criteria->addSelectColumn(AppNotesPeer::NOTE_AFFECTED_OBJ1);
$criteria->addSelectColumn(AppNotesPeer::NOTE_AFFECTED_OBJ2);
$criteria->addSelectColumn(AppNotesPeer::NOTE_RECIPIENTS);
$criteria->addSelectColumn(UsersPeer::USR_USERNAME);
$criteria->addSelectColumn(UsersPeer::USR_FIRSTNAME);
$criteria->addSelectColumn(UsersPeer::USR_LASTNAME);
$criteria->addSelectColumn(UsersPeer::USR_EMAIL);
2012-10-22 05:57:53 -04:00
2019-03-11 12:32:10 -04:00
$criteria->addJoin(AppNotesPeer::USR_UID, UsersPeer::USR_UID, Criteria::LEFT_JOIN);
2012-10-22 05:57:53 -04:00
2019-03-11 12:32:10 -04:00
$criteria->add(AppNotesPeer::APP_UID, $appUid, Criteria::EQUAL);
2012-10-22 05:57:53 -04:00
if ($usrUid != '') {
2019-03-11 12:32:10 -04:00
$criteria->add(AppNotesPeer::USR_UID, $usrUid, Criteria::EQUAL);
2014-03-28 11:40:59 -04:00
}
if ($dateFrom != '') {
2019-03-11 12:32:10 -04:00
$criteria->add(AppNotesPeer::NOTE_DATE, $dateFrom, Criteria::GREATER_EQUAL);
2014-03-28 11:40:59 -04:00
}
if ($dateTo != '') {
2019-03-11 12:32:10 -04:00
$criteria->add(AppNotesPeer::NOTE_DATE, $dateTo, Criteria::LESS_EQUAL);
2014-03-28 11:40:59 -04:00
}
if ($search != '') {
2019-03-11 12:32:10 -04:00
$criteria->add(AppNotesPeer::NOTE_CONTENT, '%' . $search . '%', Criteria::LIKE);
2012-10-22 05:57:53 -04:00
}
2014-03-28 11:40:59 -04:00
if ($dir == 'DESC') {
2019-03-11 12:32:10 -04:00
$criteria->addDescendingOrderByColumn($sort);
2014-03-28 11:40:59 -04:00
} else {
2019-03-11 12:32:10 -04:00
$criteria->addAscendingOrderByColumn($sort);
2014-03-28 11:40:59 -04:00
}
2012-10-22 05:57:53 -04:00
2019-03-11 12:32:10 -04:00
$response = [];
$totalCount = AppNotesPeer::doCount($criteria);
2012-10-22 05:57:53 -04:00
$response['totalCount'] = $totalCount;
2019-03-11 12:32:10 -04:00
$response['notes'] = [];
2012-10-22 05:57:53 -04:00
if ($start != '') {
2019-03-11 12:32:10 -04:00
$criteria->setLimit($limit);
$criteria->setOffset($start);
2012-10-22 05:57:53 -04:00
}
2019-03-11 12:32:10 -04:00
$dataset = AppNotesPeer::doSelectRS($criteria);
$dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$dataset->next();
2012-10-22 05:57:53 -04:00
2019-03-11 12:32:10 -04:00
while ($row = $dataset->getRow()) {
$row['NOTE_CONTENT'] = stripslashes($row['NOTE_CONTENT']);
$response['notes'][] = $row;
$dataset->next();
2012-10-22 05:57:53 -04:00
}
2019-03-11 12:32:10 -04:00
$result = [];
$result['criteria'] = $criteria;
2012-10-22 05:57:53 -04:00
$result['array'] = $response;
return $result;
}
public function postNewNote ($appUid, $usrUid, $noteContent, $notify = true, $noteAvalibility = "PUBLIC", $noteRecipients = "", $noteType = "USER", $noteDate = "now")
{
$this->setAppUid( $appUid );
$this->setUsrUid( $usrUid );
$this->setNoteDate( $noteDate );
$this->setNoteContent( $noteContent );
$this->setNoteType( $noteType );
$this->setNoteAvailability( $noteAvalibility );
$this->setNoteOriginObj( '' );
$this->setNoteAffectedObj1( '' );
$this->setNoteAffectedObj2( '' );
$this->setNoteRecipients( $noteRecipients );
if ($this->validate()) {
2012-10-19 21:30:26 +00:00
// we save it, since we get no validation errors, or do whatever else you like.
2012-10-22 05:57:53 -04:00
$res = $this->save();
$msg = '';
} else {
2012-10-19 21:30:26 +00:00
// Something went wrong. We can now get the validationFailures and handle them.
2012-10-22 05:57:53 -04:00
$msg = '';
$validationFailuresArray = $this->getValidationFailures();
foreach ($validationFailuresArray as $objValidationFailure) {
$msg .= $objValidationFailure->getMessage() . "<br/>";
}
2012-10-19 21:30:26 +00:00
//return array ( 'codError' => -100, 'rowsAffected' => 0, 'message' => $msg );
2012-10-22 05:57:53 -04:00
}
if ($msg != "") {
$response['success'] = G::LoadTranslation("ID_FAILURE");
2012-10-22 05:57:53 -04:00
$response['message'] = $msg;
} else {
$response['success'] = 'success';
$response['message'] = '';
2012-10-22 05:57:53 -04:00
}
if ($notify) {
if ($noteRecipients == "") {
$noteRecipientsA = array ();
$oCase = new Cases();
$p = $oCase->getUsersParticipatedInCase( $appUid );
foreach ($p['array'] as $key => $userParticipated) {
$noteRecipientsA[] = $key;
}
$noteRecipients = implode( ",", $noteRecipientsA );
}
$this->sendNoteNotification( $appUid, $usrUid, $noteContent, $noteRecipients );
}
return $response;
}
2018-04-04 09:21:59 -04:00
/**
* Case note notification
*
* @param string $appUid
* @param string $usrUid
* @param string $noteContent
* @param string $noteRecipients
* @param string $from
* @param integer $delIndex
2019-03-29 10:00:39 -04:00
* @return void
2018-04-04 09:21:59 -04:00
* @throws Exception
2019-03-29 10:00:39 -04:00
*
* @see AppNotes->addCaseNote()
* @see AppNotes->postNewNote()
* @see workflow/engine/src/ProcessMaker/Util/helpers.php::postNote()
*/
public function sendNoteNotification($appUid, $usrUid, $noteContent, $noteRecipients, $from = '', $delIndex = 0)
2012-10-22 05:57:53 -04:00
{
try {
2018-04-04 09:21:59 -04:00
$configuration = System::getEmailConfiguration();
2012-10-22 05:57:53 -04:00
$msgError = "";
if (!isset($configuration['MESS_ENABLED']) || $configuration['MESS_ENABLED'] != '1') {
$msgError = "The default configuration wasn't defined";
2018-04-04 09:21:59 -04:00
$configuration['MESS_ENGINE'] = '';
2012-10-22 05:57:53 -04:00
}
2019-02-13 15:53:21 -04:00
//This value can be empty when the previous task is: 'Script Task', 'Timer Event' or other without user.
if (!empty($usrUid)) {
$users = new Users();
$userInfo = $users->load($usrUid);
$authorName = ((($userInfo['USR_FIRSTNAME'] != '') || ($userInfo['USR_LASTNAME'] != '')) ? $userInfo['USR_FIRSTNAME'] . ' ' . $userInfo['USR_LASTNAME'] . ' ' : '') . '<' . $userInfo['USR_EMAIL'] . '>';
} else {
$authorName = G::LoadTranslation('UID_UNDEFINED_USER');
}
2012-10-22 05:57:53 -04:00
2018-04-04 09:21:59 -04:00
$cases = new Cases();
$fieldCase = $cases->loadCase($appUid, $delIndex);
$configNoteNotification['subject'] = G::LoadTranslation('ID_MESSAGE_SUBJECT_NOTE_NOTIFICATION') . " @#APP_TITLE ";
2017-06-13 15:59:09 -04:00
//Define the body for the notification
2018-04-04 09:21:59 -04:00
$configNoteNotification['body'] = $this->getBodyCaseNote($authorName, $noteContent);
2019-07-01 11:49:26 -04:00
$body = nl2br(G::replaceDataField($configNoteNotification['body'], $fieldCase, 'mysql', false));
2012-10-22 05:57:53 -04:00
$attachFileLinks = $this->getAttachedFilesFromTheCaseNote($appUid);
if (!empty($attachFileLinks)) {
$body = $body . "<br>" . G::LoadTranslation('ID_ATTACHED_FILES') . ":&nbsp;" . implode("<br>", $attachFileLinks) . ".";
}
2018-04-04 09:21:59 -04:00
$users = new Users();
$recipientsArray = explode(",", $noteRecipients);
2012-10-22 05:57:53 -04:00
foreach ($recipientsArray as $recipientUid) {
2018-04-04 09:21:59 -04:00
$userInfo = $users->load($recipientUid);
$ifUserNameDefined = $userInfo['USR_FIRSTNAME'] != '' || $userInfo['USR_LASTNAME'] != '';
$to = ($ifUserNameDefined ? $userInfo['USR_FIRSTNAME'] . ' ' . $userInfo['USR_LASTNAME'] . ' ' : '') . '<' . $userInfo['USR_EMAIL'] . '>';
2018-04-04 09:21:59 -04:00
$spool = new SpoolRun();
$spool->setConfig($configuration);
$parameters = [
2018-04-04 09:21:59 -04:00
'',
$appUid,
$delIndex,
2019-03-29 10:00:39 -04:00
WsBase::MESSAGE_TYPE_CASE_NOTE,
2019-07-01 11:49:26 -04:00
G::replaceDataField($configNoteNotification['subject'], $fieldCase, 'mysql', false),
2018-04-04 09:21:59 -04:00
G::buildFrom($configuration, $from),
$to,
$body,
'',
'',
'',
'',
'pending',
2019-04-17 15:53:12 -04:00
1,
2018-04-04 09:21:59 -04:00
$msgError,
true,
(isset($fieldCase['APP_NUMBER'])) ? $fieldCase['APP_NUMBER'] : 0,
(isset($fieldCase['PRO_ID'])) ? $fieldCase['PRO_ID'] : 0,
(isset($fieldCase['TAS_ID'])) ? $fieldCase['TAS_ID'] : 0
];
$messageArray = AppMessage::buildMessageRow(...$parameters);
2018-04-04 09:21:59 -04:00
$spool->create($messageArray);
2012-10-22 05:57:53 -04:00
if ($msgError == '') {
2018-04-04 09:21:59 -04:00
if (($configuration['MESS_BACKGROUND'] == '') || ($configuration['MESS_TRY_SEND_INMEDIATLY'] == '1')) {
$spool->sendMail();
}
2012-10-22 05:57:53 -04:00
}
}
2018-04-04 09:21:59 -04:00
} catch (Exception $exception) {
throw $exception;
2012-10-22 05:57:53 -04:00
}
}
/**
* Get attached files from the case note, this require appUid.
* @param string $appUid
* @return array
*/
public function getAttachedFilesFromTheCaseNote(string $appUid): array
{
$attachFileLinks = [];
$url = System::getServerMainPath();
$result = Documents::getAttachedFilesFromTheCaseNote($appUid);
$result->each(function($item) use($url, &$attachFileLinks) {
$href = $url . "/cases/casesShowCaseNotes?a={$item->APP_DOC_UID}=&v={$item->DOC_VERSION}";
$attachFileLinks[] = "<a href='{$href}'>{$item->APP_DOC_FILENAME}</a>";
});
return $attachFileLinks;
}
public function addCaseNote($applicationUid, $userUid, $note, $sendMail)
{
$response = $this->postNewNote($applicationUid, $userUid, $note, false);
if ($sendMail == 1) {
$case = new Cases();
$p = $case->getUsersParticipatedInCase($applicationUid, 'ACTIVE');
$noteRecipientsList = array();
foreach ($p["array"] as $key => $userParticipated) {
if ($key != '') {
$noteRecipientsList[] = $key;
}
}
$noteRecipients = implode(",", $noteRecipientsList);
$note = stripslashes($note);
$this->sendNoteNotification($applicationUid, $userUid, $note, $noteRecipients);
}
return $response;
}
/**
* Add htmlEntities to notes in node_content
* @param $notes
* @return array
*/
public static function applyHtmlentitiesInNotes($notes)
{
if (isset($notes) && isset($notes["array"])) {
foreach ($notes["array"]["notes"] as &$note) {
$note["NOTE_CONTENT"] = htmlentities($note["NOTE_CONTENT"], ENT_QUOTES, 'UTF-8');
}
}
return $notes;
}
2018-04-04 09:21:59 -04:00
/**
* Define the body for the case note notification
*
* @param string $authorName
* @param string $noteContent
*
* @return string
*/
private function getBodyCaseNote($authorName = '', $noteContent = '')
{
$body = G::LoadTranslation('ID_CASE_TITLE') . ': @#APP_TITLE<br />';
$body .= G::LoadTranslation('ID_CASE_NUMBER') . ': @#APP_NUMBER<br />';
$body .= G::LoadTranslation('ID_AUTHOR') . ': ' . $authorName . '<br /><br />' . $noteContent;
return $body;
}
2012-10-22 05:57:53 -04:00
}