예제 #1
0
 /**
  * Create a new record
  * @param  array                     $input_data
  * @throws \Exception
  * @return \App\Core\Models\Category
  */
 public function create(array $input_data)
 {
     //Add some default data that can be overwritten by the input data using array_merge()
     $default_data = array('translations' => array('en' => array('category_translation_name' => 'Category name', 'category_translation_slug' => '', 'category_translation_lang' => 'en')), 'category_is_active' => 0);
     $data = array_merge($default_data, $input_data);
     $category = new Category();
     $category->setCategoryIsActive($data['category_is_active']);
     //Create all the CategoryTranslations for the Category
     $categoryTranslations = array();
     foreach ($data['translations'] as $lang => $translation) {
         $tmp = new CategoryTranslation();
         $tmp->assign($translation);
         $categoryTranslations[] = $tmp;
     }
     //Add all CategoryTranslations to the Category
     $category->translations = $categoryTranslations;
     return $this->save($category, 'create');
 }
예제 #2
0
 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')));
 }