1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* image-friendly upload field. |
5
|
|
|
|
6
|
|
|
* Usage: |
7
|
|
|
* $field = PerfectCMSImagesUploadFielde::create( |
8
|
|
|
* "ImageField", |
9
|
|
|
* "Add Image", |
10
|
|
|
* null, |
11
|
|
|
* 300, |
12
|
|
|
* "anything you like", |
13
|
|
|
* "folder-name", |
14
|
|
|
* "png or jpg" |
15
|
|
|
* ); |
16
|
|
|
*/ |
17
|
|
|
class PerfectCMSImagesUploadField extends UploadField implements flushable |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
private static $max_size_in_kilobytes = 1024; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $name |
23
|
|
|
* @param string $title |
24
|
|
|
* @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation |
|
|
|
|
25
|
|
|
* |
26
|
|
|
* @return UploadField |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
$name, |
30
|
|
|
$title, |
31
|
|
|
SS_List $items = null |
32
|
|
|
) { |
33
|
|
|
parent::__construct( |
34
|
|
|
$name, |
35
|
|
|
$title, |
36
|
|
|
$items |
37
|
|
|
); |
38
|
|
|
$perfectCMSImageValidator = new PerfectCMSImage_Validator(); |
39
|
|
|
$this->setValidator($perfectCMSImageValidator); |
40
|
|
|
$this->selectFormattingStandard($name); |
41
|
|
|
return $this; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setRightTitle($string) |
45
|
|
|
{ |
46
|
|
|
parent::setRightTitle( |
47
|
|
|
$string. |
48
|
|
|
'<br />'. |
49
|
|
|
$this->RightTitle() |
50
|
|
|
); |
51
|
|
|
//important! |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* |
58
|
|
|
* |
59
|
|
|
* @param string $name Formatting Standard |
60
|
|
|
* @return this |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function selectFormattingStandard($name) |
63
|
|
|
{ |
64
|
|
|
parent::setRightTitle(''); |
|
|
|
|
65
|
|
|
$widthRecommendation = PerfectCMSImageDataExtension::get_width($name); |
66
|
|
|
$heightRecommendation = PerfectCMSImageDataExtension::get_height($name); |
67
|
|
|
$folderName = PerfectCMSImageDataExtension::get_folder($name); |
68
|
|
|
if (!$folderName) { |
69
|
|
|
$folderName = 'other-images'; |
70
|
|
|
} |
71
|
|
|
$recommendedFileType = PerfectCMSImageDataExtension::get_file_type($name); |
72
|
|
|
if (!$recommendedFileType) { |
73
|
|
|
$recommendedFileType = 'jpg'; |
74
|
|
|
} |
75
|
|
|
$folderName = 'Uploads/'.$folderName.'/'; |
76
|
|
|
if ($widthRecommendation) { |
77
|
|
|
if (intval($widthRecommendation)) { |
78
|
|
|
//cater for retina |
79
|
|
|
$widthRecommendation = $widthRecommendation * 2; |
80
|
|
|
$actualWidthDescription = $widthRecommendation.'px'; |
81
|
|
|
} else { |
82
|
|
|
$actualWidthDescription = $widthRecommendation; |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$actualWidthDescription = 'flexible'; |
86
|
|
|
} |
87
|
|
|
if ($heightRecommendation) { |
88
|
|
|
if (intval($heightRecommendation)) { |
89
|
|
|
//cater for retina |
90
|
|
|
$heightRecommendation = $heightRecommendation * 2; |
91
|
|
|
$actualHeightDescription = $heightRecommendation.'px'; |
92
|
|
|
} else { |
93
|
|
|
$actualHeightDescription = $heightRecommendation; |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
$actualHeightDescription = 'flexible'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
$rightTitle = ""; |
101
|
|
|
|
102
|
|
|
if ($actualWidthDescription == 'flexible') { |
103
|
|
|
$rightTitle .= 'Image width is flexible, and '; |
104
|
|
|
} else { |
105
|
|
|
$rightTitle .= "Image should be <strong>$actualWidthDescription</strong> wide and "; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
if ($actualHeightDescription == 'flexible') { |
110
|
|
|
$rightTitle .= 'image height is flexible, and '; |
111
|
|
|
} else { |
112
|
|
|
$rightTitle .= "image should be <strong>$actualHeightDescription</strong> high and "; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$rightTitle .= 'the image should be less than 1MB in size. <br/> |
116
|
|
|
The recommend file type (file extension) is <strong>'.$recommendedFileType.'</strong>. |
117
|
|
|
'; |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
parent::setRightTitle($rightTitle); |
|
|
|
|
121
|
|
|
|
122
|
|
|
//create folder |
123
|
|
|
Folder::find_or_make($folderName); |
124
|
|
|
//set folder |
125
|
|
|
$this->setFolderName($folderName); |
126
|
|
|
$this->setAllowedFileCategories('image'); |
127
|
|
|
$alreadyAllowed = $this->getAllowedExtensions(); |
128
|
|
|
$this->setAllowedExtensions($alreadyAllowed + array('svg')); |
129
|
|
|
//keep the size reasonable |
130
|
|
|
$this->getValidator()->setAllowedMaxFileSize(1 * 1024 * Config::inst()->get('PerfectCMSImagesUploadFieldeProvider', 'max_size_in_kilobytes')); |
131
|
|
|
$this->getValidator()->setFieldName($name); |
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function flush() { |
136
|
|
|
if(ASSETS_PATH) { |
137
|
|
|
if(! file_exists(ASSETS_PATH)) { |
138
|
|
|
Filesystem::makeFolder(ASSETS_PATH); |
139
|
|
|
} |
140
|
|
|
$fileName = ASSETS_PATH.'/.htaccess'; |
141
|
|
|
if(! file_exists($fileName)) { |
142
|
|
|
$string = ' |
143
|
|
|
<IfModule mod_rewrite.c> |
144
|
|
|
RewriteEngine On |
145
|
|
|
RewriteBase / |
146
|
|
|
|
147
|
|
|
RewriteCond %{REQUEST_FILENAME} !-f |
148
|
|
|
RewriteCond %{REQUEST_FILENAME} !-d |
149
|
|
|
RewriteRule ^(.+)\.(v[A-Za-z0-9]+)\.(js|css|png|jpg|gif)$ $1.$3 [L] |
150
|
|
|
</IfModule> |
151
|
|
|
'; |
152
|
|
|
if(!file_exists(ASSETS_PATH)) { |
153
|
|
|
Filesystem::makeFolder(ASSETS_PATH); |
154
|
|
|
} |
155
|
|
|
file_put_contents($fileName, $string); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.