-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHelper.php
452 lines (371 loc) · 12.6 KB
/
Helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* News4ward
* a contentelement driven news/blog-system
*
* @author Christoph Wiechert <[email protected]>
* @copyright 4ward.media GbR <http://www.4wardmedia.de>
* @package news4ward
* @filesource
* @licence LGPL
*/
namespace Psi\News4ward;
use Contao\System;
use Contao\StringUtil;
class Helper extends \Frontend
{
/**
* Execute some ajax actions
* * toggle news4ward_article.status
*
* @param $strAction
* @return void
*/
public function ajaxHandler($strAction)
{
switch($strAction)
{
case 'news4wardArticleStatusToggle':
$this->loadDataContainer('tl_news4ward_article');
$this->import('BackendUser','User');
$tl_news4ward_article = new \tl_news4ward_article();
\Input::setGet('id', \Input::post('id'));
// validation
if( TL_MODE != 'BE'
|| !preg_match("~^\d+$~", \Input::post('id'))
|| !in_array(\Input::post('status'), $GLOBALS['TL_DCA']['tl_news4ward_article']['fields']['status']['options'])
|| !$this->User->hasAccess('tl_news4ward_article::status','alexf')
|| $tl_news4ward_article->checkPermission()
)
{
header('HTTP/1.0 400 Bad Request',true,400);
exit;
}
$this->import('Database');
$this->Database->prepare('UPDATE tl_news4ward_article SET status=? WHERE id=? LIMIT 1')
->executeUncached(\Input::post('status'),\Input::post('id'));
break;
default: return;
}
exit;
}
/**
* Replace news4ward insert tags
* @param $strTag
* @return bool|string
*/
public function inserttagReplacer($strTag)
{
list($strTag, $strValue) = explode('::',$strTag);
switch($strTag)
{
case 'news4ward':
switch($strValue)
{
case 'filter_hint':
if(!isset($GLOBALS['news4ward_filter_hint'])) return '';
$tpl = new \FrontendTemplate('news4ward_filter_hint');
$tpl->items = $GLOBALS['news4ward_filter_hint'];
return $tpl->parse();
break;
default: return false; break;
}
break;
case 'news4ward_link':
case 'news4ward_open':
case 'news4ward_url':
case 'news4ward_title':
$objArticle = $this->Database->prepare('
SELECT a.id, a.alias, a.title, p.jumpTo as parentJumpTo
FROM tl_news4ward_article AS a
LEFT JOIN tl_news4ward AS p ON (a.pid = p.id)
WHERE (a.id=? OR a.alias=?)'
. (!BE_USER_LOGGED_IN ? "AND (a.start='' OR a.start<?) AND (a.stop='' OR a.stop>?) AND a.status='published'" : ""))
->execute($strValue, $strValue, time(), time());
if(!$objArticle->numRows) return '';
if($strTag == 'news4ward_link')
{
return sprintf('<a href="%s" title="%s">%s</a>',$this->generateUrl($objArticle->row()), $objArticle->title, $objArticle->title);
}
else if($strTag == 'news4ward_open')
{
return sprintf('<a href="%s" title="%s">',$this->generateUrl($objArticle->row()), $objArticle->title);
}
else if($strTag == 'news4ward_url')
{
return $this->generateUrl($objArticle->row());
}
else if($strTag == 'news4ward_title')
{
return $objArticle->title;
}
break;
default: return false; break;
}
}
/**
* Add items to the indexer
* @param array
* @param integer
* @param boolean
* @return array
*/
public function getSearchablePages($arrPages, $intRoot=0, $blnIsSitemap=false)
{
$arrRoot = array();
if ($intRoot > 0)
{
$arrRoot = $this->Database->getChildRecords($intRoot, 'tl_page');
}
$time = time();
$arrProcessed = array();
// Get all news archives
$objArchive = $this->Database->execute("SELECT id, jumpTo FROM tl_news4ward WHERE protected!=1");
// Walk through each archive
while ($objArchive->next())
{
if (!empty($arrRoot) && !in_array($objArchive->jumpTo, $arrRoot))
{
continue;
}
// Get the URL of the jumpTo page
if (!isset($arrProcessed[$objArchive->jumpTo]))
{
$arrProcessed[$objArchive->jumpTo] = false;
// Get the target page
$objParent = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=? AND (start='' OR start<$time) AND (stop='' OR stop>$time) AND published=1 AND noSearch!=1")
->limit(1)
->execute($objArchive->jumpTo);
// Determin domain
if ($objParent->numRows)
{
$domain = $this->Environment->base;
$objParent = $this->getPageDetails($objParent->id);
if ($objParent->domain != '')
{
$domain = ($this->Environment->ssl ? 'https://' : 'http://') . $objParent->domain . TL_PATH . '/';
}
$arrProcessed[$objArchive->jumpTo] = $domain . $this->generateFrontendUrl($objParent->row(), (($GLOBALS['TL_CONFIG']['useAutoItem'] && in_array('items', $GLOBALS['TL_AUTO_ITEM'])) ? '/%s' : '/items/%s'), $objParent->language);
}
}
// Skip items without target page
if ($arrProcessed[$objArchive->jumpTo] === false)
{
continue;
}
$strUrl = $arrProcessed[$objArchive->jumpTo];
// Get items
$objArticle = $this->Database->prepare("SELECT tl_news4ward_article.id, tl_news4ward_article.alias, tl_news4ward.jumpTo as parentJumpTo
FROM tl_news4ward_article
LEFT JOIN tl_news4ward ON (tl_news4ward.id=tl_news4ward_article.pid)
WHERE tl_news4ward_article.pid=? AND (start='' OR start<$time) AND (stop='' OR stop>$time) AND status='published'
ORDER BY start DESC")
->execute($objArchive->id);
// Add items to the indexer
while ($objArticle->next())
{
$arrPages[] = $this->generateUrl($objArticle->row(), $strUrl);
}
}
return $arrPages;
}
/**
* Return the link of a news article
*
* @param array $arrArticle
* @param bool|string $strUrl an optional predefined url
* @return string
*/
public function generateUrl($arrArticle, $strUrl=false)
{
$strParam = (($arrArticle['alias'] != '' && !$GLOBALS['TL_CONFIG']['disableAlias']) ? $arrArticle['alias'] : $arrArticle['id']);
if (!$GLOBALS['TL_CONFIG']['useAutoItem'] || !in_array('items', $GLOBALS['TL_AUTO_ITEM']))
{
$strParam = 'items/' . $strParam;
}
if ($strUrl)
{
return sprintf($strUrl, $strParam);
}
elseif ($arrArticle['parentJumpTo'] && ($objJumpTo = \PageModel::findByPk($arrArticle['parentJumpTo'])) !== null)
{
return $objJumpTo->getFrontendUrl('/' . $strParam);
}
elseif (TL_MODE == 'FE')
{
return $this->generateFrontendUrl($GLOBALS['objPage']->row(), '/' . $strParam);
}
return '';
}
/**
* Update a particular RSS feed
* @param integer
*/
public function generateFeed($intId)
{
$objArchive = $this->Database->prepare("SELECT * FROM tl_news4ward WHERE id=? AND makeFeed=?")
->limit(1)
->execute($intId, 1);
if ($objArchive->numRows < 1)
{
return;
}
$objArchive->feedName = ($objArchive->alias != '') ? $objArchive->alias : 'news4ward' . $objArchive->id;
// Delete XML file
if (\Input::get('act') == 'delete' || $objArchive->protected)
{
$this->import('Files');
$this->Files->delete($objArchive->feedName . '.xml');
}
// Update XML file
else
{
$this->generateFiles($objArchive->row());
$this->log('Generated news4ward feed "' . $objArchive->feedName . '.xml"', 'Helper generateFeed()', TL_CRON);
}
}
/**
* Delete old files and generate all feeds
*/
public function generateFeeds()
{
$this->import('Automator');
$this->Automator->purgeXmlFiles();
$objArchive = $this->Database->execute("SELECT * FROM tl_news4ward WHERE makeFeed=1 AND protected!=1");
while ($objArchive->next())
{
$objArchive->feedName = ($objArchive->alias != '') ? $objArchive->alias : 'news4ward' . $objArchive->id;
$this->generateFiles($objArchive->row());
$this->log('Generated news4ward feed "' . $objArchive->feedName . '.xml"', 'Helper generateFeeds()', TL_CRON);
}
}
/**
* Generate an XML files and save them to the root directory
* @param array
*/
protected function generateFiles($arrArchive)
{
$time = time();
$strType = ($arrArchive['format'] == 'atom') ? 'generateAtom' : 'generateRss';
$strLink = ($arrArchive['feedBase'] != '') ? $arrArchive['feedBase'] : $this->Environment->base;
$strFile = $arrArchive['feedName'];
$objFeed = new \Feed($strFile);
$objFeed->link = $strLink;
$objFeed->title = $arrArchive['title'];
$objFeed->description = $arrArchive['description'];
$objFeed->language = $arrArchive['language'];
$objFeed->published = $arrArchive['tstamp'];
// Get items
$objArticleStmt = $this->Database->prepare("SELECT *, (SELECT name FROM tl_user u WHERE u.id=n.author) AS authorName
FROM tl_news4ward_article n
WHERE pid=? AND (start='' OR start<$time) AND (stop='' OR stop>$time) AND status='published'
ORDER BY start DESC");
if ($arrArchive['maxItems'] > 0)
{
$objArticleStmt->limit($arrArchive['maxItems']);
}
$objArticle = $objArticleStmt->execute($arrArchive['id']);
// Get the default URL
$objParent = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
->limit(1)
->execute($arrArchive['jumpTo']);
if ($objParent->numRows < 1)
{
return;
}
$objParent = $this->getPageDetails($objParent->id);
$strUrl = $this->generateFrontendUrl($objParent->row(), (($GLOBALS['TL_CONFIG']['useAutoItem'] && in_array('items', $GLOBALS['TL_AUTO_ITEM'])) ? '/%s' : '/items/%s'), $objParent->language);
// be sure to be absolute
if (substr($strUrl,0,4) != 'http')
{
$strUrl = $strLink.$strUrl;
}
// Parse items
while ($objArticle->next())
{
$objItem = new \FeedItem();
$objItem->title = $objArticle->title;
$objItem->link = $this->generateUrl($objArticle->row(), $strUrl);
$objItem->published = $objArticle->start;
$objItem->author = $objArticle->authorName;
// Prepare the description
if ($arrArchive['source'] == 'source_text')
{
/* generate the content-elements */
$objContentelements = $this->Database->prepare('SELECT id FROM tl_content WHERE pid=? AND ptable="news4ward" AND invisible="" ORDER BY sorting')->execute($objArticle->id);
$strDescription = '';
while($objContentelements->next())
{
$strDescription .= $this->getContentElement($objContentelements->id);
}
}
else
{
$strDescription = $objArticle->teaser;
}
$strDescription = $this->replaceInsertTags($strDescription);
// $strDescription = str_replace(array('<br>', '[-]', '­', '[nbsp]', ' '), array('<br/>', '', '', ' ', ' '), $strDescription);
// $strDescription = preg_replace('~/<img([^>]+)>/g~', '<img$i />', $strDescription);
$strDescription = \StringUtil::toXhtml($strDescription);
$objItem->description = $this->convertRelativeUrls($strDescription, $strLink);
// Add the article image as enclosure
if ($objArticle->addImage)
{
$objItem->addEnclosure($objArticle->singleSRC);
}
// Enclosure
if ($objArticle->addEnclosure)
{
$arrEnclosure = deserialize($objArticle->enclosure, true);
if (is_array($arrEnclosure))
{
foreach ($arrEnclosure as $strEnclosure)
{
if (is_file(TL_ROOT . '/' . $strEnclosure))
{
$objItem->addEnclosure($strEnclosure);
}
}
}
}
$objFeed->addItem($objItem);
}
// Create file
if (class_exists(\Contao\CoreBundle\ContaoCoreBundle::class)) {
$objRss = new \File(StringUtil::stripRootDir(System::getContainer()->getParameter('contao.web_dir')) . '/share/' . $strFile . '.xml');
} else {
$objRss = new \File($strFile . '.xml');
}
$objRss->write($this->replaceInsertTags($objFeed->$strType()));
$objRss->close();
}
/**
* Restrict the path for all fileTree fields when the archive has a filePath restriction
* this method gets call through DC-onload_callback
*
* @param DataContainer $dc
*/
public function setFiletreePath($dc)
{
$objNews4ward = null;
switch($dc->table)
{
case 'tl_news4ward_article':
$objNews4wardArticle = $this->Database->prepare('SELECT pid FROM tl_news4ward_article WHERE id=?')->execute($dc->id);
$objNews4ward = $this->Database->prepare('SELECT useFilePath, filePath FROM tl_news4ward WHERE id=?')->execute($objNews4wardArticle->pid);
break;
case 'tl_content':
$objNews4wardArticle = $this->Database->prepare('SELECT a.pid FROM tl_content AS c LEFT JOIN tl_news4ward_article AS a ON (c.pid=a.id) WHERE c.id=?')->execute($dc->id);
$objNews4ward = $this->Database->prepare('SELECT useFilePath, filePath FROM tl_news4ward WHERE id=?')->execute($objNews4wardArticle->pid);
break;
}
if(!$objNews4ward || $objNews4ward->numRows <= 0 || $objNews4ward->useFilePath != '1') return;
$objFile = \FilesModel::findByPk($objNews4ward->filePath);
if(!$objFile) return;
foreach($GLOBALS['TL_DCA'][$dc->table]['fields'] as $fld => $data)
{
if($data['inputType']!='fileTree') continue;
$GLOBALS['TL_DCA'][$dc->table]['fields'][$fld]['eval']['path'] = $objFile->path;
}
}
}