You are here

function drupalgap_services_request_postprocess_alter in DrupalGap 7

Same name and namespace in other branches
  1. 7.2 drupalgap.module \drupalgap_services_request_postprocess_alter()

Implements hook_services_request_postprocess_alter().

File

./drupalgap.module, line 109
A module to provide a bridge between Drupal websites and PhoneGap mobile applications.

Code

function drupalgap_services_request_postprocess_alter($controller, $args, &$result) {
  switch ($controller['callback']) {
    case '_system_resource_connect':

      // If the caller wants to skip extras, let them.
      if (isset($_GET['dgSkipExtras']) && $_GET['dgSkipExtras']) {
        return;
      }

      // Grab the extras DG provides to system connect and append them to result.
      module_load_include('inc', 'drupalgap', 'drupalgap.resource');
      $extras = drupalgap_system_connect_extras(false);
      foreach ($extras as $key => $value) {
        $result->{$key} = $value;
      }
      break;
    case '_comment_resource_retrieve':
    case '_comment_resource_index':

      // Iterate over all of the results and pull out node ids and user ids from
      // the comments.
      $nids = null;
      $uids = null;
      if ($controller['callback'] == '_comment_resource_index') {
        $nids = array();
        $uids = array();
        foreach ($result as $i => $comment) {
          if (!in_array($comment->nid, $nids)) {
            $nids[] = $comment->nid;
          }
          if (!in_array($comment->uid, $uids)) {
            $uids[] = $comment->uid;
          }
        }
      }
      else {

        // We're loading a comment, so just extract the single ids.
        $nids = $result->nid;
        $uids = $result->uid;
      }

      // On the comment retrieve resource, add the bundle to the result, it is
      // stored in the node_tye property.
      if ($controller['callback'] == '_comment_resource_retrieve') {
        $result->bundle = $result->node_type;
      }

      // If user profile pictures are enabled, add the picture uri to the
      // comment result(s).
      if (variable_get('user_pictures', 0)) {
        if ($controller['callback'] == '_comment_resource_retrieve' && $uids != 0) {
          watchdog('drupalgap', 'uid');
          $sql = "SELECT fm.uri, u.uid FROM {file_managed} fm " . "INNER JOIN {users} u ON u.picture = fm.fid WHERE u.uid = :uids";
          $picture = db_query($sql, array(
            ':uids' => $uids,
          ))
            ->fetch();
          $result->picture_uri = isset($picture->uri) ? $picture->uri : NULL;
        }
        else {
          if (!empty($uids)) {
            watchdog('drupalgap', 'uids');
            watchdog('drupalgap', '<pre>' . print_r($uids, true) . '</pre>');
            $sql = "SELECT fm.uri, u.uid FROM {file_managed} fm " . "INNER JOIN {users} u ON u.picture = fm.fid WHERE u.uid IN (:uids)";
            $pictures = db_query($sql, array(
              ':uids' => $uids,
            ))
              ->fetchAll();
            watchdog('drupalgap', '<pre>' . print_r($pictures, true) . '</pre>');
            foreach ($result as $i => $comment) {
              $uri = null;
              foreach ($pictures as $picture) {
                if ($picture->uid == $comment->uid) {
                  $uri = $picture->uri;
                  break;
                }
              }
              if ($uri) {
                $result[$i]->picture_uri = $uri;
              }
            }
          }
        }
      }

      // Let's add any custom fields to the index results.
      if ($controller['callback'] == '_comment_resource_index' && !empty($nids)) {

        // Grab a list of all the nids and their types, then build an array of
        // types keyed by their nid.
        $sql = "SELECT n.nid, n.type FROM {node} n WHERE n.nid IN (:nids)";
        $nids_and_types = db_query($sql, array(
          ':nids' => $nids,
        ))
          ->fetchAll();
        $types = array();
        foreach ($nids_and_types as $item) {
          $types[$item->nid] = $item->type;
        }

        // Build an array of entities, keyed by the entity id, and attach the
        // bundle to each, so we can load and attach the fields to each. We need
        // to attach the node_type property to comment entities, it is equal to
        // the bundle value, or we won't be able to always load the fields
        // properly.
        $entities = array();
        foreach ($result as $i => $comment) {
          $result[$i]->bundle = 'comment_node_' . $types[$comment->nid];
          $result[$i]->node_type = $result[$i]->bundle;
          $entities[$comment->cid] = $comment;
        }

        // @todo - what is this line used for?
        if (!empty($entities)) {
          field_attach_load('comment', $entities);
        }
        _drupalgap_add_term_data_to_entities($controller, $args, $result);
      }
      break;
    case '_node_resource_retrieve':
      if (isset($result->rdf_mapping)) {
        unset($result->rdf_mapping);
      }
      _drupalgap_add_term_data_to_entities($controller, $args, $result);
      break;
    case '_user_resource_login':

      // Remove rdf_mapping from user object.
      if ($result && isset($result->user) && isset($result->user->rdf_mapping)) {
        unset($result->user->rdf_mapping);
      }
      break;
  }
}