<commit_before><?php

namespace Remp\MailerModule\Forms;

use Nette\Application\UI\Form;
use Nette\Forms\Controls\SubmitButton;
use Nette\Object;
use Remp\MailerModule\Forms\Rules\FormRules;
use Remp\MailerModule\Repository\LayoutsRepository;
use Remp\MailerModule\Repository\ListsRepository;
use Remp\MailerModule\Repository\TemplatesRepository;

class TemplateFormFactory extends Object implements IFormFactory
{
    /** @var TemplatesRepository */
    private $templatesRepository;

    /** @var LayoutsRepository */
    private $layoutsRepository;

    /** @var ListsRepository */
    private $listsRepository;

    public $onCreate;

    public $onUpdate;

    public function __construct(
        TemplatesRepository $templatesRepository,
        LayoutsRepository $layoutsRepository,
        ListsRepository $listsRepository
    ) {
        $this->templatesRepository = $templatesRepository;
        $this->layoutsRepository = $layoutsRepository;
        $this->listsRepository = $listsRepository;
    }

    public function create($id)
    {
        $count = 0;
        $defaults = [];

        if (isset($id)) {
            $template = $this->templatesRepository->find($id);
            $count = $template->related('mail_logs')->count('*');
            $defaults = $template->toArray();
        }

        $form = new Form;
        $form->addProtection();

        $form->addHidden('id', $id);

        $form->addText('name', 'Name')
            ->setRequired('Name is required');

        if (isset($id) && $count > 0) {
            $form->addText('code', 'Code')
                ->setRequired('Code is required')
                ->setDisabled();
        } else {
            $form->addText('code', 'Code')
                ->setRequired('Code is required');
        }

        $form->addText('description', 'Description');

        $form->addSelect('mail_layout_id', 'Layout', $this->layoutsRepository->all()->fetchPairs('id', 'name'));

        $form->addSelect('mail_type_id', 'Newsletter list', $this->listsRepository->all()->fetchPairs('id', 'title'))
            ->setRequired('Newsletter list is required');

        $form->addText('from', 'From')
            ->addRule(FormRules::ADVANCED_EMAIL, 'Enter correct email')
            ->setRequired('From field is required');

        $form->addText('subject', 'Subject')<commit_msg>fix missing email layout when creating new email<commit_after><?php

namespace Remp\MailerModule\Forms;

use Nette\Application\UI\Form;
use Nette\Forms\Controls\SubmitButton;
use Nette\Object;
use Remp\MailerModule\Forms\Rules\FormRules;
use Remp\MailerModule\Repository\LayoutsRepository;
use Remp\MailerModule\Repository\ListsRepository;
use Remp\MailerModule\Repository\TemplatesRepository;

class TemplateFormFactory extends Object implements IFormFactory
{
    /** @var TemplatesRepository */
    private $templatesRepository;

    /** @var LayoutsRepository */
    private $layoutsRepository;

    /** @var ListsRepository */
    private $listsRepository;

    public $onCreate;

    public $onUpdate;

    public function __construct(
        TemplatesRepository $templatesRepository,
        LayoutsRepository $layoutsRepository,
        ListsRepository $listsRepository
    ) {
        $this->templatesRepository = $templatesRepository;
        $this->layoutsRepository = $layoutsRepository;
        $this->listsRepository = $listsRepository;
    }

    public function create($id)
    {
        $count = 0;
        $defaults = [];

        $layouts = $this->layoutsRepository->all()->fetchPairs('id', 'name');

        if (isset($id)) {
            $template = $this->templatesRepository->find($id);
            $count = $template->related('mail_logs')->count('*');
            $defaults = $template->toArray();
        } else {
            $defaults['mail_layout_id'] = key($layouts);
        }

        $form = new Form;
        $form->addProtection();

        $form->addHidden('id', $id);

        $form->addText('name', 'Name')
            ->setRequired('Name is required');

        if (isset($id) && $count > 0) {
            $form->addText('code', 'Code')
                ->setRequired('Code is required')
                ->setDisabled();
        } else {
            $form->addText('code', 'Code')
                ->setRequired('Code is required');
        }

        $form->addText('description', 'Description');

        $form->addSelect('mail_layout_id', 'Layout', $layouts);

        $form->addSelect('mail_type_id', 'Newsletter list', $this->listsRepository->all()->fetchPairs('id', 'title'))
            ->setRequired('Newsletter list is required');

        $form->addText('from', 'From')
            ->addRule(FormRules::ADVANCED_EMAIL, 'Enter correct email')
            ->setRequired('From field is required');

        $form->addText('subject', 'Subject')
{'fraction_non_alphanumeric': 0.1448975367418754, 'fraction_numerical': 0.0008279859242392879, 'mean_word_length': 2.804724409448819, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '6121808', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1301, 'n_words': 255}