tripal_ds.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. // Adds a +Add Tripal Pane button to 'Tripal Content Types' page.
  38. $items['admin/structure/bio_data/manage/%/display/create'] = array(
  39. 'title' => 'Create an empty Tripal Pane',
  40. 'description' => t('Create a new empty tripal pane.'),
  41. 'page callback' => 'drupal_get_form',
  42. 'access arguments' => array('administer tripal'),
  43. 'page arguments' => array('tripal_ds_pane_addition_button_form', 4),
  44. 'type' => MENU_LOCAL_ACTION,
  45. );
  46. return $items;
  47. }
  48. /**
  49. * Implements hook_bundle_postcreate().
  50. *
  51. * This is a Triapl defined hook and is called in the TripalBundle::create()
  52. * function to allow modules to perform tasks when a bundle is created.
  53. */
  54. function tripal_ds_bundle_postcreate($bundle) {
  55. $bundle_name = $bundle->name;
  56. $bundle_data_table = $bundle->data_table;
  57. $instances = field_info_instances('TripalEntity', $bundle_name);
  58. if($bundle_data_table == 'pub'){
  59. _ds_layout_pub_settings_info($bundle_name, $instances);
  60. }
  61. else {
  62. _ds_layout_settings_info($bundle_name, $instances);
  63. }
  64. }
  65. /**
  66. * Update the tripal_ds table when a tripal pane is deleted.
  67. */
  68. function tripal_ds_table_column_delete($bundle){
  69. $bundle_name = $bundle->name;
  70. db_delete('tripal_ds')
  71. ->condition('bundle', $bundle_name, '=')
  72. ->execute();
  73. }
  74. /**
  75. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  76. */
  77. function tripal_ds_bundle_delete($bundle){
  78. tripal_ds_table_column_delete($bundle);
  79. }
  80. /**
  81. * Implements hook_ds_field_settings_alter()
  82. */
  83. function tripal_ds_ds_field_settings_alter(&$field_settings, $form, $form_state){
  84. // Get the form info from the bundle about to be saved.
  85. $tripal_entity_object = $form_state['build_info']['args']['1'];
  86. // Grab the bundle.
  87. $bundle_id = $tripal_entity_object->name;
  88. // Grab the field groups from the bundle.
  89. $updated_field_groups = $form_state['field_group'];
  90. // Grab the fields from the bundle.
  91. $fields = $form_state['values']['fields'];
  92. // Delete the menu items associated with the bundle id.
  93. db_delete('tripal_ds')
  94. ->condition('bundle', $bundle_id, '=')
  95. ->execute();
  96. // Traverse the updated field_groups grabbing the tripal pane items.
  97. $tripal_pane_field_groups = array();
  98. $i = 0;
  99. foreach($updated_field_groups as $updated_field_group){
  100. if($updated_field_group->format_type == 'tripalpane'){
  101. $tripal_pane_field_groups += [ $i => $updated_field_group->group_name];
  102. $i++;
  103. }
  104. }
  105. // Now grab the labels of the tripalpane.
  106. foreach($updated_field_groups as $updated_field_group){
  107. foreach($tripal_pane_field_groups as $tripal_pane_field_group){
  108. if($updated_field_group->group_name == $tripal_pane_field_group){
  109. if($fields[$tripal_pane_field_group]['region'] !== 'hidden'){
  110. tripal_ds_bundle_menu_item($bundle_id, $updated_field_group->label, $tripal_pane_field_group, 'tripalentity');
  111. }
  112. }
  113. }
  114. }
  115. // Update the menu items weight.
  116. tripal_ds_toc_order($bundle_id, $form_state['values']['fields']);
  117. }
  118. /**
  119. *
  120. * Trigger the update to the tripal_ds table when a tripal pane is deleted.
  121. *
  122. * @param $bundle_name
  123. * @param $field_label
  124. * @param $field_name
  125. * @param $entity_type
  126. */
  127. function tripal_ds_bundle_menu_item($bundle_name, $field_label, $field_name, $entity_type){
  128. //Check the record does not already exist
  129. $tripal_ds_rows = db_select('tripal_ds', 'ds')
  130. ->fields('ds', array('tripal_ds_field_name', 'tripal_ds_field_label'))
  131. ->condition('bundle', $bundle_name, '=')
  132. ->condition('tripal_ds_field_label', $field_label, '=')
  133. ->condition('tripal_ds_field_name', $field_name, '=')
  134. ->execute()->fetchAll();
  135. if(!empty($tripal_ds_rows)){
  136. foreach ($tripal_ds_rows as $tripal_ds_row){
  137. 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)) {
  138. // Do not write the field to the table
  139. drupal_set_message("Could not update the bundle menu because that field already exists.", 'error');
  140. }
  141. }
  142. }
  143. else {
  144. //Write to the tripal_ds table to record the new tripal pane.
  145. $field_for_table = new stdClass();
  146. $field_for_table->tripal_ds_field_name = $field_name;
  147. $field_for_table->tripal_ds_field_label = $field_label;
  148. $field_for_table->entity_type = $entity_type;
  149. $field_for_table->bundle = $bundle_name;
  150. drupal_write_record('tripal_ds', $field_for_table);
  151. }
  152. }
  153. /**
  154. * Implements hook_ds_layout_info() to define layouts from code in a module for
  155. * display suite
  156. */
  157. function tripal_ds_ds_layout_info() {
  158. $path = drupal_get_path('module', 'tripal_ds');
  159. $layouts = array(
  160. 'tripal_ds_feature' => array(
  161. 'label' => t('Tripal Feature Layout'),
  162. 'path' => $path . '/theme/templates',
  163. 'regions' => array(
  164. 'left' => t('Left'),
  165. 'right' => t('Right'),
  166. ),
  167. 'css' => TRUE,
  168. ),
  169. );
  170. return $layouts;
  171. }
  172. /**
  173. * Implements hook_form()
  174. *
  175. * Adds a confirmation message to applying default layout option in 'Manage
  176. * Display'
  177. *
  178. * @param $form
  179. * @param $form_state
  180. * @param $bundle_name
  181. *
  182. * @return mixed
  183. */
  184. function tripal_ds_update_layout_form($form, &$form_state, $bundle_name) {
  185. $form = array();
  186. $form['bundle_name'] = array(
  187. '#type' => 'value',
  188. '#value' => $bundle_name,
  189. );
  190. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  191. $bundle_label = $bundle->label;
  192. return confirm_form($form,
  193. t('Please confirm you would like to apply this layout: ' . $bundle_label),
  194. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  195. t('This action cannot be undone.'),
  196. t('Yes, apply layout'),
  197. t('No, cancel')
  198. );
  199. }
  200. /**
  201. * Implements hook_form_submit()
  202. *
  203. * @param $form_state
  204. * @param $form
  205. */
  206. function tripal_ds_update_layout_form_submit($form, &$form_state) {
  207. $bundle_name = $form_state['build_info']['args'][0];
  208. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  209. //Build the identifier to check against ds_layout_settings.
  210. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  211. //Check to see if the layout already exists.
  212. $result = db_select('ds_layout_settings', 'ds')
  213. ->fields('ds')
  214. ->condition('id', $ds_identifier, '=')
  215. ->execute()
  216. ->fetchField();
  217. //Check to see if there are any field groups associated with the bundle.
  218. $result_fg = db_select('field_group', 'fg')
  219. ->fields('fg')
  220. ->condition('bundle', $bundle_name, '=')
  221. ->execute()
  222. ->fetchField();
  223. //Check to see if there are any tripal ds fields associated with the bundle.
  224. $result_tds = db_select('tripal_ds', 'tds')
  225. ->fields('tds')
  226. ->condition('bundle', $bundle_name, '=')
  227. ->execute();
  228. //Check to see if there are any field settings associated with the bundle.
  229. $result_fs = db_select('ds_field_settings', 'fs')
  230. ->fields('fs')
  231. ->condition('bundle', $bundle_name, '=')
  232. ->execute();
  233. //If the layout exists, delete it.
  234. if(!empty($result)) {
  235. db_delete('ds_layout_settings')
  236. ->condition('id', $ds_identifier, '=')
  237. ->execute();
  238. }
  239. //Then delete the field_group_fields associated with the identifier.
  240. if(!empty($result_fg)) {
  241. db_delete('field_group')
  242. ->condition('bundle', $bundle_name, '=')
  243. ->execute();
  244. }
  245. //Then delete the ds_field_settings associated with the identifier.
  246. if(!empty($result_tds)) {
  247. db_delete('ds_field_settings')
  248. ->condition('bundle', $bundle_name, '=')
  249. ->execute();
  250. }
  251. //Then delete the tripal_ds menu item.
  252. if(!empty($result_fs)) {
  253. db_delete('tripal_ds')
  254. ->condition('bundle', $bundle_name, '=')
  255. ->execute();
  256. }
  257. //Now you can build the layout fresh.
  258. $instances = field_info_instances('TripalEntity', $bundle_name);
  259. $bundle_data_table = $bundle->data_table;
  260. if($bundle_data_table == 'pub'){
  261. $success = _ds_layout_pub_settings_info($bundle_name, $instances);
  262. }
  263. else {
  264. $success = _ds_layout_settings_info($bundle_name, $instances);
  265. }
  266. if ($success) {
  267. drupal_set_message("Layout applied successfully and saved.");
  268. }
  269. else {
  270. drupal_set_message("Could not apply layout.", 'error');
  271. }
  272. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  273. }
  274. /**
  275. * Implements hook_form()
  276. *
  277. * Creates the button that creates an empty tripal pane.
  278. *
  279. * @param $form
  280. * @param $form_state
  281. * @param $bundle_name
  282. * @return mixed
  283. */
  284. function tripal_ds_pane_addition_button_form($form, &$form_state, $bundle_name) {
  285. $form = array();
  286. $form['bundle_name'] = array(
  287. '#type' => 'value',
  288. '#value' => $bundle_name,
  289. );
  290. $form['field_name'] = array(
  291. '#type' => 'textfield',
  292. '#title' => t('Tripal Panel Label'),
  293. '#required' => TRUE,
  294. '#description' => "Please enter the label for the new Tripal Pane",
  295. '#size' => 20,
  296. '#maxlength' => 50,
  297. );
  298. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  299. $bundle_label = $bundle->label;
  300. return confirm_form($form,
  301. t('Please confirm you would like to create a new field for: ' . $bundle_label),
  302. 'admin/structure/bio_data/manage/' . $bundle_name . '/display',
  303. t('Create new Tripal Pane'),
  304. t('Yes'),
  305. t('No, cancel')
  306. );
  307. }
  308. /**
  309. * Implements hook_form_submit()
  310. *
  311. * @param $form_state
  312. * @param $form
  313. */
  314. function tripal_ds_pane_addition_button_form_submit($form, &$form_state) {
  315. $bundle_name = $form_state['build_info']['args'][0];
  316. //Build the rest of the passed variables.
  317. $field_name = $form_state['input']['field_name'];
  318. $group_field_name = 'gp_'.$form_state['input']['field_name'];
  319. //Create the field groups, last passed parameter is NULL because there are no
  320. //children.
  321. _additional_fields_field_group_info($bundle_name, $field_name, $group_field_name, NULL);
  322. //Place the field groups in the layout.
  323. tripal_ds_update_ds_layout($bundle_name, NULL, $group_field_name);
  324. drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
  325. }
  326. /**
  327. * @param $bundle_name
  328. */
  329. function tripal_ds_update_ds_layout($bundle_name, $field_name, $tripal_pane_name) {
  330. //Build the identifier to check against ds_layout_settings.
  331. $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';
  332. //Check to see if the layout already exists.
  333. $result = db_select('ds_layout_settings', 'ds')
  334. ->fields('ds', array('settings'))
  335. ->condition('ds.id', $ds_identifier, '=')
  336. ->execute()
  337. ->fetchObject();
  338. //If the layout exists unserialize it.
  339. if(!empty($result)) {
  340. $layout_info = $result->settings;
  341. $layout_info = unserialize($layout_info);
  342. //Count the number of rows in the region and add the field to the region.
  343. $index = count($layout_info['regions']['right']);
  344. //Now add the tripal pane and field to the right region and field array.
  345. if(!empty($field_name)){
  346. $layout_info['regions']['right'][$index] = $field_name;
  347. $incremented_index = $index++;
  348. $layout_info['fields'][$field_name] = 'right';
  349. }
  350. if(!empty($tripal_pane_name)){
  351. if(!empty($incremented_index)){
  352. $layout_info['regions']['right'][$incremented_index] = $tripal_pane_name;
  353. $layout_info['fields'][$tripal_pane_name] = 'right';
  354. }
  355. else {
  356. $layout_info['regions']['right'][$index] = $tripal_pane_name;
  357. $layout_info['fields'][$tripal_pane_name] = 'right';
  358. }
  359. }
  360. //Update the ds_layout_settings table with the new layout info.
  361. drupal_write_record('ds_layout_settings', $layout_info);
  362. }
  363. }
  364. /*
  365. * Code for the view of the menu items
  366. //get tripal entity id from url then run it against tripal entity db
  367. //and grab the bundle id, then pass bundle id to view
  368. $url = current_path();
  369. $url_exploded = explode("/", $url);
  370. $tripal_entity_id = (int)$url_exploded[1];
  371. $result = db_select('tripal_entity', 'te')
  372. ->fields('te', array('bundle'))
  373. ->condition('id', $tripal_entity_id, '=')
  374. ->execute()
  375. ->fetchField();
  376. */
  377. /**
  378. * Implements hook_field_display_alter().
  379. * @param $display
  380. * @param $context
  381. */
  382. function tripal_ds_field_display_alter(&$display, $context){
  383. $field_name = $context['field']['field_name'];
  384. $bundle = $context['entity']->bundle;
  385. $bundle_info = tripal_load_bundle_entity(array('name' => $bundle));
  386. $hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle_info->id, 'hide');
  387. if ($field_name && ($hide_variable == 'hide')) {
  388. $item = field_get_items('TripalEntity', $context['entity'], $field_name);
  389. $field = field_info_field($field_name);
  390. if ($item) {
  391. if (tripal_field_is_empty($item[0], $field)) {
  392. $parent_field_info = tripal_ds_find_field_group_parent($field_name, 'TripalEntity', $bundle, $context);
  393. if (!empty($parent_field_info)) {
  394. foreach ($parent_field_info as $parent_key => $parent_field){
  395. // Stop the right rail element from rendering.
  396. drupal_add_css('.' . $parent_field_info[$parent_key] . ' {display:none;}', 'inline');
  397. // Hide any associated menu links.
  398. drupal_add_css('#' . $parent_field_info[$parent_key] . ' {display:none;}', 'inline');
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. /*
  406. */
  407. /**
  408. * Identifies field_group parents to find tripal_panes and return that
  409. * information to the function that calls it.
  410. *
  411. * @param $field_name
  412. * @param $entity_type
  413. * @param $bundle
  414. *
  415. * @return array
  416. */
  417. function tripal_ds_find_field_group_parent($field_name, $entity_type, $bundle, $context){
  418. $field_groups_to_hide = array();
  419. $increment = 0;
  420. // Get the field groups associated with this bundle.
  421. $fg_for_bundle = db_select('field_group', 'fg')
  422. ->fields('fg')
  423. ->condition('bundle', $bundle, '=')
  424. ->condition('entity_type', $entity_type, '=')
  425. ->execute()->fetchAll();
  426. // Run through the field groups looking for the provided $field_name
  427. foreach ($fg_for_bundle as $field_groups => $field_group) {
  428. $field_group_data = unserialize($field_group->data);
  429. // There is a separate function to deal with tables, so disregard.
  430. if ($field_group_data['format_type'] == 'table'){
  431. // Do nothing
  432. }
  433. elseif (!empty($field_group_data['children'][0])) {
  434. $children = $field_group_data['children'];
  435. //If there is more than one child all need to be checked.
  436. if (count($children) > 1) {
  437. foreach ($children as $kids => $child) {
  438. // Now check if each child if empty.
  439. $item = field_get_items('TripalEntity', $context['entity'], $child);
  440. $field = field_info_fild($child);
  441. if(!tripal_field_is_empty($item[0], $field)){
  442. //If any of the fields are not empty do not add the parent.
  443. break 2;
  444. }
  445. else {
  446. continue;
  447. }
  448. }
  449. $field_groups_to_hide[$increment] = $field_group->group_name;
  450. }
  451. elseif($children[0] == $field_name) {
  452. $field_groups_to_hide[$increment] = $field_group->group_name;
  453. }
  454. }
  455. $increment++;
  456. }
  457. // Remove duplicate values.
  458. $field_groups_to_hide = array_unique($field_groups_to_hide);
  459. return $field_groups_to_hide;
  460. }
  461. /**
  462. * Updates the tripal_ds table with weight information of field_groups on
  463. * save of "manage display' of content type. Weight is what is used to order
  464. * the menu items.
  465. *
  466. * @param $bundle
  467. * @param array $fields
  468. */
  469. function tripal_ds_toc_order($bundle, $fields = array()){
  470. // Find all menu items associated with the bundle id.
  471. $menu_items = db_select('tripal_ds', 'ds')
  472. ->fields('ds')
  473. ->condition('bundle', $bundle, '=')
  474. ->execute()->fetchAll();
  475. // Now find all menu items in the $fields array
  476. foreach ($menu_items as $menu_items => $menu_item) {
  477. $toc_field_name = $menu_item->tripal_ds_field_name;
  478. // Compare the field name from the table with the fields in the array.
  479. if (array_key_exists($toc_field_name, $fields)) {
  480. $weight = $fields[$toc_field_name]['weight'];
  481. //If a weight is returned update the tripal_ds table.
  482. if(!empty($weight)){
  483. db_update('tripal_ds')
  484. ->fields(array(
  485. 'weight' => $weight,
  486. ))
  487. ->condition('bundle', $bundle, '=')
  488. ->condition('tripal_ds_field_name', $toc_field_name, '=')
  489. ->execute();
  490. }
  491. }
  492. }
  493. }