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

Commit

Permalink
improve field:create command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabea David committed Dec 12, 2016
1 parent 78e6264 commit c2c7438
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 57 deletions.
114 changes: 59 additions & 55 deletions src/Commands/Field/FieldCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Wireshell\Helpers\PwConnector;
use Wireshell\Helpers\PwTools;
use Wireshell\Helpers\WsTools as Tools;

/**
* Class FieldCreateCommand
Expand All @@ -17,68 +18,71 @@
* @author Marcus Herrmann
* @author Tabea David
*/
class FieldCreateCommand extends PwConnector
{
class FieldCreateCommand extends PwConnector {

/**
* Configures the current command.
*/
protected function configure()
{
$this
->setName('field:create')
->setDescription('Creates a field')
->addArgument('name', InputArgument::REQUIRED)
->addOption('label', null, InputOption::VALUE_REQUIRED, 'Label')
->addOption('desc', null, InputOption::VALUE_REQUIRED, 'Description')
->addOption('tag', null, InputOption::VALUE_REQUIRED, 'Tag')
->addOption('type', null, InputOption::VALUE_REQUIRED,
'Type of field: text|textarea|email|datetime|checkbox|file|float|image|integer|page|url');
}
/**
* Configures the current command.
*/
protected function configure() {
$this
->setName('field:create')
->setDescription('Creates a field')
->addArgument('name', InputArgument::OPTIONAL)
->addOption('label', null, InputOption::VALUE_REQUIRED, 'Label')
->addOption('desc', null, InputOption::VALUE_REQUIRED, 'Description')
->addOption('tag', null, InputOption::VALUE_REQUIRED, 'Tag')
->addOption('type', null, InputOption::VALUE_REQUIRED,
'Type of field: text|textarea|email|datetime|checkbox|file|float|image|integer|page|url');
}

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

$name = $input->getArgument('name');
$label = $input->getOption('label') !== "" ? $input->getOption('label') : $name;
$tools = new Tools($output);
$tools
->setInput($input)
->setHelper($this->getHelper('question'))
->writeBlockCommand($this->getName());

$type = PwTools::getProperFieldtypeName($input->getOption('type'));
$check = $this->checkFieltype($type);
$name = $tools->ask($input->getArgument('name'), 'New field name', null, false, null, 'required');
$label = $input->getOption('label') ? $input->getOption('label') : $name;

if ($check === true) {
$field = new Field();
$field->type = \ProcessWire\wire('modules')->get($type);
$field->name = $name;
$field->label = $label;
$field->description = $input->getOption('desc');
if ($input->getOption('tag')) $field->tags = $input->getOption('tag');
$field->save();
$type = PwTools::getProperFieldtypeName($input->getOption('type'));
$check = $this->checkFieltype($type);

$output->writeln("<info>Field '{$name}' ($type) created successfully!</info>");
} else {
$output->writeln("<error>This fieldtype `$type` does not exists.</error>");
}
}
if ($check === true) {
$field = new Field();
$field->type = \ProcessWire\wire('modules')->get($type);
$field->name = $name;
$field->label = $label;
$field->description = $input->getOption('desc');
if ($input->getOption('tag')) $field->tags = $input->getOption('tag');
$field->save();

/**
* @param $type
*/
protected function checkFieltype($type) {
// get available fieldtypes
$fieldtypes = array();
foreach (\ProcessWire\wire('modules') as $module) {
if (preg_match('/^Fieldtype/', $module->name)) {
$fieldtypes[] = $module->name;
}
}
$tools->writeSuccess("Field '{$name}' ($type) created successfully.");
} else {
$tools->writeError("This fieldtype `$type` does not exists.");
}
}

// check whether fieldtype exists
return in_array($type, $fieldtypes) ? true : false;
/**
* @param $type
*/
protected function checkFieltype($type) {
// get available fieldtypes
$fieldtypes = array();
foreach (\ProcessWire\wire('modules') as $module) {
if (preg_match('/^Fieldtype/', $module->name)) {
$fieldtypes[] = $module->name;
}
}

// check whether fieldtype exists
return in_array($type, $fieldtypes) ? true : false;
}
}
9 changes: 9 additions & 0 deletions src/Helpers/WsTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ public function ask($item, $question, $default = null, $hidden = false, $autoco
return $answer;
});
break;

case 'required':
$question->setValidator(function ($answer) {
if (!$answer) {
throw new \RuntimeException('Please provide an answer.');
}
return $answer;
});
break;
}

$question->setMaxAttempts(3);
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:create Creates a field
- field:delete Deletes fields
- field:edit Edit a field
- field:tag Tags fields
Expand All @@ -23,7 +22,7 @@ Improve Commands
- page:delete Deletes ProcessWire pages
- page:emptytrash Empty Trash

Done: 24 of 37 ~ 65%
Done: 25 of 37 ~ 68%

Sections Done: 6 of 9

Expand Down

0 comments on commit c2c7438

Please sign in to comment.