ClientChoiceLoader.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace WebserviceBundle\Form\ChoiceList\Loader;
  3. use Base\AdminBundle\Controller\TenancyService;
  4. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  5. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. class ClientChoiceLoader implements ChoiceLoaderInterface
  10. {
  11. /**
  12. * @var Webservice
  13. */
  14. protected $webservice;
  15. /**
  16. * @var TenancyService
  17. */
  18. protected $tenancyService;
  19. // Currently selected choices
  20. protected $selected = [];
  21. /**
  22. * @var string
  23. */
  24. protected $webserviceParameter;
  25. /**
  26. * @var boolean
  27. */
  28. protected $filterTenancy;
  29. /**
  30. * @param Webservice $webservice
  31. * @param TenancyService $tenancyService Contiene el servicio de tenencias.
  32. */
  33. public function __construct($webservice, $tenancyService, $webserviceParameter = 'client', $filterTenancy = true)
  34. {
  35. $this->webservice = $webservice;
  36. $this->tenancyService = $tenancyService;
  37. $this->webserviceParameter = $webserviceParameter;
  38. $this->filterTenancy = $filterTenancy;
  39. }
  40. /**
  41. * @param FormBuilderInterface $builder
  42. */
  43. public function setBuilder(FormBuilderInterface $builder)
  44. {
  45. if (is_object($builder) && ($builder instanceof FormBuilderInterface)) {
  46. $builder->addEventListener(
  47. FormEvents::POST_SET_DATA, [$this, 'onFormPostSetData']
  48. );
  49. $builder->addEventListener(
  50. FormEvents::POST_SUBMIT, [$this, 'onFormPostSetData']
  51. );
  52. }
  53. }
  54. /**
  55. * Form submit event callback
  56. * Here we get notified about the submitted choices.
  57. * Remember them so we can add them in loadChoiceList().
  58. */
  59. public function onFormPostSetData(FormEvent $event)
  60. {
  61. $this->selected = [];
  62. $formdata = $event->getData();
  63. if (!is_object($formdata)) {
  64. return;
  65. }
  66. if ($formdata->getClientId()) {
  67. $this->selected = array($formdata->getClientId());
  68. }
  69. }
  70. /**
  71. * Choices to be displayed in the SELECT element.
  72. * It's okay to not return all available choices, but the
  73. * selected/submitted choices (model values) must be
  74. * included.
  75. * Required by ChoiceLoaderInterface.
  76. */
  77. public function loadChoiceList($value = null)
  78. {
  79. $choices = $this->getChoicesList(false);
  80. $missing_choices = array_flip($this->selected);
  81. foreach ($choices as $label => $id) {
  82. if (isset($missing_choices[$id])) {
  83. unset($missing_choices[$id]);
  84. }
  85. }
  86. // Now add selected choices if they're missing
  87. foreach (array_keys($missing_choices) as $id) {
  88. $label = $this->getChoiceLabel($id);
  89. if (strlen($label) === 0) {
  90. continue;
  91. }
  92. $choices[$label] = $id;
  93. }
  94. return new ArrayChoiceList($choices);
  95. }
  96. /**
  97. * Validate submitted choices, and turn them from strings
  98. * (HTML option values) into other datatypes if needed
  99. * (not needed here since our choices are strings).
  100. * We're also using this place for creating new choices
  101. * from new values typed into the autocomplete field.
  102. * Required by ChoiceLoaderInterface.
  103. */
  104. public function loadChoicesForValues(array $values, $value = null)
  105. {
  106. $result = [];
  107. foreach ($values as $id) {
  108. if ($this->choiceExists($id)) {
  109. $result[] = $id;
  110. }
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Turn choices from other datatypes into strings (HTML option
  116. * values) if needed - we can simply return the choices as
  117. * they're strings already.
  118. * Required by ChoiceLoaderInterface.
  119. */
  120. public function loadValuesForChoices(array $choices, $value = null)
  121. {
  122. $result = [];
  123. foreach ($choices as $id) {
  124. if ($this->choiceExists($id)) {
  125. $this->selected = array($id);
  126. $result[] = $id;
  127. }
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Get first n choices
  133. *
  134. * @param string $filter
  135. *
  136. * @return array
  137. */
  138. public function getChoicesList($filter)
  139. {
  140. $params = array();
  141. if ($filter !== false) {
  142. $params['name'] = urlencode($filter);
  143. if ($this->filterTenancy) {
  144. $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
  145. }
  146. }
  147. $choices = $this->webservice->getChoices($this->webserviceParameter, $params);
  148. $result = [];
  149. $cnt = 0;
  150. $limit = 10;
  151. $filter = mb_strtolower($filter);
  152. $filter_len = mb_strlen($filter);
  153. foreach ($choices as $label => $id) {
  154. $result[$label] = $id;
  155. if (++$cnt >= $limit) {
  156. break;
  157. }
  158. }
  159. return $result;
  160. }
  161. /**
  162. * Validate whether a choice exists
  163. */
  164. protected function choiceExists($id)
  165. {
  166. $label = array_search($id, $this->initChoices(array("id" => $id)));
  167. return $label === false ? false : true;
  168. }
  169. /**
  170. * Get choice label
  171. */
  172. protected function getChoiceLabel($id)
  173. {
  174. $label = array_search($id, $this->initChoices(array("id" => $id)));
  175. return $label === false ? false : $label;
  176. }
  177. /**
  178. * @param array $params Contiene un array con los datos a verificar.
  179. * @return array
  180. */
  181. protected function initChoices($params = array())
  182. {
  183. return $this->webservice->getChoices($this->webserviceParameter, $params);
  184. }
  185. /**
  186. * @param mixed $id Contiene el id a buscar.
  187. * @return mixed Retorna el id en caso de no encontrar resultados o los datos del objeto.
  188. */
  189. public function getByIdData($id)
  190. {
  191. return $this->webservice->getByIdData($this->webserviceParameter, $id);
  192. }
  193. }