2017-01-02 327 views
2

我想知道如何在模塊頁面複選框可以進行檢查 我的代碼是: -的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. */ 
} 
} 

也想知道我怎麼可以讓全選來選擇所有的複選框。我已經試過很多着成功,請大家幫我

我曾試圖複選框被選中,但沒有得到正確的

+0

我有絕對的Prestashop沒有經驗,但由於這只是一個PHP關聯數組:你嘗試過'=> TRUE'而不是「檢查」'檢查「(因爲這不是一個布爾值)? – indextwo

+0

#indextwo不,不適用 –

回答

2

您可以使用下面的代碼通過的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.'), 
         ) 
        ) 
+0

#Knowband那麼它將如何檢查?並選擇? –

1

您需要設置$fields_value陣列使複選框被默認選中。

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

$helper->fields_value[id_checkbox] = true; 

用複選框的ID替換id_checkbox

確保當表單POST'ed與您所設置的複選框值作爲用戶提交表單之前選擇的錯誤,否則表單總是有複選框選中。

編輯:

好吧,表單輔助使用複選框nameProperty_checkboxId創建複選框的名字。由於您沒有定義名稱屬性,所以複選框的名稱和ID的前綴爲_

所以刪除$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將檢查每次加載窗體。正如前面所說,修改此來處理表單提交,保存設置等

+0

#TheDrot沒有它不工作:( –

+0

@Tijojoel然後你正在做的事情是錯誤的。我們展示的形式呈現的整個代碼,否則我們無法幫助您進一步。同時告訴我們您所使用的的Prestashop版本。 – TheDrot

+0

我將粘貼整個代碼 –