forked from koala-framework/koala-framework
/
Component.php
177 lines (155 loc) · 5.89 KB
/
Component.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
<?php
class Kwc_Basic_ImageEnlarge_EnlargeTag_Component extends Kwc_Abstract
implements Kwf_Media_Output_IsValidInterface
{
public static function getSettings()
{
$ret = parent::getSettings();
$ret['componentName'] = trlKwfStatic('Enlarge Image');
$ret['fullSizeDownloadable'] = false;
$ret['imageTitle'] = true;
$ret['dimension'] = array('width'=>1920, 'height'=>1440, 'cover' => false);
$ret['generators']['imagePage'] = array(
'class' => 'Kwf_Component_Generator_Page_Static',
'name' => trlKwfStatic('Image'),
'component' => 'Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Component'
);
$ret['ownModel'] = 'Kwf_Component_FieldModel';
return $ret;
}
/**
* This function is used by Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Component
* to get the dimension-values defined in getSettings and the crop-values
* if use_crop was checked.
*/
public function getImageDimensions()
{
$dimension = $this->_getSetting('dimension');
if ($this->getRow()->use_crop) {
$parentDimension = $this->_getImageEnlargeComponent()->getImageDimensions();
$dimension['crop'] = $parentDimension['crop'];
}
$data = $this->getImageData();
return Kwf_Media_Image::calculateScaleDimensions($data['file'], $dimension);
}
public static function validateSettings($settings, $componentClass)
{
parent::validateSettings($settings, $componentClass);
if (isset($settings['showInactiveSwitchLinks'])) {
throw new Kwf_Exception("'showInactiveSwitchLinks' setting got removed; style them using css");
}
}
public function getTemplateVars()
{
$ret = parent::getTemplateVars();
$ret['imageUrl'] = $this->getImageUrl();
$ret['imagePage'] = $this->getData()->getChildComponent('_imagePage', array('ignoreVisible'=>true));
$parent = $this->getData()->parent;
if (is_instance_of($parent->componentClass, 'Kwc_Basic_LinkTag_Component')) {
$ret['linkTitle'] = $this->getData()->parent->getComponent()->getLinkTitle();
}
return $ret;
}
protected function _getOptions()
{
$ret = array();
if ($this->_getSetting('imageTitle')) {
$ret['title'] = nl2br($this->getRow()->title);
}
if ($this->_getSetting('fullSizeDownloadable')) {
$data = $this->getImageData();
if ($data && $data['filename']) {
$ret['fullSizeUrl'] = Kwf_Media::getUrl($this->getData()->componentClass,
$this->getData()->componentId, 'original', $data['filename']);
}
}
return $ret;
}
/**
* This function is called by Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Trl_Component
*/
public final function getOptions()
{
return $this->_getOptions();
}
protected function _getImageEnlargeComponent()
{
$d = $this->getData();
while (!is_instance_of($d->componentClass, 'Kwc_Basic_Image_Component')) {
$d = $d->parent;
}
return $d->getComponent();
}
/**
* This function is used by Kwc_Basic_ImageEnlarge_EnlargeTag_ImagePage_Component
* to get the url to show the image from parent with dimension defined through
* this component.
*/
public function getImageUrl()
{
$baseUrl = $this->getBaseImageUrl();
if ($baseUrl) {
$dimensions = $this->getImageDimensions();
$imageData = $this->getImageData();
$width = Kwf_Media_Image::getResponsiveWidthStep($dimensions['width'],
Kwf_Media_Image::getResponsiveWidthSteps($dimensions, $imageData['file']));
return str_replace('{width}', $width, $baseUrl);
}
return null;
}
public function getBaseType()
{
$type = Kwf_Media::DONT_HASH_TYPE_PREFIX.'{width}';
$type .= '-'.substr(md5(json_encode($this->_getSetting('dimension'))), 0, 6);
return $type;
}
public function getBaseImageUrl()
{
$data = $this->_getImageEnlargeComponent()->getImageData();
if ($data) {
$id = $this->getData()->componentId;
return Kwf_Media::getUrl($this->getData()->componentClass, $id, $this->getBaseType(), $data['filename']);
}
return null;
}
public function getImageData()
{
return $this->_getImageEnlargeComponent()->getImageData();
}
public final function getImageDataOrEmptyImageData()
{
return $this->_getImageEnlargeComponent()->getImageDataOrEmptyImageData();
}
public static function isValidMediaOutput($id, $type, $className)
{
return Kwf_Media_Output_Component::isValidImage($id, $type, $className);
}
public static function getMediaOutput($id, $type, $className)
{
$component = Kwf_Component_Data_Root::getInstance()->getComponentById($id, array('ignoreVisible' => true));
if (!$component) return null;
$data = $component->getComponent()->getImageData();
if (!$data) {
return null;
}
$dimension = $component->getComponent()->getImageDimensions();
return Kwf_Media_Output_Component::getMediaOutputForDimension($data, $dimension, $type);
}
public function getFulltextContent()
{
$ret = array();
//don't call parent, we handle imageCaption ourself
$options = (object)$this->_getOptions();
$text = '';
if (isset($options->title) && $options->title) {
$text .= ' '.$options->title;
}
if (isset($options->imageCaption) && $options->imageCaption) {
$text .= ' '.$options->imageCaption;
}
$text = trim($text);
$ret['content'] = $text;
$ret['normalContent'] = $text;
return $ret;
}
}