65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class RenameAndGrantSchoolStatisticsNavItem extends Migration
|
|
{
|
|
private string $url = 'administrator/stats';
|
|
|
|
public function up(): void
|
|
{
|
|
$navItemId = $this->rename('School Statistics');
|
|
if ($navItemId <= 0 || ! $this->db->tableExists('role_nav_items')) {
|
|
return;
|
|
}
|
|
|
|
foreach (['administrator', 'admin', 'principal', 'vice_principal', 'administrative staff', 'head of department (education)'] as $role) {
|
|
$roleRow = $this->db->table('roles')
|
|
->select('id')
|
|
->where('LOWER(name)', strtolower($role))
|
|
->get()
|
|
->getRowArray();
|
|
if ($roleRow === null) {
|
|
continue;
|
|
}
|
|
|
|
$roleId = (int) $roleRow['id'];
|
|
$exists = $this->db->table('role_nav_items')
|
|
->where('role_id', $roleId)
|
|
->where('nav_item_id', $navItemId)
|
|
->countAllResults() > 0;
|
|
if (! $exists) {
|
|
$this->db->table('role_nav_items')->insert([
|
|
'role_id' => $roleId,
|
|
'nav_item_id' => $navItemId,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
$this->rename('Academic Statistics');
|
|
}
|
|
|
|
private function rename(string $label): int
|
|
{
|
|
if (! $this->db->tableExists('nav_items')) {
|
|
return 0;
|
|
}
|
|
|
|
$row = $this->db->table('nav_items')->select('id')->where('url', $this->url)->get()->getRowArray();
|
|
if ($row === null) {
|
|
return 0;
|
|
}
|
|
|
|
$navItemId = (int) $row['id'];
|
|
$this->db->table('nav_items')->where('id', $navItemId)->update(['label' => $label]);
|
|
|
|
return $navItemId;
|
|
}
|
|
}
|