/
File.php
364 lines (321 loc) · 12.4 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* RokGallery_Model_File
*
* This class has been auto-generated by the Doctrine ORM Framework
*
* @package RokGallery
* @subpackage models
* @author RocketTheme LLC <support@rockettheme.com>
* @version SVN: $Id: File.php 39574 2011-07-06 10:05:24Z btowles $
*/
class RokGallery_Model_File extends RokGallery_Model_Base_File
{
/**
* @param Doctrine_Event $event
*/
public function preDelete(Doctrine_Event $event)
{
// Queue the directory for deletion
RokGallery_Queue_DirectoryDelete::add($this->getDirectoryPath());
}
/**
* @param Doctrine_Event $event
*/
public function preUpdate(Doctrine_Event $event)
{
$record = $event->getInvoker();
$modified = $record->getModified();
$original = $record->getModified(true);
foreach ($this->Slices as &$slice)
{
if (array_key_exists('title', $modified)) {
if ($slice->title == $original['title'] || $slice->title == null) {
$slice->title = $modified['title'];
}
}
if (array_key_exists('slug', $modified)) {
if ($slice->slug == $original['slug'] || $slice->slug == null) {
$slice->slug = $modified['slug'];
}
}
if (array_key_exists('description', $modified)) {
if ($slice->caption == $original['description'] || $slice->caption == null) {
$slice->caption = $modified['description'];
}
}
if (array_key_exists('published', $modified)
&& $modified['published'] == false
&& $original['published'] == true
) {
$slice->published = false;
}
if (array_key_exists('published', $modified)
&& $modified['published'] == true
&& $original['published'] == false
&& RokGallery_Config::getOption(RokGallery_Config::OPTION_SLICE_AUTOPUBLISH_ON_FILE_PUBLISH, false)
) {
$slice->published = true;
}
}
if (array_key_exists('manipulations', $modified)) {
$this->processImages();
}
}
public function postDelete(Doctrine_Event $event)
{
//Make sure all linked tables are clean
//RokGallery_Model_FileTags
$q = Doctrine_Query::create()
->delete('RokGallery_Model_FileTags ft')
->andWhere('ft.file_id NOT IN (SELECT f.id from RokGallery_Model_File f)');
$q->execute();
$q->free();
//RokGallery_Model_FileViews
$q = Doctrine_Query::create()
->delete('RokGallery_Model_FileViews fv')
->andWhere('fv.file_id NOT IN (SELECT f.id from RokGallery_Model_File f)');
$q->execute();
$q->free();
//RokGallery_Model_FileLoves
$q = Doctrine_Query::create()
->delete('RokGallery_Model_FileLoves fl')
->andWhere('fl.file_id NOT IN (SELECT f.id from RokGallery_Model_File f)');
$q->execute();
$q->free();
//rokgallery_files_index
$conn =& Doctrine_Manager::connection();
$dbh =& $conn->getDbh();
$stmt = $dbh->prepare('delete from ' . RokCommon_Doctrine::getPlatformInstance()->setTableName('rokgallery_files_index')
. ' where id NOT IN (SELECT f.id from ' . RokGallery_Model_FileTable::getInstance()->getTableName() . ' f)');
$stmt->execute();
//RokGallery_Model_Slice
$q = Doctrine_Query::create()
->delete('RokGallery_Model_Slice s')
->andWhere('s.file_id NOT IN (SELECT f.id from RokGallery_Model_File f)');
$q->execute();
$q->free();
}
/**
* @param $filename
* @param $path
* @param null $title
* @param null $description
* @return RokGallery_Model_File
*/
public static function &createNew($filename, $path, $title = null, $description = null)
{
$file = new RokGallery_Model_File();
$file->filename = $filename;
$file->guid = RokGallery_Helper::createUUID();
if (!(file_exists($path) && is_readable($path)))
throw new RokGallery_Exception(rc__('ROKGALLERY_UNABLE_TO_GET_MD5_OF_FILE_N', $path));
$file->md5 = md5_file($path);
if (empty($title)) {
$title = @pathinfo($filename, PATHINFO_FILENAME);
$title = str_replace('_', ' ', $title);
}
$file->title = $title;
$file->filesize = @filesize($path);
$file->type = strtolower(@pathinfo($filename, PATHINFO_EXTENSION));
$file->description = $description;
return $file;
}
/**
* @return RokGallery_Model_Slice
*/
public function &getAdminThumbSlice()
{
$thumb_slice = null;
foreach ($this->Slices as &$slice)
{
/** @var RokGallery_Model_Slice $slice */
if ($slice->admin_thumb == true) {
$thumb_slice =& $slice;
break;
}
}
return $thumb_slice;
}
/**
* Returns the current full path to the file
* @return string
*/
public function getFullPath()
{
// Copy file to fine directory
$basepath = $this->getDirectoryPath();
return $basepath . DS . $this->filename;
}
/**
* @return string
*/
public function getDirectoryPath()
{
return RokGallery_Config::getOption(RokGallery_Config::OPTION_ROOT_PATH) . RokGallery_Helper::getPathFromGUID($this->guid, DS);
}
/**
* Returns the relative path to the slice image
*
* @param string $seperator
* @return string
*/
public function getRelativePath($seperator = '/')
{
return RokGallery_Helper::getPathFromGUID($this->guid, $seperator) . $seperator . $this->filename;
}
/**
* Get all slices with the passed tag
* @param $tag
* @return RokGallery_Model_Slice[]
*/
public function getSlicesWithTag($tag)
{
$ret = array();
foreach ($this->Slices as $slice)
{
/** @var RokGallery_Model_Slice $slice */
if ($slice->hasTag($tag)) {
$ret[] = $slice;
}
}
if (empty($ret)) {
$ret = false;
}
return $ret;
}
/**
* Get all slices with the passed tag
* @param $tag
* @return RokGallery_Model_Slice[]
*/
public function getSlicesForGallery($gallery_id)
{
$ret = array();
foreach ($this->Slices as $slice)
{
/** @var RokGallery_Model_Slice $slice */
if ($slice->gallery_id == $gallery_id) {
$ret[] = $slice;
}
}
if (empty($ret)) {
$ret = false;
}
return $ret;
}
/**
* @param RokGallery_Model_Gallery $gallery
*/
public function updateSlicesForGallery(RokGallery_Model_Gallery &$gallery)
{
try
{
$manipulations = array();
if ($gallery->keep_aspect) {
$gallery_aspect_ratio = $gallery->width / $gallery->height;
$image_aspect_ratio = $this->xsize / $this->ysize;
$resize_height = $gallery->height;
$resize_width = $gallery->width;
if ($image_aspect_ratio < $gallery_aspect_ratio) // taller image
{
$resize_height = $gallery->height;
$resize_width = (int)round($gallery->height * $image_aspect_ratio);
}
elseif ($image_aspect_ratio > $gallery_aspect_ratio) // wider image
{
$resize_width = $gallery->width;
$resize_height = (int)round($gallery->width / $image_aspect_ratio);
}
$manipulations[] = new RokGallery_Manipulation_Action_Resize(array('width' => $resize_width, 'height' => $resize_height));
}
else
{
// Create thumbnail but dont make thumbnail keep the aspect ratio
$source_aspect_ratio = $this->xsize / $this->ysize;
$desired_aspect_ratio = $gallery->width / $gallery->height;
$temp_width = $gallery->width;
$temp_height = $gallery->height;
$temp_left = 0;
$temp_top = 0;
if ($source_aspect_ratio > $desired_aspect_ratio) // wider image
{
$temp_height = $gallery->height;
$temp_width = ( int )round($gallery->height * $source_aspect_ratio);
$temp_left = (int)round(($temp_width - $gallery->width) / 2);
$temp_top = 0;
}
elseif ($source_aspect_ratio < $desired_aspect_ratio) // taller image
{
$temp_width = $gallery->width;
$temp_height = ( int )round($gallery->width / $source_aspect_ratio);
$temp_left = 0;
$temp_top = (int)round(($temp_height - $gallery->height) / 2);
}
$manipulations[] = new RokGallery_Manipulation_Action_Resize(array('width' => $temp_width, 'height' => $temp_height));
$manipulations[] = new RokGallery_Manipulation_Action_Crop(array('left' => $temp_left, 'top' => $temp_top, 'width' => $gallery->width, 'height' => $gallery->height));
}
$slices = $this->getSlicesForGallery($gallery->id);
if ($slices === false) {
$slices = array();
$slice = RokGallery_Model_Slice::createNew($this, $this->title, $this->description, $gallery->auto_publish);
$slice->Gallery = $gallery;
$slices[] = $slice;
}
foreach ($slices as &$slice)
{
// skip changing manipulations if force_image_size is set and image size is the same position could have been modified
if (!$gallery->keep_aspect && $gallery->force_image_size && $gallery->width == $slice->xsize && $gallery->height == $slice->ysize) {
$manipulations = $slice->manipulations;
}
if ($slice->Gallery->id == $gallery->id) {
$slice->manipulations = $manipulations;
$slice->save();
if (!file_exists($slice->getFullPath())
|| $gallery->thumb_xsize != $slice->thumb_xsize
|| $gallery->thumb_ysize != $slice->thumb_ysize
|| $gallery->thumb_keep_aspect != $slice->thumb_keep_aspect
|| $gallery->thumb_background != $slice->thumb_background
) {
$slice->thumb_xsize = $gallery->thumb_xsize;
$slice->thumb_ysize = $gallery->thumb_ysize;
$slice->thumb_keep_aspect = $gallery->thumb_keep_aspect;
$slice->thumb_background = $gallery->thumb_background;
$slice->generateThumbnail($gallery->thumb_xsize, $gallery->thumb_ysize, $gallery->thumb_keep_aspect, $gallery->thumb_background);
$slice->save();
}
}
}
}
catch (Exception $e)
{
throw $e;
}
}
/**
*/
public function incrementView()
{
if (null == $this->Views) $this->Views = new RokGallery_Model_FileViews();
$this->Views->count++;
$this->Views->save();
}
/**
*/
public function incrementLoves()
{
if (null == $this->Loves) $this->Loves = new RokGallery_Model_FileLoves();
$this->Loves->count++;
$this->Loves->save();
}
/**
*/
public function decrementLoves()
{
if (null == $this->Loves) $this->Loves = new RokGallery_Model_FileLoves();
if ($this->Loves->count > 0) {
$this->Loves->count--;
$this->Loves->save();
}
}
}