Skip to content
Code-Schnipsel Gruppen Projekte
sendMail.php 3,27 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
     * Returns e-mail data for an output document
     *
     * @param string|int $entityId
     * @param string $entityType
     * @param string $template
     * @param string $templateProvider
    
     * @param string $mailRecipient
    
     * @param string $mailSubject (optional)
     * @param string $mailContent (optional)
    
     * @param array $mailAttachmentMediaFileIds (optional)
    
     *
     * @return void
     */
    
    
    use QUI\ERP\Output\Output as ERPOutput;
    use QUI\Permissions\Permission;
    use QUI\Utils\Security\Orthos;
    
    
    QUI::$Ajax->registerFunction(
        'package_quiqqer_erp_ajax_output_sendMail',
    
        function (
            $entityId,
            $entityType,
            $template,
            $templateProvider,
            $mailRecipient,
            $mailSubject,
            $mailContent,
            $mailAttachmentMediaFileIds
        ) {
    
            try {
                $entityType = Orthos::clear($entityType);
    
    
                $outputProvider = ERPOutput::getOutputProviderByEntityType($entityType);
                $OutputProvider = new $outputProvider();
    
                $TemplateProvider = ERPOutput::getOutputTemplateProviderByPackage(Orthos::clear($templateProvider));
    
                if (empty($TemplateProvider)) {
                    $TemplateProvider = ERPOutput::getDefaultOutputTemplateProviderForEntityType($entityType);
                }
    
    
                if (
                    !empty($mailAttachmentMediaFileIds) &&
                    Permission::hasPermission(ERPOutput::PERMISSION_ATTACH_EMAIL_FILES)
                ) {
                    $Media = QUI::getRewrite()->getProject()->getMedia();
    
    Henning Leutz's avatar
    Henning Leutz committed
                    $mailAttachmentMediaFileIds = json_decode($mailAttachmentMediaFileIds, true);
    
    
                    foreach ($mailAttachmentMediaFileIds as $fileId) {
                        if (empty($fileId)) {
                            continue;
                        }
    
                        try {
                            $attachedMediaFiles[] = $Media->get((int)$fileId);
    
    Henning Leutz's avatar
    Henning Leutz committed
                        } catch (Exception $Exception) {
    
                            QUI\System\Log::writeException($Exception);
                        }
                    }
                }
    
    
                ERPOutput::sendPdfViaMail(
                    $entityId,
                    $entityType,
                    $OutputProvider,
                    $TemplateProvider,
                    Orthos::clear($template),
    
                    Orthos::clear($mailRecipient),
    
                    $mailSubject,
    
                    $mailContent,
                    $attachedMediaFiles
    
            } catch (QUI\ERP\Exception $Exception) {
                QUI\System\Log::writeDebugException($Exception);
                throw $Exception;
    
    Henning Leutz's avatar
    Henning Leutz committed
            } catch (Exception $Exception) {
    
                if ($Exception->getCode() === 403) {
                    throw $Exception;
                } else {
                    QUI\System\Log::writeException($Exception);
    
                    throw new \QUI\Exception([
                        'quiqqer/erp',
                        'exception.ajax.output.sendMail.error'
                    ]);
                }
    
            }
    
            QUI::getMessagesHandler()->addSuccess(
                QUI::getLocale()->get('quiqqer/erp', 'Output.send.success')
            );
        },
    
        [
            'entityId',
            'entityType',
            'template',
            'templateProvider',
            'mailRecipient',
            'mailSubject',
            'mailContent',
            'mailAttachmentMediaFileIds'
        ],
    
        'Permission::checkAdminUser'
    );