helper.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. class HelperBBus
  3. {
  4. public $db;
  5. public function __construct(DoliDB $db)
  6. {
  7. $this->db = $db;
  8. }
  9. /**
  10. * Output the buttons to submit a creation/edit form
  11. *
  12. * @param string $save_label Alternative label for save button
  13. * @param string $cancel_label Alternative label for cancel button
  14. * @param array $morebuttons Add additional buttons between save and cancel
  15. * @param bool $withoutdiv Option to remove enclosing centered div
  16. * @param string $morecss More CSS
  17. * @param string $dol_openinpopup If the button are shown in a context of a page shown inside a popup, we put here the string name of popup.
  18. * @return string Html code with the buttons
  19. */
  20. public function buttonsSaveCancel($save_label = 'Save', $cancel_label = 'Cancel', $cancelURL = '', $morebuttons = array(), $withoutdiv = 0, $morecss = '', $dol_openinpopup = '')
  21. {
  22. global $langs;
  23. $buttons = array();
  24. $save = array(
  25. 'name' => 'save',
  26. 'label_key' => $save_label,
  27. );
  28. if ($save_label == 'Create' || $save_label == 'Add') {
  29. $save['name'] = 'add';
  30. } elseif ($save_label == 'Modify') {
  31. $save['name'] = 'edit';
  32. }
  33. $cancel = array(
  34. 'name' => 'cancel',
  35. 'label_key' => 'Cancel',
  36. );
  37. !empty($save_label) ? $buttons[] = $save : '';
  38. if (!empty($morebuttons)) {
  39. $buttons[] = $morebuttons;
  40. }
  41. //!empty($cancel_label) ? $buttons[] = $cancel : '';
  42. $retstring = $withoutdiv ? '' : '<div class="center">';
  43. foreach ($buttons as $button) {
  44. $addclass = empty($button['addclass']) ? '' : $button['addclass'];
  45. $retstring .= '<input type="submit" class="button button-' . $button['name'] . ($morecss ? ' ' . $morecss : '') . ' ' . $addclass . '" name="' . $button['name'] . '" value="' . dol_escape_htmltag($langs->trans($button['label_key'])) . '">';
  46. }
  47. if ($cancel_label == "Cancel") {
  48. $retstring .= '<a class="button button-' . $cancel['name'] . '" href="' . dol_buildpath($cancelURL, 1) . '?restore_lastsearch_values=1' . (!empty($socid) ? '&socid=' . $socid : '') . '">' . $langs->trans($cancel['label_key']) . '</a>';
  49. }
  50. $retstring .= $withoutdiv ? '' : '</div>';
  51. if ($dol_openinpopup) {
  52. $retstring .= '<!-- buttons are shown into a $dol_openinpopup=' . $dol_openinpopup . ' context, so we enable the close of dialog on cancel -->' . "\n";
  53. $retstring .= '<script>';
  54. $retstring .= 'jQuery(".button-cancel").click(function(e) {
  55. e.preventDefault(); console.log(\'We click on cancel in iframe popup ' . $dol_openinpopup . '\');
  56. window.parent.jQuery(\'#idfordialog' . $dol_openinpopup . '\').dialog(\'close\');
  57. });';
  58. $retstring .= '</script>';
  59. }
  60. return $retstring;
  61. }
  62. }