Magento using core_config_data table

Core_config_data is a magento table used to save configuration and setting. It’s really helpfull and easy to use :

save or update a new entry :

$data = new Mage_Core_Model_Config();
$data->saveConfig('adin/ssid', 'value', 'default', 0);

and retrieve saved value :

$value = Mage::getConfig()->getNode('default/adin/ssid');

You may need to specify scope and scope code to retrieve the value

$value = Mage::getConfig()->getNode('default/sogen/file_number', 'default', 0 );

Update or create a value inside the installer (data, no sql)

  $installer->setConfigData('customer/address/prefix_show', 0);

In a setup file

 $installer->setConfigData('your/path', 'value', 'stores', 1);
 $installer->deleteConfigData('your/path', 'stores');

Magento how to make a helper (and use it)

Creating a helper is quite easy, go on your /etc module directory and edit your config.xml file
ex : app/code/local/Adin/Epub/etc/config.xml
Add a block inside the <global> and after </blocks>

<?xml version="1.0"?>
<config>
	<modules>
		<Adin_Epub>
			<version>1.0.0</version>
		</Adin_Epub>
	</modules>
	
	<global>
		<helpers>
			<epub>
				<class>Adin_Epub_Helper</class>
			</epub>
		</helpers>
	</global>
	
</config>

Then creates the Helper folder on your module directory and create a data class :
app/code/local/Adin/Epub/Helper/Data.php

<?php
class Adin_Epub_Helper_Data extends Mage_Core_Helper_Abstract
{
    //fonction de test
    public function getTest()
    {
        return "Ceci est un test de helper";
    }	
}

If you create a new module, don’t forget to activate it, for that, in app/etc/modules directory, create or append a xml file and add your new module, for example :

<?xml version="1.0"?>
<config>
    <modules>
        <Adin_Epub>
            <active>true</active>
            <codePool>local</codePool>
        </Adin_Epub>
    </modules>
</config>

Now, for using your new helper, just instanciate it and call the function :

$helper = Mage::helper('epub');
echo $helper->getTest();

How to enter line break into mailto body html link

You are writing a html mailto link and fill the body section but you don’t know how to enter line break ?
You need to read the RFC 2368. It tell that line breaks must be encoded using the %0D%0A sequence.

<a href="mailto:?subject=wordpress&body=First%20line%0D%0ASecond%20line" >send mail</a>

Yes, white spaces should also be replaced by %20.

Another way to do it is to use php function, plus, it will convert all your special characters

<?php
$body=" Bonjour,

Ceci est un test de mail avec caractères spéciaux";
$body = rawurlencode($body);
?>
<a href="mailto:?subject=Question concernant un message client&body=<?php echo $body; ?>" class="mail">Partager par mail</a>

fpdf import pdf file

You are using fpdf to generate PDF file in php. You want to use an existing PDF file to import header and footer ? This is easy :

First, download FPDI lib here

and do that on your code :

require_once('fpdf.php'); 
require_once('fpdi.php'); 

$pdf = new FPDI();   // FPDI and not FPDF
$pdf->AliasNbPages();
$pdf->AddPage();
$pagecount = $pdf->setSourceFile(SF_ROOT_DIR.'/lib/fpdf/courrier.pdf');
$tplix = $pdf->importPage(1);
$pdf->useTemplate($tplix);
$pdf->Output('newdocument.pdf', 'D'); 

If you get this error

Warning: require_once(fpdf_tpl.php): failed to open stream:

you need to get the FPDF_TPL class too.

This only import file for the first page, if you want it on every page, override AddPage() function :

public function AddPage($orientation='', $size='', $keepmargins=false, $tocpage=false)
{
    parent::AddPage();
    $pagecount = $this->setSourceFile(SF_ROOT_DIR.'/lib/fpdf/courrier.pdf');                                                          
    $tplix = $this->importPage(1);
    $this->useTemplate($tplix);
}

Symfony validator custom error message

If you need to customize your validator error messages, you can use the setMessage() function.
On your Form class (ex: /lib/form/doctrine/DemandeForm.class.php)

//sfValidatorInteger
$this->validatorSchema['produit_ean']->setMessage('invalid', 'ean should be integer');
$this->validatorSchema['produit_ean']->setMessage('min', 'ean must be at least %min%');
$this->validatorSchema['produit_ean']->setMessage('max', 'ean must be at least %max%');

//sfValidatorDate
$this->validatorSchema['date']->setMessage('bad_format', 'Format must be dd/mm/YYYY');
$this->validatorSchema['date']->setMessage('max', 'Date should be after than 01/01/2013');
$this->validatorSchema['date']->setMessage('min', 'Date should be before than 01/02/2013');

//required
$this->validatorSchema['date']->setMessage('required', 'Date is mandatory');

Symfony2 twig add new function file_exists

An important function is missing on twig : file_exists.

I will show you here how to define a new function for twig.

On your namespace/bundle, add a Twig/Extension directory and create your class file :
/src/Adin/ArlogisBundle/Twig/Extension/FileExistsExtension.php

<?php

namespace Adin\ArlogisBundle\Twig\Extension;

class FileExistsExtension extends \twig_Extension
{
        /**
         *Return the function registered as twig extension
         *
         *@return array
         */
        public function getFunctions()
        {
                return array(
                        'file_exists' => new \Twig_Function_Function('file_exists'),
                        );
        }

        public function getName()
        {
                return 'adin_file_exists';
        }
}
?>

You then have to register your service, add a in your /Adin/ArlogisBundle/Ressources/config/services.xml file

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
       <service id="adin.twig.tripix_extension" class="Adin\ArlogisBundle\Twig\Extension\FileExistsExtension">
        <tag name="twig.extension" />
        </service>
    </services>
</container>

It’s ready, you can use it on your twig template

{% if file_exists('/var/www/image.jpg') %}
    File exists
{% else %}
    File not exists
{% endif %}

Be carefull, you need to specify the absolute path of the file. You can create a twig global variable root_path.
app/config/config.yml

# Twig Configuration
twig:
    globals:
        root_path: %kernel.root_dir%/../web

Then on twig template

{% if file_exists({{root_path}}'/var/www/image.jpg') %}
    File exists
{% else %}
    File not exists
{% endif %}

Robots.txt disallow all

To prevent google to reference your dev website, you can add a robots.txt file to your web root directory and fill it with

User-agent: *
Disallow: /

The “User-agent: *” means that the file applies to all robots. The “Disallow: /” means the robot will not visit any pages on the website.

Prestashop disable smarty cache

Smarty cache is great for server performance on production but is annoying on developpement.
Hopefully, you can enable or disable it.

On prestashop 1.3.x or less, you have to edit the root/config/smarty.config.php and edit these 2 lines

$smarty->caching        = false;   // to pass "true" when put into production
$smarty->force_compile  = true;    // to pass "false" when put into production

On prestashop 1.4.x or more, you can do it directly on the backoffice, preference tab, performences subtab