Wednesday, January 11, 2012

Drush : Set password, watchdog entries and run sql-query

The more you learn about Drush, the more you will love it. Generally most of the developers use Drush only for download modules, install modules and clear cache. But there is more to Drush than that.

1. You have a Drupal instance and you don't remember the password for the admin.

drush user-password admin  --password="IamNotadmin$123"

converting the password to MD5 and storing it in DB drush will take care :)

2. Recently I had an instance of Drupal to which I was not able to login. I wanted to check out the watchdog entries, but as I was not able to login I had to check it from db. I thought drush may help me out and it did.


drush watchdog-show  

You can also use watchdog-list which will give you more control. This throws up a set of options like cc does and you can select what you want
drush watchdog-list 

3. If you are drupal developer and you are using, it's a safe bet to assume that you are using sql-cli. But the pain with that is that you need to remove the braces from the table names to actuall run the query.


drush sql-query "SELECT u.name, u.status from {users} u where uid = 1"



And you can see the result of the query ;) Saves a  lot of time. Doesn't it?

Tuesday, January 3, 2012

Drupal 6 : PHP snipper to get a list of all the content types with respective fields.

Here is small snippet of php code that will help you get a list of all Content types in an instance along with the fields within the content type. It came handy to me when I wanted to give a list of conent types with fields to one of my clients for remapping them to the new structure that they wanted to build.

  
  $content_types = node_get_types();
  $output = '';
  foreach($content_types as $content_type => $content_type_object) {
    $output .= "" . $content_type_object->name . "";
    $output .= '
';
    $fields = _content_type_info();
    $fields = $fields['content types'][$content_type]['fields'];
    $ouput = '';
    foreach ($fields as $field_name=>$field) {
      $output .= $field['widget']['label'];
      $output .= '
';
    }
  }

Wednesday, December 28, 2011

Using drush to get a list of enable modules

If you just wanted to make a list of all the modules which are installed in your instance. Don't waste time, be smart write a command using drush.

  1. List all modules and themes : drush pm-list
  2. List only modules : drush pm-list --type=Module
  3. List all modules except core modules : drush pm-list --type=Module --no core
  4. List all enabled modules except those from the core : drush pm-list --type=Module --no core -- status=enabled
  5. List all enabled modules except those from the core which belong to the CCK package : drush pm-list --type=Module --no core --package=CCK status=enabled
If you want more info about the pm-list
  1. drush help pm-list


Tuesday, October 4, 2011

Some frequently used commands in Ubuntu Linux for SQL and DRupal

MYSQL : 
To create a database, to create a user and then to give all the permissions on that database to the user.



create database my_database_name_to_be_created; 
create USER 'username_to_be_created'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON  my_database_name_to_be_created.* TO 'username_to_be_created'@'localhost' ;
FLUSH ALL PRIVILEGES;



Linux/ Mysql : 
To take a dump of the data base.

mysqldump -u root -p databased_to_be_dumped> dump_file_name.sql;


To create a new database
mysql -u root -p name_of_the_database_to_which_dump_is_loaded < dump_file_name.sql

Linux : 

find . -maxdepth 1 -exec mv {} .. \;

Monday, July 4, 2011

How to remove few formatting options from CK editor.

  • Download and install CK editor.
  • Make sure that you have given the permissions.
  • Associate the roles with the CK editor profiles.
  • Now edit the appropriate profile. For example if you want to edit the CK editor for the authenticated user then you will have to edit the Advanced profile editor.


  • Click open the "Advanced Options" fieldset.





And copy paste the following code so that it looks like the image on the right.
format_tags = 'p;pre;h4;h5;h6'
removeFormatTags = 'b;big;code;del;dfn;em;font;i;ins;kbd;q;samp;small;span;strike;strong;sub;sup;tt;u;var;h1;h2;h3' 




How to make titles optional and generate title from body text

Head to automatic nodetitles , download and install the module.

Goto /admin/content/node-type/entry under the "Automatic title generation" field set select the second or third option and paste the code below.

//the least number of words in a scentence when trying to extract a complete scentence
$w_sentence= 1;
//if the scentence obtained contains more words than this it will be truncated to this many words and "..."
$w_wordnum = 10;
//if the number of characters after all truncation is above this number it will be truncated to this many characters and "...",  This is to protect agains extremely long words
$w_maxchar = 50;

// this is the name of the cck node token you want to use for example"
$bodyfield = '[field_sms_text-formatted]';


function summarize($paragraph, $limit)
    // cuts the 
       {
           $tok = strtok($paragraph, " ");
           while($tok)
           {
               $text .= " $tok";
               $words++;
               if(($words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == ".")))
                   break;
               $tok = strtok(" ");
           }
           
           return ltrim($text);
       }

function shorten_string($string, $wordsreturned)
     /*  Returns the first $wordsreturned out of $string.  If string
    contains more words than $wordsreturned, the entire string
    is returned.
    */
    {
    $retval = $string;    //    Just in case of a problem
    $array = explode(" ", $string);
    if (count($array)<=$wordsreturned)
    /*  Already short enough, return the whole thing
        */
        {
        $retval = $string;
        }
    else
    /*  Need to chop of some words
        */
        {
        array_splice($array, $wordsreturned);
        $retval = implode(" ", $array)."...";
        }
    return $retval;
    }     

// if somone managed to put in html we have to clean it out
$bodyfield = strip_tags($bodyfield);    

// multiple spaces in my editor were correctly converted to hardspaces but it confuses drupal so we need to remove them
$bodyfield = str_replace(' ', " ", $bodyfield);
$bodyfield = trim($bodyfield);


$bodyfield = summarize($bodyfield, $w_sentence);
$bodyfield = shorten_string($bodyfield, $w_wordnum);

// to protect agains extremely long words we also have a character based rule
if(strlen($bodyfield) > $w_maxchar )
    {
    $bodyfield = substr($bodyfield,0,$w_maxchar)."...";
    }
    
print check_plain($bodyfield);
    

Monday, June 20, 2011

Drupal Taleo Integration with SOAPClient

Taleo is a Talent Management System. Many companies use Taleo for the recruitment services. Taleo has an API which can be used to make API calls to fetch data from the Taleo servers. Though there is documentation about the Taleo integration, it is confusing at places and misleading at some.

The following code helps us to fetch job titles and location from the Taleo server. The explanation of each block of code follows next.

try {
  $client = new SoapClient("http://tbe.taleo.net/wsdl/DispatcherAPI.wsdl", array('trace' => true)); 
  $url = $client->__call('getURL', $company_name);
  $clientTaleo = new SoapClient("http://tbe.taleo.net/wsdl/WebAPI.wsdl", array('trace' => true));
  $clientTaleo->__setLocation($url);
  $params = array(
             'in0' => $comp_name,
             'in1' => $user_name,
             'in2' => $password,
  );
  $session =  $clientTaleo->__call('login', $params);
  $params = array(
           'in0' => $session,
           'in1' => array('status' => 'Filled', 'city' => 'Bothell',)
          );
  $jobIds =  $clientTaleo->__call('searchRequisition', $params);
            foreach($jobIds->array->item as $jobid){
              $params = array(
                'in0' => $session,
                'in1' => $jobid->id,
                );

                $job = $clientTaleo->__call('getRequisitionById', $params);
                print $job->title . '  ' . $job->location;
            }  
  
            
} catch (SoapFault $fault) {
            $error = 1;
            print("
            
'Sorry, blah returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring
            );
} 


You need to follow a three step procedure to get any meaningful data from taleo.

1) Access a fixed url to get a local-url for your company : Based on your location you get different urls. This is done for load balancing.
$client = new SoapClient("http://tbe.taleo.net/wsdl/DispatcherAPI.wsdl", array('trace' => true)); 
 $url = $client->__call('getURL', $company_name);

Gives you a url like : https://tbe.taleo.net/NA7/ats/services/rpcrouter
2) Authenticate via the url retrieved from first step to get a session token : This session token changes on every request you make.
$clientTaleo = new SoapClient("http://tbe.taleo.net/wsdl/WebAPI.wsdl", array('trace' => true));
  $clientTaleo->__setLocation($url);
  $params = array(
             'in0' => $comp_name,
             'in1' => $user_name,
             'in2' => $password,
  );
  $session =  $clientTaleo->__call('login', $params);  

3) Use the session token from the second step to invoke the various Taleo API methods.
$params = array(
            'in0' => $session,
            'in1' => $jobid_id,
          );

$job = $clientTaleo->__call('getRequisitionById', $params);
print $job->title . '  ' . $job->location;