function HelpDesk_notifyOwnerOnTicketChange($entityData)
{
    global $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID;
    $moduleName = $entityData->getModuleName();
    $wsId = $entityData->getId();
    $parts = explode('x', $wsId);
    $entityId = $parts[1];
    $isNew = $entityData->isNew();
    if (!$isNew) {
        $reply = 'Re : ';
    } else {
        $reply = '';
    }
    // SalesPlatform.ru begin
    $subject = ' [ ' . getTranslatedString('Ticket No', $moduleName) . ' ' . $entityData->get('ticket_no') . ' ] ' . $reply . $entityData->get('ticket_title');
    //$subject = $entityData->get('ticket_no') . ' [ '.getTranslatedString('LBL_TICKET_ID', $moduleName)
    //					.' : '.$entityId.' ] '.$reply.$entityData->get('ticket_title');
    // SalesPlatform.ru end
    $email_body = HelpDesk::getTicketEmailContents($entityData, true);
    if (PerformancePrefs::getBoolean('NOTIFY_OWNER_EMAILS', true) === true) {
        //send mail to the assigned to user and the parent to whom this ticket is assigned
        require_once 'modules/Emails/mail.php';
        $wsAssignedUserId = $entityData->get('assigned_user_id');
        $userIdParts = explode('x', $wsAssignedUserId);
        $ownerId = $userIdParts[1];
        $ownerType = vtws_getOwnerType($ownerId);
        if ($ownerType == 'Users') {
            $to_email = getUserEmailId('id', $ownerId);
        }
        if ($ownerType == 'Groups') {
            $to_email = implode(',', getDefaultAssigneeEmailIds($ownerId));
        }
        if ($to_email != '') {
            if ($isNew) {
                $mail_status = send_mail('HelpDesk', $to_email, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $subject, $email_body);
            } else {
                $entityDelta = new VTEntityDelta();
                $statusHasChanged = $entityDelta->hasChanged($entityData->getModuleName(), $entityId, 'ticketstatus');
                $solutionHasChanged = $entityDelta->hasChanged($entityData->getModuleName(), $entityId, 'solution');
                $ownerHasChanged = $entityDelta->hasChanged($entityData->getModuleName(), $entityId, 'assigned_user_id');
                $descriptionHasChanged = $entityDelta->hasChanged($entityData->getModuleName(), $entityId, 'description');
                if ($statusHasChanged && $entityData->get('ticketstatus') == "Closed" || $solutionHasChanged || $ownerHasChanged || $descriptionHasChanged) {
                    $mail_status = send_mail('HelpDesk', $to_email, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $subject, $email_body);
                }
            }
            $mail_status_str = $to_email . "=" . $mail_status . "&&&";
        } else {
            $mail_status_str = "'" . $to_email . "'=0&&&";
        }
        if ($mail_status != '') {
            $mail_error_status = getMailErrorString($mail_status_str);
        }
    }
}
function TicketOwnerComments($entityData)
{
    global $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID;
    $adb = PearDatabase::getInstance();
    //if commented from portal by the customer, then ignore this
    $customer = $entityData->get('customer');
    if (!empty($customer)) {
        return;
    }
    $wsParentId = $entityData->get('related_to');
    $parentIdParts = explode('x', $wsParentId);
    $parentId = $parentIdParts[1];
    $moduleName = getSalesEntityType($parentId);
    $isNew = $entityData->isNew();
    if ($moduleName == 'HelpDesk') {
        $ticketFocus = CRMEntity::getInstance($moduleName);
        $ticketFocus->retrieve_entity_info($parentId, $moduleName);
        $ticketFocus->id = $parentId;
        if (!$isNew) {
            $reply = 'Re : ';
        } else {
            $reply = '';
        }
        $subject = $ticketFocus->column_fields['ticket_no'] . ' [ ' . getTranslatedString('LBL_TICKET_ID', $moduleName) . ' : ' . $parentId . ' ] ' . $reply . $ticketFocus->column_fields['ticket_title'];
        $emailOptOut = 0;
        $contactId = $ticketFocus->column_fields['contact_id'];
        $accountId = $ticketFocus->column_fields['parent_id'];
        //To get the emailoptout vtiger_field value and then decide whether send mail about the tickets or not
        if (!empty($contactId)) {
            $result = $adb->pquery('SELECT email, emailoptout FROM vtiger_contactdetails WHERE contactid=?', array($contactId));
            $emailOptOut = $adb->query_result($result, 0, 'emailoptout');
            $parentEmail = $contactMailId = $adb->query_result($result, 0, 'email');
            $displayValueArray = getEntityName('Contacts', $contactId);
            if (!empty($displayValueArray)) {
                foreach ($displayValueArray as $key => $value) {
                    $contactName = $value;
                }
            }
            $parentName = $contactName;
            //Get the status of the vtiger_portal user. if the customer is active then send the vtiger_portal link in the mail
            if ($parentEmail != '') {
                $sql = "SELECT * FROM vtiger_portalinfo WHERE user_name=?";
                $isPortalUser = $adb->query_result($adb->pquery($sql, array($parentEmail)), 0, 'isactive');
            }
        } else {
            if (!empty($accountId)) {
                $result = $adb->pquery("SELECT accountname, emailoptout, email1 FROM vtiger_account WHERE accountid=?", array($accountId));
                $emailOptOut = $adb->query_result($result, 0, 'emailoptout');
                $parentEmail = $adb->query_result($result, 0, 'email1');
                $parentName = $adb->query_result($result, 0, 'accountname');
            }
        }
        //added condition to check the emailoptout
        if ($emailOptOut == 0) {
            $entityData = VTEntityData::fromCRMEntity($ticketFocus);
            if ($isPortalUser == 1) {
                $bodysubject = getTranslatedString('Ticket No', $moduleName) . ": " . $ticketFocus->column_fields['ticket_no'] . "<br>" . getTranslatedString('LBL_TICKET_ID', $moduleName) . ' : ' . $parentId . '<br> ' . getTranslatedString('LBL_SUBJECT', $moduleName) . $ticketFocus->column_fields['ticket_title'];
                $emailBody = $bodysubject . '<br><br>' . HelpDesk::getPortalTicketEmailContents($entityData);
            } else {
                $emailBody = HelpDesk::getTicketEmailContents($entityData);
            }
            send_mail('HelpDesk', $parentEmail, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_ID, $subject, $emailBody);
        }
    }
}