tripal_ds.module 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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) {
  86. $form = array();
  87. $form['bundle_name'] = array(
  88. '#type' => 'value',
  89. '#value' => $bundle,
  90. );
  91. return confirm_form($form,
  92. t('Please confirm you would like to update this layout: '.$bundle),
  93. 'admin/structure/bio_data/manage/'.$bundle.'/display',
  94. t('This action cannot be undone.'),
  95. t('Yes, apply layout'),
  96. t('No, cancel')
  97. );
  98. }
  99. /**
  100. *
  101. * @param $bundle_name
  102. */
  103. function tripal_ds_update_layout_form_submit($form, &$form_state) {
  104. $bundle_name = $form_state['build_info']['args'][0];
  105. //Build the identifier to check against ds_layout_settings.
  106. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  107. //Check to see if the layout already exists.
  108. $result = db_select('ds_layout_settings', 'ds')
  109. ->fields('ds')
  110. ->condition('id', $ds_identifier, '=')
  111. ->execute()
  112. ->fetchField();
  113. //Check to see if there are any field groups associated with the bundle.
  114. $result_fg = db_select('field_group', 'fg')
  115. ->fields('fg')
  116. ->condition('bundle', $bundle_name, '=')
  117. ->execute()
  118. ->fetchField();
  119. //Check to see if there are any tripal ds fields associated with the bundle.
  120. $result_tds = db_select('tripal_ds', 'tds')
  121. ->fields('tds')
  122. ->condition('bundle', $bundle_name, '=')
  123. ->execute();
  124. //Check to see if there are any field settings associated with the bundle.
  125. $result_fs = db_select('ds_field_settings', 'fs')
  126. ->fields('fs')
  127. ->condition('bundle', $bundle_name, '=')
  128. ->execute();
  129. //If the layout exists, delete it.
  130. if(!empty($result)) {
  131. db_delete('ds_layout_settings')
  132. ->condition('id', $ds_identifier, '=')
  133. ->execute();
  134. }
  135. //Then delete the field_group_fields associated with the identifier.
  136. if(!empty($result_fg)) {
  137. db_delete('field_group')
  138. ->condition('bundle', $bundle_name, '=')
  139. ->execute();
  140. }
  141. //Then delete the ds_field_settings associated with the identifier.
  142. if(!empty($result_tds)) {
  143. db_delete('ds_field_settings')
  144. ->condition('bundle', $bundle_name, '=')
  145. ->execute();
  146. }
  147. //Then delete the tripal_ds menu item.
  148. if(!empty($result_fs)) {
  149. db_delete('tripal_ds')
  150. ->condition('bundle', $bundle_name, '=')
  151. ->execute();
  152. }
  153. //Now you can build the layout fresh.
  154. $instances = field_info_instances('TripalEntity', $bundle_name);
  155. $success = _ds_layout_settings_info($bundle_name, $instances);
  156. if ($success) {
  157. drupal_set_message("Layout applied successfully and saved.");
  158. }
  159. else {
  160. drupal_set_message("Could not apply layout.", 'error');
  161. }
  162. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  163. }
  164. /*
  165. * Code for the view of the menu items
  166. //get tripal entity id from url then run it against tripal entity db
  167. //and grab the bundle id, then pass bundle id to view
  168. $url = current_path();
  169. $url_exploded = explode("/", $url);
  170. $tripal_entity_id = (int)$url_exploded[1];
  171. $result = db_select('tripal_entity', 'te')
  172. ->fields('te', array('bundle'))
  173. ->condition('id', $tripal_entity_id, '=')
  174. ->execute()
  175. ->fetchField();
  176. */