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