| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- Core::addScript("public/js/app-project-form.js");
- $page_title = "Add Project";
- $customer = Customer::getForAdmin();
- if(empty($customer)) throw new Exception("Customer not found.");
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/projects", "Projects");
- Core::addBreadcrumb("/account/projects?customer_id=$customer->id", "$customer->name Projects");
- Core::addBreadcrumb("/account/projects/add", $page_title);
- // Check templates
- $templates = $customer->getTemplates();
- if(empty($templates)) throw new Exception("A template must exist for $customer->name before a project can be created.");
- $project = new Project;
- $project->dts_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1));
- $project->dts_deadline = date("Y-m-d H:i:s", strtotime("last day of " . date("M")));
- $project->customer_id = $customer->id;
- $project->default_tester_fee = 10;
- $location_ids = http::value("location_ids");
- $location_ids = explode(", ", $location_ids);
- $location_ids = array_filter($location_ids);
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- $project->template_id = http::value("template_id", "int");
- $project->name = http::value("name", "trim");
- $project->test_count = http::value("test_count", "int");
- $project->min_age = http::value("min_age", "int");
- $project->max_age = http::value("max_age", "int");
- $project->default_tester_fee = http::value("default_tester_fee", "float");
- $date_start = http::getDateFromFields("date_start");
- $date_deadline = http::getDateFromFields("date_deadline");
- $form_exception = new FormException;
- if(empty($project->template_id)) $form_exception->addError("Template is required.");
- else {
- $template = Template::get($project->template_id);
- if($template->customer_id != $customer->id) $form_exception->addError("Invalid template selected.");
- }
- if(empty($project->name)) $form_exception->addError("Name is required.");
- if(empty($project->test_count)) $form_exception->addError("Test count is required.");
- if(empty($project->min_age)) $form_exception->addError("Minimum age is required.");
- if(empty($project->max_age)) $form_exception->addError("Maximum age is required.");
- if(empty($project->default_tester_fee)) $form_exception->addError("Default tester fee is required.");
- if(empty($date_start)) $form_exception->addError("Start Date is required.");
- if(empty($date_deadline)) $form_exception->addError("Deadline is required.");
- if($project->min_age > $project->max_age) $form_exception->addError("Minimum age cannot be greater than maximum age.");
- $unix_start = strtotime($date_start . " 00:00:00");
- $unix_deadline = strtotime($date_deadline . " 23:59:59");
- $project->dts_start = Util::getDateTime($unix_start);
- $project->dts_deadline = Util::getDateTime($unix_deadline);
- $project->dts_created = Util::getDateTime();
- if($unix_start > $unix_deadline) $form_exception->addError("Start Date cannot be after the Deadline.");
- // Verify location IDs
- Location::load($location_ids);
- foreach($location_ids as $location_id) {
- $location = Location::get($location_id);
- if(!$location) $form_exception->addError("An invalid location was picked.");
- if($location->customer_id != $customer->id) $form_exception->addError("An invalid location was picked.");
- }
- if($form_exception->hasErrors()) throw $form_exception;
- $success = $project->commit();
- if(!$success) throw new Exception("Project could not be added at this time. Please try again later.");
- // Create test for location IDs
- foreach($location_ids as $location_id) {
- $test = new Test;
- $test->project_id = $project->id;
- $test->location_id = $location_id;
- $test->status = Test::STATUS_UNASSIGNED;
- $test->dts_created = Util::getDateTime();
- $success = $test->commit();
- if(!$success) throw new Exception("Test cannot be created, please try again later.");
- }
- Core::addAlert("success", "Project added successfully.");
- HTTP::redirect("/account/projects/$project->id");
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- $locations = $customer->getLocations(["closed" => 0]);
- View::includeOnce("header", compact("page_title"));
- View::include("project_form", compact("project", "templates", "locations", "location_ids"));
- View::includeOnce("footer");
- ?>
|