Created
January 2, 2020 04:02
-
-
Save IsaacVe/702fb21c4516675023efd3a0145df764 to your computer and use it in GitHub Desktop.
Trying to validate a new field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 2007-2020 PrestaShop | |
* | |
* NOTICE OF LICENSE | |
* | |
* This source file is subject to the Academic Free License (AFL 3.0) | |
* that is bundled with this package in the file LICENSE.txt. | |
* It is also available through the world-wide-web at this URL: | |
* http://opensource.org/licenses/afl-3.0.php | |
* If you did not receive a copy of the license and are unable to | |
* obtain it through the world-wide-web, please send an email | |
* to license@prestashop.com so we can send you a copy immediately. | |
* | |
* DISCLAIMER | |
* | |
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | |
* versions in the future. If you wish to customize PrestaShop for your | |
* needs please refer to http://www.prestashop.com for more information. | |
* | |
* @author PrestaShop SA <contact@prestashop.com> | |
* @copyright 2007-2020 PrestaShop SA | |
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) | |
* International Registered Trademark & Property of PrestaShop SA | |
*/ | |
if (!defined('_PS_VERSION_')) { | |
exit; | |
} | |
class FormularioCliente extends Module | |
{ | |
public function __construct() | |
{ | |
$this->name = 'formulariocliente'; | |
$this->tab = 'checkout'; | |
$this->version = '1.0.0'; | |
$this->author = 'Javier Isaac Velásquez Venegas'; | |
$this->need_instance = 0; | |
/** | |
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6) | |
*/ | |
$this->bootstrap = true; | |
parent::__construct(); | |
$this->displayName = $this->l('formulariocliente'); | |
$this->description = $this->l('Se agrega los campos de identificación y el cuadro seleccionado'); | |
$this->confirmUninstall = $this->l('¿Estas seguro de querer desinstalar el módulo?'); | |
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); | |
} | |
/** | |
* Don't forget to create update methods if needed: | |
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update | |
*/ | |
public function install() | |
{ | |
include(dirname(__FILE__).'/sql/install.php'); | |
return parent::install() && | |
$this->registerHook('additionalCustomerFormFields') && | |
$this->registerHook('actionObjectCustomerAddAfter') && | |
$this->registerHook('actionObjectCustomerUpdateAfter') && | |
$this->registerHook('validateCustomerFormFields'); | |
} | |
public function uninstall() | |
{ | |
include(dirname(__FILE__).'/sql/uninstall.php'); | |
return parent::uninstall(); | |
} | |
public function hookAdditionalCustomerFormFields($params) | |
{ | |
$documentos = $this->listarTipoDocumento(); | |
$format = [ | |
'tipodocumento' => (new FormField()) | |
->setName('tipodocumento') | |
->setType('select') | |
->setRequired(true) | |
->setLabel($this->l('Tipo de Documento')), | |
'documento' => (new FormField()) | |
->setName('documento') | |
->setType('text') | |
->setRequired(true) | |
->setLabel($this->l('Documento')), | |
]; | |
foreach ($documentos as $documento) { | |
$format['tipodocumento']->addAvailableValue( | |
$documento['id_tipodocumento'], | |
$documento['descripcion'] | |
); | |
} | |
if (isset($params['cookie']->id_customer)) { | |
$documento = Db::getInstance()->getValue( | |
'select `documento` from `' . _DB_PREFIX_. 'documentocliente` where id_customer = ' | |
. (int) $params['cookie']->id_customer | |
); | |
$tipodocumento = Db::getInstance()->getValue( | |
'select `id_tipodocumento` from `' . _DB_PREFIX_. 'documentocliente` where id_customer = ' | |
. (int) $params['cookie']->id_customer | |
); | |
$format['documento']->setValue($documento); | |
$format['tipodocumento']->setValue($tipodocumento); | |
} | |
$position = (int) array_search('email', array_keys($params['fields']), null) + 1; | |
$fieldcount = count($params['fields']); | |
$result = array_merge( | |
array_slice($params['fields'], 0, $position), | |
$format, | |
array_slice($params['fields'], $position - $fieldcount) | |
); | |
$params['fields'] = $result; | |
} | |
public function hookActionObjectCustomerAddAfter($params) | |
{ | |
/* Place your code here. */ | |
$this->actualizarDocumento($params); | |
} | |
public function hookValidateCustomerFormFields($params) | |
{ | |
foreach ($params['fields'] as $field) { | |
if ($field->getName() == 'documento') { | |
$field->setErrors(array($this->l('Test Error'))); | |
} | |
} | |
return $params['fields']; | |
} | |
public function hookActionObjectCustomerUpdateAfter($params) | |
{ | |
/* Place your code here. */ | |
$this->actualizarDocumento($params); | |
} | |
private function actualizarDocumento($params) | |
{ | |
$idCliente = $params['object']->id; | |
$tipodocumento = Tools::getValue('tipodocumento'); | |
$documento = Tools::getValue('documento'); | |
return Db::getInstance()->insert( | |
'documentocliente', | |
[ | |
'documento' => pSQL($documento), | |
'id_tipodocumento' => (int) $tipodocumento, | |
'id_customer' => (int) $idCliente, | |
], | |
false, | |
true, | |
Db::REPLACE | |
); | |
} | |
private function listarTipoDocumento() | |
{ | |
return Db::getInstance()->executeS('select `id_tipodocumento`, `descripcion` from `' . _DB_PREFIX_. 'tipodocumento`'); | |
} | |
private function obtenerDocumento($idCliente) | |
{ | |
return Db::getInstance()->getValue( | |
'select `documento` from `' . _DB_PREFIX_. 'documentocliente` where id_customer = ' | |
. (int) $idCliente | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment