123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace WebserviceBundle\Form\ChoiceList\Loader;
- use Base\AdminBundle\Controller\TenancyService;
- use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
- use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\Form\FormEvent;
- use Symfony\Component\Form\FormEvents;
- class ClientChoiceLoader implements ChoiceLoaderInterface
- {
- /**
- * @var Webservice
- */
- protected $webservice;
- /**
- * @var TenancyService
- */
- protected $tenancyService;
- // Currently selected choices
- protected $selected = [];
- /**
- * @var string
- */
- protected $webserviceParameter;
- /**
- * @var boolean
- */
- protected $filterTenancy;
- /**
- * @param Webservice $webservice
- * @param TenancyService $tenancyService Contiene el servicio de tenencias.
- */
- public function __construct($webservice, $tenancyService, $webserviceParameter = 'client', $filterTenancy = true)
- {
- $this->webservice = $webservice;
- $this->tenancyService = $tenancyService;
- $this->webserviceParameter = $webserviceParameter;
- $this->filterTenancy = $filterTenancy;
- }
- /**
- * @param FormBuilderInterface $builder
- */
- public function setBuilder(FormBuilderInterface $builder)
- {
- if (is_object($builder) && ($builder instanceof FormBuilderInterface)) {
- $builder->addEventListener(
- FormEvents::POST_SET_DATA, [$this, 'onFormPostSetData']
- );
- $builder->addEventListener(
- FormEvents::POST_SUBMIT, [$this, 'onFormPostSetData']
- );
- }
- }
- /**
- * Form submit event callback
- * Here we get notified about the submitted choices.
- * Remember them so we can add them in loadChoiceList().
- */
- public function onFormPostSetData(FormEvent $event)
- {
- $this->selected = [];
- $formdata = $event->getData();
- if (!is_object($formdata)) {
- return;
- }
- if ($formdata->getClientId()) {
- $this->selected = array($formdata->getClientId());
- }
- }
- /**
- * Choices to be displayed in the SELECT element.
- * It's okay to not return all available choices, but the
- * selected/submitted choices (model values) must be
- * included.
- * Required by ChoiceLoaderInterface.
- */
- public function loadChoiceList($value = null)
- {
- $choices = array();
- if (is_array($this->selected) and !empty($this->selected)) {
- $choices = $this->getChoicesList(false);
- $missing_choices = array_flip($this->selected);
- foreach ($choices as $label => $id) {
- if (isset($missing_choices[$id])) {
- unset($missing_choices[$id]);
- }
- }
- // Now add selected choices if they're missing
- foreach (array_keys($missing_choices) as $id) {
- $label = $this->getChoiceLabel($id);
- if (strlen($label) === 0) {
- continue;
- }
- $choices[$label] = $id;
- }
- }
- return new ArrayChoiceList($choices);
- }
- /**
- * Validate submitted choices, and turn them from strings
- * (HTML option values) into other datatypes if needed
- * (not needed here since our choices are strings).
- * We're also using this place for creating new choices
- * from new values typed into the autocomplete field.
- * Required by ChoiceLoaderInterface.
- */
- public function loadChoicesForValues(array $values, $value = null)
- {
- $result = [];
- foreach ($values as $id) {
- if ($this->choiceExists($id)) {
- $result[] = $id;
- }
- }
- return $result;
- }
- /**
- * Turn choices from other datatypes into strings (HTML option
- * values) if needed - we can simply return the choices as
- * they're strings already.
- * Required by ChoiceLoaderInterface.
- */
- public function loadValuesForChoices(array $choices, $value = null)
- {
- $result = [];
- foreach ($choices as $id) {
- if ($this->choiceExists($id)) {
- $this->selected = array($id);
- $result[] = $id;
- }
- }
- return $result;
- }
- /**
- * Get first n choices
- *
- * @param string $filter
- *
- * @return array
- */
- public function getChoicesList($filter)
- {
- $params = array();
- $limit = 100;
- if ($filter !== false) {
- $params['qb-criteria'] = "";
- $params['orWhere'] = "";
- $params['externalId'] = urlencode($filter);
- $params['name'] = urlencode($filter);
- if ($this->filterTenancy) {
- $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
- }
- }
- $choices = $this->webservice->getChoices($this->webserviceParameter, $params, true, array(), $limit);
- $result = [];
- $cnt = 0;
-
- $filter = mb_strtolower($filter);
- $filter_len = mb_strlen($filter);
- foreach ($choices as $label => $id) {
- $result[$label] = $id;
- $cnt++;
- if ($cnt == $limit) {
- break;
- }
- }
- return $result;
- }
- /**
- * Get first n choices
- *
- * @param string $filter
- *
- * @return array
- */
- public function getChoicesListActive($filter)
- {
- $params = array();
- $limit = 100;
- if ($filter !== false) {
- $params['qb-criteria'] = "";
- $params['orWhere'] = "";
- $params['externalId'] = urlencode($filter);
- $params['name'] = urlencode($filter);
- if ($this->filterTenancy) {
- $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
- }
- }
- $choices = $this->webservice->getChoices('client_active', $params, true, array(), $limit);
- $result = [];
- $cnt = 0;
- $filter = mb_strtolower($filter);
- $filter_len = mb_strlen($filter);
- foreach ($choices as $label => $id) {
- $result[$label] = $id;
- $cnt++;
- if ($cnt == $limit) {
- break;
- }
- }
- return $result;
- }
- /**
- * Validate whether a choice exists
- */
- protected function choiceExists($id)
- {
- if (!is_null($id)) {
- $label = array_search($id, $this->initChoices(array("id" => $id)));
-
- return $label === false ? false : true;
- }
-
- return false;
- }
- /**
- * Get choice label
- */
- protected function getChoiceLabel($id)
- {
- $label = array_search($id, $this->initChoices(array("id" => $id)));
- return $label === false ? false : $label;
- }
- /**
- * @param array $params Contiene un array con los datos a verificar.
- * @return array
- */
- protected function initChoices($params = array())
- {
- return $this->webservice->getChoices($this->webserviceParameter, $params);
- }
- /**
- * @param mixed $id Contiene el id a buscar.
- * @return mixed Retorna el id en caso de no encontrar resultados o los datos del objeto.
- */
- public function getByIdData($id)
- {
- return $this->webservice->getByIdData($this->webserviceParameter, $id);
- }
- }
|