Command/ImportPeopleFromCSVCommand.php
protected function processChoiceType(
            $value,
            \Symfony\Component\Form\FormInterface $form, 
            \Chill\CustomFieldsBundle\Entity\CustomField $cf
            )
    {
        // getting the possible answer and their value :
        $view = $form->get($cf->getSlug())->createView();
        $answers = $this->collectChoicesAnswers($view->vars['choices']);
        
        // if we do not have any answer on the question, throw an error.
        if (count($answers) === 0) {
            $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);
        }
        
        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])) {
            
            // try to find the answer (with array_keys and a search value
            $values = array_keys(
                    array_map(function($label) { return trim(strtolower($label)); }, $answers), 
                    trim(strtolower($value)),
                    true
                    );
            
            if (count($values) === 1) {
                // we could guess an answer !
                $this->logger->info("This question accept multiple answers");
                $this->cacheAnswersMapping[$cf->getSlug()][$value] = 
                        $view->vars['multiple'] == false ? $values[0] : array($values[0]);
                $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));
            }
        }
        var_dump($this->cacheAnswersMapping[$cf->getSlug()][$value]);
        $form->submit(array($cf->getSlug() => $this->cacheAnswersMapping[$cf->getSlug()][$value]));
        $value = $form->getData()[$cf->getSlug()];
        
        $this->logger->debug(sprintf(
                "Found value : %s for custom field with question '%s'", 
                is_array($value) ? implode(',', $value) : $value, 
                $this->helper->localize($cf->getName()))
                );
        
        return $value;
    }
protected function processChoiceType(
            $value,
            \Symfony\Component\Form\FormInterface $form, 
            \Chill\CustomFieldsBundle\Entity\CustomField $cf
            )
    {
        // getting the possible answer and their value :
        $view = $form->get($cf->getSlug())->createView();
        $answers = $this->collectChoicesAnswers($view->vars['choices']);
        
        // if we do not have any answer on the question, throw an error.
        if (count($answers) === 0) {
            $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);
        }
        
        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])) {
            
            // try to find the answer (with array_keys and a search value
            $values = array_keys(
                    array_map(function($label) { return trim(strtolower($label)); }, $answers), 
                    trim(strtolower($value)),
                    true
                    );
            
            if (count($values) === 1) {
                // we could guess an answer !
                $this->logger->info("This question accept multiple answers");
                $this->cacheAnswersMapping[$cf->getSlug()][$value] = 
                        $view->vars['multiple'] == false ? $values[0] : array($values[0]);
                $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));
            }
        }
        var_dump($this->cacheAnswersMapping[$cf->getSlug()][$value]);
        $form->submit(array($cf->getSlug() => $this->cacheAnswersMapping[$cf->getSlug()][$value]));
        $value = $form->getData()[$cf->getSlug()];
        
        $this->logger->debug(sprintf(
                "Found value : %s for custom field with question '%s'", 
                is_array($value) ? implode(',', $value) : $value, 
                $this->helper->localize($cf->getName()))
                );
        
        return $value;
    }
protected function processChoiceType ( $ value , \ Symfony \ Component \ Form \ FormInterface $ form , \ Chill \ CustomFieldsBundle \ Entity \ CustomField $ cf ) { // getting the possible answer and their value : $ view = $ form -> get ( $ cf -> getSlug ( ) ) -> createView ( ) ; $ answers = $ this -> collectChoicesAnswers ( $ view -> vars [ 'choices' ] ) ; // if we do not have any answer on the question, throw an error. if ( count ( $ answers ) === 0 ) { $ 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 ) ; } 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 ] ) ) { // try to find the answer (with array_keys and a search value $ values = array_keys ( array_map ( function ( $ label ) { return trim ( strtolower ( $ label ) ) ; } , $ answers ) , trim ( strtolower ( $ value ) ) , true ) ; if ( count ( $ values ) === 1 ) { // we could guess an answer ! $ this -> logger -> info ( "This question accept multiple answers" ) ; $ this -> cacheAnswersMapping [ $ cf -> getSlug ( ) ] [ $ value ] = $ view -> vars [ 'multiple' ] == false ? $ values [ 0 ] : array ( $ values [ 0 ] ) ; $ 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 ) ) ; } } var_dump ( $ this -> cacheAnswersMapping [ $ cf -> getSlug ( ) ] [ $ value ] ) ; $ form -> submit ( array ( $ cf -> getSlug ( ) => $ this -> cacheAnswersMapping [ $ cf -> getSlug ( ) ] [ $ value ] ) ) ; $ value = $ form -> getData ( ) [ $ cf -> getSlug ( ) ] ; $ this -> logger -> debug ( sprintf ( "Found value : %s for custom field with question '%s'" , is_array ( $ value ) ? implode ( ',' , $ value ) : $ value , $ this -> helper -> localize ( $ cf -> getName ( ) ) ) ) ; return $ value ; }
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
Process a custom field choice .