1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Rafidion Michael |
5
|
|
|
* Date: 30/11/2014 |
6
|
|
|
* Time: 03:40 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Mykees\MediaBundle\Manager; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
12
|
|
|
use Mykees\MediaBundle\Entity\Media; |
13
|
|
|
use Mykees\MediaBundle\Interfaces\Mediable; |
14
|
|
|
use Mykees\MediaBundle\Util\Reflection; |
15
|
|
|
|
16
|
|
|
class MediaManager extends AbstractManager |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public $em; |
20
|
|
|
public $rootDir; |
21
|
|
|
public $resize_parameters; |
22
|
|
|
|
23
|
|
|
public function __construct( ManagerRegistry $managerRegistry, $rootDir,$resize_parameters ) |
24
|
|
|
{ |
25
|
|
|
$this->em = $managerRegistry->getManager(); |
26
|
|
|
$this->rootDir = $rootDir; |
27
|
|
|
$this->resize_parameters = $resize_parameters; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function webroot() |
32
|
|
|
{ |
33
|
|
|
return $this->rootDir . '/../web/img/'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Find medias by model name and model id |
38
|
|
|
* @param $model_name |
39
|
|
|
* @param $model_id |
40
|
|
|
*/ |
41
|
|
|
public function findMediasByModelAndId($model_name, $model_id) |
42
|
|
|
{ |
43
|
|
|
return $this->em->getRepository('MykeesMediaBundle:Media')->findForModelAndId($model_name, $model_id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Find medias for an array of objects |
48
|
|
|
* @param array $models |
49
|
|
|
*/ |
50
|
|
|
public function findMediasByArrayObject(array $models) |
51
|
|
|
{ |
52
|
|
|
$model_info = $this->getModelInfos($models); |
53
|
|
|
$queryResult = $this->em->getRepository("MykeesMediaBundle:Media")->findForArrayModels($model_info); |
54
|
|
|
|
55
|
|
|
return $this->refreshMediasArray($queryResult,$models); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Find all medias by model name |
60
|
|
|
* @param $model_name |
61
|
|
|
*/ |
62
|
|
|
public function findMediasByModel($model_name) |
63
|
|
|
{ |
64
|
|
|
return $this->em->getRepository('MykeesMediaBundle:Media')->findForModel($model_name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Find all medias for Mediable object |
69
|
|
|
* @param Mediable $obj |
70
|
|
|
*/ |
71
|
|
|
public function findMedias(Mediable $obj) |
72
|
|
|
{ |
73
|
|
|
$model = Reflection::getClassShortName($obj); |
74
|
|
|
$model_id = $obj->getId(); |
75
|
|
|
|
76
|
|
|
$queryResult = $this->em->getRepository('MykeesMediaBundle:Media')->findForModelAndId($model,$model_id); |
77
|
|
|
|
78
|
|
|
return $this->refreshMedias($queryResult,$obj); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get query for model name and model id |
83
|
|
|
* @param $model_name |
84
|
|
|
* @param $model_id |
85
|
|
|
*/ |
86
|
|
|
public function getQueryByModelAndId($model_name, $model_id) |
87
|
|
|
{ |
88
|
|
|
return $this->em->getRepository('MykeesMediaBundle:Media')->queryForModelAndId($model_name,$model_id); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get query for model name |
93
|
|
|
* @param $model_name |
94
|
|
|
*/ |
95
|
|
|
public function getQueryByModel($model_name) |
96
|
|
|
{ |
97
|
|
|
return $this->em->getRepository('MykeesMediaBundle:Media')->queryForModel($model_name); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get query for Mediable object |
102
|
|
|
* @param Mediable $obj |
103
|
|
|
*/ |
104
|
|
|
public function getQuery(Mediable $obj) |
105
|
|
|
{ |
106
|
|
|
$model = Reflection::getClassShortName($obj); |
107
|
|
|
$model_id = $obj->getId(); |
108
|
|
|
|
109
|
|
|
return $this->em->getRepository('MykeesMediaBundle:Media')->queryForModelAndId($model,$model_id); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function unlink($model, Media $media) |
113
|
|
|
{ |
114
|
|
|
$info = pathinfo($media->getFile()); |
115
|
|
|
|
116
|
|
|
$this->removeSizes($model,$info); |
117
|
|
|
|
118
|
|
|
if(file_exists($this->webroot() . $media->getFile())) |
119
|
|
|
{ |
120
|
|
|
return unlink($this->webroot() . $media->getFile()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function removeSizes($model,$info) |
127
|
|
|
{ |
128
|
|
|
if(!empty($this->resize_parameters[$model])) |
129
|
|
|
{ |
130
|
|
|
if(!empty($this->resize_parameters[$model]['size'])) |
131
|
|
|
{ |
132
|
|
|
$sizes = $this->resize_parameters[$model]['size']; |
133
|
|
|
|
134
|
|
|
foreach($sizes as $k=>$size) |
135
|
|
|
{ |
136
|
|
|
$w = $size['width']; |
137
|
|
|
$h = $size['height']; |
138
|
|
|
$resizedFile = $this->webroot() . $info['dirname'] . '/' . $info['filename'] . "_$w" . "x$h" . '.jpg'; |
139
|
|
|
|
140
|
|
|
if(file_exists($resizedFile)) |
141
|
|
|
{ |
142
|
|
|
unlink($resizedFile); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Media $media |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function remove(Media $media) |
154
|
|
|
{ |
155
|
|
|
$this->em->remove($media); |
156
|
|
|
$this->em->flush(); |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Mediable $model |
162
|
|
|
*/ |
163
|
|
|
public function removeAllMediasForModel( Mediable $model ) |
164
|
|
|
{ |
165
|
|
|
$model_name = Reflection::getClassShortName($model); |
166
|
|
|
$model_id = $model->getId(); |
167
|
|
|
|
168
|
|
|
if(method_exists($model,'getThumb') && $model->getThumb() !== null) |
169
|
|
|
{ |
170
|
|
|
$model->setThumb(null); |
171
|
|
|
} |
172
|
|
|
$medias = $this->em->getRepository('MykeesMediaBundle:Media')->findBy([ |
173
|
|
|
'model'=>$model_name, |
174
|
|
|
'modelId'=>$model_id |
175
|
|
|
]); |
176
|
|
|
foreach($medias as $k=>$media) |
177
|
|
|
{ |
178
|
|
|
$this->unlink($model_name, $media); |
179
|
|
|
$this->remove($media); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Remodel ALL Medias |
185
|
|
|
*/ |
186
|
|
|
public function removeAll(){} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|