Files
animal_management/controllers/SettingsController.php

92 lines
3.9 KiB
PHP

<?php
namespace humhub\modules\animal_management\controllers;
use humhub\modules\animal_management\models\Animal;
use humhub\modules\animal_management\models\forms\DisplaySettingsForm;
use humhub\modules\animal_management\models\forms\FieldDefinitionSettingsForm;
use humhub\modules\animal_management\services\ModuleSetupService;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\content\components\ContentContainerControllerAccess;
use humhub\modules\rescue_foundation\widgets\RescueSettingsMenu;
use humhub\modules\space\models\Space;
use Yii;
use yii\web\BadRequestHttpException;
class SettingsController extends ContentContainerController
{
protected function getAccessRules()
{
return [[ContentContainerControllerAccess::RULE_USER_GROUP_ONLY => [Space::USERGROUP_OWNER, Space::USERGROUP_ADMIN]]];
}
public function actionIndex()
{
$subNav = null;
if (class_exists(RescueSettingsMenu::class)) {
$subNav = RescueSettingsMenu::widget(['space' => $this->contentContainer]);
}
$fieldSettingsForm = new FieldDefinitionSettingsForm();
$fieldSettingsForm->loadRows();
$displaySettingsForm = new DisplaySettingsForm([
'contentContainer' => $this->contentContainer,
]);
$displaySettingsForm->loadValues();
if (Yii::$app->request->post('DisplaySettingsForm') !== null) {
if ($displaySettingsForm->load(Yii::$app->request->post()) && $displaySettingsForm->save()) {
$this->view->success(Yii::t('AnimalManagementModule.base', 'Display settings saved.'));
return $this->redirect($this->contentContainer->createUrl('/animal_management/settings'));
}
}
if (Yii::$app->request->post('FieldDefinitionSettingsForm') !== null) {
if ($fieldSettingsForm->load(Yii::$app->request->post()) && $fieldSettingsForm->save()) {
$this->view->success(Yii::t('AnimalManagementModule.base', 'Field settings saved.'));
return $this->redirect($this->contentContainer->createUrl('/animal_management/settings'));
}
}
$animalCount = Animal::find()->where(['contentcontainer_id' => $this->contentContainer->contentcontainer_id])->count();
return $this->render('index', [
'subNav' => $subNav,
'animalCount' => (int)$animalCount,
'fieldSettingsForm' => $fieldSettingsForm,
'displaySettingsForm' => $displaySettingsForm,
'space' => $this->contentContainer,
]);
}
public function actionSetup()
{
if (!Yii::$app->request->isPost) {
throw new BadRequestHttpException('Invalid request method.');
}
if (!$this->contentContainer instanceof Space) {
$this->view->error(Yii::t('AnimalManagementModule.base', 'Setup can only be run inside a space.'));
return $this->redirect($this->contentContainer->createUrl('/animal_management/settings'));
}
try {
$result = ModuleSetupService::runForSpace($this->contentContainer);
$appliedCount = count($result['applied'] ?? []);
if ($appliedCount > 0) {
$this->view->success(Yii::t('AnimalManagementModule.base', 'Setup completed. Applied {count} migration(s).', [
'count' => $appliedCount,
]));
} else {
$this->view->success(Yii::t('AnimalManagementModule.base', 'Setup completed. No pending migrations were found.'));
}
} catch (\Throwable $e) {
Yii::error($e, 'animal_management.setup');
$this->view->error(Yii::t('AnimalManagementModule.base', 'Setup failed. Please check logs and try again.'));
}
return $this->redirect($this->contentContainer->createUrl('/animal_management/settings'));
}
}