public function Sync($tag_list) { // Requere a valid post_id $myPost = new Post(); $myPost->SetPrimary($this->GetValue('post_id')); if (!$myPost->GetInfo()) { return false; } // Get a list of all the tags for this post $tmpPostTag = new PostTag(); $tmpPostTag->SetValue('post_id', $myPost->GetPrimary()); $tmpTag = new Tag(); $tmpPostTag->Join($tmpTag, 'tag_id', 'LEFT'); $tmpPostTag->GetList(); // Create an array worth using $old_tags = array(); foreach ($tmpPostTag->results as $tag) { $old_tags[$tag['post_tag_id']] = $tag['tag']; } // Split up the categories and make sure there is tags $new_tags = explode(',', strtolower($tag_list)); foreach ($new_tags as $key => $data) { $new_tags[$key] = trim($data); } // Get the difference $diff_tags = array_diff($new_tags, $old_tags); // Add the different tags to the database foreach ($diff_tags as $tag) { // Save the Tag $tmpTag->ResetValues(); $tmpTag->SetValue('tag', $tag); $item = $tmpTag->GetAssoc('tag', 'tag', 'ASC', 0, 1); if (count($item) == 1) { $tmpTag->SetPrimary(key($item)); } else { $tmpTag->Save(); } $tmpPostTag->ResetValues(); $tmpPostTag->SetValue('post_id', $myPost->GetPrimary()); $tmpPostTag->SetValue('tag_id', $tmpTag->GetPrimary()); $tmpPostTag->Save(); } // Get a list of the tags to remove $rem_tags = array_diff($old_tags, $new_tags); // Remove the non-needed tags foreach ($rem_tags as $id => $tag) { $tmpPostTag->ResetValues(); $tmpPostTag->SetPrimary($id); $tmpPostTag->Delete(); } }