Example #1
0
 /**
  * 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;
     }
 }