Source of file TemplateOverviewPageController.php
Size: 9,864 Bytes - Last Modified: 2021-12-23T10:47:10+00:00
/var/www/docs.ssmods.com/process/src/src/Control/TemplateOverviewPageController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 | <?php namespace Sunnysideup\TemplateOverview\Control; use PageController; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Assets\Image; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; use SilverStripe\Versioned\Versioned; use SilverStripe\View\ArrayData; use SilverStripe\View\Requirements; use SilverStripe\View\SSViewer; use Sunnysideup\PrettyPhoto\PrettyPhoto; use Sunnysideup\TemplateOverview\Api\SiteTreeDetails; /** *@author: nicolaas [at] sunnysideup.co.nz *@description Add a page to your site that allows you to view all the html that can be used in the typography section - if applied correctly. */ class TemplateOverviewPageController extends PageController { protected $myMoreList; private static $url_segment = 'admin/templateoverviewtemplates'; /** * folder for example images from root dir * it is recommended to keep this outside of public dir! * * @var string */ private static $folder_for_example_images = ''; private static $allowed_actions = [ 'showmore' => true, 'quicklist' => true, 'listofobjectsused' => true, 'exampleimage' => true, ]; private static $base_class = SiteTree::class; private static $base_class_provider = SiteTreeDetails::class; public function index(HTTPRequest $request = null) { // $this->renderWith(['Sunnysideup\\TemplateOverview\\TemplateOverviewPageController']); return []; } public function Content() { return $this->renderWith('Sunnysideup\\TemplateOverview\\Includes\\TemplateOverviewList'); } public function showmore($request) { $id = $request->param('ID'); $className = $this->getBaseClass(); /** @var null|DataObject $obj */ $obj = $className::get_by_id((int) $id); if (null !== $obj) { $className = $obj->ClassName; $list = $className::get() ->filter(['ClassName' => $obj->ClassName]) ->limit(300) ; $this->myMoreList = ArrayList::create(); foreach ($list as $count => $item) { $this->myMoreList->push(clone $this->createPageObject($item, $count)); } } return $this->renderWith('Sunnysideup\\TemplateOverview\\TemplateOverviewPageShowMoreList'); } public function getMyMoreList() { return $this->myMoreList; } public function Link($action = null) { $v = '/' . $this->Config()->url_segment; if ($action) { $v .= $action . '/'; } return $v; } public function TestTaskLink() { return '/dev/tasks/CheckAllTemplates/'; } public function QuickListLink() { return $this->Link('quicklist'); } public function ImagesListLink() { return $this->Link('listofobjectsused/' . Image::class); } public function quicklist() { $list = $this->ListOfAllClasses(); foreach ($list as $item) { DB::alteration_message($item->ClassName); } } public function listofobjectsused($request) { $classWeAreLookingFor = '\\' . str_replace('-', '\\', $request->param('ID')); $classWeAreLookingFor = Injector::inst()->get($classWeAreLookingFor); if ($classWeAreLookingFor instanceof DataObject) { $list = $this->ListOfAllClasses(); foreach ($list as $item) { $config = Config::inst(); /** @var array $listOfImages */ $a = (array) $config->get($item->ClassName, 'has_one', Config::UNINHERITED); $b = (array) $config->get($item->ClassName, 'has_many', Config::UNINHERITED); $c = (array) $config->get($item->ClassName, 'many_many', Config::UNINHERITED); $listOfImages = array_merge($a, $b, $c); foreach ($listOfImages as $fieldName => $potentialImage) { if (is_array($potentialImage)) { $potentialImage = $potentialImage['to'] ?? ''; } $potentialImage = (explode('.', $potentialImage))[0]; if (class_exists($potentialImage)) { $innerSingleton = Injector::inst()->get($potentialImage); if ($innerSingleton instanceof $classWeAreLookingFor) { DB::alteration_message($item->ClassName . '.' . $fieldName); } } } } } else { user_error('Please specify the ID for the model you are looking for - e.g. /listofobjectsused/Image/', E_USER_ERROR); } } /** * returns a list of all (SiteTree) Classes. * * @return ArrayList */ public function ListOfAllClasses() { $providerClass = Injector::inst()->get($this->Config()->get('base_class_provider')); $originalList = $providerClass->ListOfAllClasses(); $countsPerClass = $providerClass->CountsPerClass(); $newList = ArrayList::create(); foreach ($originalList as $obj) { $count = (int) ($countsPerClass[$obj->ClassName] ?? 0); $newList->push($this->createPageObject($obj, $count)); } return $newList; } public function TotalCount() { $className = $this->getBaseClass(); return count(ClassInfo::subclassesFor($className)) - 1; } protected function getBaseClass(): string { return $this->Config()->get('base_class'); } protected function init() { parent::init(); //important Versioned::set_stage(Versioned::DRAFT); if (Director::is_cli() || Director::isDev() || Permission::check('ADMIN')) { Config::modify()->set(SSViewer::class, 'theme_enabled', false); Requirements::css('sunnysideup/templateoverview: client/css/TemplateOverviewPage.css'); Requirements::css('silverstripe/admin: client/dist/styles/bundle.css'); Requirements::javascript('https://code.jquery.com/jquery-1.7.2.min.js'); Requirements::javascript('sunnysideup/templateoverview: client/javascript/TemplateOverviewPage.js'); if (class_exists(PrettyPhoto::class)) { PrettyPhoto::include_code(); } //user_error("It is recommended that you install the Sunny Side Up Pretty Photo Module", E_USER_NOTICE); } else { return Security::permissionFailure($this, 'Please login to access this list'); } } /** * @param SiteTree $obj * @param int $count * * @return ArrayData */ protected function createPageObject($obj, $count) { $canCreateString = ($obj->canCreate() ? 'Yes' : 'No'); $isAdmin = Permission::check('ADMIN'); $listArray = []; $listArray['Name'] = 1 === $count ? $obj->i18n_singular_name() : $obj->i18n_plural_name(); $listArray['Description'] = DBField::create_field('HTMLText', $obj->i18n_classDescription()); $listArray['ClassName'] = $obj->ClassName; $listArray['Count'] = $count; $listArray['ID'] = $obj->ID; $listArray['URLSegment'] = $obj->URLSegment ?? 'n/a'; $listArray['ControllerLink'] = $this->Link(); $listArray['Title'] = $obj->getTitle(); $listArray['LiveLink'] = $obj->hasMethod('Link') ? str_replace('?stage=Stage', '', $obj->Link()) : 'please-add-Link-method'; $listArray['PreviewLink'] = $obj->hasMethod('PreviewLink') ? $obj->PreviewLink() : 'please-add-PreviewLink-method'; $listArray['CMSEditLink'] = $obj->hasMethod('CMSEditLink') ? $obj->CMSEditLink() : 'please-add-CMSEditLink-method'; $listArray['MoreCanBeCreated'] = $isAdmin ? $canCreateString : 'Please login as ADMIN to see this value'; $children = $this->listOfTitles($obj->allowedChildren()); $listArray['AllowedChildren'] = implode(', ', $children); $listArray['Icon'] = $this->getIcon($obj); return new ArrayData($listArray); } protected function getIcon($obj): string { $icon = ''; $icon = $this->getIconInner($obj, 'getPageIconURL'); if (! $icon) { $icon = $this->getIconInner($obj, 'getIconClass'); if (! $icon) { $icon = $this->getIconInner($obj, 'getIcon'); if (! $icon) { return (string) LeftAndMain::menu_icon_for_class($obj->ClassName); } } } if (! $icon) { $icon = 'font-page'; } if (false === strpos('.', $icon)) { // $icon = str_replace('font-icon-', 'fa-', $icon); } return $icon; } protected function getIconInner($obj, $method): ?string { if ($obj->hasMethod($method)) { $icon = (string) $obj->{$method}(); if ($icon) { return $icon; } } return null; } protected function listOfTitles($array) { if (is_array($array) && count($array)) { $newArray = []; foreach ($array as $item) { $obj = Injector::inst()->get($item); $newArray[] = $obj->i18n_singular_name(); } return $newArray; } return ['none']; } } |