Explorar el Código

Refactory client widget template. Fix client choice loader

Guillermo Espinoza hace 7 años
padre
commit
b52ce2ca48

+ 24 - 8
Form/ChoiceList/Loader/ClientChoiceLoader.php

@@ -24,17 +24,29 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
     // Currently selected choices
     protected $selected = [];
 
+    /**
+     * @var string
+     */
+    protected $webserviceParameter;
+
+    /**
+     * @var boolean
+     */
+    protected $filterTenancy;
+    
 
     /**
      * @param Webservice $webservice
      * @param TenancyService $tenancyService Contiene el servicio de tenencias.
      */
-    public function __construct($webservice, $tenancyService)
+    public function __construct($webservice, $tenancyService, $webserviceParameter = 'client', $filterTenancy = true)
     {
         $this->webservice = $webservice;
         $this->tenancyService = $tenancyService;
+        $this->webserviceParameter = $webserviceParameter;
+        $this->filterTenancy = $filterTenancy;
     }
-
+    
     /**
      * @param FormBuilderInterface $builder
      */
@@ -81,9 +93,9 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
     public function loadChoiceList($value = null)
     {
         $choices = $this->getChoicesList(false);
-
+        
         $missing_choices = array_flip($this->selected);
-
+        
         foreach ($choices as $label => $id) {
             if (isset($missing_choices[$id])) {
                 unset($missing_choices[$id]);
@@ -135,6 +147,7 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
         $result = [];
         foreach ($choices as $id) {
             if ($this->choiceExists($id)) {
+                $this->selected = array($id);
                 $result[] = $id;
             }
         }
@@ -154,9 +167,12 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
         $params = array();
         if ($filter !== false) {
             $params['name'] = urlencode($filter);
-            $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
+            if ($this->filterTenancy) {
+                $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
+            }
         }
-        $choices = $this->webservice->getChoices('client', $params);
+        
+        $choices = $this->webservice->getChoices($this->webserviceParameter, $params);
 
         $result = [];
         $cnt = 0;
@@ -189,7 +205,7 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
      */
     protected function getChoiceLabel($id)
     {
-        $label = array_search($id, $this->initChoices());
+        $label = array_search($id, $this->initChoices(array("id" => $id)));
 
         return $label === false ? false : $label;
     }
@@ -200,7 +216,7 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
      */
     protected function initChoices($params = array())
     {
-        return $this->webservice->getChoices('client', $params);
+        return $this->webservice->getChoices($this->webserviceParameter, $params);
     }
 
 }

+ 16 - 3
Resources/views/Type/remote_client_widget.html.twig

@@ -24,10 +24,18 @@
                     p: params.page
                 };
             };
-
-            $('select[id$="clientId"]').select2({
+            
+        {% if id is not defined %}
+            {% set id = 'clientId' %}
+        {% endif %}
+            
+        {% if ajax_url is not defined %}
+            {% set ajax_url = 'ajax_client_list' %}
+        {% endif %}
+            
+            $('select[id$="{{ id }}"]').select2({
                 ajax: {
-                    url: '{{ path('ajax_client_list') }}',
+                    url: '{{ path(ajax_url) }}',
                     dataType: 'json',
                     delay: 250,
                     data: getAjaxListData,
@@ -43,6 +51,10 @@
             <div style="width: 50%; float: left;">
                 {{ block('choice_widget') }}
             </div>
+            {% if create is not defined %}
+                {% set create = true %}
+            {% endif %}
+            {% if create %}
             {% set url = remote_client_url() %}
             {% if url != '#' %}
             <div style="width: 50%; float: left;">
@@ -51,6 +63,7 @@
                 </a>
             </div>
             {% endif %}
+            {% endif %}
         </div>
     {% endspaceless %}
 {% endblock %}