TripalEntityService_v0_1.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. class TripalEntityService_v0_1 extends TripalWebService {
  3. /**
  4. * The human-readable label for this web service.
  5. */
  6. public static $label = 'Content Types';
  7. /**
  8. * A bit of text to describe what this service provides.
  9. */
  10. public static $description = 'Provides acesss to the biological and ' .
  11. 'ancilliary data available on this site. Each content type represents ' .
  12. 'biological data that is defined in a controlled vocabulary (e.g. ' .
  13. 'Sequence Ontology term: gene (SO:0000704)).';
  14. /**
  15. * A machine-readable type for this service. This name must be unique
  16. * among all Tripal web services and is used to form the URL to access
  17. * this service.
  18. */
  19. public static $type = 'content';
  20. /**
  21. * Implements the constructor
  22. */
  23. public function __construct() {
  24. parent::__construct();
  25. }
  26. /**
  27. * @see TripalWebService::handleRequest()
  28. */
  29. public function handleRequest() {
  30. // Get the content type.
  31. $ctype = (count($this->path) > 0) ? $this->path[0] : '';
  32. $entity_id = (count($this->path) > 1) ? $this->path[1] : '';
  33. // If we have no content type then list all of the available content types.
  34. if ($ctype and !$entity_id) {
  35. $this->doContentTypeList($ctype);
  36. }
  37. // If we don't have an entity ID then show a paged list of entities with
  38. // the given type.
  39. else if ($ctype and !$entity_id) {
  40. }
  41. // If we have a content type and an entity ID then show the entity
  42. else {
  43. $this->doAllTypesList();
  44. }
  45. }
  46. /**
  47. * Creates a collection of resources for a given type.
  48. */
  49. private function doContentTypeList($ctype) {
  50. $this->resource = new TripalWebServiceCollection();
  51. $this->resource->addContextItem('label', 'rdfs:label');
  52. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  53. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  54. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  55. $term = reset($term);
  56. // Set the label for this collection.
  57. $this->resource->addProperty('label', $bundle->label . " collection");
  58. // Iterate through the fields and create a $field_mapping array that makes
  59. // it easier to determine which filter criteria belongs to which field. The
  60. // key is the label for the field and the value is the field name. This way
  61. // user's can use the field label or the field name to form a query.
  62. $field_mapping = array();
  63. $fields = field_info_fields();
  64. foreach ($fields as $field) {
  65. if (array_key_exists('TripalEntity', $field['bundles'])) {
  66. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  67. if ($bundle_name == $bundle->name) {
  68. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  69. if (array_key_exists('term_accession', $instance['settings'])){
  70. $vocabulary = $instance['settings']['term_vocabulary'];
  71. $accession = $instance['settings']['term_accession'];
  72. $fterm = tripal_get_term_details($vocabulary, $accession);
  73. $key = $fterm['name'];
  74. $key = strtolower(preg_replace('/ /', '_', $key));
  75. $field_mapping[$key] = $field['field_name'];
  76. $field_mapping[$field['field_name']] = $field['field_name'];
  77. }
  78. }
  79. }
  80. }
  81. }
  82. // Convert the filters to their field names
  83. $new_params = array();
  84. $order = array();
  85. $order_dir = array();
  86. $URL_add = array();
  87. foreach ($this->params as $param => $value) {
  88. $URL_add[] = "$param=$value";
  89. // Ignore non filter parameters
  90. if ($param == 'page' or $param == 'limit') {
  91. continue;
  92. }
  93. // Handle order separately
  94. if ($param == 'order') {
  95. $temp = explode(',', $value);
  96. foreach ($temp as $key) {
  97. $matches = array();
  98. $dir = 'ASC';
  99. // The user can provide a direction by separating the field key and the
  100. // direction with a '|' character.
  101. if (preg_match('/^(.*)\|(.*)$/', $key, $matches)) {
  102. $key = $matches[1];
  103. if ($matches[2] == 'ASC' or $matches[2] == 'DESC') {
  104. $dir = $matches[2];
  105. }
  106. else {
  107. // TODO: handle error of providing an incorrect direction.
  108. }
  109. }
  110. if (array_key_exists($key, $field_mapping)) {
  111. $order[$field_mapping[$key]] = $key;
  112. $order_dir[] = $dir;
  113. }
  114. else {
  115. // TODO: handle error of providing a non existing field name.
  116. }
  117. }
  118. continue;
  119. }
  120. // Break apart any operators
  121. $key = $param;
  122. $op = '=';
  123. $matches = array();
  124. if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
  125. $key = $matches[1];
  126. $op = $matches[2];
  127. }
  128. // Break apart any subkeys and pull the first one out for the term name key.
  129. $subkeys = explode(',', $key);
  130. if (count($subkeys) > 0) {
  131. $key = array_shift($subkeys);
  132. }
  133. $column_name = $key;
  134. // Map the values in the filters to their appropriate field names.
  135. if (array_key_exists($key, $field_mapping)) {
  136. $field_name = $field_mapping[$key];
  137. if (count($subkeys) > 0) {
  138. $column_name .= '.' . implode('.', $subkeys);
  139. }
  140. $new_params[$field_name]['value'] = $value;
  141. $new_params[$field_name]['op'] = $op;
  142. $new_params[$field_name]['column'] = $column_name;
  143. }
  144. else {
  145. throw new Exception("The filter term, '$key', is not available for use.");
  146. }
  147. }
  148. // Get the list of entities for this bundle.
  149. $query = new TripalFieldQuery();
  150. $query->entityCondition('entity_type', 'TripalEntity');
  151. $query->entityCondition('bundle', $bundle->name);
  152. foreach($new_params as $field_name => $details) {
  153. $value = $details['value'];
  154. $column_name = $details['column'];
  155. switch ($details['op']) {
  156. case 'eq':
  157. $op = '=';
  158. break;
  159. case 'gt':
  160. $op = '>';
  161. break;
  162. case 'gte':
  163. $op = '>=';
  164. break;
  165. case 'lt':
  166. $op = '<';
  167. break;
  168. case 'lte':
  169. $op = '<=';
  170. break;
  171. case 'ne':
  172. $op = '<>';
  173. break;
  174. case 'contains':
  175. $op = 'CONTAINS';
  176. break;
  177. case 'starts':
  178. $op = 'STARTS WITH';
  179. break;
  180. default:
  181. $op = '=';
  182. }
  183. // We pass in the $column_name as an identifier for any sub fields
  184. // that are present for the fields.
  185. $query->fieldCondition($field_name, $column_name, $value, $op);
  186. }
  187. // Perform the query just as a count first to get the number of records.
  188. $cquery = clone $query;
  189. $cquery->count();
  190. $num_records = $cquery->execute();
  191. if (!$num_records) {
  192. $num_records = 0;
  193. }
  194. // Add in the pager to the response.
  195. $response['totalItems'] = $num_records;
  196. $limit = array_key_exists('limit', $this->params) ? $this->params['limit'] : 25;
  197. $total_pages = ceil($num_records / $limit);
  198. $page = array_key_exists('page', $this->params) ? $this->params['page'] : 1;
  199. // Set the query order
  200. $order_keys = array_keys($order);
  201. for($i = 0; $i < count($order_keys); $i++) {
  202. $query->fieldOrderBy($order_keys[$i], $order[$order_keys[$i]], $order_dir[$i]);
  203. }
  204. // Set the query range
  205. $start = ($page - 1) * $limit;
  206. $query->range($start, $limit);
  207. // Now perform the query.
  208. $results = $query->execute();
  209. //$this->resource->initPager($num_records, $params['limit']);
  210. $this->resource->initPager($num_records, 25);
  211. // Iterate through the entities and add them to the list.
  212. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  213. // We don't need all of the attached fields for an entity so, we'll
  214. // not use the entity_load() function. Instead just pull it from the
  215. // database table.
  216. $query = db_select('tripal_entity', 'TE');
  217. $query->join('tripal_term', 'TT', 'TE.term_id = TT.id');
  218. $query->fields('TE');
  219. $query->fields('TT', array('name'));
  220. $query->condition('TE.id', $entity_id);
  221. $entity = $query->execute()->fetchObject();
  222. $member = new TripalWebServiceResource();
  223. $member->addContextItem('label', 'rdfs:label');
  224. $member->addContextItem('itemPage', 'schema:itemPage');
  225. $member->addContextItem($term->name, $term->url);
  226. $member->setID(urldecode($bundle->label) . '/' . $entity->id);
  227. $member->setType($term->name);
  228. $member->addProperty('label', $entity->title);
  229. $member->addProperty('itemPage', url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
  230. $this->resource->addMember($member);
  231. }
  232. }
  233. /**
  234. * Creates a resources that contains the list of content types.
  235. */
  236. private function doAllTypesList() {
  237. $this->resource = new TripalWebServiceCollection();
  238. $this->resource->addContextItem('label', 'rdfs:label');
  239. $this->resource->addProperty('label', 'Content Types');
  240. // Get the list of published terms (these are the bundle IDs)
  241. $bundles = db_select('tripal_bundle', 'tb')
  242. ->fields('tb')
  243. ->orderBy('tb.label', 'ASC')
  244. ->execute();
  245. // Iterate through the terms and add an entry in the collection.
  246. $i = 0;
  247. while ($bundle = $bundles->fetchObject()) {
  248. $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
  249. $term = reset($entity);
  250. $vocab = $term->vocab;
  251. // Get the bundle description. If no description is provided then
  252. // use the term definition
  253. $description = tripal_get_bundle_variable('description', $bundle->id);
  254. if (!$description) {
  255. $description = $term->definition;
  256. }
  257. $member = new TripalWebServiceResource();
  258. $member->addContextItem($term->name, $term->url);
  259. $member->addContextItem('label', 'rdfs:label');
  260. $member->addContextItem('description', 'hydra:description');
  261. $member->setID(urlencode($bundle->label));
  262. $member->setType($term->name);
  263. $member->addProperty('label', $bundle->label);
  264. $member->addProperty('description', $description);
  265. $this->resource->addMember($member);
  266. }
  267. }
  268. }