@@ -23,329 +23,329 @@ |
||
| 23 | 23 | |
| 24 | 24 | class BasePageController extends ContentController |
| 25 | 25 | { |
| 26 | - private static $allowed_actions = [ |
|
| 27 | - 'downloadpdf', |
|
| 28 | - 'SearchForm', |
|
| 29 | - 'results', |
|
| 30 | - ]; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * How many search results should be shown per-page? |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - public static $results_per_page = 10; |
|
| 37 | - |
|
| 38 | - public static $search_index_class = CwpSolrIndex::class; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * If spelling suggestions for searches are given, enable |
|
| 42 | - * suggested searches to be followed immediately |
|
| 43 | - * |
|
| 44 | - * @config |
|
| 45 | - * @var bool |
|
| 46 | - */ |
|
| 47 | - private static $search_follow_suggestions = true; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Which classes should be queried when searching? |
|
| 51 | - * @var array |
|
| 52 | - */ |
|
| 53 | - public static $classes_to_search = [ |
|
| 54 | - [ |
|
| 55 | - 'class' => 'Page', |
|
| 56 | - 'includeSubclasses' => true, |
|
| 57 | - ] |
|
| 58 | - ]; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Serve the page rendered as PDF. |
|
| 62 | - */ |
|
| 63 | - public function downloadpdf() |
|
| 64 | - { |
|
| 65 | - if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 66 | - return false; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // We only allow producing live pdf. There is no way to secure the draft files. |
|
| 70 | - Versioned::set_stage(Versioned::LIVE); |
|
| 71 | - |
|
| 72 | - $path = $this->dataRecord->getPdfFilename(); |
|
| 73 | - if (!file_exists($path)) { |
|
| 74 | - $this->generatePDF(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /* |
|
| 26 | + private static $allowed_actions = [ |
|
| 27 | + 'downloadpdf', |
|
| 28 | + 'SearchForm', |
|
| 29 | + 'results', |
|
| 30 | + ]; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * How many search results should be shown per-page? |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + public static $results_per_page = 10; |
|
| 37 | + |
|
| 38 | + public static $search_index_class = CwpSolrIndex::class; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * If spelling suggestions for searches are given, enable |
|
| 42 | + * suggested searches to be followed immediately |
|
| 43 | + * |
|
| 44 | + * @config |
|
| 45 | + * @var bool |
|
| 46 | + */ |
|
| 47 | + private static $search_follow_suggestions = true; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Which classes should be queried when searching? |
|
| 51 | + * @var array |
|
| 52 | + */ |
|
| 53 | + public static $classes_to_search = [ |
|
| 54 | + [ |
|
| 55 | + 'class' => 'Page', |
|
| 56 | + 'includeSubclasses' => true, |
|
| 57 | + ] |
|
| 58 | + ]; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Serve the page rendered as PDF. |
|
| 62 | + */ |
|
| 63 | + public function downloadpdf() |
|
| 64 | + { |
|
| 65 | + if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 66 | + return false; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // We only allow producing live pdf. There is no way to secure the draft files. |
|
| 70 | + Versioned::set_stage(Versioned::LIVE); |
|
| 71 | + |
|
| 72 | + $path = $this->dataRecord->getPdfFilename(); |
|
| 73 | + if (!file_exists($path)) { |
|
| 74 | + $this->generatePDF(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /* |
|
| 81 | 81 | * This will return either pdf_base_url from YML, CWP_SECURE_DOMAIN |
| 82 | 82 | * from _ss_environment, or blank. In that order of importance. |
| 83 | 83 | */ |
| 84 | - public function getPDFBaseURL() |
|
| 85 | - { |
|
| 86 | - //if base url YML is defined in YML, use that |
|
| 87 | - if (Config::inst()->get(BasePage::class, 'pdf_base_url')) { |
|
| 88 | - $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/'; |
|
| 89 | - //otherwise, if we are CWP use the secure domain |
|
| 90 | - } elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) { |
|
| 91 | - $pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/'; |
|
| 92 | - //or if neither, leave blank |
|
| 93 | - } else { |
|
| 94 | - $pdfBaseUrl = ''; |
|
| 95 | - } |
|
| 96 | - return $pdfBaseUrl; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /* |
|
| 84 | + public function getPDFBaseURL() |
|
| 85 | + { |
|
| 86 | + //if base url YML is defined in YML, use that |
|
| 87 | + if (Config::inst()->get(BasePage::class, 'pdf_base_url')) { |
|
| 88 | + $pdfBaseUrl = Config::inst()->get(BasePage::class, 'pdf_base_url').'/'; |
|
| 89 | + //otherwise, if we are CWP use the secure domain |
|
| 90 | + } elseif (Environment::getEnv('CWP_SECURE_DOMAIN')) { |
|
| 91 | + $pdfBaseUrl = Environment::getEnv('CWP_SECURE_DOMAIN') . '/'; |
|
| 92 | + //or if neither, leave blank |
|
| 93 | + } else { |
|
| 94 | + $pdfBaseUrl = ''; |
|
| 95 | + } |
|
| 96 | + return $pdfBaseUrl; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /* |
|
| 100 | 100 | * Don't use the proxy if the pdf domain is the CWP secure domain |
| 101 | 101 | * Or if we aren't on a CWP server |
| 102 | 102 | */ |
| 103 | - public function getPDFProxy($pdfBaseUrl) |
|
| 104 | - { |
|
| 105 | - if (!Environment::getEnv('CWP_SECURE_DOMAIN') |
|
| 106 | - || $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/' |
|
| 107 | - ) { |
|
| 108 | - $proxy = ''; |
|
| 109 | - } else { |
|
| 110 | - $proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 111 | - . ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 112 | - } |
|
| 113 | - return $proxy; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Render the page as PDF using wkhtmltopdf. |
|
| 118 | - */ |
|
| 119 | - public function generatePDF() |
|
| 120 | - { |
|
| 121 | - if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary'); |
|
| 126 | - if (!$binaryPath || !is_executable($binaryPath)) { |
|
| 127 | - if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
|
| 128 | - && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
|
| 129 | - ) { |
|
| 130 | - $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (!$binaryPath) { |
|
| 135 | - user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if (Versioned::get_reading_mode() == 'Stage.Stage') { |
|
| 139 | - user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - set_time_limit(60); |
|
| 143 | - |
|
| 144 | - // prepare the paths |
|
| 145 | - $pdfFile = $this->dataRecord->getPdfFilename(); |
|
| 146 | - $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
|
| 147 | - $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
|
| 148 | - |
|
| 149 | - // make sure the work directory exists |
|
| 150 | - if (!file_exists(dirname($pdfFile))) { |
|
| 151 | - Filesystem::makeFolder(dirname($pdfFile)); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - //decide the domain to use in generation |
|
| 155 | - $pdfBaseUrl = $this->getPDFBaseURL(); |
|
| 156 | - |
|
| 157 | - // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
|
| 158 | - if (Environment::getEnv('CWP_ENVIRONMENT')) { |
|
| 159 | - Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 160 | - //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
|
| 161 | - if ($pdfBaseUrl) { |
|
| 162 | - Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $bodyViewer = $this->getViewer('pdf'); |
|
| 167 | - |
|
| 168 | - // write the output of this page to HTML, ready for conversion to PDF |
|
| 169 | - file_put_contents($bodyFile, $bodyViewer->process($this)); |
|
| 170 | - |
|
| 171 | - // get the viewer for the current template with _pdffooter |
|
| 172 | - $footerViewer = $this->getViewer('pdffooter'); |
|
| 173 | - |
|
| 174 | - // write the output of the footer template to HTML, ready for conversion to PDF |
|
| 175 | - file_put_contents($footerFile, $footerViewer->process($this)); |
|
| 176 | - |
|
| 177 | - //decide what the proxy should look like |
|
| 178 | - $proxy = $this->getPDFProxy($pdfBaseUrl); |
|
| 179 | - |
|
| 180 | - // finally, generate the PDF |
|
| 181 | - $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
|
| 182 | - . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
|
| 183 | - $retVal = 0; |
|
| 184 | - $output = array(); |
|
| 185 | - exec( |
|
| 186 | - $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
|
| 187 | - $output, |
|
| 188 | - $retVal |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - // remove temporary file |
|
| 192 | - unlink($bodyFile); |
|
| 193 | - unlink($footerFile); |
|
| 194 | - |
|
| 195 | - // output any errors |
|
| 196 | - if ($retVal != 0) { |
|
| 197 | - user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // serve the generated file |
|
| 201 | - return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Site search form |
|
| 206 | - */ |
|
| 207 | - public function SearchForm() |
|
| 208 | - { |
|
| 209 | - $searchText = $this->getRequest()->getVar('Search'); |
|
| 210 | - |
|
| 211 | - $fields = FieldList::create( |
|
| 212 | - TextField::create('Search', false, $searchText) |
|
| 213 | - ); |
|
| 214 | - $actions = FieldList::create( |
|
| 215 | - FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go')) |
|
| 216 | - ); |
|
| 217 | - |
|
| 218 | - $form = SearchForm::create($this, SearchForm::class, $fields, $actions); |
|
| 219 | - $form->setFormAction('search/SearchForm'); |
|
| 220 | - |
|
| 221 | - return $form; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Get search form with _header suffix |
|
| 226 | - * |
|
| 227 | - * @return SearchForm |
|
| 228 | - */ |
|
| 229 | - public function HeaderSearchForm() |
|
| 230 | - { |
|
| 231 | - return $this->SearchForm()->setTemplate('SearchForm_header'); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Process and render search results. |
|
| 236 | - * |
|
| 237 | - * @param array $data The raw request data submitted by user |
|
| 238 | - * @param SearchForm $form The form instance that was submitted |
|
| 239 | - * @param HTTPRequest $request Request generated for this action |
|
| 240 | - * @return DBHTMLText |
|
| 241 | - */ |
|
| 242 | - public function results($data, $form, $request) |
|
| 243 | - { |
|
| 244 | - // Check parameters for terms, pagination, and if we should follow suggestions |
|
| 245 | - $keywords = empty($data['Search']) ? '' : $data['Search']; |
|
| 246 | - $start = isset($data['start']) ? $data['start'] : 0; |
|
| 247 | - $suggestions = isset($data['suggestions']) |
|
| 248 | - ? $data['suggestions'] |
|
| 249 | - : $this->config()->search_follow_suggestions; |
|
| 250 | - |
|
| 251 | - $results = CwpSearchEngine::create() |
|
| 252 | - ->search( |
|
| 253 | - $keywords, |
|
| 254 | - $this->getClassesToSearch(), |
|
| 255 | - $this->getSearchIndex(), |
|
| 256 | - $this->getSearchPageSize(), |
|
| 257 | - $start, |
|
| 258 | - $suggestions |
|
| 259 | - ); |
|
| 260 | - |
|
| 261 | - // Customise content with these results |
|
| 262 | - $properties = [ |
|
| 263 | - 'MetaTitle' => _t(__CLASS__ . '.MetaTitle', 'Search {keywords}', ['keywords' => $keywords]), |
|
| 264 | - 'NoSearchResults' => _t(__CLASS__ . '.NoResult', 'Sorry, your search query did not return any results.'), |
|
| 265 | - 'EmptySearch' => _t(__CLASS__ . '.EmptySearch', 'Search field empty, please enter your search query.'), |
|
| 266 | - 'PdfLink' => '', |
|
| 267 | - 'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results'), |
|
| 268 | - ]; |
|
| 269 | - $this->extend('updateSearchResults', $results, $properties); |
|
| 270 | - |
|
| 271 | - // Customise page |
|
| 272 | - $response = $this->customise($properties); |
|
| 273 | - if ($results) { |
|
| 274 | - $response = $response |
|
| 275 | - ->customise($results) |
|
| 276 | - ->customise(array( 'Results' => $results->getResults() )); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - // Render |
|
| 280 | - $templates = $this->getResultsTemplate($request); |
|
| 281 | - return $response->renderWith($templates); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Select the template to render search results with |
|
| 286 | - * |
|
| 287 | - * @param HTTPRequest $request |
|
| 288 | - * @return array |
|
| 289 | - */ |
|
| 290 | - protected function getResultsTemplate($request) |
|
| 291 | - { |
|
| 292 | - $templates = [Page::class . '_results', Page::class]; |
|
| 293 | - if ($request->getVar('format') == 'rss') { |
|
| 294 | - array_unshift($templates, 'Page_results_rss'); |
|
| 295 | - } |
|
| 296 | - if ($request->getVar('format') == 'atom') { |
|
| 297 | - array_unshift($templates, 'Page_results_atom'); |
|
| 298 | - } |
|
| 299 | - return $templates; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Provide current year. |
|
| 304 | - */ |
|
| 305 | - public function CurrentDatetime() |
|
| 306 | - { |
|
| 307 | - return DBDatetime::now(); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - public function getRSSLink() |
|
| 311 | - { |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Get the search index registered for this application |
|
| 316 | - * |
|
| 317 | - * @return CwpSearchIndex |
|
| 318 | - */ |
|
| 319 | - protected function getSearchIndex() |
|
| 320 | - { |
|
| 321 | - // Will be a service name in 2.0 and returned via injector |
|
| 322 | - /** @var CwpSearchIndex $index */ |
|
| 323 | - $index = null; |
|
| 324 | - if (self::$search_index_class) { |
|
| 325 | - $index = Injector::inst()->get(self::$search_index_class); |
|
| 326 | - } |
|
| 327 | - return $index; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Gets the list of configured classes to search |
|
| 332 | - * |
|
| 333 | - * @return array |
|
| 334 | - */ |
|
| 335 | - protected function getClassesToSearch() |
|
| 336 | - { |
|
| 337 | - // Will be private static config in 2.0 |
|
| 338 | - return self::$classes_to_search; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get page size for search |
|
| 343 | - * |
|
| 344 | - * @return int |
|
| 345 | - */ |
|
| 346 | - protected function getSearchPageSize() |
|
| 347 | - { |
|
| 348 | - // Will be private static config in 2.0 |
|
| 349 | - return self::$results_per_page; |
|
| 350 | - } |
|
| 103 | + public function getPDFProxy($pdfBaseUrl) |
|
| 104 | + { |
|
| 105 | + if (!Environment::getEnv('CWP_SECURE_DOMAIN') |
|
| 106 | + || $pdfBaseUrl == Environment::getEnv('CWP_SECURE_DOMAIN') . '/' |
|
| 107 | + ) { |
|
| 108 | + $proxy = ''; |
|
| 109 | + } else { |
|
| 110 | + $proxy = ' --proxy ' . Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 111 | + . ':' . Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 112 | + } |
|
| 113 | + return $proxy; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Render the page as PDF using wkhtmltopdf. |
|
| 118 | + */ |
|
| 119 | + public function generatePDF() |
|
| 120 | + { |
|
| 121 | + if (!Config::inst()->get(BasePage::class, 'pdf_export')) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $binaryPath = Config::inst()->get(BasePage::class, 'wkhtmltopdf_binary'); |
|
| 126 | + if (!$binaryPath || !is_executable($binaryPath)) { |
|
| 127 | + if (Environment::getEnv('WKHTMLTOPDF_BINARY') |
|
| 128 | + && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) |
|
| 129 | + ) { |
|
| 130 | + $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (!$binaryPath) { |
|
| 135 | + user_error('Neither WKHTMLTOPDF_BINARY nor BasePage.wkhtmltopdf_binary are defined', E_USER_ERROR); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if (Versioned::get_reading_mode() == 'Stage.Stage') { |
|
| 139 | + user_error('Generating PDFs on draft is not supported', E_USER_ERROR); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + set_time_limit(60); |
|
| 143 | + |
|
| 144 | + // prepare the paths |
|
| 145 | + $pdfFile = $this->dataRecord->getPdfFilename(); |
|
| 146 | + $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); |
|
| 147 | + $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); |
|
| 148 | + |
|
| 149 | + // make sure the work directory exists |
|
| 150 | + if (!file_exists(dirname($pdfFile))) { |
|
| 151 | + Filesystem::makeFolder(dirname($pdfFile)); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + //decide the domain to use in generation |
|
| 155 | + $pdfBaseUrl = $this->getPDFBaseURL(); |
|
| 156 | + |
|
| 157 | + // Force http protocol on CWP - fetching from localhost without using the proxy, SSL terminates on gateway. |
|
| 158 | + if (Environment::getEnv('CWP_ENVIRONMENT')) { |
|
| 159 | + Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 160 | + //only set alternate protocol if CWP_SECURE_DOMAIN is defined OR pdf_base_url is |
|
| 161 | + if ($pdfBaseUrl) { |
|
| 162 | + Config::modify()->set(Director::class, 'alternate_base_url', 'http://' . $pdfBaseUrl); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $bodyViewer = $this->getViewer('pdf'); |
|
| 167 | + |
|
| 168 | + // write the output of this page to HTML, ready for conversion to PDF |
|
| 169 | + file_put_contents($bodyFile, $bodyViewer->process($this)); |
|
| 170 | + |
|
| 171 | + // get the viewer for the current template with _pdffooter |
|
| 172 | + $footerViewer = $this->getViewer('pdffooter'); |
|
| 173 | + |
|
| 174 | + // write the output of the footer template to HTML, ready for conversion to PDF |
|
| 175 | + file_put_contents($footerFile, $footerViewer->process($this)); |
|
| 176 | + |
|
| 177 | + //decide what the proxy should look like |
|
| 178 | + $proxy = $this->getPDFProxy($pdfBaseUrl); |
|
| 179 | + |
|
| 180 | + // finally, generate the PDF |
|
| 181 | + $command = $binaryPath . $proxy . ' --outline -B 40pt -L 20pt -R 20pt -T 20pt --encoding utf-8 ' |
|
| 182 | + . '--orientation Portrait --disable-javascript --quiet --print-media-type '; |
|
| 183 | + $retVal = 0; |
|
| 184 | + $output = array(); |
|
| 185 | + exec( |
|
| 186 | + $command . " --footer-html \"$footerFile\" \"$bodyFile\" \"$pdfFile\" &> /dev/stdout", |
|
| 187 | + $output, |
|
| 188 | + $retVal |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + // remove temporary file |
|
| 192 | + unlink($bodyFile); |
|
| 193 | + unlink($footerFile); |
|
| 194 | + |
|
| 195 | + // output any errors |
|
| 196 | + if ($retVal != 0) { |
|
| 197 | + user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // serve the generated file |
|
| 201 | + return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Site search form |
|
| 206 | + */ |
|
| 207 | + public function SearchForm() |
|
| 208 | + { |
|
| 209 | + $searchText = $this->getRequest()->getVar('Search'); |
|
| 210 | + |
|
| 211 | + $fields = FieldList::create( |
|
| 212 | + TextField::create('Search', false, $searchText) |
|
| 213 | + ); |
|
| 214 | + $actions = FieldList::create( |
|
| 215 | + FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go')) |
|
| 216 | + ); |
|
| 217 | + |
|
| 218 | + $form = SearchForm::create($this, SearchForm::class, $fields, $actions); |
|
| 219 | + $form->setFormAction('search/SearchForm'); |
|
| 220 | + |
|
| 221 | + return $form; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Get search form with _header suffix |
|
| 226 | + * |
|
| 227 | + * @return SearchForm |
|
| 228 | + */ |
|
| 229 | + public function HeaderSearchForm() |
|
| 230 | + { |
|
| 231 | + return $this->SearchForm()->setTemplate('SearchForm_header'); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Process and render search results. |
|
| 236 | + * |
|
| 237 | + * @param array $data The raw request data submitted by user |
|
| 238 | + * @param SearchForm $form The form instance that was submitted |
|
| 239 | + * @param HTTPRequest $request Request generated for this action |
|
| 240 | + * @return DBHTMLText |
|
| 241 | + */ |
|
| 242 | + public function results($data, $form, $request) |
|
| 243 | + { |
|
| 244 | + // Check parameters for terms, pagination, and if we should follow suggestions |
|
| 245 | + $keywords = empty($data['Search']) ? '' : $data['Search']; |
|
| 246 | + $start = isset($data['start']) ? $data['start'] : 0; |
|
| 247 | + $suggestions = isset($data['suggestions']) |
|
| 248 | + ? $data['suggestions'] |
|
| 249 | + : $this->config()->search_follow_suggestions; |
|
| 250 | + |
|
| 251 | + $results = CwpSearchEngine::create() |
|
| 252 | + ->search( |
|
| 253 | + $keywords, |
|
| 254 | + $this->getClassesToSearch(), |
|
| 255 | + $this->getSearchIndex(), |
|
| 256 | + $this->getSearchPageSize(), |
|
| 257 | + $start, |
|
| 258 | + $suggestions |
|
| 259 | + ); |
|
| 260 | + |
|
| 261 | + // Customise content with these results |
|
| 262 | + $properties = [ |
|
| 263 | + 'MetaTitle' => _t(__CLASS__ . '.MetaTitle', 'Search {keywords}', ['keywords' => $keywords]), |
|
| 264 | + 'NoSearchResults' => _t(__CLASS__ . '.NoResult', 'Sorry, your search query did not return any results.'), |
|
| 265 | + 'EmptySearch' => _t(__CLASS__ . '.EmptySearch', 'Search field empty, please enter your search query.'), |
|
| 266 | + 'PdfLink' => '', |
|
| 267 | + 'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results'), |
|
| 268 | + ]; |
|
| 269 | + $this->extend('updateSearchResults', $results, $properties); |
|
| 270 | + |
|
| 271 | + // Customise page |
|
| 272 | + $response = $this->customise($properties); |
|
| 273 | + if ($results) { |
|
| 274 | + $response = $response |
|
| 275 | + ->customise($results) |
|
| 276 | + ->customise(array( 'Results' => $results->getResults() )); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + // Render |
|
| 280 | + $templates = $this->getResultsTemplate($request); |
|
| 281 | + return $response->renderWith($templates); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Select the template to render search results with |
|
| 286 | + * |
|
| 287 | + * @param HTTPRequest $request |
|
| 288 | + * @return array |
|
| 289 | + */ |
|
| 290 | + protected function getResultsTemplate($request) |
|
| 291 | + { |
|
| 292 | + $templates = [Page::class . '_results', Page::class]; |
|
| 293 | + if ($request->getVar('format') == 'rss') { |
|
| 294 | + array_unshift($templates, 'Page_results_rss'); |
|
| 295 | + } |
|
| 296 | + if ($request->getVar('format') == 'atom') { |
|
| 297 | + array_unshift($templates, 'Page_results_atom'); |
|
| 298 | + } |
|
| 299 | + return $templates; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Provide current year. |
|
| 304 | + */ |
|
| 305 | + public function CurrentDatetime() |
|
| 306 | + { |
|
| 307 | + return DBDatetime::now(); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + public function getRSSLink() |
|
| 311 | + { |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Get the search index registered for this application |
|
| 316 | + * |
|
| 317 | + * @return CwpSearchIndex |
|
| 318 | + */ |
|
| 319 | + protected function getSearchIndex() |
|
| 320 | + { |
|
| 321 | + // Will be a service name in 2.0 and returned via injector |
|
| 322 | + /** @var CwpSearchIndex $index */ |
|
| 323 | + $index = null; |
|
| 324 | + if (self::$search_index_class) { |
|
| 325 | + $index = Injector::inst()->get(self::$search_index_class); |
|
| 326 | + } |
|
| 327 | + return $index; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Gets the list of configured classes to search |
|
| 332 | + * |
|
| 333 | + * @return array |
|
| 334 | + */ |
|
| 335 | + protected function getClassesToSearch() |
|
| 336 | + { |
|
| 337 | + // Will be private static config in 2.0 |
|
| 338 | + return self::$classes_to_search; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get page size for search |
|
| 343 | + * |
|
| 344 | + * @return int |
|
| 345 | + */ |
|
| 346 | + protected function getSearchPageSize() |
|
| 347 | + { |
|
| 348 | + // Will be private static config in 2.0 |
|
| 349 | + return self::$results_per_page; |
|
| 350 | + } |
|
| 351 | 351 | } |
@@ -7,42 +7,42 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SitemapPageController extends PageController |
| 9 | 9 | { |
| 10 | - private static $allowed_actions = [ |
|
| 11 | - 'showpage', |
|
| 12 | - ]; |
|
| 13 | - |
|
| 14 | - private static $url_handlers = [ |
|
| 15 | - 'page/$ID' => 'showpage', |
|
| 16 | - ]; |
|
| 17 | - |
|
| 18 | - public function showpage($request) |
|
| 19 | - { |
|
| 20 | - $id = (int) $request->param('ID'); |
|
| 21 | - if (!$id) { |
|
| 22 | - return false; |
|
| 23 | - } |
|
| 24 | - $page = SiteTree::get()->byId($id); |
|
| 25 | - |
|
| 26 | - // does the page exist? |
|
| 27 | - if (!($page && $page->exists())) { |
|
| 28 | - return $this->httpError(404); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - // can the page be viewed? |
|
| 32 | - if (!$page->canView()) { |
|
| 33 | - return $this->httpError(403); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - $viewer = $this->customise([ |
|
| 37 | - 'IsAjax' => $request->isAjax(), |
|
| 38 | - 'SelectedPage' => $page, |
|
| 39 | - 'Children' => $page->Children(), |
|
| 40 | - ]); |
|
| 41 | - |
|
| 42 | - if ($request->isAjax()) { |
|
| 43 | - return $viewer->renderWith('SitemapNodeChildren'); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - return $viewer; |
|
| 47 | - } |
|
| 10 | + private static $allowed_actions = [ |
|
| 11 | + 'showpage', |
|
| 12 | + ]; |
|
| 13 | + |
|
| 14 | + private static $url_handlers = [ |
|
| 15 | + 'page/$ID' => 'showpage', |
|
| 16 | + ]; |
|
| 17 | + |
|
| 18 | + public function showpage($request) |
|
| 19 | + { |
|
| 20 | + $id = (int) $request->param('ID'); |
|
| 21 | + if (!$id) { |
|
| 22 | + return false; |
|
| 23 | + } |
|
| 24 | + $page = SiteTree::get()->byId($id); |
|
| 25 | + |
|
| 26 | + // does the page exist? |
|
| 27 | + if (!($page && $page->exists())) { |
|
| 28 | + return $this->httpError(404); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + // can the page be viewed? |
|
| 32 | + if (!$page->canView()) { |
|
| 33 | + return $this->httpError(403); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + $viewer = $this->customise([ |
|
| 37 | + 'IsAjax' => $request->isAjax(), |
|
| 38 | + 'SelectedPage' => $page, |
|
| 39 | + 'Children' => $page->Children(), |
|
| 40 | + ]); |
|
| 41 | + |
|
| 42 | + if ($request->isAjax()) { |
|
| 43 | + return $viewer->renderWith('SitemapNodeChildren'); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + return $viewer; |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -28,302 +28,302 @@ |
||
| 28 | 28 | |
| 29 | 29 | class BasePage extends SiteTree |
| 30 | 30 | { |
| 31 | - private static $icon = 'cwp/cwp:images/icons/sitetree_images/page.png'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Hide this page type from the CMS. hide_ancestor is slightly misnamed, should really be just "hide" |
|
| 35 | - * |
|
| 36 | - * {@inheritDoc} |
|
| 37 | - */ |
|
| 38 | - private static $hide_ancestor = BasePage::class; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @config |
|
| 42 | - * @var bool |
|
| 43 | - */ |
|
| 44 | - private static $pdf_export = false; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Domain to generate PDF's from, DOES not include protocol |
|
| 48 | - * i.e. google.com not http://google.com |
|
| 49 | - * @config |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - private static $pdf_base_url = ""; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases |
|
| 56 | - * where multiple versions of the binary are available to choose from. This |
|
| 57 | - * should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf) |
|
| 58 | - * @see BasePage_Controller::generatePDF(); |
|
| 59 | - * |
|
| 60 | - * @config |
|
| 61 | - * @var string|null |
|
| 62 | - */ |
|
| 63 | - private static $wkhtmltopdf_binary = null; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Where to store generated PDF files |
|
| 67 | - * |
|
| 68 | - * @config |
|
| 69 | - * @var string |
|
| 70 | - */ |
|
| 71 | - private static $generated_pdf_path = 'assets/_generated_pdfs'; |
|
| 72 | - |
|
| 73 | - private static $api_access = [ |
|
| 74 | - 'view' => [ |
|
| 75 | - 'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription', |
|
| 76 | - 'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID' |
|
| 77 | - ], |
|
| 78 | - 'edit' => [ |
|
| 79 | - 'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription', |
|
| 80 | - 'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID' |
|
| 81 | - ], |
|
| 82 | - ]; |
|
| 83 | - |
|
| 84 | - private static $table_name = 'BasePage'; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @config |
|
| 88 | - * @var string |
|
| 89 | - */ |
|
| 90 | - private static $related_pages_title = 'Related pages'; |
|
| 91 | - |
|
| 92 | - private static $many_many = [ |
|
| 93 | - 'Terms' => TaxonomyTerm::class, |
|
| 94 | - 'RelatedPages' => BasePage::class, |
|
| 95 | - ]; |
|
| 96 | - |
|
| 97 | - private static $many_many_extraFields = [ |
|
| 98 | - 'RelatedPages' => [ |
|
| 99 | - 'SortOrder' => 'Int', |
|
| 100 | - ], |
|
| 101 | - ]; |
|
| 102 | - |
|
| 103 | - private static $plural_name = 'Base Pages'; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Get the footer holder. |
|
| 107 | - */ |
|
| 108 | - public function getFooter() |
|
| 109 | - { |
|
| 110 | - return FooterHolder::get_one(FooterHolder::class); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Return the full filename of the pdf file, including path & extension |
|
| 115 | - */ |
|
| 116 | - public function getPdfFilename() |
|
| 117 | - { |
|
| 118 | - $baseName = sprintf('%s-%s', $this->URLSegment, $this->ID); |
|
| 119 | - |
|
| 120 | - $folderPath = $this->config()->get('generated_pdf_path'); |
|
| 121 | - if ($folderPath[0] != '/') { |
|
| 122 | - $folderPath = BASE_PATH . '/' . $folderPath; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return sprintf('%s/%s.pdf', $folderPath, $baseName); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Build pdf link for template. |
|
| 130 | - */ |
|
| 131 | - public function PdfLink() |
|
| 132 | - { |
|
| 133 | - if (!$this->config()->get('pdf_export')) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $path = $this->getPdfFilename(); |
|
| 138 | - |
|
| 139 | - if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) { |
|
| 140 | - return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path)); |
|
| 141 | - } |
|
| 142 | - return $this->Link('downloadpdf'); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - public function RelatedPages() |
|
| 146 | - { |
|
| 147 | - return $this->getManyManyComponents('RelatedPages')->sort('SortOrder'); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function RelatedPagesTitle() |
|
| 151 | - { |
|
| 152 | - return $this->config()->get('related_pages_title'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Remove linked pdf when publishing the page, |
|
| 157 | - * as it would be out of date. |
|
| 158 | - */ |
|
| 159 | - public function onAfterPublish() |
|
| 160 | - { |
|
| 161 | - $filepath = $this->getPdfFilename(); |
|
| 162 | - if (file_exists($filepath)) { |
|
| 163 | - unlink($filepath); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Remove linked pdf when unpublishing the page, |
|
| 169 | - * so it's no longer valid. |
|
| 170 | - * |
|
| 171 | - * @return boolean |
|
| 172 | - */ |
|
| 173 | - public function doUnpublish() |
|
| 174 | - { |
|
| 175 | - if (!parent::doUnpublish()) { |
|
| 176 | - return false; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $filepath = $this->getPdfFilename(); |
|
| 180 | - if (file_exists($filepath)) { |
|
| 181 | - unlink($filepath); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return true; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - public function getCMSFields() |
|
| 188 | - { |
|
| 189 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 190 | - // Related Pages |
|
| 191 | - $components = GridFieldConfig_RelationEditor::create(); |
|
| 192 | - $components->removeComponentsByType(GridFieldAddNewButton::class); |
|
| 193 | - $components->removeComponentsByType(GridFieldEditButton::class); |
|
| 194 | - $components->removeComponentsByType(GridFieldFilterHeader::class); |
|
| 195 | - $components->addComponent(new GridFieldSortableRows('SortOrder')); |
|
| 196 | - |
|
| 197 | - /** @var GridFieldDataColumns $dataColumns */ |
|
| 198 | - $dataColumns = $components->getComponentByType(GridFieldDataColumns::class); |
|
| 199 | - $dataColumns->setDisplayFields([ |
|
| 200 | - 'Title' => _t(__CLASS__ . '.ColumnTitle', 'Title'), |
|
| 201 | - 'ClassName' => _t(__CLASS__ . '.ColumnPageType', 'Page Type') |
|
| 202 | - ]); |
|
| 203 | - |
|
| 204 | - $fields->findOrMakeTab( |
|
| 205 | - 'Root.RelatedPages', |
|
| 206 | - _t(__CLASS__ . '.RelatedPages', 'Related pages') |
|
| 207 | - ); |
|
| 208 | - $fields->addFieldToTab( |
|
| 209 | - 'Root.RelatedPages', |
|
| 210 | - GridField::create( |
|
| 211 | - 'RelatedPages', |
|
| 212 | - _t(__CLASS__ . '.RelatedPages', 'Related pages'), |
|
| 213 | - $this->RelatedPages(), |
|
| 214 | - $components |
|
| 215 | - ) |
|
| 216 | - ); |
|
| 217 | - |
|
| 218 | - // Taxonomies - Unless they have their own 'Tags' field (such as in Blog, etc) |
|
| 219 | - $hasMany = $this->hasMany(); |
|
| 220 | - $manyMany = $this->manyMany(); |
|
| 221 | - if (!isset($hasMany['Tags']) && !isset($manyMany['Tags'])) { |
|
| 222 | - $components = GridFieldConfig_RelationEditor::create(); |
|
| 223 | - $components->removeComponentsByType(GridFieldAddNewButton::class); |
|
| 224 | - $components->removeComponentsByType(GridFieldEditButton::class); |
|
| 225 | - |
|
| 226 | - /** @var GridFieldAddExistingAutocompleter $autoCompleter */ |
|
| 227 | - $autoCompleter = $components->getComponentByType(GridFieldAddExistingAutocompleter::class); |
|
| 228 | - $autoCompleter->setResultsFormat('$Name ($TaxonomyName)'); |
|
| 229 | - |
|
| 230 | - /** @var GridFieldDataColumns $dataColumns */ |
|
| 231 | - $dataColumns = $components->getComponentByType(GridFieldDataColumns::class); |
|
| 232 | - $dataColumns->setDisplayFields([ |
|
| 233 | - 'Name' => _t(__CLASS__ . '.Term', 'Term'), |
|
| 234 | - 'TaxonomyName' => _t(__CLASS__ . '.Taxonomy', 'Taxonomy') |
|
| 235 | - ]); |
|
| 236 | - |
|
| 237 | - $fields->findOrMakeTab('Root.Tags', _t(__CLASS__ . '.TagsTabTitle', 'Tags')); |
|
| 238 | - $fields->addFieldToTab( |
|
| 239 | - 'Root.Tags', |
|
| 240 | - TreeMultiselectField::create( |
|
| 241 | - 'Terms', |
|
| 242 | - _t(__CLASS__ . '.Terms', 'Terms'), |
|
| 243 | - TaxonomyTerm::class |
|
| 244 | - )->setDescription(_t(__CLASS__ . '.TermsDescription', 'Click to search for additional terms')) |
|
| 245 | - ); |
|
| 246 | - } |
|
| 247 | - }); |
|
| 248 | - return parent::getCMSFields(); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Provides data for translation navigation. |
|
| 253 | - * Collects all site translations, marks the current one, and redirects |
|
| 254 | - * to the translated home page if a. there is a translated homepage and b. the |
|
| 255 | - * translation of the specific page is not available. |
|
| 256 | - * @todo re-implement with Fluent |
|
| 257 | - */ |
|
| 258 | - public function getAvailableTranslations() |
|
| 259 | - { |
|
| 260 | - if (!class_exists('Translatable')) { |
|
| 261 | - return false; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - $translations = new ArrayList(); |
|
| 265 | - $globalTranslations = Translatable::get_existing_content_languages(); |
|
| 266 | - |
|
| 267 | - foreach ($globalTranslations as $loc => $langName) { |
|
| 268 | - // Find out the language name in native language. |
|
| 269 | - $nativeLangName = i18n::get_language_name($loc, true); |
|
| 270 | - if (!$nativeLangName) { |
|
| 271 | - $nativeLangName = i18n::get_language_name(i18n::get_lang_from_locale($loc), true); |
|
| 272 | - } |
|
| 273 | - if (!$nativeLangName) { |
|
| 274 | - // Fall back to the locale name. |
|
| 275 | - $nativeLangName = $langName; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // Eliminate the part in brackets (e.g. [mandarin]) |
|
| 279 | - $nativeLangName = preg_replace('/ *[\(\[].*$/', '', $nativeLangName); |
|
| 280 | - |
|
| 281 | - // Find out the link to the translated page. |
|
| 282 | - $link = null; |
|
| 283 | - $page = $this->getTranslation($loc); |
|
| 284 | - if ($page) { |
|
| 285 | - $link = $page->Link(); |
|
| 286 | - } |
|
| 287 | - if (!$link) { |
|
| 288 | - // Fall back to the home page |
|
| 289 | - $link = Translatable::get_homepage_link_by_locale($loc); |
|
| 290 | - } |
|
| 291 | - if (!$link) { |
|
| 292 | - continue; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - // Assemble the table for the switcher. |
|
| 296 | - $translations->push(new ArrayData(array( |
|
| 297 | - 'Locale' => i18n::convert_rfc1766($loc), |
|
| 298 | - 'LangName' => $nativeLangName, |
|
| 299 | - 'Link' => $link, |
|
| 300 | - 'Current' => (Translatable::get_current_locale()==$loc) |
|
| 301 | - ))); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - if ($translations->count()>1) { |
|
| 305 | - return $translations; |
|
| 306 | - } else { |
|
| 307 | - return null; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Returns the native language name for the selected locale/language, empty string if Translatable is not available |
|
| 313 | - * |
|
| 314 | - * @return string |
|
| 315 | - * @todo re-implement with Fluent |
|
| 316 | - */ |
|
| 317 | - public function getSelectedLanguage() |
|
| 318 | - { |
|
| 319 | - if (!class_exists('Translatable')) { |
|
| 320 | - return ''; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - $language = explode('_', Translatable::get_current_locale()); |
|
| 324 | - $languageCode = array_shift($language); |
|
| 325 | - $nativeName = i18n::get_language_name($languageCode, true); |
|
| 326 | - |
|
| 327 | - return $nativeName; |
|
| 328 | - } |
|
| 31 | + private static $icon = 'cwp/cwp:images/icons/sitetree_images/page.png'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Hide this page type from the CMS. hide_ancestor is slightly misnamed, should really be just "hide" |
|
| 35 | + * |
|
| 36 | + * {@inheritDoc} |
|
| 37 | + */ |
|
| 38 | + private static $hide_ancestor = BasePage::class; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @config |
|
| 42 | + * @var bool |
|
| 43 | + */ |
|
| 44 | + private static $pdf_export = false; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Domain to generate PDF's from, DOES not include protocol |
|
| 48 | + * i.e. google.com not http://google.com |
|
| 49 | + * @config |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + private static $pdf_base_url = ""; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases |
|
| 56 | + * where multiple versions of the binary are available to choose from. This |
|
| 57 | + * should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf) |
|
| 58 | + * @see BasePage_Controller::generatePDF(); |
|
| 59 | + * |
|
| 60 | + * @config |
|
| 61 | + * @var string|null |
|
| 62 | + */ |
|
| 63 | + private static $wkhtmltopdf_binary = null; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Where to store generated PDF files |
|
| 67 | + * |
|
| 68 | + * @config |
|
| 69 | + * @var string |
|
| 70 | + */ |
|
| 71 | + private static $generated_pdf_path = 'assets/_generated_pdfs'; |
|
| 72 | + |
|
| 73 | + private static $api_access = [ |
|
| 74 | + 'view' => [ |
|
| 75 | + 'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription', |
|
| 76 | + 'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID' |
|
| 77 | + ], |
|
| 78 | + 'edit' => [ |
|
| 79 | + 'Locale', 'URLSegment', 'Title', 'MenuTitle', 'Content', 'MetaDescription', |
|
| 80 | + 'ExtraMenu', 'Sort', 'Version', 'ParentID', 'ID' |
|
| 81 | + ], |
|
| 82 | + ]; |
|
| 83 | + |
|
| 84 | + private static $table_name = 'BasePage'; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @config |
|
| 88 | + * @var string |
|
| 89 | + */ |
|
| 90 | + private static $related_pages_title = 'Related pages'; |
|
| 91 | + |
|
| 92 | + private static $many_many = [ |
|
| 93 | + 'Terms' => TaxonomyTerm::class, |
|
| 94 | + 'RelatedPages' => BasePage::class, |
|
| 95 | + ]; |
|
| 96 | + |
|
| 97 | + private static $many_many_extraFields = [ |
|
| 98 | + 'RelatedPages' => [ |
|
| 99 | + 'SortOrder' => 'Int', |
|
| 100 | + ], |
|
| 101 | + ]; |
|
| 102 | + |
|
| 103 | + private static $plural_name = 'Base Pages'; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Get the footer holder. |
|
| 107 | + */ |
|
| 108 | + public function getFooter() |
|
| 109 | + { |
|
| 110 | + return FooterHolder::get_one(FooterHolder::class); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Return the full filename of the pdf file, including path & extension |
|
| 115 | + */ |
|
| 116 | + public function getPdfFilename() |
|
| 117 | + { |
|
| 118 | + $baseName = sprintf('%s-%s', $this->URLSegment, $this->ID); |
|
| 119 | + |
|
| 120 | + $folderPath = $this->config()->get('generated_pdf_path'); |
|
| 121 | + if ($folderPath[0] != '/') { |
|
| 122 | + $folderPath = BASE_PATH . '/' . $folderPath; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return sprintf('%s/%s.pdf', $folderPath, $baseName); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Build pdf link for template. |
|
| 130 | + */ |
|
| 131 | + public function PdfLink() |
|
| 132 | + { |
|
| 133 | + if (!$this->config()->get('pdf_export')) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $path = $this->getPdfFilename(); |
|
| 138 | + |
|
| 139 | + if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) { |
|
| 140 | + return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path)); |
|
| 141 | + } |
|
| 142 | + return $this->Link('downloadpdf'); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + public function RelatedPages() |
|
| 146 | + { |
|
| 147 | + return $this->getManyManyComponents('RelatedPages')->sort('SortOrder'); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + public function RelatedPagesTitle() |
|
| 151 | + { |
|
| 152 | + return $this->config()->get('related_pages_title'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Remove linked pdf when publishing the page, |
|
| 157 | + * as it would be out of date. |
|
| 158 | + */ |
|
| 159 | + public function onAfterPublish() |
|
| 160 | + { |
|
| 161 | + $filepath = $this->getPdfFilename(); |
|
| 162 | + if (file_exists($filepath)) { |
|
| 163 | + unlink($filepath); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Remove linked pdf when unpublishing the page, |
|
| 169 | + * so it's no longer valid. |
|
| 170 | + * |
|
| 171 | + * @return boolean |
|
| 172 | + */ |
|
| 173 | + public function doUnpublish() |
|
| 174 | + { |
|
| 175 | + if (!parent::doUnpublish()) { |
|
| 176 | + return false; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $filepath = $this->getPdfFilename(); |
|
| 180 | + if (file_exists($filepath)) { |
|
| 181 | + unlink($filepath); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return true; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + public function getCMSFields() |
|
| 188 | + { |
|
| 189 | + $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 190 | + // Related Pages |
|
| 191 | + $components = GridFieldConfig_RelationEditor::create(); |
|
| 192 | + $components->removeComponentsByType(GridFieldAddNewButton::class); |
|
| 193 | + $components->removeComponentsByType(GridFieldEditButton::class); |
|
| 194 | + $components->removeComponentsByType(GridFieldFilterHeader::class); |
|
| 195 | + $components->addComponent(new GridFieldSortableRows('SortOrder')); |
|
| 196 | + |
|
| 197 | + /** @var GridFieldDataColumns $dataColumns */ |
|
| 198 | + $dataColumns = $components->getComponentByType(GridFieldDataColumns::class); |
|
| 199 | + $dataColumns->setDisplayFields([ |
|
| 200 | + 'Title' => _t(__CLASS__ . '.ColumnTitle', 'Title'), |
|
| 201 | + 'ClassName' => _t(__CLASS__ . '.ColumnPageType', 'Page Type') |
|
| 202 | + ]); |
|
| 203 | + |
|
| 204 | + $fields->findOrMakeTab( |
|
| 205 | + 'Root.RelatedPages', |
|
| 206 | + _t(__CLASS__ . '.RelatedPages', 'Related pages') |
|
| 207 | + ); |
|
| 208 | + $fields->addFieldToTab( |
|
| 209 | + 'Root.RelatedPages', |
|
| 210 | + GridField::create( |
|
| 211 | + 'RelatedPages', |
|
| 212 | + _t(__CLASS__ . '.RelatedPages', 'Related pages'), |
|
| 213 | + $this->RelatedPages(), |
|
| 214 | + $components |
|
| 215 | + ) |
|
| 216 | + ); |
|
| 217 | + |
|
| 218 | + // Taxonomies - Unless they have their own 'Tags' field (such as in Blog, etc) |
|
| 219 | + $hasMany = $this->hasMany(); |
|
| 220 | + $manyMany = $this->manyMany(); |
|
| 221 | + if (!isset($hasMany['Tags']) && !isset($manyMany['Tags'])) { |
|
| 222 | + $components = GridFieldConfig_RelationEditor::create(); |
|
| 223 | + $components->removeComponentsByType(GridFieldAddNewButton::class); |
|
| 224 | + $components->removeComponentsByType(GridFieldEditButton::class); |
|
| 225 | + |
|
| 226 | + /** @var GridFieldAddExistingAutocompleter $autoCompleter */ |
|
| 227 | + $autoCompleter = $components->getComponentByType(GridFieldAddExistingAutocompleter::class); |
|
| 228 | + $autoCompleter->setResultsFormat('$Name ($TaxonomyName)'); |
|
| 229 | + |
|
| 230 | + /** @var GridFieldDataColumns $dataColumns */ |
|
| 231 | + $dataColumns = $components->getComponentByType(GridFieldDataColumns::class); |
|
| 232 | + $dataColumns->setDisplayFields([ |
|
| 233 | + 'Name' => _t(__CLASS__ . '.Term', 'Term'), |
|
| 234 | + 'TaxonomyName' => _t(__CLASS__ . '.Taxonomy', 'Taxonomy') |
|
| 235 | + ]); |
|
| 236 | + |
|
| 237 | + $fields->findOrMakeTab('Root.Tags', _t(__CLASS__ . '.TagsTabTitle', 'Tags')); |
|
| 238 | + $fields->addFieldToTab( |
|
| 239 | + 'Root.Tags', |
|
| 240 | + TreeMultiselectField::create( |
|
| 241 | + 'Terms', |
|
| 242 | + _t(__CLASS__ . '.Terms', 'Terms'), |
|
| 243 | + TaxonomyTerm::class |
|
| 244 | + )->setDescription(_t(__CLASS__ . '.TermsDescription', 'Click to search for additional terms')) |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | + }); |
|
| 248 | + return parent::getCMSFields(); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Provides data for translation navigation. |
|
| 253 | + * Collects all site translations, marks the current one, and redirects |
|
| 254 | + * to the translated home page if a. there is a translated homepage and b. the |
|
| 255 | + * translation of the specific page is not available. |
|
| 256 | + * @todo re-implement with Fluent |
|
| 257 | + */ |
|
| 258 | + public function getAvailableTranslations() |
|
| 259 | + { |
|
| 260 | + if (!class_exists('Translatable')) { |
|
| 261 | + return false; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + $translations = new ArrayList(); |
|
| 265 | + $globalTranslations = Translatable::get_existing_content_languages(); |
|
| 266 | + |
|
| 267 | + foreach ($globalTranslations as $loc => $langName) { |
|
| 268 | + // Find out the language name in native language. |
|
| 269 | + $nativeLangName = i18n::get_language_name($loc, true); |
|
| 270 | + if (!$nativeLangName) { |
|
| 271 | + $nativeLangName = i18n::get_language_name(i18n::get_lang_from_locale($loc), true); |
|
| 272 | + } |
|
| 273 | + if (!$nativeLangName) { |
|
| 274 | + // Fall back to the locale name. |
|
| 275 | + $nativeLangName = $langName; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // Eliminate the part in brackets (e.g. [mandarin]) |
|
| 279 | + $nativeLangName = preg_replace('/ *[\(\[].*$/', '', $nativeLangName); |
|
| 280 | + |
|
| 281 | + // Find out the link to the translated page. |
|
| 282 | + $link = null; |
|
| 283 | + $page = $this->getTranslation($loc); |
|
| 284 | + if ($page) { |
|
| 285 | + $link = $page->Link(); |
|
| 286 | + } |
|
| 287 | + if (!$link) { |
|
| 288 | + // Fall back to the home page |
|
| 289 | + $link = Translatable::get_homepage_link_by_locale($loc); |
|
| 290 | + } |
|
| 291 | + if (!$link) { |
|
| 292 | + continue; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + // Assemble the table for the switcher. |
|
| 296 | + $translations->push(new ArrayData(array( |
|
| 297 | + 'Locale' => i18n::convert_rfc1766($loc), |
|
| 298 | + 'LangName' => $nativeLangName, |
|
| 299 | + 'Link' => $link, |
|
| 300 | + 'Current' => (Translatable::get_current_locale()==$loc) |
|
| 301 | + ))); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + if ($translations->count()>1) { |
|
| 305 | + return $translations; |
|
| 306 | + } else { |
|
| 307 | + return null; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Returns the native language name for the selected locale/language, empty string if Translatable is not available |
|
| 313 | + * |
|
| 314 | + * @return string |
|
| 315 | + * @todo re-implement with Fluent |
|
| 316 | + */ |
|
| 317 | + public function getSelectedLanguage() |
|
| 318 | + { |
|
| 319 | + if (!class_exists('Translatable')) { |
|
| 320 | + return ''; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + $language = explode('_', Translatable::get_current_locale()); |
|
| 324 | + $languageCode = array_shift($language); |
|
| 325 | + $nativeName = i18n::get_language_name($languageCode, true); |
|
| 326 | + |
|
| 327 | + return $nativeName; |
|
| 328 | + } |
|
| 329 | 329 | } |