ClientChoiceLoader.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 = array();
  80. if (is_array($this->selected) and !empty($this->selected)) {
  81. $choices = $this->getChoicesList(false);
  82. $missing_choices = array_flip($this->selected);
  83. foreach ($choices as $label => $id) {
  84. if (isset($missing_choices[$id])) {
  85. unset($missing_choices[$id]);
  86. }
  87. }
  88. // Now add selected choices if they're missing
  89. foreach (array_keys($missing_choices) as $id) {
  90. $label = $this->getChoiceLabel($id);
  91. if (strlen($label) === 0) {
  92. continue;
  93. }
  94. $choices[$label] = $id;
  95. }
  96. }
  97. return new ArrayChoiceList($choices);
  98. }
  99. /**
  100. * Validate submitted choices, and turn them from strings
  101. * (HTML option values) into other datatypes if needed
  102. * (not needed here since our choices are strings).
  103. * We're also using this place for creating new choices
  104. * from new values typed into the autocomplete field.
  105. * Required by ChoiceLoaderInterface.
  106. */
  107. public function loadChoicesForValues(array $values, $value = null)
  108. {
  109. $result = [];
  110. foreach ($values as $id) {
  111. if ($this->choiceExists($id)) {
  112. $result[] = $id;
  113. }
  114. }
  115. return $result;
  116. }
  117. /**
  118. * Turn choices from other datatypes into strings (HTML option
  119. * values) if needed - we can simply return the choices as
  120. * they're strings already.
  121. * Required by ChoiceLoaderInterface.
  122. */
  123. public function loadValuesForChoices(array $choices, $value = null)
  124. {
  125. $result = [];
  126. foreach ($choices as $id) {
  127. if ($this->choiceExists($id)) {
  128. $this->selected = array($id);
  129. $result[] = $id;
  130. }
  131. }
  132. return $result;
  133. }
  134. /**
  135. * Get first n choices
  136. *
  137. * @param string $filter
  138. *
  139. * @return array
  140. */
  141. public function getChoicesList($filter)
  142. {
  143. $params = array();
  144. $limit = 100;
  145. if ($filter !== false) {
  146. $params['qb-criteria'] = "";
  147. $params['orWhere'] = "";
  148. $params['externalId'] = urlencode($filter);
  149. $params['name'] = urlencode($filter);
  150. if ($this->filterTenancy) {
  151. $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
  152. }
  153. }
  154. $choices = $this->webservice->getChoices($this->webserviceParameter, $params, true, array(), $limit);
  155. $result = [];
  156. $cnt = 0;
  157. $filter = mb_strtolower($filter);
  158. $filter_len = mb_strlen($filter);
  159. foreach ($choices as $label => $id) {
  160. $result[$label] = $id;
  161. $cnt++;
  162. if ($cnt == $limit) {
  163. break;
  164. }
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Get first n choices
  170. *
  171. * @param string $filter
  172. *
  173. * @return array
  174. */
  175. public function getChoicesListActive($filter)
  176. {
  177. $params = array();
  178. $limit = 100;
  179. if ($filter !== false) {
  180. $params['qb-criteria'] = "";
  181. $params['orWhere'] = "";
  182. $params['externalId'] = urlencode($filter);
  183. $params['name'] = urlencode($filter);
  184. if ($this->filterTenancy) {
  185. $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
  186. }
  187. }
  188. $choices = $this->webservice->getChoices('client_active', $params, true, array(), $limit);
  189. $result = [];
  190. $cnt = 0;
  191. $filter = mb_strtolower($filter);
  192. $filter_len = mb_strlen($filter);
  193. foreach ($choices as $label => $id) {
  194. $result[$label] = $id;
  195. $cnt++;
  196. if ($cnt == $limit) {
  197. break;
  198. }
  199. }
  200. return $result;
  201. }
  202. /**
  203. * Validate whether a choice exists
  204. */
  205. protected function choiceExists($id)
  206. {
  207. if (!is_null($id)) {
  208. $label = array_search($id, $this->initChoices(array("id" => $id)));
  209. return $label === false ? false : true;
  210. }
  211. return false;
  212. }
  213. /**
  214. * Get choice label
  215. */
  216. protected function getChoiceLabel($id)
  217. {
  218. $label = array_search($id, $this->initChoices(array("id" => $id)));
  219. return $label === false ? false : $label;
  220. }
  221. /**
  222. * @param array $params Contiene un array con los datos a verificar.
  223. * @return array
  224. */
  225. protected function initChoices($params = array())
  226. {
  227. return $this->webservice->getChoices($this->webserviceParameter, $params);
  228. }
  229. /**
  230. * @param mixed $id Contiene el id a buscar.
  231. * @return mixed Retorna el id en caso de no encontrar resultados o los datos del objeto.
  232. */
  233. public function getByIdData($id)
  234. {
  235. return $this->webservice->getByIdData($this->webserviceParameter, $id);
  236. }
  237. }