ClientChoiceLoader.php 4.9 KB

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