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,314 @@
1
+<?php
2
+
3
+/**
4
+ * References 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
+namespace eSM_clients;
14
+
15
+
16
+/**
17
+ * Class tl_module_esm_clients
18
+ * Provide miscellaneous methods that are used by the data configuration array.
19
+ */
20
+class tl_esm_clients_projects extends \Backend
21
+{
22
+	/**
23
+	 * Import the back end user object
24
+	 */
25
+	public function __construct()
26
+	{
27
+		parent::__construct();
28
+		$this->import('BackendUser', 'User');
29
+	}
30
+
31
+	public function addProjects($arrRow)
32
+	{
33
+		return '<div>'.$arrRow['title'].'</div>' . "\n";
34
+	}
35
+
36
+	/**
37
+	 * Return the file picker wizard
38
+	 * @param \DataContainer
39
+	 * @return string
40
+	 */
41
+	public function filePicker(\DataContainer $dc)
42
+	{
43
+		return ' <a href="contao/file.php?do='.\Input::get('do').'&amp;table='.$dc->table.'&amp;field='.$dc->field.'&amp;value='.$dc->value.'" title="'.specialchars(str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['filepicker'])).'" onclick="Backend.getScrollOffset();Backend.openModalSelector({\'width\':768,\'title\':\''.specialchars($GLOBALS['TL_LANG']['MOD']['files'][0]).'\',\'url\':this.href,\'id\':\''.$dc->field.'\',\'tag\':\'ctrl_'.$dc->field . ((\Input::get('act') == 'editAll') ? '_' . $dc->id : '').'\',\'self\':this});return false">' . \Image::getHtml('pickfile.gif', $GLOBALS['TL_LANG']['MSC']['filepicker'], 'style="vertical-align:top;cursor:pointer"') . '</a>';
44
+	}
45
+
46
+	/**
47
+	 * Return the link picker wizard
48
+	 * @param \DataContainer
49
+	 * @return string
50
+	 */
51
+	public function pagePicker(\DataContainer $dc)
52
+	{
53
+		return ' <a href="contao/page.php?do=' . \Input::get('do') . '&amp;table=' . $dc->table . '&amp;field=' . $dc->field . '&amp;value=' . str_replace(array('{{link_url::', '}}'), '', $dc->value) . '" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['pagepicker']) . '" onclick="Backend.getScrollOffset();Backend.openModalSelector({\'width\':768,\'title\':\'' . specialchars(str_replace("'", "\\'", $GLOBALS['TL_LANG']['MOD']['page'][0])) . '\',\'url\':this.href,\'id\':\'' . $dc->field . '\',\'tag\':\'ctrl_'. $dc->field . ((\Input::get('act') == 'editAll') ? '_' . $dc->id : '') . '\',\'self\':this});return false">' . \Image::getHtml('pickpage.gif', $GLOBALS['TL_LANG']['MSC']['pagepicker'], 'style="vertical-align:top;cursor:pointer"') . '</a>';
54
+	}
55
+
56
+	/**
57
+	 * Return the "toggle visibility" button
58
+	 * @param array
59
+	 * @param string
60
+	 * @param string
61
+	 * @param string
62
+	 * @param string
63
+	 * @param string
64
+	 * @return string
65
+	 */
66
+	public function toggleIcon($row, $href, $label, $title, $icon, $attributes)
67
+	{
68
+		if (strlen(\Input::get('tid')))
69
+		{
70
+			$this->toggleVisibility(\Input::get('tid'), (\Input::get('state') == 1));
71
+			$this->redirect($this->getReferer());
72
+		}
73
+
74
+		// Check permissions AFTER checking the tid, so hacking attempts are logged
75
+		if (!$this->User->hasAccess('tl_esm_clients_projects::invisible', 'alexf'))
76
+		{
77
+			return '';
78
+		}
79
+
80
+		$href .= '&amp;id='.\Input::get('id').'&amp;tid='.$row['id'].'&amp;state='.$row['invisible'];
81
+
82
+		if ($row['invisible'])
83
+		{
84
+			$icon = 'invisible.gif';
85
+		}
86
+
87
+		return '<a href="'.$this->addToUrl($href).'" title="'.specialchars($title).'"'.$attributes.'>'.\Image::getHtml($icon, $label).'</a> ';
88
+	}
89
+
90
+
91
+	/**
92
+	 * Toggle the visibility of an element
93
+	 * @param integer
94
+	 * @param boolean
95
+	 */
96
+	public function toggleVisibility($intId, $blnVisible)
97
+	{
98
+		// Check permissions to edit
99
+		\Input::setGet('id', $intId);
100
+		\Input::setGet('act', 'toggle');
101
+
102
+		// The onload_callbacks vary depending on the dynamic parent table (see #4894)
103
+		if (is_array($GLOBALS['TL_DCA']['tl_esm_clients_projects']['config']['onload_callback']))
104
+		{
105
+			foreach ($GLOBALS['TL_DCA']['tl_esm_clients_projects']['config']['onload_callback'] as $callback)
106
+			{
107
+				if (is_array($callback))
108
+				{
109
+					$this->import($callback[0]);
110
+					$this->$callback[0]->$callback[1]($this);
111
+				}
112
+				elseif (is_callable($callback))
113
+				{
114
+					$callback($this);
115
+				}
116
+			}
117
+		}
118
+
119
+		// Check permissions to publish
120
+		if (!$this->User->hasAccess('tl_esm_clients_projects::invisible', 'alexf'))
121
+		{
122
+			$this->log('Not enough permissions to show/hide content element ID "'.$intId.'"', __METHOD__, TL_ERROR);
123
+			$this->redirect('contao/main.php?act=error');
124
+		}
125
+
126
+		$objVersions = new \Versions('tl_esm_clients_projects', $intId);
127
+		$objVersions->initialize();
128
+
129
+		// Trigger the save_callback
130
+		if (is_array($GLOBALS['TL_DCA']['tl_esm_clients_projects']['fields']['invisible']['save_callback']))
131
+		{
132
+			foreach ($GLOBALS['TL_DCA']['tl_esm_clients_projects']['fields']['invisible']['save_callback'] as $callback)
133
+			{
134
+				if (is_array($callback))
135
+				{
136
+					$this->import($callback[0]);
137
+					$blnVisible = $this->$callback[0]->$callback[1]($blnVisible, $this);
138
+				}
139
+				elseif (is_callable($callback))
140
+				{
141
+					$blnVisible = $callback($blnVisible, $this);
142
+				}
143
+			}
144
+		}
145
+
146
+		// Update the database
147
+		$this->Database->prepare("UPDATE tl_esm_clients_projects SET tstamp=". time() .", invisible='" . ($blnVisible ? '' : 1) . "' WHERE id=?")
148
+			->execute($intId);
149
+
150
+		$objVersions->create();
151
+		$this->log('A new version of record "tl_esm_clients_projects.id='.$intId.'" has been created', __METHOD__, TL_GENERAL);
152
+	}
153
+
154
+	public function getTypes(\DataContainer $dc)
155
+	{
156
+		$arrOptions = array();
157
+
158
+		$Types = \EsmClientsSettingsModel::findByType('type');
159
+
160
+		if (!is_null($Types))
161
+		{
162
+			while($Types->next())
163
+			{
164
+				$arrOptions[$Types->id] = $Types->title;
165
+			}
166
+		}
167
+
168
+		return $arrOptions;
169
+	}
170
+
171
+	public function getServices(\DataContainer $dc)
172
+	{
173
+		$arrOptions = array();
174
+
175
+		$Services = \EsmClientsSettingsModel::findByType('services');
176
+
177
+		if (!is_null($Services))
178
+		{
179
+			while($Services->next())
180
+			{
181
+				$arrOptions[$Services->id] = $Services->title;
182
+			}
183
+		}
184
+
185
+		return $arrOptions;
186
+	}
187
+
188
+	public function getTech(\DataContainer $dc)
189
+	{
190
+		$arrOptions = array();
191
+
192
+		$Tech = \EsmClientsSettingsModel::findByType('tech');
193
+
194
+		if (!is_null($Tech))
195
+		{
196
+			while($Tech->next())
197
+			{
198
+				$arrOptions[$Tech->id] = $Tech->title;
199
+			}
200
+		}
201
+
202
+		return $arrOptions;
203
+	}
204
+
205
+	/**
206
+	 * Auto-generate alias if it has not been set yet
207
+	 * @param $varValue
208
+	 * @param \DataContainer $dc
209
+	 * @return string
210
+	 * @throws \Exception
211
+	 * @internal param $mixed
212
+	 * @internal param $ \DataContainer
213
+	 */
214
+	public function generateAlias($varValue, \DataContainer $dc)
215
+	{
216
+		$autoAlias = false;
217
+
218
+		// Generate alias if there is none
219
+		if ($varValue == '')
220
+		{
221
+			$autoAlias = true;
222
+
223
+			$objParent = EsmClientsModel::findByPk($dc->activeRecord->pid);
224
+
225
+			$varValue = standardize(\String::restoreBasicEntities($dc->activeRecord->title).(($objParent !== null) ? '__'.($objParent->alias != '' ? $objParent->alias : \String::restoreBasicEntities($objParent->title)) : ''));
226
+		}
227
+
228
+		$objAlias = $this->Database->prepare("SELECT id FROM tl_esm_clients_projects WHERE alias=?")
229
+			->execute($varValue);
230
+
231
+		// Check whether the alias exists
232
+		if ($objAlias->numRows > 1 && !$autoAlias)
233
+		{
234
+			throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
235
+		}
236
+
237
+		// Add ID to alias
238
+		if ($objAlias->numRows && $autoAlias)
239
+		{
240
+			$varValue .= '-' . $dc->id;
241
+		}
242
+
243
+		return $varValue;
244
+	}
245
+
246
+	/**
247
+	 * Automatically generate the folder URL aliases
248
+	 * @param array
249
+	 * @param \DataContainer
250
+	 * @return array
251
+	 */
252
+	public function addAliasButton($arrButtons, \DataContainer $dc)
253
+	{
254
+		// Generate the aliases
255
+		if (\Input::post('FORM_SUBMIT') == 'tl_select' && isset($_POST['alias']))
256
+		{
257
+			$session = $this->Session->getData();
258
+			$ids = $session['CURRENT']['IDS'];
259
+
260
+			foreach ($ids as $id)
261
+			{
262
+				$objProjects = EsmClientsProjectsModel::findByPk($id);
263
+
264
+				if ($objProjects === null)
265
+				{
266
+					continue;
267
+				}
268
+
269
+				$dc->id = $id;
270
+				$dc->activeRecord = $objProjects;
271
+
272
+				$strAlias = '';
273
+
274
+				// Generate new alias through save callbacks
275
+				foreach ($GLOBALS['TL_DCA'][$dc->table]['fields']['alias']['save_callback'] as $callback)
276
+				{
277
+					if (is_array($callback))
278
+					{
279
+						$this->import($callback[0]);
280
+						$strAlias = $this->$callback[0]->$callback[1]($strAlias, $dc);
281
+					}
282
+					elseif (is_callable($callback))
283
+					{
284
+						$strAlias = $callback($strAlias, $dc);
285
+					}
286
+				}
287
+
288
+				// The alias has not changed
289
+				if ($strAlias == $objProjects->alias)
290
+				{
291
+					continue;
292
+				}
293
+
294
+				// Initialize the version manager
295
+				$objVersions = new \Versions($dc->table, $id);
296
+				$objVersions->initialize();
297
+
298
+				// Store the new alias
299
+				$this->Database->prepare("UPDATE tl_esm_clients_projects SET alias=? WHERE id=?")
300
+					->execute($strAlias, $id);
301
+
302
+				// Create a new version
303
+				$objVersions->create();
304
+			}
305
+
306
+			$this->redirect($this->getReferer());
307
+		}
308
+
309
+		// Add the button
310
+		$arrButtons['alias'] = '<input type="submit" name="alias" id="alias" class="tl_submit" accesskey="a" value="'.specialchars($GLOBALS['TL_LANG']['MSC']['aliasSelected']).'"> ';
311
+
312
+		return $arrButtons;
313
+	}
314
+}
0 315
\ No newline at end of file