TripalContentService_v0_1.inc 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. <?php
  2. class TripalContentService_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($base_path) {
  24. parent::__construct($base_path);
  25. }
  26. /**
  27. * @see TripalWebService::getDocumentation()
  28. */
  29. public function getDocumentation() {
  30. // Add the classes that this resource supports.
  31. $this->addDocBundleClasses();
  32. $this->addDocContentCollectionClass();
  33. return parent::getDocumentation();
  34. }
  35. /**
  36. * @see TripalWebService::handleRequest()
  37. */
  38. public function handleRequest() {
  39. // Get the content type.
  40. $ctype = (count($this->path) > 0) ? $this->path[0] : '';
  41. $entity_id = (count($this->path) > 1) ? $this->path[1] : '';
  42. $expfield = (count($this->path) > 2) ? $this->path[2] : '';
  43. // is this a valid content type?
  44. if ($ctype) {
  45. // Get the list of published terms (these are the bundle IDs)
  46. $bquery = db_select('tripal_bundle', 'tb');
  47. $bquery->join('tripal_term', 'tt', 'tt.id = tb.term_id');
  48. $bquery->join('tripal_vocab', 'tv', 'tv.id = tt.vocab_id');
  49. $bquery->fields('tb', array('label'));
  50. $bquery->fields('tt', array('accession'));
  51. $bquery->fields('tv', array('vocabulary'));
  52. $bquery->orderBy('tb.label', 'ASC');
  53. $bundles = $bquery->execute();
  54. // Iterate through the terms convert the santized name to the real label.
  55. $i = 0;
  56. $ctype_lookup = array();
  57. $found = FALSE;
  58. while ($bundle = $bundles->fetchObject()) {
  59. if ($ctype == preg_replace('/[^\w]/', '_', $bundle->label)) {
  60. $ctype = $bundle->label;
  61. $found = TRUE;
  62. }
  63. if ($ctype == $bundle->vocabulary . ':' . $bundle->accession) {
  64. $ctype = $bundle->label;
  65. $found = TRUE;
  66. }
  67. }
  68. if (!$found) {
  69. throw new Exception('Invalid content type: ' . $ctype);
  70. }
  71. }
  72. // If we have a content type then list all of the entities that belong
  73. // to it.
  74. if ($ctype and !$entity_id and !$expfield) {
  75. $this->doContentTypeList($ctype);
  76. }
  77. // If we have an entity ID then build the resource for a single entity.
  78. else if ($ctype and $entity_id and !$expfield) {
  79. $this->doEntity($ctype, $entity_id);
  80. }
  81. else if ($ctype and $entity_id and $expfield) {
  82. $this->doExpandedField($ctype, $entity_id, $expfield);
  83. }
  84. // Otherwise just list all of the available content types.
  85. else {
  86. $this->doAllTypesList();
  87. }
  88. }
  89. /**
  90. * Creates a resource for an expanded field of an entity.
  91. */
  92. private function doExpandedField($ctype, $entity_id, $expfield) {
  93. $service_path = $this->getServicePath() . '/' . urlencode($ctype) . '/' . $entity_id;
  94. $this->resource = new TripalWebServiceResource($service_path);
  95. // Get the TripalBundle, TripalTerm and TripalVocab for this type.
  96. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  97. // Find the field that matches the field name provided by the user.
  98. list($field, $instance, $term) = $this->findField($bundle, $expfield);
  99. if (!$field) {
  100. throw new Exception("Could not find a matching field for the name: $expfield");
  101. }
  102. // Get the TripalEntity
  103. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id), FALSE, array($field['id']));
  104. $entity = reset($entity);
  105. // If we couldn't find the entity then fail.
  106. if (!$entity) {
  107. throw new Exception("Cannot find the record with id $entity_id.");
  108. }
  109. // Check that the user has access to this entity. If not then the
  110. // function call will throw an error.
  111. $this->checkAccess($entity);
  112. // Next add in the ID and Type for this resources.
  113. $this->setResourceType($this->resource, $term);
  114. $this->resource->setID(urlencode($term['name']));
  115. if (!property_exists($entity, $field['field_name'])) {
  116. // Attach the field and then add its values to the response.
  117. field_attach_load($entity->type, array($entity->id => $entity),
  118. FIELD_LOAD_CURRENT, array('field_id' => $field['id']));
  119. }
  120. $this->addEntityField($term, $entity, $bundle, $field, $instance, $service_path, $expfield);
  121. }
  122. /**
  123. * Find the field whose term matches the one provied.
  124. */
  125. private function findField($bundle, $expfield) {
  126. $value = array();
  127. $instances = field_info_instances('TripalEntity', $bundle->name);
  128. foreach ($instances as $instance) {
  129. $field_name = $instance['field_name'];
  130. $field = field_info_field($field_name);
  131. $field_type = $field['type'];
  132. // Skip fields of remote data.
  133. if ($field_type == 'remote__data') {
  134. continue;
  135. }
  136. $vocabulary = $instance['settings']['term_vocabulary'];
  137. $accession = $instance['settings']['term_accession'];
  138. $temp_term = tripal_get_term_details($vocabulary, $accession);
  139. if ($temp_term['name'] == $expfield) {
  140. return array($field, $instance, $temp_term);
  141. }
  142. }
  143. }
  144. /**
  145. * Creates a resource for a single entity.
  146. */
  147. private function doEntity($ctype, $entity_id) {
  148. $service_path = $this->getServicePath() . '/' . urlencode($ctype);
  149. $this->resource = new TripalWebServiceResource($service_path);
  150. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  151. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  152. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  153. $term = reset($term);
  154. // Convert the $term to a simple array
  155. $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);
  156. // Add the vocabulary to the context.
  157. $this->resource->addContextItem($term['name'], $term['url']);
  158. // Get the TripalEntity.
  159. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  160. $entity = reset($entity);
  161. // If we couldn't match this field argument to a field and entity then return
  162. if (!$entity) {
  163. throw new Exception("Cannot find this record.");
  164. }
  165. // Check that the user has access to this entity. If not then the
  166. // function call will throw an error.
  167. $this->checkAccess($entity);
  168. $itemPage = tripal_get_term_details('schema', 'ItemPage');
  169. $label = tripal_get_term_details('rdfs', 'label');
  170. $this->resource->setID($entity_id);
  171. $this->setResourceType($this->resource, $term);
  172. $this->addResourceProperty($this->resource, $label, $entity->title);
  173. $this->addResourceProperty($this->resource, $itemPage, url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
  174. // Add in the entitie's fields.
  175. $this->addEntityFields($entity, $bundle, $term, $service_path);
  176. }
  177. /**
  178. * Ensures that user's only have access to content they should see.
  179. *
  180. * Denies access to an entity if it is unpublished or if the user does
  181. * not have permission to see it.
  182. *
  183. * @param $entity
  184. * The full entity object.
  185. *
  186. * @throws Exception
  187. */
  188. private function checkAccess($entity) {
  189. global $user;
  190. if (!tripal_entity_access('view', $entity, $user, 'TripalEntity')) {
  191. throw new Exception("Permission Denied.");
  192. }
  193. // Don't show entities that aren't published
  194. if ($entity->status == 0) {
  195. throw new Exception("This record is currently unavailable.");
  196. }
  197. }
  198. /**
  199. * Adds the fields as properties of an entity resource.
  200. */
  201. private function addEntityFields($entity, $bundle, $term, $service_path) {
  202. // If the entity is set to hide fields that have no values then we
  203. // want to honor that in the web services too.
  204. $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  205. // Get information about the fields attached to this bundle and sort them
  206. // in the order they were set for the display.
  207. $instances = field_info_instances('TripalEntity', $bundle->name);
  208. // Sort the instances by their weight.
  209. uasort($instances, function($a, $b) {
  210. $a_weight = (is_array($a) && isset($a['widget']['weight'])) ? $a['widget']['weight'] : 0;
  211. $b_weight = (is_array($b) && isset($b['widget']['weight'])) ? $b['widget']['weight'] : 0;
  212. if ($a_weight == $b_weight) {
  213. return 0;
  214. }
  215. return ($a_weight < $b_weight) ? -1 : 1;
  216. });
  217. // Iterate through the fields and add each value to the response.
  218. //$response['fields'] = $fields;
  219. foreach ($instances as $field_name => $instance) {
  220. // Skip hidden fields.
  221. if ($instance['display']['default']['type'] == 'hidden') {
  222. continue;
  223. }
  224. // Get the information about this field.
  225. $field = field_info_field($field_name);
  226. // Skip the remote__data field that is provided by the tripal_Ws
  227. // module.
  228. if ($field['type'] == 'remote__data') {
  229. continue;
  230. }
  231. // By default, the label for the key in the output should be the
  232. // term from the vocabulary that the field is assigned. But in the
  233. // case that the field is not assigned a term, we must use the field name.
  234. $field_name = $instance['field_name'];
  235. $vocabulary = $instance['settings']['term_vocabulary'];
  236. $accession = $instance['settings']['term_accession'];
  237. $term = tripal_get_term_details($vocabulary, $accession);
  238. if (!$term) {
  239. continue;
  240. }
  241. // If this field should not be attached by default then just add a link
  242. // so that the caller can get the information separately.
  243. $instance_settings = $instance['settings'];
  244. if (array_key_exists('auto_attach', $instance['settings']) and
  245. $instance_settings['auto_attach'] == FALSE) {
  246. // Add a URL only if there are values. If there are no values then
  247. // don't add a URL which would make the end-user think they can get
  248. // that information.
  249. $items = field_get_items('TripalEntity', $entity, $field_name);
  250. $term_key = $this->getContextTerm($term, array('lowercase', 'spacing'));
  251. $this->resource->addContextItem($term_key, $vocabulary . ':' . $accession);
  252. $this->resource->addContextItem($vocabulary . ':' . $accession, array(
  253. '@id' => $term['url'],
  254. '@type' => '@id'
  255. ));
  256. if ($items and count($items) > 0 and $items[0]['value']) {
  257. $this->addResourceProperty($this->resource, $term, $service_path . '/' . $entity->id . '/' . urlencode($term['name']), array('lowercase', 'spacing'));
  258. }
  259. else {
  260. if ($hide_fields == 'show') {
  261. $this->addResourceProperty($this->resource, $term, NULL, array('lowercase', 'spacing'));
  262. }
  263. }
  264. continue;
  265. }
  266. // Get the details for this field for the JSON-LD response.
  267. $this->addEntityField($term, $entity, $bundle, $field, $instance, $service_path);
  268. }
  269. }
  270. /**
  271. * Adds the field as a property of the entity resource.
  272. */
  273. private function addEntityField($term, $entity, $bundle, $field, $instance,
  274. $service_path, $expfield = NULL) {
  275. // If the entity is set to hide fields that have no values then we
  276. // want to honor that in the web services too.
  277. $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  278. // Get the field settings.
  279. $field_name = $field['field_name'];
  280. $field_settings = $field['settings'];
  281. $items = field_get_items('TripalEntity', $entity, $field_name);
  282. if (!$items) {
  283. return;
  284. }
  285. // Give modules the opportunity to edit values for web services. This hook
  286. // really should be used sparingly. Where it helps is with non Tripal fields
  287. // that are added to a TripalEntity content type and it doesn't follow
  288. // the rules (e.g. Image field).
  289. drupal_alter('tripal_ws_value', $items, $field, $instance);
  290. $values = array();
  291. for ($i = 0; $i < count($items); $i++) {
  292. if (array_key_exists('value', $items[$i])) {
  293. $values[$i] = $this->sanitizeFieldKeys($this->resource, $items[$i]['value'], $bundle, $service_path);
  294. }
  295. elseif ($field['type'] == 'image') {
  296. $url = file_create_url($items[$i]['uri']);
  297. $values[$i] = $this->sanitizeFieldKeys($this->resource, $url, $bundle, $service_path);
  298. }
  299. else {
  300. // TODO: handle this case.
  301. }
  302. }
  303. if ($hide_fields == 'hide' and empty($values[0])) {
  304. return;
  305. }
  306. // If the field cardinality is 1
  307. if ($field['cardinality'] == 1) {
  308. // If the value is an array and this is the field page then all of those
  309. // key/value pairs should be added directly to the response.
  310. if (is_array($values[0])) {
  311. if ($expfield) {
  312. foreach ($values[0] as $k => $v) {
  313. $this->resource->addProperty($k, $v);
  314. }
  315. }
  316. else {
  317. $this->addResourceProperty($this->resource, $term, $values[0], array('lowercase', 'spacing'));
  318. }
  319. }
  320. // If the value is not an array it's a scalar so add it as is to the
  321. // response.
  322. else {
  323. $this->addResourceProperty($this->resource, $term, $values[0], array('lowercase', 'spacing'));
  324. }
  325. }
  326. // If the field cardinality is > 1 or -1 (for unlimited)
  327. if ($field['cardinality'] != 1) {
  328. // If this is the expanded field page then we need to swap out
  329. // the resource for a collection.
  330. $response = new TripalWebServiceCollection($service_path . '/' . urlencode($expfield), $this->params);
  331. $label = tripal_get_term_details('rdfs', 'label');
  332. $this->addResourceProperty($response, $label, $instance['label']);
  333. $i = 0;
  334. foreach ($values as $delta => $element) {
  335. $member = new TripalWebServiceResource($service_path . '/' . urlencode($expfield));
  336. $member->setID($i);
  337. // Add the context of the parent resource because all of the keys
  338. // were santizied and set to match the proper context.
  339. $member->setContext($this->resource);
  340. $this->setResourceType($member, $term);
  341. foreach ($element as $key => $value) {
  342. $member->addProperty($key, $value);
  343. }
  344. $response->addMember($member);
  345. $i++;
  346. }
  347. if ($expfield) {
  348. $this->resource = $response;
  349. }
  350. else {
  351. //$this->resource->addProperty($key, $response);
  352. $this->addResourceProperty($this->resource, $term, $response, array('lowercase', 'spacing'));
  353. }
  354. }
  355. }
  356. /**
  357. * Rewrites the keys of a field's items array for use with web services.
  358. */
  359. private function sanitizeFieldKeys($resource, $value, $bundle, $service_path) {
  360. // If the entity is set to hide fields that have no values then we
  361. // want to honor that in the web services too.
  362. $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
  363. $new_value = '';
  364. // If the value is an array rather than a scalar then map the sub elements
  365. // to controlled vocabulary terms.
  366. if (is_array($value)) {
  367. $temp = array();
  368. foreach ($value as $k => $v) {
  369. // exclude fields that have no values so we can hide them
  370. if (!isset($v) and $hide_fields == 'hide') {
  371. continue;
  372. }
  373. $matches = array();
  374. if (preg_match('/^(.+):(.+)$/', $k, $matches)) {
  375. $vocabulary = $matches[1];
  376. $accession = $matches[2];
  377. $term = tripal_get_term_details($vocabulary, $accession);
  378. $key = $this->addContextTerm($resource, $term, array('lowercase', 'spacing'));
  379. if (is_array($v)) {
  380. $temp[$key] = $this->sanitizeFieldKeys($resource, $v, $bundle, $service_path);
  381. }
  382. else {
  383. $temp[$key] = $v;
  384. }
  385. $term['name'] = $key;
  386. }
  387. else {
  388. // TODO: this is an error, if we get here then we have
  389. // a key that isn't using the proper format... what to do?
  390. }
  391. }
  392. $new_value = $temp;
  393. // Recurse through the values array and set the entity elements
  394. // and add the fields to the context.
  395. $this->sanitizeFieldEntity($new_value, $service_path);
  396. }
  397. else {
  398. $new_value = $value;
  399. }
  400. return $new_value;
  401. }
  402. /**
  403. * Rewrites any TripalEntity elements in the values array for use with WS.
  404. */
  405. private function sanitizeFieldEntity(&$items, $service_path) {
  406. if (!$items) {
  407. return;
  408. }
  409. foreach ($items as $key => $value) {
  410. if (is_array($value)) {
  411. $this->sanitizeFieldEntity($items[$key], $service_path);
  412. continue;
  413. }
  414. if ($key == 'entity') {
  415. list($item_etype, $item_eid) = explode(':', $items['entity']);
  416. if ($item_eid) {
  417. $item_entity = tripal_load_entity($item_etype, array($item_eid));
  418. $item_entity = reset($item_entity);
  419. $bundle = tripal_load_bundle_entity(array('name' => $item_entity->bundle));
  420. $items['@id'] = $this->getServicePath() . '/' . urlencode($bundle->label) . '/' . $item_eid;
  421. }
  422. unset($items['entity']);
  423. }
  424. }
  425. }
  426. /**
  427. * A helper function to make it easy to map between keys and their fields.
  428. *
  429. * @bundle
  430. * The bundle object. Fields attached to this bundle will be included
  431. * in the mapping array.
  432. * @return
  433. * An associative arrray that maps web servcies keys to fields and
  434. * fields to web services keys (reciprocol).
  435. */
  436. private function getFieldMapping($bundle) {
  437. // Iterate through the fields and create a $field_mapping array that makes
  438. // it easier to determine which filter criteria belongs to which field. The
  439. // key is the label for the field and the value is the field name. This way
  440. // user's can use the field label or the field name to form a query.
  441. $field_mapping = array();
  442. $fields = field_info_fields();
  443. foreach ($fields as $field) {
  444. if (array_key_exists('TripalEntity', $field['bundles'])) {
  445. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  446. if ($bundle_name == $bundle->name) {
  447. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  448. if (array_key_exists('term_accession', $instance['settings'])){
  449. $vocabulary = $instance['settings']['term_vocabulary'];
  450. $accession = $instance['settings']['term_accession'];
  451. $fterm = tripal_get_term_details($vocabulary, $accession);
  452. $key = $fterm['name'];
  453. $key = strtolower(preg_replace('/ /', '_', $key));
  454. $field_mapping[$key] = $field['field_name'];
  455. $field_mapping[$field['field_name']] = $field['field_name'];
  456. }
  457. }
  458. }
  459. }
  460. }
  461. return $field_mapping;
  462. }
  463. /**
  464. * Gets any order by statements provided by the user.
  465. *
  466. * @field_mapping
  467. * An array that maps WS keys to field names. As provided by the
  468. * getFieldMapping() function.
  469. * @return
  470. * An array of fields for ordering.
  471. *
  472. * @throws Exception
  473. */
  474. private function getOrderBy($field_mapping, $bundle) {
  475. $order_by = array();
  476. // Handle order separately.
  477. if (array_key_exists('order', $this->params)) {
  478. $order_params = $this->params['order'];
  479. $dir = 'ASC';
  480. // If the user provided more than one order statement then those are
  481. // separated by a semicolon.
  482. $items = explode(';', $order_params);
  483. foreach ($items as $key) {
  484. // The user can provide a direction by separating the field key and the
  485. // direction with a '|' character.
  486. $matches = array();
  487. if (preg_match('/^(.*)\|(.*)$/', $key, $matches)) {
  488. $key = $matches[1];
  489. if ($matches[2] == 'ASC' or $matches[2] == 'DESC') {
  490. $dir = $matches[2];
  491. }
  492. else {
  493. throw new Exception('Please provide "ASC" or "DESC" for the ordering direction');
  494. }
  495. }
  496. // Break apart any subkeys and pull the first one as this is the parent
  497. // field.
  498. $subkeys = explode(',', $key);
  499. if (count($subkeys) > 0) {
  500. $key = $subkeys[0];
  501. }
  502. if (array_key_exists($key, $field_mapping)) {
  503. $key_field_name = $field_mapping[$key];
  504. $key_field = field_info_field($key_field_name);
  505. $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
  506. // Complex fields provied by the TripalField class may have sub
  507. // elements that support filtering. We need to see if the user
  508. // wants to filter on those.
  509. $field_class = $key_field['type'];
  510. if (tripal_load_include_field_class($field_class)) {
  511. // To find out which fields are sortable we'll call the
  512. // webServicesData() function.
  513. $key_field = new $field_class($key_field, $key_instance);
  514. $ws_data = $key_field->webServicesData();
  515. $sortable_keys = $ws_data['sortable'];
  516. $criteria = implode('.', $subkeys);
  517. if (array_key_exists($criteria, $sortable_keys)) {
  518. $order_by[$key_field_name][] = array(
  519. 'column' => $sortable_keys[$criteria],
  520. 'dir' => $dir,
  521. );
  522. }
  523. else {
  524. throw new Exception("The value, '$criteria', is not available for sorting.");
  525. }
  526. }
  527. // If this field is not a TripalField then it should just have
  528. // a simple value and we can query for that.
  529. else {
  530. $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
  531. $order_by[$key_field_name][] = array(
  532. 'column' => $key_field_id,
  533. 'dir' => $dir,
  534. );
  535. }
  536. }
  537. else {
  538. throw new Exception("The value, '$key', is not available for sorting.");
  539. }
  540. }
  541. }
  542. // If there is no ordering that is set then set a default order.
  543. if (count(array_keys($order_by)) == 0) {
  544. $key_field_names = array();
  545. if (in_array('data__identifier', $field_mapping)) {
  546. $key_field_names['data__identifier'][] = 'identifier';
  547. }
  548. else if (in_array('schema__name', $field_mapping)) {
  549. $key_field_names['schema__name'][] = 'name';
  550. }
  551. else if (in_array('rdfs_label', $field_mapping)) {
  552. $key_field_names['rdfs_label'][] = 'label';
  553. }
  554. else if (in_array('taxrank__genus', $field_mapping)) {
  555. $key_field_names['taxrank__genus'][] = 'genus';
  556. $key_field_names['taxrank__species'][] = 'species';
  557. }
  558. foreach ($key_field_names as $key_field_name => $criteria) {
  559. $key_field = field_info_field($key_field_name);
  560. $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
  561. $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
  562. $field_class = $key_field['type'];
  563. if (tripal_load_include_field_class($field_class)) {
  564. // To find out which fields are sortable we'll call the
  565. // webServicesData() function.
  566. $key_field = new $field_class($key_field, $key_instance);
  567. $ws_data = $key_field->webServicesData();
  568. $sortable_keys = $ws_data['sortable'];
  569. if (array_key_exists($criteria, $sortable_keys)) {
  570. $order_by[$key_field_name][] = array(
  571. 'column' => $sortable_keys[$criteria],
  572. 'dir' => $dir,
  573. );
  574. }
  575. }
  576. // If this field is not a TripalField then it should just have
  577. // a simple value and we can query for that.
  578. else {
  579. $order_by[$key_field_name][] = array(
  580. 'column' => $key_field_id,
  581. 'dir' => 'ASC',
  582. );
  583. }
  584. }
  585. }
  586. return $order_by;
  587. }
  588. /**
  589. * Gets any filter by statements provided by the user.
  590. *
  591. * @field_mapping
  592. * An array that maps WS keys to field names. As provided by the
  593. * getFieldMapping() function.
  594. *
  595. * @return
  596. * An array of fields for filtering.
  597. *
  598. * @throws Exception
  599. */
  600. private function getFilters($field_mapping, $bundle) {
  601. $filters = array();
  602. // Iterate through the paramter list provided by user.
  603. foreach ($this->params as $param => $value) {
  604. // Ignore non filter parameters.
  605. if ($param == 'page' or $param == 'limit') {
  606. continue;
  607. }
  608. // Ignore the order parameter as that is handled by the getOrderBy()
  609. // function
  610. if ($param == 'order') {
  611. continue;
  612. }
  613. // Break apart any operators
  614. $key = $param;
  615. $op = '=';
  616. $matches = array();
  617. if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
  618. $key = $matches[1];
  619. $op = $matches[2];
  620. }
  621. // Break apart any subkeys and pull the first one as this is the parent
  622. // field.
  623. $subkeys = explode(',', $key);
  624. if (count($subkeys) > 0) {
  625. $key = $subkeys[0];
  626. }
  627. // Map the values in the filters to their appropriate field names.
  628. if (array_key_exists($key, $field_mapping)) {
  629. $key_field_name = $field_mapping[$key];
  630. $key_field = field_info_field($key_field_name);
  631. $key_instance = field_info_instance('TripalEntity', $key_field_name, $bundle->name);
  632. // Complex fields provied by the TripalField class may have sub
  633. // elements that support filtering. We need to see if the user
  634. // wants to filter on those.
  635. $field_class = $key_field['type'];
  636. if (tripal_load_include_field_class($field_class)) {
  637. // To find out which fields are searchable we'll call the wsData()
  638. // function.
  639. $key_field = new $field_class($key_field, $key_instance);
  640. $ws_data = $key_field->webServicesData();
  641. $searchable_keys = $ws_data['searchable'];
  642. $criteria = implode('.', $subkeys);
  643. if (array_key_exists($criteria, $searchable_keys)) {
  644. $filters[$key_field_name][] = array(
  645. 'value' => $value,
  646. 'op' => $op,
  647. 'column' => $searchable_keys[$criteria]
  648. );
  649. }
  650. else {
  651. throw new Exception("The filter term, '$criteria', is not available for use.");
  652. }
  653. }
  654. // If this field is not a TripalField then it should just have
  655. // a simple value and we can query for that.
  656. else {
  657. $key_field_id = $key_instance['settings']['term_vocabulary'] . ':' . $key_instance['settings']['term_accession'];
  658. $filters[$key_field_name][] = array(
  659. 'value' => $value,
  660. 'op' => $op,
  661. 'column' => $key_field_id,
  662. );
  663. }
  664. }
  665. else {
  666. throw new Exception("The filter term, '$key', is not available for use.");
  667. }
  668. }
  669. // Now convert the operation for each filter to one that is compatible
  670. // with TripalFieldQuery.
  671. foreach ($filters as $key_field_name => $key_filters) {
  672. foreach ($key_filters as $i => $filter) {
  673. $op = '=';
  674. switch ($filters[$key_field_name][$i]['op']) {
  675. case 'eq':
  676. $op = '=';
  677. break;
  678. case 'gt':
  679. $op = '>';
  680. break;
  681. case 'gte':
  682. $op = '>=';
  683. break;
  684. case 'lt':
  685. $op = '<';
  686. break;
  687. case 'lte':
  688. $op = '<=';
  689. break;
  690. case 'ne':
  691. $op = '<>';
  692. break;
  693. case 'contains':
  694. $op = 'CONTAINS';
  695. break;
  696. case 'starts':
  697. $op = 'STARTS WITH';
  698. break;
  699. default:
  700. $op = '=';
  701. }
  702. $filters[$key_field_name][$i]['op'] = $op;
  703. }
  704. }
  705. return $filters;
  706. }
  707. /**
  708. * Creates a collection of resources for a given type.
  709. */
  710. private function doContentTypeList($ctype) {
  711. $service_path = $this->getServicePath() . '/' . preg_replace('/[^\w]/', '_', $ctype);
  712. $this->resource = new TripalWebServiceCollection($service_path, $this->params);
  713. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  714. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  715. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  716. $term = reset($term);
  717. // The type of collection is provided by our API vocabulary service.
  718. $vocab_service = new TripalDocService_v0_1($this->base_path);
  719. $this->resource->addContextItem('vocab', $vocab_service->getServicePath() . '#');
  720. $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
  721. $this->resource->addContextItem($accession, 'vocab:' . $accession);
  722. $this->resource->setType($accession);
  723. // Convert term to a simple array
  724. $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);
  725. // Set the label for this collection.
  726. $label = tripal_get_term_details('rdfs', 'label');
  727. $this->addResourceProperty($this->resource, $label, $bundle->label . " Collection");
  728. // For quick lookup, get the mapping of WS keys to their appropriate fields.
  729. $field_mapping = $this->getFieldMapping($bundle);
  730. // Get arrays for filters and order by statements.
  731. $filters = $this->getFilters($field_mapping, $bundle);
  732. $order_by = $this->getOrderBy($field_mapping, $bundle);
  733. // Initialize the query to search for records for out bundle type
  734. // that are published.
  735. $query = new TripalFieldQuery();
  736. $query->entityCondition('entity_type', 'TripalEntity');
  737. $query->entityCondition('bundle', $bundle->name);
  738. $query->propertyCondition('status', 1);
  739. // Now iterate through the filters and add those.
  740. foreach ($filters as $key_field_name => $key_filters) {
  741. foreach ($key_filters as $i => $filter) {
  742. $column_name = $filter['column'];
  743. $value = $filter['value'];
  744. $op = $filter['op'];
  745. $query->fieldCondition($key_field_name, $column_name, $value, $op);
  746. }
  747. }
  748. // Now set the order by.
  749. foreach ($order_by as $key_field_name => $key_order) {
  750. foreach ($key_order as $i => $order) {
  751. $column_name = $order['column'];
  752. $dir = $order['dir'];
  753. $query->fieldOrderBy($key_field_name, $column_name, $dir);
  754. }
  755. }
  756. // Perform the query just as a count first to get the number of records.
  757. $cquery = clone $query;
  758. $cquery->count();
  759. $num_records = $cquery->execute();
  760. if (!$num_records) {
  761. $num_records = 0;
  762. }
  763. // Add in the pager to the response.
  764. $response['totalItems'] = $num_records;
  765. $limit = array_key_exists('limit', $this->params) ? $this->params['limit'] : 25;
  766. $total_pages = ceil($num_records / $limit);
  767. $page = array_key_exists('page', $this->params) ? $this->params['page'] : 1;
  768. // Set the query range
  769. $start = ($page - 1) * $limit;
  770. $query->range($start, $limit);
  771. // Now perform the query.
  772. $results = $query->execute();
  773. $this->resource->initPager($num_records, $limit, $page);
  774. // Check to make sure there are results.
  775. $entity_ids = array();
  776. if (isset($results['TripalEntity']) AND is_array($results['TripalEntity'])) {
  777. $entity_ids = $results['TripalEntity'];
  778. }
  779. // Iterate through the entities and add them to the output list.
  780. foreach ($entity_ids as $entity_id => $stub) {
  781. // We don't need all of the attached fields for an entity so, we'll
  782. // not use the entity_load() function. Instead just pull it from the
  783. // database table.
  784. $query = db_select('tripal_entity', 'TE');
  785. $query->join('tripal_term', 'TT', 'TE.term_id = TT.id');
  786. $query->fields('TE');
  787. $query->fields('TT', array('name'));
  788. $query->condition('TE.id', $entity_id);
  789. $entity = $query->execute()->fetchObject();
  790. $itemPage = tripal_get_term_details('schema', 'ItemPage');
  791. $label = tripal_get_term_details('rdfs', 'label');
  792. $member = new TripalWebServiceResource($service_path);
  793. $member->setID($entity->id);
  794. $this->setResourceType($member, $term);
  795. $this->addResourceProperty($member, $label, $entity->title);
  796. $this->addResourceProperty($member, $itemPage, url('/bio_data/' . $entity->id, array('absolute' => TRUE)));
  797. $this->resource->addMember($member);
  798. }
  799. }
  800. /**
  801. * Creates a resources that contains the list of content types.
  802. */
  803. private function doAllTypesList() {
  804. $service_path = $this->getServicePath();
  805. $service_vocab = new TripalDocService_v0_1($this->base_path);
  806. $this->resource = new TripalWebServiceCollection($service_path, $this->params);
  807. $this->resource->addContextItem('vocab', $service_vocab->getServicePath());
  808. $this->resource->addContextItem('Content_Collection', $service_vocab->getServicePath() . '#Content_Collection');
  809. $this->resource->setType('Content_Collection');
  810. $label = tripal_get_term_details('rdfs', 'label');
  811. $this->addResourceProperty($this->resource, $label, 'Content Types');
  812. // Get the list of published terms (these are the bundle IDs)
  813. $bundles = db_select('tripal_bundle', 'tb')
  814. ->fields('tb')
  815. ->orderBy('tb.label', 'ASC')
  816. ->execute();
  817. // Iterate through the terms and add an entry in the collection.
  818. $i = 0;
  819. while ($bundle = $bundles->fetchObject()) {
  820. $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
  821. $term = reset($entity);
  822. $vocab = $term->vocab;
  823. // Convert the term to a simple array
  824. $term = tripal_get_term_details($term->vocab->vocabulary, $term->accession);
  825. $member = new TripalWebServiceResource($service_path);
  826. $member->setID(preg_replace('/[^\w]/', '_', $bundle->label));
  827. $vocab_service = new TripalDocService_v0_1($this->base_path);
  828. $member->addContextItem('vocab', $vocab_service->getServicePath() . '#');
  829. $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
  830. $member->addContextItem($accession, 'vocab:' . $accession);
  831. $member->setType($accession);
  832. // Make sure the term has a URL.
  833. $url = $term['url'];
  834. if (!$url) {
  835. throw new Exception(t('Missing a URL for the term: @term.', array('@term' => $term['vocabulary']['short_name'] . ':' . $term['accession'])));
  836. }
  837. $this->addResourceProperty($member, $label, $bundle->label . ' Collection');
  838. $member->addContextItem('description', 'rdfs:comment');
  839. // Get the bundle description. If no description is provided then
  840. // use the term definition
  841. $description = trim(tripal_get_bundle_variable('description', $bundle->id));
  842. if (!$description) {
  843. $description = $term['definition'];
  844. }
  845. if (!$description) {
  846. $description = '';
  847. }
  848. $member->addProperty('description', 'A collection of ' . $bundle->label . ' resources: ' . lcfirst($description));
  849. $this->resource->addMember($member);
  850. }
  851. }
  852. /**
  853. * Adds the content collection class to the document for this service.
  854. */
  855. private function addDocContentCollectionClass() {
  856. $details = array(
  857. 'id' => 'vocab:Content_Collection',
  858. 'term' => 'vocab:Content_Collection',
  859. 'title' => 'Content Collection',
  860. );
  861. $vocab = tripal_get_vocabulary_details('hydra');
  862. $properties = array();
  863. $properties[] = array(
  864. 'type' => $vocab['sw_url'],
  865. 'title' => 'member',
  866. 'description' => "The list of available content types.",
  867. "required" => null,
  868. "readonly" => FALSE,
  869. "writeonly" => FALSE,
  870. );
  871. $properties[] = array(
  872. "type" => $vocab['sw_url'],
  873. "title" => "totalItems",
  874. "description" => "The total number of content types.",
  875. "required" => null,
  876. "readonly" => FALSE,
  877. "writeonly" => FALSE
  878. );
  879. $properties[] = array(
  880. "type" => $vocab['sw_url'],
  881. "title" => "label",
  882. "description" => "The type content.",
  883. "required" => null,
  884. "readonly" => FALSE,
  885. "writeonly" => FALSE
  886. );
  887. $operations = array();
  888. $operations['GET'] = array(
  889. 'label' => 'Retrieves a collection (a list) of available content types.',
  890. 'type' => '_:content_collection_retrieve',
  891. 'expects' => NULL,
  892. 'returns' => 'vocab:ContentCollection',
  893. );
  894. $this->addDocClass($details,$operations, $properties);
  895. }
  896. /**
  897. * Adds classes for every content type to the documentation for this service.
  898. */
  899. private function addDocBundleClasses() {
  900. global $user;
  901. // Get the list of published terms (these are the bundle IDs)
  902. $bundles = db_select('tripal_bundle', 'tb')
  903. ->fields('tb')
  904. ->orderBy('tb.label', 'ASC')
  905. ->execute();
  906. // Iterate through the content types and add a class for each one.
  907. $i = 0;
  908. while ($bundle = $bundles->fetchObject()) {
  909. $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
  910. $term = reset($entity);
  911. $vocab = $term->vocab;
  912. // Get the bundle description. If no description is provided then
  913. // use the term definition
  914. $description = tripal_get_bundle_variable('description', $bundle->id);
  915. if (!$description) {
  916. $description = $term->getDefinition();
  917. }
  918. // Create the details array for the class.
  919. $class_id = $this->getServicePath() . '/' . urlencode($bundle->label);
  920. $details = array(
  921. 'id' => $term->getURL(),
  922. 'term' => $term->getAccession(),
  923. 'title' => preg_replace('/[^\w]/', '_', $bundle->label),
  924. 'description' => $description,
  925. );
  926. // Add in the supported operations for this content type.
  927. $operations = array();
  928. // If the user can view this content type.
  929. if (user_access('view ' . $bundle->name)) {
  930. $label = "Retrieves the " . $bundle->label . " resource.";
  931. $operations['GET'] = array(
  932. 'label' => $label,
  933. 'description' => NULL,
  934. 'returns' => $term->url,
  935. 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_retrieve',
  936. );
  937. }
  938. // If the user can edit this content type.
  939. if (user_access('edit ' . $bundle->name)) {
  940. $label = "Update and replace the " . $bundle->label . " resource.";
  941. if (preg_match('/^[aeiou]/i', $bundle->label)) {
  942. $label = "Update and replace an " . $bundle->label . " resource.";
  943. }
  944. // TODO: add this back in when web services support this method.
  945. // $operations['PUT'] = array(
  946. // 'label' => $label,
  947. // 'description' => NULL,
  948. // 'returns' => $term->url,
  949. // 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_update',
  950. // );
  951. }
  952. // If the user can edit this content type.
  953. if (user_access('delete ' . $bundle->name)) {
  954. $label = "Deletes the " . $bundle->label . " resource.";
  955. if (preg_match('/^[aeiou]/i', $bundle->label)) {
  956. $label = "Deletes an " . $bundle->label . " resource.";
  957. }
  958. // TODO: add this back in when web services support this method.
  959. // $operations['DELETE'] = array(
  960. // 'label' => $label,
  961. // 'description' => NULL,
  962. // 'returns' => $term->url,
  963. // 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_delete',
  964. // );
  965. }
  966. // Add in the properties that correspond to fields in the data.
  967. $properties = $this->addDocBundleFieldProperties($bundle, $term);
  968. $this->addDocClass($details, $operations, $properties);
  969. // Now add the bundle collection class.
  970. $this->addDocBundleCollectionClass($bundle, $term);
  971. } // end while ($bundle = $bundles->fetchObject()) { ...
  972. }
  973. /**
  974. * Every content type (bundle) has fields that need to be set as properties.
  975. */
  976. private function addDocBundleFieldProperties($bundle, $bundle_term) {
  977. $properties = array();
  978. $content_type_accession = $bundle_term->vocab->vocabulary . ':' . $bundle_term->accession;
  979. $instances = field_info_instances('TripalEntity', $bundle->name);
  980. foreach ($instances as $instance) {
  981. // Skip deleted fields.
  982. if ($instance['deleted']) {
  983. continue;
  984. }
  985. // Skip hidden fields.
  986. if ($instance['display']['default']['type'] == 'hidden') {
  987. continue;
  988. }
  989. $accession = $instance['settings']['term_vocabulary'] . ":" . $instance['settings']['term_accession'];
  990. $field_name = $instance['field_name'];
  991. $field = field_info_field($field_name);
  992. $field_type = $field['type'];
  993. // Skip fields of remote data.
  994. if ($field_type == 'remote__data') {
  995. continue;
  996. }
  997. // Check if this field is an auto attach. If not, then we have alink and
  998. // we need to indicate that the link has operations.
  999. $proptype = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
  1000. if ($instance['settings']['auto_attach'] == FALSE) {
  1001. // Create a WebServiceResource for the hydra:Link type.
  1002. $id = $content_type_accession . '/' . $accession;
  1003. $link = new TripalWebServiceResource($this->base_path);
  1004. $link->setID($accession);
  1005. $link->setType('hydra:Link');
  1006. $link->addContextItem('domain', array(
  1007. "@id" => "rdfs:domain",
  1008. "@type" => "@id"
  1009. ));
  1010. $link->addContextItem('range', array(
  1011. "@id" => "rdfs:range",
  1012. "@type" => "@id"
  1013. ));
  1014. $link->addContextItem('readable', 'hydra:readable');
  1015. $link->addContextItem('writeable', 'hydra:writeable');
  1016. $link->addContextItem('required', 'hydra:required');
  1017. $link->addContextItem('description', 'rdfs:comment');
  1018. $link->addContextItem('label', 'rdfs:label');
  1019. $link->addProperty('hydra:title', $instance['label']);
  1020. $link->addProperty('hydra:description', $instance['description']);
  1021. // $link->addProperty('domain', $service_path . '#EntryPoint');
  1022. // $link->addProperty('range', $service_class::$label);
  1023. $ops = array();
  1024. $op = new TripalWebServiceResource($this->base_path);
  1025. $op->setID('_:' . $field_name . '_retrieve');
  1026. $op->setType('hydra:Operation');
  1027. $op->addContextItem('method', 'hydra:method');
  1028. $op->addContextItem('label', 'rdfs:label');
  1029. $op->addContextItem('description', 'rdfs:comment');
  1030. $op->addContextItem('expects', array(
  1031. "@id" => "hydra:expects",
  1032. "@type" => "@id"
  1033. ));
  1034. $op->addContextItem('returns', array(
  1035. "@id" => "hydra:returns",
  1036. "@type" => "@id"
  1037. ));
  1038. $op->addContextItem('statusCodes', 'hydra:statusCodes');
  1039. $op->addProperty('method', "GET");
  1040. $op->addProperty('label', 'Retrieves the ' . $instance['label'] . ' resource.');
  1041. $op->addProperty('description', $instance['description']);
  1042. $op->addProperty('expects', NULL);
  1043. $op->addProperty('returns', $accession);
  1044. $op->addProperty('statusCodes', array());
  1045. $ops[] = $op;
  1046. $link->addContextItem('supportedOperation', 'hydra:supportedOperation');
  1047. $link->addProperty('supportedOperation', $ops);
  1048. $proptype = $link;
  1049. }
  1050. $formatters = tripal_get_field_field_formatters($field, $instance);
  1051. $property = array(
  1052. 'type' => $proptype,
  1053. 'title' => $instance['label'],
  1054. 'description' => $instance['description'],
  1055. "required" => $instance['required'] ? TRUE : FALSE,
  1056. "readonly" => FALSE,
  1057. "writeonly" => TRUE,
  1058. "tripal_formatters" => $formatters,
  1059. );
  1060. $properties[] = $property;
  1061. }
  1062. return $properties;
  1063. }
  1064. /**
  1065. * Every content type (bundle) needs a collection class in the documentation.
  1066. */
  1067. private function addDocBundleCollectionClass($bundle, $term) {
  1068. $accession = preg_replace('/[^\w]/', '_', $bundle->label . ' Collection');
  1069. $details = array(
  1070. 'id' => 'vocab:' . $accession,
  1071. 'term' => 'vocab:' . $accession,
  1072. 'title' => $bundle->label . ' Collection',
  1073. 'subClassOf' => 'hydra:Collection',
  1074. 'description' => 'A collection (or list) of ' . $bundle->label . ' resources.',
  1075. );
  1076. $vocab = tripal_get_vocabulary_details('hydra');
  1077. $properties = array();
  1078. $properties[] = array(
  1079. 'type' => $vocab['sw_url'],
  1080. 'title' => 'member',
  1081. 'description' => "The list of available " . $bundle->label . '(s).',
  1082. "required" => null,
  1083. "readonly" => FALSE,
  1084. "writeonly" => FALSE,
  1085. );
  1086. $properties[] = array(
  1087. "type" => $vocab['sw_url'],
  1088. "title" => "totalItems",
  1089. "description" => "The total number of resources.",
  1090. "required" => null,
  1091. "readonly" => FALSE,
  1092. "writeonly" => FALSE
  1093. );
  1094. $properties[] = array(
  1095. "type" => $vocab['sw_url'],
  1096. "title" => "label",
  1097. "description" => "A label or name for the resource.",
  1098. "required" => null,
  1099. "readonly" => FALSE,
  1100. "writeonly" => FALSE
  1101. );
  1102. $class_id = $this->getServicePath() . '/' . urlencode($bundle->label);
  1103. $operations = array();
  1104. $operations['GET'] = array(
  1105. 'label' => 'Retrieves a list of all ' . $bundle->label . ' resources.',
  1106. 'description' => NULL,
  1107. 'expects' => NULL,
  1108. 'returns' => $term->url,
  1109. 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_collection_retrieve',
  1110. );
  1111. // If the user can create this content type then we allow a POST on the
  1112. // collection type.
  1113. if (user_access('create ' . $bundle->name)) {
  1114. $label = "Creates a " . $bundle->label;
  1115. if (preg_match('/^[aeiou]/i', $bundle->label)) {
  1116. $label = "Creates an " . $bundle->label;
  1117. }
  1118. // TODO: add this back in when web services support this method.
  1119. // $operations['POST'] = array(
  1120. // 'label' => $label,
  1121. // 'description' => NULL,
  1122. // 'expects' => $term->url,
  1123. // 'returns' => $term->url,
  1124. // 'type' => '_:' . preg_replace('/[^\w]/', '_', strtolower($bundle->label)) . '_create',
  1125. // 'statusCodes' => array(
  1126. // array(
  1127. // "code" => 201,
  1128. // "description" => "If the " . $bundle->label . " was created successfully."
  1129. // ),
  1130. // ),
  1131. // );
  1132. }
  1133. $this->addDocClass($details, $operations, $properties);
  1134. }
  1135. }