0
0
Fork 0
mirror of https://github.com/salesagility/SuiteCRM.git synced 2025-02-11 08:48:55 +00:00
salesagility_SuiteCRM/modules/AOP_Case_Updates/AOP_Case_Updates.php

326 lines
10 KiB
PHP
Raw Permalink Normal View History

2016-09-19 19:44:18 +00:00
<?php
2013-10-20 16:24:09 +00:00
/**
*
2016-09-19 19:44:18 +00:00
* SugarCRM Community Edition is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
2013-10-20 16:24:09 +00:00
*
2016-09-19 19:44:18 +00:00
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
2018-01-19 13:22:29 +00:00
* Copyright (C) 2011 - 2018 SalesAgility Ltd.
2013-10-20 16:24:09 +00:00
*
2016-09-19 19:44:18 +00:00
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
2013-10-20 16:24:09 +00:00
*
2016-09-19 19:44:18 +00:00
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
2016-09-19 19:44:18 +00:00
* details.
2013-10-20 16:24:09 +00:00
*
2016-09-19 19:44:18 +00:00
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
2013-10-20 16:24:09 +00:00
*
2016-09-19 19:44:18 +00:00
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
2013-10-20 16:24:09 +00:00
*/
require_once 'util.php';
require_once 'include/clean.php';
2013-10-20 16:24:09 +00:00
2016-09-19 19:44:18 +00:00
/**
* Class AOP_Case_Updates.
*/
2023-06-01 13:10:03 +00:00
#[\AllowDynamicProperties]
2016-09-19 19:44:18 +00:00
class AOP_Case_Updates extends Basic
{
public $new_schema = true;
public $module_dir = 'AOP_Case_Updates';
public $object_name = 'AOP_Case_Updates';
public $table_name = 'aop_case_updates';
public $tracker_visibility = false;
public $importable = false;
public $disable_row_level_security = true;
public $id;
public $name;
public $date_entered;
public $date_modified;
public $modified_user_id;
public $modified_by_name;
public $created_by;
public $created_by_name;
public $description;
public $deleted;
public $created_by_link;
public $modified_user_link;
public $assigned_user_id;
public $assigned_user_name;
public $assigned_user_link;
public $case;
public $case_name;
public $case_id;
public $contact;
public $contact_name;
public $contact_id;
public $internal;
public $notes;
public function __construct()
{
2016-03-24 09:22:57 +00:00
parent::__construct();
2013-10-20 16:24:09 +00:00
}
2016-09-19 19:44:18 +00:00
/**
* @param $interface
*
* @return bool
*/
public function bean_implements($interface)
{
switch ($interface) {
case 'ACL':
return true;
default:
return false;
}
}
2016-09-19 19:44:18 +00:00
/**
* @param bool $check_notify
* @return string
*/
public function save($check_notify = false)
{
$this->name = SugarCleaner::cleanHtml($this->name);
2018-01-05 17:35:47 +00:00
$this->parseDescription();
2013-10-20 16:24:09 +00:00
parent::save($check_notify);
2016-09-19 19:44:18 +00:00
if (file_exists('custom/modules/AOP_Case_Updates/CaseUpdatesHook.php')) {
2016-03-24 09:22:57 +00:00
require_once 'custom/modules/AOP_Case_Updates/CaseUpdatesHook.php';
2016-09-19 19:44:18 +00:00
} else {
2016-03-24 09:22:57 +00:00
require_once 'modules/AOP_Case_Updates/CaseUpdatesHook.php';
}
2016-09-19 19:44:18 +00:00
if (class_exists('CustomCaseUpdatesHook')) {
2016-03-24 09:22:57 +00:00
$hook = new CustomCaseUpdatesHook();
2016-09-19 19:44:18 +00:00
} else {
2016-03-24 09:22:57 +00:00
$hook = new CaseUpdatesHook();
2013-10-20 16:24:09 +00:00
}
2016-03-24 09:22:57 +00:00
$hook->sendCaseUpdate($this);
2013-10-20 16:24:09 +00:00
2016-09-19 19:44:18 +00:00
return $this->id;
2013-10-20 16:24:09 +00:00
}
2018-01-05 17:35:47 +00:00
/**
* Fixes unclosed HTML tags
*/
private function parseDescription()
{
$description = SugarCleaner::cleanHtml($this->description);
2023-06-01 13:10:03 +00:00
if (preg_match('/<[^<]+>/', (string) $description, $matches) !== 0) {
2018-01-12 14:44:13 +00:00
// remove external warning, if HTML is not valid
libxml_use_internal_errors(true);
2018-01-12 14:24:23 +00:00
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($description, 'HTML-ENTITIES', 'UTF-8'));
2018-01-12 14:24:23 +00:00
foreach ($dom->getElementsByTagName('head') as $headElement) {
$headElement->parentNode->removeChild($headElement);
}
$dom->removeChild($dom->doctype);
$dom->appendChild($dom->firstChild);
2018-01-12 14:24:23 +00:00
$description = $dom->saveHTML();
2018-01-12 15:20:38 +00:00
foreach (libxml_get_errors() as $xmlError) {
$GLOBALS['log']->warn(sprintf('%s in %s', trim($xmlError->message), get_class($this)));
}
libxml_clear_errors();
}
2018-01-05 17:35:47 +00:00
2023-06-01 13:10:03 +00:00
$this->description = trim(preg_replace('/\s\s+/', ' ', (string) $description));
2018-01-05 17:35:47 +00:00
}
2013-10-20 16:24:09 +00:00
/**
* @return aCase
*/
2016-09-19 19:44:18 +00:00
public function getCase()
{
return BeanFactory::getBean('Cases', $this->case_id);
2013-10-20 16:24:09 +00:00
}
2016-09-19 19:44:18 +00:00
/**
* @return null|Contact[]
*/
public function getContacts()
{
2013-10-20 16:24:09 +00:00
$case = $this->getCase();
2016-09-19 19:44:18 +00:00
if ($case) {
return $case->get_linked_beans('contacts', 'Contacts');
2013-10-20 16:24:09 +00:00
}
2016-09-19 19:44:18 +00:00
2013-10-20 16:24:09 +00:00
return null;
}
2016-09-19 19:44:18 +00:00
/**
* @return null|Contact
*/
public function getUpdateContact()
{
if ($this->contact_id) {
return BeanFactory::getBean('Contacts', $this->contact_id);
2013-11-01 10:05:00 +00:00
}
2016-09-19 19:44:18 +00:00
2013-11-01 10:05:00 +00:00
return null;
}
2016-09-19 19:44:18 +00:00
/**
* @return User
*/
public function getUser()
{
return BeanFactory::getBean('Users', $this->getCase()->assigned_user_id);
2013-10-20 16:24:09 +00:00
}
2016-09-19 19:44:18 +00:00
/**
* @return User
*/
public function getUpdateUser()
{
return BeanFactory::getBean('Users', $this->assigned_user_id);
2013-10-20 16:24:09 +00:00
}
2016-09-19 19:44:18 +00:00
/**
* @return array
*/
public function getEmailForUser()
{
2013-10-20 16:24:09 +00:00
$user = $this->getUser();
2016-09-19 19:44:18 +00:00
if ($user) {
2013-10-20 16:24:09 +00:00
return array($user->emailAddress->getPrimaryAddress($user));
}
2016-09-19 19:44:18 +00:00
2013-10-20 16:24:09 +00:00
return array();
}
2016-09-19 19:44:18 +00:00
/**
* @param EmailTemplate $template
* @param bool $addDelimiter
* @param null $contactId
*
* @return array
*/
private function populateTemplate(EmailTemplate $template, $addDelimiter = true, $contactId = null)
{
2013-10-20 16:24:09 +00:00
global $app_strings, $sugar_config;
2016-09-19 19:44:18 +00:00
2013-10-20 16:24:09 +00:00
$user = $this->getUpdateUser();
2016-09-19 19:44:18 +00:00
if (!$user) {
2013-10-20 16:24:09 +00:00
$this->getUser();
}
2016-09-19 19:44:18 +00:00
$beans = array('Contacts' => $contactId, 'Cases' => $this->getCase()->id, 'Users' => $user->id, 'AOP_Case_Updates' => $this->id);
2013-10-20 16:24:09 +00:00
$ret = array();
2016-09-19 19:44:18 +00:00
$ret['subject'] = from_html(aop_parse_template($template->subject, $beans));
2023-06-01 13:10:03 +00:00
$body = aop_parse_template(str_replace('$sugarurl', $sugar_config['site_url'], (string) $template->body_html), $beans);
$bodyAlt = aop_parse_template(str_replace('$sugarurl', $sugar_config['site_url'], (string) $template->body), $beans);
2016-09-19 19:44:18 +00:00
if ($addDelimiter) {
$body = $app_strings['LBL_AOP_EMAIL_REPLY_DELIMITER'].$body;
$bodyAlt = $app_strings['LBL_AOP_EMAIL_REPLY_DELIMITER'].$bodyAlt;
2013-10-20 16:24:09 +00:00
}
$ret['body'] = from_html($body);
$ret['body_alt'] = strip_tags(from_html($bodyAlt));
2016-09-19 19:44:18 +00:00
2013-10-20 16:24:09 +00:00
return $ret;
}
2016-09-19 19:44:18 +00:00
/**
2016-12-28 21:01:43 +00:00
* @param array $emails
* @param EmailTemplate $template
2016-09-19 19:44:18 +00:00
* @param array $signature
2016-12-28 21:01:43 +00:00
* @param null $caseId
* @param bool $addDelimiter
* @param null $contactId
2016-09-19 19:44:18 +00:00
*
* @return bool
*/
2016-12-28 21:01:43 +00:00
public function sendEmail(
$emails,
$template,
$signature = array(),
$caseId = null,
$addDelimiter = true,
$contactId = null
) {
2016-09-19 19:44:18 +00:00
$GLOBALS['log']->info('AOPCaseUpdates: sendEmail called');
require_once 'include/SugarPHPMailer.php';
$mailer = new SugarPHPMailer();
$admin = BeanFactory::newBean('Administration');
2013-10-20 16:24:09 +00:00
$admin->retrieveSettings();
$mailer->prepForOutbound();
$mailer->setMailerForSystem();
2016-09-19 19:44:18 +00:00
$signatureHTML = '';
if ($signature && array_key_exists('signature_html', $signature)) {
2013-10-20 16:24:09 +00:00
$signatureHTML = from_html($signature['signature_html']);
}
2016-09-19 19:44:18 +00:00
$signaturePlain = '';
if ($signature && array_key_exists('signature', $signature)) {
2013-10-20 16:24:09 +00:00
$signaturePlain = $signature['signature'];
}
$emailSettings = getPortalEmailSettings();
$text = $this->populateTemplate($template, $addDelimiter, $contactId);
2013-10-20 16:24:09 +00:00
$mailer->Subject = $text['subject'];
2016-12-28 21:01:43 +00:00
$mailer->Body = $text['body'] . $signatureHTML;
2016-09-19 19:44:18 +00:00
$mailer->isHTML(true);
2016-12-28 21:01:43 +00:00
$mailer->AltBody = $text['body_alt'] . $signaturePlain;
2016-09-19 19:44:18 +00:00
$mailer->From = $emailSettings['from_address'];
isValidEmailAddress($mailer->From);
2013-10-20 16:24:09 +00:00
$mailer->FromName = $emailSettings['from_name'];
2016-09-19 19:44:18 +00:00
foreach ($emails as $email) {
$mailer->addAddress($email);
2013-10-20 16:24:09 +00:00
}
2016-12-28 21:01:43 +00:00
try {
if ($mailer->send()) {
require_once 'modules/Emails/Email.php';
$emailObj = BeanFactory::newBean('Emails');
2016-12-28 21:01:43 +00:00
$emailObj->to_addrs_names = implode(',', $emails);
$emailObj->type = 'out';
$emailObj->deleted = '0';
$emailObj->name = $mailer->Subject;
$emailObj->description = $mailer->AltBody;
$emailObj->description_html = $mailer->Body;
$emailObj->from_addr_name = $mailer->From;
if ($caseId) {
$emailObj->parent_type = 'Cases';
$emailObj->parent_id = $caseId;
}
2019-01-11 13:05:46 +00:00
$emailObj->date_sent_received = TimeDate::getInstance()->nowDb();
2016-12-28 21:01:43 +00:00
$emailObj->modified_user_id = '1';
$emailObj->created_by = '1';
$emailObj->status = 'sent';
$emailObj->save();
2016-09-19 19:44:18 +00:00
2016-12-28 21:01:43 +00:00
return true;
}
} catch (phpmailerException $exception) {
$GLOBALS['log']->fatal('AOPCaseUpdates: sending email Failed: ' . $exception->getMessage());
2013-10-20 16:24:09 +00:00
}
2016-12-28 21:01:43 +00:00
$GLOBALS['log']->info('AOPCaseUpdates: Could not send email: ' . $mailer->ErrorInfo);
2016-09-19 19:44:18 +00:00
2016-12-28 21:01:43 +00:00
return false;
2013-10-20 16:24:09 +00:00
}
}