tripal_entities.api.inc 2.8 KB

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