Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create ElementType #197

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Check warning on line 12 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L12

Added line #L12 was not covered by tests
{
$elementTypes = ElementType::findAll();
View::render([
'view' => 'Admin/ElementTypes',
'title' => 'Manage Elements Types',
'layout' => 'Admin/AdminLayout',
'data' => ['elementTypes' => $elementTypes],
]);

Check warning on line 20 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L14-L20

Added lines #L14 - L20 were not covered by tests
}

public function create($queryParams)

Check warning on line 23 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L23

Added line #L23 was not covered by tests
{
//$elements = Element::findAll();
View::render([
'view' => 'Admin/ElementType/Create',
'title' => 'Add Element Type',
'layout' => 'Admin/AdminLayout',
'data' => [
//'elements' => $elements,
],
]);

Check warning on line 33 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L26-L33

Added lines #L26 - L33 were not covered by tests
}

public function store($postData)

Check warning on line 36 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L36

Added line #L36 was not covered by tests
{
$elementType = new ElementType();
$elementType->name = $postData['name'];
$elementType->description = $postData['description'];

Check warning on line 40 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L38-L40

Added lines #L38 - L40 were not covered by tests

$elementType->save();

Check warning on line 42 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L42

Added line #L42 was not covered by tests

Session::set('success', 'Element-Type created successfully');

Check warning on line 44 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L44

Added line #L44 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved

header('Location: /admin/elementTypes');

Check warning on line 46 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L46

Added line #L46 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
}

public function edit($id, $queryParams)

Check warning on line 49 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L49

Added line #L49 was not covered by tests
{
$elementType = ElementType::find($id);
View::render([
'view' => 'Admin/ElementType/Edit',
'title' => 'Edit Element Type',
'layout' => 'Admin/AdminLayout',
'data' => ['elementType' => $elementType],
]);

Check warning on line 57 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L51-L57

Added lines #L51 - L57 were not covered by tests
}

public function update($id, $postData)

Check warning on line 60 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L60

Added line #L60 was not covered by tests
{
$elementType = ElementType::find($id);
$elementType->name = $postData['name'];
$elementType->description = $postData['description'];
$elementType->save();

Check warning on line 65 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L62-L65

Added lines #L62 - L65 were not covered by tests

Session::set('success', 'Element-Type updated successfully');

Check warning on line 67 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L67

Added line #L67 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved

header('Location: /admin/elementTypes');

Check warning on line 69 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L69

Added line #L69 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
}

public function destroy($id, $queryParams)

Check warning on line 72 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L72

Added line #L72 was not covered by tests
{
$elementType = ElementType::find($id);
$elementType->delete();

Check warning on line 75 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L74-L75

Added lines #L74 - L75 were not covered by tests

Session::set('success', 'Element-Type deleted successfully');

Check warning on line 77 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L77

Added line #L77 was not covered by tests

header('Location: /admin/elementTypes');

Check warning on line 79 in app/src/app/Controllers/Admin/ElementTypeController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/ElementTypeController.php#L79

Added line #L79 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
}
}
32 changes: 32 additions & 0 deletions app/src/app/Models/ElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

class ElementType extends BaseModel
{
public string $name;

public string $description;

protected static function getTableName(): string

Check warning on line 11 in app/src/app/Models/ElementType.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Models/ElementType.php#L11

Added line #L11 was not covered by tests
{
return 'element_types';

Check warning on line 13 in app/src/app/Models/ElementType.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Models/ElementType.php#L13

Added line #L13 was not covered by tests
}

protected static function mapDataToModel($data): ElementType

Check warning on line 16 in app/src/app/Models/ElementType.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Models/ElementType.php#L16

Added line #L16 was not covered by tests
{
$elementType = new self();
$elementType->id = $data['id'];
$elementType->name = $data['name'];
$elementType->description = $data['description'];
$elementType->created_at = $data['created_at'];

Check warning on line 22 in app/src/app/Models/ElementType.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Models/ElementType.php#L18-L22

Added lines #L18 - L22 were not covered by tests

return $elementType;

Check warning on line 24 in app/src/app/Models/ElementType.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Models/ElementType.php#L24

Added line #L24 was not covered by tests
}

//public function element(): Element
//{
// return $this->belongsTo(Element::class, 'element_id');
//}

}
40 changes: 40 additions & 0 deletions app/src/app/Views/Admin/ElementType/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<div class="mb-4 flex justify-end">
<a href="/admin/elementTypes"
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
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">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
<span>Return to Element-Types</span>
</a>
</div>

Check warning on line 10 in app/src/app/Views/Admin/ElementType/Create.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Create.php#L1-L10

Added lines #L1 - L10 were not covered by tests

<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 Type</h2>
<form action="/admin/elementType/store" method="POST" class="space-y-6">

Check warning on line 14 in app/src/app/Views/Admin/ElementType/Create.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Create.php#L12-L14

Added lines #L12 - L14 were not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved

<!-- Name -->
<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>
</div>

Check warning on line 22 in app/src/app/Views/Admin/ElementType/Create.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Create.php#L16-L22

Added lines #L16 - L22 were not covered by tests

<!-- Description -->
<div>
<label for="description" class="block text-sm font-medium text-gray-700 mb-1">Description</label>
<input type="text" id="description" name="description"
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>
</div>

Check warning on line 30 in app/src/app/Views/Admin/ElementType/Create.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Create.php#L24-L30

Added lines #L24 - L30 were not covered by tests

<!-- 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">
Create Element-Type
</button>
</div>
</form>
</div>

Check warning on line 40 in app/src/app/Views/Admin/ElementType/Create.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Create.php#L32-L40

Added lines #L32 - L40 were not covered by tests
43 changes: 43 additions & 0 deletions app/src/app/Views/Admin/ElementType/Edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<div class="mb-4 flex justify-end">
<a href="/admin/elementTypes"
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
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">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
<span>Return to Elements</span>
</a>
</div>

Check warning on line 10 in app/src/app/Views/Admin/ElementType/Edit.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Edit.php#L1-L10

Added lines #L1 - L10 were not covered by tests

<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">Edit Element Type</h2>
<form
action="/admin/elementType/<?php echo htmlspecialchars($elementType->getId()); ?>/update"

Check warning on line 15 in app/src/app/Views/Admin/ElementType/Edit.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Edit.php#L12-L15

Added lines #L12 - L15 were not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
method="POST" class="space-y-6">

<!-- Name -->
<div>
<label for="name" class="block text-sm font-medium text-gray-700 mb-1">Name</label>
<input type="text" id="name" name="name"
value="<?php echo htmlspecialchars($elementType->name); ?>"

Check warning on line 22 in app/src/app/Views/Admin/ElementType/Edit.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Edit.php#L22

Added line #L22 was not covered by tests
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">
</div>

<!-- Description -->
<div>
<label for="description" class="block text-sm font-medium text-gray-700 mb-1">Description</label>
<input type="description" id="description" name="description"
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
value="<?php echo htmlspecialchars($elementType->description); ?>"

Check warning on line 30 in app/src/app/Views/Admin/ElementType/Edit.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementType/Edit.php#L30

Added line #L30 was not covered by tests
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">
</div>


<!-- 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">
Update User
</button>
</div>
</form>
</div>
62 changes: 62 additions & 0 deletions app/src/app/Views/Admin/ElementTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

use App\Core\Session;

?>

<?php if (Session::has('success')) { ?>
<div id="alert-msg" class="bg-blue-500 text-white px-4 py-3 rounded-lg mb-6" role="alert">
<strong class="font-bold">Success: </strong>
<span><?php echo htmlspecialchars(Session::get('success')); ?></span>

Check warning on line 10 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L7-L10

Added lines #L7 - L10 were not covered by tests
</div>
<?php } ?>

<div class="mb-4 flex justify-end">
<a href="/admin/elementType/create"
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
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">
Create Element Type
</a>
</div>

Check warning on line 19 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L14-L19

Added lines #L14 - L19 were not covered by tests

<div class="overflow-x-auto">
<table class="min-w-full table-fixed bg-white border border-gray-300 rounded-lg shadow-md">
<thead>
<tr class="bg-gray-700 text-white text-left h-14">
<th class="px-4 py-2 border-b">ID</th>
<th class="px-4 py-2 border-b">Name</th>
<th class="px-4 py-2 border-b">Description</th>

Check warning on line 27 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L21-L27

Added lines #L21 - L27 were not covered by tests

<th class="px-4 py-2 border-b">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($elementTypes as $elementType) { ?>
<tr class="hover:bg-gray-50">
<td class="px-4 py-2 border-b">
<?php echo $elementType->getId(); ?></td>

Check warning on line 36 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L29-L36

Added lines #L29 - L36 were not covered by tests
<td class="px-4 py-2 border-b">
<?php echo $elementType->name; ?></td>

Check warning on line 38 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L38

Added line #L38 was not covered by tests
<td class="px-4 py-2 border-b">
<?php echo $elementType->description; ?></td>

Check warning on line 40 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L40

Added line #L40 was not covered by tests
<td class="px-4 py-3 border-b text-center flex space-x-4">
<!-- Edit Button (Pencil Icon) -->
<a href="/admin/elementType/<?php echo htmlspecialchars($elementType->getId()); ?>/edit"

Check warning on line 43 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L43

Added line #L43 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
class="text-blue-500 hover:text-blue-700" title="Edit">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
</a>
<!-- Delete Button (Trash Icon) -->
<a href="/admin/elementType/<?php echo htmlspecialchars($elementType->getId()); ?>/delete"

Check warning on line 50 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L50

Added line #L50 was not covered by tests
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
onclick="return confirm('Are you sure you want to delete this element?');"
class="text-red-500 hover:text-red-700" title="Delete">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
<path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>

Check warning on line 62 in app/src/app/Views/Admin/ElementTypes.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/ElementTypes.php#L60-L62

Added lines #L60 - L62 were not covered by tests
33 changes: 33 additions & 0 deletions app/src/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,6 +138,27 @@
'method' => 'destroy',
'middlewares' => [AdminMiddleware::class],
],
// ElementType
'/admin/elementTypes' => [
'controller' => ElementTypeController::class,
'method' => 'index',
'middlewares' => [AdminMiddleware::class],
],
'/admin/elementType/create' => [
'controller' => ElementTypeController::class,
'method' => 'create',
'middlewares' => [AdminMiddleware::class],
],
'/admin/elementType/:id/edit' => [
'controller' => ElementTypeController::class,
'method' => 'edit',
'middlewares' => [AdminMiddleware::class],
],
'/admin/elementType/:id/delete' => [
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
'controller' => ElementTypeController::class,
'method' => 'destroy',
'middlewares' => [AdminMiddleware::class],
],
// === TaskTypes GET Routes
'/admin/task-types' => [
'controller' => TaskTypeController::class,
Expand Down Expand Up @@ -202,6 +224,17 @@
'method' => 'update',
'middlewares' => [AdminMiddleware::class],
],
// === Element-Types POST Routes
'/admin/elementType/store' => [
'controller' => ElementTypeController::class,
'method' => 'store',
'middlewares' => [AdminMiddleware::class],
],
'/admin/elementType/:id/update' => [
albert1413 marked this conversation as resolved.
Show resolved Hide resolved
'controller' => ElementTypeController::class,
'method' => 'update',
'middlewares' => [AdminMiddleware::class],
],
// === WorkOrders POST Routes
'/admin/work-order/store' => [
'controller' => WorkOrderController::class,
Expand Down
Loading