You are here

function community_tags_from_js in Community Tags 6.2

Same name and namespace in other branches
  1. 5 community_tags.module \community_tags_from_js()
  2. 6 community_tags.ajax.inc \community_tags_from_js()
  3. 7 community_tags.ajax.inc \community_tags_from_js()

Callback for the JS tagger.

Related topics

1 string reference to 'community_tags_from_js'
community_tags_menu in ./community_tags.module
Implementation of hook_menu().

File

./community_tags.ajax.inc, line 17
community_tags.ajax.inc

Code

function community_tags_from_js($node, $vid = NULL) {
  global $user;
  if ($node && isset($_POST['token']) && drupal_valid_token($_POST['token'], 'community_tags_form')) {
    $tags = isset($_POST['tags']) && is_array($_POST['tags']) ? $_POST['tags'] : array();

    // Merge in new tag and save.
    $tags = array_unique(array_merge($tags, drupal_explode_tags($_POST['add'])));

    //There should always be $vid, but if not, this will make a best guess.
    if (!$vid) {
      $vid = array_shift(community_tags_vids_for_node($node));
    }
    community_tags_taxonomy_node_save($node, array(
      'tags' => array(
        $vid => $tags,
      ),
    ), FALSE, $user->uid);

    // Fetch updated list.
    $tags = community_tags_flatten(community_tags_get_user_node_tags($user, $node, $vid));
    $json = array(
      'status' => TRUE,
      'tags' => $tags,
      'sequence' => $_POST['sequence'],
    );
    $messages = drupal_get_messages('status', TRUE);
    if (!empty($messages['status'])) {
      $messages = implode('; ', $messages['status']);
      $json['messages'] = $messages;
    }
    drupal_alter('community_tags_json', $json, $node, $user, $vid);

    // Output JSON.
    print drupal_json($json);
  }
}