/** * insertPostTags * * @param $post_id * @param $post_data */ public function insertPostTags($post_id, $tags) { $post_id = (int) $post_id; $tag_map = array(); $tags_table = Solar::factory('Foresmo_Model_Tags'); $existing_tags = $tags_table->fetchAllAsArray(); foreach ($existing_tags as $existing_tag) { foreach ($tags as $tag) { if (strtolower($tag) == strtolower($existing_tag['tag'])) { $tag_map[$tag] = $existing_tag['id']; } } } foreach ($tags as $tag) { if (array_key_exists($tag, $tag_map)) { $data = array('post_id' => $post_id, 'tag_id' => $tag_map[$tag]); $this->insert($data); } else { $data = array('tag' => $tag, 'tag_slug' => Foresmo::makeSlug($tag)); $last_insert_id = $tags_table->insert($data); $data = array('post_id' => $post_id, 'tag_id' => $last_insert_id); $this->insert($data); } } }
/** * updateContentTags * Update Tags for a post/page * @param $id * @param $tags */ public function updateContentTags($id, $tags) { if ($id != (int) $id) { return false; } $id = (int) $id; $tag_map = array(); $tags_table = Solar::factory('Foresmo_Model_Tags'); $existing_tags = $tags_table->fetchAllAsArray(); $content_tags = $tags_table->fetchTagsByID($id); foreach ($existing_tags as $existing_tag) { foreach ($tags as $tag) { if (Foresmo::makeSlug($tag) == $existing_tag['tag_slug']) { $t = $existing_tag['tag_slug']; $tag_map[$t] = $existing_tag['id']; } } } // find diff in tags to update and existing tags for content $ct_slugs = array(); $tag_slugs = array(); foreach ($content_tags as $t) { $ct_slugs[] = $t['tag_slug']; } foreach ($tags as $tag) { $tag_slugs[] = Foresmo::makeSlug($tag); } // delete tags if necessary $to_delete = array_diff($ct_slugs, $tag_slugs); $delete_tag_ids = array(); foreach ($to_delete as $del_tag) { $delete_tag_ids[] = $tags_table->fetchTagIdBySlug($del_tag); } if (!empty($to_delete)) { $this->deleteContentTagsById($id, $delete_tag_ids); } // add tags foreach ($tag_slugs as $tag) { if (array_key_exists($tag, $tag_map) && !$this->hasTag($id, $tag_map[$tag])) { $data = array('post_id' => $id, 'tag_id' => $tag_map[$tag]); $this->insert($data); } elseif (!array_key_exists($tag, $tag_map)) { $data = array('tag' => $tag, 'tag_slug' => Foresmo::makeSlug($tag)); $last_insert_id = $tags_table->insert($data); $data = array('post_id' => $id, 'tag_id' => $last_insert_id); $this->insert($data); } } }
/** * addContent * New blog post/page * * @return void */ public function addContent() { $errors = array(); if (!isset($this->_post['post_title']) || $this->validate('validateBlank', $this->_post['post_title'])) { $errors[] = 'Title cannot be blank.'; } if (!isset($this->_post['post_content']) || $this->validate('validateBlank', $this->_post['post_title'])) { $errors[] = 'Content cannot be blank.'; } $this->_post['post_slug'] = Foresmo::makeSlug($this->_post['post_title']); if (in_array(strtolower($this->_post['post_slug']), $this->_restricted_names)) { $errors[] = 'The slug for this post/page "' . $this->_post['post_slug'] . '" is restricted. Please choose a different slug/title'; } if (count($errors) > 0) { $message = implode('<br/>', $errors); $this->success = false; $this->message = $message; return; } if (!isset($this->_post['post_excerpt']) || $this->validate('validateBlank', $this->_post['post_excerpt'])) { $this->_post['post_excerpt'] = Foresmo::makeExcerpt($this->_post['post_content'], 60, '...'); } $last_insert_id = $this->_model->posts->insertContent($this->_post); if (!$this->validate('validateBlank', $this->_post['post_tags'])) { $tags = explode(',', rtrim(trim($this->_post['post_tags']), ',')); foreach ($tags as $key => $tag) { $tags[$key] = trim($tag); } $this->_model->posts_tags->insertContentTags($last_insert_id, $tags); } if (isset($this->_post['post_comments_disabled']) && $this->_post['post_comments_disabled'] == 'true') { $this->_model->post_info->insertCommentsDisabled($last_insert_id, true); } else { $this->_model->post_info->insertCommentsDisabled($last_insert_id, false); } if ((int) $this->_post['post_type'] == 1) { $message = "Successly created new post! <a href=\"/{$this->_post['post_slug']}\">View post</a>."; } elseif ((int) $this->_post['post_type'] == 2) { $message = "Successly created new page! <a href=\"/{$this->_post['post_slug']}\">View page</a>."; } $this->success = true; $this->data = array('id' => $last_insert_id); $this->message = $message; }
/** * ajax_admin_new_content * New blog post * * @param $post_data * @return string */ public function ajax_admin_new_content($post_data) { $errors = array(); if (!isset($post_data['post_title']) || $this->validate('validateBlank', $post_data['post_title'])) { $errors[] = 'Title cannot be blank.'; } if (!isset($post_data['post_content']) || $this->validate('validateBlank', $post_data['post_title'])) { $errors[] = 'Content cannot be blank.'; } $post_data['post_slug'] = Foresmo::makeSlug($post_data['post_title']); if (in_array(strtolower($post_data['post_slug']), $this->_restricted_names)) { $errors[] = 'The slug for this post/page "' . $post_data['post_slug'] . '" is restricted. Please choose a different slug/title'; } if (count($errors) > 0) { $message = implode('<br/>', $errors); $ret = array('success' => false, 'message' => $message); } else { $last_insert_id = $this->_model->posts->insertNewPost($post_data); if (!$this->validate('validateBlank', $post_data['post_tags'])) { $tags = explode(',', rtrim(trim($post_data['post_tags']), ',')); foreach ($tags as $key => $tag) { $tags[$key] = trim($tag); } $this->_model->posts_tags->insertPostTags($last_insert_id, $tags); } if (isset($post_data['post_comments_disabled']) && $post_data['post_comments_disabled'] == 'true') { $this->_model->post_info->insertCommentsDisabled($last_insert_id, true); } else { $this->_model->post_info->insertCommentsDisabled($last_insert_id, false); } if ((int) $post_data['post_type'] == 1) { $message = "Successly created new post! <a href=\"/{$post_data['post_slug']}\">View post</a>."; } elseif ((int) $post_data['post_type'] == 2) { $message = "Successly created new page! <a href=\"/{$post_data['post_slug']}\">View page</a>."; } $ret = array('success' => true, 'id' => $last_insert_id, 'message' => $message); } return json_encode($ret); }