mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-19 00:34:24 +00:00
refactoring Command/CreateFieldsOnGroupCommand.php : code indent
This commit is contained in:
parent
afc51d2f31
commit
653112ba57
@ -38,47 +38,44 @@ use Chill\CustomFieldsBundle\Entity\CustomField;
|
|||||||
*/
|
*/
|
||||||
class CreateFieldsOnGroupCommand extends ContainerAwareCommand
|
class CreateFieldsOnGroupCommand extends ContainerAwareCommand
|
||||||
{
|
{
|
||||||
|
|
||||||
const ARG_PATH = 'path';
|
const ARG_PATH = 'path';
|
||||||
const ARG_DELETE = 'delete';
|
const ARG_DELETE = 'delete';
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this->setName('chill:custom_fields:populate_group')
|
$this->setName('chill:custom_fields:populate_group')
|
||||||
->setDescription('Create custom fields from a yml file')
|
->setDescription('Create custom fields from a yml file')
|
||||||
->addArgument(self::ARG_PATH, InputOption::VALUE_REQUIRED,
|
->addArgument(self::ARG_PATH, InputOption::VALUE_REQUIRED,
|
||||||
'Path to description file')
|
'Path to description file')
|
||||||
->addOption(self::ARG_DELETE, null, InputOption::VALUE_NONE,
|
->addOption(self::ARG_DELETE, null, InputOption::VALUE_NONE,
|
||||||
'If set, delete existing fields')
|
'If set, delete existing fields');
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$dialog = $this->getHelperSet()->get('dialog');
|
$dialog = $this->getHelperSet()->get('dialog');
|
||||||
$em = $this->getContainer()
|
$em = $this->getContainer()
|
||||||
->get('doctrine.orm.default_entity_manager');
|
->get('doctrine.orm.default_entity_manager');
|
||||||
|
|
||||||
$customFieldsGroups = $em
|
$customFieldsGroups = $em
|
||||||
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
|
->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
|
||||||
->findAll()
|
->findAll();
|
||||||
;
|
|
||||||
|
|
||||||
if (count($customFieldsGroups) === 0) {
|
if (count($customFieldsGroups) === 0) {
|
||||||
$output->writeln('<error>There aren\'t any CustomFieldsGroup recorded'
|
$output->writeln('<error>There aren\'t any CustomFieldsGroup recorded'
|
||||||
. ' Please create at least one.</error>');
|
. ' Please create at least one.</error>');
|
||||||
}
|
}
|
||||||
|
|
||||||
$table = $this->getHelperSet()->get('table');
|
$table = $this->getHelperSet()->get('table');
|
||||||
$table->setHeaders(array_merge(array('id', 'entity'), $this->getContainer()
|
$table->setHeaders(array_merge(array('id', 'entity'), $this->getContainer()
|
||||||
->getParameter('chill_main.available_languages')))
|
->getParameter('chill_main.available_languages')))
|
||||||
->setRows($this->_prepareRows($customFieldsGroups));
|
->setRows($this->_prepareRows($customFieldsGroups));
|
||||||
$table->render($output);
|
$table->render($output);
|
||||||
|
|
||||||
$customFieldsGroup = $dialog->askAndValidate($output,
|
$customFieldsGroup = $dialog->askAndValidate($output,
|
||||||
"Enter the customfieldGroup's id on which the custom fields "
|
"Enter the customfieldGroup's id on which the custom fields "
|
||||||
. "should be added :",
|
. "should be added :",
|
||||||
function($answer) use ($customFieldsGroups) {
|
function($answer) use ($customFieldsGroups) {
|
||||||
foreach ($customFieldsGroups as $customFieldsGroup) {
|
foreach ($customFieldsGroups as $customFieldsGroup) {
|
||||||
if ($answer == $customFieldsGroup->getId()) {
|
if ($answer == $customFieldsGroup->getId()) {
|
||||||
return $customFieldsGroup;
|
return $customFieldsGroup;
|
||||||
@ -86,10 +83,11 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new \RunTimeException('The id does not match an existing '
|
throw new \RunTimeException('The id does not match an existing '
|
||||||
. 'CustomFieldsGroup');
|
. 'CustomFieldsGroup');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO : getCustomFields or getActiveCustomFields ?
|
||||||
if ($input->getOption(self::ARG_DELETE)) {
|
if ($input->getOption(self::ARG_DELETE)) {
|
||||||
foreach ($customFieldsGroup->getCustomFields() as $field) {
|
foreach ($customFieldsGroup->getCustomFields() as $field) {
|
||||||
$em->remove($field);
|
$em->remove($field);
|
||||||
@ -97,43 +95,41 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
$fieldsInput = $this->_parse($input->getArgument(self::ARG_PATH),
|
$fieldsInput = $this->_parse($input->getArgument(self::ARG_PATH),
|
||||||
$output);
|
$output);
|
||||||
|
|
||||||
$fields = $this->_addFields($customFieldsGroup, $fieldsInput, $output);
|
$fields = $this->_addFields($customFieldsGroup, $fieldsInput, $output);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _prepareRows ($customFieldsGroups)
|
private function _prepareRows ($customFieldsGroups)
|
||||||
{
|
{
|
||||||
$rows = array();
|
$rows = array();
|
||||||
$languages = $this->getContainer()
|
$languages = $this->getContainer()
|
||||||
->getParameter('chill_main.available_languages');
|
->getParameter('chill_main.available_languages');
|
||||||
//gather entitites and create an array to access them easily
|
//gather entitites and create an array to access them easily
|
||||||
$customizableEntities = array();
|
$customizableEntities = array();
|
||||||
foreach ($this->getContainer()
|
foreach ($this->getContainer()
|
||||||
->getParameter('chill_custom_fields.customizables_entities') as $entry) {
|
->getParameter('chill_custom_fields.customizables_entities') as $entry) {
|
||||||
$customizableEntities[$entry['class']] = $entry['name'];
|
$customizableEntities[$entry['class']] = $entry['name'];
|
||||||
}
|
}
|
||||||
|
|
||||||
array_walk($customFieldsGroups,
|
array_walk($customFieldsGroups,
|
||||||
function(CustomFieldsGroup $customFieldGroup, $key)
|
function(CustomFieldsGroup $customFieldGroup, $key)
|
||||||
use ($languages, &$rows, $customizableEntities) {
|
use ($languages, &$rows, $customizableEntities) {
|
||||||
//set id and entity
|
//set id and entity
|
||||||
$row = array(
|
$row = array(
|
||||||
$customFieldGroup->getId(),
|
$customFieldGroup->getId(),
|
||||||
$customizableEntities[$customFieldGroup->getEntity()]
|
$customizableEntities[$customFieldGroup->getEntity()]
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($languages as $lang) {
|
foreach ($languages as $lang) {
|
||||||
//todo replace with service to find lang when available
|
//todo replace with service to find lang when available
|
||||||
$row[] = (isset($customFieldGroup->getName()[$lang])) ?
|
$row[] = (isset($customFieldGroup->getName()[$lang])) ?
|
||||||
$customFieldGroup->getName()[$lang] :
|
$customFieldGroup->getName()[$lang] :
|
||||||
'Not available in this language';
|
'Not available in this language';
|
||||||
}
|
}
|
||||||
$rows[] = $row;
|
$rows[] = $row;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return $rows;
|
return $rows;
|
||||||
}
|
}
|
||||||
@ -152,7 +148,6 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
|
|||||||
throw new \RunTimeException("The yaml file is not valid", 0, $ex);
|
throw new \RunTimeException("The yaml file is not valid", 0, $ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,44 +158,41 @@ class CreateFieldsOnGroupCommand extends ContainerAwareCommand
|
|||||||
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
|
||||||
|
|
||||||
$languages = $this->getContainer()
|
$languages = $this->getContainer()
|
||||||
->getParameter('chill_main.available_languages');
|
->getParameter('chill_main.available_languages');
|
||||||
|
|
||||||
foreach($values['fields'] as $slug => $field) {
|
foreach($values['fields'] as $slug => $field) {
|
||||||
//check the cf type exists
|
//check the cf type exists
|
||||||
$cfType = $cfProvider->getCustomFieldByType($field['type']);
|
$cfType = $cfProvider->getCustomFieldByType($field['type']);
|
||||||
if ($cfType === NULL) {
|
if ($cfType === NULL) {
|
||||||
throw new \RunTimeException('the type '.$field['type'].' '
|
throw new \RunTimeException('the type '.$field['type'].' '
|
||||||
. 'does not exists');
|
. 'does not exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
$cf = new CustomField();
|
$cf = new CustomField();
|
||||||
$cf->setSlug($slug)
|
$cf->setSlug($slug)
|
||||||
->setName($field['name'])
|
->setName($field['name'])
|
||||||
->setOptions(isset($field['options']) ? $field['options'] : array() )
|
->setOptions(isset($field['options']) ? $field['options'] : array() )
|
||||||
->setOrdering($field['ordering'])
|
->setOrdering($field['ordering'])
|
||||||
->setType($field['type'])
|
->setType($field['type'])
|
||||||
->setCustomFieldsGroup($group)
|
->setCustomFieldsGroup($group);
|
||||||
;
|
|
||||||
|
|
||||||
//add to table
|
//add to table
|
||||||
$names = array();
|
$names = array();
|
||||||
foreach ($languages as $lang) {
|
foreach ($languages as $lang) {
|
||||||
//todo replace with service to find lang when available
|
//todo replace with service to find lang when available
|
||||||
$names[] = (isset($cf->getName()[$lang])) ?
|
$names[] = (isset($cf->getName()[$lang])) ?
|
||||||
$cf->getName()[$lang] :
|
$cf->getName()[$lang] :
|
||||||
'Not available in this language';
|
'Not available in this language';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($this->getContainer()->get('validator')->validate($cf)) {
|
if ($this->getContainer()->get('validator')->validate($cf)) {
|
||||||
$em->persist($cf);
|
$em->persist($cf);
|
||||||
$output->writeln("<info>Adding Custom Field of type "
|
$output->writeln("<info>Adding Custom Field of type "
|
||||||
.$cf->getType()."\t with slug ".$cf->getSlug().
|
.$cf->getType()."\t with slug ".$cf->getSlug().
|
||||||
"\t and names : ".implode($names, ', ')."</info>");
|
"\t and names : ".implode($names, ', ')."</info>");
|
||||||
} else {
|
} else {
|
||||||
throw new \RunTimeException("Error in field ".$slug);
|
throw new \RunTimeException("Error in field ".$slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user