Jump to content

how can i trigger insert query in my controller with a button


jemmeli
 Share

Recommended Posts

hello,

 

I have the following code in my controller:

 

$insertData = array(
	 'app_id'  => 1,
	 'app_name'  => 'google plus',
	 'status'   => 0,
	 'title'  => 'google title'
  );

Db::getInstance()->insert("your table name", $insertData);

 

 

and in my view i have the following button :

 

<button name="avant_btn" id="avant_btn" type="button" style="width: 65px; height: 65px;" >Avant</button>

 

how to perform the add operation when click to button and thank you for advance

Share this post


Link to post
Share on other sites

add POST to the button (just send some form)

then in the init() function check if the field is in the $_POST['']

 

for example:

if (Tools::getValue('button_posted') {
do something
}

  • Like 1

Share this post


Link to post
Share on other sites

This is my controller

 

<?php




if (!defined('_PS_VERSION_')) {
exit;
}

require_once(dirname(__FILE__) . '/models/BlockPersoJs.php');


class BlockPersoAdmin extends Module {

public function __construct() {
	$this->name = 'BlockPersoAdmin';
	$this->tab = 'front_office_features';
	$this->version = '1.0.0';
	$this->author = 'jemmeli nejmeddine';
	$this->need_instance = 0;
	//$this->module_key = "";

	parent::__construct();

	$this->displayName = $this->l('Block Perso Module');
	$this->description = $this->l('Works with displayAdminProductsExtra and actionProductUpdate');

	//$directory = _PS_IMG_DIR_.'texture';

}

public function install() {

	//creation du dossier du texture
	mkdir(_PS_IMG_DIR_.'texture', 0700);

	$sql = array();

	$sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'blockpersoadmin` (
			  `id_blockpersoadmin` int(10) unsigned NOT NULL AUTO_INCREMENT,
			  `id_product` INT( 10 ) UNSIGNED NOT NULL,
			  `textarea` TEXT NOT NULL,
			  PRIMARY KEY (`id_blockpersoadmin`),
			  UNIQUE (  `id_blockpersoadmin` )
			) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';

	   $sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'blockpersoadmintexture` (
			  `id_blockpersoadmintexture` int(10)  NOT NULL AUTO_INCREMENT,
			  `id_themetexture` INT( 10 )  NOT NULL,
			  `id_product` INT( 10 )  NOT NULL,
			  `reference` varchar(255) NOT NULL,
			  `nom` varchar(255) NOT NULL,
			  `description` varchar(255) NOT NULL,
			  PRIMARY KEY (`id_blockpersoadmintexture`),
			  UNIQUE (  `id_blockpersoadmintexture` )
			) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';

	   $sql[] = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'themetexture` (
			  `id_themetexture` int(10)  NOT NULL AUTO_INCREMENT,
			  `id_blockpersoadmintexture` INT( 10 )  NOT NULL,
			  `nom` varchar(255) NOT NULL,
			  PRIMARY KEY (`id_themetexture`),
			  UNIQUE (  `id_themetexture` )
			) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';


	if (!parent::install() OR
		!$this->registerHook('backOfficeHeader')OR
		!$this->registerHook('displayHeader') OR
		!$this->registerHook('displayAdminProductsExtra') OR
		!$this->registerHook('actionProductUpdate') OR
		!$this->registerHook('displayFooterProduct') OR
		!Configuration::updateValue('sample_module_textarea', '') OR
		!$this->runSql($sql)
		/*
		!$this->registerHook('displayFooterProduct') OR
  !Configuration::updateValue('sample_module_textarea', '') OR
		!$this->runSql($sql)
		 */
	) {
		return FALSE;
	}

	return TRUE;
}



public function uninstall() {

	//supprission du dossier du texture
	//rmdir(_PS_IMG_DIR_.'texture');
	$this->recursiveRemove(_PS_IMG_DIR_.'texture');

	$sql = array();
	$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'blockpersoadmin`';
	$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'blockpersoadmintexture`';
	$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'themetexture`';


	if (!parent::uninstall() OR
		!$this->runSql($sql)
	) {
		return FALSE;
	}

	return TRUE;
}



//use of The DBQuery class
public function runSql($sql) {
	foreach ($sql as $s) {
if (!Db::getInstance()->Execute($s)){
return FALSE;
}
	}

	return TRUE;
}/*end of runSql */



// displayAdminProductsExtra : is required to output content into the tab, such as, for instance, a form to load images or some personal custom fields
public function hookDisplayAdminProductsExtra($params) {

/*	  */	
	//$cat_products = Product::getProducts((int) $this->context->language->id, 0, 0, 'id_product', 'ASC');


	$id_product = Tools::getValue('id_product');
	$images = Image::getImages($this->context->language->id,$id_product);
	//echo 'images :<br>';
	//print_r($images);
	//echo "<hr>";
	//echo 'images :<br>';
	//echo $images[1]['id_image'];



	$newImages = array();

	foreach ($images as $k => $image) {

		$img = new Image();
		$img->id = $image['id_image'];
		$newImages[$k]['image'] = $img->getExistingImgPath();


	}

	print_r($newImages);

	$this->smarty->assign('newImages', $newImages);


	////////////////////////////////////////////////////////////////////////////////////////
	//  
	//		  Here i want add my insert request as i descibed to you in the post
	//
	//////////////////////////////////////////////////////////////////////////////////////////




	$id_product = Tools::getValue('id_product');
	$sampleObj = BlockPersoJs::loadByIdProduct($id_product);
	if(!empty($sampleObj) && isset($sampleObj->id)){
		$this->context->smarty->assign(array(
			//BlockPersoAdmin_textarea is the variable to be displayed in the textarea in admin view
			'BlockPersoAdmin_textarea' => $sampleObj->textarea,
		));
	}

	return $this->display(__FILE__, 'views/admin/sample_admin.tpl');
}


/*
public function getAllpersoProductImages(){

}*/




// actionProductUpdate : is called when the product is changed, so at this moment we can fetch data from our tab and process it.
public function hookActionProductUpdate($params) {
	$id_product = Tools::getValue('id_product');
	$sampleObj = BlockPersoJs::loadByIdProduct($id_product);
	//BlockPersoJs is the name of the textarea in admin view
	$sampleObj->textarea = Tools::getValue('BlockPersoJs');
	$sampleObj->id_product = $id_product;

	if(!empty($sampleObj) && isset($sampleObj->id)){
		$sampleObj->update();
	} else {
		$sampleObj->add();
	}
}


  public function hookDisplayFooterProduct($params) {

	$id_product = Tools::getValue('id_product');
	$sampleObj = BlockPersoJs::loadByIdProduct($id_product);
	if (!empty($sampleObj) && isset($sampleObj->id)) {
		$this->context->smarty->assign(array(
			'BlockPersoAdmin_textarea' => $sampleObj->textarea,
		));
	}

	//Lets display our template file.
	return $this->display(__FILE__, 'views/frontend/sample_front.tpl');
}

// End of hookdisplayRightColumnProduct



/*
public function hookDisplayFooterProduct($params) {
	$id_product = Tools::getValue('id_product');
	$sampleObj = BlockPersoJs::loadByIdProduct($id_product);
	if(!empty($sampleObj) && isset($sampleObj->id)){
		$this->context->smarty->assign(array(
			'belvg_textarea' => $sampleObj->textarea,
		));
	}

	return $this->display(__FILE__, 'views/frontend/sample.tpl');
}

 */
public function hookBackOfficeHeader( $params ){


	//pour le css
   // $html = '<link rel="stylesheet" type="text/css" href="'.$this->_path.'css/monfichiercss.css" />';
	$jshtml = '<link rel="stylesheet" media="all" type="text/css" href="'._PS_JS_DIR_.'jquery/ui/themes/ui-lightness/jquery.ui.theme.css" />';
	$jshtml .= '<link rel="stylesheet" media="all" type="text/css" href="'._PS_JS_DIR_.'jquery/ui/themes/ui-lightness/jquery.ui.tabs.css" />';
	$jshtml .= '<link rel="stylesheet" media="all" type="text/css" href="'.$this->_path.'css/mystyle.css" />';

	//pour le javascript
	$jshtml .= '<script type="text/javascript" src="'._PS_JS_DIR_.'jquery/jquery-ui.will.be.removed.in.1.6.js"></script>';
	$jshtml .= '<script type="text/javascript" src="'._PS_JS_DIR_.'jquery/ui/jquery.ui.core.min.js"></script>';
	$jshtml .= '<script type="text/javascript" src="'._PS_JS_DIR_.'jquery/ui/jquery.ui.tabs.min.js"></script>';
	$jshtml .= '<script type="text/javascript" src="'.$this->_path.'js/bezier_generator.js"></script>';
	$jshtml .= '<script type="text/javascript" src="'.$this->_path.'js/jquery.form.js"></script>';
	$jshtml .= '<script type="text/javascript" src="'.$this->_path.'js/script.js"></script>';

	 //$smarty->assign('csshtml', $csshtml);
	 //$smarty->assign('jshtml', $jshtml);


   return $jshtml;
}/* End of hookBackOfficeHeader */



public function hookdisplayHeader($params) {

	$this->context->controller->addJS(_PS_JS_DIR_ . 'jquery/jquery-ui.will.be.removed.in.1.6.js');
	$this->context->controller->addJS(_PS_JS_DIR_ . 'jquery/ui/jquery.ui.core.min.js');
	$this->context->controller->addJS(_PS_JS_DIR_ . 'jquery/ui/jquery.ui.tabs.min.js');
	$this->context->controller->addJS($this->_path . 'js_front/bezier_generator.js');
	$this->context->controller->addJS($this->_path . 'js_front/jquery.form.js');

	$this->context->controller->addJS($this->_path . 'js_front/script.js');

	//folder
	//$smarty->assign('pattern_folder', $this->_path . 'pattern/');
	//CSS
	$this->context->controller->addCSS(_PS_JS_DIR_ . 'jquery/ui/themes/ui-lightness/jquery.ui.theme.css', 'all');
	$this->context->controller->addCSS(_PS_JS_DIR_ . 'jquery/ui/themes/ui-lightness/jquery.ui.tabs.css', 'all');
}

/* End of hookdisplayHeader */


/*** functions utility ***/

//function to delete the directory and all subfiles from it
public function recursiveRemove($dir) {
	$structure = glob(rtrim($dir, "/") . '/*');
	if (is_array($structure)) {
		foreach ($structure as $file) {
			if (is_dir($file))
				recursiveRemove($file);
			elseif (is_file($file))
				unlink($file);
		}
	}
	rmdir($dir);
}





}

Edited by jemmeli (see edit history)

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More