You are here

function merci_taxonomy_json in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 includes/menu.inc \merci_taxonomy_json()
  2. 7.2 includes/menu.inc \merci_taxonomy_json()

Get taxonomy data as JSON for a node

Parameters

$node_id: The node ID.

Return value

JSON string of taxonomy data

1 string reference to 'merci_taxonomy_json'
merci_menu in ./merci.module
Implementation of hook_menu().

File

./merci.module, line 2820
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_taxonomy_json($node_id) {
  $node = node_load($node_id);
  $output = array();
  if (node_access('update', $node)) {

    // Current user has access to update this node
    $vocabularies = taxonomy_get_vocabularies($node->type);
    if (isset($_POST['taxonomy'])) {

      // Changes to taxonomy sent
      $required_sent = TRUE;

      // Make sure all required vocabularies were sent
      foreach ($vocabularies as $vocabulary) {
        if ($vocabulary->required && !isset($_POST['taxonomy'][$vocabulary->vid])) {
          $required_sent = FALSE;
        }

        // if
      }

      // foreach
      if ($required_sent) {

        // Save new node revision, clear taxonomy, and re-add sent term
        node_save($node);
        taxonomy_node_delete_revision($node);
        foreach ($_POST['taxonomy'] as $vocabulary => $terms) {
          foreach ($terms as $id => $term_id) {
            db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $term_id);
          }

          // foreach
        }

        // foreach
        $output['status'] = 'success';
        $output['node'] = $node_id;
      }
      else {

        // Missing required vocabulary
        $output['status'] = 'failed';
        $output['error'] = 'Required fields not selected.';

        //DIFF??
      }

      // else
    }
    else {

      // Get last author admin status and node ID
      $user = user_load($node->revision_uid);
      $output['node'] = $node_id;
      $output['vocabularies'] = array();
      $output['admin'] = user_access('administer MERCI', $user);
      usort($vocabularies, 'merci_by_vid');

      // Get the select HTML and previously selected options for each vocabulary
      foreach ($vocabularies as $vocabulary) {
        $select = taxonomy_form($vocabulary->vid);
        $select['#name'] = 'taxonomy[' . $vocabulary->vid . ']';
        $select['#parents'] = array();
        $select_html = theme_select($select);
        $vocabulary_output = array(
          'select' => $select_html,
          'selected' => array(),
        );
        foreach ($node->taxonomy as $tid => $term) {
          $vocabulary_output['selected'][] = $tid;
        }
        $output['vocabularies'][] = $vocabulary_output;
      }

      // foreach
    }

    // else
  }
  else {

    // No permission
    $output['error'] = 'Permission denied.';
  }

  // else
  drupal_json($output);
}