/** * Includes all necessary JS code for admin editing of CmsBlocks. * * @return void */ public function includeCmsAdminAssets() { $script = 'var Cms = {AdminWidgetControllers: {}};'; $this->Html->scriptBlock($script, ['block' => true]); $this->Html->script('Cms.app/lib/AdminWidgetController.js', ['block' => true]); $availableWidgets = WidgetManager::getAvailableWidgets(); $adminControllers = []; $folder = new Folder(); foreach ($availableWidgets as $widgetIdentifier) { $widget = WidgetFactory::identifierFactory($widgetIdentifier); $webrootPath = $widget->getWebrootPath(); if (!is_dir($webrootPath)) { continue; } $folder->cd($webrootPath . 'js/'); $jsFiles = $folder->read(); if (empty($jsFiles[1])) { continue; } foreach ($jsFiles[1] as $filename) { if (strpos($filename, 'AdminWidgetController.js') !== false) { $adminControllers[] = '/cms/widget/' . $widgetIdentifier . '/js/' . $filename; } } } $this->Html->script($adminControllers, ['block' => true]); }
/** * Builds asset file path based off url * * @param string $url Asset URL * @return string Absolute path for asset file */ protected function _getAssetFile($url) { $pattern = '/^cms\\/widget\\/([a-z\\.]+)\\/(.*)$/i'; if (preg_match($pattern, $url, $matches)) { $widgetIdentifier = $matches[1]; $assetPath = $matches[2]; if (!WidgetManager::widgetExists($widgetIdentifier)) { throw new \Cake\Network\Exception\NotFoundException("Widget {$widgetIdentifier} could not be found"); } $widget = WidgetFactory::identifierFactory($widgetIdentifier); $assetFile = $widget->getWebrootPath() . $assetPath; return $assetFile; } }
/** * Returns an array, indexed by widget identifier with title and description * for the widgets * * @return array */ public static function getWidgetList() { $availableWidgets = self::getAvailableWidgets(); $widgetList = []; foreach ($availableWidgets as $widgetIdentifier) { $widget = WidgetFactory::identifierFactory($widgetIdentifier); $widgetList[$widgetIdentifier] = ['title' => $widget->config('title') ? $widget->config('title') : $widgetIdentifier, 'description' => $widget->config('description')]; } return $widgetList; }