Files
thetool/db/migrations/20230905130832_add_can_permissions.php
2023-09-18 10:01:53 +02:00

97 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddCanPermissions extends AbstractMigration
{
public function up(): void
{
// only run in thetool Env
if($this->getEnvironment() == "thetool") {
$new_perms = [
"canBuilding",
"canPipework",
"canLinework",
"canPatching",
"canFilestore",
"canCpeprovisioning",
"canCpeshipping",
"canVoipnumbering",
"canPreorder",
"canOrder"
];
$table = $this->table("WorkerPermission");
$setAllTrue = "";
$after = "preorderaddressreporting";
foreach($new_perms as $p) {
$table->addColumn($p, "enum", ["values" => 'false,true', "default" => "false", "after" => $after]);
$after = $p;
$setAllTrue .= "`$p` = 'true', ";
}
$table->update();
$setAllTrue = substr($setAllTrue, 0, strlen($setAllTrue) -2);
//echo "$setAllTrue\n";
$builder = $this->getQueryBuilder();
$q = $builder->select("*")->from("WorkerPermission")->execute();
while($perm = $q->fetch('assoc')) {
$id = $perm["id"];
if($perm["admin"] == "true") {
$this->query("UPDATE WorkerPermission SET $setAllTrue WHERE id=$id");
} elseif($perm["preorderfront"] == "true" || $perm['preorderaddressreporting'] == "true") {
$this->query("UPDATE WorkerPermission SET `canPreorder` = 'true' WHERE id=$id");
} else {
$this->query("UPDATE WorkerPermission SET
`canBuilding` = 'true',
`canPipework` = 'true',
`canLinework` = 'true',
`canPatching` = 'true',
`canFilestore` = 'true',
`canOrder` = 'true',
`canPreorder` = 'true'
WHERE id=$id");
}
}
}
if($this->getEnvironment() == "addressdb") {
// do nothing in addressdb Env
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("WorkerPermission");
$table->removeColumn("canOrder");
$table->removeColumn("canPreorder");
$table->removeColumn("canVoipnumbering");
$table->removeColumn("canCpeshipping");
$table->removeColumn("canCpeprovisioning");
$table->removeColumn("canFilestore");
$table->removeColumn("canPatching");
$table->removeColumn("canLinework");
$table->removeColumn("canPipework");
$table->removeColumn("canBuilding");
$table->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
}