Child pages
  • Overriding default behaviors

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of content

Table of Contents
maxLevel3

...

Overriding default behaviors

...

The solution is to use the following override of the MySQLCore class:

Code Block

<?php
class MySQL extends MySQLCore
{
	public function __construct($server, $user, $password, $database, $newlink = false)
	{
		$this->_server = $server;
		$this->_user = $user;
		$this->_password = $password;
		$this->_type = _DB_TYPE_;
		$this->_database = $database;

		$this->connect($newlink);
	}
	
	public function connect($newlink = false)
	{
		if (!defined('_PS_DEBUG_SQL_'))
			define('_PS_DEBUG_SQL_', false);

		if ($this->_link = mysql_connect($this->_server, $this->_user, $this->_password, $newlink) )
		{
			if (!$this->set_db($this->_database))
				die(Tools::displayError('The database selection cannot be made.'));
		}
		else
			die(Tools::displayError('Link to database cannot be established.'));

		/* UTF-8 support */
		if (!mysql_query('SET NAMES \'utf8\'', $this->_link))
			die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
		return $this->_link;
	}
}
?>

...

The last parameter forces the creation of a MySQL connection.

Example 2

Code Block

/*
 * Create a cron task to make periodical database backup
*/
class AdminTab extends AdminTabCore{
	public function ajaxProcess()
	{			 
		// Here we call the same thing as if we did the old way
		// + with "if": maybe we want to limit its use to only adding backup
		// note: find yourself a way to get the file link if you want to send it by mail!
		if (isset($_REQUEST['addbackup']))
			return $this->postProcess();
	} 

	public function displayAjax()
	{
		if (sizeof($this->_errors) > 0)
		{
			// handle errors 
			// for example, send mail with all error msg
			$content = '';
			foreach($this->_errors as $errorMsg)
			 $content .= $errorMsg;

			 $lang = Configuration::get('PS_LANG_DEFAULT');
			 // here we send a mail to give the result of the process
			 // notice: you have to create template mails files
			 Mail::Send($lang, 'backuptaskdone', '[autobackup] report backup error', array('backup_link'=>)), $to); 
		}
		else
		{
			// no error, but maybe we want a mail ?
			if(Configuration::get('PS_NOTICE_SUCCEED_BACKUP')) 
			{
				// fileAttachment available, see 9th param of Send() method in classes/Mail.php
				// + we can add a condition "if (Configuration::get('PS_AUTOBACKUP_SEND_FILE'))"
				Mail::Send($lang, 'backuptaskerror', '[autobackup] report backup error', array('vars to use in tpl'), $to); 
			}
		}
	return true;
	}
}
Code Block

/*
 * This override allows you to use ajax-tab.php to make any admin action.
 * Use it for crontask for example
*/
class AdminTab extends AdminTabCore {
	public function ajaxProcess()
	{
		return $this->postProcess();
	}
}

Example 3

Code Block

/*
 * With this override, you have a new Smarty variable called "currentController" available in header.tpl
 * This allows you to use a different header if you are on a product page, category page or home.
 */
class FrontController extends FrontControllerCore {
	public function displayHeader()
	{
		self::$smarty->assign('currentController', get_class($this));
		return parent::displayHeader();
	}
}