From 87b38e1cb5d5d5ebddfa4601a9088c3eadaf7ef0 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 31 Jul 2016 23:39:22 +0200 Subject: Easier emails --- include/ajax-email-offer.php | 86 ++++++++++++++++++++++++++++++++ include/offers-view.php | 20 -------- include/offers.php | 115 +++++++++++++++++++++---------------------- 3 files changed, 143 insertions(+), 78 deletions(-) create mode 100644 include/ajax-email-offer.php (limited to 'include') diff --git a/include/ajax-email-offer.php b/include/ajax-email-offer.php new file mode 100644 index 0000000..3960d37 --- /dev/null +++ b/include/ajax-email-offer.php @@ -0,0 +1,86 @@ +. + */ + +require_once('./login-ajax.php'); + +$_offer = new Offer($_pdo, $_REQUEST['id']); +$_mailer = $_offer->mailer(); + +function format_email($address) { + if ($address[1] != '') { + return "{$address[1]} <{$address[0]}>"; + } else { + return $address[0]; + } +} + +if ($_SERVER['REQUEST_METHOD'] === 'GET') { +?> +
+ +
+ +
+
+ getReplyToAddresses() as $addr) { ?> +
+ +
+
+ getBccAddresses() as $addr) { ?> +
+ +
+
+ getCcAddresses() as $addr) { ?> +
+ +
+
+ getToAddresses() as $addr) { ?> +
+ +
+
+ +
+ +
+
+
+ +
+
+
+Subject = $_POST['subject']; + $_mailer->Body = $_POST['body']; + try { + if ($_mailer->send()) { + echo 'OK'; + } else { + http_response_code(500); + echo 'Sending the email failed:
' . $_mailer->ErrorInfo; + } + } catch (Exception $e) { + http_response_code(500); + echo "Sending the email failed with an exception ({$e->getCode()}): {$e->getMessage()}
" . $e->getTraceAsString(); + } +} diff --git a/include/offers-view.php b/include/offers-view.php index d96e9ed..ec579b6 100644 --- a/include/offers-view.php +++ b/include/offers-view.php @@ -107,26 +107,6 @@ $_offer = new Offer($_pdo, $_id); -
-
-
-
Tools
-
- accepted ? - ['Accepted', 'btn-success'] : - ['Not accepted', 'btn-default']; - $eligible = $_offer->getPaymentEligibility() ? - ['Eligible for online payment', 'btn-success'] : - ['Not eligible for online payment', 'btn-default']; - ?> - - - Send invoice to contact - Delete -
-
-
diff --git a/include/offers.php b/include/offers.php index dbe5df0..e06a4eb 100644 --- a/include/offers.php +++ b/include/offers.php @@ -41,49 +41,22 @@ require('./header.php'); // ?delete= Delete the offer with id //------------------------------------------------------------------------------ - // The header of the page - $header = 'Offers'; - // Whether or not to show an individual offer in the end (false if not, or the id if yes) - $show_individual = false; - - // View offer - if (isset($_GET['id'])) { - $id = (int) $_GET['id']; - try { - $offer = new Offer($_pdo, $id); - $header = "Offers / #{$offer->id}"; - $show_individual = $id; - } catch (PDOException $e) { - $alert = "
The offer with id $id could not be found.
"; - } catch (Exception $e) { - $alert = "
The offer with id $id could not be found.
"; - } - } - - // Show the header - echo "

$header

"; - if (isset($alert)) echo "
$alert
"; - // Accept offer if (isset($_GET['toggle_accept'])) { - echo "
"; $id = (int) $_GET['toggle_accept']; try { $offer = new Offer($_pdo, $id); $offer->accepted = !$offer->accepted; - echo "
The status of offer #{$offer->id} has been set to ".($offer->accepted ? "accepted" : "unaccepted").".
"; + $alert = "
The status of offer #{$offer->id} has been set to ".($offer->accepted ? "accepted" : "unaccepted").".
"; } catch (PDOException $e) { - echo "
The status of the offer could not be changed due to a PDO error.
"; + $alert = "
The status of the offer could not be changed due to a PDO error.
"; } catch (Exception $e) { - echo "
The offer with id {$id} could not be found.
"; + $alert = "
The offer with id {$id} could not be found.
"; } - - echo "
"; } // Toggle offer payment eligibility if (isset($_GET['toggle_payment_eligibility'])) { - echo "
"; $id = (int) $_GET['toggle_payment_eligibility']; try { $offer = new Offer($_pdo, $id); @@ -92,91 +65,117 @@ require('./header.php'); } else { $offer->payment_key = Offer::getRandomPaymentKey(); } - echo "
The offer #{$offer->id} is now ".($offer->getPaymentEligibility() ? "eligible" : "ineligible")." for online payment.
"; + $alert = "
The offer #{$offer->id} is now ".($offer->getPaymentEligibility() ? "eligible" : "ineligible")." for online payment.
"; } catch (PDOException $e) { - echo "
The online payment eligibility could not be changed due to a PDO error.
"; + $alert = "
The online payment eligibility could not be changed due to a PDO error.
"; } catch (Exception $e) { - echo "
The offer with id {$id} could not be found.
"; + $alert = "
The offer with id {$id} could not be found.
"; } - - echo "
"; } // Generate invoice if (isset($_GET['generate_invoice'])) { - echo "
"; $id = (int) $_GET['generate_invoice']; try { $offer = new Offer($_pdo, $id); $file = $offer->generateInvoice(); - echo "
The invoice for offer #{$offer->id} is generated: {$file->filename}
"; + $alert = "
The invoice for offer #{$offer->id} is generated: {$file->filename}
"; } catch (PDOException $e) { - echo "
The invoice for offer #{$offer->id} could not be generated due to a PDO error.
"; + $alert = "
The invoice for offer #{$offer->id} could not be generated due to a PDO error.
"; } catch (Exception $e) { - echo "
The invoice for offer #{$id} could not be generated.
"; + $alert = "
The invoice for offer #{$id} could not be generated.
"; } - echo "
"; } // Trash invoice if (isset($_GET['trash_invoice'])) { - echo "
"; $id = (int) $_GET['trash_invoice']; try { $offer = new Offer($_pdo, $id); $file = $offer->getInvoiceFile(); if ($file instanceof file && $file->delete()) { - echo "
The invoice for offer #{$offer->id} is trashed.
"; + $alert = "
The invoice for offer #{$offer->id} is trashed.
"; } else { - echo "
The invoice for offer #{$id} could not be trashed.
"; + $alert = "
The invoice for offer #{$id} could not be trashed.
"; } } catch (PDOException $e) { - echo "
The invoice for offer #{$offer->id} could not be trashed due to a PDO error.
"; + $alert = "
The invoice for offer #{$offer->id} could not be trashed due to a PDO error.
"; } catch (Exception $e) { - echo "
The invoice for offer #{$id} could not be trashed.
"; + $alert = "
The invoice for offer #{$id} could not be trashed.
"; } - echo "
"; } // Send invoice if (isset($_GET['send_invoice'])) { - echo "
"; $id = (int) $_GET['send_invoice']; try { $offer = new Offer($_pdo, $id); if ($offer->send()) { - echo "
The invoice for offer {$id} has been sent to {$offer->getContact()->email}.
"; + $alert = "
The invoice for offer {$id} has been sent to {$offer->getContact()->email}.
"; } else { - echo "
The invoice could not be sent due to an unknown error.
"; + $alert = "
The invoice could not be sent due to an unknown error.
"; } } catch (PDOException $e) { - echo "
The invoice could not be sent due to a PDO error.
"; + $alert = "
The invoice could not be sent due to a PDO error.
"; } catch (Exception $e) { - echo "
The offer with id {$id} could not be found.
"; + $alert = "
The offer with id {$id} could not be found.
"; } - echo "
"; } // Delete offer if (isset($_GET['delete'])) { - echo "
"; $id = (int) $_GET['delete']; try { $offer = new Offer($_pdo, $id); if ($offer->delete()) { - echo "
The offer #{$offer->id} has been removed.
"; + $alert = "
The offer #{$offer->id} has been removed.
"; } else { - echo "
The offer #{$offer->id} could not be removed. Perhaps it's already removed?
"; + $alert = "
The offer #{$offer->id} could not be removed. Perhaps it's already removed?
"; } } catch (PDOException $e) { - echo "
The offer could not be removed due to a PDO error.
"; + $alert = "
The offer could not be removed due to a PDO error.
"; } catch (Exception $e) { - echo "
The offer with id {$id} could not be found.
"; + $alert = "
The offer with id {$id} could not be found.
"; } + } + + //------------------------------------------------------------------------------ + // The header of the page + //------------------------------------------------------------------------------ + $header = 'Offers'; + // Whether or not to show an individual offer in the end (false if not, or the id if yes) + $show_individual = false; - echo "
"; + // View offer + if (isset($_GET['id'])) { + $id = (int) $_GET['id']; + try { + $offer = new Offer($_pdo, $id); + $header = "Offers / #{$offer->id}"; + $show_individual = $id; + $accepted = $offer->accepted ? + ['Accepted', 'btn-success'] : + ['Not accepted', 'btn-default']; + $eligible = $offer->getPaymentEligibility() ? + ['Eligible for online payment', 'btn-success'] : + ['Not eligible for online payment', 'btn-default']; + $header .= " + {$accepted[0]} + {$eligible[0]} + Send invoice to contact + Delete + "; + } catch (PDOException $e) { + $alert = "
The offer with id $id could not be found.
"; + } catch (Exception $e) { + $alert = "
The offer with id $id could not be found.
"; + } } + // Show the header + echo "

$header

"; + if (isset($alert)) echo "
$alert
"; + if ($show_individual !== false) { $_id = $show_individual; require('offers-view.php'); -- cgit v1.2.3