Completed
Pull Request — master (#30)
by Robbie
02:19
created
src/PageTypes/DatedUpdateHolderController.php 1 patch
Indentation   +342 added lines, -342 removed lines patch added patch discarded remove patch
@@ -33,357 +33,357 @@
 block discarded – undo
33 33
  */
34 34
 class DatedUpdateHolderController extends PageController
35 35
 {
36
-    private static $allowed_actions = [
37
-        'rss',
38
-        'atom',
39
-        'DateRangeForm',
40
-    ];
41
-
42
-    private static $casting = [
43
-        'MetaTitle' => 'Text',
44
-        'FilterDescription' => 'Text',
45
-    ];
46
-
47
-    /**
48
-     * Get the meta title for the current action
49
-     *
50
-     * @return string
51
-     */
52
-    public function getMetaTitle()
53
-    {
54
-        $title = $this->data()->getTitle();
55
-        $filter = $this->FilterDescription();
56
-        if ($filter) {
57
-            $title = "{$title} - {$filter}";
58
-        }
59
-
60
-        $this->extend('updateMetaTitle', $title);
61
-        return $title;
62
-    }
63
-
64
-    /**
65
-     * Returns a description of the current filter
66
-     *
67
-     * @return string
68
-     */
69
-    public function FilterDescription()
70
-    {
71
-        $params = $this->parseParams();
72
-
73
-        $filters = array();
74
-        if ($params['tag']) {
75
-            $term = TaxonomyTerm::get_by_id(TaxonomyTerm::class, $params['tag']);
76
-            if ($term) {
77
-                $filters[] = _t(
78
-                    'CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_WITHIN',
79
-                    'within'
80
-                ) . ' "' . $term->Name . '"';
81
-            }
82
-        }
83
-
84
-        if ($params['from'] || $params['to']) {
85
-            if ($params['from']) {
86
-                $from = strtotime($params['from']);
87
-                if ($params['to']) {
88
-                    $to = strtotime($params['to']);
89
-                    $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_BETWEEN', 'between') . ' '
90
-                        . date('j/m/Y', $from) . ' and ' . date('j/m/Y', $to);
91
-                } else {
92
-                    $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on')
93
-                        . ' ' . date('j/m/Y', $from);
94
-                }
95
-            } else {
96
-                $to = strtotime($params['to']);
97
-                $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on')
98
-                    . ' ' . date('j/m/Y', $to);
99
-            }
100
-        }
101
-
102
-        if ($params['year'] && $params['month']) {
103
-            $timestamp = mktime(1, 1, 1, $params['month'], 1, $params['year']);
104
-            $filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_IN', 'in')
105
-                . ' ' . date('F', $timestamp) . ' ' . $params['year'];
106
-        }
107
-
108
-        if ($filters) {
109
-            return $this->getUpdateName() . ' ' . implode(' ', $filters);
110
-        }
111
-    }
112
-
113
-    public function getUpdateName()
114
-    {
115
-        return Config::inst()->get($this->data()->ClassName, 'update_name');
116
-    }
117
-
118
-    protected function init()
119
-    {
120
-        parent::init();
121
-        RSSFeed::linkToFeed($this->Link() . 'rss', $this->getSubscriptionTitle());
122
-    }
123
-
124
-    /**
125
-     * Parse URL parameters.
126
-     *
127
-     * @param bool $produceErrorMessages Set to false to omit session messages.
128
-     */
129
-    public function parseParams($produceErrorMessages = true)
130
-    {
131
-        $tag = $this->request->getVar('tag');
132
-        $from = $this->request->getVar('from');
133
-        $to = $this->request->getVar('to');
134
-        $year = $this->request->getVar('year');
135
-        $month = $this->request->getVar('month');
136
-
137
-        if ($tag == '') {
138
-            $tag = null;
139
-        }
140
-        if ($from == '') {
141
-            $from = null;
142
-        }
143
-        if ($to == '') {
144
-            $to = null;
145
-        }
146
-        if ($year == '') {
147
-            $year = null;
148
-        }
149
-        if ($month == '') {
150
-            $month = null;
151
-        }
152
-
153
-        if (isset($tag)) {
154
-            $tag = (int)$tag;
155
-        }
156
-        if (isset($from)) {
157
-            $from = urldecode($from);
158
-            $parser = DBDatetime::create();
159
-            $parser->setValue($from);
160
-            $from = $parser->Format('y-MM-dd');
161
-        }
162
-        if (isset($to)) {
163
-            $to = urldecode($to);
164
-            $parser = DBDatetime::create();
165
-            $parser->setValue($to);
166
-            $to = $parser->Format('y-MM-dd');
167
-        }
168
-        if (isset($year)) {
169
-            $year = (int)$year;
170
-        }
171
-        if (isset($month)) {
172
-            $month = (int)$month;
173
-        }
174
-
175
-        // If only "To" has been provided filter by single date. Normalise by swapping with "From".
176
-        if (isset($to) && !isset($from)) {
177
-            list($to, $from) = array($from, $to);
178
-        }
179
-
180
-        // Flip the dates if the order is wrong.
181
-        if (isset($to) && isset($from) && strtotime($from)>strtotime($to)) {
182
-            list($to, $from) = array($from, $to);
183
-
184
-            if ($produceErrorMessages) {
185
-                // @todo replace
36
+	private static $allowed_actions = [
37
+		'rss',
38
+		'atom',
39
+		'DateRangeForm',
40
+	];
41
+
42
+	private static $casting = [
43
+		'MetaTitle' => 'Text',
44
+		'FilterDescription' => 'Text',
45
+	];
46
+
47
+	/**
48
+	 * Get the meta title for the current action
49
+	 *
50
+	 * @return string
51
+	 */
52
+	public function getMetaTitle()
53
+	{
54
+		$title = $this->data()->getTitle();
55
+		$filter = $this->FilterDescription();
56
+		if ($filter) {
57
+			$title = "{$title} - {$filter}";
58
+		}
59
+
60
+		$this->extend('updateMetaTitle', $title);
61
+		return $title;
62
+	}
63
+
64
+	/**
65
+	 * Returns a description of the current filter
66
+	 *
67
+	 * @return string
68
+	 */
69
+	public function FilterDescription()
70
+	{
71
+		$params = $this->parseParams();
72
+
73
+		$filters = array();
74
+		if ($params['tag']) {
75
+			$term = TaxonomyTerm::get_by_id(TaxonomyTerm::class, $params['tag']);
76
+			if ($term) {
77
+				$filters[] = _t(
78
+					'CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_WITHIN',
79
+					'within'
80
+				) . ' "' . $term->Name . '"';
81
+			}
82
+		}
83
+
84
+		if ($params['from'] || $params['to']) {
85
+			if ($params['from']) {
86
+				$from = strtotime($params['from']);
87
+				if ($params['to']) {
88
+					$to = strtotime($params['to']);
89
+					$filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_BETWEEN', 'between') . ' '
90
+						. date('j/m/Y', $from) . ' and ' . date('j/m/Y', $to);
91
+				} else {
92
+					$filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on')
93
+						. ' ' . date('j/m/Y', $from);
94
+				}
95
+			} else {
96
+				$to = strtotime($params['to']);
97
+				$filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_ON', 'on')
98
+					. ' ' . date('j/m/Y', $to);
99
+			}
100
+		}
101
+
102
+		if ($params['year'] && $params['month']) {
103
+			$timestamp = mktime(1, 1, 1, $params['month'], 1, $params['year']);
104
+			$filters[] = _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FILTER_IN', 'in')
105
+				. ' ' . date('F', $timestamp) . ' ' . $params['year'];
106
+		}
107
+
108
+		if ($filters) {
109
+			return $this->getUpdateName() . ' ' . implode(' ', $filters);
110
+		}
111
+	}
112
+
113
+	public function getUpdateName()
114
+	{
115
+		return Config::inst()->get($this->data()->ClassName, 'update_name');
116
+	}
117
+
118
+	protected function init()
119
+	{
120
+		parent::init();
121
+		RSSFeed::linkToFeed($this->Link() . 'rss', $this->getSubscriptionTitle());
122
+	}
123
+
124
+	/**
125
+	 * Parse URL parameters.
126
+	 *
127
+	 * @param bool $produceErrorMessages Set to false to omit session messages.
128
+	 */
129
+	public function parseParams($produceErrorMessages = true)
130
+	{
131
+		$tag = $this->request->getVar('tag');
132
+		$from = $this->request->getVar('from');
133
+		$to = $this->request->getVar('to');
134
+		$year = $this->request->getVar('year');
135
+		$month = $this->request->getVar('month');
136
+
137
+		if ($tag == '') {
138
+			$tag = null;
139
+		}
140
+		if ($from == '') {
141
+			$from = null;
142
+		}
143
+		if ($to == '') {
144
+			$to = null;
145
+		}
146
+		if ($year == '') {
147
+			$year = null;
148
+		}
149
+		if ($month == '') {
150
+			$month = null;
151
+		}
152
+
153
+		if (isset($tag)) {
154
+			$tag = (int)$tag;
155
+		}
156
+		if (isset($from)) {
157
+			$from = urldecode($from);
158
+			$parser = DBDatetime::create();
159
+			$parser->setValue($from);
160
+			$from = $parser->Format('y-MM-dd');
161
+		}
162
+		if (isset($to)) {
163
+			$to = urldecode($to);
164
+			$parser = DBDatetime::create();
165
+			$parser->setValue($to);
166
+			$to = $parser->Format('y-MM-dd');
167
+		}
168
+		if (isset($year)) {
169
+			$year = (int)$year;
170
+		}
171
+		if (isset($month)) {
172
+			$month = (int)$month;
173
+		}
174
+
175
+		// If only "To" has been provided filter by single date. Normalise by swapping with "From".
176
+		if (isset($to) && !isset($from)) {
177
+			list($to, $from) = array($from, $to);
178
+		}
179
+
180
+		// Flip the dates if the order is wrong.
181
+		if (isset($to) && isset($from) && strtotime($from)>strtotime($to)) {
182
+			list($to, $from) = array($from, $to);
183
+
184
+			if ($produceErrorMessages) {
185
+				// @todo replace
186 186
 //                Session::setFormMessage(
187 187
 //                    'Form_DateRangeForm',
188 188
 //                    _t('DateUpdateHolder.FilterAppliedMessage', 'Filter has been applied with the dates reversed.'),
189 189
 //                    'warning'
190 190
 //                );
191
-            }
192
-        }
191
+			}
192
+		}
193 193
 
194
-        // Notify the user that filtering by single date is taking place.
195
-        if (isset($from) && !isset($to)) {
196
-            if ($produceErrorMessages) {
197
-                // @todo replace
194
+		// Notify the user that filtering by single date is taking place.
195
+		if (isset($from) && !isset($to)) {
196
+			if ($produceErrorMessages) {
197
+				// @todo replace
198 198
 //                Session::setFormMessage(
199 199
 //                    'Form_DateRangeForm',
200 200
 //                    _t('DateUpdateHolder.DateRangeFilterMessage', 'Filtered by a single date.'),
201 201
 //                    'warning'
202 202
 //                );
203
-            }
204
-        }
205
-
206
-        return [
207
-            'tag' => $tag,
208
-            'from' => $from,
209
-            'to' => $to,
210
-            'year' => $year,
211
-            'month' => $month,
212
-        ];
213
-    }
214
-
215
-    /**
216
-     * Build the link - keep the date range, reset the rest.
217
-     */
218
-    public function AllTagsLink()
219
-    {
220
-        $link = HTTP::setGetVar('tag', null, null, '&');
221
-        $link = HTTP::setGetVar('month', null, $link, '&');
222
-        $link = HTTP::setGetVar('year', null, $link, '&');
223
-        $link = HTTP::setGetVar('start', null, $link, '&');
224
-
225
-        return $link;
226
-    }
227
-
228
-    /**
229
-     * List tags and attach links.
230
-     */
231
-    public function UpdateTagsWithLinks()
232
-    {
233
-        $tags = $this->UpdateTags();
234
-
235
-        $processed = ArrayList::create();
236
-
237
-        foreach ($tags as $tag) {
238
-            // Build the link - keep the tag, and date range, but reset month, year and pagination.
239
-            $link = HTTP::setGetVar('tag', $tag->ID, null, '&');
240
-            $link = HTTP::setGetVar('month', null, $link, '&');
241
-            $link = HTTP::setGetVar('year', null, $link, '&');
242
-            $link = HTTP::setGetVar('start', null, $link, '&');
243
-
244
-            $tag->Link = $link;
245
-            $processed->push($tag);
246
-        }
247
-
248
-        return $processed;
249
-    }
250
-
251
-    /**
252
-     * Get the TaxonomyTerm related to the current tag GET parameter.
253
-     */
254
-    public function CurrentTag()
255
-    {
256
-        $tagID = $this->request->getVar('tag');
257
-
258
-        if (isset($tagID)) {
259
-            return TaxonomyTerm::get_by_id(TaxonomyTerm::class, (int)$tagID);
260
-        }
261
-    }
262
-
263
-    /**
264
-     * Extract the available months based on the current query.
265
-     * Only tag is respected. Pagination and months are ignored.
266
-     */
267
-    public function AvailableMonths()
268
-    {
269
-        $params = $this->parseParams();
270
-
271
-        return DatedUpdateHolder::ExtractMonths(
272
-            $this->Updates($params['tag'], $params['from'], $params['to']),
273
-            Director::makeRelative($_SERVER['REQUEST_URI']),
274
-            $params['year'],
275
-            $params['month']
276
-        );
277
-    }
278
-
279
-    /**
280
-     * Get the updates based on the current query.
281
-     */
282
-    public function FilteredUpdates($pageSize = 20)
283
-    {
284
-        $params = $this->parseParams();
285
-
286
-        $items = $this->Updates(
287
-            $params['tag'],
288
-            $params['from'],
289
-            $params['to'],
290
-            $params['year'],
291
-            $params['month']
292
-        );
293
-
294
-        // Apply pagination
295
-        $list = PaginatedList::create($items, $this->getRequest());
296
-        $list->setPageLength($pageSize);
297
-        return $list;
298
-    }
299
-
300
-    /**
301
-     * @return Form
302
-     */
303
-    public function DateRangeForm()
304
-    {
305
-        $dateFromTitle = DBField::create_field('HTMLText', sprintf(
306
-            '%s <span class="field-note">%s</span>',
307
-            _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FROM_DATE', 'From date'),
308
-            _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 2017/12/30)')
309
-        ));
310
-        $dateToTitle = DBField::create_field('HTMLText', sprintf(
311
-            '%s <span class="field-note">%s</span>',
312
-            _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.TO_DATE', 'To date'),
313
-            _t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 2017/12/30)')
314
-        ));
315
-
316
-        $fields = FieldList::create(
317
-            DateField::create('from', $dateFromTitle),
318
-            DateField::create('to', $dateToTitle),
319
-            HiddenField::create('tag')
320
-        );
321
-
322
-        $actions = FieldList::create(
323
-            FormAction::create("doDateFilter")
324
-                ->setTitle(_t(__CLASS__ . '.Filter', 'Filter'))
325
-                ->addExtraClass('btn btn-primary primary'),
326
-            FormAction::create("doDateReset")
327
-                ->setTitle(_t(__CLASS__ . '.Clear', 'Clear'))
328
-                ->addExtraClass('btn')
329
-        );
330
-
331
-        $form = Form::create($this, 'DateRangeForm', $fields, $actions);
332
-        $form->loadDataFrom($this->request->getVars());
333
-        $form->setFormMethod('get');
334
-
335
-        // Manually extract the message so we can clear it.
336
-        $form->ErrorMessage = $form->getMessage();
337
-        $form->ErrorMessageType = $form->getMessageType();
338
-        $form->clearMessage();
339
-
340
-        return $form;
341
-    }
342
-
343
-    public function doDateFilter()
344
-    {
345
-        $params = $this->parseParams();
346
-
347
-        // Build the link - keep the tag, but reset month, year and pagination.
348
-        $link = HTTP::setGetVar('from', $params['from'], $this->AbsoluteLink(), '&');
349
-        $link = HTTP::setGetVar('to', $params['to'], $link, '&');
350
-        if (isset($params['tag'])) {
351
-            $link = HTTP::setGetVar('tag', $params['tag'], $link, '&');
352
-        }
353
-
354
-        $this->redirect($link);
355
-    }
356
-
357
-    public function doDateReset()
358
-    {
359
-        $params = $this->parseParams(false);
360
-
361
-        // Reset the link - only include the tag.
362
-        $link = $this->AbsoluteLink();
363
-        if (isset($params['tag'])) {
364
-            $link = HTTP::setGetVar('tag', $params['tag'], $link, '&');
365
-        }
366
-
367
-        $this->redirect($link);
368
-    }
369
-
370
-    public function rss()
371
-    {
372
-        $rss = RSSFeed::create(
373
-            $this->Updates()->sort('Created DESC')->limit(20),
374
-            $this->Link('rss'),
375
-            $this->getSubscriptionTitle()
376
-        );
377
-        return $rss->outputToBrowser();
378
-    }
379
-
380
-    public function atom()
381
-    {
382
-        $atom = CwpAtomFeed::create(
383
-            $this->Updates()->sort('Created DESC')->limit(20),
384
-            $this->Link('atom'),
385
-            $this->getSubscriptionTitle()
386
-        );
387
-        return $atom->outputToBrowser();
388
-    }
203
+			}
204
+		}
205
+
206
+		return [
207
+			'tag' => $tag,
208
+			'from' => $from,
209
+			'to' => $to,
210
+			'year' => $year,
211
+			'month' => $month,
212
+		];
213
+	}
214
+
215
+	/**
216
+	 * Build the link - keep the date range, reset the rest.
217
+	 */
218
+	public function AllTagsLink()
219
+	{
220
+		$link = HTTP::setGetVar('tag', null, null, '&');
221
+		$link = HTTP::setGetVar('month', null, $link, '&');
222
+		$link = HTTP::setGetVar('year', null, $link, '&');
223
+		$link = HTTP::setGetVar('start', null, $link, '&');
224
+
225
+		return $link;
226
+	}
227
+
228
+	/**
229
+	 * List tags and attach links.
230
+	 */
231
+	public function UpdateTagsWithLinks()
232
+	{
233
+		$tags = $this->UpdateTags();
234
+
235
+		$processed = ArrayList::create();
236
+
237
+		foreach ($tags as $tag) {
238
+			// Build the link - keep the tag, and date range, but reset month, year and pagination.
239
+			$link = HTTP::setGetVar('tag', $tag->ID, null, '&');
240
+			$link = HTTP::setGetVar('month', null, $link, '&');
241
+			$link = HTTP::setGetVar('year', null, $link, '&');
242
+			$link = HTTP::setGetVar('start', null, $link, '&');
243
+
244
+			$tag->Link = $link;
245
+			$processed->push($tag);
246
+		}
247
+
248
+		return $processed;
249
+	}
250
+
251
+	/**
252
+	 * Get the TaxonomyTerm related to the current tag GET parameter.
253
+	 */
254
+	public function CurrentTag()
255
+	{
256
+		$tagID = $this->request->getVar('tag');
257
+
258
+		if (isset($tagID)) {
259
+			return TaxonomyTerm::get_by_id(TaxonomyTerm::class, (int)$tagID);
260
+		}
261
+	}
262
+
263
+	/**
264
+	 * Extract the available months based on the current query.
265
+	 * Only tag is respected. Pagination and months are ignored.
266
+	 */
267
+	public function AvailableMonths()
268
+	{
269
+		$params = $this->parseParams();
270
+
271
+		return DatedUpdateHolder::ExtractMonths(
272
+			$this->Updates($params['tag'], $params['from'], $params['to']),
273
+			Director::makeRelative($_SERVER['REQUEST_URI']),
274
+			$params['year'],
275
+			$params['month']
276
+		);
277
+	}
278
+
279
+	/**
280
+	 * Get the updates based on the current query.
281
+	 */
282
+	public function FilteredUpdates($pageSize = 20)
283
+	{
284
+		$params = $this->parseParams();
285
+
286
+		$items = $this->Updates(
287
+			$params['tag'],
288
+			$params['from'],
289
+			$params['to'],
290
+			$params['year'],
291
+			$params['month']
292
+		);
293
+
294
+		// Apply pagination
295
+		$list = PaginatedList::create($items, $this->getRequest());
296
+		$list->setPageLength($pageSize);
297
+		return $list;
298
+	}
299
+
300
+	/**
301
+	 * @return Form
302
+	 */
303
+	public function DateRangeForm()
304
+	{
305
+		$dateFromTitle = DBField::create_field('HTMLText', sprintf(
306
+			'%s <span class="field-note">%s</span>',
307
+			_t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.FROM_DATE', 'From date'),
308
+			_t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 2017/12/30)')
309
+		));
310
+		$dateToTitle = DBField::create_field('HTMLText', sprintf(
311
+			'%s <span class="field-note">%s</span>',
312
+			_t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.TO_DATE', 'To date'),
313
+			_t('CWP\\CWP\\PageTypes\\DatedUpdateHolder.DATE_EXAMPLE', '(example: 2017/12/30)')
314
+		));
315
+
316
+		$fields = FieldList::create(
317
+			DateField::create('from', $dateFromTitle),
318
+			DateField::create('to', $dateToTitle),
319
+			HiddenField::create('tag')
320
+		);
321
+
322
+		$actions = FieldList::create(
323
+			FormAction::create("doDateFilter")
324
+				->setTitle(_t(__CLASS__ . '.Filter', 'Filter'))
325
+				->addExtraClass('btn btn-primary primary'),
326
+			FormAction::create("doDateReset")
327
+				->setTitle(_t(__CLASS__ . '.Clear', 'Clear'))
328
+				->addExtraClass('btn')
329
+		);
330
+
331
+		$form = Form::create($this, 'DateRangeForm', $fields, $actions);
332
+		$form->loadDataFrom($this->request->getVars());
333
+		$form->setFormMethod('get');
334
+
335
+		// Manually extract the message so we can clear it.
336
+		$form->ErrorMessage = $form->getMessage();
337
+		$form->ErrorMessageType = $form->getMessageType();
338
+		$form->clearMessage();
339
+
340
+		return $form;
341
+	}
342
+
343
+	public function doDateFilter()
344
+	{
345
+		$params = $this->parseParams();
346
+
347
+		// Build the link - keep the tag, but reset month, year and pagination.
348
+		$link = HTTP::setGetVar('from', $params['from'], $this->AbsoluteLink(), '&');
349
+		$link = HTTP::setGetVar('to', $params['to'], $link, '&');
350
+		if (isset($params['tag'])) {
351
+			$link = HTTP::setGetVar('tag', $params['tag'], $link, '&');
352
+		}
353
+
354
+		$this->redirect($link);
355
+	}
356
+
357
+	public function doDateReset()
358
+	{
359
+		$params = $this->parseParams(false);
360
+
361
+		// Reset the link - only include the tag.
362
+		$link = $this->AbsoluteLink();
363
+		if (isset($params['tag'])) {
364
+			$link = HTTP::setGetVar('tag', $params['tag'], $link, '&');
365
+		}
366
+
367
+		$this->redirect($link);
368
+	}
369
+
370
+	public function rss()
371
+	{
372
+		$rss = RSSFeed::create(
373
+			$this->Updates()->sort('Created DESC')->limit(20),
374
+			$this->Link('rss'),
375
+			$this->getSubscriptionTitle()
376
+		);
377
+		return $rss->outputToBrowser();
378
+	}
379
+
380
+	public function atom()
381
+	{
382
+		$atom = CwpAtomFeed::create(
383
+			$this->Updates()->sort('Created DESC')->limit(20),
384
+			$this->Link('atom'),
385
+			$this->getSubscriptionTitle()
386
+		);
387
+		return $atom->outputToBrowser();
388
+	}
389 389
 }
Please login to merge, or discard this patch.
src/PageTypes/BasePageController.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -13,163 +13,163 @@
 block discarded – undo
13 13
 
14 14
 class BasePageController extends ContentController
15 15
 {
16
-    private static $allowed_actions = [
17
-        'downloadpdf',
18
-    ];
19
-
20
-    /**
21
-     * Serve the page rendered as PDF.
22
-     */
23
-    public function downloadpdf()
24
-    {
25
-        if (!Config::inst()->get(BasePage::class, 'pdf_export')) {
26
-            return false;
27
-        }
28
-
29
-        // We only allow producing live pdf. There is no way to secure the draft files.
30
-        Versioned::set_stage(Versioned::LIVE);
31
-
32
-        $path = $this->dataRecord->getPdfFilename();
33
-        if (!file_exists($path)) {
34
-            $this->generatePDF();
35
-        }
36
-
37
-        return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf');
38
-    }
39
-
40
-    /*
16
+	private static $allowed_actions = [
17
+		'downloadpdf',
18
+	];
19
+
20
+	/**
21
+	 * Serve the page rendered as PDF.
22
+	 */
23
+	public function downloadpdf()
24
+	{
25
+		if (!Config::inst()->get(BasePage::class, 'pdf_export')) {
26
+			return false;
27
+		}
28
+
29
+		// We only allow producing live pdf. There is no way to secure the draft files.
30
+		Versioned::set_stage(Versioned::LIVE);
31
+
32
+		$path = $this->dataRecord->getPdfFilename();
33
+		if (!file_exists($path)) {
34
+			$this->generatePDF();
35
+		}
36
+
37
+		return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf');
38
+	}
39
+
40
+	/*
41 41
     * This will return either pdf_base_url from YML, CWP_SECURE_DOMAIN
42 42
     * from _ss_environment, or blank. In that order of importance.
43 43
     */
44
-    public function getPDFBaseURL()
45
-    {
46
-        //if base url YML is defined in YML, use that
47
-        if (Config::inst()->get(BasePage::class, 'pdf_base_url')) {
48
-            $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/';
49
-            //otherwise, if we are CWP use the secure domain
50
-        } elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) {
51
-            $pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/';
52
-            //or if neither, leave blank
53
-        } else {
54
-            $pdfBaseUrl = '';
55
-        }
56
-        return $pdfBaseUrl;
57
-    }
58
-
59
-    /*
44
+	public function getPDFBaseURL()
45
+	{
46
+		//if base url YML is defined in YML, use that
47
+		if (Config::inst()->get(BasePage::class, 'pdf_base_url')) {
48
+			$pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/';
49
+			//otherwise, if we are CWP use the secure domain
50
+		} elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) {
51
+			$pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/';
52
+			//or if neither, leave blank
53
+		} else {
54
+			$pdfBaseUrl = '';
55
+		}
56
+		return $pdfBaseUrl;
57
+	}
58
+
59
+	/*
60 60
     * Don't use the proxy if the pdf domain is the CWP secure domain
61 61
     * Or if we aren't on a CWP server
62 62
     */
63
-    public function getPDFProxy($pdfBaseUrl)
64
-    {
65
-        if (!Environment::getEnv('CWP_SECURE_DOMAIN')
66
-            || $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/'
67
-        ) {
68
-            $proxy = '';
69
-        } else {
70
-            $proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY')
71
-                . ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT');
72
-        }
73
-        return $proxy;
74
-    }
75
-
76
-    /**
77
-     * Render the page as PDF using wkhtmltopdf.
78
-     */
79
-    public function generatePDF()
80
-    {
81
-        if (!Config::inst()->get(BasePage::class, 'pdf_export')) {
82
-            return false;
83
-        }
84
-
85
-        $binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary');
86
-        if (!$binaryPath || !is_executable($binaryPath)) {
87
-            if (Environment::getEnv('WKHTMLTOPDF_BINARY')
88
-                && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY'))
89
-            ) {
90
-                $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY');
91
-            }
92
-        }
93
-
94
-        if (!$binaryPath) {
95
-            user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR);
96
-        }
97
-
98
-        if (Versioned::get_reading_mode() == 'Stage.Stage') {
99
-            user_error('Generating PDFs on draft is not supported', E_USER_ERROR);
100
-        }
101
-
102
-        set_time_limit(60);
103
-
104
-        // prepare the paths
105
-        $pdfFile = $this->dataRecord->getPdfFilename();
106
-        $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile);
107
-        $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile);
108
-
109
-        // make sure the work directory exists
110
-        if (!file_exists(dirname($pdfFile))) {
111
-            Filesystem::makeFolder(dirname($pdfFile));
112
-        }
113
-
114
-        //decide the domain to use in generation
115
-        $pdfBaseUrl = $this->getPDFBaseURL();
116
-
117
-        // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway.
118
-        if (Environment::getEnv('CWP_ENVIRONMENT')) {
119
-            Config::modify()->set(Director::class, 'alternate_protocol', 'http');
120
-            //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is
121
-            if ($pdfBaseUrl) {
122
-                Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl);
123
-            }
124
-        }
125
-
126
-        $bodyViewer = $this->getViewer('pdf');
127
-
128
-        // write the output of this page to HTML, ready for conversion to PDF
129
-        file_put_contents($bodyFile, $bodyViewer->process($this));
130
-
131
-        // get the viewer for the current template with _pdffooter
132
-        $footerViewer = $this->getViewer('pdffooter');
133
-
134
-        // write the output of the footer template to HTML, ready for conversion to PDF
135
-        file_put_contents($footerFile, $footerViewer->process($this));
136
-
137
-        //decide what the proxy should look like
138
-        $proxy = $this->getPDFProxy($pdfBaseUrl);
139
-
140
-        // finally, generate the PDF
141
-        $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 '
142
-            . '--orientation Portrait --disable-javascript --quiet --print-media-type ';
143
-        $retVal = 0;
144
-        $output = array();
145
-        exec(
146
-            $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout",
147
-            $output,
148
-            $retVal
149
-        );
150
-
151
-        // remove temporary file
152
-        unlink($bodyFile);
153
-        unlink($footerFile);
154
-
155
-        // output any errors
156
-        if ($retVal != 0) {
157
-            user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR);
158
-        }
159
-
160
-        // serve the generated file
161
-        return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf');
162
-    }
163
-
164
-    /**
165
-     * Provide current year.
166
-     */
167
-    public function CurrentDatetime()
168
-    {
169
-        return DBDatetime::now();
170
-    }
171
-
172
-    public function getRSSLink()
173
-    {
174
-    }
63
+	public function getPDFProxy($pdfBaseUrl)
64
+	{
65
+		if (!Environment::getEnv('CWP_SECURE_DOMAIN')
66
+			|| $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/'
67
+		) {
68
+			$proxy = '';
69
+		} else {
70
+			$proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY')
71
+				. ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT');
72
+		}
73
+		return $proxy;
74
+	}
75
+
76
+	/**
77
+	 * Render the page as PDF using wkhtmltopdf.
78
+	 */
79
+	public function generatePDF()
80
+	{
81
+		if (!Config::inst()->get(BasePage::class, 'pdf_export')) {
82
+			return false;
83
+		}
84
+
85
+		$binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary');
86
+		if (!$binaryPath || !is_executable($binaryPath)) {
87
+			if (Environment::getEnv('WKHTMLTOPDF_BINARY')
88
+				&& is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY'))
89
+			) {
90
+				$binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY');
91
+			}
92
+		}
93
+
94
+		if (!$binaryPath) {
95
+			user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR);
96
+		}
97
+
98
+		if (Versioned::get_reading_mode() == 'Stage.Stage') {
99
+			user_error('Generating PDFs on draft is not supported', E_USER_ERROR);
100
+		}
101
+
102
+		set_time_limit(60);
103
+
104
+		// prepare the paths
105
+		$pdfFile = $this->dataRecord->getPdfFilename();
106
+		$bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile);
107
+		$footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile);
108
+
109
+		// make sure the work directory exists
110
+		if (!file_exists(dirname($pdfFile))) {
111
+			Filesystem::makeFolder(dirname($pdfFile));
112
+		}
113
+
114
+		//decide the domain to use in generation
115
+		$pdfBaseUrl = $this->getPDFBaseURL();
116
+
117
+		// Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway.
118
+		if (Environment::getEnv('CWP_ENVIRONMENT')) {
119
+			Config::modify()->set(Director::class, 'alternate_protocol', 'http');
120
+			//only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is
121
+			if ($pdfBaseUrl) {
122
+				Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl);
123
+			}
124
+		}
125
+
126
+		$bodyViewer = $this->getViewer('pdf');
127
+
128
+		// write the output of this page to HTML, ready for conversion to PDF
129
+		file_put_contents($bodyFile, $bodyViewer->process($this));
130
+
131
+		// get the viewer for the current template with _pdffooter
132
+		$footerViewer = $this->getViewer('pdffooter');
133
+
134
+		// write the output of the footer template to HTML, ready for conversion to PDF
135
+		file_put_contents($footerFile, $footerViewer->process($this));
136
+
137
+		//decide what the proxy should look like
138
+		$proxy = $this->getPDFProxy($pdfBaseUrl);
139
+
140
+		// finally, generate the PDF
141
+		$command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 '
142
+			. '--orientation Portrait --disable-javascript --quiet --print-media-type ';
143
+		$retVal = 0;
144
+		$output = array();
145
+		exec(
146
+			$command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout",
147
+			$output,
148
+			$retVal
149
+		);
150
+
151
+		// remove temporary file
152
+		unlink($bodyFile);
153
+		unlink($footerFile);
154
+
155
+		// output any errors
156
+		if ($retVal != 0) {
157
+			user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR);
158
+		}
159
+
160
+		// serve the generated file
161
+		return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf');
162
+	}
163
+
164
+	/**
165
+	 * Provide current year.
166
+	 */
167
+	public function CurrentDatetime()
168
+	{
169
+		return DBDatetime::now();
170
+	}
171
+
172
+	public function getRSSLink()
173
+	{
174
+	}
175 175
 }
Please login to merge, or discard this patch.