<commit_before><?php

namespace Remp\MailerModule\Presenters;

use Nette\Application\BadRequestException;
use Nette\Database\Table\ActiveRow;
use Nette\Utils\Json;
use Remp\MailerModule\Components\IDataTableFactory;
use Remp\MailerModule\Components\ISendingStatsFactory;
use Remp\MailerModule\ContentGenerator\ContentGenerator;
use Remp\MailerModule\Forms\TemplateFormFactory;
use Remp\MailerModule\Forms\TemplateTestFormFactory;
use Remp\MailerModule\Repository\LayoutsRepository;
use Remp\MailerModule\Repository\ListsRepository;
use Remp\MailerModule\Repository\LogsRepository;
use Remp\MailerModule\Repository\TemplatesRepository;

final class TemplatePresenter extends BasePresenter
{
    private $templatesRepository;

    private $logsRepository;

    private $templateFormFactory;

    private $templateTestFormFactory;

    private $layoutsRepository;

    private $listsRepository;

    public function __construct(
        TemplatesRepository $templatesRepository,
        LogsRepository $logsRepository,
        TemplateFormFactory $templateFormFactory,
        TemplateTestFormFactory $templateTestFormFactory,
        LayoutsRepository $layoutsRepository,
        ListsRepository $listsRepository
    ) {

        parent::__construct();
        $this->templatesRepository = $templatesRepository;
        $this->logsRepository = $logsRepository;
        $this->templateFormFactory = $templateFormFactory;
        $this->templateTestFormFactory = $templateTestFormFactory;
        $this->layoutsRepository = $layoutsRepository;
        $this->listsRepository = $listsRepository;
    }

    public function createComponentDataTableDefault(IDataTableFactory $dataTableFactory)
    {
        $mailTypePairs = $this->listsRepository->all()->fetchPairs('id', 'title');

        $dataTable = $dataTableFactory->create();
        $dataTable
            ->setColSetting('created_at', [
                'header' => 'created at',
                'render' => 'date',
                'priority' => 1,
            ])
            ->setColSetting('code', [
                'priority' => 2,
            ])
            ->setColSetting('subject', [
                'priority' => 1,
            ])
            ->setColSetting('type', [
                'orderable' => false,
                'filter' => $mailTypePairs,
                'priority' => 2,
            ])
            ->setColSetting('opened', [
                'priority' => 3,
            ])
            ->setColSetting('clicked', [
                'priority' => 3,
            ])
            ->setRowAction('show', 'palette-Cyan zmdi-eye', 'Show template')
            ->setRowAction('edit', 'palette-Cyan zmdi-edit', 'Edit template')
            ->setRowAction('duplicate', 'palette-Cyan zmdi-copy', 'Duplicate template')
            ->setTableSetting('order', Json::encode([[0, 'DESC']]));

        return $dataTable;
    }

    public function renderDefaultJsonData()
    {
        $request = $this->request->getParameters();

        $listIds = null;
        foreach ($request['columns'] as $column) {
            if ($column['name'] !== 'type') {
                continue;
            }
            if (!empty($column['search']['value'])) {
                $listIds = explode(',', $column['search']['value']);
            }
            break;
        }

        $templatesCount = $this->templatesRepository
            ->tableFilter($request['search']['value'], $request['columns'][$request['order'][0]['column']]['name'], $request['order'][0]['dir'], $listIds)
            ->count('*');

        $templates = $this->templatesRepository
            ->tableFilter($request['search']['value'], $request['columns'][$request['order'][0]['column']]['name'], $request['order'][0]['dir'], $listIds, $request['length'], $request['start'])
            ->fetchAll();

        $result = [
            'recordsTotal' => $this->templatesRepository->totalCount(),
            'recordsFiltered' => $templatesCount,
            'data' => []
        ];

        /** @var ActiveRow $template */
        foreach ($templates as $template) {
            $editUrl = $this->link('Edit', $template->id);
            $result['data'][] = [
                'actions' => [
                    'show' => $this->link('Show', $template->id),
                    'edit' => $this->link('Edit', $template->id),
                    'duplicate' => $this->link('Duplicate!', $template->id),
                ],
                $template->created_at,
                $template->code,
                "<a href='{$editUrl}'>{$template->subject}</a>",
                $template->type->title,
                $template->related('mail_job_batch_template')->sum('opened'),
                $template->related('mail_job_batch_template')->sum('clicked'),
            ];
        }
        $this->presenter->sendJson($result);
    }

    public function renderShow($id)
    {
        $template = $this->templatesRepository->find($id);
        if (!$template) {
            throw new BadRequestException();
        }
<commit_msg>Adding missing sending stats for non-job (system) emails on listing<commit_after><?php

namespace Remp\MailerModule\Presenters;

use Nette\Application\BadRequestException;
use Nette\Database\Table\ActiveRow;
use Nette\Utils\Json;
use Remp\MailerModule\Components\IDataTableFactory;
use Remp\MailerModule\Components\ISendingStatsFactory;
use Remp\MailerModule\ContentGenerator\ContentGenerator;
use Remp\MailerModule\Forms\TemplateFormFactory;
use Remp\MailerModule\Forms\TemplateTestFormFactory;
use Remp\MailerModule\Repository\LayoutsRepository;
use Remp\MailerModule\Repository\ListsRepository;
use Remp\MailerModule\Repository\LogsRepository;
use Remp\MailerModule\Repository\TemplatesRepository;

final class TemplatePresenter extends BasePresenter
{
    private $templatesRepository;

    private $logsRepository;

    private $templateFormFactory;

    private $templateTestFormFactory;

    private $layoutsRepository;

    private $listsRepository;

    public function __construct(
        TemplatesRepository $templatesRepository,
        LogsRepository $logsRepository,
        TemplateFormFactory $templateFormFactory,
        TemplateTestFormFactory $templateTestFormFactory,
        LayoutsRepository $layoutsRepository,
        ListsRepository $listsRepository
    ) {

        parent::__construct();
        $this->templatesRepository = $templatesRepository;
        $this->logsRepository = $logsRepository;
        $this->templateFormFactory = $templateFormFactory;
        $this->templateTestFormFactory = $templateTestFormFactory;
        $this->layoutsRepository = $layoutsRepository;
        $this->listsRepository = $listsRepository;
    }

    public function createComponentDataTableDefault(IDataTableFactory $dataTableFactory)
    {
        $mailTypePairs = $this->listsRepository->all()->fetchPairs('id', 'title');

        $dataTable = $dataTableFactory->create();
        $dataTable
            ->setColSetting('created_at', [
                'header' => 'created at',
                'render' => 'date',
                'priority' => 1,
            ])
            ->setColSetting('code', [
                'priority' => 2,
            ])
            ->setColSetting('subject', [
                'priority' => 1,
            ])
            ->setColSetting('type', [
                'orderable' => false,
                'filter' => $mailTypePairs,
                'priority' => 2,
            ])
            ->setColSetting('opened', [
                'priority' => 3,
            ])
            ->setColSetting('clicked', [
                'priority' => 3,
            ])
            ->setRowAction('show', 'palette-Cyan zmdi-eye', 'Show template')
            ->setRowAction('edit', 'palette-Cyan zmdi-edit', 'Edit template')
            ->setRowAction('duplicate', 'palette-Cyan zmdi-copy', 'Duplicate template')
            ->setTableSetting('order', Json::encode([[0, 'DESC']]));

        return $dataTable;
    }

    public function renderDefaultJsonData()
    {
        $request = $this->request->getParameters();

        $listIds = null;
        foreach ($request['columns'] as $column) {
            if ($column['name'] !== 'type') {
                continue;
            }
            if (!empty($column['search']['value'])) {
                $listIds = explode(',', $column['search']['value']);
            }
            break;
        }

        $templatesCount = $this->templatesRepository
            ->tableFilter($request['search']['value'], $request['columns'][$request['order'][0]['column']]['name'], $request['order'][0]['dir'], $listIds)
            ->count('*');

        $templates = $this->templatesRepository
            ->tableFilter($request['search']['value'], $request['columns'][$request['order'][0]['column']]['name'], $request['order'][0]['dir'], $listIds, $request['length'], $request['start'])
            ->fetchAll();

        $result = [
            'recordsTotal' => $this->templatesRepository->totalCount(),
            'recordsFiltered' => $templatesCount,
            'data' => []
        ];

        /** @var ActiveRow $template */
        foreach ($templates as $template) {
            $editUrl = $this->link('Edit', $template->id);
            $result['data'][] = [
                'actions' => [
                    'show' => $this->link('Show', $template->id),
                    'edit' => $this->link('Edit', $template->id),
                    'duplicate' => $this->link('Duplicate!', $template->id),
                ],
                $template->created_at,
                $template->code,
                "<a href='{$editUrl}'>{$template->subject}</a>",
                $template->type->title,
                $template->related('mail_job_batch_template')->sum('opened') + $template->related('mail_logs')->where('mail_job_id IS NULL')->count('opened_at'),
                $template->related('mail_job_batch_template')->sum('clicked') + $template->related('mail_logs')->where('mail_job_id IS NULL')->count('clicked_at'),
            ];
        }
        $this->presenter->sendJson($result);
    }

    public function renderShow($id)
    {
        $template = $this->templatesRepository->find($id);
        if (!$template) {
            throw new BadRequestException();
        }

{'fraction_non_alphanumeric': 0.15115037375012136, 'fraction_numerical': 0.00213571497912824, 'mean_word_length': 2.42828618968386, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 149, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1358168', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2672, 'n_words': 437}