<commit_before><?php

namespace SilverStripe\UserForms\Model\Recipient;

use SilverStripe\Assets\FileFinder;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Session;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableMultipleOptionField;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\View\Requirements;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;

/**
 * A Form can have multiply members / emails to email the submission
 * to and custom subjects
 *
 * @package userforms
 */
class EmailRecipient extends DataObject
{
    private static $db = [
        'EmailAddress' => 'Varchar(200)',
        'EmailSubject' => 'Varchar(200)',
        'EmailFrom' => 'Varchar(200)',
        'EmailReplyTo' => 'Varchar(200)',
        'EmailBody' => 'Text',
        'EmailBodyHtml' => 'HTMLText',
        'EmailTemplate' => 'Varchar',
        'SendPlain' => 'Boolean',
        'HideFormData' => 'Boolean',
        'CustomRulesCondition' => 'Enum("And,Or")'
    ];

    private static $has_one = [
        'Form' => UserDefinedForm::class,
        'SendEmailFromField' => EditableFormField::class,
        'SendEmailToField' => EditableFormField::class,
        'SendEmailSubjectField' => EditableFormField::class
    ];

    private static $has_many = [
        'CustomRules' => EmailRecipientCondition::class,
    ];

    private static $owns = [
        'CustomRules',
    ];

    private static $cascade_deetes = [
        'CustomRules',
    ];

    private static $summary_fields = [
        'EmailAddress',
        'EmailSubject',
        'EmailFrom'
    ];

    private static $table_name = 'UserDefinedForm_EmailRecipient';

    /**
     * Setting this to true will allow you to select "risky" fields as
     * email recipient, such as free-text entry fields.
     *
     * It's advisable to leave this off.
     *
     * @config
     * @var bool
     */
    private static $allow_unbound_recipient_fields = false;

    public function summaryFields()
    {
        $fields = parent::summaryFields();
        if (isset($fields['EmailAddress'])) {
            $fields['EmailAddress'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILADDRESS', Email::class);
        }
        if (isset($fields['EmailSubject'])) {
            $fields['EmailSubject'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILSUBJECT', 'Subject');
        }
        if (isset($fields['EmailFrom'])) {
            $fields['EmailFrom'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILFROM', 'From');
        }
        return $fields;
    }

    /**
     * Get instance of UserDefinedForm when editing in getCMSFields
     *
     * @return UserDefinedFrom
     */
    protected function getFormParent()
    {
        $formID = $this->FormID
            ? $this->FormID
            : Session::get('CMSMain.currentPage');
        return UserDefinedForm::get()->byID($formID);
    }

    public function getTitle()
    {
        if ($this->EmailAddress) {
            return $this->EmailAddress;
        }
        if ($this->EmailSubject) {
            return $this->EmailSubject;
        }
        return parent::getTitle();
    }

    /**
     * Generate a gridfield config for editing filter rules
     *
     * @return GridFieldConfig
     */
    protected function getRulesConfig()
    {
        $formFields = $this->getFormParent()->Fields();

        $config = GridFieldConfig::create()
            ->addComponents(
                new GridFieldButtonRow('before'),<commit_msg>FIX Replace static session getter for the current page ID<commit_after><?php

namespace SilverStripe\UserForms\Model\Recipient;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Assets\FileFinder;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableMultipleOptionField;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\View\Requirements;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;

/**
 * A Form can have multiply members / emails to email the submission
 * to and custom subjects
 *
 * @package userforms
 */
class EmailRecipient extends DataObject
{
    private static $db = [
        'EmailAddress' => 'Varchar(200)',
        'EmailSubject' => 'Varchar(200)',
        'EmailFrom' => 'Varchar(200)',
        'EmailReplyTo' => 'Varchar(200)',
        'EmailBody' => 'Text',
        'EmailBodyHtml' => 'HTMLText',
        'EmailTemplate' => 'Varchar',
        'SendPlain' => 'Boolean',
        'HideFormData' => 'Boolean',
        'CustomRulesCondition' => 'Enum("And,Or")'
    ];

    private static $has_one = [
        'Form' => UserDefinedForm::class,
        'SendEmailFromField' => EditableFormField::class,
        'SendEmailToField' => EditableFormField::class,
        'SendEmailSubjectField' => EditableFormField::class
    ];

    private static $has_many = [
        'CustomRules' => EmailRecipientCondition::class,
    ];

    private static $owns = [
        'CustomRules',
    ];

    private static $cascade_deetes = [
        'CustomRules',
    ];

    private static $summary_fields = [
        'EmailAddress',
        'EmailSubject',
        'EmailFrom'
    ];

    private static $table_name = 'UserDefinedForm_EmailRecipient';

    /**
     * Setting this to true will allow you to select "risky" fields as
     * email recipient, such as free-text entry fields.
     *
     * It's advisable to leave this off.
     *
     * @config
     * @var bool
     */
    private static $allow_unbound_recipient_fields = false;

    public function summaryFields()
    {
        $fields = parent::summaryFields();
        if (isset($fields['EmailAddress'])) {
            $fields['EmailAddress'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILADDRESS', Email::class);
        }
        if (isset($fields['EmailSubject'])) {
            $fields['EmailSubject'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILSUBJECT', 'Subject');
        }
        if (isset($fields['EmailFrom'])) {
            $fields['EmailFrom'] = _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILFROM', 'From');
        }
        return $fields;
    }

    /**
     * Get instance of UserDefinedForm when editing in getCMSFields
     *
     * @return UserDefinedFrom
     */
    protected function getFormParent()
    {
        // LeftAndMain::sessionNamespace is protected. @todo replace this with a non-deprecated equivalent.
        $sessionNamespace = $this->config()->get('session_namespace') ?: LeftAndMain::class;

        $formID = $this->FormID
            ? $this->FormID
            : Controller::curr()->getRequest()->getSession()->get($sessionNamespace . '.currentPage');
        return UserDefinedForm::get()->byID($formID);
    }

    public function getTitle()
    {
        if ($this->EmailAddress) {
            return $this->EmailAddress;
        }
        if ($this->EmailSubject) {
            return $this->EmailSubject;
        }
        return parent::getTitle();
    }

    /**
     * Generate a gridfield config for editing filter rules
     *
     * @return GridFieldConfig
     */
    protected function getRulesConfig()
    {
        $formFields = $this->getFormParent()->Fields();

        $config = GridFieldConfig::create()
            ->addComponents(
                new GridFieldButtonRow('before'),
{'fraction_non_alphanumeric': 0.11642580907795644, 'fraction_numerical': 0.0024424994911459393, 'mean_word_length': 3.363676731793961, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, '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': '5436367', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2915, 'n_words': 532}