Commit 2d3679e5 authored by Jonathan Hunt's avatar Jonathan Hunt

#53 Output pitch fields. Output route images, comments.

parent 95a92250
......@@ -236,6 +236,7 @@ function _climbnz_api_place_relation($entity) {
function climbnz_api_bundle_to_type_map() {
return array(
'comment_node_place' => 'comment',
'comment_node_route' => 'comment',
'image' => 'image',
'place' => 'place',
'route' => 'route',
......@@ -439,6 +440,13 @@ function _climbnz_api_map_node_to_route($entity, $parameters) {
// Need node as well...
$node = node_load($entity->getIdentifier());
$created = $entity->created->value();
$timezone = variable_get('date_default_timezone', 'Pacific/Auckland');
$date_created = new DateTime('@' . $created);
date_timezone_set($date_created, timezone_open($timezone));
$updated = $entity->changed->value();
$date_updated = new DateTime('@' . $updated);
$description = $entity->field_description->value();
if (count($node->field_pitch[LANGUAGE_NONE]) == 1) {
$description = empty($description) ? $node->field_pitch[LANGUAGE_NONE][0]['description'] : $description . "\n" . $node->field_pitch[LANGUAGE_NONE][0]['description'];
......@@ -482,41 +490,40 @@ function _climbnz_api_map_node_to_route($entity, $parameters) {
'uuid' => $entity->uuid->value(),
'routeType' => $entity->field_route_type->value(),
'grade' => $grade,
//'url' => url('node/' . $entity->getIdentifier(), array('absolute' => TRUE)),
'length' => $entity->field_length->value(),
'length' => (int)$entity->field_length->value(),
'length_unit' => 'm',
'reference' => $entity->field_reference->value(),
'quality' => $quality, // 34= 1 star, 67=2 star?
'quality' => (int)$quality, // 34= 1 star, 67=2 star?
'gone' => $entity->field_gone->value(), // @todo yes,no?
'attribution' => $entity->field_attribution->value(),
'description' => $description,
'ascent' => $entity->field_ascent->value(), // Note: can be array().
'bolts' => $entity->field_bolts->value(),
'natural_pro' => $entity->field_natural_pro->value(),
// meta
'created' => date_format($date_created, 'c'),
'updated' => date_format($date_updated, 'c'),
'pitches_count' => count($node->field_pitch[LANGUAGE_NONE]),
),
'links' => array(
'self' => url($endpoint_path . '/routes/' . $entity->uuid->value(), array('absolute' => true)),
'url' => url('node/' . $entity->getIdentifier(), array('absolute' => true)),
),
);
// Inject pitches (depends on Route type)
// Inject pitches.
if (count($node->field_pitch[LANGUAGE_NONE]) > 1) {
foreach ($node->field_pitch[LANGUAGE_NONE] as $pitch) {
$pitch_grade = '';
$length = 0;
$bolts = 0;
$natural_pro = 0;
switch ($entity->field_route_type->value()) {
case 'alpine':
break;
}
$pitch_grade = climbnz_route_pitch_grades($entity->field_route_type->value(), $pitch);
$output['attributes']['pitches'][] = array(
'grade' => $pitch_grade = climbnz_route_pitch_grades($entity->field_route_type->value(), $pitch),
'length' => '',
'grade' => $pitch_grade,
'description' => $pitch['description'],
'trad' => $pitch['trad'] ? true : false,
'bolts' => (int)$pitch['bolts'],
'length' => (int)$pitch['length'],
'length_unit' => 'm', // metres.
);
}
}
......@@ -525,9 +532,62 @@ function _climbnz_api_map_node_to_route($entity, $parameters) {
return $output;
}
// relationships
// images
// comments
// Relations.
// Gather various related files, nodes and comments, output as entities to array.
$relations = array('images', 'comments');
foreach ($relations as $relation) {
$related_images = array();
$related_comments = array();
switch ($relation) {
case 'images':
$related_images = $node->field_image[LANGUAGE_NONE];
break;
case 'comments':
$related_comments = $node->comments;
break;
}
$related = array();
if ($related_images) {
foreach ($related_images as $key => $related_image) {
$file = (object) $related_image;
$related_file_entity = entity_metadata_wrapper('file', $file);
$related[] = $related_file_entity;
}
}
if ($related_comments) {
foreach ($related_comments as $key => $related_comment) {
$related_comment_entity = entity_metadata_wrapper('comment', $related_comment);
$related[] = $related_comment_entity;
}
}
$output['relationships'][$relation] = array(
'data' => _climbnz_api_entities_to_data_array($related),
);
$included[$relation] = $related;
}
// Did client request resources to be included in response?
if (isset($parameters['include'])) {
$included_resources_labels = explode(',', $parameters['include']);
foreach ($included_resources_labels as $included_resource_label) {
if (!empty($included[$included_resource_label])) {
foreach ($included[$included_resource_label] as $delta => $included_resource) {
$bundle = $included_resource->getBundle();
switch ($bundle) {
case 'comment_node_route':
$output['included'][] = _climbnz_api_map_comment($included_resource);
break;
case 'image':
// Augment image with title from Place field.
$output['included'][] = _climbnz_api_map_image($included_resource, $node->field_image[LANGUAGE_NONE][$delta]);
break;
}
}
}
}
}
return $output;
}
......
......@@ -100,6 +100,14 @@ function climbnz_route_node_presave($node) {
}
}
/**
* Return grade for a pitch based on route type, e.g. alpine, rock.
*
* @param $route_type
* @param $pitch
*
* @return array|string
*/
function climbnz_route_pitch_grades($route_type, $pitch) {
$grades = array();
$grade_ewbank_options = climbnz_pitch_grade_ewbank_options();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment