MediasController::indexAction()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 5 Features 0
Metric Value
c 6
b 5
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 17
nc 4
nop 4
1
<?php
2
namespace Mykees\MediaBundle\Controller;
3
4
use Mykees\MediaBundle\Entity\Media;
5
use Mykees\MediaBundle\Event\MediaUploadEvents;
6
use Mykees\MediaBundle\Event\UploadEvent;
7
use Mykees\MediaBundle\Form\Type\MediaShowType;
8
use Mykees\MediaBundle\Form\Type\MediaType;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class MediasController extends Controller
14
{
15
16
    private function getManage()
17
    {
18
        return $this->getDoctrine()->getManager();
19
    }
20
21
    /**
22
     * Index Media List
23
     * @param $model
24
     * @param $bundle
25
     * @param $model_id
26
     * @param $editor
27
     * @return \Symfony\Component\HttpFoundation\Response
28
     */
29
    public function indexAction( $model, $bundle, $model_id, $editor )
30
    {
31
        $params = [
32
            'medias'=>$this->get('mk.media.manager')->findMediasByModelAndId($model, $model_id),
33
            'entity'=>$this->getManage()->getRepository("$bundle:$model")->find($model_id),
34
            'mode'=>$editor=='true' ? $editor : null,
35
            'url'=>$editor == "true" ? ['model'=>$model,'bundle'=>$bundle,'model_id'=>$model_id,'mode'=>'true'] : ['model'=>$model,'bundle'=>$bundle,'model_id'=>$model_id]
36
        ];
37
        $form = $this->createForm(
38
            new MediaType(),
39
            new Media,
40
            [
41
                'action' => $this->generateUrl('mykees_media_add',$params['url']),
42
                'method' => 'POST',
43
            ]
44
        );
45
46
        return $this->render('MykeesMediaBundle:Media:index.html.twig',[
47
            'form'=>$form->createView(),
48
            'model'=>$model,
49
            'bundle'=> $bundle,
50
            'model_id'=> $model_id,
51
            'params'=>$params
52
        ]);
53
    }
54
55
    /**
56
     * Save And Upload Media (Ajax)
57
     * @param Request $request
58
     * @throws \Exception
59
     * @return \Symfony\Component\HttpFoundation\Response
60
     */
61
    public function addAction( Request $request )
62
    {
63
        $requestArray = [
64
            'file'=>$request->files,
65
            'mode'=>$request->get('mode')
66
        ];
67
        $model = $request->get('model');
68
        $bundle = $request->get('bundle');
69
        $model_id = $request->get('model_id');
70
71
        if( $request->isXmlHttpRequest() && $requestArray['file'] )
72
        {
73
            //Init Event
74
            $event = $this->initEvent($requestArray['file'],$model,$model_id);
75
76
            if($event->getMedia())
77
            {
78
                $requestArray['entity'] = $this->getManage()->getRepository("$bundle:$model")->find($model_id);
79
80
81
                return $this->render('MykeesMediaBundle:Media:upload/upload_list.html.twig',[
82
                    'params'=>$requestArray,
83
                    'media'=>$event->getMedia(),
84
                    'model'=>$model,
85
                    'bundle'=>$bundle,
86
                    'model_id'=>$model_id
87
                ]);
88
            }else{
89
                $response = new Response();
90
                $response->setContent(json_encode(array(
91
                    'error'=>"Le format n'est pas valid"
92
                )));
93
                $response->headers->set('Content-Type', 'application/json');
94
                $response->setStatusCode(500);
95
                return $response;
96
            }
97
        }
98
    }
99
100
    private function initEvent($file,$model,$model_id)
101
    {
102
        $event = new UploadEvent();
103
        $event->setFile($file);
104
        $event->setMediableModel($model);
105
        $event->setMediableId($model_id);
106
        $event->setContainer($this->container);
107
        $event->setRootDir($this->get('kernel')->getRootDir());
108
        //File upload process
109
        $this->get("event_dispatcher")->dispatch(MediaUploadEvents::UPLOAD_FILE, $event);
110
111
        return $event;
112
    }
113
114
    /**
115
     * Add Thumb to an entity
116
     * @param $model
117
     * @param $bundle
118
     * @param $model_id
119
     * @param $id
120
     * @param Request $request
121
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
122
     */
123
    public function thumbAction( $model, $bundle, $model_id, $id, Request $request )
124
    {
125
        $media  = $this->getManage()->getRepository("MykeesMediaBundle:Media")->find($id);
126
        $entity  = $this->getManage()->getRepository("$bundle:$model")->find($model_id);
127
        $entity->setThumb($media);
128
        $this->getManage()->persist($entity);
129
        $this->getManage()->flush();
130
131
        return $this->referer($request);
132
    }
133
134
    /**
135
     * Show a medias for add in the textarea
136
     * @param null $model
137
     * @param null $id
138
     * @param Request $request
139
     * @return Response
140
     */
141
    public function showAction( $model=null, $id = null, Request $request )
142
    {
143
        if( !$id )
144
        {
145
            $params = [
146
                'class'=>$request->get('class'),
147
                'alt'=>$request->get('alt')
148
            ];
149
            $params['media'] = $this->getManage()->getRepository('MykeesMediaBundle:Media')->findOneBy(['name'=>$params['alt'],'model'=>$model]);
150
        }else{
151
            $params = [
152
                'media'=>$this->getManage()->getRepository('MykeesMediaBundle:Media')->find($id),
153
                'class'=>null
154
            ];
155
        }
156
        $form = $this->createForm(
157
            new MediaShowType(),
158
            $params['media'],
159
            [
160
                'action' => $this->generateUrl('mykees_media_show',['model'=>$model,'id'=>$id]),
161
                'method' => 'POST',
162
            ]
163
        );
164
        if($request->getMethod() == "POST")
165
        {
166
            $media = $request->request->all();
167
            return $this->render('MykeesMediaBundle:Media:tinymce.html.twig',['media'=>$media]);
168
        }
169
170
        return $this->render('MykeesMediaBundle:Media:show/show.html.twig',[
171
            'media'=>$params['media'],'form'=>$form->createView(),'class'=>$params['class'],"model"=>$model
172
        ]);
173
    }
174
175
176
    /**
177
     * Delete a media (Ajax)
178
     * @param $model
179
     * @param $bundle
180
     * @param $id
181
     * @return Response
182
     */
183
    public function deleteAction( $model, $bundle, $id )
184
    {
185
        if( $id )
186
        {
187
            $media = $this->getManage()->getRepository('MykeesMediaBundle:Media')->find($id);
188
            $media_manager = $this->get('mk.media.manager');
189
            $media_manager->unlink($model,$media);
190
191
            $model_referer = $this->getManage()->getRepository("$bundle:$model")->find($media->getMediableId());
192
            if(method_exists($model_referer,'getThumb') && $model_referer->getThumb() !== null && $model_referer->getThumb()->getId() == $media->getId())
193
            {
194
                $model_referer->setThumb(null);
195
            }
196
197
            $media_manager->remove($media);
198
        }
199
200
        return new Response();
201
    }
202
203
    /**
204
     * Referer
205
     * @param Request $request
206
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
207
     */
208
    private function referer(Request $request)
209
    {
210
        $referer = $request->headers->get('referer');
211
212
        return $this->redirect($referer);
213
    }
214
}
215