1 | <?php |
||
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() |
||
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) |
||
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) |
||
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) |
||
90 | |||
91 | /** |
||
92 | * Get query for model name |
||
93 | * @param $model_name |
||
94 | */ |
||
95 | public function getQueryByModel($model_name) |
||
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 ) |
||
182 | |||
183 | /** |
||
184 | * Remodel ALL Medias |
||
185 | */ |
||
186 | public function removeAll(){} |
||
187 | |||
188 | } |
||
189 |