tripal_entities.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. * Checks access permissions for a given entity.
  145. */
  146. function tripal_entities_entity_access($entity) {
  147. // TODO: need to implement this function.
  148. return TRUE;
  149. }
  150. /**
  151. * Implements hook_theme().
  152. */
  153. function tripal_entities_theme($existing, $type, $theme, $path) {
  154. return array(
  155. 'tripal_entity' => array(
  156. 'render element' => 'elements',
  157. 'template' => 'tripal_entity',
  158. 'path' => "$path/theme/templates"
  159. ),
  160. 'tripal_entities_add_list' => array(
  161. 'variables' => array('content' => NULL),
  162. 'file' => 'includes/tripal_entities.entity_form.inc',
  163. ),
  164. );
  165. }
  166. /**
  167. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  168. */
  169. function tripal_entities_rdf_mapping() {
  170. return array();
  171. /* return array(
  172. 'type' => 'tripal_entity',
  173. 'bundle' => 'gene',
  174. 'mapping' => array(
  175. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  176. 'title' => array(
  177. 'predicates' => array('dc:title'),
  178. ),
  179. 'uid' => array(
  180. 'predicates' => array('sioc:has_creator'),
  181. 'type' => 'rel',
  182. ),
  183. 'name' => array(
  184. 'predicates' => array('foaf:name'),
  185. ),
  186. 'uniquename' => array(
  187. 'predicates' => array('foaf:name'),
  188. ),
  189. 'organism_id' => array(
  190. 'predicates' => array('sioc:has_parent'),
  191. 'type' => 'rel'
  192. )
  193. ),
  194. ); */
  195. }
  196. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  197. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  198. /**
  199. * Implement hook_entity_info().
  200. */
  201. function tripal_entities_entity_info() {
  202. $entities = array();
  203. //
  204. // The TripalVocab entity is meant to house vocabularies. It is these
  205. // vocabs that are used by the TripalTerm entities. The storage backend
  206. // is responsible for setting the values of this entity.
  207. //
  208. $entities['TripalVocab'] = array(
  209. // A human readable label to identify our entity.
  210. 'label' => 'Controlled Vocabulary',
  211. 'plural label' => 'Controlled Vocabularies',
  212. // The entity class and controller class extend the classes provided by the
  213. // Entity API.
  214. 'entity class' => 'TripalVocab',
  215. 'controller class' => 'TripalVocabController',
  216. // The table for this entity defined in hook_schema()
  217. 'base table' => 'tripal_vocab',
  218. // If fieldable == FALSE, we can't attach fields.
  219. 'fieldable' => TRUE,
  220. // entity_keys tells the controller what database fields are used for key
  221. // functions. It is not required if we don't have bundles or revisions.
  222. // Here we do not support a revision, so that entity key is omitted.
  223. 'entity keys' => array (
  224. 'id' => 'id',
  225. ),
  226. // Callback function for access to this entity.
  227. 'access callback' => 'tripal_entity_access',
  228. // FALSE disables caching. Caching functionality is handled by Drupal core.
  229. 'static cache' => FALSE,
  230. // This entity doesn't support bundles.
  231. 'bundles' => array (),
  232. 'view modes' => array (
  233. 'full' => array (
  234. 'label' => t ('Full content'),
  235. 'custom settings' => FALSE
  236. ),
  237. 'teaser' => array (
  238. 'label' => t ('Teaser'),
  239. 'custom settings' => TRUE
  240. ),
  241. ),
  242. );
  243. //
  244. // The TripalTerm entity is meant to house vocabulary terms. It is these
  245. // terms that are used by the TripalEntity entities. The storage backend
  246. // is responsible for setting the values of this entity.
  247. //
  248. $entities['TripalTerm'] = array(
  249. // A human readable label to identify our entity.
  250. 'label' => 'Controlled Vocabulary Term',
  251. 'plural label' => 'Controlled Vocabulary Terms',
  252. // The entity class and controller class extend the classes provided by the
  253. // Entity API.
  254. 'entity class' => 'TripalTerm',
  255. 'controller class' => 'TripalTermController',
  256. // The table for this entity defined in hook_schema()
  257. 'base table' => 'tripal_term',
  258. // If fieldable == FALSE, we can't attach fields.
  259. 'fieldable' => TRUE,
  260. // entity_keys tells the controller what database fields are used for key
  261. // functions. It is not required if we don't have bundles or revisions.
  262. // Here we do not support a revision, so that entity key is omitted.
  263. 'entity keys' => array (
  264. 'id' => 'id',
  265. ),
  266. // Callback function for access to this entity.
  267. 'access callback' => 'tripal_entity_access',
  268. // FALSE disables caching. Caching functionality is handled by Drupal core.
  269. 'static cache' => FALSE,
  270. // This entity doesn't support bundles.
  271. 'bundles' => array (),
  272. 'view modes' => array (
  273. 'full' => array (
  274. 'label' => t ('Full content'),
  275. 'custom settings' => FALSE
  276. ),
  277. 'teaser' => array (
  278. 'label' => t ('Teaser'),
  279. 'custom settings' => TRUE
  280. ),
  281. ),
  282. );
  283. //
  284. // The TripalEntity is used for all data. It links data from a storage
  285. // back-end to a TripalTerm entity.
  286. //
  287. $entities['TripalEntity'] = array (
  288. // A human readable label to identify our entity.
  289. 'label' => 'Tripal Content',
  290. 'plural label' => 'Tripal Content',
  291. // The entity class and controller class extend the classes provided by the
  292. // Entity API.
  293. 'entity class' => 'TripalEntity',
  294. 'controller class' => 'TripalEntityController',
  295. // The table for this entity defined in hook_schema()
  296. 'base table' => 'tripal_entity',
  297. // Returns the uri elements of an entity.
  298. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  299. // IF fieldable == FALSE, we can't attach fields.
  300. 'fieldable' => TRUE,
  301. // entity_keys tells the controller what database fields are used for key
  302. // functions. It is not required if we don't have bundles or revisions.
  303. // Here we do not support a revision, so that entity key is omitted.
  304. 'entity keys' => array (
  305. 'id' => 'id',
  306. 'bundle' => 'bundle'
  307. ),
  308. 'bundle keys' => array (
  309. 'bundle' => 'bundle'
  310. ),
  311. // Callback function for access to this entity.
  312. 'access callback' => 'tripal_entity_access',
  313. // FALSE disables caching. Caching functionality is handled by Drupal core.
  314. 'static cache' => FALSE,
  315. // Bundles are added dynamically below.
  316. 'bundles' => array (),
  317. 'label callback' => 'tripal_entity_label',
  318. // The information below is used by the TripalEntityUIController
  319. // (which extends the EntityDefaultUIController). The admin_ui
  320. // key here is mean to appear on the 'Find Content' page of the
  321. // administrative menu.
  322. 'admin ui' => array (
  323. 'path' => 'admin/content/bio-data',
  324. 'controller class' => 'TripalEntityUIController',
  325. 'menu wildcard' => '%tripal_entity',
  326. 'file' => 'includes/TripalEntityUIController.inc'
  327. ),
  328. 'view modes' => array (
  329. 'full' => array (
  330. 'label' => t ('Full content'),
  331. 'custom settings' => FALSE
  332. ),
  333. 'teaser' => array (
  334. 'label' => t ('Teaser'),
  335. 'custom settings' => TRUE
  336. )
  337. )
  338. );
  339. //
  340. // The TripalBundle entity is used manage the bundle types. The 'bundle of'
  341. // attribute links this to the TripalEntity and allows the UI provided
  342. // by the entity module to work for each TripalEntity bundle.
  343. //
  344. $entities['TripalBundle'] = array (
  345. 'label' => 'Tripal Content Type',
  346. 'entity class' => 'TripalBundle',
  347. 'controller class' => 'TripalBundleController',
  348. 'base table' => 'tripal_bundle',
  349. 'fieldable' => FALSE,
  350. 'bundle of' => 'TripalEntity',
  351. 'exportable' => FALSE,
  352. 'entity keys' => array (
  353. 'id' => 'id',
  354. 'name' => 'bundle',
  355. 'label' => 'label'
  356. ),
  357. 'access callback' => 'tripal_bundle_access',
  358. 'module' => 'tripal_entities',
  359. // Enable the entity API's admin UI.
  360. 'admin ui' => array (
  361. 'path' => 'admin/structure/bio-data',
  362. 'controller class' => 'TripalBundleUIController',
  363. 'file' => 'includes/TripalBundleUIController.inc',
  364. 'menu wildcard' => '%tripal_bundle',
  365. )
  366. );
  367. return $entities;
  368. }
  369. /**
  370. * Implements hook_entities_info_alter().
  371. *
  372. * Add in the bundles (entity types) to the TripalEntity entity.
  373. */
  374. function tripal_entities_entity_info_alter(&$entity_info){
  375. if (array_key_exists('TripalEntity', $entity_info)) {
  376. // Dynamically add in the bundles. Bundles are alternative groups of fields
  377. // or configuration associated with an entity type .We want to dynamically
  378. // add the bundles to the entity.
  379. $bundles = db_select('tripal_bundle', 'tb')
  380. ->fields('tb')
  381. ->execute();
  382. while ($bundle = $bundles->fetchObject()) {
  383. $bundle_name = $bundle->bundle;
  384. $term_id = preg_replace('/bio-data_/', '', $bundle_name);
  385. $term = entity_load('TripalTerm', array('id' => $term_id));
  386. $term = reset($term);
  387. $label = preg_replace('/_/', ' ', ucwords($term->name));
  388. $entity_info['TripalEntity']['bundles'][$bundle_name] = array (
  389. 'label' => $label,
  390. 'admin' => array (
  391. 'path' => 'admin/structure/bio-data/manage/%tripal_bundle',
  392. 'real path' => 'admin/structure/bio-data/manage/' . $bundle_name,
  393. 'bundle argument' => 4,
  394. 'access arguments' => array (
  395. 'administer tripal data types'
  396. )
  397. )
  398. );
  399. }
  400. }
  401. }
  402. /**
  403. * Menu argument loader; Load a tripal data type by string.
  404. *
  405. * @param $type
  406. * The machine-readable name of a tripal data type to load.
  407. * @return
  408. * A tripal data type array or FALSE if $type does not exist.
  409. */
  410. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  411. // Get the type of entity by the ID.
  412. $bundle_types = db_select('tripal_bundle', 'tdt')
  413. ->fields('tdt', array('id', 'type'))
  414. ->condition('bundle', $bundle_type)
  415. ->execute()
  416. ->fetchObject();
  417. if ($bundle_types) {
  418. $entity = entity_load('TripalBundle', array($bundle_types->id), array(), $reset);
  419. return reset($entity);
  420. }
  421. return FALSE;
  422. }
  423. /**
  424. * Allows the menu system to use a wildcard to fetch the entity.
  425. *
  426. * Make sure that the wildcard you choose in the tripal_entity entity
  427. * definition fits the function name here.
  428. *
  429. * @param $id
  430. * Integer specifying the tripal_entity id.
  431. * @param $reset
  432. * A boolean indicating that the internal cache should be reset.
  433. * @return
  434. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  435. *
  436. * @see tripal_entity_load_multiple()
  437. */
  438. function tripal_entity_load($id, $reset = FALSE) {
  439. $entity = entity_load('TripalEntity', array($id), array(), $reset);
  440. return reset($entity);
  441. }