Example #1
0
 public function update($id, $args)
 {
     $langs = get_langs();
     $post = $this->findItem($id);
     if (isset($args['image'])) {
         $post->image = get_path($args['image']);
     }
     if (isset($args['post_status'])) {
         $post->post_status = $args['post_status'];
     }
     if (!$post->update()) {
         throw new ExcuteException('save_error', 'Không lưu được');
     }
     $post->cats()->detach();
     if (isset($args['cats'])) {
         $post->cats()->attach($args['cats']);
     }
     if (isset($args['newtags'])) {
         $newtags = $args['newtags'];
         if ($newtags) {
             foreach ($newtags as $tag) {
                 $newtag = new Category();
                 $newtag->type = 'tag';
                 $newtag->save();
                 $newtag->langs()->attach(default_lang_id(), ['name' => $tag, 'slug' => toSlug($tag)]);
                 $post->cats()->attach($newtag->id);
             }
         }
     }
     if (isset($args['availtags'])) {
         $availtags = $args['availtags'];
         if ($availtags) {
             $post->cats()->attach($availtags);
         }
     }
     $syncs = [];
     foreach ($langs as $lang) {
         $code = $args[$lang->code];
         if (isset($code['post_title'])) {
             $this->post_title = $code['post_title'];
         }
         if (isset($code['slug'])) {
             $this->slug = $code['slug'];
         }
         $slug = trim($this->slug) == '' ? toSlug($this->post_title) : toSlug($this->slug);
         if (isset($code['post_content'])) {
             $this->post_content = $code['post_content'];
         }
         if (isset($code['post_excerpt'])) {
             $this->post_excerpt = $code['post_excerpt'];
         }
         if (isset($code['custom'])) {
             $this->custom = $code['custom'];
         }
         $post_desc = ['post_title' => $this->post_title, 'slug' => $slug, 'post_content' => $this->post_content, 'post_excerpt' => $this->post_excerpt, 'custom' => $this->custom];
         $syncs[$lang->id] = $post_desc;
     }
     $post->langs()->sync($syncs);
 }