<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>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace MailBundle\Controller\Admin;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    CommonBundle\Component\Util\AcademicYear,
    CommonBundle\Entity\General\AcademicYear as AcademicYearEntity,
    DateInterval,
    DateTime,
    MailBundle\Form\Admin\Cudi\Mail as MailForm,
    Zend\Mail\Message,
    Zend\View\Model\ViewModel;

/**
 * ProfController
 *
 * @autor <NAME> <<EMAIL>>
 */    
class ProfController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function cudiAction()
    {
        $academicYear = $this->_getAcademicYear();
    
        $semester = (new DateTime() < $academicYear->getUniversityStartDate()) ? 1 : 2;
        
        $mailSubject = str_replace(
            array(
                '{{ semester }}',
                '{{ academicYear }}',
            ),
            array(
                $semester,
                $academicYear->getCode(),
            ),
            $this->getEntityManager()
                ->getRepository('CommonBundle\Entity\General\Config')
                ->getConfigValue('mail.start_cudi_mail_subject')
        );
        
        $mail = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('mail.start_cudi_mail');
        
        $form = new MailForm();
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->post()->toArray();
            
            if ($form->isValid($formData)) {
                $mailAddress = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('cudi.mail');
                    
                $mailName = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('cudi.mail_name');
                    
                $statuses = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\Users\Statuses\University')
                    ->findAllByStatus('professor', $academicYear);
                
                foreach($statuses as $status) {
                    $allSubjects = $this->getEntityManager()
                        ->getRepository('SyllabusBundle\Entity\SubjectProfMap')
                        ->findAllByProfAndAcademicYear($status->getPerson(), $academicYear);
                        
                    $subjects = array();
                    foreach($allSubjects as $subject) {
                        if ($subject->getSubject()->getSemester() == $semester || $subject->getSubject()->getSemester() == 3) {
                            $subjects[] = $subject->getSubject();
                        }
                    }
                    
                    if (sizeof($subjects) == 0)
                        continue;
                        
                    $text = '';
                    foreach($subjects as $subject)
                        $text .= ' - ' . $subject->getName() . PHP_EOL;
        
                    $body = str_replace('{{ subjects }}', ' ' . trim($text), $mail);

                    $message = new Message();
                    $message->setBody($body)
                        ->setFrom($mailAddress, $mailName)
                        ->setSubject($mailSubject);
                    
                    if ($formData['test_it']) {
                        $message->addTo(
                            $this->getEntityManager()
                                ->getRepository('CommonBundle\Entity\General\Config')
                                ->getConfigValue('system_administrator_mail'),
                            'System Administrator'
                        );
                    } else {
                        $message->addTo($status->getPerson()->getEmail(), $status->getPerson()->getFullName());                    
                    }
                       
                    if ('production' == getenv('APPLICATION_ENV'))
                        $this->getMailTransport()->send($message);
                    
                    if ($formData['test_it'])
                        break;
                }

                $this->flashMessenger()->addMessage(
                    new FlashMessage(
                        FlashMessage::SUCCESS,
                        'Success',
                        'The mail was successfully send!'
                    )
                );
                
                $this->redirect()->toRoute(
                    'admin_mail_prof',
                    array(
                        'action' => 'cudi'
                    )
                );
                
                return new ViewModel();
            }
        }
        
        return new ViewModel(
            array(
                'subject' => $subject,
                'mail' => $mail,
                'semester' => $semester,
                'form' => $form,
            )
        );
    }
     
    private function _getAcademicYear()
    {
        $startAcademicYear = AcademicYear::getStartOfAcademicYear();
        $startAcademicYear->setTime(0, 0);
                
        $now = new DateTime();
        $profStart = new DateTime($this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('cudi.prof_start_academic_year'));
        if ($now > $profStart) {
            $startAcademicYear->add(new DateInterval('P1Y2M'));
            $startAcademicYear = AcademicYear::getStartOfAcademicYear($startAcademicYear);
        }

        $academicYear = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\AcademicYear')
            ->findOneByUniversityStart($startAcademicYear);
        <commit_msg>Made a small mistake<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>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace MailBundle\Controller\Admin;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    CommonBundle\Component\Util\AcademicYear,
    CommonBundle\Entity\General\AcademicYear as AcademicYearEntity,
    DateInterval,
    DateTime,
    MailBundle\Form\Admin\Cudi\Mail as MailForm,
    Zend\Mail\Message,
    Zend\View\Model\ViewModel;

/**
 * ProfController
 *
 * @autor <NAME> <<EMAIL>>
 */    
class ProfController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function cudiAction()
    {
        $academicYear = $this->_getAcademicYear();
    
        $semester = (new DateTime() < $academicYear->getUniversityStartDate()) ? 1 : 2;
        
        $mailSubject = str_replace(
            array(
                '{{ semester }}',
                '{{ academicYear }}',
            ),
            array(
                $semester,
                $academicYear->getCode(),
            ),
            $this->getEntityManager()
                ->getRepository('CommonBundle\Entity\General\Config')
                ->getConfigValue('mail.start_cudi_mail_subject')
        );
        
        $mail = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('mail.start_cudi_mail');
        
        $form = new MailForm();
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->post()->toArray();
            
            if ($form->isValid($formData)) {
                $mailAddress = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('cudi.mail');
                    
                $mailName = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\General\Config')
                    ->getConfigValue('cudi.mail_name');
                    
                $statuses = $this->getEntityManager()
                    ->getRepository('CommonBundle\Entity\Users\Statuses\University')
                    ->findAllByStatus('professor', $academicYear);
                
                foreach($statuses as $status) {
                    $allSubjects = $this->getEntityManager()
                        ->getRepository('SyllabusBundle\Entity\SubjectProfMap')
                        ->findAllByProfAndAcademicYear($status->getPerson(), $academicYear);
                        
                    $subjects = array();
                    foreach($allSubjects as $subject) {
                        if ($subject->getSubject()->getSemester() == $semester || $subject->getSubject()->getSemester() == 3) {
                            $subjects[] = $subject->getSubject();
                        }
                    }
                    
                    if (sizeof($subjects) == 0)
                        continue;
                        
                    $text = '';
                    foreach($subjects as $subject)
                        $text .= ' - ' . $subject->getName() . PHP_EOL;
        
                    $body = str_replace('{{ subjects }}', ' ' . trim($text), $mail);

                    $message = new Message();
                    $message->setBody($body)
                        ->setFrom($mailAddress, $mailName)
                        ->setSubject($mailSubject);
                    
                    if ($formData['test_it']) {
                        $message->addTo(
                            $this->getEntityManager()
                                ->getRepository('CommonBundle\Entity\General\Config')
                                ->getConfigValue('system_administrator_mail'),
                            'System Administrator'
                        );
                    } else {
                        $message->addTo($status->getPerson()->getEmail(), $status->getPerson()->getFullName());                    
                    }
                       
                    if ('production' == getenv('APPLICATION_ENV'))
                        $this->getMailTransport()->send($message);
                    
                    if ($formData['test_it'])
                        break;
                }

                $this->flashMessenger()->addMessage(
                    new FlashMessage(
                        FlashMessage::SUCCESS,
                        'Success',
                        'The mail was successfully send!'
                    )
                );
                
                $this->redirect()->toRoute(
                    'admin_mail_prof',
                    array(
                        'action' => 'cudi'
                    )
                );
                
                return new ViewModel();
            }
        }
        
        return new ViewModel(
            array(
                'subject' => $mailSubject,
                'mail' => $mail,
                'semester' => $semester,
                'form' => $form,
            )
        );
    }
     
    private function _getAcademicYear()
    {
        $startAcademicYear = AcademicYear::getStartOfAcademicYear();
        $startAcademicYear->setTime(0, 0);
                
        $now = new DateTime();
        $profStart = new DateTime($this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\Config')
            ->getConfigValue('cudi.prof_start_academic_year'));
        if ($now > $profStart) {
            $startAcademicYear->add(new DateInterval('P1Y2M'));
            $startAcademicYear = AcademicYear::getStartOfAcademicYear($startAcademicYear);
        }

        $academicYear = $this->getEntityManager()
            ->getRepository('CommonBundle\Entity\General\AcademicYear')
            ->findOneByUniversityStart($startAcademicYear);
        
{'fraction_non_alphanumeric': 0.12296593186372745, 'fraction_numerical': 0.001282565130260521, 'mean_word_length': 1.3601967461218312, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 177, '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': '3868923', 'n_tokens_mistral': 3182, 'n_tokens_neox': 2950, 'n_words': 473}