Skip to content

Commit

Permalink
feat: create ElementType (#197)
Browse files Browse the repository at this point in the history
This pull request includes significant changes to the `app/src/app`
directory, focusing on the addition of new fields and methods to various
model classes and the creation of a new controller for managing element
types.

### New Controller:
*
[`app/src/app/Controllers/Admin/ElementTypeController.php`](diffhunk://#diff-b853c2d8221709f16b1628ada40365adee6d7d67e0e85a419e63566f096b2318R1-R81):
Added a new controller with methods for CRUD operations on element
types.

### Model Updates:
*
[`app/src/app/Models/BaseModel.php`](diffhunk://#diff-57d475838e6428c42c3b9d5c460bea147ffb64f834e6ff8e980d351bf00c5c77R13-R16):
Added `updated_at` and `deleted_at` fields and their respective getter
methods.
[[1]](diffhunk://#diff-57d475838e6428c42c3b9d5c460bea147ffb64f834e6ff8e980d351bf00c5c77R13-R16)
[[2]](diffhunk://#diff-57d475838e6428c42c3b9d5c460bea147ffb64f834e6ff8e980d351bf00c5c77R350-R359)
*
[`app/src/app/Models/ElementType.php`](diffhunk://#diff-9fe6da3941cdf9002b36799641e9d90e702f85f505b34c28ba6d57805b54298dR1-R34):
Created a new model for `ElementType` with fields and a method to map
data to the model.

### Data Mapping Enhancements:
* Updated the `mapDataToModel` method in multiple model files to include
`updated_at` and `deleted_at` fields:
  - `Contract.php`
  - `Element.php`
  - `Incidence.php`
  - `Machine.php`
  - `Photo.php`
  - `Point.php`
  - `PruningType.php`
  - `Role.php`
  - `Route.php`
  - `Sensor.php`
  - `SensorHistory.php`
  - `Task.php`
  - `TaskType.php`
  - `TreeType.php`
  - `User.php`
  - `WorkOrder.php`
  - `WorkReport.php`

### Code Simplification:
*
[`app/src/app/Models/BaseModel.php`](diffhunk://#diff-57d475838e6428c42c3b9d5c460bea147ffb64f834e6ff8e980d351bf00c5c77L24-R28):
Simplified the `bulkInsert` method by combining nested loops into a
single line.
  • Loading branch information
albert1413 authored Dec 11, 2024
2 parents 0f197fa + d4eb8bb commit ddab5b7
Show file tree
Hide file tree
Showing 26 changed files with 391 additions and 44 deletions.
81 changes: 81 additions & 0 deletions app/src/app/Controllers/Admin/ElementTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Controllers\Admin;

use App\Core\Session;
use App\Core\View;
//use App\Models\Element;
use App\Models\ElementType;

class ElementTypeController
{
public function index($queryParams)
{
$elementTypes = ElementType::findAll();
View::render([
'view' => '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');
}
}
43 changes: 28 additions & 15 deletions app/src/app/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -75,7 +78,7 @@ public function belongsToMany(

$results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]);

if (! is_array($results))
if (!is_array($results))
$results = [];

// Process results
Expand All @@ -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);
}

Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 [];
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
}
}
2 changes: 2 additions & 0 deletions app/src/app/Models/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
34 changes: 34 additions & 0 deletions app/src/app/Models/ElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Models;

class ElementType extends BaseModel
{
public string $name;

public string $description;

protected static function getTableName(): string
{
return 'element_types';
}

protected static function mapDataToModel($data): ElementType
{
$element_type = new self();
$element_type->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');
//}

}
2 changes: 2 additions & 0 deletions app/src/app/Models/Incidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/PruningType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/app/Models/Sensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/SensorHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/app/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 8 additions & 6 deletions app/src/app/Models/TaskType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ddab5b7

Please sign in to comment.