tripal_entities.module 14 KB

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