diff --git a/app/src/app/Controllers/Admin/ElementTypeController.php b/app/src/app/Controllers/Admin/ElementTypeController.php new file mode 100644 index 0000000..e4eae63 --- /dev/null +++ b/app/src/app/Controllers/Admin/ElementTypeController.php @@ -0,0 +1,81 @@ + 'Admin/ElementTypes', + 'title' => 'Manage Elements Types', + 'layout' => 'Admin/AdminLayout', + 'data' => ['elementTypes' => $elementTypes], + ]); + } + + public function create($queryParams) + { + //$elements = Element::findAll(); + View::render([ + 'view' => 'Admin/ElementType/Create', + 'title' => 'Add Element Type', + 'layout' => 'Admin/AdminLayout', + 'data' => [ + //'elements' => $elements, + ], + ]); + } + + public function store($postData) + { + $elementType = new ElementType(); + $elementType->name = $postData['name']; + $elementType->description = $postData['description']; + + $elementType->save(); + + Session::set('success', 'Type of Element created successfully'); + + header('Location: /admin/element-types'); + } + + public function edit($id, $queryParams) + { + $elementType = ElementType::find($id); + View::render([ + 'view' => 'Admin/ElementType/Edit', + 'title' => 'Edit Element Type', + 'layout' => 'Admin/AdminLayout', + 'data' => ['elementType' => $elementType], + ]); + } + + public function update($id, $postData) + { + $elementType = ElementType::find($id); + $elementType->name = $postData['name']; + $elementType->description = $postData['description']; + $elementType->save(); + + Session::set('success', 'Type of Element updated successfully'); + + header('Location: /admin/element-types'); + } + + public function destroy($id, $queryParams) + { + $elementType = ElementType::find($id); + $elementType->delete(); + + Session::set('success', 'Type of Element deleted successfully'); + + header('Location: /admin/element-types'); + } +} diff --git a/app/src/app/Models/BaseModel.php b/app/src/app/Models/BaseModel.php index cc58c91..8df0f65 100644 --- a/app/src/app/Models/BaseModel.php +++ b/app/src/app/Models/BaseModel.php @@ -10,6 +10,10 @@ abstract class BaseModel protected ?string $created_at; + protected ?string $updated_at; + + protected ?string $deleted_at; + // Insert multiple records into the table public static function bulkInsert(array $records): void { @@ -21,8 +25,7 @@ public static function bulkInsert(array $records): void $query .= implode(", ", array_fill(0, count($records), "(" . implode(", ", $placeholders) . ")")); $params = []; - foreach ($records as $index => $record) - foreach ($record as $key => $value) + foreach ($records as $index => $record) foreach ($record as $key => $value) $params["{$key}_{$index}"] = $value; Database::prepareAndExecute($query, $params); @@ -40,7 +43,7 @@ public function belongsTo(string $relatedModel, string $foreignKey, string $owne $query = "SELECT * FROM {$relatedTable} WHERE {$ownerKey} = :foreignKeyValue LIMIT 1"; $results = Database::prepareAndExecute($query, ['foreignKeyValue' => $foreignKeyValue]); - return ! empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; + return !empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; } // Many-to-Many relationship @@ -75,7 +78,7 @@ public function belongsToMany( $results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); - if (! is_array($results)) + if (!is_array($results)) $results = []; // Process results @@ -85,7 +88,7 @@ public function belongsToMany( if ($withPivot) { // Attach pivot data as a property $relatedInstance->pivot = array_filter($row, function ($key) use ($relatedModel) { - return ! property_exists($relatedModel, $key); + return !property_exists($relatedModel, $key); }, ARRAY_FILTER_USE_KEY); } @@ -99,7 +102,7 @@ public static function count(array $conditions = []): ?int $query = "SELECT COUNT(*) as count FROM " . static::getTableName(); $params = []; - if (! empty($conditions)) { + if (!empty($conditions)) { $query .= " WHERE "; $query .= implode(' AND ', array_map(function ($key) { return "$key = :$key"; @@ -156,7 +159,7 @@ public static function find(string $id): ?object $results = Database::prepareAndExecute($query, ['id' => $id]); - return ! empty($results) ? static::mapDataToModel($results[0]) : null; + return !empty($results) ? static::mapDataToModel($results[0]) : null; } // Fetch all records from the table @@ -206,7 +209,7 @@ public static function findBy(array $conditions, bool $single = false): array|ob $results = Database::prepareAndExecute($query, $parameters); if ($single) - return ! empty($results) ? static::mapDataToModel($results[0]) : null; + return !empty($results) ? static::mapDataToModel($results[0]) : null; if (empty($results)) return []; @@ -217,7 +220,7 @@ public static function findBy(array $conditions, bool $single = false): array|ob // Fetch all soft deleted records public static function findSoftDeleted(): array { - if (! static::hasSoftDelete()) + if (!static::hasSoftDelete()) return []; $table = static::getTableName(); @@ -236,7 +239,7 @@ public function hasOne(string $relatedModel, string $foreignKey, string $localKe $query = "SELECT * FROM $relatedTable WHERE $foreignKey = :localKeyValue LIMIT 1"; $results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); - return ! empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; + return !empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; } // One-to-Many relationship @@ -249,7 +252,7 @@ public function hasMany(string $relatedModel, string $foreignKey, string $localK $results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); // Ensure $results is an array - if (! is_array($results)) + if (!is_array($results)) $results = []; return array_map(fn($row) => $relatedModel::mapDataToModel($row), $results); @@ -261,10 +264,10 @@ protected static function hasSoftDelete(): bool static $softDeleteCache = []; $table = static::getTableName(); - if (! isset($softDeleteCache[$table])) { + if (!isset($softDeleteCache[$table])) { $query = "SHOW COLUMNS FROM {$table} LIKE 'deleted_at'"; $result = Database::prepareAndExecute($query); - $softDeleteCache[$table] = ! empty($result); // Cache the result + $softDeleteCache[$table] = !empty($result); // Cache the result } return $softDeleteCache[$table]; @@ -277,7 +280,7 @@ public static function paginate(int $page = 1, int $perPage = 10, array $conditi $query = "SELECT * FROM " . static::getTableName(); $params = []; - if (! empty($conditions)) { + if (!empty($conditions)) { $query .= " WHERE " . implode(' AND ', array_map(fn($key) => "{$key} = :{$key}", array_keys($conditions))); $params = $conditions; } @@ -326,7 +329,7 @@ public function save(): void Database::prepareAndExecute($query, $properties); - if (! isset($this->id)) + if (!isset($this->id)) $this->id = Database::connect()->lastInsertId(); } @@ -344,4 +347,14 @@ public function getCreatedAt(): ?string { return $this->created_at; } + + public function getUpdatedAt(): ?string + { + return $this->updated_at; + } + + public function getDeletedAt(): ?string + { + return $this->deleted_at; + } } diff --git a/app/src/app/Models/Contract.php b/app/src/app/Models/Contract.php index e89eb9d..600d6a7 100644 --- a/app/src/app/Models/Contract.php +++ b/app/src/app/Models/Contract.php @@ -32,6 +32,8 @@ protected static function mapDataToModel($data): Contract $contract->invoice_agreed = $data['invoice_agreed']; $contract->invoice_paid = $data['invoice_paid']; $contract->created_at = $data['created_at']; + $contract->updated_at = $data['updated_at']; + $contract->deleted_at = $data['deleted_at']; return $contract; } diff --git a/app/src/app/Models/Element.php b/app/src/app/Models/Element.php index 582c1dd..35c20d5 100644 --- a/app/src/app/Models/Element.php +++ b/app/src/app/Models/Element.php @@ -29,6 +29,8 @@ protected static function mapDataToModel($data): Element // $element->point_id = $data['point_id']; $element->tree_type_id = $data['tree_type_id']; $element->created_at = $data['created_at']; + $element->updated_at = $data['updated_at']; + $element->deleted_at = $data['deleted_at']; return $element; } diff --git a/app/src/app/Models/ElementType.php b/app/src/app/Models/ElementType.php new file mode 100644 index 0000000..4912cad --- /dev/null +++ b/app/src/app/Models/ElementType.php @@ -0,0 +1,34 @@ +id = $data['id']; + $element_type->name = $data['name']; + $element_type->description = $data['description']; + $element_type->created_at = $data['created_at']; + $element_type->updated_at = $data['updated_at']; + $element_type->deleted_at = $data['deleted_at']; + + return $element_type; + } + + //public function element(): Element + //{ + // return $this->belongsTo(Element::class, 'element_id'); + //} + +} diff --git a/app/src/app/Models/Incidence.php b/app/src/app/Models/Incidence.php index 6d36098..f33cc6b 100644 --- a/app/src/app/Models/Incidence.php +++ b/app/src/app/Models/Incidence.php @@ -26,6 +26,8 @@ public static function mapDataToModel($data): Incidence $incidence->description = $data['description']; $incidence->photo_id = $data['photo_id']; $incidence->created_at = $data['created_at']; + $incidence->updated_at = $data['updated_at']; + $incidence->deleted_at = $data['deleted_at']; return $incidence; } diff --git a/app/src/app/Models/Machine.php b/app/src/app/Models/Machine.php index 28a988e..fa801de 100644 --- a/app/src/app/Models/Machine.php +++ b/app/src/app/Models/Machine.php @@ -23,6 +23,8 @@ protected static function mapDataToModel($data): Machine $machine->max_basket_size = $data['max_basket_size']; $machine->photo_id = $data['photo_id']; $machine->created_at = $data['created_at']; + $machine->updated_at = $data['updated_at']; + $machine->deleted_at = $data['deleted_at']; return $machine; } diff --git a/app/src/app/Models/Photo.php b/app/src/app/Models/Photo.php index effccef..bc5dfa2 100644 --- a/app/src/app/Models/Photo.php +++ b/app/src/app/Models/Photo.php @@ -20,6 +20,8 @@ protected static function mapDataToModel($data): Photo $photo->name = $data['name']; $photo->path = $data['path']; $photo->created_at = $data['created_at']; + $photo->updated_at = $data['updated_at']; + $photo->deleted_at = $data['deleted_at']; return $photo; } diff --git a/app/src/app/Models/Point.php b/app/src/app/Models/Point.php index f434b47..843251b 100644 --- a/app/src/app/Models/Point.php +++ b/app/src/app/Models/Point.php @@ -20,6 +20,8 @@ protected static function mapDataToModel($data): Point $point->latitude = $data['latitude']; $point->longitude = $data['longitude']; $point->created_at = $data['created_at']; + $point->updated_at = $data['updated_at']; + $point->deleted_at = $data['deleted_at']; return $point; } diff --git a/app/src/app/Models/PruningType.php b/app/src/app/Models/PruningType.php index b53f963..a2a6866 100644 --- a/app/src/app/Models/PruningType.php +++ b/app/src/app/Models/PruningType.php @@ -23,6 +23,8 @@ protected static function mapDataToModel($data): PruningType $pruning_type->description = $data['description']; $pruning_type->photo_id = $data['photo_id']; $pruning_type->created_at = $data['created_at']; + $pruning_type->updated_at = $data['updated_at']; + $pruning_type->deleted_at = $data['deleted_at']; return $pruning_type; } diff --git a/app/src/app/Models/Role.php b/app/src/app/Models/Role.php index 016ece7..6841339 100644 --- a/app/src/app/Models/Role.php +++ b/app/src/app/Models/Role.php @@ -17,6 +17,8 @@ protected static function mapDataToModel($data): Role $role->id = $data['id']; $role->name = $data['name']; $role->created_at = $data['created_at']; + $role->updated_at = $data['updated_at']; + $role->deleted_at = $data['deleted_at']; return $role; } diff --git a/app/src/app/Models/Route.php b/app/src/app/Models/Route.php index 386e74e..b958350 100644 --- a/app/src/app/Models/Route.php +++ b/app/src/app/Models/Route.php @@ -20,6 +20,8 @@ protected static function mapDataToModel($data): Route $route->distance = $data['distance']; $route->travel_time = $data['travel_time']; $route->created_at = $data['created_at']; + $route->updated_at = $data['updated_at']; + $route->deleted_at = $data['deleted_at']; return $route; } diff --git a/app/src/app/Models/Sensor.php b/app/src/app/Models/Sensor.php index 8929281..dc322a5 100644 --- a/app/src/app/Models/Sensor.php +++ b/app/src/app/Models/Sensor.php @@ -21,7 +21,9 @@ protected static function mapDataToModel($data): Sensor $sensor->model = $data["model"]; $sensor->class = $data["class"]; $sensor->is_active = $data["is_active"]; - $sensor->created_at = $data["created_at"]; + $sensor->created_at = $data['created_at']; + $sensor->updated_at = $data['updated_at']; + $sensor->deleted_at = $data['deleted_at']; return $sensor; } diff --git a/app/src/app/Models/SensorHistory.php b/app/src/app/Models/SensorHistory.php index 0b1e1d4..03ca55e 100644 --- a/app/src/app/Models/SensorHistory.php +++ b/app/src/app/Models/SensorHistory.php @@ -22,6 +22,8 @@ protected static function mapDataToModel($data): SensorHistory $sensor_history->humidity = $data['humidity']; $sensor_history->inclination = $data['inclination']; $sensor_history->created_at = $data['created_at']; + $sensor_history->updated_at = $data['updated_at']; + $sensor_history->deleted_at = $data['deleted_at']; return $sensor_history; } diff --git a/app/src/app/Models/Task.php b/app/src/app/Models/Task.php index d3bf9f5..d458d80 100644 --- a/app/src/app/Models/Task.php +++ b/app/src/app/Models/Task.php @@ -26,6 +26,8 @@ protected static function mapDataToModel($data): Task $task->notes = $data['notes']; $task->route_id = $data['route_id']; $task->created_at = $data['created_at']; + $task->updated_at = $data['updated_at']; + $task->deleted_at = $data['deleted_at']; return $task; } diff --git a/app/src/app/Models/TaskType.php b/app/src/app/Models/TaskType.php index 3b3d172..b4b1589 100644 --- a/app/src/app/Models/TaskType.php +++ b/app/src/app/Models/TaskType.php @@ -15,13 +15,15 @@ protected static function getTableName(): string protected static function mapDataToModel($data): TaskType { - $task = new self(); - $task->id = $data['id']; - $task->name = $data['name']; - $task->photo_id = $data['photo_id']; - $task->created_at = $data['created_at']; + $task_type = new self(); + $task_type->id = $data['id']; + $task_type->name = $data['name']; + $task_type->photo_id = $data['photo_id']; + $task_type->created_at = $data['created_at']; + $task_type->updated_at = $data['updated_at']; + $task_type->deleted_at = $data['deleted_at']; - return $task; + return $task_type; } public function photo(): ?Photo diff --git a/app/src/app/Models/TreeType.php b/app/src/app/Models/TreeType.php index da15340..c27b014 100644 --- a/app/src/app/Models/TreeType.php +++ b/app/src/app/Models/TreeType.php @@ -22,6 +22,9 @@ protected static function mapDataToModel($data): TreeType $tree_type->family = $data['family']; $tree_type->genus = $data['genus']; $tree_type->species = $data['species']; + $tree_type->created_at = $data['created_at']; + $tree_type->updated_at = $data['updated_at']; + $tree_type->deleted_at = $data['deleted_at']; return $tree_type; } diff --git a/app/src/app/Models/User.php b/app/src/app/Models/User.php index 5f36594..f6b11e2 100644 --- a/app/src/app/Models/User.php +++ b/app/src/app/Models/User.php @@ -43,6 +43,8 @@ protected static function mapDataToModel($data): User $user->role = $data['role']; $user->photo_id = $data['photo_id']; $user->created_at = $data['created_at']; + $user->updated_at = $data['updated_at']; + $user->deleted_at = $data['deleted_at']; return $user; } diff --git a/app/src/app/Models/WorkOrder.php b/app/src/app/Models/WorkOrder.php index abf14a1..ef86b4b 100644 --- a/app/src/app/Models/WorkOrder.php +++ b/app/src/app/Models/WorkOrder.php @@ -13,12 +13,14 @@ protected static function getTableName(): string protected static function mapDataToModel($data): WorkOrder { - $order = new self(); - $order->id = $data['id']; - $order->contract_id = $data['contract_id']; - $order->created_at = $data['created_at']; - - return $order; + $work_order = new self(); + $work_order->id = $data['id']; + $work_order->contract_id = $data['contract_id']; + $work_order->created_at = $data['created_at']; + $work_order->updated_at = $data['updated_at']; + $work_order->deleted_at = $data['deleted_at']; + + return $work_order; } public function report(): WorkReport diff --git a/app/src/app/Models/WorkReport.php b/app/src/app/Models/WorkReport.php index 9e105ed..5f349ae 100644 --- a/app/src/app/Models/WorkReport.php +++ b/app/src/app/Models/WorkReport.php @@ -17,14 +17,16 @@ protected static function getTableName(): string protected static function mapDataToModel($data): WorkReport { - $workReport = new self(); - $workReport->id = $data['id']; - $workReport->work_order_id = $data['work_order_id']; - $workReport->observation = $data['observation']; - $workReport->spent_fuel = $data['spent_fuel']; - $workReport->created_at = $data['created_at']; - - return $workReport; + $work_report = new self(); + $work_report->id = $data['id']; + $work_report->work_order_id = $data['work_order_id']; + $work_report->observation = $data['observation']; + $work_report->spent_fuel = $data['spent_fuel']; + $work_report->created_at = $data['created_at']; + $work_report->updated_at = $data['updated_at']; + $work_report->deleted_at = $data['deleted_at']; + + return $work_report; } public function workOrder(): WorkOrder diff --git a/app/src/app/Models/Zone.php b/app/src/app/Models/Zone.php index f528b11..53397b4 100644 --- a/app/src/app/Models/Zone.php +++ b/app/src/app/Models/Zone.php @@ -21,6 +21,8 @@ protected static function mapDataToModel($data): Zone $zone->id = $data['id']; $zone->point_id = $data['point_id']; $zone->created_at = $data['created_at']; + $zone->updated_at = $data['updated_at']; + $zone->deleted_at = $data['deleted_at']; return $zone; } @@ -75,6 +77,8 @@ public static function getPredefinedZonesWithElements(): array $zone->predefined->name = $row['predefined_name']; $zone->predefined->photo_id = $row['predefined_photo_id']; $zone->predefined->created_at = $row['created_at']; + $zone->predefined->updated_at = $row['updated_at']; + $zone->predefined->deleted_at = $row['deleted_at']; $groupedZones[$row['id']] = $zone; } diff --git a/app/src/app/Models/ZonePredefined.php b/app/src/app/Models/ZonePredefined.php index 3ae4b5e..e67bd4a 100644 --- a/app/src/app/Models/ZonePredefined.php +++ b/app/src/app/Models/ZonePredefined.php @@ -17,14 +17,16 @@ protected static function getTableName(): string protected static function mapDataToModel($data): ZonePredefined { - $predefined = new self(); - $predefined->id = $data['id']; - $predefined->zone_id = $data['zone_id']; - $predefined->name = $data['name']; - $predefined->photo_id = $data['photo_id']; - $predefined->created_at = $data['created_at']; - - return $predefined; + $zone_predefined = new self(); + $zone_predefined->id = $data['id']; + $zone_predefined->zone_id = $data['zone_id']; + $zone_predefined->name = $data['name']; + $zone_predefined->photo_id = $data['photo_id']; + $zone_predefined->created_at = $data['created_at']; + $zone_predefined->updated_at = $data['updated_at']; + $zone_predefined->deleted_at = $data['deleted_at']; + + return $zone_predefined; } public function photo(): ?Photo diff --git a/app/src/app/Views/Admin/ElementType/Create.php b/app/src/app/Views/Admin/ElementType/Create.php new file mode 100644 index 0000000..b339848 --- /dev/null +++ b/app/src/app/Views/Admin/ElementType/Create.php @@ -0,0 +1,40 @@ +
+ + + + + + Return to Element-Types + +
+ +
+

Create Element Type

+
+ + +
+ + +
+ + +
+ + +
+ + +
+ +
+
+
diff --git a/app/src/app/Views/Admin/ElementType/Edit.php b/app/src/app/Views/Admin/ElementType/Edit.php new file mode 100644 index 0000000..111f7f9 --- /dev/null +++ b/app/src/app/Views/Admin/ElementType/Edit.php @@ -0,0 +1,43 @@ +
+ + + + + + Return to Elements + +
+ +
+

Edit Element Type

+
+ + +
+ + +
+ + +
+ + +
+ + + +
+ +
+
+
diff --git a/app/src/app/Views/Admin/ElementTypes.php b/app/src/app/Views/Admin/ElementTypes.php new file mode 100644 index 0000000..a9d94c3 --- /dev/null +++ b/app/src/app/Views/Admin/ElementTypes.php @@ -0,0 +1,62 @@ + + + + + + +
+ + Create Element Type + +
+ +
+ + + + + + + + + + + + + + + + + + + + +
IDNameDescriptionActions
+ getId(); ?> + name; ?> + description; ?> + + + + + + + + + + + + +
+
diff --git a/app/src/routes/admin.php b/app/src/routes/admin.php index 81f96ff..29fe0b6 100644 --- a/app/src/routes/admin.php +++ b/app/src/routes/admin.php @@ -3,6 +3,7 @@ use App\Controllers\Admin\ContractController; use App\Controllers\Admin\DashboardController; use App\Controllers\Admin\ElementController; +use App\Controllers\Admin\ElementTypeController; use App\Controllers\Admin\IncidenceController; use App\Controllers\Admin\TaskTypeController; use App\Controllers\Admin\TreeTypeController; @@ -137,6 +138,27 @@ 'method' => 'destroy', 'middlewares' => [AdminMiddleware::class], ], + // ElementType + '/admin/element-types' => [ + 'controller' => ElementTypeController::class, + 'method' => 'index', + 'middlewares' => [AdminMiddleware::class], + ], + '/admin/element-type/create' => [ + 'controller' => ElementTypeController::class, + 'method' => 'create', + 'middlewares' => [AdminMiddleware::class], + ], + '/admin/element-type/:id/edit' => [ + 'controller' => ElementTypeController::class, + 'method' => 'edit', + 'middlewares' => [AdminMiddleware::class], + ], + '/admin/element-type/:id/delete' => [ + 'controller' => ElementTypeController::class, + 'method' => 'destroy', + 'middlewares' => [AdminMiddleware::class], + ], // === TaskTypes GET Routes '/admin/task-types' => [ 'controller' => TaskTypeController::class, @@ -202,6 +224,17 @@ 'method' => 'update', 'middlewares' => [AdminMiddleware::class], ], + // === Element-Types POST Routes + '/admin/element-type/store' => [ + 'controller' => ElementTypeController::class, + 'method' => 'store', + 'middlewares' => [AdminMiddleware::class], + ], + '/admin/element-type/:id/update' => [ + 'controller' => ElementTypeController::class, + 'method' => 'update', + 'middlewares' => [AdminMiddleware::class], + ], // === WorkOrders POST Routes '/admin/work-order/store' => [ 'controller' => WorkOrderController::class,