~tsep-dev/tsep/tsep2

« back to all changes in this revision

Viewing changes to src/TSEP/Bundle/AdminBundle/Controller/ProfileController.php

  • Committer: xaav
  • Date: 2011-09-27 01:31:36 UTC
  • Revision ID: git-v1:3c3f2e8d21ccd506f3cd12b2650591f6532368fb
First commit'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace TSEP\Bundle\AdminBundle\Controller;
 
4
 
 
5
use Xaav\QueueBundle\JobQueue\Provider\FlatFileProvider;
 
6
 
 
7
use Symfony\Component\HttpFoundation\RedirectResponse;
 
8
use Symfony\Component\HttpFoundation\Response;
 
9
use TSEP\Bundle\AdminBundle\Job\ProcessIndexingRequestJob;
 
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
 
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 
14
use TSEP\Bundle\SearchBundle\Entity\Profile;
 
15
use TSEP\Bundle\AdminBundle\Form\ProfileType;
 
16
 
 
17
/**
 
18
 * Profile controller.
 
19
 *
 
20
 * @Route("/admin/profile")
 
21
 */
 
22
class ProfileController extends Controller
 
23
{
 
24
    /**
 
25
     * Lists all Profile entities.
 
26
     *
 
27
     * @Route("/", name="admin_profile")
 
28
     * @Template()
 
29
     */
 
30
    public function indexAction()
 
31
    {
 
32
        $em = $this->getDoctrine()->getEntityManager();
 
33
 
 
34
        $entities = $em->getRepository('TSEPSearchBundle:Profile')->findAll();
 
35
 
 
36
        return array('entities' => $entities);
 
37
    }
 
38
 
 
39
    /**
 
40
     * Finds and displays a Profile entity.
 
41
     *
 
42
     * @Route("/{id}/show", name="admin_profile_show")
 
43
     * @Template()
 
44
     */
 
45
    public function showAction($id)
 
46
    {
 
47
        $em = $this->getDoctrine()->getEntityManager();
 
48
 
 
49
        $entity = $em->getRepository('TSEPSearchBundle:Profile')->find($id);
 
50
 
 
51
        if (!$entity) {
 
52
            throw $this->createNotFoundException('Unable to find Profile entity.');
 
53
        }
 
54
 
 
55
        $deleteForm = $this->createDeleteForm($id);
 
56
 
 
57
        return array(
 
58
            'entity'      => $entity,
 
59
            'delete_form' => $deleteForm->createView(),
 
60
        );
 
61
    }
 
62
 
 
63
    /**
 
64
     * Displays a form to create a new Profile entity.
 
65
     *
 
66
     * @Route("/new", name="admin_profile_new")
 
67
     * @Template()
 
68
     */
 
69
    public function newAction()
 
70
    {
 
71
        $entity = new Profile();
 
72
        $form   = $this->createForm(new ProfileType(), $entity);
 
73
 
 
74
        return array(
 
75
            'entity' => $entity,
 
76
            'form'   => $form->createView()
 
77
        );
 
78
    }
 
79
 
 
80
    /**
 
81
     * Creates a new Profile entity.
 
82
     *
 
83
     * @Route("/create", name="admin_profile_create")
 
84
     * @Method("post")
 
85
     * @Template("TSEPAdminBundle:Profile:new.html.twig")
 
86
     */
 
87
    public function createAction()
 
88
    {
 
89
        $entity  = new Profile();
 
90
        $request = $this->getRequest();
 
91
        $form    = $this->createForm(new ProfileType(), $entity);
 
92
 
 
93
        if ('POST' === $request->getMethod()) {
 
94
            $form->bindRequest($request);
 
95
 
 
96
            if ($form->isValid()) {
 
97
                $em = $this->getDoctrine()->getEntityManager();
 
98
                $em->persist($entity);
 
99
                $em->flush();
 
100
 
 
101
                return $this->redirect($this->generateUrl('admin_profile_show', array('id' => $entity->getId())));
 
102
 
 
103
            }
 
104
        }
 
105
 
 
106
        return array(
 
107
            'entity' => $entity,
 
108
            'form'   => $form->createView()
 
109
        );
 
110
    }
 
111
 
 
112
    /**
 
113
     * Displays a form to edit an existing Profile entity.
 
114
     *
 
115
     * @Route("/{id}/edit", name="admin_profile_edit")
 
116
     * @Template()
 
117
     */
 
118
    public function editAction($id)
 
119
    {
 
120
        $em = $this->getDoctrine()->getEntityManager();
 
121
 
 
122
        $entity = $em->getRepository('TSEPSearchBundle:Profile')->find($id);
 
123
 
 
124
        if (!$entity) {
 
125
            throw $this->createNotFoundException('Unable to find Profile entity.');
 
126
        }
 
127
 
 
128
        $editForm = $this->createForm(new ProfileType(), $entity);
 
129
        $deleteForm = $this->createDeleteForm($id);
 
130
 
 
131
        return array(
 
132
            'entity'      => $entity,
 
133
            'edit_form'   => $editForm->createView(),
 
134
            'delete_form' => $deleteForm->createView(),
 
135
        );
 
136
    }
 
137
 
 
138
    /**
 
139
     * Edits an existing Profile entity.
 
140
     *
 
141
     * @Route("/{id}/update", name="admin_profile_update")
 
142
     * @Method("post")
 
143
     * @Template("TSEPAdminBundle:Profile:edit.html.twig")
 
144
     */
 
145
    public function updateAction($id)
 
146
    {
 
147
        $em = $this->getDoctrine()->getEntityManager();
 
148
 
 
149
        $entity = $em->getRepository('TSEPSearchBundle:Profile')->find($id);
 
150
 
 
151
        if (!$entity) {
 
152
            throw $this->createNotFoundException('Unable to find Profile entity.');
 
153
        }
 
154
 
 
155
        $editForm   = $this->createForm(new ProfileType(), $entity);
 
156
        $deleteForm = $this->createDeleteForm($id);
 
157
 
 
158
        $request = $this->getRequest();
 
159
 
 
160
        if ('POST' === $request->getMethod()) {
 
161
            $editForm->bindRequest($request);
 
162
 
 
163
            if ($editForm->isValid()) {
 
164
                $em = $this->getDoctrine()->getEntityManager();
 
165
                $em->persist($entity);
 
166
                $em->flush();
 
167
 
 
168
                return $this->redirect($this->generateUrl('admin_profile_edit', array('id' => $id)));
 
169
            }
 
170
        }
 
171
 
 
172
        return array(
 
173
            'entity'      => $entity,
 
174
            'edit_form'   => $editForm->createView(),
 
175
            'delete_form' => $deleteForm->createView(),
 
176
        );
 
177
    }
 
178
 
 
179
    /**
 
180
     * Deletes a Profile entity.
 
181
     *
 
182
     * @Route("/{id}/delete", name="admin_profile_delete")
 
183
     * @Method("post")
 
184
     */
 
185
    public function deleteAction($id)
 
186
    {
 
187
        $form = $this->createDeleteForm($id);
 
188
        $request = $this->getRequest();
 
189
 
 
190
        if ('POST' === $request->getMethod()) {
 
191
            $form->bindRequest($request);
 
192
 
 
193
            if ($form->isValid()) {
 
194
                $em = $this->getDoctrine()->getEntityManager();
 
195
                $entity = $em->getRepository('TSEPSearchBundle:Profile')->find($id);
 
196
 
 
197
                if (!$entity) {
 
198
                    throw $this->createNotFoundException('Unable to find Profile entity.');
 
199
                }
 
200
 
 
201
                $em->remove($entity);
 
202
                $em->flush();
 
203
            }
 
204
        }
 
205
 
 
206
        return $this->redirect($this->generateUrl('admin_profile'));
 
207
    }
 
208
 
 
209
    private function createDeleteForm($id)
 
210
    {
 
211
        return $this->createFormBuilder(array('id' => $id))
 
212
            ->add('id', 'hidden')
 
213
            ->getForm()
 
214
        ;
 
215
    }
 
216
 
 
217
    /**
 
218
     * @Route("/{id}/index", name="admin_profile_startIndexer")
 
219
     */
 
220
    public function startIndexerAction($id)
 
221
    {
 
222
        $em = $this->getDoctrine()->getEntityManager();
 
223
        $profile = $em->getRepository('TSEPSearchBundle:Profile')->findOneById($id);
 
224
        $provider = new FlatFileProvider($this->get('kernel')->getCacheDir());
 
225
        $queue = $provider->getJobQueueByName('indexingRequests');
 
226
        $this->get('logger')->debug($profile);
 
227
        $queue->addJobToQueue(new ProcessIndexingRequestJob($profile));
 
228
 
 
229
        return new RedirectResponse($this->generateUrl('admin_processQueue', array('queue' => 'indexingRequests')));
 
230
    }
 
231
}