Saturday, 19 September 2015

Git - Pre commit hook script for PHP & Drupal

Introduction

I think, most of you know about Git repository's support for hooks. One of those hooks is pre-commit, which will be called before whenever a commit is been made. So I have created one Git pre-commit hook script specifically for PHP & Drupal 7 (Can be customized for your purpose as well) git repository. You can fork or download it from manojapare/pre-commit

Installation

For using this hook script just copy/download the script file to your git repository hooks folder that is in.git/hooks/. Don't forget to give executable permission for the script file.

Working

Whenever a git commit is been performed, pre-commit hook script will be executed by git inbuilt hook support. The above script does syntax check as well as looks for any debugging function in the changes staged to commit (which is a bad practice). In both case, the commit operation will be aborted and can be committed after doing corrections or removing the debugging function from the code.
In case you want to force commit with any debugging functions (not recommended) you can do it by using the git command option -n OR --no-verify.

Syntax check

Syntax check is been performed using php -l command for each php files (file with extensions 'php, inc and module').

Check for debugging functions

Checking for debugging function is been performed using git diff command only to files staged to commit and in turn only to added/modified changes.
List of debugging functions included for checking are: dpm, dpq, dvm, ddebug_backtrace, print_r, var_dump, debug_backtrace, console.log. First 4 functions are of Drupal 7 devel module, later 3 are of php and last one is of javascript.
For better performance of the script, I have excluded the checking of files with following extensions: 'info, png, gif, jpg, ico, patch, htaccess, sh, ttf, woff, eot, svg'. Also excluded files residing in the following directories: 'features, contrib, devel, libraries'.

Merge conflict marker check

Merge conflict marker check is been performed to all files staged to commit using egrep pattern "(<<<<|====|>>>>)+.*(\n)?".

Thursday, 9 July 2015

How to create html mail template for Drupal?

In Drupal 7 by default we can't send html mail, since Drupal will convert all emails to plain text before sending. This could be overridden using Drupal 7htmlmail contributed module. We may also need i>mailsystem module for implementing htmlmail, which is a dependency. The module also give html mail template, which could be customized as well in same way of theme templates.
Firstly, we can look on the configuration details and then theming of html template mail. If there is a need to make the html template for a particular mail with module name and with a particular mail key then we have to create a new settings in 'admin/config/system/mailsystem' namely module_name and key_name respectively. After saving the new settings we have to set 'HTMLMAILSystem' for the new 'Mail System Settings'. Next in htmlmail configuration 'admin/config/system/htmlmail'
  • step-1: List of available templates and descriptions.
  • step-2: Email theme, where the above custom template will hold.
  • step-3: Post-filtering, text format used for filtering the mail.
Find out the template file which meets your need and copy paste them to your theme template and specify it in above config page.
Now theme the copy of mail template as you wish. That's all and you have completed with your config and theming of html mail template.
Using the help of Drupal functions like drupal_mail we can send mail using the 'htmlmail' module template.
We need to specify the module name and key according to template file we use.
Let us assume, if we use 'htmlmail--module_name--key_name.tpl.php' template then drupal_mail call be as below to use this template for the same.

  $module = 'module_name';
  $key = 'key_name';
  $to = $user->mail;
  $language = language_default();
  $name = $user->name;
  $params = array();
  $from = 'no-reply@gmail.com';
  drupal_mail($module, $key, $to, $language, $params, $from);
    
Also it will be a good practice to add hook_mail(), if we call drupal_mail with in our module.
Hope this article about html template mail for Drupal 7 helped you.

How to write MySql single export query

We as a PHP Drupal web platform developer most often has to face the situation to export data from one table to another table of same database or to table of another database (if site uses multiple databases) during the process of any change in normal work flow.

Most of us normally think to export the data from one table to another by firstly querying the required data from source table and then looping through the query result and inserting them to destination table. This can be simply achieved with a single query.
Major drawback of this is that first select query has to be compiled and run in MySQL server then for each query result front-end server has to loop through it and return the corresponding insert query to MySQL, which in turn has to compile and run for each iteration. If lakhs of data is to be exported, then this will be a huge load for front end server.
Let me tell you another option. The single query export approach:
  1. If the source table and destination table are both in same database.
            
              $query = db_query(
                "INSERT INTO `destination_table_name` (`title`, `uid`, `created`)
                  SELECT title, uid, created
                  FROM `source_table_name`
                  ORDER BY created DESC
              );
            
          
  2. If the source table and destination table are in different database, namely source table is in database_1 and destination table is in database_2.
            
              $query = db_query(
                "INSERT INTO `database_2.destination_table_name` (`title`, `uid`, `created`)
                  SELECT title, uid, created
                  FROM `database_1.source_table_name`
                  ORDER BY created DESC
              );
            
          
Normally this seems weird, but its the real trick. Avoiding of multiple compilation of query in MySQL server and looping in front-end server. Hope this helps!

Saturday, 23 May 2015

Puzzle

One day I got crazy sloving picture/number grid puzzle available in windows desktop gadget. Then suddenly why I can't try create the same puzzle of my own using html, js & css and the result is what you are seeing.

Getting started with CiviCRM

FSF-CiviCRM is a popular Customer Relationship Management (CRM) system. The first step in getting started is understanding the CiviCRM codebase. For better understanding, please download CiviCRM zip file from https://civicrm.org/download and install.

Object Oriented Programming

CiviCRM core is built using OOP singleton concept. Singleton framework is one which initiates one class object only once and reuses it afterwards.

Business logic

CiviCRM business logic is handled in directory 'CRM/' in the root of CiviCRM installation. In this directory, you can see core functionalities such as Groups, Contacts, Profiles etc. Each of them inturn has directories named DAO, BAO, Form, Page etc.

DAO

DAO stands for Data Access Object, which acts as wrapper layer around database. It is recommended to use DAO to interact/manage database data.

BAO

BAO is abbreviation of Business Access Object. Actually, BAO is used to extend the DAO for handling business logic.

Form

Form in CiviCRM is implemented as a class extending CRM_Core_Form, which is done using the following methods: preProcess - retrieve data before processing form, buildForm - generate html form using QuickBuildForm helper functions, formRule - form validation rules and postProcess - form subit handler. Also, there will be separate template for each form, which can be found in 'templates/Form/' directory.

Page

If the screen isn't Form, it will be Page. Page is also class similar to Form extended from CRM_Core_Page. The methods available for Page preProcess, run - which displays the page content. Template for the page can be found in the directory 'templates/Page/'.

xml

xml directory contains menu structure of urls mapping to CRM form or page classes and controls access to these URLs using permissions.

Templates

Directory which contains templates of Form, Page etc.
Go through the CiviCRM core for better understanding. Also, before starting work on CiviCRM, please post your plan of work to civicrm forum OR consult other developers instantly on CiviCRM IRC

Saturday, 2 May 2015

Geany editor snippets for Drupal

This article discuss about setting snippets for Geany, a light weight IDE especially for Drupal 7 web developers as per coding standards. For those whom doesn't know, snippets are a small piece of source code which are meant for reuse. For example: while coding instead of typing full if conditional statement structure for each time we can just reuse them by the means of creating snippet for the same.

Creation of snippet is simple as like coding in any language. Before implementing custom snippets for Drupal 7 as per coding satandard we have to study about geany snippets.conf file structure, which can be found in '/home/user/.config/geany'.
Firstly we can look on the keywords available
  1. '\n' OR '%newline%': Used for new line.
  2. '\t' OR '%ws%': Used for one indentation step.
  3. '\s': Used for white space.
  4. '%key%': Keys defined in special section.
  5. '%cursor%': Defines where the cursor has to be placed. We can use multiple cursors in one snippet and can be jumped to next cursor position by using 'Move snippet cursor' keybinding.
Now we can look on important sections available in snippets.conf file.
  • Default is where we define snippets for files type other than the defined or known file types.
  • Special is where we can define our own special configuration keywords like


    # special keys to be used in other snippets, cannot be used "standalone"
    # can be used by %key%, e.g. %brace_open%
    # nesting of special keys is not supported (e.g. brace_open=\n{\n%brace_close% won't work)
    # key "wordchars" is very special, it defines the word delimiting characters when looking for
    # a word to auto complete, leave commented to use the default wordchars
    [Special]
    brace_open=\s{\n\t
    brace_close=}\n
    brace_close_cursor=\n\t%cursor%\n}%cursor%
    block=\s{\n\t%cursor%\n}
    block_cursor=\s{\n\t%cursor%\n}%cursor%
  • Keybindings is where we can define keybindings or keyboard shortcut for snippets defined.

    # Optional keybindings to insert snippets
    # Note: these can be overridden by Geany's configurable keybindings
    [Keybindings]
    #for=7
  • PHP is where we define snippets for php files.
For Drupal 7 snippets, we have to define snippets in php section like:

[PHP]

if=// Documentation.\nif (%cursor%)%block_cursor%
els=else%brace_open%// Documentation.%brace_close_cursor%
eli=elseif (%cursor%)%brace_open%// Documentation.%brace_close_cursor%
whi=// Documentation.\nwhile (%cursor%)%block_cursor%
do=// Documentation.\ndo%block% while (%cursor%);%cursor%
swi=// Documentation.\nswitch (%cursor%)%brace_open%case %cursor%:\n\t\t%cursor%\n\t\tbreak;\n\tdefault:\n\t\t%cursor%\n}%cursor%
try=// Documentation.\ntry%block%\ncatch (%cursor%)%brace_open%// Documentation.%brace_close_cursor%
for=// Documentation.\nfor (%cursor%; %cursor%; %cursor%)%block_cursor%
fun=\n/**\n * Documentation.\n */\nfunction %cursor%(%cursor%)%block_cursor%arr=array(\n\t%cursor%\n)%cursor%
foe=// Documentation.\nforeach (%cursor% as %cursor% => %cursor%)%block_cursor%
You can try these by copy and paste on snippets.conf file's PHP section. Save the file and type if and press tab to see the geany snippets magic. The following will create if conditional statement structure and cursor will be focused on first '%cursur%', to move next step press the keyboard shortcut for 'Move snippet cursor' which can be set in edit -> preferences -> keybindings -> editor Action section. In these snippets definition you have noticed the special keywords such as '%brace_open%', '%brace_close%' and '%block%' which are defined in Special section as explained above.
Like this you can create or customize already available snippets. Firstly using snippets take time, gradually on practice these will reduce the time of development. Hope this will help you using geany editor for Drupal development.

Set up Authorize.Net ARB to work with Drupal commerce

Authorize.net is a popular alternative to Paypal for eCommerce websites especially its recurring transaction product - Authorize.net ARB (Automated Recurring Billing). Drupal support for Authnet ARB is available through Ubercart but it is not available for Drupal_commerce. However there is a workaround to setup Authorizet.net ARB to work with Drupal commerce. Read on to know more.

There is no Authorize.net ARB module for Drupal commerce but there is a sandbox module - Authnet ARB This module does not work straight out of box but you need to make a few changes. But before that lets get familiar with the module
Drupal authnet_arb sandbox module uses authorize.net SDK for ARB API request, so we have to install the sdk for using this module. First of all we can look on what else features the authnet_arb module provide and then we can look on how to integrate it with drupal commerce.

Features provided by authnet_arb module.

  1. Payment method for ARB:
    The sandbox module provides an additional payment method named 'Recurring payments with Authorize.Net ARB (with credit card)' along with default drupal commerce payement methods. Enabling this payment method, we get an option for choosing 'Recurring payments' method of payment in drupal commerce payment form.
            
    /**
     * Implements hook_commerce_payment_method_info().
     */
    function authnet_arb_commerce_payment_method_info() {
     $payment_methods = array();
     $payment_methods['authnet_arb'] = array(
      'base' =>  'authnet_arb',
      'title' => t('Recurring payments with Authorize.Net ARB (with credit card)'),
      'short_title' => t('Authorize.Net ARB CC'),
      'display_title' => t('Recurring payments'),
      'description' => t('Integrates Authorize.Net ARB for transactions.'),
      'callbacks' => array(
        'settings_form' => 'authnet_arb_commerce_settings_pane',
        'submit_form' => 'authet_arb_payment_pane',
        'submit_form_submit' => 'authet_arb_payment_pane_submit',
        'submit_form_validate' => 'authet_arb_payment_pane_validate',
       ),
       'file' => 'commerce.inc',
     );
    
     return $payment_methods;
    }
    
  2. Silent post URL:
    Silent post is the feature provided by Authorize.Net, which will post a http request to our domain silent url (which we need to configure in Authorize.Net account settings page) with the transaction details of ARB subscriptions payement as xml. Using this we can listen to the post request coming to our drupal site and do necessary business logic according to transaction status details.
    
    /**
     * Implements hook_menu().
     */
    function authnet_arb_menu () {
      $items = array();
      $items['authnet-arb-silentpost'] = array(
        'type' => MENU_CALLBACK,
        'page callback' => 'authnet_arb_silentpost',
        'access callback' => TRUE,
      );
    
      return $items;
    }
    
  3. Authnet ARB settings:
    Admin config page for setting the API login Id and transaction key. This form also provide an option for sandbox transactions, which will be more useful while testing.
    Authnet_ARB_part_1_img1.png
  4. ARB Subscription cancellation form:
    Using this subscribers can cancel their ARB subscription any time they want.
Since we are now familiar with the module, lets get to work on those changes to make this module production ready.

Making production ready

Before integration we have make some hack on the sandbox module to make it effectively work with drupal_commerce and bug free.
In Drupal organization hacking module and generating a patch is more oftenly done by all drupal contributors.
Basically to be a good drupal contributor before hacking a set of code we need to have clear idea of what is the cause of problem and why we need to fix it. Hence we can look on the problem, whenever creating ARB subscription or an API request is been to happen SDK will check for sandbox boolean value is true or false. Since authnet_arb is a sandbox module its not checking whether the payment method is been set as sandbox and then set the SDK sandbox boolean as proper. So we have to hack the sandbox module for this to work properly before each API request is to created or generated. Here come the sample code needed to set sandbox boolean of SDK.
    $request = new AuthorizeNetARB($settings['login'], $settings['tran_key']);
    if ($settings['sandbox']) {
      $request->setSandbox(TRUE);
    } else {
      $request->setSandbox(FALSE);
    }
  
Another feature we needed while implementing Authorize.Net ARB is the option for update subscription details such as credit card. authnet_arb sandbox module doesn't provide this update feature, instead they says it need to be implemented. Here I havehack that also as follow:
    function authnet_arb_update_subscription ($update_values) {

      $settings = authnet_arb_settings();
      require_once authnet_arb_sdk_path().'/AuthorizeNet.php';

      $subscription = new AuthorizeNet_Subscription;
      $subscription->billToFirstName = $update_values['first_name'];
      $subscription->billToLastName = $update_values['last_name'];
      $subscription->billToAddress = $update_values['thoroughfare'] . ' ' . $update_values['premise'];
      $subscription->billToCity = $update_values['locality'];
      $subscription->billToState = $update_values['administrative_area'];
      $subscription->billToZip = $update_values['postal_code'];
      $subscription->billToCountry = 'US';

      $subscription->creditCardCardNumber = $update_values['card_number'];
      $subscription->creditCardExpirationDate = $update_values['expiration_date'];

      $request = new AuthorizeNetARB($settings['login'], $settings['tran_key']);
      if ($settings['sandbox']) {
        $request->setSandbox(TRUE);
      } else {
        $request->setSandbox(FALSE);
      }
      $response = $request->updateSubscription($update_values['subscription_id'], $subscription);
      return $response;
    }
  
There is one more feature need to implement delete subscription. That you can try. That's all enjoy coding and hacking.