private function TripalContentService_v0_1::doEntityList

3.x TripalContentService_v0_1.inc private TripalContentService_v0_1::doEntityList($ctype)

Creates a collection of resources for a given type.

1 call to TripalContentService_v0_1::doEntityList()

File

tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc, line 807

Class

TripalContentService_v0_1

Code

private function doEntityList($ctype) {
  $service_path = $this->getServicePath() . '/' . preg_replace('/[^\w]/', '_', $ctype);
  $this->resource = new TripalWebServiceCollection($service_path, $this->params);

  // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  $term = reset($term);

  // The type of collection is provided by our API vocabulary service.
  $vocab_service = new TripalDocService_v0_1($this->base_path);
  $this->resource->addContextItem('vocab', $vocab_service->getServicePath() . '#');
  $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
  $this->resource->addContextItem($accession, 'vocab:' . $accession);
  $this->resource->setType($accession);

  // Convert term to a simple array
  $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);

  // Set the label for this collection.
  $label = tripal_get_term_details('rdfs', 'label');
  $this->addResourceProperty($this->resource, $label, $bundle->label . " Collection");

  // For quick lookup, get the mapping of WS keys to their appropriate fields.
  $field_mapping = $this->getFieldMapping($bundle);

  // Get arrays for filters and order by statements.
  $filters = $this->getFieldFilters($field_mapping, $bundle);
  $order_by = $this->getOrderBy($field_mapping, $bundle);

  // Initialize the query to search for records for our bundle types
  // that are published.
  $query = new TripalFieldQuery();
  $query->entityCondition('entity_type', 'TripalEntity');
  $query->entityCondition('bundle', $bundle->name);
  $query->propertyCondition('status', 1);

  if (array_key_exists('ids', $this->params)) {
    $eids = explode(',', $this->params['ids']);
    if (count($eids) > 1000) {
      throw new Exception('Please provide no more than 1000 ids.');
    }
    if (!is_numeric(implode('', $eids))) {
      throw new Exception('All supplied ids must be numeric.');
    }
    $query->entityCondition('entity_id', $eids, 'IN');
  }

  // Now iterate through the filters and add those.
  foreach ($filters as $key_field_name => $key_filters) {
    foreach ($key_filters as $i => $filter) {
      $column_name = $filter['column'];
      $value = $filter['value'];
      $op = $filter['op'];
      $query->fieldCondition($key_field_name, $column_name, $value, $op);
    }
  }

  // Now set the order by.
  foreach ($order_by as $key_field_name => $key_order) {
    foreach ($key_order as $i => $order) {
      $column_name = $order['column'];
      $dir = $order['dir'];
      $query->fieldOrderBy($key_field_name, $column_name, $dir);
    }
  }

  // Perform the query just as a count first to get the number of records.
  $cquery = clone $query;
  $cquery->count();
  $num_records = $cquery->execute();

  if (!$num_records) {
    $num_records = 0;
  }

  // Add in the pager to the response.
  $response['totalItems'] = $num_records;
  $limit = array_key_exists('limit', $this->params) ? $this->params['limit'] : 25;

  $total_pages = ceil($num_records / $limit);
  $page = array_key_exists('page', $this->params) ? $this->params['page'] : 1;

  // Set the query range
  $start = ($page - 1) * $limit;
  $query->range($start, $limit);

  // Now perform the query.
  $results = $query->execute();

  $this->resource->initPager($num_records, $limit, $page);

  // Check to make sure there are results.
  $entity_ids = array();
  if (isset($results['TripalEntity']) AND is_array($results['TripalEntity'])) {
    $entity_ids = $results['TripalEntity'];
  }

  // If the user wants to include any fields in the list then those provided
  // names need to be converted to fields.
  $add_fields = array();
  $add_field_ids = array();
  if (array_key_exists('fields', $this->params)) {
    $fields = explode(',', $this->params['fields']);
    foreach ($fields as $expfield) {
      list($field, $instance, $temp_term) = $this->findField($bundle, $expfield);
      if ($field) {
        $add_fields[$expfield]['field'] = $field;
        $add_fields[$expfield]['instance'] = $instance;
        $add_fields[$expfield]['term'] = $temp_term;
        $add_field_ids[] = $field['id'];
      }
      else {
        throw new Exception(t('The field named, "!field", does not exist.', array('!field' => $expfield)));
      }
    }
  }

  // Iterate through the entities and add them to the output list.
  foreach ($entity_ids as $entity_id => $stub) {
    // We don't need all of the attached fields for an entity so, we'll
    // not use the entity_load() function.  Instead just pull it from the
    // database table.
    $query = db_select('tripal_entity', 'TE');
    $query->join('tripal_term', 'TT', 'TE.term_id = TT.id');
    $query->fields('TE');
    $query->fields('TT', array('name'));
    $query->condition('TE.id', $entity_id);
    $entity = $query->execute()->fetchObject();

    $itemPage = tripal_get_term_details('schema', 'ItemPage');
    $label = tripal_get_term_details('rdfs', 'label');
    $member = new TripalWebServiceResource($service_path);
    $member->setID($entity->id);
    $this->setResourceType($member, $term);
    $this->addResourceProperty($member, $label, $entity->title);
    $this->addResourceProperty($member, $itemPage, url('/bio_data/' . $entity->id, array('absolute' => TRUE)));

    $entity = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $add_field_ids);
    $entity = $entity[$entity_id];

    // Add in any requested fields
    foreach ($fields as $expfield) {
      if (array_key_exists($expfield, $add_fields)) {
        $this->addEntityField($member, $add_fields[$expfield]['term'], $entity, 
        $bundle, $add_fields[$expfield]['field'], $add_fields[$expfield]['instance'], 
        $service_path);
      }
    }
    $this->resource->addMember($member);
  }
}