tripal_entities.module 14 KB

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