filters.statusnotify.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * A filter for compressing bckups with zip, gz bzip etc.
  5. */
  6. /**
  7. * A filter to send a notification email on success or failure of backup.
  8. *
  9. * @ingroup backup_migrate_filters
  10. */
  11. class backup_migrate_filter_statusnotify extends backup_migrate_filter {
  12. /**
  13. * Get the default backup settings for this filter.
  14. */
  15. public function backup_settings_default() {
  16. return array(
  17. 'notify_success_enable' => FALSE,
  18. 'notify_failure_enable' => FALSE,
  19. 'notify_success_email' => variable_get('site_mail', ''),
  20. 'notify_failure_email' => variable_get('site_mail', ''),
  21. );
  22. }
  23. /**
  24. * Get the form for the settings for this filter.
  25. */
  26. public function backup_settings_form($settings) {
  27. $form = array();
  28. $form['advanced']['notify_success_enable'] = array(
  29. "#type" => 'checkbox',
  30. "#title" => t("Send an email if backup succeeds"),
  31. "#default_value" => @$settings['notify_success_enable'],
  32. );
  33. $form['advanced']['notify_success_email_wrapper'] = array(
  34. '#type' => 'backup_migrate_dependent',
  35. '#dependencies' => array(
  36. 'filters[notify_success_enable]' => TRUE,
  37. ),
  38. );
  39. $form['advanced']['notify_success_email_wrapper']['notify_success_email'] = array(
  40. "#type" => "textfield",
  41. "#title" => t("Email Address for Success Notices"),
  42. "#default_value" => @$settings['notify_success_email'],
  43. );
  44. $form['advanced']['notify_failure_enable'] = array(
  45. "#type" => 'checkbox',
  46. "#title" => t("Send an email if backup fails"),
  47. "#default_value" => @$settings['notify_failure_enable'],
  48. );
  49. $form['advanced']['notify_failure_email_wrapper'] = array(
  50. '#type' => 'backup_migrate_dependent',
  51. '#dependencies' => array(
  52. 'filters[notify_failure_enable]' => TRUE,
  53. ),
  54. );
  55. $form['advanced']['notify_failure_email_wrapper']['notify_failure_email'] = array(
  56. "#type" => "textfield",
  57. "#title" => t("Email Address for Failure Notices"),
  58. "#default_value" => @$settings['notify_failure_email'],
  59. );
  60. return $form;
  61. }
  62. /**
  63. * Send the success email.
  64. */
  65. public function backup_succeed($settings) {
  66. if (@$settings->filters['notify_success_enable'] && $to = @$settings->filters['notify_success_email']) {
  67. $messages = $this->get_messages();
  68. if ($messages = $this->get_messages()) {
  69. $body = t("The site backup has completed successfully with the following messages:\n!messages", array('!messages' => $messages));
  70. }
  71. else {
  72. $body = t("The site backup has completed successfully.\n");
  73. }
  74. drupal_mail('backup_migrate', 'backup_succeed', $settings->filters['notify_success_email'], language_default(), array('body' => $body));
  75. }
  76. }
  77. /**
  78. * Send the failure email.
  79. */
  80. public function backup_fail($settings) {
  81. if (@$settings->filters['notify_failure_enable'] && $to = @$settings->filters['notify_failure_email']) {
  82. $messages = $this->get_messages();
  83. if ($messages = $this->get_messages()) {
  84. $body = t("The site backup has failed with the following messages:\n!messages", array('!messages' => $messages));
  85. }
  86. else {
  87. $body = t("The site backup has failed for an unknown reason.");
  88. }
  89. drupal_mail('backup_migrate', 'backup_fail', $settings->filters['notify_failure_email'], language_default(), array('body' => $body));
  90. }
  91. }
  92. /**
  93. * Render the messages and errors for the email.
  94. */
  95. public function get_messages() {
  96. $out = "";
  97. $messages = _backup_migrate_messages();
  98. foreach ($messages as $message) {
  99. $out .= strip_tags(t($message['message'], $message['replace'])) . "\n";
  100. }
  101. return $out;
  102. }
  103. }