Просмотр исходного кода

Add ability to set CC email addresses for a customer

Alan Lam 2 лет назад
Родитель
Сommit
23b26aa16a

+ 14 - 2
controllers/customer_add.php

@@ -2,11 +2,13 @@
 
 $page_title = "Add Customer";
 
+Core::addScript("public/js/app-customer-form.js");
+
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/customers", "Customers");
 Core::addBreadcrumb("/account/customers/add", $page_title);
 
-$customer = new Customer;
+$customer = new Customer();
 $customer->country = DEFAULT_COUNTRY;
 $customer->show_in_map_overview = 1;
 $customer->email_after_test = 1;
@@ -17,7 +19,7 @@ if($submit) {
 	try {
 		Core::validateCSRFToken($submit);
 
-		$form_exception = new FormException;
+		$form_exception = new FormException();
 
 		$customer->name = http::value("name", "trim");
 		$customer->address_1 = http::value("address_1", "trim");
@@ -33,6 +35,16 @@ if($submit) {
 		$customer->year_cycle_month = http::value("year_cycle_month", "int");
 		$customer->show_in_map_overview = (int) http::value("show_in_map_overview", "int");
 
+		$cc_email_addresses = HTTP::value("cc_email_addresses", "trim");
+		if($cc_email_addresses) {
+			try {
+				$customer->setCcEmailAddresses($cc_email_addresses);
+			}
+			catch(Exception $e) {
+				$form_exception->addError($e->getMessage());
+			}
+		}
+
 		if(empty($customer->name)) $form_exception->addError("Name is required.");
 
 		if($form_exception->hasErrors()) throw $form_exception;

+ 13 - 1
controllers/customer_edit.php

@@ -5,6 +5,8 @@ if(!$customer) throw new Exception("Customer not found.");
 
 $page_title = "Edit Customer";
 
+Core::addScript("public/js/app-customer-form.js");
+
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/customers", "Customers");
 Core::addBreadcrumb("/account/customers/$id", $customer->name);
@@ -15,7 +17,7 @@ if($submit) {
 	try {
 		Core::validateCSRFToken($submit);
 
-		$form_exception = new FormException;
+		$form_exception = new FormException();
 
 		$customer->name = http::value("name", "trim");
 		$customer->address_1 = http::value("address_1", "trim");
@@ -33,6 +35,16 @@ if($submit) {
 		$customer->show_in_map_overview = (int) http::value("show_in_map_overview", "int");
 		$customer->config_json = http::value("config_json", "trim");
 
+		$cc_email_addresses = HTTP::value("cc_email_addresses", "trim");
+		if($cc_email_addresses) {
+			try {
+				$customer->setCcEmailAddresses($cc_email_addresses);
+			}
+			catch(Exception $e) {
+				$form_exception->addError($e->getMessage());
+			}
+		}
+
 		if(empty($customer->name)) $form_exception->addError("Name is required.");
 
 		if($form_exception->hasErrors()) throw $form_exception;

+ 4 - 0
controllers/test_verify_form.php

@@ -203,6 +203,10 @@ if($submit) {
 
 				if($email_customer && $customer->email_after_test && $customer->email) {
 					$email_addresses[] = $customer->email;
+
+					foreach($customer->getCcEmailAddresses() as $cc_email_address) {
+						$email_addresses[] = $cc_email_address;
+					}
 				}
 
 				if($email_location && $customer->email_locations_after_test && $location->email) {

+ 11 - 0
db/migrations/20240603111628_add_cc_email_json_field_to_customer_table.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddCcEmailJsonFieldToCustomerTable extends AbstractMigration {
+	public function change() {
+		$this->table("customer")
+			->addColumn("cc_email_json", "text", ["null" => true, "after" => "email"])
+			->update();
+	}
+}

+ 18 - 1
engine/customer.php

@@ -12,6 +12,7 @@ use Carbon\CarbonImmutable;
  * @property string $postal_code
  * @property string $country
  * @property string $email
+ * @property string $cc_email_json
  * @property string $phone
  * @property string $invoice_contact_name
  * @property bool $email_after_test
@@ -29,7 +30,7 @@ class Customer extends Model {
 	protected static $table = "customer";
 	protected static $id_field = "id";
 	protected static $fillable_fields = ["name", "address_1", "address_2",
-		"city", "county", "postal_code", "country", "email", "phone",
+		"city", "county", "postal_code", "country", "email", "cc_email_json", "phone",
 		"invoice_contact_name",
 		"email_after_test", "email_locations_after_test",
 		"internal_id_field_name", "region_field_name", "area_manager_field_name",
@@ -298,4 +299,20 @@ class Customer extends Model {
 		return $this->getConfig()->seniorGamingManagerLabels[$index] ?? $default_labels[$index];
 	}
 
+	public function getCcEmailAddresses(): array {
+		$addresses = json_decode($this->cc_email_json);
+		return is_array($addresses) ? $addresses : [];
+	}
+
+	public function setCcEmailAddresses(array $addresses): void {
+		foreach($addresses as $address) {
+			$valid = filter_var($address, FILTER_VALIDATE_EMAIL);
+			if(!$valid) {
+				throw new RuntimeException(__("%s is not a valid email address.", $address));
+			}
+		}
+
+		$this->cc_email_json = json_encode($addresses);
+	}
+
 }

+ 1 - 1
engine/http.php

@@ -144,7 +144,7 @@ class HTTP {
 			}
 
 			case "email": {
-				return filter_var($value, FILTER_VALIDATE_EMAIL);
+				return is_array($value) ? filter_var_array($value, FILTER_VALIDATE_EMAIL) : filter_var($value, FILTER_VALIDATE_EMAIL);
 			}
 
 			default: {

+ 1 - 1
engine/test.php

@@ -404,7 +404,7 @@ class Test extends Model {
 		$content = $builder->build();
 
 		// Send
-		$mail = new Email;
+		$mail = new Email();
 		$mail->setHtmlContent($content);
 		$mail->setFrom(EMAIL_PDF_FROM_ADDRESS, EMAIL_FROM_NAME);
 		$mail->addAddress($to_address);

+ 13 - 0
public/js/app-customer-form.js

@@ -0,0 +1,13 @@
+class CustomerForm {
+	constructor() {
+		this.$ccEmailAddressesField = $("#cc-email-addresses");
+
+		this.$ccEmailAddressesField.select2({
+			placeholder: "Type email addresses",
+			tags: true,
+			tokenSeparators: [',', ' ', ';']
+		});
+	}
+}
+
+$(() => new CustomerForm());

+ 14 - 0
views/customer_form.php

@@ -60,6 +60,20 @@
 				<input class="form-control" type="email" name="email" id="email" placeholder="Enter email address" value="<?=htmlspecialchars($customer->email)?>" />
 			</div>
 		</div>
+		<div class="row form-group">
+			<label class="col-sm-4 col-form-label" for="cc-email-addresses">CC Email Addresses</label>
+			<div class="col-sm">
+				<select class="form-control" type="email" name="cc_email_addresses[]" id="cc-email-addresses" multiple>
+					<?php
+					foreach($customer->getCcEmailAddresses() as $cc_email_address) {
+						?>
+						<option value="<?=htmlspecialchars($cc_email_address)?>" selected><?=htmlspecialchars($cc_email_address)?></option>
+						<?php
+					}
+					?>
+				</select>
+			</div>
+		</div>
 		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="phone">Contact Number</label>
 			<div class="col-sm">