Archive for May, 2008

Using Validation Library Separately from CodeIgniter

Inspired by Hasin Hayder’s separation of ActiveRecord here; i always wanted to separate some other cool stuffs from CodeIgniter so that i could use those in some other projects which are not built using CodeIgniter. One of the attractive libraries of CodeIgniter which i wanted to separate was Validation Library. Especially for its custom callback functions and individual element error calling like $this->validation->fname_error. This helped me to give my clients more accessibility.

Step by step i will try to separate some other cool stuff ( I hope Derek Allard wont’ mind ;) ). Here is my first contribution :

To separate the validation library first take out the Validation.php file from CodeIgniter’s system/libraries folder. BTB i am using CodeIgniter 1.6.1 . Now modify by function names:

  1. strip_image_tags() – Comment the body content of this function. I did not able to track it’s use. :(
  2. valid_ip() – Replace the body of this function from Input library ’s valid_ip() in system/libraries folder.
  3. xss_clean() – Same as task 2
  4. CI_Validation() – Comment the whole body
  5. Run()

For Task 5:

This is where i had to do maximum modification.

First: To eliminate the language pack dependency i commented line Number : 189 containing ‘$this->CI->lang->load(‘validation’);’. Now to be able to find the language strings i have added the following array in the class from ’system\language\english\validation_lang.php’.

var $lang = array(

‘required’ => “The %s field is required.”,

‘isset’ => “The %s field must have a value.”,

…….

…….

‘matches’ => “The %s field does not match the %s field.”,

);

In line : 221; i changed ‘$this->CI->lang->line('isset')‘ => ‘$this->lang['isset']

In line: 319: i changed ‘$this->CI->lang->line($rule)‘ => ‘$this->lang[$rule]

Second: Now the tricky one. Using the Callback functions. For this i commented line 278-281. Actually i did not know what to do with these lines that’s why i commented them :) . Now change the line 282 containing ‘$result = $this->CI->$rule($_POST[$field], $param); ‘ to ‘$result = $rule($_POST[$field], $this); ‘.

Now the structure of the callback function:

function abc_check($str, &$validClass)

{

if ($str == ‘abc’)

{

$validClass->set_message(‘abc_check’, ‘The %s field can not be the word “abc”‘);

return FALSE;

}

else

{

return TRUE;

}

}

That’s all for the validation function. Oh.. now to use this class u have to do is :

require_once("Validation.php");

$valid = new CI_Validation();

$rules = array(

‘fname’ => ‘required|callback_abc_check’,

);

$fields = array(

‘fname’ => ‘First Name’,

);

$valid->set_rules( $rules );

$valid->set_fields( $fields );

if( $valid->run() == false )

echo $valid->error_string;

Here are the 2 files :

Validation.php

test.php

Leave a Comment

Super Scratch Development-1: Ideas from CodeIgniter & Drupal

There are a good number of developers i have seen who still develop sites using Scratch Development. Each and every one of them have their own excuses over CMS and MVC based frameworks. Even most of the fresh students from universities never heard of Web Application Frameworks ( very sad ). So, guiding new freshers to the world of Web Application Framework is little tough and sometimes they get frustrated. In such cases i let them develop using scratch but give them some tools and ideas from codeIgniter and Drupal. After working with such things sooner they become more interested on newer things like Frameworks.

Here i am not going for details just an overview. Otherwise i have to write a whole e-book :D … Is there any good rich guy who can sponsor me to write a book on this .. :) )

  1. BASE PATH / Database Operation
    Freshers and other experienced scratch developers mostly uses Relative path ( ./ or ../) to target their image or PHP files. And for database activity i can bet 70% scratch coder uses ( common.php or common.inc) where they put a connect_to_db function which connects to database.For PATH location i usually suggest to use a ‘config.php’ :D file and store necessary paths like Image folder, JS etc.. and in your code use such folder location to locate your images and other stuffs.

    Now for database activity… in this case i take the risk and usually introduce ActiveRecord of CodeIgniter to the freshers. And for this reason i am very thankful to Hasin Hayder for separating ActiveRecord from CodeIgniter. I used his separation :D .

  2. Template Engine ( tiny but strong / Smarty)
    PHP template Engine can reduce coding lines and it gives a great opportunity to reshape or change the orientation of the site as clients and bosses likes to change their minds very often. In my personal opinion i liked ‘tiny but strong’.

    Ideas From drupal

    : I very much :D like the block mechanism of drupal. In drupal you can put your block in any particular place like left sidebar, right sidebar, footer etc even by theme customizing you can create your particular region where you can put your block . So, this can be easily inherit by using tiny but strong. By using a database table which specify the position of a particular block and some individual block code; you can achieve such functionality. Even the header and footer can be handled more efficiently.

  3. TinyMCE / FCKeditor
    In every web project there are always a number of static pages needs to be handled :( like contact us, about us etc. Such cases can be handled by Rich Text Editors like TinyMCE and FCKeditor. Keep the contents of those static pages inside the database. And provide TinyMCE ( my personal favorite ) to edit such contents. In those pages you can retrieve the body content from database.

    you can see their comparison with others :D

to be continued …………………………………….. :D

Leave a Comment

Introduction to CodeIgniter

On February 2008, i have participated on a workshop organized by BDOSDN titled as
OSS Essential Training Program at Chittagong University of Engineering and Technology, February 22-23, 2008“.

I have presented a Lecture on “Introduction to CodeIgniter”. Here is the link of all other presentation materials .. including mine.

Hopefully this will help other novice programmers. I personally suggest new programmers to study ‘Abdur Rahmans‘ material on PHP & MySQL basic first then ‘Introduction to CodeIgniter’ presentation and demo application.

Though i have a little plan to re-furnish my slides.. so do post suggestion.

Comments (2)

Exploring BambooInvoice

Usually bamboo refers to grass ( same category ) sort of plant.. bt in bangladesh; sometime we refer bamboo as having hard time.
Like:  I am getting a bamboo in my a** dealing the last project. Means: i am having a hard time with my last project.

But in case of BambooInvoice .. quoting from the main site..

BambooInvoice is free open-source invoicing software intended for small businesses and independent contractors. Our number one priorities are ease of use, user-interface, and beautiful code. BambooInvoice was built by designer and programmer Derek Allard.”

And i am really surprised and impressed by seeing the simplicity of this system. My objectives for 1 of my projects were:

  1. Integrating BambooInvoice 0.8.4 with one of my project developed using CodeIgniter 1.6.1 .
  2. Pass additional Data to Clients.
  3. Pass additional Data during New Invoice Creation.
  4. Creating New Items in Invoice, on Fly.

To solve objective 1:  BambooInvoice uses ’Site Sentry security library’ By James Nicol www.glossopteris.com. So, all i did is removed ‘if (!$this->site_sentry->is_logged_in()…. ‘ from each controller and in 1(not sure may be 2 ) view files.

To solve objective 2:  I passed my Agency(in my project agency is client) data into clients table and altered the table as necesary.

To solve objective 3: In my project after selecting the client the user had to choose a contract id. So, i had to use an onchage event. That did not worked because ‘newinvoice.js’ defines the event for that client_id select box. Moreover from a view file (common_assets/invoice_new.php) the display of newinvoice is made hidden. For this i tweaked these 2 files and other necessary changes. To hold the other important datas in table i added ‘contract_id’ field in ‘invoices’ table.

To solve Objective 4: My Items in Invoice were categorized like VAT on total Amount, AIT on total amount.. etc. I have added a reference key from table ‘tbl_invoice_item_type’ which holds the percentage of VAT and other info, into ‘invoice_items’.

The logic structure and data manipulation in BambooInvoice is so organized that all i have to ask myself is what i want to do?? and then i have to check the flow of the application.

Though ‘BambooInvoice is still pre 1.0. There are a few bugs and uncompleted features. It is stable for everyday use, and is growing all the time.’ bt it saved a good amount of my work. 3 cheers to Derek Allard. Keep up the good work.

 

Comments (2)