SIDEBAR
»
S
I
D
E
B
A
R
«
Magento Banklink version 1.4.1 released
Nov 11th, 2009 by Sven

We released Magento Banklink (Pangalink) version 1.4.1 today.

Main change is that it can be selected as a payment method for the orders that are created in the admin on behalf of a customer. The module has a new field in the admin also – sort order. So it can be ordered amongst other payment methods.

Can’t login to Magento admin?
Oct 11th, 2009 by Sven

This is a simple tip that could help you in case when you cannot login to Magento Admin after database migration.

Sometimes it happens that you need to move your whole Magento database from one site to another. Let’s say you want to copy your production database to your testsite. Everything goes well, you copy all data but you cannot login to admin of your test Magento… You use correct admin password, the same that you use for the production site. Still nothing…

OK, here are the clear steps to check before you start. You need to check and change a few fields in Magento configuration that is stored in database:

in table “core_config_data” check the following fields:

web/unsecure/base_url – set this to your testsite URL
web/secure/base_url – set this to your testsite URL
web/cookie/cookie_domain (this one got me once… :) ) – better set it to empty
web/cookie/cookie_path – better set it to empty

After that remove cache – delete var/cache folder.

Please leave us a comment if this post helped you or if you have anything to add here.

Important change to Magento Pangalink (Banklink) module configuration
Sep 28th, 2009 by Sven

Important! In Banklink configuration there is field “New order status”. Set this to Pending (module versions 1.3.6 and up)! Older installations of banklink module may have incorrect values there.

Another issue is that Banklink module name is now configurable in every store and view. It means that you can set different names for the module in the different views (languages).

Banklink title configurable!
Sep 12th, 2009 by Sven

Today we released an update to Magento Banklink module. Current version number is 1.3.4. This updates contains important and essential improvement of configuration. The title of the module is freely configurable now in different stores and views.

Banklink title is configurable

Banklink title is configurable

Fast and good-looking templates in PHP
Aug 14th, 2009 by Sven

This is the second article in serie of articles about using PHP in a better way than an average script-kiddie. You want to be better than an average, right?;) Then read on…

In my house no one likes spaghetti when it’s in code. That’s why we at Wasabi developed a way to separate presentation from content and code already hmm… how many years ago. I don’t know. I know that buzzword of MVC was not around yet… :) However – since we don’t need yet another mambo-jambo-fancy templating language then we need to use minimum set of code in templates also. So, let’s start.

It uses best parts of PHP like dynamic including and output buffering.

First Model-View-Controller a.k.a. MVC. It’s good, it’s easy but don’t take it like it would be the “final solution” or like it would solve all your problems. It won’t.

First let’s create a simple controller:

<?php
/**
 * Controller is invoked when web server gets a request.
 * For simplicity's sake we don't implement
 * fancy URL-to-controller-and-action mapping logic
 * This is usually done by our framework PHP-X.
 *
 * @author Sven Varkel
 */
class Controller
{
    /**
     * This is the initial action
     * that will be called by default
     *
     * @return string
     */
    public function defaultAction()
    {
        /*
         * Let's create model. This holds
         * our data. Model can be used to load data
         * from database or anywhere else.
         */
        $model = new Model();
        $model->setTitle('Mr');
        $model->setAge(33);
        $model->setFirstName('Sven');
        $model->setLastName('Varkel');
        $model->addHobby('Music');
        $model->addHobby('Fishing');
        $model->addHobby('Cooking');

        /*
         * Here we instantiate View
         * that loads a view
         * and then fills the template with data
         * that comes from the prepared
         * model. After that we render
         * the view to a wanted format.
         * In our case the format is HTML
         * but it can be PDF, XML, WML ...
         */
        $view = new View('person');
        $view->setModel($model);
        $renderedView = $view->render();

        /*
         * Finally we return rendered view
         * for output
         */
        return $renderedView;
    }
}
//EOF Controller.php

Then we create a Model …

<?php
/**
 * Model is a structure that
 * holds our data. Model or model helper
 * can be used to pull and push data from/to a data source
 * (e.g database)
 * It can also include operations for data manipulation.
 * Just to keep things clear and easy-to-understand I recommend
 * to NOT put too much logic inside a model. Use helpers for that.
 *
 * @author Sven Varkel
 */
class Model
{
    private $title = null;
    private $age = 0;
    private $firstName = null;
    private $lastName = null;
    private $hobbyList = array();

    /**
     * Gets Title
     *
     * @return Title
     */
    public function getTitle(){
        return $this->title;
    }
    /**
     * Sets Title
     *
     * @param Title
     */
    public function setTitle($value){
        $this->title = $value;
    }
    /**
     * Gets Age
     *
     * @return Age
     */
    public function getAge(){
        return $this->age;
    }
    /**
     * Sets Age
     *
     * @param Age
     */
    public function setAge($value){
        $this->age = $value;
    }
    /**
     * Gets FirstName
     *
     * @return FirstName
     */
    public function getFirstName(){
        return $this->firstName;
    }
    /**
     * Sets FirstName
     *
     * @param FirstName
     */
    public function setFirstName($value){
        $this->firstName = $value;
    }
    /**
     * Gets LastName
     *
     * @return LastName
     */
    public function getLastName(){
        return $this->lastName;
    }
    /**
     * Sets LastName
     *
     * @param LastName
     */
    public function setLastName($value){
        $this->lastName = $value;
    }
    /**
     * Gets HobbyList
     *
     * @return HobbyList
     */
    public function getHobbyList(){
        return $this->hobbyList;
    }
    /**
     * Sets HobbyList
     *
     * @param HobbyList
     */
    public function setHobbyList($value){
        $this->hobbyList = $value;
    }
    /**
     * Adds hobby with name $aHobby
     * to the list of hobbies
     *
     * @param $aHobby
     * @return
     */
    public function addHobby($aHobby){
        $this->hobbyList[] = $aHobby;
    }
    /**
     * In this override helper we
     * create getter names
     *
     * @param $theName
     * @return mixed
     */
    public function __get($theName){
        $getterName = sprintf('get%s', $theName);
        return $this->$getterName();
    }
}
//EOF Model.php

Then we create a View…

<?php
/**
 * This is the view that will
 * be rendered using a template
 *
 * @author Sven Varkel
 */
class View
{
    private $name = null;
    private $path = null;
    private $model = null;
    /**
     *
     * @param $theName
     * @return
     */
    public function __construct($theName){
        $this->setName($theName);
    }
    /**
     * Gets Name
     *
     * @return Name
     */
    public function getName(){
        return $this->name;
    }
    /**
     * Sets Name
     *
     * @param Name
     */
    public function setName($value){
        $this->name = $value;
    }
    /**
     * Gets Model
     *
     * @return Model
     */
    public function getModel(){
        return $this->model;
    }
    /**
     * Sets Model
     *
     * @param Model
     */
    public function setModel($value){
        $this->model = $value;
    }
    /**
     * Gets Path
     *
     * @return Path
     */
    public function getPath(){
        return $this->path;
    }
    /**
     * Sets Path
     *
     * @param Path
     */
    public function setPath($value){
        $this->path = $value;
    }
    /**
     * This method renders the view.
     * It loads template with slots
     * and it loads data from the model into
     * template slots.
     *
     * @return string
     */
    public function render(){
        /*
         * First we start output buffering.
         * This is critical!
         */
        ob_start();
        /*
         * Then we include the template.
         * Look at the fields inside the template!
         * Also look at how we create path of
         * the template.
         */
        $templatePath = sprintf('view/%s.tpl.php', $this->getName());
        /*
         * This is the funny trick to include template here.
         * Template becomes PART OF this class!
         * So we can use all methods from this class
         * inside our template
         */
        include $templatePath;

        /*
         * Here we collect the contents from
         * the buffer to variable $outstr
         */
        $outstr = ob_get_contents();

        /*
         * Here we end output buffering for this level
         */
        ob_end_clean();
        /*
         * Return rendered view
         */
        return $outstr;
    }
    /**
     * Returns slot value.
     * It loads value with given name from
     * the model
     *
     * @param $theName
     * @return mixed
     */
    public function getSlot($theName){
        return $this->getModel()->__get($theName);
    }
}

... and here is the template

<div>
 <h3>Personal information</h3>
 <div>
 Name and title: <?=$this->getSlot('Title')?> <?=$this->getSlot('FirstName')?> <?=$this->getSlot('LastName')?>
 </div>
 <div>
 Age: <?=$this->getSlot('Age')?>
 </div>
 <div>
 <h3>Hobbies</h3>
 <?php
 /**
 * Pay attention to this foreach. This is the elegant way
 * to use php code inside a template.
 * Code is nice and clean, easy to read.
 *
 * Use of PHP short tags is recommended, it makes code much nicer.
 * However - you can use long opening tag as well but then
 * you have to use echo instead of =
 */
 ?>

 <?foreach( $this->getModel()->getHobbyList() as $hobby ):?>

 <div><?=$hobby?></div>

 <?endforeach?>

 </div>
</div>

… and then we clue all pieces together

<?php
/**
 * This script may be called as Front Controller.
 *
 * This one takes the hits. It can ve called directly
 * but it's also very common to use rewrite that makes the URL-s
 * nicer and sends all requests to this script regardless of their
 * visible URL.
 */

/*
 * First include our required classes
 */
require_once '../lib/Model.php';
require_once '../lib/View.php';
require_once '../lib/Controller.php';

/*
 * Instantiate our default controller
 */
$controller = new Controller();

/*
 * Call default action in our controller
 */
echo $controller->defaultAction();

//EOF index.php

OK, that’s it!

This is a simple way to make MVC work for you. The trick of template is that we include it inside a class. The template which is actually made of fragments of PHP code becomes part of the class. Template can use methods of class. It can load data into itself in a clever, fast and elegant way.

All comments to this technique are welcome!

»  Substance:WordPress   »  Style:Ahren Ahimsa
© Copyright Wasabi Ltd 2010