~tsep-dev/tsep/tsep2

« back to all changes in this revision

Viewing changes to src/TSEP/Packagist/WebBundle/Controller/WebController.php

  • Committer: xaav
  • Date: 2011-10-01 18:10:50 UTC
  • Revision ID: git-v1:e1357bf55d2d528a58efeead2a02193370e17823
CS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/*
4
 
 * This file is part of Packagist.
5
 
 *
6
 
 * (c) Jordi Boggiano <j.boggiano@seld.be>
7
 
 *     Nils Adermann <naderman@naderman.de>
8
 
 *
9
 
 * For the full copyright and license information, please view the LICENSE
10
 
 * file that was distributed with this source code.
11
 
 */
12
 
 
13
 
namespace Packagist\WebBundle\Controller;
14
 
 
15
 
use Packagist\WebBundle\Form\ConfirmPackageType;
16
 
use Packagist\WebBundle\Form\ConfirmForm;
17
 
use Packagist\WebBundle\Form\ConfirmFormType;
18
 
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
 
use Packagist\WebBundle\Entity\Package;
20
 
use Packagist\WebBundle\Entity\Version;
21
 
use Packagist\WebBundle\Form\PackageType;
22
 
use Packagist\WebBundle\Form\VersionType;
23
 
use Symfony\Component\HttpFoundation\RedirectResponse;
24
 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
26
 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
 
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
28
 
 
29
 
/**
30
 
 * @author Jordi Boggiano <j.boggiano@seld.be>
31
 
 */
32
 
class WebController extends Controller
33
 
{
34
 
    protected function getUser()
35
 
    {
36
 
        return $user = $this->get('security.context')->getToken()->getUser();
37
 
    }
38
 
 
39
 
    /**
40
 
     * @Template()
41
 
     * @Route("/", name="home")
42
 
     */
43
 
    public function indexAction()
44
 
    {
45
 
        $packages = $this->get('doctrine')
46
 
            ->getRepository('Packagist\WebBundle\Entity\Package')
47
 
            ->findAll();
48
 
 
49
 
        return array('packages' => $packages, 'page' => 'home');
50
 
    }
51
 
 
52
 
    /**
53
 
     * @Template()
54
 
     * @Route("/submit", name="submit")
55
 
     */
56
 
    public function submitPackageAction()
57
 
    {
58
 
        $package = new Package;
59
 
        $form = $this->createForm(new PackageType, $package);
60
 
 
61
 
        $request = $this->getRequest();
62
 
        if ($request->getMethod() == 'POST') {
63
 
            $form->bindRequest($request);
64
 
            $children = $form->getChildren();
65
 
            if($children['repository']->isValid()) {
66
 
                $this->get('session')->set('repository', $package->getRepository());
67
 
 
68
 
                return new RedirectResponse($this->generateUrl('confirm'));
69
 
            }
70
 
        }
71
 
 
72
 
        return array('form' => $form->createView(), 'page' => 'submit');
73
 
    }
74
 
 
75
 
    /**
76
 
     * @Template()
77
 
     * @Route("/submit/confirm", name="confirm")
78
 
     */
79
 
    public function confirmPackageAction()
80
 
    {
81
 
        $session = $this->get('session');
82
 
        $em = $this->getDoctrine()->getEntityManager();
83
 
        $package = new Package;
84
 
 
85
 
        if($repository = $session->get('repository')) {
86
 
            $session->remove('repository');
87
 
            $package->setRepository($repository);
88
 
            $package->fromProvider($this->get('packagist.repository_provider'));
89
 
        }
90
 
 
91
 
        $form = $this->createForm(new ConfirmPackageType, $package);
92
 
 
93
 
        $request = $this->getRequest();
94
 
        if ($request->getMethod() == 'POST') {
95
 
            $form->bindRequest($request);
96
 
            $package->fromProvider($this->get('packagist.repository_provider'));
97
 
 
98
 
            $children = $form->getChildren();
99
 
            if ($children['repository']->isValid()) {
100
 
                $user = $this->getUser();
101
 
                $package->addMaintainers($user);
102
 
 
103
 
                $em = $this->getDoctrine()->getEntityManager();
104
 
                $em->persist($package);
105
 
                $em->flush();
106
 
 
107
 
                $this->get('session')->remove('repository');
108
 
 
109
 
                return new RedirectResponse($this->generateUrl('home'));
110
 
            }
111
 
        } elseif (!$repository) {
112
 
            return new RedirectResponse($this->generateUrl('submit'));
113
 
        }
114
 
 
115
 
        return array('form' => $form->createView(), 'package' => $package, 'page' => 'submit');
116
 
    }
117
 
 
118
 
    /**
119
 
     * @Template()
120
 
     * @Route("/about", name="about")
121
 
     */
122
 
    public function aboutAction()
123
 
    {
124
 
        return array();
125
 
    }
126
 
}