<commit_before><?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright (c) 2013-2014 Cross Solution (http://cross-solution.de)
 * @license   AGPLv3
 */

/** Applications controllers */ 
namespace Applications\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
use Applications\Entity\Application;
use Zend\View\Model\ViewModel;
use Auth\Entity\AnonymousUser;
use Zend\View\Model\JsonModel;
use Core\Form\Container;
use Core\Form\SummaryForm;
use Core\Entity\PermissionsInterface;
use Applications\Entity\Status;
use Applications\Entity\StatusInterface;

/**
 *
 *
 * @author <NAME> <<EMAIL>>
 */
class ApplyController extends AbstractActionController
{
    
    protected $container;
    
    public function attachDefaultListeners()
    {
        parent::attachDefaultListeners();
        $events = $this->getEventManager();
        $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 10);
    }
    
    public function preDispatch(MvcEvent $e)
    {
        if ($this->params()->fromQuery('do')) {
            $e->getRouteMatch()->setParam('action', 'do');
            return;
        }
        
        $request      = $this->getRequest();
        $services     = $this->getServiceLocator();
        $repositories = $services->get('repositories');
        $repository   = $repositories->get('Applications/Application');
        $container    = $services->get('forms')->get('Applications/Apply');
        
        if ($request->isPost()) {
            $appId = $this->params()->fromPost('applicationId');
            if (!$appId) {
                throw new \RuntimeException('Missing application id.');
            }
            $application = $repository->find($appId);
            if (!$application) {
                throw new \RuntimeException('Invalid application id.');
            }
            $action     = 'process';
            $routeMatch = $e->getRouteMatch();
            $routeMatch->setParam('action', $action);
            
        } else {
            $user  = $this->auth()->getUser();
            $appId = $this->params('applyId');
            if (!$appId) {
                throw new \RuntimeException('Missing apply id');
            }
            $application = $repository->findDraft($user, $appId);
            if ($application) {
               $form = $container->getForm('contact.contact');
               $form->setDisplayMode('summary');
            } else {
                $job = $repositories->get('Jobs/Job')->findOneByApplyId($appId);
                
                if (!$job) {
                    $e->getRouteMatch()->setParam('action', 'job-not-found');
                    return;
                }
                
                $application = $repository->create();
                $application->setIsDraft(true)
                            ->setContact($user->info)
                            ->setUser($user)
                            ->setJob($job);
                $repositories->store($application);
                /*
                 * If we had copy an user image, we need to refresh its data
                 * to populate the length property.
                 */
                if ($image = $application->contact->image) {
                    $repositories->refresh($image);
                }
            }
        } 
        
        $container->setEntity($application);
        $this->container = $container;
    }
    
    public function jobNotFoundAction()
    {
        $this->response->setStatusCode(410);
        $model = new ViewModel(array(
            'content' => /*@translate*/ 'Invalid apply id'
        ));
        $model->setTemplate('auth/index/job-not-found.phtml');
        return $model;
    }
    
    public function indexAction()
    {
        $form        = $this->container;
        $application = $form->getEntity();
        
        $this->container->setParam('applicationId', $application->id);

        return array(
            'form' => $form,
            'isApplicationValid' => $this->checkApplication($application),
            'application' => $application,
        );
    }
    
    public function processAction()
    {
        $formName  = $this->params()->fromQuery('form');
        $form      = $this->container->getForm($formName);
        $postData  = $form->getOption('use_post_array') ? $_POST : array();
        $filesData = $form->getOption('use_files_array') ? $_FILES : array();
        $data      = array_merge($postData, $filesData);

        $form->setData($data);
        
        if (!$form->isValid()) {
            return new JsonModel(array(
                'valid' => false,
                'errors' => $form->getMessages(),
            ));
        }
        $application = $this->container->getEntity();
        $this->getServiceLocator()->get('repositories')->store($application);
        
        if ('file-uri' === $this->params()->fromPost('return')) {
            $content = $form->getHydrator()->getLastUploadedFile()->getUri();
        } else {
            if ($form instanceOf SummaryForm) {
                $form->setRenderMode(SummaryForm::RENDER_SUMMARY);
                $viewHelper = 'summaryform';
            } else {
                $viewHelper = 'form';
            }
            $content = $this->getServiceLocator()->get('ViewHelperManager')->get($viewHelper)->__invoke($form);
        }
        
        return new JsonModel(array(
            'valid' => $form->isValid(),
            'content' => $content,
            'isApplicationValid' => $this->checkApplication($application)
        ));
    }
    
    public function doAction()
    {
        $services     = $this->getServiceLocator();
        $repositories = $services->get('repositories');
        $repository   = $repositories->get('Applications/Application');
        $application  = $repository->findDraft(
                            $this->auth()->getUser(),
                            $this->params('applyId')
                        );<commit_msg>[Applications] Fixes: file upload returns uri without basepath.<commit_after><?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright (c) 2013-2014 Cross Solution (http://cross-solution.de)
 * @license   AGPLv3
 */

/** Applications controllers */ 
namespace Applications\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
use Applications\Entity\Application;
use Zend\View\Model\ViewModel;
use Auth\Entity\AnonymousUser;
use Zend\View\Model\JsonModel;
use Core\Form\Container;
use Core\Form\SummaryForm;
use Core\Entity\PermissionsInterface;
use Applications\Entity\Status;
use Applications\Entity\StatusInterface;

/**
 *
 *
 * @author <NAME> <<EMAIL>>
 */
class ApplyController extends AbstractActionController
{
    
    protected $container;
    
    public function attachDefaultListeners()
    {
        parent::attachDefaultListeners();
        $events = $this->getEventManager();
        $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 10);
    }
    
    public function preDispatch(MvcEvent $e)
    {
        if ($this->params()->fromQuery('do')) {
            $e->getRouteMatch()->setParam('action', 'do');
            return;
        }
        
        $request      = $this->getRequest();
        $services     = $this->getServiceLocator();
        $repositories = $services->get('repositories');
        $repository   = $repositories->get('Applications/Application');
        $container    = $services->get('forms')->get('Applications/Apply');
        
        if ($request->isPost()) {
            $appId = $this->params()->fromPost('applicationId');
            if (!$appId) {
                throw new \RuntimeException('Missing application id.');
            }
            $application = $repository->find($appId);
            if (!$application) {
                throw new \RuntimeException('Invalid application id.');
            }
            $action     = 'process';
            $routeMatch = $e->getRouteMatch();
            $routeMatch->setParam('action', $action);
            
        } else {
            $user  = $this->auth()->getUser();
            $appId = $this->params('applyId');
            if (!$appId) {
                throw new \RuntimeException('Missing apply id');
            }
            $application = $repository->findDraft($user, $appId);
            if ($application) {
               $form = $container->getForm('contact.contact');
               $form->setDisplayMode('summary');
            } else {
                $job = $repositories->get('Jobs/Job')->findOneByApplyId($appId);
                
                if (!$job) {
                    $e->getRouteMatch()->setParam('action', 'job-not-found');
                    return;
                }
                
                $application = $repository->create();
                $application->setIsDraft(true)
                            ->setContact($user->info)
                            ->setUser($user)
                            ->setJob($job);
                $repositories->store($application);
                /*
                 * If we had copy an user image, we need to refresh its data
                 * to populate the length property.
                 */
                if ($image = $application->contact->image) {
                    $repositories->refresh($image);
                }
            }
        } 
        
        $container->setEntity($application);
        $this->container = $container;
    }
    
    public function jobNotFoundAction()
    {
        $this->response->setStatusCode(410);
        $model = new ViewModel(array(
            'content' => /*@translate*/ 'Invalid apply id'
        ));
        $model->setTemplate('auth/index/job-not-found.phtml');
        return $model;
    }
    
    public function indexAction()
    {
        $form        = $this->container;
        $application = $form->getEntity();
        
        $this->container->setParam('applicationId', $application->id);

        return array(
            'form' => $form,
            'isApplicationValid' => $this->checkApplication($application),
            'application' => $application,
        );
    }
    
    public function processAction()
    {
        $formName  = $this->params()->fromQuery('form');
        $form      = $this->container->getForm($formName);
        $postData  = $form->getOption('use_post_array') ? $_POST : array();
        $filesData = $form->getOption('use_files_array') ? $_FILES : array();
        $data      = array_merge($postData, $filesData);

        $form->setData($data);
        
        if (!$form->isValid()) {
            return new JsonModel(array(
                'valid' => false,
                'errors' => $form->getMessages(),
            ));
        }
        $application = $this->container->getEntity();
        $this->getServiceLocator()->get('repositories')->store($application);
        
        if ('file-uri' === $this->params()->fromPost('return')) {
            $basepath = $this->getServiceLocator()->get('ViewHelperManager')->get('basepath');
            $content = $basepath($form->getHydrator()->getLastUploadedFile()->getUri());
        } else {
            if ($form instanceOf SummaryForm) {
                $form->setRenderMode(SummaryForm::RENDER_SUMMARY);
                $viewHelper = 'summaryform';
            } else {
                $viewHelper = 'form';
            }
            $content = $this->getServiceLocator()->get('ViewHelperManager')->get($viewHelper)->__invoke($form);
        }
        
        return new JsonModel(array(
            'valid' => $form->isValid(),
            'content' => $content,
            'isApplicationValid' => $this->checkApplication($application)
        ));
    }
    
    public function doAction()
    {
        $services     = $this->getServiceLocator();
        $repositories = $services->get('repositories');
        $repository   = $repositories->get('Applications/Application');
        $application  = $repository->findDraft(
                            $this->auth()->getUser(),
                            $this->params('applyId')
                        );
{'fraction_non_alphanumeric': 0.14902186421173763, 'fraction_numerical': 0.0023014959723820483, 'mean_word_length': 1.844750993687164, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 194, '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': '3751043', 'n_tokens_mistral': 3220, 'n_tokens_neox': 3123, 'n_words': 537}