Akaunting/overrides/akaunting/module/Commands/DisableCommand.php

83 lines
2.1 KiB
PHP
Raw Normal View History

2018-05-21 15:10:43 +00:00
<?php
2019-11-16 07:21:14 +00:00
namespace Akaunting\Module\Commands;
2018-05-21 15:10:43 +00:00
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use Illuminate\Console\Command;
2019-11-16 07:21:14 +00:00
use Illuminate\Support\Str;
2018-05-21 15:10:43 +00:00
use Symfony\Component\Console\Input\InputArgument;
2019-11-16 07:21:14 +00:00
class DisableCommand extends Command
2018-05-21 15:10:43 +00:00
{
/**
2018-05-21 18:54:32 +00:00
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:disable {alias} {company_id}';
2018-05-21 15:10:43 +00:00
/**
2018-05-21 18:54:32 +00:00
* The console command description.
*
* @var string
*/
2018-05-21 15:10:43 +00:00
protected $description = 'Disable the specified module.';
/**
2018-05-21 18:54:32 +00:00
* Execute the console command.
*
* @return mixed
*/
2018-05-21 15:10:43 +00:00
public function handle()
{
2019-11-16 07:21:14 +00:00
$alias = Str::kebab($this->argument('alias'));
2018-05-21 15:17:54 +00:00
$company_id = $this->argument('company_id');
2018-05-21 18:54:32 +00:00
$model = Module::alias($alias)->companyId($company_id)->first();
2018-05-21 15:10:43 +00:00
2018-05-21 15:17:54 +00:00
if (!$model) {
$this->info("Module [{$alias}] not found.");
return;
}
2019-11-16 07:21:14 +00:00
if ($model->enabled == 1) {
$model->enabled = 0;
2018-05-21 15:10:43 +00:00
$model->save();
2019-11-16 07:21:14 +00:00
$module = module($alias);
2018-05-21 15:17:54 +00:00
// Add history
$data = [
'company_id' => $company_id,
'module_id' => $model->id,
'category' => $module->get('category', 'payment-method'),
2018-05-21 15:17:54 +00:00
'version' => $module->get('version'),
2019-11-16 07:21:14 +00:00
'description' => trans('modules.disabled', ['module' => $module->get('alias')]),
2018-05-21 15:17:54 +00:00
];
ModuleHistory::create($data);
2018-05-21 15:10:43 +00:00
2019-11-16 07:21:14 +00:00
// Trigger event
event(new \App\Events\Module\Disabled($alias, $company_id));
2018-05-21 15:10:43 +00:00
$this->info("Module [{$alias}] disabled.");
} else {
$this->comment("Module [{$alias}] is already disabled.");
}
}
/**
2018-05-21 15:17:54 +00:00
* Get the console command arguments.
*
* @return array
*/
2018-05-21 15:10:43 +00:00
protected function getArguments()
{
return array(
array('alias', InputArgument::REQUIRED, 'Module alias.'),
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
}
}