Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding cod_api from github.com/zroger/cod_api for use as the data sou…

…rce of the ios app
  • Loading branch information...
commit f28f3e344808e56894162212234e6fa5442860db 1 parent 4b0c763
@zroger zroger authored
View
3  sites/all/modules/custom/cod_api/cod_api.info
@@ -0,0 +1,3 @@
+name = COD API
+description = JSON API for COD.
+core = 6.x
View
228 sites/all/modules/custom/cod_api/cod_api.module
@@ -0,0 +1,228 @@
+<?php
+
+/**
+ * Implementation of hook_menu().
+ */
+function cod_api_menu() {
+ $items = array();
+
+ $items['cod-api/combined'] = array(
+ 'type' => MENU_CALLBACK,
+ 'access arguments' => array('access content'),
+ 'page callback' => '_cod_api_json_node_list_combined',
+ );
+
+ $items['cod-api/%'] = array(
+ 'type' => MENU_CALLBACK,
+ 'access callback' => 'cod_api_node_type_access',
+ 'access arguments' => array(1),
+ 'page callback' => '_cod_api_json_node_list',
+ 'page arguments' => array(1),
+ );
+
+ $items['cod-api/%/%node'] = array(
+ 'type' => MENU_CALLBACK,
+ 'access callback' => 'cod_api_menu_node_access',
+ 'access arguments' => array('view', 1, 2),
+ 'page callback' => '_cod_api_node_json_page_callback',
+ 'page arguments' => array(2),
+ );
+
+ $items['cod-api/user/%user'] = array(
+ 'type' => MENU_CALLBACK,
+ 'access callback' => 'user_view_access',
+ 'access arguments' => array(2),
+ 'page callback' => '_cod_api_user_json_page_callback',
+ 'page arguments' => array(2),
+ );
+
+ return $items;
+}
+
+function cod_api_accessible_node_types() {
+ return array('session', 'sponsor', 'news');
+}
+
+function cod_api_node_type_access($type) {
+ return in_array($type, cod_api_accessible_node_types());
+}
+
+/**
+ * Menu access callback. Make sure that the passed type matches the node type
+ * and then delegate to node_access().
+ */
+function cod_api_menu_node_access($op, $type, $node) {
+ return ($type == $node->type) && cod_api_node_type_access($type) && node_access($op, $node);
+}
+
+/**
+ * Query wrapper to return an array of nodes of a given type.
+ */
+function _cod_api_get_nodes_by_type($type) {
+ $nodes = array();
+
+ $query = 'SELECT n.nid FROM {node} n
+ WHERE n.type = "%s" AND n.status = 1
+ ORDER BY n.sticky DESC, n.created DESC';
+ $result = db_query($query, $type);
+ while ($row = db_fetch_object($result)) {
+ $node = node_load($row->nid);
+ if (!empty($node)) {
+ $nodes[] = _cod_api_node_to_json($node);
+ }
+ }
+
+ return $nodes;
+}
+
+function _cod_api_timestamp_to_json($timestamp) {
+ return date_format_date(date_make_date($timestamp, NULL, DATE_UNIX), 'custom', 'Y-m-d\TH:i:s');
+}
+
+/**
+ * Prepare a node for rendering to json.
+ */
+function _cod_api_node_to_json($node) {
+ $json = (object)array(
+ 'nid' => $node->nid,
+ 'title' => $node->title,
+ 'created' => _cod_api_timestamp_to_json($node->created),
+ 'changed' => _cod_api_timestamp_to_json($node->changed),
+ 'body' => check_markup($node->body, $node->format, FALSE),
+ );
+ if (cod_api_node_type_access($node->type)) {
+ $json->uri = url('cod-api/'. $node->type .'/'. $node->nid, array('absolute' => TRUE));
+ }
+ drupal_alter('cod_api_node', $json, $node);
+ return $json;
+}
+
+/**
+ * Prepare a user for rendering to json.
+ */
+function _cod_api_user_to_json($account) {
+ $json = (object)array(
+ 'uid' => $account->uid,
+ 'name' => $account->name,
+ 'picture' => file_create_url($account->picture),
+ 'first_name' => $account->profile_first,
+ 'last_name' => $account->profile_last,
+ 'organization' => $account->profile_org,
+ 'uri' => url('cod-api/user/'. $account->uid, array('absolute' => TRUE)),
+ );
+ drupal_alter('cod_api_user', $json, $account);
+ return $json;
+}
+
+/**
+ * Page callback to render a single node.
+ */
+function _cod_api_node_json_page_callback($node) {
+ $data = array(
+ $node->type => _cod_api_node_to_json($node),
+ );
+
+ drupal_set_header("Content-Type: application/json; charset=utf-8");
+ print json_encode($data);
+ module_invoke_all('exit');
+ exit;
+}
+
+/**
+ * Page callback to render a single user.
+ */
+function _cod_api_user_json_page_callback($account) {
+ $data = array(
+ 'user' => _cod_api_user_to_json($account),
+ );
+
+ drupal_set_header("Content-Type: application/json; charset=utf-8");
+ print json_encode($data);
+ module_invoke_all('exit');
+ exit;
+}
+
+/**
+ * Page callback to render a list of nodes by type.
+ */
+function _cod_api_json_node_list($type) {
+ $data = array(
+ $type => _cod_api_get_nodes_by_type($type),
+ );
+
+ drupal_set_header("Content-Type: application/json; charset=utf-8");
+ print json_encode($data);
+ module_invoke_all('exit');
+ exit;
+}
+
+/**
+ * Page callback to render a combined list of nodes grouped by type.
+ */
+function _cod_api_json_node_list_combined() {
+ $data = array(
+ 'room' => _cod_api_get_nodes_by_type('room'),
+ 'session' => _cod_api_get_nodes_by_type('session'),
+ );
+
+ drupal_set_header("Content-Type: application/json; charset=utf-8");
+ print json_encode($data);
+ module_invoke_all('exit');
+ exit;
+}
+
+function cod_api_cod_api_node_alter(&$json, $node) {
+ switch ($node->type) {
+ case 'session':
+ $json->experience = $node->field_experience[0]['value'];
+
+ $field = content_fields('field_experience', 'session');
+ $json->experience = content_format($field, $node->field_experience[0], 'default', $node);
+
+ $field = content_fields('field_track', 'session');
+ $json->track = content_format($field, $node->field_track[0], 'default', $node);
+
+ $json->speakers = array();
+ foreach ($node->field_speakers as $delta => $speaker) {
+ $speaker = user_load($speaker);
+ $json->speakers[] = _cod_api_user_to_json($speaker);
+ }
+
+ if (!empty($node->field_session_slot[0]['nid']) && ($slot = node_load($node->field_session_slot[0]['nid']))) {
+ $fields = content_fields();
+ $field = $fields['field_slot_datetime'];
+ $data = $slot->field_slot_datetime[0];
+
+ $start_date = date_make_date($data['value'], $data['timezone'], $field['type'], $field['granularity']);
+ $end_date = date_make_date($data['value2'], $data['timezone'], $field['type'], $field['granularity']);
+
+ $json->start = date_format_date($start_date, 'custom', 'Y-m-d\TH:i:s');
+ $json->end = date_format_date($end_date, 'custom', 'Y-m-d\TH:i:s');
+ }
+
+ if (!empty($node->field_session_room[0]['nid']) && ($room = node_load($node->field_session_room[0]['nid']))) {
+ $json->room = $room->title;
+ }
+
+ $json->accepted = !empty($node->field_accepted[0]['value']);
+ break;
+
+ case 'room':
+ $json->capacity = $node->field_room_capacity[0]['value'];
+ break;
+
+ case 'sponsor':
+ $field = content_fields('field_sponsorship_level', 'sponsor');
+ $json->level = content_format($field, $node->field_sponsorship_level[0], 'default', $node);
+ $json->logo = file_create_url($node->field_logo[0]['filepath']);
+ $json->url = check_url($node->field_url[0]['url']);
+
+ break;
+
+ case 'news':
+ break;
+
+ default:
+ break;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.