tripal_ds.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 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->group_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. * Implements hook_field_group_pre_render().
  109. *
  110. * This function gives you the oppertunity to create the given
  111. * wrapper element that can contain the fields.
  112. * In the example beneath, some variables are prepared and used when building the
  113. * actual wrapper element. All elements in drupal fapi can be used.
  114. *
  115. * Note that at this point, the field group has no notion of the fields in it.
  116. *
  117. * There is also an alternative way of handling this. The default implementation
  118. * within field_group calls "field_group_pre_render_<format_type>".
  119. * @see field_group_pre_render_fieldset.
  120. *
  121. * @param Array $elements by address.
  122. * @param Object $group The Field group info.
  123. function tripal_ds_field_group_pre_render(& $element, $group, & $form) {
  124. watchdog('debug', '<pre>tripal_ds_preprocess_TripalEntity $group: '. print_r($group, TRUE) .'</pre>');
  125. if ($group->format_settings['formatter'] != 'open') {
  126. $add['#prefix'] = '<div class="field-group-format ' . $classes . '">
  127. <span class="field-group-format-toggler">' . check_plain(t($group->label)) . '</span>
  128. <div class="field-group-format-wrapper" style="display: none;">';
  129. $add['#suffix'] = '</div></div>';
  130. }
  131. }
  132. */
  133. /**
  134. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  135. */
  136. function tripal_ds_bundle_menu_item($bundle_name, $field_label, $field_name, $entity_type){
  137. //Check the record does not already exist
  138. $tripal_ds_rows = db_select('tripal_ds', 'ds')
  139. ->fields('ds', array('tripal_ds_field_name', 'tripal_ds_field_label'))
  140. ->condition('bundle', $bundle_name, '=')
  141. ->condition('tripal_ds_field_label', $field_label, '=')
  142. ->condition('tripal_ds_field_name', $field_name, '=')
  143. ->execute()->fetchAll();
  144. if(!empty($tripal_ds_rows)){
  145. foreach ($tripal_ds_rows as $tripal_ds_row){
  146. 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)) {
  147. // Do not write the field to the table
  148. drupal_set_message("Could not update the bundle menu because that field already exists.", 'error');
  149. }
  150. }
  151. }
  152. else {
  153. //Write to the tripal_ds table to record the new tripal pane.
  154. $field_for_table = new stdClass();
  155. $field_for_table->tripal_ds_field_name = $field_name;
  156. $field_for_table->tripal_ds_field_label = $field_label;
  157. $field_for_table->entity_type = $entity_type;
  158. $field_for_table->bundle = $bundle_name;
  159. drupal_write_record('tripal_ds', $field_for_table);
  160. }
  161. }
  162. /*
  163. * Implements hook_ds_layout_info() to define layouts from code in a module for
  164. * display suite
  165. */
  166. function tripal_ds_ds_layout_info() {
  167. $path = drupal_get_path('module', 'tripal_ds');
  168. $layouts = array(
  169. 'tripal_ds_feature' => array(
  170. 'label' => t('Tripal Feature Layout'),
  171. 'path' => $path . '/theme/templates',
  172. 'regions' => array(
  173. 'left' => t('Left'),
  174. 'right' => t('Right'),
  175. ),
  176. 'css' => TRUE,
  177. ),
  178. );
  179. return $layouts;
  180. }
  181. function tripal_ds_update_layout_form($form, &$form_state, $bundle_name) {
  182. $form = array();
  183. $form['bundle_name'] = array(
  184. '#type' => 'value',
  185. '#value' => $bundle_name,
  186. );
  187. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  188. $bundle_label = $bundle->label;
  189. return confirm_form($form,
  190. t('Please confirm you would like to apply this layout: ' . $bundle_label),
  191. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  192. t('This action cannot be undone.'),
  193. t('Yes, apply layout'),
  194. t('No, cancel')
  195. );
  196. }
  197. /**
  198. *
  199. * @param $bundle_name
  200. */
  201. function tripal_ds_update_layout_form_submit($form, &$form_state) {
  202. $bundle_name = $form_state['build_info']['args'][0];
  203. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  204. //Build the identifier to check against ds_layout_settings.
  205. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  206. //Check to see if the layout already exists.
  207. $result = db_select('ds_layout_settings', 'ds')
  208. ->fields('ds')
  209. ->condition('id', $ds_identifier, '=')
  210. ->execute()
  211. ->fetchField();
  212. //Check to see if there are any field groups associated with the bundle.
  213. $result_fg = db_select('field_group', 'fg')
  214. ->fields('fg')
  215. ->condition('bundle', $bundle_name, '=')
  216. ->execute()
  217. ->fetchField();
  218. //Check to see if there are any tripal ds fields associated with the bundle.
  219. $result_tds = db_select('tripal_ds', 'tds')
  220. ->fields('tds')
  221. ->condition('bundle', $bundle_name, '=')
  222. ->execute();
  223. //Check to see if there are any field settings associated with the bundle.
  224. $result_fs = db_select('ds_field_settings', 'fs')
  225. ->fields('fs')
  226. ->condition('bundle', $bundle_name, '=')
  227. ->execute();
  228. //If the layout exists, delete it.
  229. if(!empty($result)) {
  230. db_delete('ds_layout_settings')
  231. ->condition('id', $ds_identifier, '=')
  232. ->execute();
  233. }
  234. //Then delete the field_group_fields associated with the identifier.
  235. if(!empty($result_fg)) {
  236. db_delete('field_group')
  237. ->condition('bundle', $bundle_name, '=')
  238. ->execute();
  239. }
  240. //Then delete the ds_field_settings associated with the identifier.
  241. if(!empty($result_tds)) {
  242. db_delete('ds_field_settings')
  243. ->condition('bundle', $bundle_name, '=')
  244. ->execute();
  245. }
  246. //Then delete the tripal_ds menu item.
  247. if(!empty($result_fs)) {
  248. db_delete('tripal_ds')
  249. ->condition('bundle', $bundle_name, '=')
  250. ->execute();
  251. }
  252. //Now you can build the layout fresh.
  253. $instances = field_info_instances('TripalEntity', $bundle_name);
  254. $bundle_data_table = $bundle->data_table;
  255. if($bundle_data_table == 'pub'){
  256. $success = _ds_layout_pub_settings_info($bundle_name, $instances);
  257. }
  258. else {
  259. $success = _ds_layout_settings_info($bundle_name, $instances);
  260. }
  261. if ($success) {
  262. drupal_set_message("Layout applied successfully and saved.");
  263. }
  264. else {
  265. drupal_set_message("Could not apply layout.", 'error');
  266. }
  267. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  268. }
  269. /*
  270. * Code for the view of the menu items
  271. //get tripal entity id from url then run it against tripal entity db
  272. //and grab the bundle id, then pass bundle id to view
  273. $url = current_path();
  274. $url_exploded = explode("/", $url);
  275. $tripal_entity_id = (int)$url_exploded[1];
  276. $result = db_select('tripal_entity', 'te')
  277. ->fields('te', array('bundle'))
  278. ->condition('id', $tripal_entity_id, '=')
  279. ->execute()
  280. ->fetchField();
  281. */