Thursday, August 26, 2010

Drupal Custom Module Structure

The module should place at sites/all/modules/custom

Ex: mymodule
-mymodule.info //contain module meta data
-mymodule.module //contain actual logic
-mymodule.js
images (folder)

mymodule.module

Wednesday, August 18, 2010

Send mails from your domain

Your hosting provided need to create one mail server for your domain then only mails sent from your domain name like from : mail@mysite.com, otherwise some default mail server details displayed in mails header information.

To check is mail server set for your domain then run below commands at dos prompt.

ping smtp.mysite.com
and/or
ping mail.mysite.com

Tuesday, August 17, 2010

Display login/register form below the post content in Drupal

Hi there,
In drupal, When we enabled the comments for blog content type and only registered users can post the comments. If user not logged in then they will see Login or register to post contents at bottom of the post. These links were redirected to a next page to login/register but it is better to display below the post, For this use below code.

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
if($node->type == 'blog'){
switch($op){
case 'view':
global $user;
if($user->uid == 0){ //If not logged in
$node->content['show_login'] = array('#value' => drupal_get_form('user_login_block'),
'#weight' => 99);
}
break;
}
}
}
This will display the login/register screen at the end of post for anonymous user

Thursday, August 12, 2010

Rails number format helper functions

1200.2345.round
o/p: 1200

1200.2345.round_with_precision(2)
o/p: 1200.23


Currency format in rails

number_to_currency is the helper method provided by rails action view helpers. The options were
:precision - Sets the level of precision (defaults to 2).
:unit - Sets the denomination of the currency (defaults to "$").
:separator - Sets the separator between the units (defaults to ".").
:delimiter - Sets the thousands delimiter (defaults to ",").
:format - Sets the format of the output string (defaults to "%u%n").

Using this method, we could write something like this:

<%=h number_to_currency @transaction.value, :unit => 'Rs', :separator => ",", :delimiter => "." %>

This would work fine, but here we breaking the DRY (Don't Repeat Yourself) rule, because I would need to set the same parameters in every place I was displaying currency values in my entire site. A better solution would be to have a helper method which I could use in every where in my application. So I came up with the solution below, which consists in writing my own helper method in app/helpers/application_helper.rb:

module ApplicationHelper
def real_currency(number)
number_to_currency(number,:delimiter => ".", :unit => "Rs ",:separator => ",")
end
end

Now, I can use <%=h real_currency @transaction.value %> every where in my templates.

Wednesday, August 11, 2010

about drupal teaser

A "teaser" is essentially a snippet of text designed to tell the user the content of a post without reading the entire post. Since most writers have embraced the common journalistic style of explaining the nature of an article in the first paragraph, teasers work well for most articles.

Here's what happens:

1) A node contains an entire article.

2) Drupal's "teaser" function, "node_teaser," strips the first x number of words from the article and makes it available as content. The exact length is determined by the values the admin sets in Drupal's settings page.

3) So, you list a bunch of articles on a page. You want the articles to display only a snippet of text from the full article, so that you can fit a bunch of articles on a page without requiring the user to page down through tons of text. If the user likes the "teaser" content of the article, they will click on the article's title and see the full content of the article on its own page. In a sense, teasers function like summaries of an article, except that the software decides where to cut off the text. If you want to determine where a teaser article ends, you can insert the comment tag to instruct Drupal exactly where to fashion the break between full text and teaser text.

Tuesday, August 10, 2010

Cross domain data exchange

You can use XML language to transfer large data from one domain to other. Suppose domain-x wants to send data from its database to domain-y. Then domain-x will generate xml file for the database and send it to the domain-y via post method for security purpose. Upon receipt of xml file from domain-x, the domain-y will store that xml file in one folder and create one new row in received_xml table, and call a xmlparser script through cron job to extract information from the xml files and store in database and then move that parsed xml file to some other folder(named like parsed) and remove the record from receive_xml table.

For parsing there are so many free scripts available in php,ruby etc., xmltoarray is one of the technique.


Cron job commands to run shell script, ruby script and php script

Cron job commands to run different Scripts

Syntax:
Time to run - Command
[ Minute - Hour - Day - Month - Weekday ] - Command

Times pattern
* * * * * => Execute every minute
0 * * * * => Execute every Hour
0 0 * * * => Execute every mid-night
0 0 0 * * => Execute every Month
0 0 0 0 * => Execute every Weekday
*/5 * * * * => Execute every 2 minutes
0 */2 * * * => Execute two Hours

Commands can be broken down as
[PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

######Run PHP Script#########
/usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

######Run Ruby Script#########
#Ruby cron job setting. Ruby will run cron jobs through script runner.
/usr/local/bin/ruby /home/inkkdds1/inkakinada/script/runner /home/inkkdds1/inkakinada/app/cron_job_database.rb
#ruby-path script-runner-path ruby-file-path

######Run Shell Script#########
cd /home/devinka/public_html/spocosy/ && sh cron.xmlParse.sh
#cd to application-path && sh shell-script-path


Linux Shell Scripting Secrets

Monday, August 9, 2010

Very Usefull Ruby on Rails URLs

10 Reasons to learn ruby
http://www.h3rald.com/articles/10-reasons-to-learn-ruby/

Awesome ruby tidbits
http://www.rubyinside.com/interesting-ruby-tidbits-that-dont-warrant-separate-posts-1-600.html

Web based deployment using webistrano
http://blog.innerewut.de/webistrano/


Thursday, August 5, 2010

URL push and URL pull to send / retrieve information between cross domains

Hello friend,
Today I have done a small but very useful functionality i.e URL push and URL pull means you can send information to remote url and retieve information from other url. Getting information is easy and i think all developers know it, but sending information is little tricky when we don't know how the post variable names.
Below is the code to track the post parameter names and values in targetsite.
//extract($_POST);
foreach($_POST as $key=>$value)
{
echo $key.'='.$value.'
';
}
echo $_SERVER['HTTP_REFERER'];
/*Output:
key = value
key = value
etc..,
http://from-site-name.com/filename
*/
?>
So here in foreach loop you will get the post param names and values. So you can store in db with table structure as id autoincrement, key, value, referrer,posted_on. OR use xml file to store the information in structured way like valuevalueetc..,

Ex: http://formsite-name.com/postinfo.php
##################################################################
form action="http://targetsite.com/getvalues.php" method="post"
Name:input type="text" name="name"
Address:input type="text" name="address"
Email:input type="text" name="email"
input type="submit" value="Submit"
/form
#####################################################################

When user submits the above form the remote script (i.e mentioned in form action will execute) here it is at http://targetsite.com/getvalues.php
//extract($_POST);
foreach($_POST as $key=>$value)
{
echo $key.'='.$value.'
';
}
echo $_SERVER['HTTP_REFERER'];
/*Output:
name = postedname
address = postedaddress
email = postedemail
http://formsite-name.com/postinfo.php
*/
?>

Tuesday, August 3, 2010

Don't do more work than you have to - Use auto deploy tools

Hello,

Human beings can't remember everything but machines can. Everyday you work on a project and changed many files and altered database tables but forgot some things to update in live server while depoloying code. Use some third party tools to deploy your php applications without missing small change or db updates. There are many such tools available, capistrano is one of the famous tool for automate the deployment process. Many people think it is only for RoR applications deployment but it is for all. You can find more info for how to auto deploy php applications with capistrano from here http://www.jonmaddox.com/2006/08/16/automated-php-deployment-with-capistrano/#comment-10668