Browse code

Initial commit

Benjamin Roth authored on26/03/2015 15:24:56
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,299 @@
1
+<?php
2
+
3
+/**
4
+ * Clients for Contao
5
+ *
6
+ * Copyright (c) 2014 Benjamin Roth [http://www.esales-media.de]
7
+ *
8
+ * @package eSM_clients
9
+ * @link    http://www.esales-media.de
10
+ * @license commercial
11
+*/
12
+
13
+
14
+/**
15
+ * Run in a custom namespace, so the class can be replaced
16
+ */
17
+namespace eSM_clients;
18
+
19
+
20
+/**
21
+ * Shows a list of clients which have active projects
22
+ *
23
+ * @package   Modules
24
+ * @author    Benjamin Roth <http://www.esales-media.de>
25
+ * @copyright eSales Media 2014
26
+ */
27
+class ModuleClientsProject extends \Module
28
+{
29
+	/**
30
+	 * Template
31
+	 * @var string
32
+	 */
33
+	protected $strTemplate = 'mod_clientsProject';
34
+
35
+	protected $objProject = null;
36
+
37
+
38
+	public function generate()
39
+	{
40
+		if (TL_MODE == 'BE')
41
+		{
42
+			$objTemplate = new \BackendTemplate('be_wildcard');
43
+
44
+			$objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['clientsProject'][0]) . ' ###';
45
+			$objTemplate->title = $this->headline;
46
+			$objTemplate->id = $this->id;
47
+			$objTemplate->link = $this->name;
48
+			$objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;
49
+
50
+			return $objTemplate->parse();
51
+		}
52
+
53
+		// Set the item from the auto_item parameter
54
+		if (!isset($_GET['items']) && \Config::get('useAutoItem') && isset($_GET['auto_item']))
55
+		{
56
+			\Input::setGet('items', \Input::get('auto_item'));
57
+		}
58
+
59
+		$this->objProject = EsmClientsProjectsModel::findByIdOrAlias($_GET['items']);
60
+
61
+		if ($this->objProject === null)
62
+		{
63
+			global $objPage;
64
+			$objHandler = new $GLOBALS['TL_PTY']['error_404']();
65
+			$objHandler->generate($objPage->id);
66
+		}
67
+
68
+		return parent::generate();
69
+	}
70
+
71
+	/**
72
+	 * Compile the current element
73
+	 */
74
+	protected function compile()
75
+	{
76
+		global $objPage;
77
+
78
+		$Client = $this->objProject->getRelated('pid');
79
+
80
+		// Get the previous and next project
81
+		$subSql = "SELECT ri.*, @row_num := @row_num+1 AS RN FROM (SELECT p.id, p.alias, p.title, c.title as 'client', p.sorting FROM tl_esm_clients c, tl_esm_clients_projects p WHERE p.pid=c.id ORDER BY c.sorting, p.sorting) ri, (SELECT @row_num := 0) r";
82
+		$Current = \Database::getInstance()->prepare("SELECT sq.RN FROM (".$subSql.") sq WHERE sq.id = ?")->execute($this->objProject->id);
83
+
84
+		if ($Current->numRows)
85
+		{
86
+			// Get previous
87
+			$Previous = \Database::getInstance()->prepare("SELECT sq.id, sq.alias, sq.title, sq.client FROM (".$subSql.") sq WHERE sq.RN = ?")->execute($Current->RN-1);
88
+
89
+			// Get next
90
+			$Next = \Database::getInstance()->prepare("SELECT sq.id, sq.alias, sq.title, sq.client FROM (".$subSql.") sq WHERE sq.RN = ?")->execute($Current->RN+1);
91
+
92
+			// Set projects nav vars
93
+			$strJumpTo = ampersand($this->generateFrontendUrl($objPage->row(), ((\Config::get('useAutoItem') && !\Config::get('disableAlias')) ?  '/%s' : '/items/%s')));
94
+
95
+			if ($Previous->numRows)
96
+			{
97
+				$this->Template->prevProject = array_merge($Previous->row(),array('link'=>sprintf($strJumpTo,(!\Config::get('disableAlias') && $Previous->alias != '') ? $Previous->alias : $Previous->id)));
98
+			}
99
+
100
+			if ($Next->numRows)
101
+			{
102
+				$this->Template->nextProject = array_merge($Next->row(),array('link'=>sprintf($strJumpTo,(!\Config::get('disableAlias') && $Next->alias != '') ? $Next->alias : $Next->id)));
103
+			}
104
+		}
105
+
106
+
107
+		// Get the file entries from the database
108
+		$objFiles = \FilesModel::findMultipleByUuids(deserialize($this->objProject->gallerySRC,true));
109
+
110
+		// Get all images
111
+		$images = array();
112
+		$auxDate = array();
113
+
114
+		if (!is_null($objFiles))
115
+		{
116
+			while ($objFiles->next())
117
+			{
118
+				// Continue if the files has been processed or does not exist
119
+				if (isset($images[$objFiles->path]) || !file_exists(TL_ROOT . '/' . $objFiles->path))
120
+				{
121
+					continue;
122
+				}
123
+
124
+				// Single files
125
+				if ($objFiles->type == 'file')
126
+				{
127
+					$objFile = new \File($objFiles->path, true);
128
+
129
+					if (!$objFile->isImage)
130
+					{
131
+						continue;
132
+					}
133
+
134
+					$arrMeta = $this->getMetaData($objFiles->meta, $objPage->language);
135
+
136
+					if (empty($arrMeta))
137
+					{
138
+						$arrMeta = $this->getMetaData($objFiles->meta, $objPage->rootFallbackLanguage);
139
+					}
140
+
141
+					// Use the file name as title if none is given
142
+					if ($arrMeta['title'] == '')
143
+					{
144
+						$arrMeta['title'] = specialchars($objFile->basename);
145
+					}
146
+
147
+					// Add the image
148
+					$images[$objFiles->path] = array
149
+					(
150
+						'id' => $objFiles->id,
151
+						'uuid' => $objFiles->uuid,
152
+						'name' => $objFile->basename,
153
+						'singleSRC' => $objFiles->path,
154
+						'alt' => $arrMeta['title'],
155
+						'imageUrl' => $arrMeta['link'],
156
+						'caption' => $arrMeta['caption'],
157
+						'size' => $this->imgSize,
158
+						'fullsize' => $this->fullsize
159
+					);
160
+
161
+					$auxDate[] = $objFile->mtime;
162
+				} // Folders
163
+				else
164
+				{
165
+					$objSubfiles = \FilesModel::findByPid($objFiles->uuid);
166
+
167
+					if ($objSubfiles === null)
168
+					{
169
+						continue;
170
+					}
171
+
172
+					while ($objSubfiles->next())
173
+					{
174
+						// Skip subfolders
175
+						if ($objSubfiles->type == 'folder')
176
+						{
177
+							continue;
178
+						}
179
+
180
+						$objFile = new \File($objSubfiles->path, true);
181
+
182
+						if (!$objFile->isImage)
183
+						{
184
+							continue;
185
+						}
186
+
187
+						$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->language);
188
+
189
+						if (empty($arrMeta))
190
+						{
191
+							$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->rootFallbackLanguage);
192
+						}
193
+
194
+						// Use the file name as title if none is given
195
+						if ($arrMeta['title'] == '')
196
+						{
197
+							$arrMeta['title'] = specialchars($objFile->basename);
198
+						}
199
+
200
+						// Add the image
201
+						$images[$objSubfiles->path] = array
202
+						(
203
+							'id' => $objSubfiles->id,
204
+							'uuid' => $objSubfiles->uuid,
205
+							'name' => $objFile->basename,
206
+							'singleSRC' => $objSubfiles->path,
207
+							'alt' => $arrMeta['title'],
208
+							'imageUrl' => $arrMeta['link'],
209
+							'caption' => $arrMeta['caption'],
210
+							'size' => $this->imgSize,
211
+							'fullsize' => $this->fullsize
212
+						);
213
+
214
+						$auxDate[] = $objFile->mtime;
215
+					}
216
+				}
217
+			}
218
+
219
+			if ($this->objProject->galleryOrder != '')
220
+			{
221
+				$tmp = deserialize($this->objProject->galleryOrder);
222
+
223
+				if (!empty($tmp) && is_array($tmp))
224
+				{
225
+					// Remove all values
226
+					$arrOrder = array_map(function ()
227
+					{
228
+					}, array_flip($tmp));
229
+
230
+					// Move the matching elements to their position in $arrOrder
231
+					foreach ($images as $k => $v)
232
+					{
233
+						if (array_key_exists($v['uuid'], $arrOrder))
234
+						{
235
+							$arrOrder[$v['uuid']] = $v;
236
+							unset($images[$k]);
237
+						}
238
+					}
239
+
240
+					// Append the left-over images at the end
241
+					if (!empty($images))
242
+					{
243
+						$arrOrder = array_merge($arrOrder, array_values($images));
244
+					}
245
+
246
+					// Remove empty (unreplaced) entries
247
+					$images = array_values(array_filter($arrOrder));
248
+					unset($arrOrder);
249
+				}
250
+			}
251
+			$images = array_values($images);
252
+		}
253
+
254
+		// Add images to Template
255
+		$arrImages = array();
256
+		foreach ($images as $image)
257
+		{
258
+			$objImage = new \stdClass();
259
+			$this->addImageToTemplate($objImage, $image, null, 'lightbox[lb' . $this->id . ']');
260
+
261
+			$arrImages[] = $objImage;
262
+		}
263
+		$this->Template->screenshots = $arrImages;
264
+
265
+		// Set header vars
266
+		$this->Template->client_title = $Client->title;
267
+		$this->Template->title = $this->objProject->title;
268
+
269
+		// Set client summary
270
+		$this->Template->client_summary = $Client->text;
271
+
272
+		// Set project summary vars
273
+		$this->Template->text = $this->objProject->text;
274
+		$this->Template->link = $this->objProject->projectUrl;
275
+		$this->Template->services = $this->getOptions(explode(',',$this->objProject->services));
276
+		$this->Template->features = deserialize($this->objProject->features,true);
277
+		$this->Template->techs = $this->getOptions(explode(',',$this->objProject->tech));
278
+
279
+	}
280
+
281
+	protected function getOptions(array $values)
282
+	{
283
+		$arrOptions = array();
284
+
285
+		$Types = \EsmClientsSettingsModel::findMultipleByIds($values);
286
+
287
+		if (!is_null($Types))
288
+		{
289
+			while($Types->next())
290
+			{
291
+				$arrOptions[] = array('title'=>$Types->title,'url'=>$Types->url);
292
+			}
293
+		}
294
+
295
+		return $arrOptions;
296
+	}
297
+
298
+
299
+}