Thứ Ba, 2 tháng 8, 2011

How to retrieve data in the database - Prestashop

Hi!
I will show you how to retrieve data in the database in back office - Prestashop.
1. The first, create table :


CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'table01(
  id_table01 int(11) NOT NULL AUTO_INCREMENT,
  name varchar(200) NULL,
  PRIMARY KEY (id_table01)
)';

2. add write following code your file admintab.

$this->fieldsDisplay(
    'id_table01' => array('title' => $this->l('ID'), 'align'=>'center', 'width'=>25),
   'name' => array('title' => $this->('ID'), 'width'=>400)
);

3. example

<?php
require_once dirname(__FILE__).'/../../classes/AdminTab.php';
class Adminexample extends AdminTab
{
    function __construct()
   {

        $this->fieldsDisplay(
           'id_table01' => array('title' => $this->l('ID'), 'align'=>'center', 'width'=>25),
           'name' => array('title' => $this->('ID'), 'width'=>400)
        );       
       parent::__construct();
   }
}


Thank you for time. Please feel free to ask any question and feedback.

Create Module Prestashop Hello World

hi!
Today, i show you how to create module prestashop hello world.
1.First create folder "helloworld", then create file helloworld.php.
2.Write following code in your helloworld.php file:
<?php
class Helloworld extends Module
{
  /*is invoked automatically when a class is called. I've added my comments below.*/
     function __construct()
    {
        $this->name = 'helloworld';
$this->tab = 'Blocks';
  $this->version = 1.0;
  parent::__construct(); // The parent construct is required for translations
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Block Hello world');
$this->description = $this->l('Module Hello world');
    }
    /*And what about install() function? Well this is called by prestashop framework. It checks that your module is installed or not, you can modify hook names here. Please note that you have to write a function respective to your hooks defined and you are allowed to add multiple hooks*/
    function install()
    {
if (!parent::install())
           return false;
        if (!$this->registerHook('leftColumn'))
           return false;
        return true;
    }


/**
* Returns module content
*
* @param array $params Parameters
* @return string Content
*/
/*This is last step. We've defined registerHook('leftColumn'), so we've written following function. This method is called by front office to check your content path/variable/smarty etc.*/
    function hookLeftColumn($params)
    {
return $this->display(__FILE__, 'helloworld.tpl');
    }
}

3. Create file helloworld.tpl.
Write following code in your file helloworld.tpl:
<h2>Hello world</h2>


Thank you for your time. Please feel free to ask any question and feedback.

How to add tab in back office - Prestashop

Hi!
I will show you how to add tab in back office - Prestashop.
1. create folder 'addtabexample'.
2. create file 'addtabexample.php' and write following code in this file:
<?php
class Addtabexample extends Module
{

function __construct()
{
$this->name = 'addtabexample';
$this->displayName = $this->l('Add tab example');
$this->description = $this->l('How to add tab in back office - prestashop');
$this->version = '1.0';
$this->tab = 'front_office_features';

parent::__construct();
}

function install()
{
if (!parent::install()||!installModuleTab('AdminAddtabexample', 'Tab example', 2))
return false;
return true;
        }
        function uninstall()
        {
               if (!parent::unistall()||!uninstallModuleTab('AdminAddtabexample'))
                   return false;
               return true;
        }

        // add new tab

function installModuleTab($tabClass, $tabName, $id_parent)
{
$tab = new Tab();

$langs = Language::getLanguages();
foreach ($langs as $lang)
{
$tab->name[$lang['id_lang']] = $tabName;
}

$tab->class_name = $tabClass;
$tab->module = $this->name;
$tab->id_parent = $id_parent;

if (!$tab->save())
return false;

return true;
}
// delete tab
function uninstallModuleTab($tabClass)
{
$idTab = Tab::getIdFromClassName($tabClass);
if($idTab != 0)
{
  $tab = new Tab($idTab);
  $tab->delete();
  return true;
}
return false;
}


}

3. create file AdminAddtabexample.php
<?php
require_once dirname(__FILE__).'/../../classes/AdminTab.php';
class AdminAddtabexample extends AdminTab
{
    function display()
   {
        echo 'Add Tab';
   }
}

Thank you for your time, Please feel free to ask any question and feedback.

How to create textarea editor - Prestashop.

Hi!
Today, I show you how to create textarea editor - Prestashop.
You ínsert this code your file:
 <?php

global $cookie;
$iso = Language::getIsoById((int)($cookie->id_lang));
$isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en');
$ad = dirname($_SERVER["PHP_SELF"]);
echo '
<script type="text/javascript">
var iso = \''.$isoTinyMCE.'\' ;
var pathCSS = \''._THEME_CSS_DIR_.'\' ;
var ad = \''.$ad.'\' ;
</script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
<script type="text/javascript">
toggleVirtualProduct(getE(\'is_virtual_good\'));
unitPriceWithTax(\'unit\');</script>';

 And create following code textarea editor your file:
<textarea class="rte"></textarea>


ur file: Thank you for your time. Please feel free to ask any question adn feedback.

How to add Capcha to your form

Hi!
Today, I show you how to add capcha to your form.
1. Create file CaptchaSecurityImages.php and Write following code in this file:

<?php
session_start();

class CaptchaSecurityImages {


var $font = 'arial.ttf';


function generateCode($characters) {
/* list all possible characters, similar looking characters and vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) { 
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}


function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.55;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 25, 25, 25);
$text_color = imagecolorallocate($image, 255, 255, 255);
$noise_color = imagecolorallocate($image, 10, 120, 18);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}


}


$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';


$captcha = new CaptchaSecurityImages($width,$height,$characters);


?>


2. in your form:


<?php
if (isset($_REQUEST['submitAddQuestion']))
{
// capcha
session_start();
if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) 
{
// Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
                        echo 'Success';
unset($_SESSION['security_code']);
} else 
{
// Insert your code for showing an error message here
echo '
<script type="text/javascript">
alert("Sorry, you have provided an invalid security code!");
</script>
';

                       echo ' 
                               <form action="" method="post">
                               Security Code<br/>
<input class="input-text" style="height:19px;width:155px;" name="security_code" type="text" size="10"/>&nbsp;&nbsp;
<img class="images" border="0px" src="CaptchaSecurityImages.php?width=65&height=20&characters=4" /><br /><br/>
                             <input type="submit" name="submitAddQuestion" value="sent" />
                            </form>

}
}
?>


Thank you for your time. Please feel free to ask any question and feedback.