<commit_before><?php
/**
 * Litus is a project by a group of students from the K.U.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>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace FormBundle\Controller;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    DateTime,
    FormBundle\Entity\Nodes\Form,
    FormBundle\Entity\Nodes\GuestInfo,
    FormBundle\Entity\Nodes\Entry as FormEntry,
    FormBundle\Entity\Entry as FieldEntry,
    FormBundle\Form\SpecifiedForm,
    Zend\Mail\Message,
    Zend\View\Model\ViewModel;

/**
 * FormController
 *
 * @author <NAME> <<EMAIL>>
 */
class FormController extends \CommonBundle\Component\Controller\ActionController\SiteController
{
    public function viewAction()
    {
        $formSpecification = $this->_getForm();

        if (!$formSpecification) {
            return new ViewModel();
        }

        $message = null;

        $now = new DateTime();
        if ($now < $formSpecification->getStartDate() ||
            $now > $formSpecification->getEndDate() ||
            !$formSpecification->isActive())
        {
            $message = 'This form is currently closed.';

            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        }

        $entriesCount = count($this->getEntityManager()
            ->getRepository('FormBundle\Entity\Nodes\Entry')
            ->findAllByForm($formSpecification));

        if ($formSpecification->getMax() != 0 && $entriesCount >= $formSpecification->getMax()) {
            $message = 'This form has reached the maximum number of submissions.';

            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        }

        $person = $this->getAuthentication()->getPersonObject();

        if ($person === null && !$formSpecification->isNonMember()) {
            $message = 'Please log in to view this form.';

            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        } else if ($person !== null) {
            $entriesCount = count($this->getEntityManager()
                ->getRepository('FormBundle\Entity\Nodes\Entry')
                ->findAllByFormAndPerson($formSpecification, $person));

            if (!$formSpecification->isMultiple() && $entriesCount > 0) {
                $message = 'You can\'t fill this form more than once.';

                return new ViewModel(
                    array(
                        'message'       => $message,
                        'specification' => $formSpecification,
                    )
                );
            }
        }

        if ($message) {
            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        }

        $form = new SpecifiedForm($this->getEntityManager(), $this->getLanguage(), $formSpecification, $person);

        if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $formData = $form->getFormData($formData);

                $guestInfo = null;
                // Create non-member entry
                if ($person === null) {
                    $guestInfo = new GuestInfo(
                        $formData['first_name'],
                        $formData['last_name'],
                        $formData['email']
                    );
                    $this->getEntityManager()->persist($guestInfo);
                }

                $formEntry = new FormEntry($person, $guestInfo, $formSpecification);

                $this->getEntityManager()->persist($formEntry);
                $this->getEntityManager()->flush();

                foreach ($formSpecification->getFields() as $field) {

                    $value = $formData['field-' . $field->getId()];

                    $fieldEntry = new FieldEntry($formEntry, $field, $value);

                    $formEntry->addFieldEntry($fieldEntry);

                    $this->getEntityManager()->persist($fieldEntry);
                }

                $this->getEntityManager()->flush();

                if ($formSpecification->hasMail()) {

                    $mailAddress = $formSpecification->getMailFrom();

                    $mail = new Message();
                    $mail->setBody($formSpecification->getCompletedMailBody($formEntry))
                        ->setFrom($mailAddress)
                        ->setSubject($formSpecification->getMailSubject());

                    $mail->addTo($formEntry->getPersonInfo()->getEmail(), $formEntry->getPersonInfo()->getFullName());

                    if ('development' != getenv('APPLICATION_ENV'))
                        $this->getMailTransport()->send($mail);
                }

                $this->redirect()->toRoute(
                    'form_view',
                    array
                    (
                        'action'   => 'complete',
                        'id'       => $formSpecification->getId(),
                        'language' => $this->getLanguage()->getAbbrev(),
                    )
                );

                return new ViewModel();
            }
        }

        return new ViewModel(
            array(
                'specification' => $formSpecification,
                'form' => $form,
            )
        );
    }

    public function completeAction()
    {<commit_msg>Forgot to Actually send BCC mail<commit_after><?php
/**
 * Litus is a project by a group of students from the K.U.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>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace FormBundle\Controller;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    DateTime,
    FormBundle\Entity\Nodes\Form,
    FormBundle\Entity\Nodes\GuestInfo,
    FormBundle\Entity\Nodes\Entry as FormEntry,
    FormBundle\Entity\Entry as FieldEntry,
    FormBundle\Form\SpecifiedForm,
    Zend\Mail\Message,
    Zend\View\Model\ViewModel;

/**
 * FormController
 *
 * @author <NAME> <<EMAIL>>
 */
class FormController extends \CommonBundle\Component\Controller\ActionController\SiteController
{
    public function viewAction()
    {
        $formSpecification = $this->_getForm();

        if (!$formSpecification) {
            return new ViewModel();
        }

        $message = null;

        $now = new DateTime();
        if ($now < $formSpecification->getStartDate() ||
            $now > $formSpecification->getEndDate() ||
            !$formSpecification->isActive())
        {
            $message = 'This form is currently closed.';

            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        }

        $entriesCount = count($this->getEntityManager()
            ->getRepository('FormBundle\Entity\Nodes\Entry')
            ->findAllByForm($formSpecification));

        if ($formSpecification->getMax() != 0 && $entriesCount >= $formSpecification->getMax()) {
            $message = 'This form has reached the maximum number of submissions.';

            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        }

        $person = $this->getAuthentication()->getPersonObject();

        if ($person === null && !$formSpecification->isNonMember()) {
            $message = 'Please log in to view this form.';

            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        } else if ($person !== null) {
            $entriesCount = count($this->getEntityManager()
                ->getRepository('FormBundle\Entity\Nodes\Entry')
                ->findAllByFormAndPerson($formSpecification, $person));

            if (!$formSpecification->isMultiple() && $entriesCount > 0) {
                $message = 'You can\'t fill this form more than once.';

                return new ViewModel(
                    array(
                        'message'       => $message,
                        'specification' => $formSpecification,
                    )
                );
            }
        }

        if ($message) {
            return new ViewModel(
                array(
                    'message'       => $message,
                    'specification' => $formSpecification,
                )
            );
        }

        $form = new SpecifiedForm($this->getEntityManager(), $this->getLanguage(), $formSpecification, $person);

        if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $formData = $form->getFormData($formData);

                $guestInfo = null;
                // Create non-member entry
                if ($person === null) {
                    $guestInfo = new GuestInfo(
                        $formData['first_name'],
                        $formData['last_name'],
                        $formData['email']
                    );
                    $this->getEntityManager()->persist($guestInfo);
                }

                $formEntry = new FormEntry($person, $guestInfo, $formSpecification);

                $this->getEntityManager()->persist($formEntry);
                $this->getEntityManager()->flush();

                foreach ($formSpecification->getFields() as $field) {

                    $value = $formData['field-' . $field->getId()];

                    $fieldEntry = new FieldEntry($formEntry, $field, $value);

                    $formEntry->addFieldEntry($fieldEntry);

                    $this->getEntityManager()->persist($fieldEntry);
                }

                $this->getEntityManager()->flush();

                if ($formSpecification->hasMail()) {

                    $mailAddress = $formSpecification->getMailFrom();

                    $mail = new Message();
                    $mail->setBody($formSpecification->getCompletedMailBody($formEntry))
                        ->setFrom($mailAddress)
                        ->setSubject($formSpecification->getMailSubject());

                    $mail->addTo($formEntry->getPersonInfo()->getEmail(), $formEntry->getPersonInfo()->getFullName());

                    if ($formSpecification->getMailBcc())
                        $mail->addBcc($mailAddress);

                    if ('development' != getenv('APPLICATION_ENV'))
                        $this->getMailTransport()->send($mail);
                }

                $this->redirect()->toRoute(
                    'form_view',
                    array
                    (
                        'action'   => 'complete',
                        'id'       => $formSpecification->getId(),
                        'language' => $this->getLanguage()->getAbbrev(),
                    )
                );

                return new ViewModel();
            }
        }

        return new ViewModel(
            array(
                'specification' => $formSpecification,
                'form' => $form,
            )
        );
    }

    public function completeAction()
    {
{'fraction_non_alphanumeric': 0.12971293039993456, 'fraction_numerical': 0.0003271448433794062, 'mean_word_length': 1.5119145439605588, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 191, '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': '7584134', 'n_tokens_mistral': 3129, 'n_tokens_neox': 2902, 'n_words': 576}