Prestashop复选框检查

时间:2017-01-02 11:46:18

标签: prestashop

我想知道如何检查模块页面中的复选框 我的代码是: -

if (!defined('_PS_VERSION_')) {
exit;
}
class ExportOrders extends Module
{

    protected $config_form = false;
    protected $_html = '';
    protected $_postErrors = array();

    public function __construct()
    {
    $this->name = 'exportorders';
    $this->tab = 'export';
    $this->version = '1.0.1';
    $this->author = 'Decathlon Sports India Limited';
    $this->need_instance = 1;

    /**
    * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
    */
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('ExportOrders');
    $this->description = $this->l('generating xml for exportorder based on manual and automation process');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall the module');
}

/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
    if (!parent::install() OR ! $this->alterTable('add') OR ! $this->registerHook('actionAdminControllerSetMedia') OR ! $this->registerHook('actionProductUpdate') OR ! $this->registerHook('displayAdminProductsExtra'))
    return false;
    return true;
}

public function uninstall()
{
    if (!parent::uninstall() OR ! $this->alterTable('remove'))
    return false;
    return true;
}

public function alterTable($method)
{
    switch ($method) {
    case 'add':
    $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'order_history ADD `export_orders` int NOT NULL';
    break;

    case 'remove':
    $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'order_history DROP COLUMN `export_orders`';
    break;
}

if (!Db::getInstance()->Execute($sql))
return false;
return true;
}

/**
* Load the configuration form
*/
public function prepareNewTab()
{

    $this->context->smarty->assign(array(
    'custom_field' => '',
    'languages' => $this->context->controller->_languages,
    'default_language' => (int)Configuration::get('PS_LANG_DEFAULT')
    ));

}
/**
* Validating the settings form
*/
protected function _postValidation()
{

    if (Tools::isSubmit('submitexportordersModule'))
    {
    // if (!Tools::getValue('Order Id'))
    //     $this->_postErrors[] = $this->l('Order Id is mandatory.');    
    if (!Tools::getValue('EXPORTORDERS_LOCATION'))                
    $this->_postErrors[] = $this->l('Export Location is required.');


}
}

public function getContent()
{
    /**
    * If values have been submitted in the form, process.
    */
    if (((bool)Tools::isSubmit('submitexportordersModule')) == true){ 

    $this->_postValidation();
    if (!count($this->_postErrors))
    $this->postProcess();
    else
    foreach ($this->_postErrors as $err)
    $this->_html .= $this->displayError($err);
}
else
$this->_html .= '<br />';

$this->_html .= $this->renderForm();

return $this->_html;

$this->context->smarty->assign('module_dir', $this->_path);

$output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
return $output.$this->renderForm();
}

/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
    $helper = new HelperForm();            

    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $helper->module = $this;
    $helper->default_form_language = $this->context->language->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitexportordersModule';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
    .'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');

    $helper->tpl_vars = array(
    'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
    'languages' => $this->context->controller->getLanguages(),
    'id_language' => $this->context->language->id,
    );

    $helper->fields_value[OrderID] = true;
    return $helper->generateForm(array($this->getConfigForm()));
}

/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
    return array(
    'form' => array(
    'legend' => array(
    'title' => $this->l('Settings'),
    'icon' => 'icon-cogs',
    ),
    'input' => array(

    array(
    'type' => 'checkbox',
    'label' => $this->l('Select Required Options'),                        
    // 'required' => true,
    'values' => array(
    'query' => array(
    array(
    'id' => 'AllFields',
    'name' => $this->l('All Fields'),                                
    'val' => 'All', 
    'class' => 'check_all',
    ),
    array(
    'id' => 'OrderID',  
    'name' => $this->l('Order Id'),                                
    'val' => 1,
    'required' => true,                                    
    'class' => 'order_id',
    ), array(
    'id' => 'InvoiceNo',
    'name' => $this->l('Invoice No'),
    'val' => 1,
    ), array(
    'id' => 'ShippedDate',
    'name' => $this->l('Shipped Date'),
    'val' => 1,
    ), array(
    'id' => 'PaymentMethod',
    'name' => $this->l('Payment Method'),
    'val' => 1,
    ), array(
    'id' => 'TransportCost',
    'name' => $this->l('Transport Cost'),
    'val' => 1,
    ), array(
    'id' => 'OrderStatus',
    'name' => $this->l('Order Status'),
    'val' => 1,
    ), array(
    'id' => 'MyDecathlonId',
    'name' => $this->l('Decathlon Id'),
    'val' => 1,
    ), array(
    'id' => 'FirstName',
    'name' => $this->l('First Name'),
    'val' => 1,
    ), array(
    'id' => 'LastName',
    'name' => $this->l('Last Name'),
    'val' => 1,
    ), array(
    'id' => 'Address1',
    'name' => $this->l('Address1'),
    'val' => 1,
    ), array(
    'id' => 'Address2',
    'name' => $this->l('Address2'),
    'val' => 1,                                   
    'id' => 'State',
    'name' => $this->l(' State'),
    'val' =>1,
    ), array(
    'id' => 'ZipCode',
    'name' => $this->l(' ZipCode'),
    'val' =>1,
    ), array(
    'id' => 'Phone',
    'name' => $this->l('Phone'),
    'val' => 1,
    ), array(
    'id' => 'Email',
    'name' => $this->l('Email'),
    'val' => 1,
    ), array(
    'id' => 'Item',
    'name' => $this->l('Item'),
    'val' => 1,
    ), array(
    'id' => 'QuantityOrdered',
    'name' => $this->l('Quantity Ordered'),
    'val' => 1,
    ), array(
    'id' => 'QuantityReturned',
    'name' => $this->l('Quantity Returned'),
    'val' => 1,
    ), array(
    'id' => 'GrossAmount',
    'name' => $this->l('Gross Amount'),
    'val' => 1,
    ), array(
    'id' => 'NetAmount',
    'name' => $this->l('Net Amount'),
    'val' => 1,
    ), array(
    'id' => 'TaxRate',
    'name' => $this->l('Tax Rate'),
    'val' => 1,
    ), array(
    'id' => 'Currency',
    'name' => $this->l('Currency'),
    'val' => 1,                                    
    ),
    ),
    'id' => 'id',
    'name' => 'name'
    ),
    ),


    array(
    'type' => 'text',
    'name' => 'EXPORTORDERS_LOCATION',
    'label' => $this->l('Export Location'),
    'required' => true
    ),
    ),
    'submit' => array(
    'title' => $this->l('Save'),
    ),
    ),
    );

}

/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
    return array(
    'EXPORTORDERS_LOCATION' => Configuration::get('EXPORTORDERS_LOCATION'),

    );
}


/**
* Save form data.
*/
protected function postProcess()
{
    //Initialise arrays and values
    $form_values = $this->getConfigFormValues();
    $_POST['module_name'] = 'export_orders';

    $arr = array_map('utf8_encode', $_POST);
    $test = json_encode($arr);       
    $insertData = array(
    'name'  => $_POST['module_name'], 
    'date_add'  => date('Y-m-d H:i:s'), 
    'value'  => $test

    );
    $updateData = array(   
    'date_upd'  => date('Y-m-d H:i:s'),      
    'value'  => $test

    );

    //Fetch from configuration table
    $sql = 'SELECT * FROM '._DB_PREFIX_.'configuration WHERE name = "export_orders"';

    if ($row = Db::getInstance()->getRow($sql))
    $decoded_values = json_decode($row['value']);

    if($decoded_values == ""){

    Db::getInstance()->insert("configuration", $insertData);

}
else 

Db::getInstance()->update('configuration', $updateData, 'name = "export_orders"');
foreach (array_keys($form_values) as $key) {
// Configuration::updateValue($key, Tools::getValue($key));

}
}

/**
* Add the CSS & JavaScript files you want to be loaded in the BO.
*/
public function hookBackOfficeHeader()
{
    if (Tools::getValue('module_name') == $this->name) {
    $this->context->controller->addJS($this->_path.'views/js/back.js');
    $this->context->controller->addJS($this->_path.'views/js/back_checkall.js');
    $this->context->controller->addCSS($this->_path.'views/css/back.css');
}
}

/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader()
{
    $this->context->controller->addJS($this->_path.'/views/js/front.js');
    $this->context->controller->addCSS($this->_path.'/views/css/front.css');
}

public function hookDisplayBackOfficeHeader()
{
    /* Place your code here. */
}

public function hookDisplayBackOfficeHome()
{
    /* Place your code here. */
}
}

还想知道我如何选择all来选择所有复选框。我已经尝试了很多成功,请帮帮我

我曾尝试过检查复选框,但它没有正确

2 个答案:

答案 0 :(得分:2)

您需要设置$fields_value数组,以便默认情况下选中复选框。

$helper = new HelperForm();
// set all form fields here...

$helper->fields_value[id_checkbox] = true;

id_checkbox替换为ID为复选框。

确保表单在POST时出现错误,您可以在表单提交前设置用户选择的复选框值,否则表单将始终选中复选框。

修改

好的,表单助手使用复选框nameProperty_checkboxId创建复选框名称。由于您没有定义名称属性,因此复选框的名称和标识前缀为_

请删除$helper->fields_value[id_checkbox] = true;行并将getConfigValues()方法修改为:

/**
 * Set values for the inputs.
 */
protected function getConfigFormValues()
{
    return array(
        'EXPORTORDERS_LOCATION' => Configuration::get('EXPORTORDERS_LOCATION'),
        '_OrderID' => true
    );
}

现在,每次加载表单时都会检查您的复选框Order ID。如前所述,修改它以处理表单提交,保存的设置等。

答案 1 :(得分:1)

您可以使用以下代码通过PrestaShop帮助程序生成一组复选框:

为包含标签的复选框数创建一个数组。

$picking_history = array(
                               array(
                                   'check_id' => '1',
                                   'name' => $this->l('Customer Login Page'),
                               ),
                               array(
                                   'check_id' => '2',
                                   'name' => $this->l('Customer Registration Page'),
                               ),
                               array(
                                   'check_id' => '3',
                                   'name' => $this->l('Customer Contact Page'),
                               ),
                               array(
                                   'check_id' => '4',
                                   'name' => $this->l('Customer Forgotton Password Page'),
                               ),
                               array(
                                   'check_id' => '5',
                                   'name' => $this->l('Back Office Login Page'),
                               ),
                               array(
                                   'check_id' => '6',
                                   'name' => $this->l('Back Office Forgotton Password Page'),
                               ),
                           );

将以下代码添加到帮助程序表单代码中,以创建一组复选框。

array(
                       'type' => 'checkbox',
                       'label' => $this->l('Enable Google ReCaptcha for:'),
                       'hint' => $this->l('Please select the position of the Google Recaptcha.'),
                       'name' => 'google_recaptcha_check',
                       'values' => array(
                           'query' => $picking_history,
                           'id' => 'check_id',
                           'name' => 'name',
                           'desc' => $this->l('Please select atleast one position to enable plugin.'),
                       )
                   )