Pārlūkot izejas kodu

Agregados para obtener la direccion del cliente partiendo del id

gabriel 7 gadi atpakaļ
vecāks
revīzija
28cfd9db40

+ 17 - 0
Controller/WebserviceController.php

@@ -34,4 +34,21 @@ class WebserviceController extends Controller
         return $response;
     }
 
+    /**
+     * @param Request $request
+     * @return JsonResponse
+     */
+    public function getClientDataAction(Request $request)
+    {
+        $query = $request->query->get('q', '');
+        $choice_loader = $this->get('webservice.client.choiceloader');
+        $choice_list = $choice_loader->getByIdData($query);
+        $list_values = array($choice_list);
+
+        $response = new JsonResponse();
+        $response->setData(['results' => $list_values]);
+
+        return $response;
+    }
+
 }

+ 14 - 5
Form/ChoiceList/Loader/ClientChoiceLoader.php

@@ -33,7 +33,7 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
      * @var boolean
      */
     protected $filterTenancy;
-    
+
 
     /**
      * @param Webservice $webservice
@@ -46,7 +46,7 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
         $this->webserviceParameter = $webserviceParameter;
         $this->filterTenancy = $filterTenancy;
     }
-    
+
     /**
      * @param FormBuilderInterface $builder
      */
@@ -93,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]);
@@ -171,7 +171,7 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
                 $params['tenancyId'] = $this->tenancyService->getTenancyIdCurrent();
             }
         }
-        
+
         $choices = $this->webservice->getChoices($this->webserviceParameter, $params);
 
         $result = [];
@@ -219,4 +219,13 @@ class ClientChoiceLoader implements ChoiceLoaderInterface
         return $this->webservice->getChoices($this->webserviceParameter, $params);
     }
 
+    /**
+     * @param mixed $id Contiene el id a buscar.
+     * @return mixed Retorna el id en caso de no encontrar resultados o los datos del objeto.
+     */
+    public function getByIdData($id)
+    {
+        return $this->webservice->getByIdData($this->webserviceParameter, $id);
+    }
+
 }

+ 4 - 0
Resources/config/routing.yml

@@ -1,3 +1,7 @@
 ajax_client_list:
     path:      /ajax_client_list
     defaults:  { _controller: WebserviceBundle:Webservice:getClientList }
+
+ajax_client_data:
+    path:      /ajax_client_data
+    defaults:  { _controller: WebserviceBundle:Webservice:getClientData }

+ 1 - 1
Resources/translations/WebserviceBundle.es.yml

@@ -1,2 +1,2 @@
 link:
-    create_client: Crear cliente
+    create_client: Crear cliente

+ 24 - 7
Services/Webservice.php

@@ -121,7 +121,7 @@ class Webservice
             if (method_exists($token, 'getAccessToken')) {
                 $headers[] = 'Authorization: Bearer ' . $token->getAccessToken();
             }
-        } elseif (!empty($credentials) && isset($credentials['username']) && isset($credentials['password'])) {    
+        } elseif (!empty($credentials) && isset($credentials['username']) && isset($credentials['password'])) {
             $headers[] = 'Authorization: Basic ' . base64_encode($credentials['username'] . ":" . $credentials['password']);
         } else {
             return '';
@@ -155,8 +155,8 @@ class Webservice
             $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
             $data = json_decode($this->makeGetRequest($url), true);
         } catch (\Exception $ex) {
-	// TODO : Loguear esta exception o lanzarla.
-	}
+            // TODO : Loguear esta exception o lanzarla.
+        }
 
         return $data;
     }
@@ -176,7 +176,8 @@ class Webservice
         try {
             $url = $this->buildUrl($url, $filters, $order_by, $limit, $offset);
             $data = json_decode($this->makeRequest($url), true);
-        } catch (\Exception $ex) {}
+        } catch (\Exception $ex) {
+        }
 
         return $data;
     }
@@ -268,10 +269,11 @@ class Webservice
         return array("error" => "Webservice({$webservice}) not found.");
     }
 
-    public function buildUrlForId($ws,$id = null, $extra= null){
+    public function buildUrlForId($ws, $id = null, $extra = null)
+    {
         $url = $this->serviceContainer->getParameter($ws);
-        if(!is_null($id)){
-                $url = str_replace(".json", "/{$id}", $url);
+        if (!is_null($id)) {
+            $url = str_replace(".json", "/{$id}", $url);
         }
         return $url . $extra;
 
@@ -301,4 +303,19 @@ class Webservice
         return $this->httpClient;
     }
 
+    /**
+     * @param string $webservice
+     * @param int $id
+     *
+     * @return string Funcion que retorna todos los datos del objeto.
+     */
+    public function getByIdData($webservice, $id)
+    {
+        $result = $this->getArray($webservice, array(
+            'id' => $id
+        ));
+
+        return isset($result[0]) ? $result[0] : $id;
+    }
+
 }