tripal_ds.module 17 KB

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