<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 MailBundle\Controller\Admin;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    CommonBundle\Entity\User\Status\Organization as OrganizationStatus,
    CommonBundle\Entity\User\Status\University as UniversityStatus,
    MailBundle\Form\Admin\Mail\Mail as MailForm,
    Zend\Mail\Message,
    Zend\Validator\EmailAddress as EmailAddressValidator,
    Zend\View\Model\ViewModel;

/**
 * GroupController
 *
 * @autor <NAME> <<EMAIL>>
 */
class GroupController extends \MailBundle\Component\Controller\AdminController
{
    public function groupsAction()
    {
        return new ViewModel(
            array(
                'university' => UniversityStatus::$possibleStatuses,
                'organization' => OrganizationStatus::$possibleStatuses,
            )
        );
    }

    public function sendAction()
    {
        if (!($type = $this->_getType()))
            return new ViewModel();

        if ('organization' == $type) {
            if (!($status = $this->_getOrganizationStatus()))
                return new ViewModel();
            $statuses = OrganizationStatus::$possibleStatuses;
        } else {
            if (!($status = $this->_getUniversityStatus()))
                return new ViewModel();
            $statuses = UniversityStatus::$possibleStatuses;
        }

        $form = new MailForm();

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

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

                $mailAddress = $formData['from'];
                if ('' == $mailAddress) {
                    $mailAddress = $this->getEntityManager()
                        ->getRepository('CommonBundle\Entity\General\Config')
                        ->getConfigValue('system_mail_address');
                }

                $mailName = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('system_mail_name');

                $mail = new Message();
                $mail->setBody($formData['message'])
                    ->setFrom($mailAddress, $mailName)
                    ->setSubject($formData['subject']);

                if ('organization' == $type) {
                    $people = $this->getEntityManager()
                        ->getRepository('CommonBundle\Entity\User\Status\Organization')
                        ->findAllByStatus($status, $this->getCurrentAcademicYear(false));
                } else {
                    $people = $this->getEntityManager()
                        ->getRepository('CommonBundle\Entity\User\Status\University')
                        ->findAllByStatus($status, $this->getCurrentAcademicYear(false));
                }

                $mail->addTo($mailAddress, $mailName);

                $emailValidator = new EmailAddressValidator();

                $addresses = array();
                foreach ($people as $person) {
                    if (null === $person->getPerson()->getEmail() || !$emailValidator->isValid($person->getPerson()->getEmail()))
                        continue;

                    $addresses[$person->getPerson()->getEmail()] = $person->getPerson()->getEmail();
                }

                $i = 0;
                foreach ($addresses as $address) {
                    $mail->addBcc($address);
                    $i++;

                    if (500 == $i) {
                        $i = 0;

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

                        $mail->setBcc(array());
                    }
                }

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

                $this->flashMessenger()->addMessage(<commit_msg>Improved mailing a group a bit<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 MailBundle\Controller\Admin;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    CommonBundle\Entity\User\Status\Organization as OrganizationStatus,
    CommonBundle\Entity\User\Status\University as UniversityStatus,
    MailBundle\Form\Admin\Mail\Mail as MailForm,
    Zend\Mail\Message,
    Zend\View\Model\ViewModel;

/**
 * GroupController
 *
 * @autor <NAME> <<EMAIL>>
 */
class GroupController extends \MailBundle\Component\Controller\AdminController
{
    public function groupsAction()
    {
        return new ViewModel(
            array(
                'university' => UniversityStatus::$possibleStatuses,
                'organization' => OrganizationStatus::$possibleStatuses,
            )
        );
    }

    public function sendAction()
    {
        if (!($type = $this->_getType()))
            return new ViewModel();

        if ('organization' == $type) {
            if (!($status = $this->_getOrganizationStatus()))
                return new ViewModel();
            $statuses = OrganizationStatus::$possibleStatuses;
        } else {
            if (!($status = $this->_getUniversityStatus()))
                return new ViewModel();
            $statuses = UniversityStatus::$possibleStatuses;
        }

        $form = new MailForm();

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

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

                $mailAddress = $formData['from'];
                if ('' == $mailAddress) {
                    $mailAddress = $this->getEntityManager()
                        ->getRepository('CommonBundle\Entity\General\Config')
                        ->getConfigValue('system_mail_address');
                }

                $mailName = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('system_mail_name');

                $mail = new Message();
                $mail->setBody($formData['message'])
                    ->setFrom($mailAddress, $mailName)
                    ->setSubject($formData['subject']);

                if ('organization' == $type) {
                    $people = $this->getEntityManager()
                        ->getRepository('CommonBundle\Entity\User\Status\Organization')
                        ->findAllByStatus($status, $this->getCurrentAcademicYear(false));
                } else {
                    $people = $this->getEntityManager()
                        ->getRepository('CommonBundle\Entity\User\Status\University')
                        ->findAllByStatus($status, $this->getCurrentAcademicYear(false));
                }

                $mail->addTo($mailAddress, $mailName);

                $addresses = array();
                foreach ($people as $person) {
                    if (null === $person->getPerson()->getEmail())
                        continue;

                    $addresses[$person->getPerson()->getEmail()] = array(
                        'address' => $person->getPerson()->getEmail(),
                        'name'    => $person->getPerson()->getFullName(),
                    );
                }

                $i = 0;
                foreach ($addresses as $address) {
                    $mail->addBcc($address['address'], $address['name']);
                    $i++;

                    if (500 == $i) {
                        $i = 0;

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

                        $mail->setBcc(array());
                    }
                }

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

                $this->flashMessenger()->addMessage(
{'fraction_non_alphanumeric': 0.14182962463474938, 'fraction_numerical': 0.0011238480557428637, 'mean_word_length': 1.7671019900497513, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 158, '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': '1457811', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2170, 'n_words': 422}