You are here

function comment_service_save in Services 7

Same name and namespace in other branches
  1. 6.2 services/comment_service/comment_service.inc \comment_service_save()

Adds a new comment to a node and returns the cid.

If a cid is specified in $comment->cid then that comment is edited.

Parameters

$comment: An array matching the form values that would be submitted in the comment edit form.

Return value

Unique identifier for the comment (cid) or FALSE if there was a problem.

1 string reference to 'comment_service_save'
comment_service_service in services/comment_service/comment_service.module
Implementation of hook_service().

File

services/comment_service/comment_service.inc, line 19
@author Services Dev Team

Code

function comment_service_save($comment) {

  // If the submitted comment does not contain a uid, set it to anonymous.
  if (isset($comment->uid)) {
    $uid = 0;
  }

  // If the submitted comment does not contain a nid, then return an error.
  if (!isset($comment->nid)) {
    return services_error(t("No node specified."));
  }

  // If a cid is present then the user is trying to edit an existing comment.
  if (isset($comment->cid) && $comment->cid != 0) {
    $initial_comment = _comment_load($comment->cid);
    $admin = user_access("administer comments");
    if ($initial_comment->uid == 0 && !$admin) {
      return services_error(t("Anonymous comments can't be edited"));
    }
    if (($initial_comment->uid != $comment->uid || comment_num_replies($comment->cid) != 0) && !$admin) {
      return services_error(t("User does not have permission to edit this comment"));
    }
  }

  // Can I just make a note here about how stupid it is that comment_load returns
  // an object but comment_save expects an array?
  return comment_save((array) $comment);
}