/**
  * Action to edit the templates of the widget
  * @param Cms $cms
  * @param TemplateFacade $templateFacade
  * @param FileBrowser $fileBrowser
  * @param string $locale
  * @param string $site
  * @param string $revision
  * @param string $node
  * @param string $region
  * @param string $widget
  * @return null
  */
 public function indexAction(Cms $cms, TemplateFacade $templateFacade, FileBrowser $fileBrowser, $locale, $site, $revision, $node, $region, $widget)
 {
     if (!$cms->resolveNode($site, $revision, $node) || !$cms->resolveRegion($node, $locale, $region)) {
         return;
     }
     $templateFacade->setThemeModel($cms->getThemeModel());
     $widgetId = $widget;
     $widget = $site->getWidget($widgetId);
     $widget = clone $cms->getWidget($widget);
     $widget->setRequest($this->request);
     $widget->setResponse($this->response);
     $widget->setProperties($node->getWidgetProperties($widgetId));
     $widget->setLocale($locale);
     $widget->setRegion($region);
     if ($widget instanceof AbstractController) {
         $widget->setConfig($this->config);
         $widget->setDependencyInjector($this->dependencyInjector);
     }
     $templates = $widget->getTemplates();
     foreach ($templates as $index => $template) {
         $tokens = explode('/', $template);
         $name = array_pop($tokens);
         unset($templates[$index]);
         $templates[$name] = $template;
     }
     $component = new TemplatesComponent();
     $data = $component->createData($templateFacade, $templates, $node->getTheme(), $widgetId);
     $form = $this->buildForm($component, $data);
     if ($form->isSubmitted()) {
         try {
             $form->validate();
             $data = $form->getData();
             $applicationDirectory = $fileBrowser->getApplicationDirectory();
             foreach ($templates as $name => $template) {
                 $file = $applicationDirectory->getChild($data['path'][$name]);
                 $file->getParent()->create();
                 $file->write($data['content'][$name]);
             }
             $this->addSuccess('success.widget.saved', array('widget' => $this->getTranslator()->translate('widget.' . $widget->getName())));
             $this->response->setRedirect($this->getUrl('cms.node.content.region', array('locale' => $locale, 'site' => $site->getId(), 'revision' => $node->getRevision(), 'node' => $node->getId(), 'region' => $region)));
             return;
         } catch (ValidationException $validationException) {
             $this->setValidationException($validationException, $form);
         }
     }
     $referer = $this->request->getQueryParameter('referer');
     $this->setTemplateView('cms/backend/widget.templates', array('site' => $site, 'node' => $node, 'referer' => $referer, 'locale' => $locale, 'locales' => $cms->getLocales(), 'region' => $region, 'widget' => $widget, 'widgetId' => $widgetId, 'widgetName' => $this->getTranslator()->translate('widget.' . $widget->getName()), 'templates' => $templates, 'form' => $form->getView()));
 }
 /**
  * Action to download the file
  * @param string $widgetId
  * @param string $fileId
  * @return null
  */
 public function downloadAction(FileBrowser $fileBrowser, $widgetId, $fileId)
 {
     if ($widgetId != $this->id) {
         return;
     }
     $files = $this->getFiles();
     if (!isset($files[$fileId])) {
         $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
         return;
     }
     $file = $fileBrowser->getFile($files[$fileId]['file']);
     if (!$file) {
         $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
         return;
     }
     $this->setDownloadView($file, $file->getName());
 }
 /**
  * Perform the template node action
  */
 public function indexAction(TemplateFacade $templateFacade, FileBrowser $fileBrowser, $locale, $site, $revision, $node)
 {
     if (!$this->cms->resolveNode($site, $revision, $node)) {
         return;
     }
     $this->setContentLocale($locale);
     $this->cms->setLastAction(self::NAME);
     $referer = $this->request->getQueryParameter('referer');
     $templateFacade->setThemeModel($this->cms->getThemeModel());
     $templates = array('index' => 'cms/frontend/index');
     if ($node->getId() == $site->getId()) {
         $id = null;
     } else {
         $id = $node->getId();
     }
     $component = new TemplatesComponent();
     $data = $component->createData($templateFacade, $templates, $node->getTheme(), $id);
     $form = $this->buildForm($component, $data);
     if ($form->isSubmitted()) {
         try {
             $form->validate();
             $data = $form->getData();
             $applicationDirectory = $fileBrowser->getApplicationDirectory();
             foreach ($templates as $name => $template) {
                 $file = $applicationDirectory->getChild($data['path'][$name]);
                 $file->getParent()->create();
                 $file->write($data['content'][$name]);
             }
             $this->addSuccess('success.node.saved', array('node' => $node->getName($locale)));
             $url = $this->getUrl(self::ROUTE, array('site' => $site->getId(), 'revision' => $node->getRevision(), 'locale' => $locale, 'node' => $node->getId()));
             if ($referer) {
                 $url .= '?referer=' . urlencode($referer);
             }
             $this->response->setRedirect($url);
             return;
         } catch (ValidationException $validationException) {
             $this->setValidationException($validationException, $form);
         }
     }
     $this->setTemplateView('cms/backend/node.templates', array('site' => $site, 'node' => $node, 'templates' => $templates, 'form' => $form->getView(), 'referer' => $referer, 'locale' => $locale, 'locales' => $this->cms->getLocales()));
 }
 /**
  * Gets a DataURI of the provided file
  * @param string $path Path of the file
  * @return string|null DataURI of the file
  */
 protected function getDataUriFromPath($path)
 {
     $file = $this->fileBrowser->getFile($path);
     if (!$file) {
         $file = $this->fileBrowser->getPublicFile($path);
         if (!$file) {
             return null;
         }
     }
     $dataUri = $this->httpFactory->createDataUriFromFile($file);
     return $dataUri->encode();
 }
 /**
  * Action to get the original value of an asset
  * @param \ride\library\orm\OrmManager $orm
  * @param string $asset
  * @return null
  */
 public function assetValueAction(OrmManager $orm, FileBrowser $fileBrowser, $asset)
 {
     $assetModel = $orm->getAssetModel();
     if (is_numeric($asset)) {
         $asset = $assetModel->getById($asset);
     } else {
         $asset = $assetModel->getBy(array('filter' => array('slug' => $asset)));
     }
     if (!$asset) {
         $this->response->setNotFound();
         return;
     }
     if ($asset->isUrl()) {
         $this->response->setRedirect($asset->getValue());
         return;
     }
     $file = $fileBrowser->getFile($asset->getValue());
     if (!$file) {
         $this->response->setNotFound();
         return;
     }
     $this->setFileView($file);
 }
 /**
  * Action to get the original value of an asset
  * @param \ride\library\orm\OrmManager $orm
  * @param string $asset
  * @return null
  */
 public function assetValueAction(OrmManager $orm, FileBrowser $fileBrowser, AssetService $assetService, $asset)
 {
     $assetModel = $orm->getAssetModel();
     if (is_numeric($asset)) {
         $asset = $assetModel->getById($asset);
     } else {
         $locales = $orm->getLocales();
         foreach ($locales as $locale) {
             $asset = $assetModel->getBy(array('filter' => array('slug' => $asset)), $locale);
             if ($asset) {
                 break;
             }
         }
     }
     if (!$asset) {
         $this->response->setNotFound();
         return;
     }
     $url = $assetService->getAssetUrl($asset, $this->request->getQueryParameter('style'));
     if ($url) {
         $this->response->setRedirect($url);
         return;
     }
     $file = $fileBrowser->getFile($asset->getValue());
     if (!$file) {
         $this->response->setNotFound();
         return;
     }
     $this->setFileView($file);
 }