guifi·net GitLab devops

Commit 088fae4b authored by Daniel's avatar Daniel
Browse files

US-025 attemp

Showing with 253 additions and 0 deletions
+253 -0
_core:
default_config_hash: 3YUV4RQQ4k8drO7uzYJ7lNc5Az0iDAH5YW8KbZVxjeY
use_cdn: true
cdn_base_url: 'https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/'
validate_all_ajax_forms: 1
force_validate_on_blur: 1
_core:
default_config_hash: R4IF-ClDHXxblLcG0L7MgsLvfBIMAvi_skumNFQwkDc
module:
webform_clientside_validation: -1
action: 0
address: 0
admin_toolbar: 0
......@@ -18,6 +19,9 @@ module:
calendar: 0
calendar_datetime: 0
ckeditor: 0
clientside_validation: 0
clientside_validation_demo: 0
clientside_validation_jquery: 0
color: 0
comment: 0
conditional_fields: 0
......@@ -36,6 +40,7 @@ module:
eck: 0
editor: 0
emergya: 0
emergya_postal_code: 0
entity: 0
entity_extra_field: 0
entity_extra_field_ui: 0
......
......@@ -47,6 +47,10 @@ elements: |-
visible:
':input[name="is_this_a_new_location_"]':
value: New
required:
':input[name="location_address[postal_code]"]':
value:
'!pattern': '^(?:0[1-9]|[1-4]\d|5[0-2])\d{3}$'
'#default_value':
country_code: ES
langcode: ''
......@@ -68,6 +72,27 @@ elements: |-
additionalName: hidden
familyName: hidden
organization: hidden
postalCode: required
'#equal_stepwise_validate': 0
'#equal_components':
0: 0
codigo_postal: 0
organization: 0
vat: 0
service_type: 0
'#compare': 0
'#compare_components': ''
'#compare_components_operator': ''
'#compare_components_custom_error': ''
'#some_of_several': 0
'#some_of_several_components':
0: 0
codigo_postal: 0
organization: 0
vat: 0
service_type: 0
'#some_of_several_components_completed': ''
'#some_of_several_final_validation': 0
coordinates:
'#type': webform_gmap_field
'#title': Coordinates
......@@ -76,6 +101,26 @@ elements: |-
visible-slide:
':input[name="is_this_a_new_location_"]':
value: New
postal_code:
'#type': textfield
'#title': 'Postal Code'
'#pattern': '^(?:0[1-9]|[1-4]\d|5[0-2])\d{3}$'
'#required': true
province:
'#type': textfield
'#title': Province
'#disabled': true
'#required': true
town:
'#type': textfield
'#title': Town
'#required': true
'#wrapper_attributes':
id: town
town_results:
'#type': webform_markup
'#wrapper_attributes':
id: town-results
contact_page:
'#type': webform_wizard_page
'#title': Contact
......
function _getIneValues($CodProvincia, $codMunicipio = '') {
if (empty($codMunicipio)) {
$url = "https://public.opendatasoft.com//api/records/1.0/search/?dataset=georef-spain-provincia&q=&sort=prov_code&facet=prov_name&refine.prov_code=".$CodProvincia;
$nameProvincia = _callApiIne($url,'prov_name');
return $nameProvincia;
}
else {
// $codMun= $CodProvincia.$codMunicipio;
$url = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=georef-spain-municipio&q=&sort=mun_code&facet=mun_name&refine.mun_code=".$codMunicipio;
$nameMuncipio = _callApiIne($url,'mun_name');
return $nameMuncipio;
}
}
function _callApiIne ($endpoint,$field_name) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: f4194f23-3e69-fdf3-c688-31c273fe1b64"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if (empty($err)) {
$param = \Drupal\Component\Serialization\Json::decode($response);
if (isset($param['records'][0]['fields'][$field_name])) {
$name = $param['records'][0]['fields'][$field_name];
if (!empty($name)) {
return $name;
}
}
}
curl_close($curl);
return '';
}
$ajax = [
'callback' => 'checkWeekNumberDates',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper' => 'week-dates',
'progress' => [
'type' => 'throbber',
'message' => t('Verifying...'),
],
];
$form['elements']['container_request']['my_job_general_request']['description_week']['#ajax'] = $ajax;
function checkWeekNumberDates(&$form, FormStateInterface $form_state, $form_id) {
$response = new AjaxResponse();
$values = $form_state->getValues();
// $weekNumber = $form_state->get('description_week');
$weekNumber = $values['description_week'];
$year = date('Y');
$dto = new DateTime();
$dto->setISODate($year, $weekNumber);
$form['elements']['container_request']['my_job_general_request']['wrapper_date']['dated']['#value'] = $dto->format('d/m/Y');
$elem = $form['elements']['container_request']['my_job_general_request']['wrapper_date']['dated'];
$response->addCommand(new ReplaceCommand('.start-date', $elem));
$dto->modify('+6 days');
$form['elements']['container_request']['my_job_general_request']['wrapper_date']['date_to']['#value'] = $dto->format('d/m/Y');
$elem = $form['elements']['container_request']['my_job_general_request']['wrapper_date']['date_to'];
$response->addCommand(new ReplaceCommand('.finish-date', $elem));
return $response;
}
name: Emergya Postal Code
type: module
description: 'Module to check CP via API'
package: Custom
core: 8.x
core_version_requirement: ^8 || ^9
<?php
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormStateInterface;
function emergya_postal_code_form_webform_submission_create_drop_order_add_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Modification for the form with the given BASE_FORM_ID goes here. For
// example, if BASE_FORM_ID is "node_form", this code would run on every
// node form, regardless of node type.
// Add a checkbox to the node form about agreeing to terms of use.
// webform_submission_create_drop_order_add_form
/*
$cp = $form_state->getValue('postal_code');
$servicio = \Drupal::service('emergya_postal_code.api_postal_code');
$proName = $servicio->getProvince('41');
var_dump($proName);
*/
$ajax = [
'callback' => 'callApiProvince',
'event' => 'change',
'wrapper' => 'wapper-province',
'progress' => [
'type' => 'throbber',
'message' => t('Verifying...'),
],
];
$ajaxTown = [
'callback' => 'callApiTown',
'event' => 'change',
'wrapper' => 'town',
'progress' => [
'type' => 'throbber',
'message' => t('Verifying...'),
],
];
$form['elements']['location_page']['postal_code']['#ajax'] = $ajax;
$form['elements']['location_page']['province']['#prefix'] = '<div id="wapper-province">';
$form['elements']['location_page']['province']['#suffix'] = '</div>';
$form['elements']['location_page']['town']['#ajax'] = $ajaxTown;
}
function callApiProvince(&$form, FormStateInterface $form_state, $form_id) {
$service = \Drupal::service('emergya_postal_code.api_postal_code');
$cp = substr($form_state->getValue('postal_code'), 0, 2);
$nameProvince = $service->getProvince($cp);
$form['elements']['location_page']['province']['#value'] = $nameProvince;
return $form['elements']['location_page']['province'];
}
function callApiTown(&$form, FormStateInterface $form_state, $form_id) {
$service = \Drupal::service('emergya_postal_code.api_postal_code');
$cp = $form_state->getValue('town');
//$nameProvince = $service->getProvince($cp);
$output = "<div id='town-results'>MUNICIPIO NO VALIDO</div>";
if (!empty($output)) {
// si es error devuelvo vacio y un mensaje
$form['elements']['location_page']['town']['#value'] = '';
Drupal::messenger()->addError('municipio no válido');
}
return $form['elements']['location_page']['town'];
}
services:
emergya_postal_code.api_postal_code:
class: Drupal\emergya_postal_code\Services\OpenData
arguments: ['@http_client']
<?php
namespace Drupal\emergya_postal_code\Services;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\ClientInterface;
class OpenData
{
/**
* @var ClientInterface
*/
protected $httpClient;
/**
*
* @param ClientInterface $http_client
*
*/
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
}
public function getProvince($cp){
$url = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=georef-spain-provincia&q=&rows=1&sort=prov_code&facet=prov_name&refine.prov_code=" . $cp;
$request = $this->httpClient->request('GET', $url);
if ($request->getStatusCode() == 200){
$body = $request->getBody();
$param = Json::decode($body);
return $param['records'][0]['fields']['prov_name'];
}
return '';
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment