Archive for Zend Framework

Royal Mail Postage by Weight – Zend Framework Module

For a recent project I have had to calculate weight based shipping costs. Before I had this as a db table, but that became a mess to manage.

So I’ve created this small shipping calculator. It’s set to use KG in weight and prices inclusive of vat taken from the Royal Mail’s 2011 Prices.

Follows are the config ini file, the class and a basic usage.

[EUR]
prices[0.01] =1.79
prices[0.12] =1.94
prices[0.14] =2.15
prices[0.16] =2.32
prices[0.18] =2.54
prices[0.2] =2.63
prices[0.22] =2.75
prices[0.24] =2.87
prices[0.26] =2.99
prices[0.28] =3.08
prices[0.3] =3.14
prices[0.32] =3.8
prices[0.42] =4.46
prices[0.52] =5.12
prices[0.62] =5.78
prices[0.72] =6.44
prices[0.82] =7.1
prices[0.92] =7.76
prices[1] =7.76

above.start = 1.1
above.increment = 0.1
above.valuePerIncrement = 0.55

[ITL]
prices[0.01] =2.49
prices[0.12] =2.79
prices[0.14] =3.12
prices[0.16] =3.48
prices[0.18] =3.84
prices[0.2] =4.2
prices[0.22] =4.56
prices[0.24] =4.76
prices[0.26] =4.88
prices[0.28] =5
prices[0.3] =5.12
prices[0.32] =6.45
prices[0.42] =7.78
prices[0.52] =9.11
prices[0.62] =10.44
prices[0.72] =11.78
prices[0.82] =13.11
prices[0.92] =14.44
prices[1] =14.44

above.start = 1.1
above.increment = 0.1
above.valuePerIncrement = 0.66

[UK]
prices[0.1] =1.9
prices[0.25] =2.36
prices[0.5] =2.98
prices[0.75] =3.66
prices[1] =4.46
prices[1.25] =5.88
prices[1.5] =6.8
prices[1.75] =7.71
prices[2] =8.62
prices[4] =10.74

above.start = 6
above.increment = 2
above.valuePerIncrement = 3.66
<?php
class BinaryKitten_Shipping {
    private $_cfg;
    private $_curCfg;
    public function __construct($postalZone=null) {
        $this->_curCfg = $this->_cfg = new Zend_Config_Ini(APPLICATION_PATH .'/configs/postal.ini');
        if (isset($postalZone)) {
            $this->setPostalZone($postalZone);
        }
    }
    public function setPostalZone($postalZone)
    {
        if (self::validatePostalZone($postalZone)) {
            $this->_curCfg = $this->_cfg->get($postalZone, $this->_cfg);
        }

    }
    public static function validatePostalZone($postalZone)
    {
        return in_array(strtoupper($postalZone), array('UK','EUR','ITL'));
    }
    public function getPriceForWeight($weight)
    {
        $priceOut = -1;
        foreach($this->_curCfg->prices as $weightBoundary=>$price) {
            if ($weightBoundary < $weight) continue;
            if ($weightBoundary >= $weight) {
                $priceOut = $price;
                break;
            }
        }
        if ($priceOut == -1) {
            for($weightBoundary = (float)$this->_curCfg->above->start;$weightBoundary < $weight;$weightBoundary += $this->_curCfg->above->increment) {
                $price+=$this->_curCfg->above->valuePerIncrement;
            }

            $priceOut = $price;
            //try to calculate the difference
        }
        return (float)$priceOut;
    }
}
$prices = new BinaryKitten_Shipping();
$prices->setPostalZone('EUR');
$price = $prices->getPriceForWeight(5);

TinyMCE and Zend_Auth – Simple Authentication checking.

I’ve been using TinyMCE for a good long while and wanted to integrate their fantastic FileManager plugin and ImageManager plugin’s (Both Commercial) in with my ZF client site. This is relatively easy, the easiest way is to create a controller action and a iframe in the view that points to the location of the imagemanager or filemanager plugins. eg

<iframe src="/js/tiny_mce/plugins/filemanager/index.php?type=fm">

This works well, but I wanted to make sure that the plugn was not accessible unless the user logged in. I already used Zend_Auth and this doesn’t exactly play well with the basic SessionAuthenticator. So I set out to create a basic simple plugin for TinyMCE that would allow it to work with the Zend_Auth System.

- Creating the plugin -
first off we have to create our plugin and plugin folder within the tinymce plugin’s plugin folder. If you think that’s a lot of use of plugin, you can understand where my brain decided to up and leave.

1st locate your tinymce folder, in there you should find plugins and then go into the filemanager folder. Of course this assumes you have purchased and installed the filemanager plugin. Your path should be something like the following:
<path_to_tiny_mce>/plugins/filemanager/ – this is what we shall refer to our “Base Path” from now on.

now we are at a comfortable location to proceed, we can go ahead and create our folder and file
<Base Path>/plugins/ZendAuthenticator
in here place a file name ZendAuthenticator.php containing this code:

<?php
/**
 * Use Zend_Auth to validate the logged in user.
 *
 * @author BinaryKitten
 */
class BinaryKitten_ZendAuthenticator extends Moxiecode_ManagerPlugin {

    private $auth = null;
    public function  __construct($man)
    {
        $config = $man->getConfig();
        if (isset($config['ZendAuthenticator.LibraryPath']) && !empty($config['ZendAuthenticator.LibraryPath'])) {
            set_include_path(
                implode(
                    PATH_SEPARATOR, array(
                        realpath(
                        $config['ZendAuthenticator.LibraryPath']
                        ),
                        get_include_path(),
                    )
                )
            );
        }
        require "Zend/Auth.php";
        $this->auth = Zend_Auth::getInstance();
    }

    //put your code here
    public function onAuthenticate(&$man)
    {
        return $this->auth->hasIdentity();
    }
}

$man->registerPlugin("ZendAuthenticator", new BinaryKitten_ZendAuthenticator($man));

Voila the plugin is ready.

- Configuring the plugin -
Now open up the config.php found in the “Base Path” mentioned earlier.
locate: $mcFileManagerConfig['authenticator'] and set it’s value to the following
$mcFileManagerConfig['authenticator'] = 'ZendAuthenticator';

- Library Location -

Finally, If your Zend_Framework is not located on the include path (like mine is) then you will need to tell the system where to find the library, to do this add the following to the config.php
$mcFileManagerConfig['ZendAuthenticator.LibraryPath'] = ' ';
Please remember though that this is relative to the “Base Path” and not the Document_Root unless you specify it as such.

– - – - –
The last thing I will leave you with is that this only checks to see if there is a stored session for Zend_Auth. I’ll leave it up to you if you want to get roles and other fancy stuff integrated.

Active Module Config V2

A brand new version of my previously Released Front controller plugin for Zend Framework.
Read more

Per form stylesheet with Zend_Form

I needed to have certain form styles attached when a form is in use. I didn’t want to do this from the controller each and every time as it was really was only to do with the form and I might want to use the form with it’s own stylesheet in other places.
Read more

Active Module Based Config with Zend Framework

I’ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won’t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead I bring to you How I managed to get Active Module Based Configuration within Zend Framework.

The Problem

The Concept I wanted to achieve was to have unique Configuration based upon the module that was active. The Issue with this is that the Bootstrap files and the _init functions for ALL modules are called with no bias as to which module is active. Thus if you created a 3 modules wanted to make menu alterations in one, those alterations will be applied to all. I also wanted to have a a system where if i added extra modules i could just add extra functions to the bootstrap file and it would work in a similar way.

With this in mind, I set about trying to figure out the solution.
Read more

Zend Framework – Getting Single Components

Recently i was looking at implementing some caching to recent websites.. 1 to improve load times as well as a possibility to improve the way that my “Framework” works.

I had just recently looked over Rob Allen’s (@Akrabat http://akrabat.com/) PHP Nw 08 Conference Talk on “First Steps with Zend Framework”. As I had not been really feeling all that great at the conference itself, I was glad to be able to watch teh recorded version on the web. During the 1st segment of the talk, Rob described how zend cache was his first foray into the Zend Framework and this lead me onto how I was thinking about how the best way to implement the caching on my site(s).

So, with this in mind, off I trotted to http://framework.zend.com to grab the Zend Cache component that I wanted to look at. That’s where I hit a snag. It seemed impossible to actually get the components on their own. After about half and hour of scouring the site I still couldn’t find what I wanted, so I Tweeted about my annoyance.. To which I got a couple of replies… the 1st reply came from Richard Chiswell (@rchiswell) who said:

@BinaryKitten You might find the Dependencies list at http://bit.ly/199FYR useful. You could try contacting @CalEvans who wrote a book on ZF

The bit.ly link took me to: http://framework.zend.com/manual/en/requirements.html#requirements.dependencies the listing of the components and their dependencies, Though this was helpful it wasn’t really as helpful as the reply i got from Cal Evans (@CalEvans):

@BinaryKitten http://epic.codeutopia.net/pack/

Short Sweet and Wham.. That is exactly what I needed.

Shortly afterwards Richard blogged about this, which in turn made me feel like I should blog about it as well, and oooh look.. So I have.