Saturday, August 13, 2011

PHP code for rendering a 'barrel widget' for charting and graphing.

I had call to write a PHP based barrel widget the other day and through I'd share the process.
The end-result needed to look like this: 
 
In order to achieve this, I chopped the graphic into the following PNG files:






Then the code which made it render as a complete new 'barrell chart' or 'barrell widget' looked like this:

<?php
function graph_render_barrel($val,$name){
    //$val is the percent value passed to the function, and $name is the name of the file to be saved
    $filename=$name;
    $img = imagecreatefrompng("g_barrel.png");
    imagealphablending($img, true);
    imagesavealpha($img, true);
    $greenbase = imagecreatefrompng("g_barrel_breen_bot.png");
    imagealphablending($greenbase, true);
    imagesavealpha($greenbase, true);
    $greenmid = imagecreatefrompng("g_barrel_breen_bak.png");
    imagealphablending($greenmid, true);
    imagesavealpha($greenmid, true);
    $greentop = imagecreatefrompng("g_barrel_breen_top.png");
    imagealphablending($greentop, true);
    imagesavealpha($greentop, true);
    $stretch=floor(184*($val/100)); //0 to 184
    imagecopyresampled($img, $greenbase, 2, 211, 0, 0, 143,21, 143,21);
    imagecopyresampled($img, $greenmid, 2, (211-$stretch), 0, 0, 143,(20+($stretch-20)), 143,20);
    imagecopyresampled($img, $greentop, 2, ((211-$stretch)-22), 0, 0, 143,45, 143,45);
    imagepng ( $img , "portal/saasmsallstar/UserFiles/bb_crp_graph_images/$filename" , 0 ,  PNG_ALL_FILTERS);
    imagedestroy($img);
    return($name); //handle possible retuns here
}
?>

So, in practice it could be used like this:

<?php
echo("<img src=\"".graph_render_barrel(66,"path/to/new_graphic.png")."\" >");
?>

Managing Timezones in BlueBox2.0

If you need to set the timzone for your BB2.0 system, or for specific timezones for individual users, you can use the following built-in settings:
  • for a system-wide setting, set the config (admin->config) setting system_default_timezone
  • for a user-specific timezone setting there is a field in the bb_users table called 'timezone'
The value for either of these can be set to one of the standards described in http://www.php.net/manual/en/timezones.php

Adding Multiple email responses to new data added in BB2.0

If you are familiar with the bbsetting_email_onadd setting for BlueBox2.0, you may be interested to know that you can set multiple arrays which send different results to various recipients, as follows:

In a .php file, use the following:

var $bbsetting_email_onadd=array(
array("to"=>"some@user.com","bcc"=>"","
from"=>"admin@domain.com","subject"=>"Subject One","template"=>"email_one"),
array("to"=>"some_other_user@domain.com","bcc"=>"","from"=>"admin@domain.com","subject"=>"Subject Two","template"=>"email_two")
);
 
or if you are editing within Admin->module settings, you can enter nested arrays as follows:
 
{to=admin@site.com,from=some@domain.com,subject=Subject One for Admin Email,template=name_of_email_
template_for_admin,}
{to=user@site.com,from=some@domain.com,subject=Subject Two for User Email ,template=name_of_email_template_for_user,}



Monday, July 18, 2011

Updated Security In BlueBox2.0

BlueBox2.0 security and passwords just got a bit of a face-lift with the inclusion of 2 new features:


Javascript Password Confirmation:

When entering a new password (or changing an existing one) a Javascript/modal popup is initiated which confirms the password prior to accepting it.

New bbsettings for Enhanced Security:

These new bbsettings have been added to the bb_secure module and can be edited via admin->module settings...

A new standard minimum password length of 5 characters has been introduced. This can be shortened or lengthened by editing bbsetting_enable_password_length for bb_secure to the number of characters required.

You can also force combined alpha/numeric passwords by setting bbsetting_enable_password_alphanumeric to 1.

Finally, you can force users to update/change their password regularly by setting bbsetting_enable_force_password_change_in_days to the number of days per cycle.

Wednesday, July 6, 2011

New In-Line Form Field Management in BlueBox2.0

One of the most powerful things about BlueBox2.0 is the ability to design form and templates for every screen and output in the system. One of the difficulties, up till now, has been knowing which bbsetting to apply in order to achieve these customisations.

In an effort to simplify this, there is now an in-line toolbox next to each field in a form which displays various options with regards to the display/behaviour of that field... for example, whether to hide it, or make it required etc.. Only users with 'admin' rights to the specific module will see these toolbox icons:

This tool will evolve significantly over the next few months, but this is a good starting point for now...

Friday, July 1, 2011

Using conditional IF and IFNOT statements in BlueBox2.0 Tags

Today I decided to use my 'sharpening time' to get to grips with BB2.0 <!--:if and <!--:ifnot tags.

The basic syntax for these tags is:

<!--:if:{variable_name}{optional condition}:{action}:-->

This can be used as follows:


<!--:if:email:{print:Your email address is (#email#).}:-->

or

<!--:ifnot:email:{print:You do not have an email address.}:-->

This is fairly straightforward, the condition of the if and ifnot statements purely checks if a positive value exists for the variable and, if so, then it runs a sub-tag between the { }'s. This subtag is exactly the same as a normal BB2.0 tag, but, due to it's embedded nature, it gets { in place of <!--: and } in place of :-->. Also: you will notice that referring to the variable value within subtags you use the (# approach, by wrappinf the variable name in (# and #).

To up the ante a bit, I then decided to play with conditional statements like ==, !=, > and <. These work as follows:

<!--:if:email==bill@microsoft.com:{print:Hi Bill!}:-->

Note here that the value is never encapsulated in the ' and " string-wrappers normally used in code, and also that the condtional statement for 'equals' is double =.

Last thought on this, and this one stumped me for a while, is that when you are evaluating form-posted variables, remember to describe the variable name fully using the global|fields|variable_name method, eg:

<!--:if:global|fields|email==bill@microsoft.com:{print:Hi Bill!}:-->

Friday, June 24, 2011

Using Postcodes in BlueBox2.0 via PostCodeAnywhere

Recently we have integrated an excellent UK postcodes library/database/service with BlueBox2.0. This is the PostCodeAnywhere product. The API's have been integrated formally into BlueBox2.0 and offer the following features:
  • Postcode Lookup
  • Find nearest-to from a certain address (used for finding 'a store near you' etc)
  • Geo-spatial data (long/lat data for mapping)
  • Government Data (wards/districts etc)
  • Database Cleansing
Installing PostCode Lookups into BlueBox2.0

This is a very easy process. Simply go to toe Postcode Module under Integration. There you will be asked for your PostcodeAnywhere customer 'code' and the API Key for your lookup-account with them. These are easily got by surfing to PostcodeAnywhere.co.uk using this link:

Postcode Anywhere

Sign up for an account, then create a new key from the backoffice menu. Once you have your account and key simply enter these into the PostCode module by clicking on Accounts in the top menu. Click on 'DataFunctions->Add' and fill in your account details.

Once these have been filled in and the account is added, go back to the Postcodes Dashboard and you will see a test lookup facility which you can use to test if the account is working correctly.

Configuring your BlueBox2.0 System for Postcode Use

Now that Postcodes have been set up in your system, you may want to configure it as follows:

Limiting abuse: BB2.0 has a default limit of 3 lookups per IP address per hour, to stop web-users from chewing up your credits unnecessarily. This would apply for 'public use', but if you have a backoffice function you may want to set this to a much higher amount, or remove the limit altogether. There is a bbsetting for this, called bbsetting_postcode_limit_per_hour. If you set this to 0, there will be no limit.

Forcing Postcode Lookups: The postcode module will not automatically 'hijack' fields with the name containing 'zipcode', but if you want all 'zipcode' formfields to automatically become postcode lookups, you can set bbsetting_postcode_force_lookups_in_forms to 1.

Disabiling Postal Address fields for manual entry: Part of postcode lookup functionality is to stop users from messing up the data. To make your address fields 'readonly' use bbsetting_readonly_fields for each module that has postal address fields. Ie for the user table you will set bb_users->bbsetting_readonly_fields to be:

postal_address,
postal_address2,
postal_suburb,
postal_city,
postal_state,