mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
handling choiceType
This commit is contained in:
parent
50e6a9e3e5
commit
0d9a5ebde6
@ -123,7 +123,7 @@ EOF
|
|||||||
)
|
)
|
||||||
->addArgument('locale', InputArgument::REQUIRED,
|
->addArgument('locale', InputArgument::REQUIRED,
|
||||||
"The locale to use in displaying translatable strings from entities")
|
"The locale to use in displaying translatable strings from entities")
|
||||||
->addArgument('center', InputARgument::REQUIRED,
|
->addArgument('center', InputArgument::REQUIRED,
|
||||||
"The id of the center")
|
"The id of the center")
|
||||||
->addOption(
|
->addOption(
|
||||||
'force',
|
'force',
|
||||||
@ -158,6 +158,18 @@ EOF
|
|||||||
"The length of line to read. 0 means unlimited.",
|
"The length of line to read. 0 means unlimited.",
|
||||||
0
|
0
|
||||||
)
|
)
|
||||||
|
->addOption(
|
||||||
|
'dump-choice-matching',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_OPTIONAL,
|
||||||
|
"The path of the file to dump the matching between label in CSV and answers"
|
||||||
|
)
|
||||||
|
->addOption(
|
||||||
|
'load-choice-matching',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_OPTIONAL,
|
||||||
|
"The path of the file to load the matching between label in CSV and answers"
|
||||||
|
)
|
||||||
;
|
;
|
||||||
|
|
||||||
// mapping columns
|
// mapping columns
|
||||||
@ -223,6 +235,9 @@ EOF
|
|||||||
fclose($csv);
|
fclose($csv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load the matching between csv and label
|
||||||
|
$this->loadAnswerMatching();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function matchColumnToCustomField($row)
|
protected function matchColumnToCustomField($row)
|
||||||
@ -247,16 +262,26 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check a custom field exists
|
// check a custom field exists
|
||||||
$customField = $em->createQuery("SELECT cf "
|
try {
|
||||||
. "FROM ChillCustomFieldsBundle:CustomField cf "
|
$customField = $em->createQuery("SELECT cf "
|
||||||
. "JOIN cf.customFieldGroup g "
|
. "FROM ChillCustomFieldsBundle:CustomField cf "
|
||||||
. "WHERE cf.slug = :slug "
|
. "JOIN cf.customFieldGroup g "
|
||||||
. "AND g.entity = :entity")
|
. "WHERE cf.slug = :slug "
|
||||||
->setParameters(array(
|
. "AND g.entity = :entity")
|
||||||
'slug' => $cfSlug,
|
->setParameters(array(
|
||||||
'entity' => Person::class
|
'slug' => $cfSlug,
|
||||||
))
|
'entity' => Person::class
|
||||||
->getSingleResult();
|
))
|
||||||
|
->getSingleResult();
|
||||||
|
} catch (\Doctrine\ORM\NoResultException $e) {
|
||||||
|
$message = sprintf(
|
||||||
|
"The customfield with slug '%s' does not exists. It was associated with column number %d",
|
||||||
|
$cfSlug,
|
||||||
|
$rowNumber
|
||||||
|
);
|
||||||
|
$this->logger->error($message);
|
||||||
|
throw new \RuntimeException($message);
|
||||||
|
}
|
||||||
// skip if custom field does not exists
|
// skip if custom field does not exists
|
||||||
if ($customField === NULL) {
|
if ($customField === NULL) {
|
||||||
$this->logger->error("The custom field with slug $cfSlug could not be found. "
|
$this->logger->error("The custom field with slug $cfSlug could not be found. "
|
||||||
@ -272,6 +297,37 @@ EOF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the mapping between answer in CSV and value in choices from a json file
|
||||||
|
*/
|
||||||
|
protected function loadAnswerMatching()
|
||||||
|
{
|
||||||
|
if ($this->input->hasOption('load-choice-matching')) {
|
||||||
|
$fs = new Filesystem();
|
||||||
|
$filename = $this->input->getOption('load-choice-matching');
|
||||||
|
|
||||||
|
if (!$fs->exists($filename)) {
|
||||||
|
$this->logger->warning("The file $filename is not found. Choice matching not loaded");
|
||||||
|
} else {
|
||||||
|
$this->logger->debug("Loading $filename as choice matching");
|
||||||
|
$this->cacheAnswersMapping = \json_decode(\file_get_contents($filename), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function dumpAnswerMatching()
|
||||||
|
{
|
||||||
|
if ($this->input->hasOption('dump-choice-matching')) {
|
||||||
|
$this->logger->debug("Dump the matching between answer and choices");
|
||||||
|
$str = json_encode($this->cacheAnswersMapping, JSON_PRETTY_PRINT);
|
||||||
|
|
||||||
|
$fs = new Filesystem();
|
||||||
|
$filename = $this->input->getOption('dump-choice-matching');
|
||||||
|
|
||||||
|
$fs->dumpFile($filename, $str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$this->logger = new ConsoleLogger($output);
|
$this->logger = new ConsoleLogger($output);
|
||||||
@ -324,6 +380,8 @@ EOF
|
|||||||
} finally {
|
} finally {
|
||||||
$this->logger->debug('closing csv', array('method' => __METHOD__));
|
$this->logger->debug('closing csv', array('method' => __METHOD__));
|
||||||
fclose($csv);
|
fclose($csv);
|
||||||
|
// dump the matching between answer and choices
|
||||||
|
$this->dumpAnswerMatching();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -520,6 +578,7 @@ EOF
|
|||||||
$this->processTextType($row[$rowNumber], $form, $customField);
|
$this->processTextType($row[$rowNumber], $form, $customField);
|
||||||
break;
|
break;
|
||||||
case \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class:
|
case \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class:
|
||||||
|
case \Chill\MainBundle\Form\Type\Select2ChoiceType::class:
|
||||||
$cfData[$customField->getSlug()] =
|
$cfData[$customField->getSlug()] =
|
||||||
$this->processChoiceType($row[$rowNumber], $form, $customField);
|
$this->processChoiceType($row[$rowNumber], $form, $customField);
|
||||||
}
|
}
|
||||||
@ -554,6 +613,17 @@ EOF
|
|||||||
protected $cacheAnswersMapping = array();
|
protected $cacheAnswersMapping = array();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process a custom field choice.
|
||||||
|
*
|
||||||
|
* The method try to guess if the result exists amongst the text of the possible
|
||||||
|
* choices. If the texts exists, then this is picked. Else, ask the user.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @param \Symfony\Component\Form\FormInterface $form
|
||||||
|
* @param \Chill\CustomFieldsBundle\Entity\CustomField $cf
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
protected function processChoiceType(
|
protected function processChoiceType(
|
||||||
$value,
|
$value,
|
||||||
\Symfony\Component\Form\FormInterface $form,
|
\Symfony\Component\Form\FormInterface $form,
|
||||||
@ -561,62 +631,140 @@ EOF
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
// getting the possible answer and their value :
|
// getting the possible answer and their value :
|
||||||
$answers = array();
|
|
||||||
$view = $form->get($cf->getSlug())->createView();
|
$view = $form->get($cf->getSlug())->createView();
|
||||||
|
$answers = $this->collectChoicesAnswers($view->vars['choices']);
|
||||||
|
|
||||||
/* @var $choice \Symfony\Component\Form\ChoiceList\View\ChoiceView */
|
// if we do not have any answer on the question, throw an error.
|
||||||
foreach($view->vars['choices'] as $choice) {
|
if (count($answers) === 0) {
|
||||||
$answers[$choice->value] = $choice->label;
|
$message = sprintf(
|
||||||
|
"The question '%s' with slug '%s' does not count any answer.",
|
||||||
|
$this->helper->localize($cf->getName()),
|
||||||
|
$cf->getSlug()
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->logger->error($message, array(
|
||||||
|
'method' => __METHOD__,
|
||||||
|
'slug' => $cf->getSlug(),
|
||||||
|
'question' => $this->helper->localize($cf->getName())
|
||||||
|
));
|
||||||
|
|
||||||
|
throw new \RuntimeException($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the answer does not exists in cache. Asking the user
|
if ($view->vars['required'] === false) {
|
||||||
|
$answers[null] = '** no answer';
|
||||||
|
}
|
||||||
|
|
||||||
|
// the answer does not exists in cache. Try to find it, or asks the user
|
||||||
if (!isset($this->cacheAnswersMapping[$cf->getSlug()][$value])) {
|
if (!isset($this->cacheAnswersMapping[$cf->getSlug()][$value])) {
|
||||||
$this->output->writeln("<info>I do not know the answer to this question : </info>");
|
|
||||||
$this->output->writeln($this->helper->localize($cf->getName()));
|
|
||||||
|
|
||||||
//printing the possible answer
|
// try to find the answer (with array_keys and a search value
|
||||||
/* @var $table \Symfony\Component\Console\Helper\Table */
|
$values = array_keys(
|
||||||
$table = new Table($this->output);
|
array_map(function($label) { return trim(strtolower($label)); }, $answers),
|
||||||
$table->setHeaders(array('#', 'label', 'value'));
|
trim(strtolower($value)),
|
||||||
$matchingTableRowAnswer = array();
|
true
|
||||||
$i = 0;
|
);
|
||||||
|
|
||||||
foreach($answers as $key => $answer) {
|
if (count($values) === 1) {
|
||||||
$table->addRow(array(
|
// we could guess an answer !
|
||||||
$i, $answer, $key
|
$this->logger->info("This question accept multiple answers");
|
||||||
));
|
$this->cacheAnswersMapping[$cf->getSlug()][$value] =
|
||||||
$matchingTableRowAnswer[$i] = $key;
|
$view->vars['multiple'] == false ? $values[0] : array($values[0]);
|
||||||
$i++;
|
$this->logger->info(sprintf("Guessed that value '%s' match with key '%s' "
|
||||||
|
. "because the CSV and the label are equals.",
|
||||||
|
$value, $values[0]));
|
||||||
|
} else {
|
||||||
|
// we could nog guess an answer. Asking the user.
|
||||||
|
$this->output->writeln("<info>I do not know the answer to this question : </info>");
|
||||||
|
$this->output->writeln($this->helper->localize($cf->getName()));
|
||||||
|
|
||||||
|
// printing the possible answers
|
||||||
|
/* @var $table \Symfony\Component\Console\Helper\Table */
|
||||||
|
$table = new Table($this->output);
|
||||||
|
$table->setHeaders(array('#', 'label', 'value'));
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
|
foreach($answers as $key => $answer) {
|
||||||
|
$table->addRow(array(
|
||||||
|
$i, $answer, $key
|
||||||
|
));
|
||||||
|
$matchingTableRowAnswer[$i] = $key;
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
$table->render($this->output);
|
||||||
|
|
||||||
|
$question = new ChoiceQuestion(
|
||||||
|
sprintf('Please pick your choice for the value "%s"', $value),
|
||||||
|
array_keys($matchingTableRowAnswer)
|
||||||
|
);
|
||||||
|
$question->setErrorMessage("This choice is not possible");
|
||||||
|
|
||||||
|
if ($view->vars['multiple']) {
|
||||||
|
$this->logger->debug("this question is multiple");
|
||||||
|
$question->setMultiselect(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$selected = $this->getHelper('question')->ask($this->input, $this->output, $question);
|
||||||
|
|
||||||
|
$this->output->writeln(sprintf('You have selected "%s"',
|
||||||
|
is_array($answers[$matchingTableRowAnswer[$selected]]) ?
|
||||||
|
implode(',', $answers[$matchingTableRowAnswer[$selected]]) :
|
||||||
|
$answers[$matchingTableRowAnswer[$selected]])
|
||||||
|
);
|
||||||
|
|
||||||
|
// recording value in cache
|
||||||
|
$this->cacheAnswersMapping[$cf->getSlug()][$value] = $matchingTableRowAnswer[$selected];
|
||||||
|
$this->logger->debug(sprintf("Setting the value '%s' in cache for customfield '%s' and answer '%s'",
|
||||||
|
is_array($this->cacheAnswersMapping[$cf->getSlug()][$value]) ?
|
||||||
|
implode(', ', $this->cacheAnswersMapping[$cf->getSlug()][$value]) :
|
||||||
|
$this->cacheAnswersMapping[$cf->getSlug()][$value],
|
||||||
|
$cf->getSlug(),
|
||||||
|
$value));
|
||||||
}
|
}
|
||||||
$table->render($this->output);
|
|
||||||
|
|
||||||
$question = new ChoiceQuestion(sprintf('Please pick your choice for the value "%s"',
|
|
||||||
$value),
|
|
||||||
array_keys($matchingTableRowAnswer));
|
|
||||||
$question->setErrorMessage("This choice is not possible");
|
|
||||||
$selected = $this->getHelper('question')->ask($this->input, $this->output, $question);
|
|
||||||
|
|
||||||
$this->output->writeln(sprintf('You have selected "%s"',
|
|
||||||
$answers[$matchingTableRowAnswer[$selected]]));
|
|
||||||
|
|
||||||
// recording value in cache
|
|
||||||
$this->cacheAnswersMapping[$cf->getSlug()][$value] = $matchingTableRowAnswer[$selected];
|
|
||||||
$this->logger->debug(sprintf("Setting the value '%s' in cache for customfield '%s' and answer '%s'",
|
|
||||||
$this->cacheAnswersMapping[$cf->getSlug()][$value],
|
|
||||||
$cf->getSlug(),
|
|
||||||
$value));
|
|
||||||
}
|
}
|
||||||
|
var_dump($this->cacheAnswersMapping[$cf->getSlug()][$value]);
|
||||||
$form->submit(array($cf->getSlug() => $this->cacheAnswersMapping[$cf->getSlug()][$value]));
|
$form->submit(array($cf->getSlug() => $this->cacheAnswersMapping[$cf->getSlug()][$value]));
|
||||||
|
|
||||||
$value = $form->getData()[$cf->getSlug()];
|
$value = $form->getData()[$cf->getSlug()];
|
||||||
|
|
||||||
$this->logger->debug(sprintf("Found value : %s for custom field with question "
|
$this->logger->debug(sprintf(
|
||||||
. "'%s'", $value, $this->helper->localize($cf->getName())));
|
"Found value : %s for custom field with question '%s'",
|
||||||
|
is_array($value) ? implode(',', $value) : $value,
|
||||||
|
$this->helper->localize($cf->getName()))
|
||||||
|
);
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursive method to collect the possibles answer from a ChoiceType (or
|
||||||
|
* its inherited types).
|
||||||
|
*
|
||||||
|
* @param \Symfony\Component\Form\FormInterface $form
|
||||||
|
* @return array where
|
||||||
|
*/
|
||||||
|
private function collectChoicesAnswers($choices)
|
||||||
|
{
|
||||||
|
$answers = array();
|
||||||
|
|
||||||
|
/* @var $choice \Symfony\Component\Form\ChoiceList\View\ChoiceView */
|
||||||
|
foreach($choices as $choice) {
|
||||||
|
if ($choice instanceof \Symfony\Component\Form\ChoiceList\View\ChoiceView) {
|
||||||
|
$answers[$choice->value] = $choice->label;
|
||||||
|
} elseif ($choice instanceof \Symfony\Component\Form\ChoiceList\View\ChoiceGroupView) {
|
||||||
|
$answers = $answers + $this->collectChoicesAnswers($choice->choices);
|
||||||
|
} else {
|
||||||
|
throw new \Exception(sprintf(
|
||||||
|
"The choice type is not know. Expected '%s' or '%s', get '%s'",
|
||||||
|
\Symfony\Component\Form\ChoiceList\View\ChoiceView::class,
|
||||||
|
\Symfony\Component\Form\ChoiceList\View\ChoiceGroupView::class,
|
||||||
|
get_class($choice)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $answers;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected function processDate($value, $formats)
|
protected function processDate($value, $formats)
|
||||||
@ -631,7 +779,7 @@ EOF
|
|||||||
$string = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
|
$string = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
|
||||||
($dateR['tm_year']+1900),
|
($dateR['tm_year']+1900),
|
||||||
($dateR['tm_mon']+1),
|
($dateR['tm_mon']+1),
|
||||||
($dateR['tm_mday']+1),
|
($dateR['tm_mday']),
|
||||||
($dateR['tm_hour']),
|
($dateR['tm_hour']),
|
||||||
($dateR['tm_min']),
|
($dateR['tm_min']),
|
||||||
($dateR['tm_sec']));
|
($dateR['tm_sec']));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user