Project Controller
Let's link the view we created on the previous page with a functioning controller. Create a new file at /src/Disaster/Controllers/ProjectController.php with the following contents:
<?php
declare(strict_types=1);
namespace App\Disaster\Controllers;
use Apex\Svc\{App, Db};
use App\Disaster\Models\Project;
use Apex\App\Base\Lists\CountryList;
/**
* Project controller
*/
class ProjectController
{
#[Inject(App::class)]
private App $app;
#[Inject(Db::class)]
private Db $db;
/**
* Create project
*/
public function create():?Project
{
// Create project
$proj = Project::insert([
'location_id' => $this->app->post('location_id'),
'status' => $this->app->post('status'),
'title' => $this->app->post('title'),
'description' => $this->app->post('description')
]);
// Return
return $proj;
}
/**
* Create location options
*/
public function createLocationOptions(string $selected = ''):string
{
// Go through locations
$options = '';
$rows = $this->db->query("SELECT id,country,city FROM disaster_locations WHERE is_active = %b ORDER BY id", true);
foreach ($rows as $row) {
$chk = $selected == $row['id'] ? 'selected="selected"' : '';
$name = $row['city'] . ', ' . CountryList::$opt[$row['country']]['name'];
$options .= "<option value=\"$row[id]\" $chk>$name</option>";
}
// Return
return $options;
}
}
Visit the Disaster Relief->Projects menu of the administration panel now, and you will see it's possible to create a new project through the one tab within the tab control. Now let's tie everything together by creating a data table and making a few more minor modifications.