/**
  * 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()));
 }
 /**
  * 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()));
 }