<commit_before><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace FormBundle\Form\SpecifiedForm;

use CommonBundle\Entity\General\Language,
    CommonBundle\Entity\User\Person,
    FormBundle\Component\Exception\UnsupportedTypeException,
    FormBundle\Entity\Field\Checkbox as CheckboxFieldEntity,
    FormBundle\Entity\Field\Dropdown as DropdownFieldEntity,
    FormBundle\Entity\Field\File as FileFieldEntity,
    FormBundle\Entity\Field\String as StringFieldEntity,
    FormBundle\Entity\Node\Entry as EntryEntity,
    FormBundle\Entity\Node\Form\Form as FormEntity,
    FormBundle\Entity\Node\GuestInfo as GuestInfoEntity;

/**
 * Specifield Form Add
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Add extends \CommonBundle\Component\Form\Bootstrap\Form
{
    protected $hydrator = 'FormBundle\Hydrator\Node\Entry';

    /**
     * @var Person
     */
    protected $_person;

    /**
    * @var GuestInfoEntity
    */
    protected $_guestInfo;

    /**
     * @var FormEntity
     */
    protected $_form;

    /**
    * @var Language
    */
    protected $_language;

    /**
    * @var EntryEntity
    */
    protected $_entry;

    /**
     * @var boolean
     */
    protected $_isDraft;

    public function init()
    {
        if (!($this->_form instanceof FormEntity)) {
            return;
        }

        if (null === $this->_person) {
            $this->add(array(
                'type'     => 'text',
                'name'     => 'first_name',
                'label'    => '<NAME>',
                'required' => true,
                'value'    => $this->_guestInfo ? $this->_guestInfo->getFirstName() : '',
                'options'  => array(
                    'input' => array(
                        'filter' => array(
                            array('name' => 'StringTrim'),
                        ),
                    ),
                ),
            ));

            $this->add(array(
                'type'     => 'text',
                'name'     => 'last_name',
                'label'    => '<NAME>',
                'required' => true,
                'value'    => $this->_guestInfo ? $this->_guestInfo->getLastName() : '',
                'options'  => array(
                    'input' => array(
                        'filter' => array(
                            array('name' => 'StringTrim'),
                        ),
                    ),
                ),
            ));

            $this->add(array(
                'type'     => 'text',
                'name'     => 'email',
                'label'    => 'Email',
                'required' => true,
                'value'    => $this->_guestInfo ? $this->_guestInfo->getEmail() : '',
                'options'  => array(
                    'input' => array(
                        'filter' => array(
                            array('name' => 'StringTrim'),
                        ),
                        'validators' => array(
                            array(
                                'name' => 'emailaddress',
                            ),
                        ),
                    ),
                ),
            ));
        }

        $fields = $this->getEntityManager()
            ->getRepository('FormBundle\Entity\Field')
            ->findAllByForm($this->_form);

        foreach ($fields as $fieldSpecification) {
            $specification = array(
                'name'       => 'field-' . $fieldSpecification->getId(),
                'label'      => $fieldSpecification->getLabel($this->_language),
                'required'   => $fieldSpecification->isRequired(),
                'attributes' => array(
                    'id' => 'field-' . $fieldSpecification->getId(),
                ),
            );
            if ($fieldSpecification instanceof StringFieldEntity) {
                if ($fieldSpecification->isMultiLine()) {
                    $specification['type'] = 'textarea';
                    $specification['attributes']['rows'] = 3;
                } else {
                    $specification['type'] = 'text';
                }

                $specification['options']['input']['filters'] = array(
                    array('name' => 'StringTrim'),
                );

                if ($fieldSpecification->hasLengthSpecification()) {
                    $specification['attributes']['class'] = 'count';
                    $specification['attributes']['maxlength'] = $fieldSpecification->getLineLength();
                    $specification['attributes']['data-linelen'] = $fieldSpecification->getLineLength();
                    $specification['attributes']['data-linecount'] = $fieldSpecification->getLines();

                    $specification['options']['input']['validators'] = array(
                        array(
                            'name' => 'field_line_length',
                            'options' => array(
                                'chars_per_line' => $fieldSpecification->getLineLength(),
                                'lines' => $fieldSpecification->getLines(),
                            ),
                        ),
                    );
                }
            } elseif ($fieldSpecification instanceof DropdownFieldEntity) {
                $specification['type'] = 'select';
                $specification['attributes']['options'] = $fieldSpecification->getOptionsArray($this->_language);
            } elseif ($fieldSpecification instanceof CheckboxFieldEntity) {
                $specification['type'] = 'checkbox';
            } elseif ($fieldSpecification instanceof FileFieldEntity) {
                $specification['type'] = 'file';
                $specification['options']['input']['validators'] = array(
                    array(
                        'name' => 'filesize',
                        'options' => array(
                            'max' => $fieldSpecification->getMaxSize() . 'MB',
                        ),
                    ),
                );
            } else {
                throw new UnsupportedTypeException('This field type is unknown!');
            }

            if (null !== $fieldSpecification->getVisibilityDecissionField()) {
                $specification['attributes']['data-visible_if_element'] = $fieldSpecification->getVisibilityDecissionField()->getId();
                $specification['attributes']['data-visible_if_value'] = $fieldSpecification->getVisibilityValue();
            }
            $this->add($specification);
        }

        if ($this->_form->isEditableByUser() && !$this->_isDraft) {
            $this->addSubmit('Save as Draft', 'btn-info', 'save_as_draft');
        }

        $this->addSubmit($this->_form->getSubmitText($this->_language));

        if (null !== $this->_entry) {
            $this->bind($this->_entry);

            foreach ($this->_entry->getFieldEntries() as $fieldEntry) {
                if ($fieldEntry->getField() instanceof FileFieldEntity) {
                    $this->get('field-' . $fieldEntry->getField()->getId())
                        ->setAttribute('data-file', $fieldEntry->getValue())
                        ->setAttribute('data-name', $fieldEntry->getReadableValue());
                }
            }
        }
    }

    /**
     * @param  Person $person
     * @return self
     */
    public function setPerson(Person $person = null)
    {
        $this->_person = $person;

        return $this;
    }

    /**
    * @param  GuestInfoEntity $guestInfo
    * @return self
    */
    public function setGuestInfo(GuestInfoEntity $guestInfo = null)
    {
        $this->_guestInfo = $guestInfo;

        return $this;
    }

    /**
    * @param  FormEntity $form
    * @return self
    */
    public function setForm(FormEntity $form)
    {
        $this->_form = $form;

        return $this;
    }

    /**
    * @param  Language $language
    * @return self
    */
    public function setLanguage(Language $language)
    {
        $this->_language = $language;

        return $this;
    }

    /**
    * @param  EntryEntity $entry
    * @return self
    */
    public function setEntry(EntryEntity $entry = null)
    {
        $this->_entry = $entry;

        return $this;
    }

    /**
    * @param  boolean $isDraft
    * @return self
    */
    public function isDraft($isDraft)
    {
        $this->_isDraft = $isDraft;

        return $this;
    }
}
<commit_msg>Small fix<commit_after><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace FormBundle\Form\SpecifiedForm;

use CommonBundle\Entity\General\Language,
    CommonBundle\Entity\User\Person,
    FormBundle\Component\Exception\UnsupportedTypeException,
    FormBundle\Entity\Field\Checkbox as CheckboxFieldEntity,
    FormBundle\Entity\Field\Dropdown as DropdownFieldEntity,
    FormBundle\Entity\Field\File as FileFieldEntity,
    FormBundle\Entity\Field\String as StringFieldEntity,
    FormBundle\Entity\Node\Entry as EntryEntity,
    FormBundle\Entity\Node\Form\Form as FormEntity,
    FormBundle\Entity\Node\GuestInfo as GuestInfoEntity;

/**
 * Specifield Form Add
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Add extends \CommonBundle\Component\Form\Bootstrap\Form
{
    protected $hydrator = 'FormBundle\Hydrator\Node\Entry';

    /**
     * @var Person
     */
    protected $_person;

    /**
    * @var GuestInfoEntity
    */
    protected $_guestInfo;

    /**
     * @var FormEntity
     */
    protected $_form;

    /**
    * @var Language
    */
    protected $_language;

    /**
    * @var EntryEntity
    */
    protected $_entry;

    /**
     * @var boolean
     */
    protected $_isDraft;

    public function init()
    {
        if (!($this->_form instanceof FormEntity)) {
            return;
        }

        if (null === $this->_person) {
            $this->add(array(
                'type'     => 'text',
                'name'     => 'first_name',
                'label'    => '<NAME>',
                'required' => true,
                'value'    => $this->_guestInfo ? $this->_guestInfo->getFirstName() : '',
                'options'  => array(
                    'input' => array(
                        'filter' => array(
                            array('name' => 'StringTrim'),
                        ),
                    ),
                ),
            ));

            $this->add(array(
                'type'     => 'text',
                'name'     => 'last_name',
                'label'    => '<NAME>',
                'required' => true,
                'value'    => $this->_guestInfo ? $this->_guestInfo->getLastName() : '',
                'options'  => array(
                    'input' => array(
                        'filter' => array(
                            array('name' => 'StringTrim'),
                        ),
                    ),
                ),
            ));

            $this->add(array(
                'type'     => 'text',
                'name'     => 'email',
                'label'    => 'Email',
                'required' => true,
                'value'    => $this->_guestInfo ? $this->_guestInfo->getEmail() : '',
                'options'  => array(
                    'input' => array(
                        'filter' => array(
                            array('name' => 'StringTrim'),
                        ),
                        'validators' => array(
                            array(
                                'name' => 'emailaddress',
                            ),
                        ),
                    ),
                ),
            ));
        }

        $fields = $this->getEntityManager()
            ->getRepository('FormBundle\Entity\Field')
            ->findAllByForm($this->_form);

        foreach ($fields as $fieldSpecification) {
            $specification = array(
                'name'       => 'field-' . $fieldSpecification->getId(),
                'label'      => $fieldSpecification->getLabel($this->_language),
                'required'   => $fieldSpecification->isRequired(),
                'attributes' => array(
                    'id' => 'field-' . $fieldSpecification->getId(),
                ),
            );
            if ($fieldSpecification instanceof StringFieldEntity) {
                if ($fieldSpecification->isMultiLine()) {
                    $specification['type'] = 'textarea';
                    $specification['attributes']['rows'] = 3;
                } else {
                    $specification['type'] = 'text';
                }

                $specification['options']['input']['filters'] = array(
                    array('name' => 'StringTrim'),
                );

                if ($fieldSpecification->hasLengthSpecification()) {
                    $specification['attributes']['class'] = 'count';
                    $specification['attributes']['maxlength'] = $fieldSpecification->getLineLength() * $fieldSpecification->getLines();
                    $specification['attributes']['data-linelen'] = $fieldSpecification->getLineLength();
                    $specification['attributes']['data-linecount'] = $fieldSpecification->getLines();

                    $specification['options']['input']['validators'] = array(
                        array(
                            'name' => 'field_line_length',
                            'options' => array(
                                'chars_per_line' => $fieldSpecification->getLineLength(),
                                'lines' => $fieldSpecification->getLines(),
                            ),
                        ),
                    );
                }
            } elseif ($fieldSpecification instanceof DropdownFieldEntity) {
                $specification['type'] = 'select';
                $specification['attributes']['options'] = $fieldSpecification->getOptionsArray($this->_language);
            } elseif ($fieldSpecification instanceof CheckboxFieldEntity) {
                $specification['type'] = 'checkbox';
            } elseif ($fieldSpecification instanceof FileFieldEntity) {
                $specification['type'] = 'file';
                $specification['options']['input']['validators'] = array(
                    array(
                        'name' => 'filesize',
                        'options' => array(
                            'max' => $fieldSpecification->getMaxSize() . 'MB',
                        ),
                    ),
                );
            } else {
                throw new UnsupportedTypeException('This field type is unknown!');
            }

            if (null !== $fieldSpecification->getVisibilityDecissionField()) {
                $specification['attributes']['data-visible_if_element'] = $fieldSpecification->getVisibilityDecissionField()->getId();
                $specification['attributes']['data-visible_if_value'] = $fieldSpecification->getVisibilityValue();
            }
            $this->add($specification);
        }

        if ($this->_form->isEditableByUser() && !$this->_isDraft) {
            $this->addSubmit('Save as Draft', 'btn-info', 'save_as_draft');
        }

        $this->addSubmit($this->_form->getSubmitText($this->_language));

        if (null !== $this->_entry) {
            $this->bind($this->_entry);

            foreach ($this->_entry->getFieldEntries() as $fieldEntry) {
                if ($fieldEntry->getField() instanceof FileFieldEntity) {
                    $this->get('field-' . $fieldEntry->getField()->getId())
                        ->setAttribute('data-file', $fieldEntry->getValue())
                        ->setAttribute('data-name', $fieldEntry->getReadableValue());
                }
            }
        }
    }

    /**
     * @param  Person $person
     * @return self
     */
    public function setPerson(Person $person = null)
    {
        $this->_person = $person;

        return $this;
    }

    /**
    * @param  GuestInfoEntity $guestInfo
    * @return self
    */
    public function setGuestInfo(GuestInfoEntity $guestInfo = null)
    {
        $this->_guestInfo = $guestInfo;

        return $this;
    }

    /**
    * @param  FormEntity $form
    * @return self
    */
    public function setForm(FormEntity $form)
    {
        $this->_form = $form;

        return $this;
    }

    /**
    * @param  Language $language
    * @return self
    */
    public function setLanguage(Language $language)
    {
        $this->_language = $language;

        return $this;
    }

    /**
    * @param  EntryEntity $entry
    * @return self
    */
    public function setEntry(EntryEntity $entry = null)
    {
        $this->_entry = $entry;

        return $this;
    }

    /**
    * @param  boolean $isDraft
    * @return self
    */
    public function isDraft($isDraft)
    {
        $this->_isDraft = $isDraft;

        return $this;
    }
}

{'fraction_non_alphanumeric': 0.14242287773024093, 'fraction_numerical': 0.00011258725512272011, 'mean_word_length': 1.5159325874522023, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 288, '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': '123701', 'n_tokens_mistral': 4717, 'n_tokens_neox': 4478, 'n_words': 830}