tripal_ds.module 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. $instances = field_info_instances('TripalEntity', $bundle_name);
  49. if($bundle_data_table == 'pub'){
  50. _ds_layout_pub_settings_info($bundle_name, $instances);
  51. }
  52. else {
  53. _ds_layout_settings_info($bundle_name, $instances);
  54. }
  55. }
  56. /**
  57. * Update the tripal_ds table when a tripal pane is deleted.
  58. */
  59. function tripal_ds_table_column_delete($bundle){
  60. $bundle_name = $bundle->name;
  61. db_delete('tripal_ds')
  62. ->condition('bundle', $bundle_name, '=')
  63. ->execute();
  64. }
  65. /**
  66. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  67. */
  68. function tripal_ds_bundle_delete($bundle){
  69. tripal_ds_table_column_delete($bundle);
  70. }
  71. /*
  72. * Implements hook_ds_field_settings_alter()
  73. */
  74. function tripal_ds_ds_field_settings_alter(&$field_settings, $form, $form_state){
  75. // Get the form info from the bundle about to be saved.
  76. $tripal_entity_object = $form_state['build_info']['args']['1'];
  77. // Grab the bundle.
  78. $bundle_id = $tripal_entity_object->name;
  79. // Grab the field groups from the bundle.
  80. $updated_field_groups = $form_state['field_group'];
  81. // Grab the fields from the bundle.
  82. $fields = $form_state['values']['fields'];
  83. // Delete the menu items associated with the bundle id.
  84. db_delete('tripal_ds')
  85. ->condition('bundle', $bundle_id, '=')
  86. ->execute();
  87. // Traverse the updated field_groups grabbing the tripal pane items.
  88. $tripal_pane_field_groups = array();
  89. $i = 0;
  90. foreach($updated_field_groups as $updated_field_group){
  91. if($updated_field_group->format_type == 'tripalpane'){
  92. $tripal_pane_field_groups += [ $i => $updated_field_group->group_name];
  93. $i++;
  94. }
  95. }
  96. // Now grab the labels of the field_groups whose parent is a tripalpane.
  97. foreach($updated_field_groups as $updated_field_group){
  98. foreach($tripal_pane_field_groups as $tripal_pane_field_group){
  99. if($updated_field_group->parent_name == $tripal_pane_field_group){
  100. if($fields[$tripal_pane_field_group]['region'] !== 'hidden'){
  101. tripal_ds_bundle_menu_item($bundle_id, $updated_field_group->label, $tripal_pane_field_group, 'tripalentity');
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  109. */
  110. function tripal_ds_bundle_menu_item($bundle_name, $field_label, $field_name, $entity_type){
  111. //Check the record does not already exist
  112. $tripal_ds_rows = db_select('tripal_ds', 'ds')
  113. ->fields('ds', array('tripal_ds_field_name', 'tripal_ds_field_label'))
  114. ->condition('bundle', $bundle_name, '=')
  115. ->condition('tripal_ds_field_label', $field_label, '=')
  116. ->condition('tripal_ds_field_name', $field_name, '=')
  117. ->execute()->fetchAll();
  118. if(!empty($tripal_ds_rows)){
  119. foreach ($tripal_ds_rows as $tripal_ds_row){
  120. if(($field_label == $tripal_ds_row->tripal_ds_field_label) && ($field_name == $tripal_ds_row->tripal_ds_field_name) && ($bundle_name == $tripal_ds_rows->bundle)) {
  121. // Do not write the field to the table
  122. drupal_set_message("Could not update the bundle menu because that field already exists.", 'error');
  123. }
  124. }
  125. }
  126. else {
  127. //Write to the tripal_ds table to record the new tripal pane.
  128. $field_for_table = new stdClass();
  129. $field_for_table->tripal_ds_field_name = $field_name;
  130. $field_for_table->tripal_ds_field_label = $field_label;
  131. $field_for_table->entity_type = $entity_type;
  132. $field_for_table->bundle = $bundle_name;
  133. drupal_write_record('tripal_ds', $field_for_table);
  134. }
  135. }
  136. /*
  137. * Implements hook_ds_layout_info() to define layouts from code in a module for
  138. * display suite
  139. */
  140. function tripal_ds_ds_layout_info() {
  141. $path = drupal_get_path('module', 'tripal_ds');
  142. $layouts = array(
  143. 'tripal_ds_feature' => array(
  144. 'label' => t('Tripal Feature Layout'),
  145. 'path' => $path . '/theme/templates',
  146. 'regions' => array(
  147. 'left' => t('Left'),
  148. 'right' => t('Right'),
  149. ),
  150. 'css' => TRUE,
  151. ),
  152. );
  153. return $layouts;
  154. }
  155. function tripal_ds_update_layout_form($form, &$form_state, $bundle_name) {
  156. $form = array();
  157. $form['bundle_name'] = array(
  158. '#type' => 'value',
  159. '#value' => $bundle_name,
  160. );
  161. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  162. $bundle_label = $bundle->label;
  163. return confirm_form($form,
  164. t('Please confirm you would like to apply this layout: ' . $bundle_label),
  165. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  166. t('This action cannot be undone.'),
  167. t('Yes, apply layout'),
  168. t('No, cancel')
  169. );
  170. }
  171. /**
  172. *
  173. * @param $bundle_name
  174. */
  175. function tripal_ds_update_layout_form_submit($form, &$form_state) {
  176. $bundle_name = $form_state['build_info']['args'][0];
  177. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  178. //Build the identifier to check against ds_layout_settings.
  179. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  180. //Check to see if the layout already exists.
  181. $result = db_select('ds_layout_settings', 'ds')
  182. ->fields('ds')
  183. ->condition('id', $ds_identifier, '=')
  184. ->execute()
  185. ->fetchField();
  186. //Check to see if there are any field groups associated with the bundle.
  187. $result_fg = db_select('field_group', 'fg')
  188. ->fields('fg')
  189. ->condition('bundle', $bundle_name, '=')
  190. ->execute()
  191. ->fetchField();
  192. //Check to see if there are any tripal ds fields associated with the bundle.
  193. $result_tds = db_select('tripal_ds', 'tds')
  194. ->fields('tds')
  195. ->condition('bundle', $bundle_name, '=')
  196. ->execute();
  197. //Check to see if there are any field settings associated with the bundle.
  198. $result_fs = db_select('ds_field_settings', 'fs')
  199. ->fields('fs')
  200. ->condition('bundle', $bundle_name, '=')
  201. ->execute();
  202. //If the layout exists, delete it.
  203. if(!empty($result)) {
  204. db_delete('ds_layout_settings')
  205. ->condition('id', $ds_identifier, '=')
  206. ->execute();
  207. }
  208. //Then delete the field_group_fields associated with the identifier.
  209. if(!empty($result_fg)) {
  210. db_delete('field_group')
  211. ->condition('bundle', $bundle_name, '=')
  212. ->execute();
  213. }
  214. //Then delete the ds_field_settings associated with the identifier.
  215. if(!empty($result_tds)) {
  216. db_delete('ds_field_settings')
  217. ->condition('bundle', $bundle_name, '=')
  218. ->execute();
  219. }
  220. //Then delete the tripal_ds menu item.
  221. if(!empty($result_fs)) {
  222. db_delete('tripal_ds')
  223. ->condition('bundle', $bundle_name, '=')
  224. ->execute();
  225. }
  226. //Now you can build the layout fresh.
  227. $instances = field_info_instances('TripalEntity', $bundle_name);
  228. $bundle_data_table = $bundle->data_table;
  229. if($bundle_data_table == 'pub'){
  230. $success = _ds_layout_pub_settings_info($bundle_name, $instances);
  231. }
  232. else {
  233. $success = _ds_layout_settings_info($bundle_name, $instances);
  234. }
  235. if ($success) {
  236. drupal_set_message("Layout applied successfully and saved.");
  237. }
  238. else {
  239. drupal_set_message("Could not apply layout.", 'error');
  240. }
  241. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  242. }
  243. /*
  244. * Code for the view of the menu items
  245. //get tripal entity id from url then run it against tripal entity db
  246. //and grab the bundle id, then pass bundle id to view
  247. $url = current_path();
  248. $url_exploded = explode("/", $url);
  249. $tripal_entity_id = (int)$url_exploded[1];
  250. $result = db_select('tripal_entity', 'te')
  251. ->fields('te', array('bundle'))
  252. ->condition('id', $tripal_entity_id, '=')
  253. ->execute()
  254. ->fetchField();
  255. */