tripal_entities.api.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Menu argument loader; Load a tripal data type by string.
  4. *
  5. * @param $type
  6. * The machine-readable name of a tripal data type to load.
  7. * @return
  8. * A tripal data type array or FALSE if $type does not exist.
  9. */
  10. function tripal_data_type_load($type) {
  11. $entity = tripal_data_get_types($type);
  12. return $entity;
  13. }
  14. /**
  15. * Gets an array of all tripal_data types, keyed by the type name.
  16. *
  17. * @param $type_name
  18. * If set, the type with the given name is returned.
  19. * @return TripalDataType[]
  20. * Depending whether $type isset, an array of tripal_data types or a single one.
  21. */
  22. function tripal_data_get_types($type_name = NULL) {
  23. // entity_load will get the Entity controller for our tripal_data entity and call the load
  24. // function of that object - we are loading entities by name here.
  25. $types = entity_load_multiple_by_name('tripal_data_type', isset($type_name) ? array($type_name) : FALSE);
  26. return isset($type_name) ? reset($types) : $types;
  27. }
  28. /**
  29. * Fetch a tripal_data object. Make sure that the wildcard you choose
  30. * in the tripal_data entity definition fits the function name here.
  31. *
  32. * @param $id
  33. * Integer specifying the tripal_data id.
  34. * @param $reset
  35. * A boolean indicating that the internal cache should be reset.
  36. * @return
  37. * A fully-loaded $tripal_data object or FALSE if it cannot be loaded.
  38. *
  39. * @see tripal_data_load_multiple()
  40. */
  41. function tripal_data_load($id, $reset = FALSE) {
  42. // Get the type of entity by the ID.
  43. $entity_type = db_select('tripal_data', 'td')
  44. ->fields('td', array('type'))
  45. ->condition('id', $id)
  46. ->execute()
  47. ->fetchField();
  48. // Load the entity.
  49. $entity = entity_load($entity_type, array($id), array(), $reset);
  50. return reset($entity);
  51. }
  52. /**
  53. * Deletes a tripal_data.
  54. */
  55. function tripal_data_delete(TripalData $tripal_data) {
  56. $tripal_data->delete();
  57. }
  58. /**
  59. * Saves a tripal_data to the database.
  60. *
  61. * @param $tripal_data
  62. * The tripal_data object.
  63. */
  64. function tripal_data_save(TripalData $entity) {
  65. return $entity->save();
  66. }
  67. /**
  68. * Saves a tripal_data type to the db.
  69. */
  70. function tripal_data_type_save(TripalDataType $entity) {
  71. $entity->save();
  72. }
  73. /**
  74. * Deletes a tripal_data type from the db.
  75. */
  76. function tripal_data_type_delete(TripalDataType $type) {
  77. $type->delete();
  78. }
  79. /**
  80. * URI callback for tripal_datas
  81. */
  82. function tripal_data_uri(TripalData $entity){
  83. return array(
  84. 'path' => 'data/' . $entity->id,
  85. );
  86. }