Removed referenced angeltype from shift type

This commit is contained in:
Igor Scheller 2022-11-06 13:57:05 +01:00
parent 354ec084eb
commit ac162f4411
6 changed files with 65 additions and 50 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint;
class ShifttypeRemoveAngeltype extends Migration
{
use Reference;
/**
* Run the migration
*/
public function up(): void
{
if (!$this->schema->hasTable('ShiftTypes')) {
return;
}
$this->schema->table(
'ShiftTypes',
function (Blueprint $table) {
$table->dropForeign('shifttypes_ibfk_1');
$table->dropColumn('angeltype_id');
}
);
}
/**
* Reverse the migration
*/
public function down(): void
{
if (!$this->schema->hasTable('ShiftTypes')) {
return;
}
$this->schema->table(
'ShiftTypes',
function (Blueprint $table) {
$table->integer('angeltype_id')
->after('name')
->index()
->nullable();
$this->addReference($table, 'angeltype_id', 'AngelTypes', null, 'shifttypes_ibfk_1');
}
);
}
}

View File

@ -57,9 +57,10 @@ trait Reference
Blueprint $table, Blueprint $table,
string $fromColumn, string $fromColumn,
string $targetTable, string $targetTable,
?string $targetColumn = null ?string $targetColumn = null,
) { ?string $name = null
$table->foreign($fromColumn) ): void {
$table->foreign($fromColumn, $name)
->references($targetColumn ?: 'id')->on($targetTable) ->references($targetColumn ?: 'id')->on($targetTable)
->onUpdate('cascade') ->onUpdate('cascade')
->onDelete('cascade'); ->onDelete('cascade');

View File

@ -49,10 +49,8 @@ function shifttype_edit_controller()
{ {
$shifttype_id = null; $shifttype_id = null;
$name = ''; $name = '';
$angeltype_id = null;
$description = ''; $description = '';
$angeltypes = AngelTypes();
$request = request(); $request = request();
if ($request->has('shifttype_id')) { if ($request->has('shifttype_id')) {
@ -63,7 +61,6 @@ function shifttype_edit_controller()
} }
$shifttype_id = $shifttype['id']; $shifttype_id = $shifttype['id'];
$name = $shifttype['name']; $name = $shifttype['name'];
$angeltype_id = $shifttype['angeltype_id'];
$description = $shifttype['description']; $description = $shifttype['description'];
} }
@ -77,24 +74,18 @@ function shifttype_edit_controller()
error(__('Please enter a name.')); error(__('Please enter a name.'));
} }
if ($request->has('angeltype_id') && preg_match('/^\d+$/', $request->input('angeltype_id'))) {
$angeltype_id = $request->input('angeltype_id');
} else {
$angeltype_id = null;
}
if ($request->has('description')) { if ($request->has('description')) {
$description = strip_request_item_nl('description'); $description = strip_request_item_nl('description');
} }
if ($valid) { if ($valid) {
if ($shifttype_id) { if ($shifttype_id) {
ShiftType_update($shifttype_id, $name, $angeltype_id, $description); ShiftType_update($shifttype_id, $name, $description);
engelsystem_log('Updated shifttype ' . $name); engelsystem_log('Updated shifttype ' . $name);
success(__('Updated shifttype.')); success(__('Updated shifttype.'));
} else { } else {
$shifttype_id = ShiftType_create($name, $angeltype_id, $description); $shifttype_id = ShiftType_create($name, $description);
engelsystem_log('Created shifttype ' . $name); engelsystem_log('Created shifttype ' . $name);
success(__('Created shifttype.')); success(__('Created shifttype.'));
@ -105,7 +96,7 @@ function shifttype_edit_controller()
return [ return [
shifttypes_title(), shifttypes_title(),
ShiftType_edit_view($name, $angeltype_id, $angeltypes, $description, $shifttype_id) ShiftType_edit_view($name, $description, $shifttype_id)
]; ];
} }
@ -123,14 +114,9 @@ function shifttype_controller()
throw_redirect(page_link_to('shifttypes')); throw_redirect(page_link_to('shifttypes'));
} }
$angeltype = [];
if (!empty($shifttype['angeltype_id'])) {
$angeltype = AngelType($shifttype['angeltype_id']);
}
return [ return [
$shifttype['name'], $shifttype['name'],
ShiftType_view($shifttype, $angeltype) ShiftType_view($shifttype)
]; ];
} }

View File

@ -17,22 +17,19 @@ function ShiftType_delete($shifttype_id)
* *
* @param int $shifttype_id * @param int $shifttype_id
* @param string $name * @param string $name
* @param int $angeltype_id
* @param string $description * @param string $description
*/ */
function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) function ShiftType_update($shifttype_id, $name, $description)
{ {
Db::update( Db::update(
' '
UPDATE `ShiftTypes` SET UPDATE `ShiftTypes` SET
`name`=?, `name`=?,
`angeltype_id`=?,
`description`=? `description`=?
WHERE `id`=? WHERE `id`=?
', ',
[ [
$name, $name,
$angeltype_id,
$description, $description,
$shifttype_id, $shifttype_id,
] ]
@ -43,20 +40,18 @@ function ShiftType_update($shifttype_id, $name, $angeltype_id, $description)
* Create a shift type. * Create a shift type.
* *
* @param string $name * @param string $name
* @param int $angeltype_id
* @param string $description * @param string $description
* @return int|false new shifttype id * @return int|false new shifttype id
*/ */
function ShiftType_create($name, $angeltype_id, $description) function ShiftType_create($name, $description)
{ {
Db::insert( Db::insert(
' '
INSERT INTO `ShiftTypes` (`name`, `angeltype_id`, `description`) INSERT INTO `ShiftTypes` (`name`, `description`)
VALUES(?, ?, ?) VALUES(?, ?)
', ',
[ [
$name, $name,
$angeltype_id,
$description $description
] ]
); );

View File

@ -36,21 +36,12 @@ function ShiftType_delete_view($shifttype)
/** /**
* @param string $name * @param string $name
* @param int $angeltype_id
* @param array[] $angeltypes
* @param string $description * @param string $description
* @param int|bool $shifttype_id * @param int|bool $shifttype_id
* @return string * @return string
*/ */
function ShiftType_edit_view($name, $angeltype_id, $angeltypes, $description, $shifttype_id) function ShiftType_edit_view($name, $description, $shifttype_id)
{ {
$angeltypes_select = [
'' => __('All')
];
foreach ($angeltypes as $angeltype) {
$angeltypes_select[$angeltype['id']] = $angeltype['name'];
}
return page_with_title($shifttype_id ? __('Edit shifttype') : __('Create shifttype'), [ return page_with_title($shifttype_id ? __('Edit shifttype') : __('Create shifttype'), [
msg(), msg(),
buttons([ buttons([
@ -58,7 +49,6 @@ function ShiftType_edit_view($name, $angeltype_id, $angeltypes, $description, $s
]), ]),
form([ form([
form_text('name', __('Name'), $name), form_text('name', __('Name'), $name),
form_select('angeltype_id', __('Angeltype'), $angeltypes_select, $angeltype_id),
form_textarea('description', __('Description'), $description), form_textarea('description', __('Description'), $description),
form_info('', __('Please use markdown for the description.')), form_info('', __('Please use markdown for the description.')),
form_submit('submit', __('Save')) form_submit('submit', __('Save'))
@ -68,24 +58,16 @@ function ShiftType_edit_view($name, $angeltype_id, $angeltypes, $description, $s
/** /**
* @param array $shifttype * @param array $shifttype
* @param array $angeltype
* @return string * @return string
*/ */
function ShiftType_view($shifttype, $angeltype) function ShiftType_view($shifttype)
{ {
$parsedown = new Parsedown(); $parsedown = new Parsedown();
$title = $shifttype['name']; $title = $shifttype['name'];
if ($angeltype) {
$title .= ' <small>' . sprintf(__('for team %s'), $angeltype['name']) . '</small>';
}
return page_with_title($title, [ return page_with_title($title, [
msg(), msg(),
buttons([ buttons([
button(page_link_to('shifttypes'), shifttypes_title(), 'back'), button(page_link_to('shifttypes'), shifttypes_title(), 'back'),
$angeltype ? button(
page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]),
$angeltype['name']
) : '',
button( button(
page_link_to('shifttypes', ['action' => 'edit', 'shifttype_id' => $shifttype['id']]), page_link_to('shifttypes', ['action' => 'edit', 'shifttype_id' => $shifttype['id']]),
__('edit'), __('edit'),

View File

@ -60,6 +60,7 @@ trait HasDatabase
['migration' => '2022_06_03_000000_shifts_add_transaction_id'], ['migration' => '2022_06_03_000000_shifts_add_transaction_id'],
['migration' => '2022_07_21_000000_fix_old_groups_table_id_and_name'], ['migration' => '2022_07_21_000000_fix_old_groups_table_id_and_name'],
['migration' => '2022_10_21_000000_add_hide_register_to_angeltypes'], ['migration' => '2022_10_21_000000_add_hide_register_to_angeltypes'],
['migration' => '2022_11_06_000000_shifttype_remove_angeltype'],
] ]
); );