Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
improve field:delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabea David committed Dec 12, 2016
1 parent c2c7438 commit 4bdaba1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
86 changes: 51 additions & 35 deletions src/Commands/Field/FieldDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Wireshell\Helpers\PwConnector;
use Wireshell\Helpers\WsTools as Tools;

/**
* Class FieldDeleteCommand
Expand All @@ -18,48 +19,63 @@
* @package Wireshell
* @author Tabea David
*/
class FieldDeleteCommand extends PwConnector
{
class FieldDeleteCommand extends PwConnector {

/**
* Configures the current command.
*/
protected function configure()
{
$this
->setName('field:delete')
->setDescription('Deletes fields')
->addArgument('field', InputArgument::REQUIRED, 'Comma separated list.');
}
/**
* Configures the current command.
*/
protected function configure() {
$this
->setName('field:delete')
->setDescription('Deletes fields')
->addArgument('field', InputArgument::OPTIONAL, 'Comma separated list.');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output) {
parent::setOutput($output)::setInput($input)::bootstrapProcessWire();

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$tools = new Tools($output);
$tools
->setInput($input)
->setHelper($this->getHelper('question'))
->writeBlockCommand($this->getName());

parent::bootstrapProcessWire($output);
// ask
$fields = \ProcessWire\wire('fields');
$availableFields = array();
foreach ($fields as $field) {
// exclude system fields
if ($field->flags & Field::flagSystem || $field->flags & Field::flagPermanent) continue;
$availableFields[] = $field->name;
}

$inputFields = explode(',', $input->getArgument('field'));
$fields = \ProcessWire\wire('fields');
$inputFields = $input->getArgument('field') ? explode(',', $input->getArgument('field')) : null;
$inputFields = $tools->askChoice($inputFields, 'Select all fields which should be deleted', $availableFields, 0, true);
$tools->nl();

foreach ($inputFields as $field) {
$fieldToDelete = $fields->get($field);
foreach ($inputFields as $field) {
$fieldToDelete = $fields->get($field);

if (is_null($fieldToDelete)) {
$output->writeln("\n<error> > Field '{$field}' does not exist!</error>");
continue;
}
if (is_null($fieldToDelete)) {
$tools->writeError("> Field '{$field}' does not exist.");
$tools->nl();
continue;
}

try {
$fields->delete($fieldToDelete);
$output->writeln("\n<info> > Field '{$field}' deleted successfully!</info>");
} catch (\WireException $e) {
$output->writeln("\n<error> > {$e->getMessage()}</error>");
}
}
try {
$fields->delete($fieldToDelete);
$tools->writeSuccess(" > Field '{$field}' deleted successfully.");
$tools->nl();
} catch (\WireException $e) {
$tools->writeError("> {$e->getMessage()}");
$tools->nl();
}
}
}

}
5 changes: 2 additions & 3 deletions src/Commands/Template/TemplateDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public function execute(InputInterface $input, OutputInterface $output) {
$tools = new Tools($output);
$tools
->setHelper($this->getHelper('question'))
->setInput($input);

$tools->writeBlockCommand($this->getName());
->setInput($input)
->writeBlockCommand($this->getName());

// ask which template should be deleted
$availableTemplates = $this->getAvailableTemplates();
Expand Down
3 changes: 1 addition & 2 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Improve Commands

## field

- field:delete Deletes fields
- field:edit Edit a field
- field:tag Tags fields

Expand All @@ -22,7 +21,7 @@ Improve Commands
- page:delete Deletes ProcessWire pages
- page:emptytrash Empty Trash

Done: 25 of 37 ~ 68%
Done: 26 of 37 ~ 70%

Sections Done: 6 of 9

Expand Down

0 comments on commit 4bdaba1

Please sign in to comment.