Source of file DataObjectPreviewController.php
Size: 2,584 Bytes - Last Modified: 2022-02-21T10:01:59+00:00
/var/www/docs.ssmods.com/process/src/src/controllers/DataObjectPreviewController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 | <?php namespace SilverStripe\DataObjectPreview\Controllers; use SilverStripe\Control\Controller; use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; use SilverStripe\Versioned\Versioned; use InvalidArgumentException; class DataObjectPreviewController extends Controller { protected $dataobject; private static $allowed_actions = ['show']; private static $url_segment = 'show'; private static $url_handlers = [ 'show/$ClassName/$ID/$OtherClassName/$OtherID' => 'show', ]; public static function strip_namespacing($namespaceClass) { if (strrpos($namespaceClass, '\\')) { return substr($namespaceClass, strrpos($namespaceClass, '\\') + 1); } return $namespaceClass; } protected function init() { parent::init(); // In the CMS Preview or draft contexts, we never want to cache page output. if ( $this->getRequest()->getVar('CMSPreview') == '1' || $this->getRequest()->getVar('stage') == Versioned::DRAFT ) { HTTPCacheControlMiddleware::singleton()->disableCache(true); } } public function getDataObject() { return $this->dataobject; } public function show($request) { $class = urldecode($request->param('ClassName')); $class = str_replace('-', '\\', $class); if (!class_exists($class)) { throw new InvalidArgumentException( sprintf( 'DataObjectPreviewController: Class of type %s doesn\'t exist', $class ) ); } $id = $request->param('ID'); if (!ctype_digit($id)) { throw new InvalidArgumentException( 'DataObjectPreviewController: ID needs to be an integer' ); } $this->dataobject = $class ::get() ->filter(['ID' => $id]) ->First(); $r = false; switch (true) { case !$this->dataobject: $r = false; break; case $this->dataobject->hasMethod('renderPreview'): $r = $this->dataobject->renderPreview(); break; default: $r = $this->dataobject->renderWith([ $class, self::strip_namespacing($class), ]); break; } return $this->customise([ 'Rendered' => $r, ])->renderWith('PreviewDataObject'); } } |