Jump to content

I cant save my checkboxes on DB


Recommended Posts

Hi everyone!

I need a little help. I have a module where you can block some payment methods filtering by category. In the backoffice I have a beauty category list with checkbox, but when I save, the checkboxes are not saved. If I use the same code, changing only the input checkbox by a text box, it is running perfectly.

The code:

class rd_crediya_blocker extends Module
{
	public function __construct(){
		$this->name = 'rd_crediya_blocker';
    	$this->tab = 'front_office_features';
    	$this->version = '1.0';
    	$this->author = 'Raúl Duque';
    	$this->bootstrap = true;
    	$this->ps_version_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);
    	parent::__construct();
    	$this->displayName = $this->l('Bloquea el método de pago de Crediya');
    	$this->description = $this->l('Bloquea el método de pago de Crediya según parámetros.');	
	}

	public function install(){
		return (parent::install() 
			&& Configuration::updateValue('TD_CATEGORY_BLOCKED', '')
		);
	}

	public function uninstall(){
		return (parent::uninstall() 
			&& Configuration::deleteByName('TD_CATEGORY_BLOCKED', '') 
    );
	}


	public function getContent(){
		$output = '';

		if ( Tools::isSubmit('submit_td_crediya_blocker') ) {
			$td_category_blocked = Tools::getValue('td_category_blocked');
			Configuration::updateValue('TD_CATEGORY_BLOCKED', pSQL($td_category_blocked));
			$output .= $this->displayConfirmation($this->trans('Configuración Actualizada.'));
		}
		return $output.$this->displayForm();
	}

	public function displayForm(){
		// Get default language
		$defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
		$cats = getCategories();
		$categories = array();
		foreach ($cats as $key => $cat) {
			$categories[] = array('id_option' => $cat['id_category'], 'name' => $cat['name'], 'val' => $cat['id_category']);
		}
		
		// Init Fields form array
		$fieldsForm = [
			[
				'form' => [
					'legend' => [       
						'title' => $this->l('Selecciona las categorías en las que quieres bloquear la financiación Crediya'),       
						'icon' => 'icon-cogs'   
					],  
					'input' => [
						[
							'label' => $this->l('Categorías'),
							'name' => 'td_category_blocked',
							'type' => 'checkbox',
							'values' => [
								'query' => $categories,
								'id' => 'id_option',
								'name' => 'name',
							]
						],
					], 
					'submit' => [
						'title' => $this->l('Save'),
						'name' => 'submit_td_crediya_blocker',
						'class' => 'btn btn-default pull-right'
					],
				],
			],
		];
		
		$helper = new HelperForm();
		// Module, token and currentIndex
		$helper->module = $this;
		$helper->name_controller = $this->name;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
		// Language
		$helper->default_form_language = $defaultLang;
		$helper->allow_employee_form_lang = $defaultLang;
		// Title and toolbar
		$helper->title = $this->displayName;
		$helper->show_toolbar = true;        // false -> remove toolbar
		$helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
		$helper->submit_action = 'submit'.$this->name;

		// Load current value
		$helper->fields_value['td_category_blocked'] = Tools::getValue('td_category_blocked', Configuration::get('TD_CATEGORY_BLOCKED'));
		return $helper->generateForm($fieldsForm);
	}
}

function getCategories(){
	// Get categories
	$cats = Category::getCategories( (int)($cookie->id_lang), true, false  ) ;
	return $cats;
}

Thanks so much!

Link to comment
Share on other sites

Hello,

When you want to get the value, you have to iterate over the $categories and check with Tools::getValue('your-field_its-id') if it's true, and then get it.

Something like this more or less, adapt it to your format :

foreach($categories as $category) {
	if ($checkedValue = Tools::getValue('td_category_blocked' . $category['id]) {
		Configuration::updateValue('TD_CATEGORY_BLOCKED', $checkedValue);
		break;
	}
}

But it looks like you can only select one value ? Radios buttons aren't what you need rather than checkboxes ?

Link to comment
Share on other sites

Hi Tom! Many thanks!

A question: Is it necesary to modify this part with an other loop too?

public function install(){
		return (parent::install() 
			&& Configuration::updateValue('TD_CATEGORY_BLOCKED', '')
		);
	}

	

Regards

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...