/** * Get related blogs to this blog * * @param ElggBlog $entity the blog to relate to * @param int $limit number of blogs to return * * @return false|ElggBlog[] */ function blog_tools_get_related_blogs(ElggBlog $entity, $limit = 4) { $limit = sanitise_int($limit, false); if (!$entity instanceof ElggBlog) { return false; } // transform to values $tag_values = $entity->tags; if (empty($tag_values)) { return false; } if (!is_array($tag_values)) { $tag_values = [$tag_values]; } // find blogs with these metadatavalues $options = ['type' => 'object', 'subtype' => 'blog', 'metadata_name' => 'tags', 'metadata_values' => $tag_values, 'wheres' => ["(e.guid <> {$entity->getGUID()})"], 'group_by' => 'e.guid', 'order_by' => 'count(msn.id) DESC', 'limit' => $limit]; return elgg_get_entities_from_metadata($options); }
/** * Pull together blog variables for the save form * * @param ElggBlog $post * @param ElggAnnotation $revision * @return array */ function blog_prepare_form_vars($post = NULL, $revision = NULL) { // input names => defaults $values = array('title' => NULL, 'description' => NULL, 'status' => 'published', 'access_id' => ACCESS_DEFAULT, 'comments_on' => 'On', 'excerpt' => NULL, 'tags' => NULL, 'container_guid' => NULL, 'guid' => NULL, 'draft_warning' => ''); if ($post) { foreach (array_keys($values) as $field) { if (isset($post->{$field})) { $values[$field] = $post->{$field}; } } } if (elgg_is_sticky_form('blog')) { $sticky_values = elgg_get_sticky_values('blog'); foreach ($sticky_values as $key => $value) { $values[$key] = $value; } } elgg_clear_sticky_form('blog'); if (!$post) { return $values; } // load the revision annotation if requested if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) { $values['revision'] = $revision; $values['description'] = $revision->value; } // display a notice if there's an autosaved annotation // and we're not editing it. if ($auto_save_annotations = $post->getAnnotations('blog_auto_save', 1)) { $auto_save = $auto_save_annotations[0]; } else { $auto_save == FALSE; } if ($auto_save && $auto_save->id != $revision->id) { $values['draft_warning'] = elgg_echo('blog:messages:warning:draft'); } return $values; }
// remove sticky form entries elgg_clear_sticky_form('blog'); // remove autosave draft if exists $blog->deleteAnnotations('blog_auto_save'); // no longer a brand new post. $blog->deleteMetadata('new_post'); // if this was an edit, create a revision annotation if (!$new_post && $revision_text) { $blog->annotate('blog_revision', $revision_text); } system_message(elgg_echo('blog:message:saved')); $status = $blog->status; // add to river if changing status or published, regardless of new post // because we remove it for drafts. if (($new_post || $old_status == 'draft') && $status == 'published') { add_to_river('river/object/blog/create', 'create', $blog->owner_guid, $blog->getGUID()); // we only want notifications sent when post published register_notification_object('object', 'blog', elgg_echo('blog:newpost')); elgg_trigger_event('publish', 'object', $blog); // reset the creation time for posts that move from draft to published if ($guid) { $blog->time_created = time(); $blog->save(); } } elseif ($old_status == 'published' && $status == 'draft') { elgg_delete_river(array('object_guid' => $blog->guid, 'action_type' => 'create')); } //Khang Added forward(REFERER); if ($blog->status == 'published' || $save == false) { forward($blog->getURL());
// remove sticky form entries elgg_clear_sticky_form('blog'); // remove autosave draft if exists $blog->deleteAnnotations('blog_auto_save'); // no longer a brand new post. $blog->deleteMetadata('new_post'); // if this was an edit, create a revision annotation if (!$new_post && $revision_text) { $blog->annotate('blog_revision', $revision_text); } system_message(elgg_echo('blog:message:saved')); $status = $blog->status; // add to river if changing status or published, regardless of new post // because we remove it for drafts. if (($new_post || $old_status == 'draft') && $status == 'published') { add_to_river('river/object/blog/create', 'create', elgg_get_logged_in_user_guid(), $blog->getGUID()); if ($guid) { $blog->time_created = time(); $blog->save(); } } elseif ($old_status == 'published' && $status == 'draft') { elgg_delete_river(array('object_guid' => $blog->guid, 'action_type' => 'create')); } if ($blog->status == 'published' || $save == false) { forward($blog->getURL()); } else { forward("blog/edit/{$blog->guid}"); } } else { register_error(elgg_echo('blog:error:cannot_save')); forward($error_forward_url);
// remove sticky form entries elgg_clear_sticky_form('blog'); // remove autosave draft if exists $blog->deleteAnnotations('blog_auto_save'); // no longer a brand new post. $blog->deleteMetadata('new_post'); // if this was an edit, create a revision annotation if (!$new_post && $revision_text) { $blog->annotate('blog_revision', $revision_text); } system_message(elgg_echo('blog:message:saved')); $status = $blog->status; // add to river if changing status or published, regardless of new post // because we remove it for drafts. if (($new_post || $old_status == 'draft') && $status == 'published') { elgg_create_river_item(array('view' => 'river/object/blog/create', 'action_type' => 'create', 'subject_guid' => $blog->owner_guid, 'object_guid' => $blog->getGUID())); elgg_trigger_event('publish', 'object', $blog); // reset the creation time for posts that move from draft to published if ($guid) { $blog->time_created = time(); $blog->save(); } } elseif ($old_status == 'published' && $status == 'draft') { elgg_delete_river(array('object_guid' => $blog->guid, 'action_type' => 'create')); } if ($blog->status == 'published' || $save == false) { forward($blog->getURL()); } else { forward("blog/edit/{$blog->guid}"); } } else {
$auto_save_annotations = $blog->getAnnotations(array('annotation_name' => 'blog_auto_save', 'limit' => 1)); if ($auto_save_annotations) { $auto_save = $auto_save_annotations[0]; } else { $auto_save = FALSE; } if (!$auto_save) { $annotation_id = $blog->annotate('blog_auto_save', $description); } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value != $description) { $blog->deleteAnnotations('blog_auto_save'); $annotation_id = $blog->annotate('blog_auto_save', $description); } elseif ($auto_save instanceof ElggAnnotation && $auto_save->value == $description) { // this isn't an error because we have an up to date annotation. $annotation_id = $auto_save->id; } if (!$annotation_id) { $error = elgg_echo('blog:error:cannot_auto_save'); } } } else { $error = elgg_echo('blog:error:missing:description'); } if ($error) { $json = array('success' => FALSE, 'message' => $error); echo json_encode($json); } else { $msg = elgg_echo('blog:message:saved'); $json = array('success' => TRUE, 'message' => $msg, 'guid' => $blog->getGUID()); echo json_encode($json); } exit;
/** * Web service for making a blog post * * @param string $title the title of blog * @param $body * @param $comment_status * @param string $access Access level of blog * * @param $status * @param $username * @param string $tags tags for blog * @param string $excerpt the excerpt of blog * @return bool * @throws InvalidParameterException * @internal param $description * @internal param $container_guid * @internal param string $text the content of blog * @internal param string $username username of author */ function blog_save_post($title, $body, $comment_status, $access, $status, $username, $tags, $excerpt) { if (!$username) { $user = elgg_get_logged_in_user_entity(); } else { $user = get_user_by_username($username); if (!$user) { throw new InvalidParameterException('registration:usernamenotvalid'); } } if ($access == 'ACCESS_FRIENDS') { $access_id = -2; } elseif ($access == 'ACCESS_PRIVATE') { $access_id = 0; } elseif ($access == 'ACCESS_LOGGED_IN') { $access_id = 1; } elseif ($access == 'ACCESS_PUBLIC') { $access_id = 2; } else { $access_id = -2; } $blog = new ElggBlog(); $blog->subtype = "blog"; $blog->owner_guid = $user->guid; $blog->container_guid = $user->guid; $blog->access_id = $access_id; $blog->description = $body; $blog->title = elgg_substr(strip_tags($title), 0, 140); $blog->status = $status; $blog->comments_on = $comment_status; $blog->excerpt = strip_tags($excerpt); $blog->tags = string_to_tag_array($tags); $guid = $blog->save(); $newStatus = $blog->status; if ($guid > 0 && $newStatus == 'published') { elgg_create_river_item(array('view' => 'river/object/blog/create', 'action_type' => 'create', 'subject_guid' => $blog->owner_guid, 'object_guid' => $blog->getGUID())); elgg_trigger_event('publish', 'object', $blog); if ($guid) { $blog->time_created = time(); $blog->save(); } $return['guid'] = $guid; $return['message'] = $newStatus; } else { $return['guid'] = $guid; $return['message'] = $status; } return $return; }