Passed
Pull Request — master (#34)
by Russell
02:20
created
src/PageTypes/BasePage.php 1 patch
Indentation   +235 added lines, -235 removed lines patch added patch discarded remove patch
@@ -27,239 +27,239 @@
 block discarded – undo
27 27
 
28 28
 class BasePage extends SiteTree
29 29
 {
30
-    private static $icon = 'cwp/cwp:images/icons/sitetree_images/page.png';
31
-
32
-    /**
33
-     * Hide this page type from the CMS. hide_ancestor is slightly misnamed, should really be just "hide"
34
-     *
35
-     * {@inheritDoc}
36
-     */
37
-    private static $hide_ancestor = BasePage::class;
38
-
39
-    /**
40
-     * @config
41
-     * @var bool
42
-     */
43
-    private static $pdf_export = false;
44
-
45
-    /**
46
-     * Domain to generate PDF's from, DOES not include protocol
47
-     * i.e. google.com not http://google.com
48
-     * @config
49
-     * @var string
50
-     */
51
-    private static $pdf_base_url = "";
52
-
53
-    /**
54
-     * Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases
55
-     * where multiple versions of the binary are available to choose from. This
56
-     * should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf)
57
-     * @see BasePage_Controller::generatePDF();
58
-     *
59
-     * @config
60
-     * @var string|null
61
-     */
62
-    private static $wkhtmltopdf_binary = null;
63
-
64
-    /**
65
-     * Where to store generated PDF files
66
-     *
67
-     * @config
68
-     * @var string
69
-     */
70
-    private static $generated_pdf_path = 'assets/_generated_pdfs';
71
-
72
-    private static $api_access = [
73
-        'view' => [
74
-            'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription',
75
-            'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'
76
-        ],
77
-        'edit' => [
78
-            'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription',
79
-            'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'
80
-        ],
81
-    ];
82
-
83
-    private static $table_name = 'BasePage';
84
-
85
-    /**
86
-     * @config
87
-     * @var string
88
-     */
89
-    private static $related_pages_title = 'Related pages';
90
-
91
-    private static $many_many = [
92
-        'Terms' => TaxonomyTerm::class,
93
-        'RelatedPages' => BasePage::class,
94
-    ];
95
-
96
-    private static $many_many_extraFields = [
97
-        'RelatedPages' => [
98
-            'SortOrder' => 'Int',
99
-        ],
100
-    ];
101
-
102
-    private static $plural_name = 'Base Pages';
103
-
104
-    /**
105
-     * Get the footer holder.
106
-     */
107
-    public function getFooter()
108
-    {
109
-        return FooterHolder::get_one(FooterHolder::class);
110
-    }
111
-
112
-    /**
113
-     * Return the full filename of the pdf file, including path & extension
114
-     */
115
-    public function getPdfFilename()
116
-    {
117
-        $baseName = sprintf('%s-%s', $this->URLSegment, $this->ID);
118
-
119
-        $folderPath = $this->config()->get('generated_pdf_path');
120
-        if ($folderPath[0] != '/') {
121
-            $folderPath = BASE_PATH . '/' . $folderPath;
122
-        }
123
-
124
-        return sprintf('%s/%s.pdf', $folderPath, $baseName);
125
-    }
126
-
127
-    /**
128
-     * Build pdf link for template.
129
-     */
130
-    public function PdfLink()
131
-    {
132
-        if (!$this->config()->get('pdf_export')) {
133
-            return false;
134
-        }
135
-
136
-        $path = $this->getPdfFilename();
137
-
138
-        if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) {
139
-            return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path));
140
-        }
141
-        return $this->Link('downloadpdf');
142
-    }
143
-
144
-    public function RelatedPages()
145
-    {
146
-        return $this->getManyManyComponents('RelatedPages')->sort('SortOrder');
147
-    }
148
-
149
-    public function RelatedPagesTitle()
150
-    {
151
-        return $this->config()->get('related_pages_title');
152
-    }
153
-
154
-    /**
155
-     * Remove linked pdf when publishing the page,
156
-     * as it would be out of date.
157
-     */
158
-    public function onAfterPublish()
159
-    {
160
-        $filepath = $this->getPdfFilename();
161
-        if (file_exists($filepath)) {
162
-            unlink($filepath);
163
-        }
164
-    }
165
-
166
-    /**
167
-     * Remove linked pdf when unpublishing the page,
168
-     * so it's no longer valid.
169
-     *
170
-     * @return boolean
171
-     */
172
-    public function doUnpublish()
173
-    {
174
-        if (!parent::doUnpublish()) {
175
-            return false;
176
-        }
177
-
178
-        $filepath = $this->getPdfFilename();
179
-        if (file_exists($filepath)) {
180
-            unlink($filepath);
181
-        }
182
-
183
-        return true;
184
-    }
185
-
186
-    public function getCMSFields()
187
-    {
188
-        $this->beforeUpdateCMSFields(function (FieldList $fields) {
189
-            // Related Pages
190
-            $components = GridFieldConfig_RelationEditor::create();
191
-            $components->removeComponentsByType(GridFieldAddNewButton::class);
192
-            $components->removeComponentsByType(GridFieldEditButton::class);
193
-            $components->removeComponentsByType(GridFieldFilterHeader::class);
194
-
195
-            /** @var GridFieldDataColumns $dataColumns */
196
-            $dataColumns = $components->getComponentByType(GridFieldDataColumns::class);
197
-            $dataColumns->setDisplayFields([
198
-                'Title' => _t(__CLASS__ . '.ColumnTitle', 'Title'),
199
-                'ClassName' => _t(__CLASS__ . '.ColumnPageType', 'Page Type')
200
-            ]);
201
-
202
-            $fields->findOrMakeTab(
203
-                'Root.RelatedPages',
204
-                _t(__CLASS__ . '.RelatedPages', 'Related pages')
205
-            );
206
-            $fields->addFieldToTab(
207
-                'Root.RelatedPages',
208
-                GridField::create(
209
-                    'RelatedPages',
210
-                    _t(__CLASS__ . '.RelatedPages', 'Related pages'),
211
-                    $this->RelatedPages(),
212
-                    $components
213
-                )
214
-            );
215
-
216
-            // Taxonomies - Unless they have their own 'Tags' field (such as in Blog, etc)
217
-            $hasMany = $this->hasMany();
218
-            $manyMany = $this->manyMany();
219
-            if (!isset($hasMany['Tags']) && !isset($manyMany['Tags'])) {
220
-                $components = GridFieldConfig_RelationEditor::create();
221
-                $components->removeComponentsByType(GridFieldAddNewButton::class);
222
-                $components->removeComponentsByType(GridFieldEditButton::class);
223
-
224
-                /** @var GridFieldAddExistingAutocompleter $autoCompleter */
225
-                $autoCompleter = $components->getComponentByType(GridFieldAddExistingAutocompleter::class);
226
-                $autoCompleter->setResultsFormat('$Name ($TaxonomyName)');
227
-
228
-                /** @var GridFieldDataColumns $dataColumns */
229
-                $dataColumns = $components->getComponentByType(GridFieldDataColumns::class);
230
-                $dataColumns->setDisplayFields([
231
-                    'Name' => _t(__CLASS__ . '.Term', 'Term'),
232
-                    'TaxonomyName' => _t(__CLASS__ . '.Taxonomy', 'Taxonomy')
233
-                ]);
234
-
235
-                $fields->findOrMakeTab('Root.Tags', _t(__CLASS__ . '.TagsTabTitle', 'Tags'));
236
-                $fields->addFieldToTab(
237
-                    'Root.Tags',
238
-                    TreeMultiselectField::create(
239
-                        'Terms',
240
-                        _t(__CLASS__ . '.Terms', 'Terms'),
241
-                        TaxonomyTerm::class
242
-                    )->setDescription(_t(__CLASS__ . '.TermsDescription', 'Click to search for additional terms'))
243
-                );
244
-            }
245
-        });
246
-        return parent::getCMSFields();
247
-    }
248
-
249
-    /**
250
-     * Returns the native language name for the selected locale/language, empty string if Fluent is not available
251
-     *
252
-     * @return string
253
-     */
254
-    public function getSelectedLanguage()
255
-    {
256
-        if (!class_exists(Locale::class) || !$this->hasMethod('Locales')) {
257
-            return '';
258
-        }
259
-
260
-        /** @var ArrayData $information */
261
-        $information = $this->LocaleInformation(FluentState::singleton()->getLocale());
262
-
263
-        return $information->LanguageNative;
264
-    }
30
+	private static $icon = 'cwp/cwp:images/icons/sitetree_images/page.png';
31
+
32
+	/**
33
+	 * Hide this page type from the CMS. hide_ancestor is slightly misnamed, should really be just "hide"
34
+	 *
35
+	 * {@inheritDoc}
36
+	 */
37
+	private static $hide_ancestor = BasePage::class;
38
+
39
+	/**
40
+	 * @config
41
+	 * @var bool
42
+	 */
43
+	private static $pdf_export = false;
44
+
45
+	/**
46
+	 * Domain to generate PDF's from, DOES not include protocol
47
+	 * i.e. google.com not http://google.com
48
+	 * @config
49
+	 * @var string
50
+	 */
51
+	private static $pdf_base_url = "";
52
+
53
+	/**
54
+	 * Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases
55
+	 * where multiple versions of the binary are available to choose from. This
56
+	 * should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf)
57
+	 * @see BasePage_Controller::generatePDF();
58
+	 *
59
+	 * @config
60
+	 * @var string|null
61
+	 */
62
+	private static $wkhtmltopdf_binary = null;
63
+
64
+	/**
65
+	 * Where to store generated PDF files
66
+	 *
67
+	 * @config
68
+	 * @var string
69
+	 */
70
+	private static $generated_pdf_path = 'assets/_generated_pdfs';
71
+
72
+	private static $api_access = [
73
+		'view' => [
74
+			'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription',
75
+			'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'
76
+		],
77
+		'edit' => [
78
+			'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription',
79
+			'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID'
80
+		],
81
+	];
82
+
83
+	private static $table_name = 'BasePage';
84
+
85
+	/**
86
+	 * @config
87
+	 * @var string
88
+	 */
89
+	private static $related_pages_title = 'Related pages';
90
+
91
+	private static $many_many = [
92
+		'Terms' => TaxonomyTerm::class,
93
+		'RelatedPages' => BasePage::class,
94
+	];
95
+
96
+	private static $many_many_extraFields = [
97
+		'RelatedPages' => [
98
+			'SortOrder' => 'Int',
99
+		],
100
+	];
101
+
102
+	private static $plural_name = 'Base Pages';
103
+
104
+	/**
105
+	 * Get the footer holder.
106
+	 */
107
+	public function getFooter()
108
+	{
109
+		return FooterHolder::get_one(FooterHolder::class);
110
+	}
111
+
112
+	/**
113
+	 * Return the full filename of the pdf file, including path & extension
114
+	 */
115
+	public function getPdfFilename()
116
+	{
117
+		$baseName = sprintf('%s-%s', $this->URLSegment, $this->ID);
118
+
119
+		$folderPath = $this->config()->get('generated_pdf_path');
120
+		if ($folderPath[0] != '/') {
121
+			$folderPath = BASE_PATH . '/' . $folderPath;
122
+		}
123
+
124
+		return sprintf('%s/%s.pdf', $folderPath, $baseName);
125
+	}
126
+
127
+	/**
128
+	 * Build pdf link for template.
129
+	 */
130
+	public function PdfLink()
131
+	{
132
+		if (!$this->config()->get('pdf_export')) {
133
+			return false;
134
+		}
135
+
136
+		$path = $this->getPdfFilename();
137
+
138
+		if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) {
139
+			return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path));
140
+		}
141
+		return $this->Link('downloadpdf');
142
+	}
143
+
144
+	public function RelatedPages()
145
+	{
146
+		return $this->getManyManyComponents('RelatedPages')->sort('SortOrder');
147
+	}
148
+
149
+	public function RelatedPagesTitle()
150
+	{
151
+		return $this->config()->get('related_pages_title');
152
+	}
153
+
154
+	/**
155
+	 * Remove linked pdf when publishing the page,
156
+	 * as it would be out of date.
157
+	 */
158
+	public function onAfterPublish()
159
+	{
160
+		$filepath = $this->getPdfFilename();
161
+		if (file_exists($filepath)) {
162
+			unlink($filepath);
163
+		}
164
+	}
165
+
166
+	/**
167
+	 * Remove linked pdf when unpublishing the page,
168
+	 * so it's no longer valid.
169
+	 *
170
+	 * @return boolean
171
+	 */
172
+	public function doUnpublish()
173
+	{
174
+		if (!parent::doUnpublish()) {
175
+			return false;
176
+		}
177
+
178
+		$filepath = $this->getPdfFilename();
179
+		if (file_exists($filepath)) {
180
+			unlink($filepath);
181
+		}
182
+
183
+		return true;
184
+	}
185
+
186
+	public function getCMSFields()
187
+	{
188
+		$this->beforeUpdateCMSFields(function (FieldList $fields) {
189
+			// Related Pages
190
+			$components = GridFieldConfig_RelationEditor::create();
191
+			$components->removeComponentsByType(GridFieldAddNewButton::class);
192
+			$components->removeComponentsByType(GridFieldEditButton::class);
193
+			$components->removeComponentsByType(GridFieldFilterHeader::class);
194
+
195
+			/** @var GridFieldDataColumns $dataColumns */
196
+			$dataColumns = $components->getComponentByType(GridFieldDataColumns::class);
197
+			$dataColumns->setDisplayFields([
198
+				'Title' => _t(__CLASS__ . '.ColumnTitle', 'Title'),
199
+				'ClassName' => _t(__CLASS__ . '.ColumnPageType', 'Page Type')
200
+			]);
201
+
202
+			$fields->findOrMakeTab(
203
+				'Root.RelatedPages',
204
+				_t(__CLASS__ . '.RelatedPages', 'Related pages')
205
+			);
206
+			$fields->addFieldToTab(
207
+				'Root.RelatedPages',
208
+				GridField::create(
209
+					'RelatedPages',
210
+					_t(__CLASS__ . '.RelatedPages', 'Related pages'),
211
+					$this->RelatedPages(),
212
+					$components
213
+				)
214
+			);
215
+
216
+			// Taxonomies - Unless they have their own 'Tags' field (such as in Blog, etc)
217
+			$hasMany = $this->hasMany();
218
+			$manyMany = $this->manyMany();
219
+			if (!isset($hasMany['Tags']) && !isset($manyMany['Tags'])) {
220
+				$components = GridFieldConfig_RelationEditor::create();
221
+				$components->removeComponentsByType(GridFieldAddNewButton::class);
222
+				$components->removeComponentsByType(GridFieldEditButton::class);
223
+
224
+				/** @var GridFieldAddExistingAutocompleter $autoCompleter */
225
+				$autoCompleter = $components->getComponentByType(GridFieldAddExistingAutocompleter::class);
226
+				$autoCompleter->setResultsFormat('$Name ($TaxonomyName)');
227
+
228
+				/** @var GridFieldDataColumns $dataColumns */
229
+				$dataColumns = $components->getComponentByType(GridFieldDataColumns::class);
230
+				$dataColumns->setDisplayFields([
231
+					'Name' => _t(__CLASS__ . '.Term', 'Term'),
232
+					'TaxonomyName' => _t(__CLASS__ . '.Taxonomy', 'Taxonomy')
233
+				]);
234
+
235
+				$fields->findOrMakeTab('Root.Tags', _t(__CLASS__ . '.TagsTabTitle', 'Tags'));
236
+				$fields->addFieldToTab(
237
+					'Root.Tags',
238
+					TreeMultiselectField::create(
239
+						'Terms',
240
+						_t(__CLASS__ . '.Terms', 'Terms'),
241
+						TaxonomyTerm::class
242
+					)->setDescription(_t(__CLASS__ . '.TermsDescription', 'Click to search for additional terms'))
243
+				);
244
+			}
245
+		});
246
+		return parent::getCMSFields();
247
+	}
248
+
249
+	/**
250
+	 * Returns the native language name for the selected locale/language, empty string if Fluent is not available
251
+	 *
252
+	 * @return string
253
+	 */
254
+	public function getSelectedLanguage()
255
+	{
256
+		if (!class_exists(Locale::class) || !$this->hasMethod('Locales')) {
257
+			return '';
258
+		}
259
+
260
+		/** @var ArrayData $information */
261
+		$information = $this->LocaleInformation(FluentState::singleton()->getLocale());
262
+
263
+		return $information->LanguageNative;
264
+	}
265 265
 }
Please login to merge, or discard this patch.