public function initialize($entity = null, $options = null)
 {
     if (isset($options['edit']) && $options['edit'] === true) {
         $this->edit = true;
     }
     $locales = $this->getDI()->get('config')->i18n->locales->toArray();
     foreach ($locales as $locale => $name) {
         if (true === $this->edit) {
             $translations = $entity->getTranslations(["article_translation_lang = '{$locale}'"])->toArray();
         }
         $article_translation_short_title[$locale] = new Text("translations[{$locale}][article_translation_short_title]", ['value' => $this->edit === true ? $translations[0]['article_translation_short_title'] : null]);
         $article_translation_long_title[$locale] = new Text("translations[{$locale}][article_translation_long_title]", ['value' => $this->edit === true ? $translations[0]['article_translation_long_title'] : null]);
         $article_translation_description[$locale] = new TextArea("translations[{$locale}][article_translation_description]", ['value' => $this->edit === true ? $translations[0]['article_translation_description'] : null]);
         $article_translation_slug[$locale] = new Text("translations[{$locale}][article_translation_slug]", ['value' => $this->edit === true ? $translations[0]['article_translation_slug'] : null]);
         $article_translation_lang[$locale] = new Hidden("translations[{$locale}][article_translation_lang]", ['value' => $locale]);
         $this->add($article_translation_short_title[$locale]);
         $this->add($article_translation_long_title[$locale]);
         $this->add($article_translation_description[$locale]);
         $this->add($article_translation_slug[$locale]);
         $this->add($article_translation_lang[$locale]);
     }
     // Categories
     $categories = new Select('categories[]', CategoryTranslation::find(["category_translation_lang = 'en'"]), ['using' => ['category_translation_category_id', 'category_translation_name'], 'multiple' => true]);
     if ($this->edit === true) {
         $categories_defaults = array();
         foreach ($entity->getCategories(["columns" => ["id"]]) as $category) {
             $categories_defaults[] = $category->id;
         }
         $categories->setDefault($categories_defaults);
     }
     $this->add($categories);
     // Hash tags
     $hashtags = new Select('hashtags[]', Hashtag::find(), ['using' => ['id', 'hashtag_name'], 'multiple' => true]);
     if ($this->edit === true) {
         $hashtags_defaults = array();
         foreach ($entity->getHashtags(["columns" => ["id"]]) as $hashtag) {
             $hashtags_defaults[] = $hashtag->id;
         }
         $hashtags->setDefault($hashtags_defaults);
     }
     $this->add($hashtags);
     // Is published
     $this->add(new Select('article_is_published', array(1 => 'Yes', 0 => 'No')));
     //CSRF
     $csrf = new Hidden('csrf');
     $csrf->addValidator(new Identical(array('value' => $this->security->getSessionToken(), 'message' => 'CSRF validation failed')));
     $this->add($csrf);
     $this->add(new Submit('save', array('class' => 'btn btn-lg btn-primary btn-block')));
 }
 /**
  * Delete an existing record
  * @param  number     $id
  * @throws \Exception
  * @return boolean
  */
 public function delete($id)
 {
     $object = Hashtag::findFirstById($id);
     if (!$object) {
         throw new \Exception('Hashtag not found');
         //Exceptions are caught in the controller
     }
     if (false === $object->delete()) {
         foreach ($object->getMessages() as $message) {
             $error[] = (string) $message;
         }
         throw new \Exception(json_encode($error));
     }
     return true;
 }
 /**
  * Update an existing article
  *
  * @param  number     $id
  * @param  array      $input_data
  * @throws \Exception
  * @return unknown
  */
 public function update($input_data)
 {
     $article = Article::findFirstById($input_data['id']);
     if (!$article) {
         throw new \Exception('Article not found', 404);
     }
     $data = $this->prepareData($input_data);
     $article->setArticleIsPublished($data['article_is_published']);
     $article->setArticleUpdatedAt(new \Phalcon\Db\RawValue('NOW()'));
     foreach ($data['translations'] as $lang => $translation) {
         $article->getTranslations()->filter(function ($t) use($lang, $translation) {
             if ($t->getArticleTranslationLang() == $lang) {
                 $t->assign($translation);
                 $t->update();
             }
         });
     }
     $results = ArticleCategoryArticle::findByArticleId($input_data['id']);
     if ($results) {
         $results->delete();
     }
     if ($data['categories']) {
         $article->categories = Category::find(["id IN (" . $data['categories'] . ")"])->filter(function ($category) {
             return $category;
         });
     }
     $results = ArticleHashtagArticle::findByArticleId($input_data['id']);
     if ($results) {
         $results->delete();
     }
     if ($data['hashtags']) {
         $article->hashtags = Hashtag::find(["id IN (" . $data['hashtags'] . ")"])->filter(function ($hashtag) {
             return $hashtag;
         });
     }
     $user = User::findFirstById((int) $data['article_user_id']);
     if (!$user) {
         throw new \Exception('User not found', 404);
     }
     $article->setArticleUserId($data['article_user_id']);
     return $this->save($article, 'update');
 }