CGI-OptimalQuery

 view release on metacpan or  search on metacpan

lib/CGI/OptimalQuery/SaveSearchTool.pm  view on Meta::CPAN

          $opts{error_handler}->("info", "sending email to: $email{to}; subject: $email{subject}");
          $sendmail_handler->(%email) or die "could not send email to: $$rec{email_to}";
        }
      }
    }

    else {
      my $total_new = 0;
      my $total_deleted = 0;
      my $total_count = 0;
      while (my ($uid, $status) = each %uids) {
        if ($status == 1) {
          $total_deleted++;
        }
        else {
          push @update_uids, $uid;
          $total_count++;
          if ($status == 3) {
            $total_new++;
          }
        }
      }
      $opts{error_handler}->("info", "total_new: $total_new; total_deleted: $total_deleted; total_count: $total_count");

      my $should_send_email;
      $should_send_email = 1 if
        ( # alert when records are added
          ($$rec{ALERT_MASK} & 1 && $total_new > 0) ||
          # alert when records are deleted
          ($$rec{ALERT_MASK} & 2 && $total_deleted > 0) ||
          # alert when records are present
          ($$rec{ALERT_MASK} & 4 && $total_count > 0)
        );

      if ($should_send_email) {
        my %email = (
          to => $$rec{email_to},
          from => $$rec{email_from} || $opts{email_from},
          'Reply-To' => $$rec{'email_Reply-To'} || $opts{'email_Reply-To'},
          subject => "$$rec{OQ_TITLE} - $$rec{USER_TITLE}",
          'content-type' => 'text/html; charset="iso-8859-1"'
        );
        $email{subject} .= " ($total_new added)" if $total_new > 0; 

        $email{body} = 
"<html>
<head>
<title>".escapeHTML("$$rec{OQ_TITLE} - $$rec{USER_TITLE}")."</title>
<style>
.OQSSAlert * {
  font-family: sans-serif;
}
.OQSSAlert h2 {
  margin: 0;
  font-size: 14px;
}
.OQSSAlert table {
  border-collapse: collapse;
}
.OQSSAlert thead td {
  font-weight: bold;
  color: white;
  background-color: #999;
}
.OQSSAlert td {
  padding: 4px;
  border: 1px solid #aaa;
  font-size: 11px;
}
.OQSSAlert .ftv {
  background-color: #E2FFE2;
}
.OQSSAlert p {
  margin: .5em 0;
}
.OQSSAlert .ib {
  display: inline-block;
  margin-left: 6px;
  padding: 6px;
}
</style>
</head>
<body>
<div class=OQSSAlert>
<h2>".escapeHTML("$$rec{OQ_TITLE} - $$rec{USER_TITLE}")."</h2>
<p>
$$rec{buf}
<p>
<span class=ib>total: $total_count</span>
<span class='ftv ib'>added: $total_new</span>
<span class=ib>removed: $total_deleted</span>
<p>
<a href='".escapeHTML($opts{base_url}.'/'.$$rec{URI}.'?OQLoadSavedSearch='.escape($$rec{ID}).$$rec{state_param_args})."'>load report</a>
</div>
</body>
</html>";

        if ($opts{debug}) {
          $opts{error_handler}->("debug", "debug sendmail (not sent): ".Dumper(\%email));
        } else {
          $opts{error_handler}->("info", "sending email to: $email{to}; subject: $email{subject}");
          $sendmail_handler->(%email) or die "could not send email to: $$rec{email_to}";
        }
      }
    }

    # update database
    my $update_uids = join('~', sort @update_uids);
    $update_uids = undef if $update_uids eq '';
    $$rec{err_msg} = undef if $$rec{err_msg} eq '';
    my @binds = ($$rec{err_msg});
    my $now = get_sysdate_sql($dbh);

    my $sql = "UPDATE oq_saved_search SET alert_last_dt=$now, alert_err=?";
    if (! $$rec{err_msg} && $update_uids ne $$rec{ALERT_UIDS}) {
      $sql .= ", alert_uids=?";
      push @binds, $update_uids;
    }
    $sql .= " WHERE id=?";
    push @binds, $$rec{ID};
    $opts{error_handler}->("debug", "SQL: $sql\nBINDS: ".join(',', @binds)) if $opts{debug};



( run in 1.147 second using v1.01-cache-2.11-cpan-d7f47b0818f )