Beispiel #1
0
 public static function create_tags_for_post(post_model $post, $tags)
 {
     if ($tags === '') {
         return;
     }
     $post_tags = [];
     $tag_names = array_map('trim', explode(',', $tags));
     $old_tags = tag_model::get_in('name', $tag_names);
     if ($old_tags === []) {
         foreach ($tag_names as $tag_name) {
             $tag_id = tag_model::add(array('name' => $tag_name, 'refer_count' => 1));
             $post_tags[] = array('post_id' => $post->id, 'tag_id' => $tag_id);
         }
         setting_model::inc_by_id(array('value' => count($tag_names)), setting_model::id_tag_count);
     } else {
         tag_model::inc_by_ids(array('refer_count' => 1), array_keys($old_tags));
         $old_tag_names = [];
         foreach ($old_tags as $old_tag) {
             $post_tags[] = array('post_id' => $post->id, 'tag_id' => $old_tag->id);
             $old_tag_names[] = $old_tag->name;
         }
         $new_tag_names = array_diff($tag_names, $old_tag_names);
         if ($new_tag_names !== []) {
             foreach ($new_tag_names as $new_tag_name) {
                 $tag_id = tag_model::add(array('name' => $new_tag_name, 'refer_count' => 1));
                 $post_tags[] = array('post_id' => $post->id, 'tag_id' => $tag_id);
             }
             setting_model::inc_by_id(array('value' => count($new_tag_names)), setting_model::id_tag_count);
         }
     }
     if ($post_tags !== []) {
         post_tag_model::add_many($post_tags);
     }
 }