Opensource, CMS, PHP, MySql, Drupal, Frameworks

Tuesday, August 16, 2011

SVN Commands

Wednesday, July 20, 2011

php serialize and unserialize

This process makes a storable representation of a value that is useful for storing or passing PHP values around without losing their type and structure.

<?php
    $the_array = array( "Merriet", "Jerry", "Drupal", "PHP" );
    $serialized = serialize($the_array);
    print $serialized.'<br/>';
    print_r(unserialize($serialized));
?>

Output:
Serialize: a:4:{i:0;s:7:"Merriet";i:1;s:5:"Jerry";i:2;s:6:"Drupal";i:3;s:3:"PHP";}
Unserialize: Array ( [0] => Merriet [1] => Jerry [2] => Drupal [3] => PHP )

Thursday, February 24, 2011

JavaScript Theming in Drupal


$(document).ready(function () {
     var links = [
         {name: 'Drupal.org', link:'http://drupal.org'},
         {name: 'jQuery', link:'http://jquery.com'},
         {name: 'No Link'}
    ];
    var text = Drupal.theme('shallow_menu', links);
    var blockInfo = {
         title: 'JavaScript Menu',
         content: text
    };
    var block = Drupal.theme('block', blockInfo);
    $('.block:first').before(block);
});

//Block Prototype
Drupal.theme.prototype.block = function (block) {
     if (!block.id) {
          block.id = "frobnitz-" + Math.floor(Math.random() * 9999);
     }
     var text = '<div class="block block-frobnitz" id="block-' +
          block.id +
          '">' +
          '<h2 class="title">' +
          block.title +
          '</h2><div class="content">' +
          block.content +
          '</div></div>';
     return text;
};

//Menu Prototype
Drupal.theme.prototype.shallow_menu = function (items) {

     var list = $('<ul class="menu"></ul>');
     for (var i = 0; i < items.length; ++i) {
          var item = items[i];
         // Get text for menu item
         var menuText = null;
         if (item.link) {
              menuText = item.name.link(item.link);
         }
         else {
              menuText = item.name;
         }
         // Create item
         var li = $('<li class="leaf"></li>');
         // figure out if this is first or last
         if (i == 0) {
              li.addClass('first');
         }
         else if (i == items.length - 1) {
              li.addClass('last');
         }
         // Add item to list
         li.html(menuText).appendTo(list);
     }
     return list.parent().html();
};

Wednesday, February 23, 2011

drupal with jquery

Drupal.jsEnabled
This variable indicates whether or not the browser will support drupal.js and jquery.js. Returns true / false.


Drupal.attachBehaviors()
The Drupal Behaviors feature provides a standard method for attaching some particular information (called a behavior) to zero or more elements on a page.


Drupal.checkPlain()
It allow us to display HTML tags in browser.
example: 
myString = "Add a line break using the <br/> tag.";
escapedString = Drupal.checkPlain(myString);
output: Add a line break using the &lt;br/&gt; tag.

Drupal.parseJson()
This function was intended to be used for parsing AJAX data that was returned from the server in the JSON (JavaScript Object Notation) format. JSON data looks like JavaScript.
Here's an example describing a person's name:
jsonString = "{'first': 'Merriet', 'last': 'Jerry'}";
name = Drupal.parseJson(jsonString);
name.first
Output: Merriet
The string is parsed and converted to an object. We can then use that object directly in JavaScript.

Drupal.encodeURIComponent()
It correctly converts other characters while preserving the slashes.
myString = 'node/1/calc%';
Drupal.encodeURIComponent(myString);
Output: node/1/calc%25

Drupal.getSelection()
This tool is used to find out what portion of a text area (<textarea></textarea>) is selected.
For example, when you select a section of text with your mouse, this function can be used to find out the starting and ending locations of the selection.
The Drupal.getSelection() function returns an object with two attributes: start and end. These two properties are integers which represent the beginning and ending of the selection.

Drupal.t()
This function's job is to take a string and perform any translation actions on it.
var params = {
     "@siteName": "Example.Com",
     "!url": "http://example.com/"
};
var txt = Drupal.t("The URL for @siteName is !url.", params);


Drupal.formatPlural()
for (i = 0; i < 6; ++i) {
     alert( Drupal.formatPlural(i, "Johnny has 1 apple.", "Johnny has @count apples."));
}
This function takes these arguments:
  • A number (If it is 1, then the singular will be used, otherwise the plural form will be used.)
  • A singular string (in English).
  • A plural string (in English)

Drupal 7 Marketing Video




append() and appendTo() jQuery methods


An easily overlooked difference between append() and appendTo() is in the return value; both return jQuery objects. But where append() returns a jQuery object wrapping the thing(s) that have been appended to, the appendTo() call returns the elements that were appended.


//Create our content    
var appendHTML = "<p>I'm going to be added to the DOM</p>";


//Select our element, then add the html     
$("#container").append(appendHTML);


//Create our content, then select the element to append to
$(appendHTML).appentTo("#container");

Wednesday, February 16, 2011

Rotate content using jquery

Create the sticky_rotate.js


var StickyRotate = StickyRotate || {};
StickyRotate.init = function() {
var stickies = $(".sticky");
// If we don't have enough, stop immediately.
if (stickies.size() <= 1 || $('#node-form').size() > 0) {
return;
}
var highest = 100;
stickies.each(function () {
var stickyHeight = $(this).height();
if(stickyHeight > highest) {
highest = stickyHeight;
}
});
stickies.hide().css('height', highest + 'px');
StickyRotate.counter = 0;
StickyRotate.stickies = stickies;
stickies.eq(0).fadeIn('slow');
setInterval(StickyRotate.periodicRefresh, 7000);
};


StickyRotate.periodicRefresh = function () {
var stickies = StickyRotate.stickies;
var count = StickyRotate.counter;
var lastSticky = stickies.size() - 1;
var newcount;
if (count == lastSticky) {
newcount = StickyRotate.counter = 0;
}
else {
newcount = StickyRotate.counter = count + 1;
}
stickies.eq(count).fadeOut('slow', function () {
stickies.eq(newcount).fadeIn('slow');
});
};


Call the script by using 
$(document).ready(StickyRotate.init);


Ref book: Drupal 6 JavaScript and jQuery 

Tuesday, February 15, 2011

filters and input formats in drupal

Input formats are made up of a collection of filters. Shown in this figure are Drupal’s three default input formats. The direction of execution is shown by arrows.




To view a list of installed filters, either configure an existing input format or create a new one at Administrator >> Site Cofiguration >> Input formats 


Life cycle of text-filtering system






Ref book: Pro Drupal Development

print page content using drupal and javascript

Create a javascript file: print_page.js

var PrinterTool = {};
PrinterTool.windowSettings = 'toolbar = no, location = no,' + ' status = no, menu = no, scrollbars = yes, width = 650, height = 400';
PrinterTool.print = function (tagID) { 
     var target = document.getElementById(tagID);
     var title = document.title;
     if(!target || target.childNodes.length === 0) {
         alert("Nothing to Print");
         return;
     }
     var content = target.innerHTML;
     var text = '<html><head><title>' + title + 
                     '</title><body>' + content + '</body></html>';
     printerWindow = window.open('', '', PrinterTool.windowSettings);
     printerWindow.document.open();
     printerWindow.document.write(text);
     printerWindow.document.close();
   printerWindow.print();
};


Call the function with ID.  Paste following code in your tpl file/ block
<a href="javascript:PrinterTool.print('main')" >Print</a>

Ref book: Drupal 6 JavaScript and jQuery 

Monday, February 14, 2011

Setup drupal subsite in local machine


10 Steps to setup subsite in local machine with windows Environment. 


1. Create a subfolder subsite under drupal6/sites/subsite.(drupal6 is an main site)


2. Copy the default.settings.php from sites/default folder and renamed as settings.php, then paste it into drupal6/sites/subsite/settings.php


3. Create folders themes and modules under sites/subsite/themes and sites/subsite/modules. Place the site specific themes and modules.


4. Open the file httpd-vhosts.conf from bin/apache/conf/extra/ httpd-vhosts.conf


5. Add the following code at the end of file.
<VirtualHost *:80>
                     DocumentRoot "E:/Jerry/wamp/www/drupal6"
                     ServerName subsite
               </VirtualHost>
Note: drupal6 is an main site. The subsite folder under wamp/www/drupal6/sites/subsite


6. In windows system goto start > run (win+r) and type drivers and press ok.


7. Then open the folder etc. edit the file hosts
Add the host name under default host like..
             127.0.0.1 tobylove6
             127.0.0.1 subsite


8. Add our sitename in browsers noproxy settings.
In Mozilla:
Method 1
* Open  Tools >> options 
* Select network tab under advanced tab.
* Click button settings.
* Choose  no proxy and press ok.

Method 2
* Open  Tools >> options 
* Select network tab under advanced tab.
* Click button settings.
* Choose  Manual proxy configuration and enter the proxy details.
* Enter subsite in noproxy for text box. Like localhost,subsite
* Then press ok.

9. Restart the apache server. Then run the subsite as http://subsite. It will show the drupal installation window.

10. Follow the drupal installation steps.


Alter page layout based on content type

Call the differant page layouts by using following code..

function phptemplate_preprocess_page(&$vars) {
if($vars['node']->type == 'story'){
$vars['template_files'][] = 'page-story';
}
}

Friday, February 11, 2011

Switch Contact form template In Drupal

step 1: call template

function garland_theme(){
return array(
'contact_mail_page' => array(
'arguments' => array('form' => NULL),
'template' => 'contact-mail-page',
),
);
}

step 2: create template files
contact-mail-page-info.tpl.php
contact-mail-page-quote.tpl.php

print $form_markup;

step 3: Create Preprocess Function
function garland_preprocess_contact_mail_page(&$vars){
switch(arg(1)){
case 'quote':
$vars['form']['name']['#title'] = t('Name');
$vars['form']['message']['#title'] = t('What you need');
$vars['template_file'] = 'contact-mail-page-quote';
break;
case 'info':
$vars['form']['message']['#title'] = t('what info you need??');
$vars['template_file'] = 'contact-mail-page-info';
break;
}

$vars['form_markup'] = drupal_render($vars['form']);
}

Implement Drupal Autocomplete

Menu

function custom_menu(){
$items = array();
$items['custom/article'] = array(
'title' => t('Hello'),
'page callback' => 'article_load',
'access callback' => 'user_access',
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
);
return $items;
}


Form

$form['article'] = array(
'#type' => 'textfield',
'#title' => t('Article Title'),
'#description' => t('Known article'),
'#default_value' => $form['_account']['#value']->article,
'#autocomplete_path' => 'custom/article',
);


function article_load($string = '') {
$matches = array();
if ($string) {
$result = db_query_range("SELECT nid, title FROM {node} WHERE LOWER(title) LIKE LOWER('%s%%')", $string, 0, 10);
while ($user = db_fetch_object($result)) {
$matches[$user->title] = check_plain($user->title);
}
}
drupal_json($matches);
}