tripal_entities.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. require_once "api/tripal_entities.api.inc";
  3. require_once "includes/tripal_entities.chado_entity.inc";
  4. require_once "includes/tripal_entities.entity_form.inc";
  5. require_once "includes/tripal_entities.tables.inc";
  6. require_once "includes/TripalEntity.inc";
  7. require_once "includes/TripalEntityController.inc";
  8. require_once "includes/TripalEntityUIController.inc";
  9. require_once "includes/TripalBundle.inc";
  10. require_once "includes/TripalBundleController.inc";
  11. require_once "includes/TripalBundleUIController.inc";
  12. /**
  13. * Implements hook_views_api().
  14. */
  15. function tripal_entities_views_api() {
  16. return array(
  17. 'api' => 3,
  18. );
  19. }
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function tripal_entities_menu() {
  24. // Note: menu items for the entities can be found in the
  25. // Entity's UI Controller class.
  26. $items = array();
  27. return $items;
  28. }
  29. /**
  30. * Implements hook_admin_paths().
  31. * Define administrative paths.
  32. */
  33. function tripal_entities_admin_paths() {
  34. if (variable_get('node_admin_theme')) {
  35. $paths = array(
  36. 'bio-data/*/edit' => TRUE,
  37. 'bio-data/*/delete' => TRUE,
  38. 'bio-data/add' => TRUE,
  39. 'bio-data/add/*' => TRUE,
  40. );
  41. return $paths;
  42. }
  43. }
  44. /**
  45. * Implements hook_menu_local_tasks_alter().
  46. *
  47. * Used to add action links to pages.
  48. */
  49. function tripal_entities_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  50. // Add an "Add Tripal Content" action link to the Admin >> Content >>
  51. // Biological Content page.
  52. if ($root_path == 'admin/content/bio-data') {
  53. $item = menu_get_item('bio-data/add');
  54. if ($item['access']) {
  55. $data['actions']['output'][] = array(
  56. '#theme' => 'menu_local_action',
  57. '#link' => $item,
  58. );
  59. }
  60. }
  61. }
  62. /**
  63. * Implements hook_shortcut_default_set().
  64. * Modify the shortcut menu to include Biological content links.
  65. *
  66. * @param object $account
  67. * The user account whose default shortcut set will be returned. If not provided, the
  68. * function will return the currently logged-in user's default shortcut set.
  69. *
  70. * @return
  71. * An object representing the default shortcut set.
  72. */
  73. function tripal_entities_shortcut_default_set($account) {
  74. $sets = shortcut_sets();
  75. $found = FALSE;
  76. foreach ($sets as $set) {
  77. if ($set->title == 'TripalDefault') {
  78. $found = TRUE;
  79. }
  80. }
  81. if (!$found) {
  82. $t = get_t();
  83. // Create an initial default shortcut set.
  84. $shortcut_set = new stdClass();
  85. $shortcut_set->title = $t('TripalDefault');
  86. $shortcut_set->links = array(
  87. array(
  88. 'link_path' => 'bio-data/add',
  89. 'link_title' => 'Add Tripal Content',
  90. 'weight' => -23,
  91. ),
  92. array(
  93. 'link_path' => 'admin/content/bio-data',
  94. 'link_title' => 'Find Tripal Content',
  95. 'weight' => -22,
  96. ),
  97. array(
  98. 'link_path' => 'node/add',
  99. 'link_title' => $t('Add content'),
  100. 'weight' => -21,
  101. ),
  102. array(
  103. 'link_path' => 'admin/content',
  104. 'link_title' => $t('Find content'),
  105. 'weight' => -20,
  106. ),
  107. );
  108. shortcut_set_save($shortcut_set);
  109. }
  110. $sets = shortcut_sets();
  111. foreach ($sets as $set) {
  112. if ($set->title == 'TripalDefault') {
  113. return $set->set_name;
  114. }
  115. }
  116. }
  117. /**
  118. * Implements hook_permission().
  119. */
  120. function tripal_entities_permission() {
  121. // We set up permisssions to manage entity types, manage all entities and the
  122. // permissions for each individual entity
  123. $permissions = array(
  124. 'administer tripal data types' => array(
  125. 'title' => t('Administer Tripal data types'),
  126. 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
  127. ),
  128. 'administer tripal data' => array(
  129. 'title' => t('Administer Tripal data'),
  130. 'description' => t('Edit and delete all tripal data'),
  131. ),
  132. );
  133. return $permissions;
  134. }
  135. /**
  136. * Implements hook_theme().
  137. */
  138. function tripal_entities_theme($existing, $type, $theme, $path) {
  139. return array(
  140. 'tripal_entity' => array(
  141. 'render element' => 'elements',
  142. 'template' => 'tripal_entity',
  143. 'path' => "$path/theme/templates"
  144. ),
  145. 'tripal_entities_date_combo' => array(
  146. 'render element' => 'element',
  147. 'file' => 'includes/tripal_entities.chado_entity.inc',
  148. ),
  149. 'tripal_entities_add_list' => array(
  150. 'variables' => array('content' => NULL),
  151. 'file' => 'includes/tripal_entities.entity_form.inc',
  152. ),
  153. );
  154. }
  155. /**
  156. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  157. */
  158. function tripal_entities_rdf_mapping() {
  159. return array();
  160. /* return array(
  161. 'type' => 'tripal_entity',
  162. 'bundle' => 'gene',
  163. 'mapping' => array(
  164. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  165. 'title' => array(
  166. 'predicates' => array('dc:title'),
  167. ),
  168. 'uid' => array(
  169. 'predicates' => array('sioc:has_creator'),
  170. 'type' => 'rel',
  171. ),
  172. 'name' => array(
  173. 'predicates' => array('foaf:name'),
  174. ),
  175. 'uniquename' => array(
  176. 'predicates' => array('foaf:name'),
  177. ),
  178. 'organism_id' => array(
  179. 'predicates' => array('sioc:has_parent'),
  180. 'type' => 'rel'
  181. )
  182. ),
  183. ); */
  184. }
  185. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  186. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  187. /**
  188. * Implement hook_entity_info().
  189. */
  190. function tripal_entities_entity_info() {
  191. $entities = array();
  192. $entities['TripalEntity'] = array (
  193. // A human readable label to identify our entity.
  194. 'label' => 'Biological Content',
  195. 'plural label' => 'Biological Content',
  196. // The entity class and controller class extend the classes provided by the
  197. // Entity API.
  198. 'entity class' => 'TripalEntity',
  199. 'controller class' => 'TripalEntityController',
  200. // The table for this entity defined in hook_schema()
  201. 'base table' => 'tripal_entity',
  202. // Returns the uri elements of an entity.
  203. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  204. // IF fieldable == FALSE, we can't attach fields.
  205. 'fieldable' => TRUE,
  206. // entity_keys tells the controller what database fields are used for key
  207. // functions. It is not required if we don't have bundles or revisions.
  208. // Here we do not support a revision, so that entity key is omitted.
  209. 'entity keys' => array (
  210. 'id' => 'id',
  211. 'bundle' => 'bundle'
  212. ),
  213. 'bundle keys' => array (
  214. 'bundle' => 'bundle'
  215. ),
  216. // Callback function for access to this entity.
  217. 'access callback' => 'tripal_entity_access',
  218. // FALSE disables caching. Caching functionality is handled by Drupal core.
  219. 'static cache' => FALSE,
  220. // Bundles are added dynamically below.
  221. 'bundles' => array (),
  222. 'label callback' => 'tripal_entity_label',
  223. // The information below is used by the TripalEntityUIController
  224. // (which extends the EntityDefaultUIController). The admin_ui
  225. // key here is mean to appear on the 'Find Content' page of the
  226. // administrative menu.
  227. 'admin ui' => array (
  228. 'path' => 'admin/content/bio-data',
  229. 'controller class' => 'TripalEntityUIController',
  230. 'menu wildcard' => '%tripal_entity',
  231. 'file' => 'includes/TripalEntityUIController.inc'
  232. ),
  233. 'view modes' => array (
  234. 'full' => array (
  235. 'label' => t ('Full content'),
  236. 'custom settings' => FALSE
  237. ),
  238. 'teaser' => array (
  239. 'label' => t ('Teaser'),
  240. 'custom settings' => TRUE
  241. )
  242. )
  243. );
  244. // The entity that holds information about the entity types.
  245. $entities['TripalBundle'] = array (
  246. 'label' => 'Tripal Content Type',
  247. 'entity class' => 'TripalBundle',
  248. 'controller class' => 'TripalBundleController',
  249. 'base table' => 'tripal_bundle',
  250. 'fieldable' => FALSE,
  251. 'bundle of' => 'TripalEntity',
  252. 'exportable' => FALSE,
  253. 'entity keys' => array (
  254. 'id' => 'id',
  255. 'name' => 'bundle',
  256. 'label' => 'label'
  257. ),
  258. 'access callback' => 'tripal_bundle_access',
  259. 'module' => 'tripal_entities',
  260. // Enable the entity API's admin UI.
  261. 'admin ui' => array (
  262. 'path' => 'admin/structure/bio-data',
  263. 'controller class' => 'TripalBundleUIController',
  264. 'file' => 'includes/TripalBundleUIController.inc',
  265. 'menu wildcard' => '%tripal_bundle',
  266. )
  267. );
  268. return $entities;
  269. }
  270. /**
  271. * Implements hook_entities_info_alter().
  272. *
  273. * Add in the bundles (entity types) to the TripalEntity entity.
  274. */
  275. function tripal_entities_entity_info_alter(&$entity_info){
  276. // Dynamically add in the bundles. Bundles are alternative groups of fields
  277. // or configuration associated with an entity type .We want to dynamically
  278. // add the bundles to the entity.
  279. $bundles = db_select('tripal_bundle', 'tb')
  280. ->fields('tb')
  281. ->execute();
  282. while ($bundle = $bundles->fetchObject()) {
  283. $bundle_id = $bundle->bundle;
  284. $cvterm_id = preg_replace('/bio-data_/', '', $bundle_id);
  285. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  286. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  287. $entity_info['TripalEntity']['bundles'][$bundle_id] = array (
  288. 'label' => $label,
  289. 'admin' => array (
  290. 'path' => 'admin/structure/bio-data/manage/%tripal_bundle',
  291. 'real path' => 'admin/structure/bio-data/manage/' . $bundle_id,
  292. 'bundle argument' => 4,
  293. 'access arguments' => array (
  294. 'administer tripal data types'
  295. )
  296. )
  297. );
  298. }
  299. }
  300. /**
  301. * Menu argument loader; Load a tripal data type by string.
  302. *
  303. * @param $type
  304. * The machine-readable name of a tripal data type to load.
  305. * @return
  306. * A tripal data type array or FALSE if $type does not exist.
  307. */
  308. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  309. // Get the type of entity by the ID.
  310. $bundle_types = db_select('tripal_bundle', 'tdt')
  311. ->fields('tdt', array('id', 'type'))
  312. ->condition('bundle', $bundle_type)
  313. ->execute()
  314. ->fetchObject();
  315. if ($bundle_types) {
  316. $entity = entity_load('TripalBundle', array($bundle_types->id), array(), $reset);
  317. return reset($entity);
  318. }
  319. return FALSE;
  320. }
  321. /**
  322. * Allows the menu system to use a wildcard to fetch the entity.
  323. *
  324. * Make sure that the wildcard you choose in the tripal_entity entity
  325. * definition fits the function name here.
  326. *
  327. * @param $id
  328. * Integer specifying the tripal_entity id.
  329. * @param $reset
  330. * A boolean indicating that the internal cache should be reset.
  331. * @return
  332. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  333. *
  334. * @see tripal_entity_load_multiple()
  335. */
  336. function tripal_entity_load($id, $reset = FALSE) {
  337. $entity = entity_load('TripalEntity', array($id), array(), $reset);
  338. return reset($entity);
  339. }