Browse code

Extract image selection functions from Hook and add a generic Pageimage Class

Benjamin Roth authored on28/01/2016 11:40:53
Showing4 changed files
... ...
@@ -23,8 +23,11 @@ ClassLoader::addNamespaces(array
23 23
  */
24 24
 ClassLoader::addClasses(array
25 25
 (
26
+	// Classes
27
+	'eSM_pageimage\Pageimage' => 'system/modules/eSM_pageimage/classes/Pageimage.php',
28
+
26 29
 	// Hooks
27
-	'eSM_pageimage\Pageimage' => 'system/modules/eSM_pageimage/hooks/Pageimage.php',
30
+	'eSM_pageimage\HookPageimage' => 'system/modules/eSM_pageimage/hooks/HookPageimage.php',
28 31
 ));
29 32
 
30 33
 
... ...
@@ -9,4 +9,4 @@
9 9
  */
10 10
 
11 11
 
12
-$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('Pageimage', 'sbReplaceInsertTags');
12
+$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('HookPageimage', 'piReplaceInsertTags');
13 13
new file mode 100644
... ...
@@ -0,0 +1,97 @@
1
+<?php
2
+
3
+/**
4
+ * Pageimage for Contao
5
+ *
6
+ * Copyright (c) 2015 Benjamin Roth
7
+ *
8
+ * @license LGPL-3.0+
9
+ */
10
+
11
+namespace eSM_pageimage;
12
+
13
+ 
14
+class HookPageimage extends Pageimage
15
+{
16
+
17
+    public function piReplaceInsertTags($strTag)
18
+    {
19
+        $flags = explode('|', $strTag);
20
+        $elements = explode('::', array_shift($flags));
21
+        $images = static::getImages();
22
+
23
+        if (!count($images) || !in_array($elements[0],array('sb_images','sb_image','sb_image_url','sb_image_css')))
24
+        {
25
+            return false;
26
+        }
27
+
28
+
29
+        if (!isset($elements[1]) || !$elements[1])
30
+        {
31
+            $elements[1] = 0;
32
+        } else if (count($images)-1 < --$elements[1])
33
+        {
34
+            $elements[1] = count($images)-1;
35
+        }
36
+
37
+        if ($elements[0] == 'sb_image_url' || $elements[0] == 'sb_image_css')
38
+        {
39
+            if (count($images))
40
+            {
41
+                $image = $images[$elements[1]];
42
+                $width = null;
43
+                $height = null;
44
+                $mode = null;
45
+
46
+                if (is_array($image['size'] = deserialize($image['size'])))
47
+                {
48
+                    $width = intval($image['size'][0]) > 0 ? $image['size'][0] : null;
49
+                    $height = intval($image['size'][1]) > 0 ? $image['size'][1] : null;
50
+                    $mode = strlen($image['size'][2]) ? $image['size'][2] : null;
51
+                }
52
+
53
+                $strBuffer = \Image::get($image['singleSRC'], $width, $height, $mode);
54
+
55
+                if ($elements[0] == 'sb_image_css')
56
+                {
57
+                    return 'background-image: url(\'' . $this->Environment->path . '/' . $strBuffer . '\');';
58
+                }
59
+
60
+                return $strBuffer;
61
+            }
62
+        }
63
+
64
+        if ($elements[0] == 'sb_images' && count($images) > 0)
65
+        {
66
+            $arrImageTplObjects = array();
67
+
68
+            foreach($images as $image)
69
+            {
70
+                $objImage = new \stdClass();
71
+                $this->addImageToTemplate($objImage, $image);
72
+
73
+                $arrImageTplObjects[] = $objImage;
74
+            }
75
+            $objTemplate = new \FrontendTemplate('eSM_pageimage_container');
76
+            $objTemplate->pictures = $arrImageTplObjects;
77
+
78
+            return $objTemplate->parse();
79
+
80
+        } else if ($elements[0] == 'sb_image')
81
+        {
82
+
83
+            $arrImageTplObjects = array();
84
+
85
+            $objImage = new \stdClass();
86
+            $this->addImageToTemplate($objImage, $images[$elements[1]]);
87
+            $arrImageTplObjects[] = $objImage;
88
+
89
+            $objTemplate = new \FrontendTemplate('eSM_pageimage_container');
90
+            $objTemplate->pictures = $arrImageTplObjects;
91
+
92
+            return $objTemplate->parse();
93
+        }
94
+
95
+        return false;
96
+    }
97
+}
0 98
\ No newline at end of file
1 99
deleted file mode 100644
... ...
@@ -1,233 +0,0 @@
1
-<?php
2
-
3
-/**
4
- * Pageimage for Contao
5
- *
6
- * Copyright (c) 2015 Benjamin Roth
7
- *
8
- * @license LGPL-3.0+
9
- */
10
-
11
-namespace eSM_pageimage;
12
-
13
- 
14
-class Pageimage extends \Frontend
15
-{
16
-
17
-	public function sbReplaceInsertTags($strTag)
18
-	{
19
-		$flags = explode('|', $strTag);
20
-		$elements = explode('::', array_shift($flags));
21
-
22
-		if (!in_array($elements[0],array('sb_images','sb_image','sb_image_url','sb_image_css')))
23
-		{
24
-			return false;
25
-		}
26
-
27
-		global $objPage;
28
-
29
-		$objPage->sb_imageUrl = deserialize($objPage->sb_imageUrl);
30
-		$objPage->sb_imageOrder = deserialize($objPage->sb_imageOrder);
31
-
32
-		if ((!count($objPage->sb_imageUrl) || (count($objPage->sb_imageUrl) == 1 && !$objPage->sb_imageUrl[0])) && !$objPage->sb_imageIgnore)
33
-		{
34
-			$this->import('Database');
35
-			$pid = $objPage->id;
36
-			// Inherit settings
37
-			do
38
-			{
39
-				$objParentPage = $this->Database->prepare("SELECT * FROM tl_page WHERE id=?")
40
-												->limit(1)
41
-												->execute($pid);
42
-				if ($objParentPage->numRows < 1)
43
-				{
44
-					break;
45
-				}
46
-
47
-				$pid = $objParentPage->pid;
48
-				$type = $objParentPage->type;
49
-
50
-				$arrParentImageUrl = deserialize($objParentPage->sb_imageUrl);
51
-
52
-				if ((!is_array($objPage->sb_imageUrl) || !count($objPage->sb_imageUrl) || !$objPage->sb_imageUrl[0]) && (is_array($arrParentImageUrl) && count($arrParentImageUrl) && $arrParentImageUrl[0]))
53
-				{
54
-
55
-					$objPage->sb_imageUrl = deserialize($objParentPage->sb_imageUrl);
56
-					$objPage->sb_imageSize = $objParentPage->sb_imageSize;
57
-					$objPage->sb_imageOrder = deserialize($objParentPage->sb_imageOrder);
58
-					break;
59
-				}
60
-			}
61
-			while ($pid > 0 && $type != 'root');
62
-		}
63
-
64
-		if (!is_array($objPage->sb_imageUrl) || !count($objPage->sb_imageUrl) || !$objPage->sb_imageUrl[0])
65
-		{
66
-			return;
67
-		}
68
-
69
-
70
-		if ($objPage->sb_imageOrder != '')
71
-		{
72
-			$tmp = $objPage->sb_imageOrder;
73
-
74
-			if (!empty($tmp) && is_array($tmp))
75
-			{
76
-				// Remove all values
77
-				$arrOrder = array_map(function(){}, array_flip($tmp));
78
-
79
-				// Move the matching elements to their position in $arrOrder
80
-				foreach ($objPage->sb_imageUrl as $k=>$v)
81
-				{
82
-					if (array_key_exists($v, $arrOrder))
83
-					{
84
-						$arrOrder[$v] = $v;
85
-						unset($objPage->sb_imageUrl[$k]);
86
-					}
87
-				}
88
-
89
-				// Append the left-over images at the end
90
-				if (!empty($objPage->sb_imageUrl))
91
-				{
92
-					$arrOrder = array_merge($arrOrder, array_values($objPage->sb_imageUrl));
93
-				}
94
-
95
-				// Remove empty (unreplaced) entries
96
-				$objPage->sb_imageUrl = array_values(array_filter($arrOrder));
97
-				unset($arrOrder);
98
-			}
99
-		}
100
-
101
-		$images = array();
102
-		$multiSRC = array_values($objPage->sb_imageUrl);
103
-		$objFiles = \FilesModel::findMultipleByIds($multiSRC);
104
-
105
-		// Get all images
106
-		while ($objFiles->next())
107
-		{
108
-			// Continue if the files has been processed or does not exist
109
-			if (isset($images[$objFiles->path]) || !file_exists(TL_ROOT . '/' . $objFiles->path))
110
-			{
111
-				continue;
112
-			}
113
-
114
-			// Single files
115
-			$objFile = new \File($objFiles->path, true);
116
-
117
-			if (!$objFile->isImage)
118
-			{
119
-				continue;
120
-			}
121
-
122
-			$arrMeta = $this->getMetaData($objFiles->meta, $objPage->language);
123
-
124
-			if (empty($arrMeta))
125
-			{
126
-				if ($this->metaIgnore)
127
-				{
128
-					continue;
129
-				}
130
-				elseif ($objPage->rootFallbackLanguage !== null)
131
-				{
132
-					$arrMeta = $this->getMetaData($objFiles->meta, $objPage->rootFallbackLanguage);
133
-				}
134
-			}
135
-
136
-			// Use the file name as title if none is given
137
-			if ($arrMeta['title'] == '')
138
-			{
139
-				$arrMeta['title'] = specialchars($objFile->basename);
140
-			}
141
-
142
-			// Add the image
143
-			$images[$objFiles->path] = array
144
-			(
145
-				'id'        => $objFiles->id,
146
-				'uuid'      => $objFiles->uuid,
147
-				'name'      => $objFile->basename,
148
-				'singleSRC' => $objFiles->path,
149
-				'alt'       => $arrMeta['title'],
150
-				'imageUrl'  => $arrMeta['link'],
151
-				'caption'   => $arrMeta['caption'],
152
-				'size'      => $objPage->sb_imageSize
153
-			);
154
-		}
155
-		$images = array_values($images);
156
-		unset($objFiles);
157
-		unset($multiSRC);
158
-
159
-
160
-		if (!isset($elements[1]) || !$elements[1])
161
-		{
162
-			$elements[1] = 0;
163
-		} else if (count($images)-1 < --$elements[1])
164
-		{
165
-			$elements[1] = count($images)-1;
166
-		}
167
-
168
-		if ($elements[0] == 'sb_image_url' || $elements[0] == 'sb_image_css')
169
-		{
170
-			if (count($images))
171
-			{
172
-				$image = $images[$elements[1]];
173
-
174
-				if (is_array($image['size'] = deserialize($image['size'])))
175
-				{
176
-					$width = intval($image['size'][0]) > 0 ? $image['size'][0] : null;
177
-					$height = intval($image['size'][1]) > 0 ? $image['size'][1] : null;
178
-					$mode = strlen($image['size'][2]) ? $image['size'][2] : null;
179
-				}
180
-				
181
-				$strBuffer = \Image::get($image['singleSRC'], $width, $height, $mode);
182
-				
183
-				if ($elements[0] == 'sb_image_css')
184
-				{
185
-					return 'background-image: url(\'' . $this->Environment->path . '/' . $strBuffer . '\');';
186
-				}
187
-				
188
-				return $strBuffer;
189
-			}
190
-		}
191
-
192
-		if ($elements[0] == 'sb_images' && count($images) > 0)
193
-		{
194
-			$arrImageTplObjects = array();
195
-
196
-			foreach($images as $image)
197
-			{
198
-				$objImage = new \stdClass();
199
-				$this->addImageToTemplate($objImage, $image);
200
-
201
-				$arrImageTplObjects[] = $objImage;
202
-			}
203
-			$objTemplate = new \FrontendTemplate('eSM_pageimage_container');
204
-			$objTemplate->pictures = $arrImageTplObjects;
205
-
206
-			return $objTemplate->parse();
207
-
208
-		} else if ($elements[0] == 'sb_image')
209
-		{
210
-
211
-			$arrImageTplObjects = array();
212
-
213
-			$objImage = new \stdClass();
214
-			$this->addImageToTemplate($objImage, $images[$elements[1]]);
215
-			$arrImageTplObjects[] = $objImage;
216
-
217
-			$objTemplate = new \FrontendTemplate('eSM_pageimage_container');
218
-			$objTemplate->pictures = $arrImageTplObjects;
219
-
220
-			return $objTemplate->parse();
221
-		}
222
-	 
223
-		return false;
224
-	}
225
-
226
-	public static function getPageImage($strType='images')
227
-	{
228
-		$pageimage = new Pageimage();
229
-		return $pageimage->sbReplaceInsertTags('sb_'.$strType);
230
-	}
231
-}
232
-
233
-?>
234 0
\ No newline at end of file