Răsfoiți Sursa

Add month and finished filters to projects page

Alan Lam 3 ani în urmă
părinte
comite
b32794b444
2 a modificat fișierele cu 95 adăugiri și 27 ștergeri
  1. 26 9
      controllers/projects.php
  2. 69 18
      views/projects.php

+ 26 - 9
controllers/projects.php

@@ -1,5 +1,7 @@
 <?php /** @noinspection PhpUnhandledExceptionInspection */
 
+use Carbon\CarbonImmutable;
+
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/projects", "Projects");
 
@@ -8,8 +10,7 @@ Core::addScript("public/js/app-projects.js");
 Core::$allow_all_customers = true;
 
 $customer_id = http::value("customer_id");
-if(Core::$user->isAdmin() && $customer_id == "ALL") {
-
+if(Core::$user->isAdmin() && $customer_id === "ALL") {
 	$page_title = "All Projects";
 	Core::addBreadcrumb("/account/projects?customer_id=ALL", $page_title);
 
@@ -18,7 +19,6 @@ if(Core::$user->isAdmin() && $customer_id == "ALL") {
 	$customer = new Customer;
 	$customer->id = "ALL";
 	$customer->name = "All";
-
 }
 else {
 	$customer = Customer::getForAdmin();
@@ -29,31 +29,48 @@ else {
 }
 
 $id = http::value("id", "int");
+if($id) {
+	$nvp["id"] = $id;
+}
+
 $name = http::value("name", "trim");
+if($name) {
+	$nvp["name"] = (new Constraint("CONTAINS"))->setValues($name);
+}
+
 $year = http::value("year", "int");
-$report_month = http::value("report_month", "db");
+$month = http::value("month", "int");
 
 if($year === false) {
 	$year = date("Y");
 }
 
-
-if($id) $nvp["id"] = $id;
-if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues($name);
-if($year) {
+if($year && $month) {
+	$dt_min = CarbonImmutable::now()->setYear($year)->setMonth($month)->startOfMonth();
+	$dt_max = $dt_min->endOfMonth();
+	$nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues((string) $dt_min, (string) $dt_max);
+}
+else if($year) {
 	$min_dts = "{$year}-01-01 00:00:00";
 	$max_dts = "{$year}-12-31 23:59:59";
 	$nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($min_dts, $max_dts);
 }
+
+$report_month = http::value("report_month", "db");
 if($report_month) {
 	$nvp["dts_report"] = (new Constraint("BEGINS_WITH"))->setValues($report_month);
 }
 
+$finished = http::value("finished", "int");
+if($finished !== null) {
+	$nvp["dts_finished"] = $finished ? Constraint::isNotNull() : null;
+}
+
 $projects = Project::query($nvp, ["sort" => "dts_start", "order" => "desc"]);
 $templates = Template::load(array_column($projects, "template_id"));
 
 $filter_count = count($nvp);
 
 View::includeOnce("header", compact("page_title"));
-View::include("projects", compact("customer", "filter_count", "projects", "year", "report_month"));
+View::include("projects", compact("customer", "filter_count", "projects", "year", "month", "report_month", "finished"));
 View::includeOnce("footer");

+ 69 - 18
views/projects.php

@@ -1,8 +1,43 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var Customer $customer
+ * @var Project[] $projects
+ * @var int $filter_count
+ * @var int $year
+ * @var int $month
+ * @var string $report_month
+ * @var int $finished
+ */
 
 $report_months = array_reverse(Project::getReportMonths());
 $user = Core::$user;
 
+$year_options = [];
+foreach(range(2018, date("Y")) as $i) {
+	$year_options[$i] = $i;
+}
+
+$month_options = [
+	1 => __("January"),
+	2 => __("February"),
+	3 => __("March"),
+	4 => __("April"),
+	5 => __("May"),
+	6 => __("June"),
+	7 => __("July"),
+	8 => __("August"),
+	9 => __("September"),
+	10 => __("October"),
+	11 => __("November"),
+	12 => __("December"),
+];
+
+$finished_options = [
+	0 => __("Unfinished Only"),
+	1 => __("Finished Only"),
+];
+
 ?>
 <div class="container mt-3 py-4">
 
@@ -14,8 +49,9 @@ $user = Core::$user;
 			</h2>
 		</div>
 		<div class="col text-right">
-			<button type="button" class="btn btn-outline-primary" data-toggle="collapse" data-target="#project-filters">
-				Toggle Filters
+			<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#project-filters">
+				<i class="fa fa-fw fa-filter"></i>
+				Filters
 				<?php
 				if($filter_count > 0) {
 					?>
@@ -24,13 +60,15 @@ $user = Core::$user;
 				}
 				?>
 			</button>
-			<a class="btn btn-outline-success" href="/account/projects/add?customer_id=<?=$customer->id?>">Add</a>
+			<a class="btn btn-success" href="/account/projects/add?customer_id=<?=$customer->id?>">
+				<i class="fa fa-fw fa-plus"></i>
+				Add
+			</a>
 		</div>
 	</div>
 
 	<?php
 	$filters_show = $filter_count > 0 ? "show" : "";
-	$year_options = array_reverse(range(2018, date("Y")));
 	?>
 
 	<div class="collapse <?=$filters_show?>" id="project-filters">
@@ -58,12 +96,20 @@ $user = Core::$user;
 								<select class="form-control" type="text" name="year" id="year">
 									<option value="0">Select Year</option>
 									<?php
-									foreach($year_options as $value) {
-										$selected = ($value == $year) ? "selected" : "";
-										?>
-										<option value="<?=$value?>" <?=$selected?>><?=$value?></option>
-										<?php
-									}
+									HtmlHelper::renderOptions($year_options, $year);
+									?>
+								</select>
+							</div>
+						</div>
+						<div class="row mb-1">
+							<label for="month" class="col-sm-4 col-form-label">
+								<?=htmlspecialchars(__("Month"))?>
+							</label>
+							<div class="col-sm">
+								<select class="form-control" id="month" name="month">
+									<option value=""><?=htmlspecialchars(__("Select Month"))?></option>
+									<?php
+									HtmlHelper::renderOptions($month_options, $month);
 									?>
 								</select>
 							</div>
@@ -74,17 +120,22 @@ $user = Core::$user;
                                 <select class="form-control" name="report_month" id="report-month">
                                     <option value=""><?=htmlspecialchars(__("Select Report Month"))?></option>
                                     <?php
-                                    foreach($report_months as $value => $display) {
-                                        $selected = ($value == $report_month) ? "selected" : "";
-                                        ?>
-                                        <option value="<?=$value?>" <?=$selected?>><?=$display?></option>
-                                        <?php
-                                    }
+									HtmlHelper::renderOptions($report_months, $report_month);
                                     ?>
                                 </select>
                             </div>
-
                         </div>
+						<div class="row mb-1">
+							<label class="col-sm-4 col-form-label" for="finished"><?=htmlspecialchars(__("Finished"))?></label>
+							<div class="col-sm">
+								<select class="form-control" type="text" name="finished" id="finished">
+									<option value="">Show all</option>
+									<?php
+									HtmlHelper::renderOptions($finished_options, $finished);
+									?>
+								</select>
+							</div>
+						</div>
 						<div class="row">
 							<div class="col-sm offset-sm-4">
 								<button class="btn btn-primary" type="submit">Submit</button>