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); } }