Browse code

Add device showcase module

Benjamin Roth authored on05/01/2016 15:52:24
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,174 @@
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 ModuleClientsProjectDevices extends \Module
28
+{
29
+	/**
30
+	 * Template
31
+	 * @var string
32
+	 */
33
+	protected $strTemplate = 'mod_clientsProjectDevices';
34
+
35
+	protected $objProject = null;
36
+
37
+	protected $arrSRCs = array();
38
+	protected $referenceHeight;
39
+
40
+
41
+	public function generate()
42
+	{
43
+		if (TL_MODE == 'BE')
44
+		{
45
+			$objTemplate = new \BackendTemplate('be_wildcard');
46
+
47
+			$objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['clientsProjectDevices'][0]) . ' ###';
48
+			$objTemplate->title = $this->headline;
49
+			$objTemplate->id = $this->id;
50
+			$objTemplate->link = $this->name;
51
+			$objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;
52
+
53
+			return $objTemplate->parse();
54
+		}
55
+
56
+		// Set the item from the auto_item parameter
57
+		if (!isset($_GET['items']) && \Config::get('useAutoItem') && isset($_GET['auto_item']))
58
+		{
59
+			\Input::setGet('items', \Input::get('auto_item'));
60
+		}
61
+
62
+		$this->objProject = EsmClientsProjectsModel::findByIdOrAlias($_GET['items']);
63
+
64
+		if ($this->objProject === null)
65
+		{
66
+			global $objPage;
67
+			$objHandler = new $GLOBALS['TL_PTY']['error_404']();
68
+			$objHandler->generate($objPage->id);
69
+		}
70
+
71
+		// Selected devices
72
+		$this->esm_clients_devices = deserialize($this->esm_clients_devices, true);
73
+
74
+		// Collect device images
75
+		if ($this->objProject->desktopSRC != '' && in_array('desktop', $this->esm_clients_devices))
76
+		{
77
+			$this->arrSRCs['desktop'] = $this->objProject->desktopSRC;
78
+		}
79
+
80
+		if ($this->objProject->phoneSRC != '' && in_array('smartphone', $this->esm_clients_devices))
81
+		{
82
+			$this->arrSRCs['phone'] = $this->objProject->phoneSRC;
83
+		}
84
+
85
+		if ($this->objProject->tabletSRC != '' && in_array('tablet', $this->esm_clients_devices))
86
+		{
87
+			$this->arrSRCs['tablet'] = $this->objProject->tabletSRC;
88
+		}
89
+
90
+		// Don't show module if no device screen is available
91
+		if (!count($this->arrSRCs))
92
+		{
93
+			return '';
94
+		}
95
+
96
+		// Load CSS
97
+		$GLOBALS['TL_CSS'][] = 'system/modules/eSM_clients/assets/css/devices.css||static';
98
+
99
+
100
+		return parent::generate();
101
+	}
102
+
103
+	/**
104
+	 * Compile the current element
105
+	 */
106
+	protected function compile()
107
+	{
108
+		global $objPage;
109
+
110
+		$this->esm_clients_devices_dimensions = deserialize($this->esm_clients_devices_dimensions);
111
+		if ($this->esm_clients_devices_dimensions[1])
112
+		{
113
+			$this->referenceHeight = $this->esm_clients_devices_dimensions[1];
114
+			$this->Template->referenceHeight = $this->referenceHeight;
115
+		}
116
+
117
+		// Add images
118
+		$arrDeviceScreenshots = array();
119
+		foreach ($this->arrSRCs as $devicetype => $source)
120
+		{
121
+			$objModel = \FilesModel::findByUuid($source);
122
+
123
+			if ($objModel === null)
124
+			{
125
+				if (!\Validator::isUuid($source))
126
+				{
127
+					$this->Template->text = '<p class="error">'.$GLOBALS['TL_LANG']['ERR']['version2format'].'</p>';
128
+				}
129
+			}
130
+			elseif (is_file(TL_ROOT . '/' . $objModel->path))
131
+			{
132
+				// Calculate devicemask height
133
+				if (isset($GLOBALS['eSM_clients']['devices'][$devicetype]))
134
+				{
135
+					$objDevicemask = new \File($GLOBALS['eSM_clients']['devices'][$devicetype]['devicemask']['imageSRC'], true);
136
+					if ($objDevicemask->isImage)
137
+					{
138
+						$intDeviceHeight = round($this->referenceHeight * $GLOBALS['eSM_clients']['devices'][$devicetype]['devicemask']['ratio']);
139
+						$objDevicemaskImage = \Image::create($objDevicemask,array('',$intDeviceHeight,'proportional'));
140
+						$objDevicemask = new \File($objDevicemaskImage->executeResize()->getResizedPath(),true);
141
+					}
142
+				}
143
+
144
+				$objFile = new \File($objModel->path,true);
145
+				if ($objFile->isImage)
146
+				{
147
+					$arrMeta = \Frontend::getMetaData($objModel->meta, $objPage->language);
148
+					$arrImage = array
149
+					(
150
+						'id'        => $objModel->id,
151
+						'uuid'      => $objModel->uuid,
152
+						'name'      => $objFile->basename,
153
+						'singleSRC' => $objModel->path,
154
+						'alt'       => $arrMeta['title'],
155
+						'imageUrl'  => $arrMeta['link'],
156
+						'caption'   => $arrMeta['caption'],
157
+						'size'      => serialize(array($objDevicemask->width*$GLOBALS['eSM_clients']['devices'][$devicetype]['screen']['ratio']['x'],$objDevicemask->height*$GLOBALS['eSM_clients']['devices'][$devicetype]['screen']['ratio']['y'],'center_top')),
158
+					);
159
+					$objImage = new \stdClass();
160
+					$this->addImageToTemplate($objImage, $arrImage);
161
+					$arrDeviceScreenshots[$devicetype] = array
162
+					(
163
+						'device'     => $objDevicemask->path,
164
+						'screenshot' => $objImage
165
+					);
166
+				}
167
+			}
168
+		}
169
+
170
+		// Add images to Template
171
+		$this->Template->screenshots = $arrDeviceScreenshots;
172
+
173
+	}
174
+}