/**
  * Action to handle and show the properties of this widget
  * @param \ride\library\i18n\I18n $i18n
  * @return null
  */
 public function propertiesAction(I18n $i18n, NodeModel $nodeModel)
 {
     $locales = $i18n->getLocaleCodeList();
     $hasMultipleLocales = count($locales) != 1;
     // get the data
     $io = $this->getTextIO();
     $existing = $this->request->getQueryParameter('text');
     if ($existing) {
         $text = $io->getExistingText($this->properties, $this->locale, $existing, $this->request->getQueryParameter('new'));
     } else {
         $text = $io->getText($this->properties, $this->locale);
     }
     if (!$text->getFormat()) {
         $text->setFormat($this->getDefaultTextFormat());
     }
     $format = $this->getTextFormat($text->getFormat());
     $data = array(self::PROPERTY_TITLE => $text->getTitle(), self::PROPERTY_SUBTITLE => $text->getSubtitle(), self::PROPERTY_BODY => $text->getBody(), self::PROPERTY_IMAGE => $text->getImage(), self::PROPERTY_IMAGE_ALIGNMENT => $text->getImageAlignment(), self::PROPERTY_CTA => $text->getCallToActions(), self::PROPERTY_TEMPLATE => $this->getTemplate(static::TEMPLATE_NAMESPACE . '/' . static::TEMPLATE_DEFAULT));
     $data['title-use'] = $data[self::PROPERTY_TITLE] || $data[self::PROPERTY_SUBTITLE];
     $data['image-use'] = $data[self::PROPERTY_IMAGE];
     $io->processFormData($text, $data);
     // create the form
     $translator = $this->getTranslator();
     $ctaComponent = new CallToActionComponent();
     $ctaComponent->setNodes($this->getNodeList($nodeModel));
     $types = $this->config->get('cms.text.cta.type');
     if (is_array($types)) {
         $types = $this->config->getConfigHelper()->flattenConfig($types);
         foreach ($types as $index => $label) {
             $types[$index] = $translator->translate($label);
         }
         $ctaComponent->setTypes(array('' => '---') + $types);
     }
     $form = $this->createFormBuilder($data);
     $form->setId('form-text');
     $format->processForm($form, $translator, $this->locale);
     $io->processForm($this->properties, $this->locale, $translator, $text, $form);
     $form->addRow('title-use', 'option', array('label' => ' ', 'description' => $translator->translate('label.title.use'), 'attributes' => array('data-toggle-dependant' => 'option-title')));
     $form->addRow(self::PROPERTY_TITLE, 'string', array('label' => $translator->translate('label.title'), 'attributes' => array('class' => 'option-title option-title-1'), 'filters' => array('trim' => array())));
     $form->addRow(self::PROPERTY_SUBTITLE, 'string', array('label' => $translator->translate('label.subtitle'), 'attributes' => array('class' => 'option-title option-title-1'), 'filters' => array('trim' => array())));
     $form->addRow('image-use', 'option', array('label' => ' ', 'description' => $translator->translate('label.image.use'), 'attributes' => array('data-toggle-dependant' => 'option-image')));
     $form->addRow(self::PROPERTY_IMAGE, 'image', array('label' => $translator->translate('label.image'), 'attributes' => array('class' => 'option-image option-image-1')));
     $form->addRow(self::PROPERTY_IMAGE_ALIGNMENT, 'select', array('label' => $translator->translate('label.alignment.image'), 'attributes' => array('class' => 'option-image option-image-1'), 'options' => array(Text::ALIGN_NONE => $translator->translate('align.none'), Text::ALIGN_LEFT => $translator->translate('align.left'), Text::ALIGN_RIGHT => $translator->translate('align.right'), Text::ALIGN_JUSTIFY => $translator->translate('align.justify'))));
     $form->addRow(self::PROPERTY_CTA, 'collection', array('label' => $translator->translate('label.cta'), 'type' => 'component', 'options' => array('component' => $ctaComponent)));
     $form->addRow(self::PROPERTY_TEMPLATE, 'select', array('label' => $translator->translate('label.template'), 'description' => $translator->translate('label.template.widget.description'), 'options' => $this->getAvailableTemplates(static::TEMPLATE_NAMESPACE), 'validators' => array('required' => array())));
     if ($hasMultipleLocales) {
         $form->addRow('locales-all', 'option', array('label' => '', 'description' => $translator->translate('label.text.locales.all')));
     }
     // handle form submission
     $form = $form->build();
     if ($form->isSubmitted()) {
         if ($existing) {
             $form->setData($data);
         } else {
             try {
                 $form->validate();
                 $data = $form->getData();
                 if (!$data['title-use']) {
                     $data[self::PROPERTY_TITLE] = '';
                     $data[self::PROPERTY_SUBTITLE] = '';
                 }
                 if (!$data['image-use']) {
                     $data[self::PROPERTY_IMAGE] = '';
                 }
                 $this->properties->setWidgetProperty(self::PROPERTY_IO, $io->getName());
                 $format->setText($text, $data);
                 if ($hasMultipleLocales && $data['locales-all']) {
                     $io->setText($this->properties, $locales, $text, $data);
                 } else {
                     $io->setText($this->properties, $this->locale, $text, $data);
                 }
                 $this->setTemplate($data[self::PROPERTY_TEMPLATE]);
                 return true;
             } catch (ValidationException $exception) {
                 $this->setValidationException($exception, $form);
             }
         }
     }
     // set view
     $node = $this->properties->getNode();
     $action = $this->getUrl('cms.node.content.widget.properties', array('locale' => $this->locale, 'site' => $node->getRootNodeId(), 'revision' => $node->getRevision(), 'node' => $node->getId(), 'region' => $this->region, 'section' => $this->section, 'block' => $this->block, 'widget' => $this->id));
     $view = $this->setTemplateView(static::TEMPLATE_NAMESPACE . '/properties', array('form' => $form->getView(), 'io' => $io, 'format' => $format, 'text' => $text, 'action' => $action));
     $view->addJavascript('js/cms/text.js');
     $io->processFormView($this->properties, $translator, $text, $view);
     $form->processView($view);
     return false;
 }
 /**
  * Action to add or edit a asset
  * @param \ride\library\i18n\I18n $i18n Instance of I18n
  * @param \ride\library\orm\OrmManager $orm
  * @param \ride\web\cms\form\AssetComponent
  * @param string $locale
  * @param string $asset
  * @return null
  */
 public function assetFormAction(I18n $i18n, OrmManager $orm, AssetComponent $assetComponent, $locale, $asset = null)
 {
     $folderModel = $orm->getAssetFolderModel();
     $assetModel = $orm->getAssetModel();
     $styleModel = $orm->getImageStyleModel();
     // prepare or lookup asset
     if ($asset) {
         $asset = $assetModel->getById($asset, $locale, true);
         if (!$asset) {
             $this->response->setNotFound();
             return;
         }
         $folder = $asset->getFolder();
     } else {
         $asset = $assetModel->createEntry();
         $folder = $this->request->getQueryParameter('folder');
         if ($folder) {
             $folder = $folderModel->createProxy($folder, $locale);
             $asset->setFolder($folder);
         }
     }
     $media = null;
     $referer = $this->getAssetReferer($asset, $locale);
     $embed = $this->request->getQueryParameter('embed', false);
     if ($asset->isUrl()) {
         try {
             $media = $assetModel->getMediaFactory()->createMediaItem($asset->value);
         } catch (UnsupportedMediaException $exception) {
         }
     }
     $data = array('asset' => $asset);
     $styles = $styleModel->find();
     foreach ($styles as $style) {
         $data['style-' . $style->getSlug()] = $asset->getStyleImage($style->getSlug());
     }
     // create form
     $form = $this->createFormBuilder($data);
     $form->addRow('asset', 'component', array('component' => $assetComponent, 'embed' => true));
     foreach ($styles as $style) {
         $form->addRow('style-' . $style->getSlug(), 'image', array('path' => $assetComponent->getDirectory()));
     }
     $form = $form->build();
     // process form
     if ($form->isSubmitted()) {
         try {
             $form->validate();
             $data = $form->getData();
             $asset = $data['asset'];
             $asset->setLocale($locale);
             $assetStyleModel = $orm->getAssetImageStyleModel();
             foreach ($styles as $style) {
                 $image = $data['style-' . $style->getSlug()];
                 $assetStyle = $asset->getStyle($style->getSlug());
                 if ($image) {
                     if (!$assetStyle) {
                         // style addition
                         $assetStyle = $assetStyleModel->createEntry();
                         $assetStyle->setStyle($style);
                         $asset->addToStyles($assetStyle);
                     }
                     $assetStyle->setImage($image);
                 } elseif ($assetStyle) {
                     // style removal
                     $asset->removeFromStyles($assetStyle);
                 }
             }
             $assetModel->save($asset);
             if ($this->request->isXmlHttpRequest()) {
                 // ajax request
                 $this->setTemplateView('assets/detail', array('item' => $asset, 'embed' => $embed, 'referer' => $referer, 'locale' => $locale));
             } else {
                 // regular client
                 $this->response->setRedirect($referer);
             }
             return;
         } catch (ValidationException $exception) {
             k($exception->getErrorsAsString());
             $this->setValidationException($exception, $form);
         }
     }
     $view = $this->setTemplateView('assets/asset', array('form' => $form->getView(), 'folder' => $folder, 'styles' => $styles, 'asset' => $asset, 'embed' => $embed, 'referer' => $referer, 'media' => $media, 'dimension' => $assetModel->getDimension($asset), 'locales' => $i18n->getLocaleCodeList(), 'locale' => $locale, 'maxSize' => $this->config->get('asset.upload.size.max')));
     $form->processView($view);
 }
 /**
  * Action to add or edit a question
  * @param \ride\library\i18n\I18n $i18n
  * @param string $locale Locale code of the data
  * @param string $survey Id of the survey
  * @param string $id Id of the question
  * @return null
  */
 public function questionFormAction(I18n $i18n, $locale, WebApplication $web, ReflectionHelper $reflectionHelper, $survey, $id = null)
 {
     // resolve locale
     $this->locale = $i18n->getLocale($locale)->getCode();
     $this->orm->setLocale($this->locale);
     // resolve event
     if (!$this->isReadable($survey)) {
         throw new UnauthorizedException();
     }
     $survey = $this->getEntry($survey);
     if (!$survey) {
         $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
         return;
     }
     // resolve performance
     $this->model = $this->orm->getSurveyQuestionModel();
     if ($id) {
         $question = $this->getEntry($id);
         if (!$question || $question->getSurvey()->getId() !== $survey->getId()) {
             $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
             return;
         }
     } else {
         $question = $this->createEntry();
         $question->setSurvey($survey);
     }
     // create form
     $translator = $this->getTranslator();
     $likertModel = $this->orm->getSurveyLikertModel();
     $answerComponent = new QuestionAnswerComponent();
     $answerComponent->setLikertScales($likertModel->find());
     $form = $this->createFormBuilder($question);
     $form->setId('form-survey-question');
     $form->addRow('question', 'text', array('label' => $translator->translate('label.question'), 'validators' => array('required' => array())));
     $form->addRow('description', 'wysiwyg', array('label' => $translator->translate('label.description')));
     $form->addRow('isOpen', 'option', array('label' => $translator->translate('label.question.open'), 'description' => $translator->translate('label.question.open.description')));
     $form->addRow('isMultiple', 'option', array('label' => $translator->translate('label.question.multiple'), 'description' => $translator->translate('label.question.multiple.description')));
     $form->addRow('answers', 'collection', array('label' => $translator->translate('label.answers'), 'type' => 'component', 'order' => true, 'options' => array('component' => $answerComponent)));
     $form = $form->build();
     // handle form
     if ($form->isSubmitted()) {
         try {
             $form->validate();
             // obtain performance from form
             $question = $form->getData();
             $question->setLocale($locale);
             foreach ($question->getAnswers() as $answer) {
                 $answer->setLocale($locale);
             }
             $this->model->save($question);
             $this->addSuccess('success.data.saved', array('data' => $question->getQuestion()));
             $this->response->setRedirect($this->getAction(self::ACTION_DETAIL, array('id' => $survey->getId())));
             return;
         } catch (ValidationException $exception) {
             $this->setValidationException($exception, $form);
         }
     }
     $referer = $this->request->getQueryParameter('referer');
     $this->templateForm = 'orm/scaffold/form';
     if ($question && $question->getId()) {
         $localizeUrl = $this->getUrl('survey.question.edit', array('locale' => '%locale%', 'survey' => $survey->getId(), 'id' => $question->getId()));
     } else {
         $localizeUrl = $this->getUrl('survey.question.add', array('locale' => '%locale%', 'survey' => $survey->getId()));
     }
     $localizeUrl .= '?referer=' . urlencode($this->request->getUrl());
     $view = $this->setFormView($form, $referer, $i18n->getLocaleCodeList(), $locale, $survey);
     $view->getTemplate()->set('localizeUrl', $localizeUrl);
 }