ClientChoiceLoader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * @param Webservice $webservice
  23. * @param TenancyService $tenancyService Contiene el servicio de tenencias.
  24. */
  25. public function __construct($webservice, $tenancyService)
  26. {
  27. $this->webservice = $webservice;
  28. $this->tenancyService = $tenancyService;
  29. }
  30. /**
  31. * @param FormBuilderInterface $builder
  32. */
  33. public function setBuilder(FormBuilderInterface $builder)
  34. {
  35. if (is_object($builder) && ($builder instanceof FormBuilderInterface)) {
  36. $builder->addEventListener(
  37. FormEvents::POST_SET_DATA, [$this, 'onFormPostSetData']
  38. );
  39. $builder->addEventListener(
  40. FormEvents::POST_SUBMIT, [$this, 'onFormPostSetData']
  41. );
  42. }
  43. }
  44. /**
  45. * Form submit event callback
  46. * Here we get notified about the submitted choices.
  47. * Remember them so we can add them in loadChoiceList().
  48. */
  49. public function onFormPostSetData(FormEvent $event)
  50. {
  51. $this->selected = [];
  52. $formdata = $event->getData();
  53. if (!is_object($formdata)) {
  54. return;
  55. }
  56. if ($formdata->getClientId()) {
  57. $this->selected = array($formdata->getClientId());
  58. }
  59. }
  60. /**
  61. * Choices to be displayed in the SELECT element.
  62. * It's okay to not return all available choices, but the
  63. * selected/submitted choices (model values) must be
  64. * included.
  65. * Required by ChoiceLoaderInterface.
  66. */
  67. public function loadChoiceList($value = null)
  68. {
  69. $choices = $this->getChoicesList(false);
  70. $missing_choices = array_flip($this->selected);
  71. foreach ($choices as $label => $id) {
  72. if (isset($missing_choices[$id])) {
  73. unset($missing_choices[$id]);
  74. }
  75. }
  76. // Now add selected choices if they're missing
  77. foreach (array_keys($missing_choices) as $id) {
  78. $label = $this->getChoiceLabel($id);
  79. if (strlen($label) === 0) {
  80. continue;
  81. }
  82. $choices[$label] = $id;
  83. }
  84. return new ArrayChoiceList($choices);
  85. }
  86. /**
  87. * Validate submitted choices, and turn them from strings
  88. * (HTML option values) into other datatypes if needed
  89. * (not needed here since our choices are strings).
  90. * We're also using this place for creating new choices
  91. * from new values typed into the autocomplete field.
  92. * Required by ChoiceLoaderInterface.
  93. */
  94. public function loadChoicesForValues(array $values, $value = null)
  95. {
  96. $result = [];
  97. foreach ($values as $id) {
  98. if ($this->choiceExists($id)) {
  99. $result[] = $id;
  100. }
  101. }
  102. return $result;
  103. }
  104. /**
  105. * Turn choices from other datatypes into strings (HTML option
  106. * values) if needed - we can simply return the choices as
  107. * they're strings already.
  108. * Required by ChoiceLoaderInterface.
  109. */
  110. public function loadValuesForChoices(array $choices, $value = null)
  111. {
  112. $result = [];
  113. foreach ($choices as $id) {
  114. if ($this->choiceExists($id)) {
  115. $result[] = $id;
  116. }
  117. }
  118. return $result;
  119. }
  120. /**
  121. * Get first n choices
  122. *
  123. * @param string $filter
  124. *
  125. * @return array
  126. */
  127. public function getChoicesList($filter)
  128. {
  129. $params = array();
  130. if ($filter !== false) {
  131. $params['name'] = urlencode($filter);
  132. $params['tenancy_id'] = $this->tenancyService->getTenancyIdCurrent();
  133. }
  134. $choices = $this->webservice->getChoices('client', $params);
  135. $result = [];
  136. $cnt = 0;
  137. $limit = 10;
  138. $filter = mb_strtolower($filter);
  139. $filter_len = mb_strlen($filter);
  140. foreach ($choices as $label => $id) {
  141. $result[$label] = $id;
  142. if (++$cnt >= $limit) {
  143. break;
  144. }
  145. }
  146. return $result;
  147. }
  148. /**
  149. * Validate whether a choice exists
  150. */
  151. protected function choiceExists($id)
  152. {
  153. $label = array_search($id, $this->initChoices());
  154. return $label === false ? false : true;
  155. }
  156. /**
  157. * Get choice label
  158. */
  159. protected function getChoiceLabel($id)
  160. {
  161. $label = array_search($id, $this->initChoices());
  162. return $label === false ? false : $label;
  163. }
  164. /**
  165. * @return array
  166. */
  167. protected function initChoices()
  168. {
  169. return $this->webservice->getChoices('client');
  170. }
  171. }