Skip to content

Commit

Permalink
refactor: update Element and Zone models, remove ZonePredefined, and …
Browse files Browse the repository at this point in the history
…adjust relationships
  • Loading branch information
0x1026 committed Dec 12, 2024
1 parent 7c4073e commit 2740c40
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 198 deletions.
19 changes: 13 additions & 6 deletions app/src/app/Controllers/Admin/ElementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function create($queryParams)
$zones = Zone::findAll();
$types = TreeType::findAll();
$contracts = Contract::findAll();
$elementsTypes = ElementType::findAll();
$points = Point::findAll();
$element_types = ElementType::findAll();
View::render([
'view' => 'Admin/Element/Create',
'title' => 'Add Element',
Expand All @@ -39,8 +38,7 @@ public function create($queryParams)
'zones' => $zones,
'types' => $types,
'contracts' => $contracts,
'points'=> $points,
'elementsTypes'=> $elementsTypes,
'element_types' => $element_types,
],
]);
}
Expand All @@ -51,7 +49,6 @@ public function store($postData)
$element->element_type_id = $postData['element_type_id'];
$element->zone_id = $postData['zone_id'];
$element->contract_id = $postData['contract_id'];
$element->point_id = $postData['point_id'];
$element->tree_type_id = $postData['tree_type_id'];

$element->save();
Expand All @@ -64,11 +61,21 @@ public function store($postData)
public function edit($id, $queryParams)
{
$element = Element::find($id);
$zones = Zone::findAll();
$types = TreeType::findAll();
$contracts = Contract::findAll();
$element_types = ElementType::findAll();
View::render([
'view' => 'Admin/Element/Edit',
'title' => 'Edit Element',
'layout' => 'Admin/AdminLayout',
'data' => ['element' => $element],
'data' => [
'element' => $element,
'zones' => $zones,
'types' => $types,
'contracts' => $contracts,
'element_types' => $element_types,
],
]);
}

Expand Down
29 changes: 16 additions & 13 deletions app/src/app/Models/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class Element extends BaseModel
{
public int $element_type_id;

public string $contract_id;
public int $contract_id;

public int $zone_id;

public int $point_id;
public ?int $point_id;

public int $tree_type_id;
public ?int $tree_type_id;

protected static function getTableName(): string
{
Expand All @@ -35,26 +35,29 @@ protected static function mapDataToModel($data): Element
return $element;
}

public function elementType()
public function elementType(): ElementType
{
return $this->hasMany(ElementType::class, 'element_type_id', 'id');
return $this->belongsTo(ElementType::class, 'element_type_id');
}
public function contract()

public function contract(): Contract
{
return $this->hasMany(Contract::class, 'contract_id','id');
return $this->belongsTo(Contract::class, 'contract_id');
}
public function zone()

public function zone(): Zone
{
return $this->hasMany(Zone::class, 'zone_id', 'id');
return $this->belongsTo(Zone::class, 'zone_id');
}

public function point()
public function point(): ?Point
{
return $this->hasMany(Point::class, 'point_id', 'id');
return $this->point_id ? $this->belongsTo(Point::class, 'point_id') : null;
}

public function treeType()
public function treeType(): ?TreeType
{
return $this->hasMany(TreeType::class, 'tree_type_id', 'id');

return $this->tree_type_id ? $this->belongsTo(TreeType::class, 'tree_type_id') : null;
}
}
69 changes: 5 additions & 64 deletions app/src/app/Models/Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class Zone extends BaseModel
{
public ?int $point_id;
public ?ZonePredefined $predefined = null; // Declare predefined property
public array $elements = []; // Declare elements property
public int $contract_id;

public ?string $name;

protected static function getTableName(): string
{
Expand All @@ -19,77 +19,18 @@ protected static function mapDataToModel($data): Zone
{
$zone = new self();
$zone->id = $data['id'];
$zone->point_id = $data['point_id'];
$zone->contract_id = $data['contract_id'];
$zone->name = $data['name'];
$zone->created_at = $data['created_at'];
$zone->updated_at = $data['updated_at'];
$zone->deleted_at = $data['deleted_at'];

return $zone;
}

// Relationship to points
public function point(): ?Point
{
return $this->point_id
? $this->belongsTo(Point::class, 'point_id')
: null;
}

// Relationship to predefined zones
public function predefined(): ?ZonePredefined
{
return $this->hasOne(ZonePredefined::class, 'zone_id', 'id');
}

// Relationship to elements
public function elements(): array
{
return $this->hasMany(Element::class, 'zone_id', 'id');
}

// Fetch all predefined zones along with elements from all zones
public static function getPredefinedZonesWithElements(): array
{
$query = "
SELECT
zones.*,
zones_predefined.name AS predefined_name,
zones_predefined.photo_id AS predefined_photo_id,
elements.*
FROM zones
INNER JOIN zones_predefined ON zones.id = zones_predefined.zone_id
LEFT JOIN elements ON zones.id = elements.zone_id
";

$results = Database::prepareAndExecute($query);

// Group results by zone and map to models
$groupedZones = [];
foreach ($results as $row) {
// Map zone
if (!isset($groupedZones[$row['id']])) {
$zone = static::mapDataToModel($row);

// Attach predefined zone details
$zone->predefined = new ZonePredefined();
$zone->predefined->id = $row['id'];
$zone->predefined->zone_id = $row['zone_id'];
$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;
}

// Map elements
if (!empty($row['name'])) { // Element name indicates a valid element
$element = Element::mapDataToModel($row);
$groupedZones[$row['id']]->elements[] = $element;
}
}

return array_values($groupedZones);
}
}
36 changes: 0 additions & 36 deletions app/src/app/Models/ZonePredefined.php

This file was deleted.

44 changes: 24 additions & 20 deletions app/src/app/Views/Admin/Element/Create.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="mb-4 flex justify-end">
<a href="/elements"
<a href="/admin/elements"
class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg shadow focus:outline-none focus:ring focus:ring-green-500 flex items-center space-x-2">
<!-- Heroicon for return/back (chevron-left) -->
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
Expand All @@ -11,35 +11,39 @@ class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg

<div class="bg-white p-8 border border-gray-300 rounded-lg shadow-md">
<h2 class="text-2xl font-semibold text-gray-800 mb-6">Create Element</h2>
<form action="/element/store" method="POST" class="space-y-6">
<form action="/admin/element/store" method="POST" class="space-y-6">

<!-- Name -->
<!-- Element Type -->
<div>
<label for="name" class="block text-sm font-medium text-gray-700 mb-1">Name</label>
<input type="text" id="name" name="name"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
<label for="element_type_id" class="block text-sm font-medium text-gray-700 mb-1">Element Type</label>
<select id="element_type_id" name="element_type_id"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
<option value="" disabled selected>Select Element Type</option>
<?php foreach ($element_types as $element_type): ?>
<?php echo '<option value="' . $element_type->getId() . '">' . $element_type->name . '</option>'; ?>
<?php endforeach; ?>
</select>
</div>
<!-- Tree Type ID-->
<div>
<label for="tree_type_id" class="block text-sm font-medium text-gray-700 mb-1">TreeType</label>
<label for="tree_type_id" class="block text-sm font-medium text-gray-700 mb-1">Tree Type</label>
<select id="tree_type_id" name="tree_type_id"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500">
<option value="" disabled selected>Select Tree Type</option>
<?php foreach ($types as $type): ?>
<?php echo '<option value="' . $type->getId() . '">' . $type->species . '</option>'; ?>
<?php endforeach; ?>
</select>
</div>

<!-- Zone ID -->
<div>
<label for="zone_id" class="block text-sm font-medium text-gray-700 mb-1">Zone Id</label>
<label for="zone_id" class="block text-sm font-medium text-gray-700 mb-1">Zone Name</label>
<select id="zone_id" name="zone_id"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
<option value=""disabled selected>Select Zone</option>
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
<option value="" disabled selected>Select Zone</option>
<?php foreach ($zones as $zone): ?>
<?php echo '<option value="' . $zone->getId() . '">' . $zone->name . '</option>'; ?>
<?php endforeach; ?>
Expand All @@ -49,16 +53,16 @@ class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none foc
<div>
<label for="contract_id" class="block text-sm font-medium text-gray-700 mb-1">Contract</label>
<select id="contract_id" name="contract_id"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring focus:ring-blue-500 focus:border-blue-500"
required>
<option value="" disabled selected>Select Contract</option>
<?php foreach ($contracts as $contract): ?>
<?php echo '<option value="' . $contract->getId() . '">' . $contract->name . '</option>'; ?>
<?php endforeach; ?>
</select>
</div>

<!--Point ID
<!--Point ID
<<div>
<label for="point_id" class="block text-sm font-medium text-gray-700 mb-1">Point</label>
<select id="point_id" name="point_id"
Expand All @@ -74,9 +78,9 @@ class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none foc
<!-- Submit Button -->
<div class="flex items-center">
<button type="submit"
class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg focus:outline-none focus:ring focus:ring-blue-500">
class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-lg focus:outline-none focus:ring focus:ring-blue-500">
Create Element
</button>
</div>
</form>
</div>
</div>
Loading

0 comments on commit 2740c40

Please sign in to comment.