sparhawk Posted October 13, 2013 Share Posted October 13, 2013 Hello everybody, I want to ask you for advice. I'm trying to add the module switching field - two options Yes a No. Do databáze by měli podle volby ukládat Yes - 1 No - 0. On source code i added: public function viewAccess($disable = false) { $result = true; return $result; } function install() { if (parent::install() == false) { return false; } if (!Configuration::updateValue('MY_STATUS', (int)Tools::getValue('choose_my_status'))) return false; if (!$id_tab) { $tab = new Tab(); $tab->class_name = $this->tabClassName; $tab->id_parent = Tab::getIdFromClassName($this->tabParentName); $tab->module = $this->name; $languages = Language::getLanguages(); foreach ($languages as $language) $tab->name[$language['id_lang']] = $this->displayName; $tab->add(); $this->installConfig(); $this->installConfig2(); $this->pdfshopname(); } return true; } <div style="clear: both; padding-top:15px;"> <label>'.$this->l('Choose status:').'</label> <div class="margin-form" style="padding-top:5px;"> <select id="choose_my_status" name="choose_my_status"> <option value="1" '.(Tools::getValue('choose_my_status', Configuration::get('MY_STATUS')) ? 'selected="selected"' : '').'>'.$this->l('Yes').'</option> <option value="0" '.(Tools::getValue('choose_my_status', Configuration::get('MY_STATUS')) ? 'selected="selected"' : '').'>'.$this->l('No').'</option> </select> </div> </div> <div align="center" style="margin-top: 20px;"> <input type="submit" value=" ' . $this->l('Save') . ' " name="submitMYPOKUS" class="button" /> if (Tools::isSubmit('submitMYPOKUS')) { Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); $this->_html .= $this->displayConfirmation($this->l('Settings updated')); } } Switching fields in the administration previewed the database also stores the value of the option of either Yes - 1 or No - 0. Only if you select Yes, and I will save, and the button switches to No and in the database is stored right value 1. I ask you the source code correctly? Why not stay for example in choosing the No option but to switch to the Yes ? Thank you in advance for your advice Link to comment Share on other sites More sharing options...
vekia Posted October 13, 2013 Share Posted October 13, 2013 to "no" option you should use different condition: <option value="0" '.(Configuration::get('MY_STATUS')==0 ? 'selected="selected"' : '').'>'.$this->l('No').'</option> Link to comment Share on other sites More sharing options...
sparhawk Posted October 13, 2013 Author Share Posted October 13, 2013 (edited) Hi Vekia, thanks for the answer. When you select in backoffice Yes or No that option remains permanently on NO. Do not know why ? Thi code is correctly ? function install() { if (parent::install() == false) { return false; } if (!Configuration::updateValue('MY_STATUS', (int)Tools::getValue('choose_my_status'))) return false; if (Tools::isSubmit('submitMYPOKUS')) { Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); $this->_html .= $this->displayConfirmation($this->l('Settings updated')); } } Edited October 13, 2013 by sparhawk (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted October 13, 2013 Share Posted October 13, 2013 Configuration::get('MY_STATUS') - this is field with your status, or field to verify which option is selected? Link to comment Share on other sites More sharing options...
sparhawk Posted October 13, 2013 Author Share Posted October 13, 2013 (edited) I wanted to add an existing module switching field with the options Yes and NO, which saved into the database values 1 or 0. I found this code that I added to the module. In the database is created ps_configuration MY_STATUS where the option of stores a value of 0 or 1. In BO by clicking on Yes and save switch OVERFLOW is to No but the database is stored correct value 1. In BO by clicking on No and save switch OVERFLOW is to Yes but the database is stored correct value 0. Now after adjustment, and any choice is still choice NO. It should not be the first choice of code like this? <option value="1" '.(Configuration::get('MY_STATUS')==1 ? 'selected="selected"' : '').'>'.$this->l('Yes').'</option> In the field MY_STATUS to save selected status 1 or 0. Configuration::get('MY_STATUS') - this is field with my status. Edited October 13, 2013 by sparhawk (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted October 13, 2013 Share Posted October 13, 2013 <option value="1" '.(Configuration::get('MY_STATUS')==1 ? 'selected="selected"' : '').'>'.$this->l('Yes').'</option> <option value="0" '.(Configuration::get('MY_STATUS')==0 ? 'selected="selected"' : '').'>'.$this->l('No').'</option> this is correct code. moreover, in what function you've got this: if (Tools::isSubmit('submitMYPOKUS')) { Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); $this->_html .= $this->displayConfirmation($this->l('Settings updated')); } it's a part of getContent() ? Link to comment Share on other sites More sharing options...
sparhawk Posted October 13, 2013 Author Share Posted October 13, 2013 (edited) Hi Vekia, thanks for the answer. It should be the choice No: <option value="0" or <option value="1" ? <option value="0" '.(Configuration::get('MY_STATUS')==0 ? 'selected="selected"' : '').'>'.$this->l('No').'</option> This is the beginning of the file php public function viewAccess($disable = false) { $result = true; return $result; } function install() { if (parent::install() == false) { return false; } if (!Configuration::updateValue('MYPOKUS_NAME', 'John Malinski')) return false; if (!Configuration::updateValue('MY_STATUS', (int)Tools::getValue('choose_my_status'))) return false; if (!$id_tab) { $tab = new Tab(); $tab->class_name = $this->tabClassName; $tab->id_parent = Tab::getIdFromClassName($this->tabParentName); $tab->module = $this->name; $languages = Language::getLanguages(); foreach ($languages as $language) $tab->name[$language['id_lang']] = $this->displayName; $tab->add(); $this->installConfig(); $this->installConfig2(); $this->pdfshopname(); } return true; } Thi is the middle the php file public function renderForm() { $this->_image_name = Configuration::get('MY_STAMP'); if (!empty($_POST)) $this->_postProcess(); $this->_html .= ' <form method="post" action="" enctype="multipart/form-data"> <fieldset><legend><img src="../img/admin/appearance.gif" /> ' . $this->l('Settings') . '</legend> <div style="clear: both; padding-top:15px;"> <label>' . $this->l('Name:') . '</label> <div class="margin-form" style="padding-top:5px;"> <input type="text" size="50" name="MYPOKUS_pole1" value="' . Configuration::get('MYPOKUS_NAME') . '" /> <p style="clear:both">' . $this->l('Name') . '</p> </div> </div> <div style="clear: both; padding-top:15px;"> <label>'.$this->l('Choose status:').'</label> <div class="margin-form" style="padding-top:5px;"> <select id="choose_my_status" name="choose_my_status"> <option value="1" '.(Configuration::get('MY_STATUS')==1 ? 'selected="selected"' : '').'>'.$this->l('Yes').'</option> <option value="1" '.(Configuration::get('MY_STATUS')==0 ? 'selected="selected"' : '').'>'.$this->l('No').'</option> </select> </div> </div> This is the end of file php private function _postProcess() { $imageInput = 'MY_imagefile'; if (isset($_FILES[$imageInput]['tmp_name']) AND !empty($_FILES[$imageInput]['tmp_name'])) { // Delete old image if (file_exists($this->_image_name)) @unlink($this->_image_name); if (is_uploaded_file($_FILES[$imageInput]['tmp_name'])) { move_uploaded_file($_FILES[$imageInput]['tmp_name'], _PS_ROOT_DIR_ . '/modules/' . $this->name . '/img/' . $_FILES[$imageInput]['name']); Configuration::updateValue('MY_STAMP', $_FILES[$imageInput]['name']); } } if (Tools::isSubmit('submitMYPOKUS')) { Configuration::updateValue('MYPOKUS_NAME', $_POST['MYPOKUS_pole1']); Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); $this->_html .= $this->displayConfirmation($this->l('Settings updated')); } } Edited October 13, 2013 by sparhawk (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted October 13, 2013 Share Posted October 13, 2013 yes of course there should be value="0" Link to comment Share on other sites More sharing options...
sparhawk Posted October 13, 2013 Author Share Posted October 13, 2013 (edited) Hi Vekia, Great thanks Now everything works Would I just wanted to ask if the code is correct ? in function public function viewAccess($disable = false) { if (!Configuration::updateValue('MY_STATUS', (int)Tools::getValue('choose_my_status'))) return false; It has to be here: if (!Configuration::updateValue('MY_STATUS', '1')) return false; beginning of the php file public function viewAccess($disable = false) { $result = true; return $result; } function install() { if (parent::install() == false) { return false; } if (!Configuration::updateValue('MYPOKUS_NAME', 'John Malinski')) return false; if (!Configuration::updateValue('MY_STATUS', (int)Tools::getValue('choose_my_status'))) return false; And code on the end of the php file Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); It has to be here: Configuration::updateValue('MY_STATUS', $_POST['MYPOKUS_pole2']); private function _postProcess() { $imageInput = 'MY_imagefile'; if (isset($_FILES[$imageInput]['tmp_name']) AND !empty($_FILES[$imageInput]['tmp_name'])) { // Delete old image if (file_exists($this->_image_name)) @unlink($this->_image_name); if (is_uploaded_file($_FILES[$imageInput]['tmp_name'])) { move_uploaded_file($_FILES[$imageInput]['tmp_name'], _PS_ROOT_DIR_ . '/modules/' . $this->name . '/img/' . $_FILES[$imageInput]['name']); Configuration::updateValue('MY_STAMP', $_FILES[$imageInput]['name']); } } if (Tools::isSubmit('submitMYPOKUS')) { Configuration::updateValue('MYPOKUS_NAME', $_POST['MYPOKUS_pole1']); Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); In the function on begin php file public function viewAccess($disable = false) { and function private function _postProcess() { on the end php file have the same row Configuration::updateValue('MY_STATUS', Tools::getValue('choose_my_status')); This code is correct ? It is all right and the end of the row ? Tools::getValue('choose_my_status') parameter 'choose_my_status' is not unnecessary ? Thank you very much for the quick help Edited October 14, 2013 by sparhawk (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts