Goo.gl API: PHP

16 May

http://Goo.gl API to shorten your URL

<?php
$longUrl = 'http://facebook.com';
$apiKey = 'Your Google Api Key here';
//You can get API key here : http://code.google.com/apis/console/

$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');

curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($curlObj, CURLOPT_HEADER, 0);

curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));

curl_setopt($curlObj, CURLOPT_POST, 1);

curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

$response = curl_exec($curlObj);

$json = json_decode($response);

curl_close($curlObj);
echo 'Shortened URL ->'.$json->id;
?>

Using Tortoise SVN at broader level

17 Jan

Hello folks,

Today we are going to have a look on Using Tortoise SVN at broad level.

Assuming you already have tortoise SVN installed on your machine.
You can use it on your local machine with repository in some folder and working on some other folder. Repository can be on other server also (Just the difference in the URL which repository you are going to use – you will get to know about this here down in this blog)

Repository is where your code files/files revisions/comments are maintained.

This blog tells you about using SVN repository and SVN project on same machine. SVN project is the projects development copy on your machine.

Creating a repository

Creating a repository

Steps to create repository.
Go to say D:\
1. Create folder with the name of your project name. Say “myproject”.
2. Go in that directory and right click.
3. Choose TortoseSVN → Create repository here
4. Repository is created.

You are done with creating repository on your local machine. And repository path is file:///D:/myproject/

If you create repository on some other machine then you need to use that path. It is recommend to create repository on other machine as it is always safe.

SVN Checkout

SVN Checkout

Using repository created in above steps.
1. Create folder where you want to create your projects development copy (says C:\xampp\htdocs\drupal).
2. Right click on this folder created at above step and Select SVN checkout…
3. Enter URL of repository as → file:///D:/myproject/
In our case we are using above local path as repository is created on local machine only. If repository is created on some other machine in the network then that path needs to be used.
Select Fully recursive from drop down for checkout depth.
4. Click OK
5. Your development copy is ready. Now start adding files in C:\xampp\htdocs\drupal

 

Adding a file in repository

Adding a file in repository

How to add files in repository?
1. Create file in your project you want to add in repository (So far this file is not added in the repository so it will come with ? Icon – right click and refresh if you don’t see ? icon)
2. Right click on the file and select TortoiseSVN → Add.
3. Refresh
4. This file will come with + icons → meaning, file is added but not yet committed to the repository.
5. Right click on the file
Select SVN Commit…
6. File is added and committed to the repository.
7. Refresh

Now this file will have green check mark → meaning, file is identical with the same file in repository.

After commit

After commit

Commiting a file

Committing a file

Changing file content and committing to repository
If you want to change code in some file.

1. Right click on the file and select SVN Update. Updating is necessary before commit because you never know some other developer is working on that file.

Updating means taking changes committed by other developer in your copy.

2. Add you code in that file.

3. Right click on the file and select SVN commit.

This will commit your changes in the repository so when other developers take updates for that file they will get updated file with your code in it.

This way you are not overriding another developers code.

Key line is “Always SVN Update a file before SVN Commit”.

SVN update and SVN commit options are available when you right click on the file. You can do these actions on folder level also.

Thank you

Sachin

How to change drupal block theme

25 Aug

Are you looking to change Drupal Block theme? Dont worry. Here is the way you can do that.

You need one module and that is Block Theme. Download and install this module. No need to set any user permissions for this module.

After installation go to configure it at this URL /admin/settings/blocktheme

In Custom block template textbox enter following and save configuration.

myblock|Some name

Where the first name is the machine-readable name of your template which may contain only
alphanumerical characters, -, or _ . The second name is the user-friendly name that appears
in the selectionbox on the block edit form.

After this save configuration.

If your theme already has block.tpl.php then make a copy of it an rename it as “blocktheme-myblock.tpl.php”.

If block.tpl.php file does not exist in your theme (Yes, some themes do not have block.tpl.php) directory copy code below and name it as “block.tpl.php” also make one more copy of this code below and rename it as “blocktheme-myblock.tpl.php”. This blockthem-myblock.tpl.php is the theme file applied for the block you select and block.tpl.php will be applied for remaining blocks.

Code.

<?php
// $Id: block.tpl.php,v 1.3 2007/08/07 08:39:36 goba Exp $
?>
<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">

<?php if (!empty($block->subject)): ?>
  <h2><?php print $block->subject ?></h2>
<?php endif;?>

  <div class="content"><?php print $block->content ?></div>
</div>

Thats it.

Now go to /admin/build/block and configure any block you want to apply a theme to. On clicking a Configure link you will find one more section “Block Theme”. Select a theme from dropdown and Save block.

IMP: Do not forget to clear cached data from “/admin/settings/performance”.

Comments/Suggestions are welcome.

Thanks,
Sachin

How to embed views in drupal pages

22 Aug

Here is the PHP code you can use on any drupal ‘page’ with input format as php code.

<?php
$view = views_get_view('Attention_sitemap'); //View name to be added on the page
print $view->execute_display('default'); //Views display
?>

Kindly add your comments for any query or suggestion.

Thanks,
Sachin

How to change “apply” button text in Drupal Views exposed filters

5 Jul

1) Open your current themes template.php file

2) append following function in it.

3)

function YOUR_THEME_NAME_preprocess_views_exposed_form(&$vars, $hook)
     {

         // only alter the required form based on id
            if ($vars['form']['#id'] == 'views-exposed-form-user-search-page-2') {
              // Change the text on the submit button
              $vars['form']['submit']['#value'] = t('Search');
              // Rebuild the rendered version (submit button, rest remains unchanged)
              unset($vars['form']['submit']['#printed']);
              $vars['button'] = drupal_render($vars['form']['submit']);
         }
      }

4) Now clear your cache and its done
You can clear cache at following location
/admin/settings/performance
scroll down to the bottom of the page and click “clear cached data” button.

IMPORTANT : Replace text “YOUR_THEME_NAME” in above function name with your current theme name like if you are using theme garland. function name will be garland_preprocess_views_exposed_form. Use firebug or view source and write your views form id in form id in ‘views-exposed-form-user-search-page-2′)

and t(‘Search’) inside this write the text which you want to appear on button instead of ‘Apply’.

How to get rid of drupals default “/node” page

30 Jun

Do you want to get rid of drupals default “/node” page?

We all know Drupal has a default front page which shows a view of nodes promoted to home page.

We might forget that we can still go to this page and user can see the content in the manner we dont want user to see.

A simple trick is we can make it “page not found”.

We can use Drupal’s the custom_url_rewrite_inbound function..

Open your settings.php file (located at /sites/default/settings.php)

And append following code in it.

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
  if ($path == 'node') {
    //this will cause a page not found
    $result = 0;
  }
}

This will make your /node “page not found”

Thanks

How to create a drupal node programmatically

30 Jun

Many a times we want to create Drupal node programatically when there is a big data in excel format or in some other format and we want it to be added in our Drupal site through cck form cause its time consuming to do that data entry manually.

If you are finding way to do that data entry programatically then here is the solution.

Suppose you want to create nodes for your content type having fields – book number, book name, author name and publication year.

$node = new stdClass(); //Create instance of class stdClass which will create node for you.
$node->type = 'library'; //Name of the content type
$node->field_book_no[0][value] = 22;//Book number
$node->field_book_name[0][value] = "Drupal"; //Book name
$node->field_book_author[0][value]='Sachin'; //Author name
$node->field_year[0][value]='2011'; //Publication year
$node->uid = 1; // user id, 1 is created by admin
$node->status = 1;//1 is published, 0 is unpublished
$node->promote = 0;//1 is promote to home page, 0 is not to promote on home page
node_save($node); // Save this node

If you are having data in excel format then you can read that excel and and have above code in for/while loop to create nodes for all books.

I have done this by reading excel data and created thousands of nodes for my content type. I have code to read excel file and create these nodes but its not included in this post, you can find excel reader class on Google.

Thank you.