1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.components.image
|
8
|
|
|
*
|
9
|
|
|
* Класс используется для работы с таблицей изображений модели и их миниатюр
|
10
|
|
|
*
|
11
|
|
|
* Examples:
|
12
|
|
|
*
|
13
|
|
|
* $image = ProductImage::model()->findByPk(1);
|
14
|
|
|
* echo $image;
|
15
|
|
|
* echo $image->pre;
|
16
|
|
|
*/
|
17
|
|
|
class FActiveImage extends FActiveRecord implements ImageInterface
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* Изображение, отображаемое при отсутствии файла
|
21
|
|
|
*
|
22
|
|
|
* @var string
|
23
|
|
|
*/
|
24
|
|
|
protected $defaultImage = '/i/sp.gif';
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* Массив доступных префиксов для файла
|
28
|
|
|
*
|
29
|
|
|
* @var array
|
30
|
|
|
*/
|
31
|
|
|
protected $availableTypes = array();
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* Путь к папке с изображениями относительно корня проекта без начального слэша
|
35
|
|
|
*
|
36
|
|
|
* @var string
|
37
|
|
|
*/
|
38
|
|
|
protected $imageDir = 'f/';
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* По умолчанию отдаётся оригинальный файл
|
42
|
|
|
*
|
43
|
|
|
* @return string
|
44
|
|
|
*/
|
45
|
2 |
View Code Duplication |
public function __toString()
|
|
|
|
|
46
|
|
|
{
|
47
|
2 |
|
if( !empty($this->name) && file_exists($this->getFullPath()) )
|
48
|
2 |
|
{
|
49
|
1 |
|
return '/' . $this->getFullPath();
|
50
|
|
|
}
|
51
|
|
|
else
|
52
|
|
|
{
|
53
|
1 |
|
return $this->defaultImage;
|
54
|
|
|
}
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
* Получение неоригинального изображения
|
59
|
|
|
* Допустимые типы находятся в $this->availableTypes
|
60
|
|
|
* Если файл не совпадает с доступными типами, вызывается магия Yii
|
61
|
|
|
* Если файл не существует, отдается defaultImage
|
62
|
|
|
*
|
63
|
|
|
* @param string $name
|
64
|
|
|
*
|
65
|
|
|
* @return string
|
66
|
|
|
* @throws CException
|
67
|
|
|
*/
|
68
|
3 |
View Code Duplication |
public function __get($name)
|
|
|
|
|
69
|
|
|
{
|
70
|
3 |
|
if( in_array($name, $this->availableTypes) )
|
71
|
3 |
|
{
|
72
|
2 |
|
if( file_exists($this->getFullPath($name)) )
|
73
|
2 |
|
{
|
74
|
1 |
|
return '/' . $this->getFullPath($name);
|
75
|
|
|
}
|
76
|
|
|
else
|
77
|
|
|
{
|
78
|
1 |
|
return $this->defaultImage;
|
79
|
|
|
}
|
80
|
|
|
}
|
81
|
|
|
else
|
82
|
3 |
|
return parent::__get($name);
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* Создание пути для файла
|
87
|
|
|
*
|
88
|
|
|
* @return string
|
89
|
|
|
*/
|
90
|
2 |
|
public function getPath()
|
91
|
|
|
{
|
92
|
2 |
|
return $this->imageDir;
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* @param $imageDir
|
97
|
|
|
*/
|
98
|
1 |
|
public function setImageDir($imageDir)
|
99
|
|
|
{
|
100
|
1 |
|
$this->imageDir = $imageDir;
|
101
|
1 |
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* Создание абсолютного пути до файла
|
105
|
|
|
*
|
106
|
|
|
* @param null $name
|
107
|
|
|
*
|
108
|
|
|
* @return string
|
109
|
|
|
*/
|
110
|
|
|
public function getAbsolutePath($name = null)
|
111
|
|
|
{
|
112
|
|
|
return Yii::app()->createAbsoluteUrl($this->getFullPath($name));
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
/**
|
116
|
|
|
* Создание полного пути до файла
|
117
|
|
|
*
|
118
|
|
|
* @param string $name
|
119
|
|
|
*
|
120
|
|
|
* @return string
|
121
|
|
|
*/
|
122
|
2 |
View Code Duplication |
protected function getFullPath($name = null)
|
|
|
|
|
123
|
|
|
{
|
124
|
2 |
|
if( empty($name) )
|
125
|
2 |
|
{
|
126
|
2 |
|
return $this->getPath() . $this->name;
|
127
|
|
|
}
|
128
|
|
|
else
|
129
|
|
|
{
|
130
|
2 |
|
return $this->getPath() . $name . '_' . $this->name;
|
131
|
|
|
}
|
132
|
|
|
}
|
133
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.