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