/** * 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]); }
/** * Add a block by first choosing the widget to use * * @param string $rowId Row to add this block to * @param int $columnIndex Numeric column index * @return void */ public function add($rowId = null, $columnIndex = null) { $cmsRow = $this->CmsBlocks->CmsRows->get($rowId); $cmsPage = $this->CmsBlocks->CmsRows->CmsPages->getPage($cmsRow->cms_page_id); $cmsBlock = $this->CmsBlocks->newEntity(); if ($this->request->is('post')) { $cmsBlock = $this->CmsBlocks->patchEntity($cmsBlock, $this->request->data); $cmsBlock->cms_row_id = $rowId; $cmsBlock->column_index = $columnIndex; $cmsBlock->status = 'active'; if ($this->CmsBlocks->save($cmsBlock)) { $this->FrontendBridge->setJson('success', true); $this->FrontendBridge->setJson('triggerAction', 'editBlock'); $this->FrontendBridge->setJson('blockId', $cmsBlock->id); } else { $this->Flash->error(__('forms.data_not_saved')); } } $widgetList = WidgetManager::getWidgetList(); $widgets = []; foreach ($widgetList as $identifier => $config) { $widgets[$identifier] = sprintf('%s (%s)', $config['title'], $config['description']); } $this->set(compact('cmsBlock', 'widgets')); }
/** * 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; } }
/** * Include necessary cms assets. This has to be called after the page has been rendered. * Best place is in the layout. * * @param array $options Options for the Html->script() calls * @return void|string */ public function includeCmsAssets(array $options = []) { if ($this->_cmsAssetsIncluded) { return; } $options = Hash::merge(['block' => true], $options); $out = ''; $out .= $this->Html->scriptBlock('var Cms = {WidgetControllers: {}};', $options); $out .= $this->Html->script('Cms.app/lib/WidgetController.js', $options); $folder = new Folder(); $controllers = []; $jsonData = []; foreach (WidgetManager::getRegistry() as $uniqueId => $widget) { $webrootPath = $widget->getWebrootPath(); if (!is_dir($webrootPath)) { continue; } $jsonData[$uniqueId] = ['identifier' => $widget->getFullIdentifier(), 'blockId' => $widget->getCmsBlock()->id, 'jsonData' => $widget->getJsonData()]; $folder->cd($webrootPath . 'js/'); $jsFiles = $folder->read(); if (empty($jsFiles[1])) { continue; } foreach ($jsFiles[1] as $filename) { if (strpos($filename, 'WidgetController.js') !== false && strpos($filename, 'AdminWidgetController.js') === false) { $controllers[] = '/cms/widget/' . $widget->getIdentifier() . '/js/' . $filename; } } } $this->FrontendBridge->setFrontendData('Cms', ['widgets' => $jsonData]); $out .= $this->Html->script($controllers, $options); $this->_cmsAssetsIncluded = true; if (!$options['block']) { return $out; } }
/** * Construct an AbstractWidget instance using only the widget identifier, * without a CmsBlock entity. * * @param string $widgetIdentifier Widget Identifier * @param array $config Optional Config * @return Cms\Widget\AbstractWidget */ public static function identifierFactory($widgetIdentifier, array $config = []) { $widgetClass = WidgetManager::getWidgetClassName($widgetIdentifier); $widget = new $widgetClass($widgetIdentifier, $config, null); return $widget; }
/** * Returns the class name for the widget to use for this block * * @return string */ public function getWidgetClassName() { return WidgetManager::getWidgetClassName($this->widget); }