2013-07-31 10:30:58 +02:00
|
|
|
<?php
|
2016-07-22 01:32:32 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\CMS\Controllers;
|
|
|
|
|
2019-11-12 22:21:52 +01:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
|
|
|
use ReflectionException;
|
2024-01-18 22:00:46 +01:00
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2017-10-18 01:32:08 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Convert;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\Extension;
|
2019-11-12 22:21:52 +01:00
|
|
|
use SilverStripe\Core\Flushable;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-10-18 01:32:08 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
2016-07-22 01:32:32 +02:00
|
|
|
|
2013-07-31 10:30:58 +02:00
|
|
|
/**
|
|
|
|
* Extension to include custom page icons
|
2024-01-18 22:00:46 +01:00
|
|
|
*
|
|
|
|
* @extends Extension<LeftAndMain>
|
2013-07-31 10:30:58 +02:00
|
|
|
*/
|
2019-11-12 22:21:52 +01:00
|
|
|
class LeftAndMainPageIconsExtension extends Extension implements Flushable
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
2019-11-12 22:21:52 +01:00
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws ReflectionException
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
public function init()
|
|
|
|
{
|
2017-09-07 02:36:26 +02:00
|
|
|
Requirements::customCSS($this->generatePageIconsCss(), CMSMain::PAGE_ICONS_ID);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 22:21:52 +01:00
|
|
|
/**
|
|
|
|
* Just broadly clears the cache on flush
|
|
|
|
*/
|
|
|
|
public static function flush()
|
|
|
|
{
|
|
|
|
Injector::inst()->get(CacheInterface::class . '.SiteTree_PageIcons')->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* Include CSS for page icons. We're not using the JSTree 'types' option
|
|
|
|
* because it causes too much performance overhead just to add some icons.
|
|
|
|
*
|
|
|
|
* @return string CSS
|
2019-11-12 22:21:52 +01:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @throws ReflectionException
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
public function generatePageIconsCss()
|
|
|
|
{
|
2019-11-12 22:21:52 +01:00
|
|
|
/** @var CacheInterface $cache */
|
|
|
|
$cache = Injector::inst()->get(CacheInterface::class . '.SiteTree_PageIcons');
|
|
|
|
|
|
|
|
if ($cache->has('css')) {
|
|
|
|
return $cache->get('css');
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$css = '';
|
2017-09-20 03:51:07 +02:00
|
|
|
$classes = ClassInfo::subclassesFor(SiteTree::class);
|
2017-01-25 21:59:25 +01:00
|
|
|
foreach ($classes as $class) {
|
2019-05-08 06:13:33 +02:00
|
|
|
if (!empty(Config::inst()->get($class, 'icon_class', Config::UNINHERITED))) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-18 01:32:08 +02:00
|
|
|
$iconURL = SiteTree::singleton($class)->getPageIconURL();
|
|
|
|
if ($iconURL) {
|
2017-11-22 20:55:35 +01:00
|
|
|
$cssClass = Convert::raw2htmlid($class);
|
|
|
|
$selector = sprintf('.page-icon.class-%1$s, li.class-%1$s > a .jstree-pageicon', $cssClass);
|
|
|
|
$css .= sprintf('%s { background: transparent url(\'%s\') 0 0 no-repeat; }', $selector, $iconURL);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 22:21:52 +01:00
|
|
|
|
|
|
|
$cache->set('css', $css);
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
return $css;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
}
|