tripal_ds.module 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. require_once "includes/tripal_ds.inc";
  3. require_once "includes/tripal_ds.ds.inc";
  4. require_once "includes/tripal_ds.field_group.inc";
  5. require_once "includes/tripal_ds.field_formatter.inc";
  6. function tripal_ds_init() {
  7. drupal_add_css(drupal_get_path('module', 'tripal_ds') . '/theme/css/tripaldsfeature.css');
  8. drupal_add_js(drupal_get_path('module', 'tripal_ds') . '/theme/js/tripal_ds.js');
  9. $theme_dir = url(drupal_get_path('module', 'tripal_ds') . '/theme');
  10. drupal_add_js("var ds_theme_dir = '$theme_dir';", 'inline', 'header');
  11. }
  12. /**
  13. * Implements hook_views_api().
  14. */
  15. function tripal_ds_views_api() {
  16. return array(
  17. 'api' => 3,
  18. 'path' => drupal_get_path('module', 'tripal_ds') . '/includes/views',
  19. );
  20. }
  21. /**
  22. * Implements hook_menu().
  23. * Defines all menu items needed by Tripal DS
  24. *
  25. */
  26. function tripal_ds_menu() {
  27. $items = array();
  28. // Adds a +Apply Tripal Display Suite option to 'Tripal Content Types' page.
  29. $items['admin/structure/bio_data/manage/%/display/apply'] = array(
  30. 'title' => 'Apply Default Tripal Layout (will reset current layout)',
  31. 'description' => t('Apply the Tripal Display Suite settings to this content type.'),
  32. 'page callback' => 'drupal_get_form',
  33. 'access arguments' => array('administer tripal'),
  34. 'page arguments' => array('tripal_ds_update_layout_form', 4),
  35. 'type' => MENU_LOCAL_ACTION,
  36. );
  37. return $items;
  38. }
  39. /**
  40. * Implements hook_bundle_postcreate().
  41. *
  42. * This is a Triapl defined hook and is called in the TripalBundle::create()
  43. * function to allow modules to perform tasks when a bundle is created.
  44. */
  45. function tripal_ds_bundle_postcreate($bundle) {
  46. $bundle_name = $bundle->name;
  47. $bundle_data_table = $bundle->data_table;
  48. if($bundle_data_table == 'pub'){
  49. $instances = field_info_instances('TripalEntity', $bundle_name);
  50. _ds_layout_pub_settings_info($bundle_name, $instances);
  51. }
  52. else {
  53. $instances = field_info_instances('TripalEntity', $bundle_name);
  54. _ds_layout_settings_info($bundle_name, $instances);
  55. }
  56. }
  57. function tripal_ds_table_column_delete($bundle){
  58. $bundle_name = $bundle->name;
  59. db_delete('tripal_ds')
  60. ->condition('bundle', $bundle_name, '=')
  61. ->execute();
  62. }
  63. function tripal_ds_bundle_delete($bundle){
  64. tripal_ds_table_column_delete($bundle);
  65. }
  66. /*
  67. * Implements hook_ds_layout_info() to define layouts from code in a module for
  68. * display suite
  69. */
  70. function tripal_ds_ds_layout_info() {
  71. $path = drupal_get_path('module', 'tripal_ds');
  72. $layouts = array(
  73. 'tripal_ds_feature' => array(
  74. 'label' => t('Tripal Feature Layout'),
  75. 'path' => $path . '/theme/templates',
  76. 'regions' => array(
  77. 'left' => t('Left'),
  78. 'right' => t('Right'),
  79. ),
  80. 'css' => TRUE,
  81. ),
  82. );
  83. return $layouts;
  84. }
  85. function tripal_ds_update_layout_form($form, &$form_state, $bundle_name) {
  86. $form = array();
  87. $form['bundle_name'] = array(
  88. '#type' => 'value',
  89. '#value' => $bundle_name,
  90. );
  91. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  92. $bundle_label = $bundle->label;
  93. return confirm_form($form,
  94. t('Please confirm you would like to apply this layout: ' . $bundle_label),
  95. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  96. t('This action cannot be undone.'),
  97. t('Yes, apply layout'),
  98. t('No, cancel')
  99. );
  100. }
  101. /**
  102. *
  103. * @param $bundle_name
  104. */
  105. function tripal_ds_update_layout_form_submit($form, &$form_state) {
  106. $bundle_name = $form_state['build_info']['args'][0];
  107. //Build the identifier to check against ds_layout_settings.
  108. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  109. //Check to see if the layout already exists.
  110. $result = db_select('ds_layout_settings', 'ds')
  111. ->fields('ds')
  112. ->condition('id', $ds_identifier, '=')
  113. ->execute()
  114. ->fetchField();
  115. //Check to see if there are any field groups associated with the bundle.
  116. $result_fg = db_select('field_group', 'fg')
  117. ->fields('fg')
  118. ->condition('bundle', $bundle_name, '=')
  119. ->execute()
  120. ->fetchField();
  121. //Check to see if there are any tripal ds fields associated with the bundle.
  122. $result_tds = db_select('tripal_ds', 'tds')
  123. ->fields('tds')
  124. ->condition('bundle', $bundle_name, '=')
  125. ->execute();
  126. //Check to see if there are any field settings associated with the bundle.
  127. $result_fs = db_select('ds_field_settings', 'fs')
  128. ->fields('fs')
  129. ->condition('bundle', $bundle_name, '=')
  130. ->execute();
  131. //If the layout exists, delete it.
  132. if(!empty($result)) {
  133. db_delete('ds_layout_settings')
  134. ->condition('id', $ds_identifier, '=')
  135. ->execute();
  136. }
  137. //Then delete the field_group_fields associated with the identifier.
  138. if(!empty($result_fg)) {
  139. db_delete('field_group')
  140. ->condition('bundle', $bundle_name, '=')
  141. ->execute();
  142. }
  143. //Then delete the ds_field_settings associated with the identifier.
  144. if(!empty($result_tds)) {
  145. db_delete('ds_field_settings')
  146. ->condition('bundle', $bundle_name, '=')
  147. ->execute();
  148. }
  149. //Then delete the tripal_ds menu item.
  150. if(!empty($result_fs)) {
  151. db_delete('tripal_ds')
  152. ->condition('bundle', $bundle_name, '=')
  153. ->execute();
  154. }
  155. //Now you can build the layout fresh.
  156. $instances = field_info_instances('TripalEntity', $bundle_name);
  157. $success = _ds_layout_settings_info($bundle_name, $instances);
  158. if ($success) {
  159. drupal_set_message("Layout applied successfully and saved.");
  160. }
  161. else {
  162. drupal_set_message("Could not apply layout.", 'error');
  163. }
  164. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  165. }
  166. /*
  167. * Code for the view of the menu items
  168. //get tripal entity id from url then run it against tripal entity db
  169. //and grab the bundle id, then pass bundle id to view
  170. $url = current_path();
  171. $url_exploded = explode("/", $url);
  172. $tripal_entity_id = (int)$url_exploded[1];
  173. $result = db_select('tripal_entity', 'te')
  174. ->fields('te', array('bundle'))
  175. ->condition('id', $tripal_entity_id, '=')
  176. ->execute()
  177. ->fetchField();
  178. */