Thursday, December 26, 2013

Create relationships within users

https://drupal.org/project/user_relationships
https://drupal.org/files/project-images/Selection_035.jpeg
This module is abig one which you can use to create relationship betwwne them

remember

you need to set permissions in oreder to make it functional

add relationship types form admin

admin/config/people/relationships/add

manage

admin/config/people/relationships

then assign permissions 

and first assign permission to view other people profiles. View user profiles under user so that users can send request to each other.







Simple and good module to adds Terms of Use and a [x] I agree check box to the registration page.


https://drupal.org/files/images/terms_of_use_0.jpg

Terms of Use

https://drupal.org/project/terms_of_use

This module adds Terms of Use and a [x] I agree check box to the registration page.

The Multiselect module defines a widget to be used with CCK fields

Multiselect Screenshot

This modules will add a widget named  Multiselect when you add a list field 

https://drupal.org/project/multiselect

Tuesday, December 24, 2013

Save node of Content Type Programatically

We can save a node of any content type programatically in Drupal It Rocks!!

 $node = new stdClass(); 

// Set content type
$node->type = 'product';
 
// Prepare defaults
node_object_prepare($node);

$node->language = LANGUAGE_NONE;
 
// Define language (currently language neutral)

 
// Basic content
$node->title = 'Save Content Type Node Programatically!!';
$node->body[$node->language][0]['value'] = 'Body text';

         //CCK or Added fields
$node->field_product_email[$node->language][0]['value'] = 'save@gmail.com';
 
// Save node
node_save($node);

Saturday, December 21, 2013

Play Video in Drupal (FlowPlayer)

Install

https://drupal.org/project/flowplayer

Add a field to your content named suppose named field_upload_videos assing some aloowed extension to it 

And then add below code to your Tpl File

<?php
  $video = '<a href="'.$base_path.'/sites/default/files/videos/'.$node->field_upload_videos[und][0][filename].'" id="player" class="flowplayer"></a>';
  flowplayer_add('#player', array(
    'clip' => array(
      'autoPlay' => FALSE, // Turn autoplay off
      //'linkUrl' => '/sites/default/files/videos/truduo.mp4', // When clicked on
    ),
  ));
print $video;
?>


You are done Enjoy!!.

Monday, December 16, 2013

Load Profile2 fields

global $user;

$profile = profile2_by_uid_load($user->uid, 'main');  

//Get exact field names by printing the array.

print "<pre>"; print_r($profile); print "</pre>"; // to know yourm all fields name

//Example to print profile fields 

print $profile->field_business_name['und'][0]['value'];

print $profile->field_first_name['und'][0]['value'];

print $profile->field_business_address['und'][0]['value'];

print $profile->field_street_address['und'][0]['value'];

print $profile->field_address_line_2['und'][0]['value'];

print $profile->field_country['und'][0]['iso2'];

print $profile->field_state_province_region['und'][0]['value']];

print $profile->field_city['und'][0]['value']];

print $profile->field_postal_zip_code['und'][0]['value']];

print $profile->field_phone['und'][0]['value'];

print $profile->field_fax_number['und'][0]['value'];

Save Profile 2 fields manually

global $user;

if(in_array('business membership', $user->roles)) {
  $profile = profile2_by_uid_load($user->uid, 'business_membership');
}
else { 
  $profile = profile2_by_uid_load($user->uid, 'main'); 
}
if(isset($_REQUEST['saveprofile'])) {
  if($_FILES["field_your_profile_photo"]["name"]) {
$filename=uniqid().$_FILES["field_your_profile_photo"]["name"];
$_FILES["field_your_profile_photo"]["type"];
$_FILES["field_your_profile_photo"]["size"];
$_FILES["field_your_profile_photo"]["tmp_name"];
 move_uploaded_file($_FILES["field_your_profile_photo"]["tmp_name"],$_SERVER['DOCUMENT_ROOT']."/goodstreet/sites/default/files/product/" .$filename);
//print $profile->field_business_name['und'][0]['value'];
$image_path = 'public://product/'. $filename;
$image_info = image_get_info($image_path);
   //print_r($image_info); exit();
// create file object
$file = new StdClass();
$file->uid = $uid;
$file->uri = $image_path;
$file->filemime = $image_info['mime_type'];
$file->status = 0; // Yes! Set status to 0 in order to save temporary file.
$file->filesize = $image_info['file_size'];
print $file->filename=$filename;
file_save($file);
$profile->field_your_profile_photo = array(
'und' => array(
0 => array(
'fid' => $file->fid,
'filename' => $file->filename,
'filemime' => $file->filemime,
'uid' => $uid,
'uri' => $file->uri,
'status' => 1
)
)
);
}
$profile->field_business_name['und'][0]['value']=$_REQUEST['field_business_name'];
//$profile->field_your_profile_photo['und'][0]['uri']='public://product/'. $_FILES["field_your_profile_photo"]["name"];
//$profile->field_your_profile_photo['und'][0]['filename']=$_FILES["field_your_profile_photo"]["name"];
$profile->field_business_address['und'][0]['value']=$_REQUEST['field_business_address'];
$profile->field_street_address['und'][0]['value']=$_REQUEST['field_street_address'];
$profile->field_address_line_2['und'][0]['value']=$_REQUEST['field_address_line_2'];
$profile->field_country['und'][0]['value']=$_REQUEST['field_country'];
$profile->field_state_province_region['und'][0]['value']=$_REQUEST['field_state_province_region'];
$profile->field_city['und'][0]['value']=$_REQUEST['field_city'];
$profile->field_postal_zip_code['und'][0]['value']=$_REQUEST['field_postal_zip_code'];
$profile->field_phone['und'][0]['value']=$_REQUEST['field_phone'];
$profile->field_fax_number['und'][0]['value']=$_REQUEST['field_fax_number'];
profile2_save($profile);
//user_save($existingUser, $edit);
//echo "<pre>"; print_r($user); echo "</pre>";
//print $user->init;
drupal_goto('profile-business_membership');
}

Save User programitically

 global $user;
$existingUser = user_load($user->uid);

$existingUser->name = $_REQUEST['user_name'];
$existingUser->mail = $_REQUEST['user_mail'];
$existingUser->init = $_REQUEST['user_mail'];
if(!empty($_REQUEST['user_pass'])) {
$existingUser->pass = $_REQUEST['user_pass']; 
}
user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);

Upload Image in Drupalize Way

if($_FILES["field_your_profile_photo"]["name"]) {
$filename=uniqid().$_FILES["field_your_profile_photo"]["name"];
$_FILES["field_your_profile_photo"]["type"];
$_FILES["field_your_profile_photo"]["size"];
$_FILES["field_your_profile_photo"]["tmp_name"];
 move_uploaded_file($_FILES["field_your_profile_photo"]["tmp_name"],$_SERVER['DOCUMENT_ROOT']."/goodstreet/sites/default/files/product/" .$filename);
//print $profile->field_business_name['und'][0]['value'];
$image_path = 'public://product/'. $filename;
$image_info = image_get_info($image_path);
   //print_r($image_info); exit();
// create file object
$file = new StdClass();
$file->uid = $uid;
$file->uri = $image_path;
$file->filemime = $image_info['mime_type'];
$file->status = 0; // Yes! Set status to 0 in order to save temporary file.
$file->filesize = $image_info['file_size'];
print $file->filename=$filename;
file_save($file);
$profile->field_your_profile_photo = array(
'und' => array(
0 => array(
'fid' => $file->fid,
'filename' => $file->filename,
'filemime' => $file->filemime,
'uid' => $uid,
'uri' => $file->uri,
'status' => 1
)
)
);
}

US states list Drupal

<?php $options = array(
'AL' => ('Alabama'),
'AK' => ('Alaska'),
'AZ' => ('Arizona'),
'AR' => ('Arkansas'),
'CA' => ('California'),
'CO' => ('Colorado'),
'CT' => ('Connecticut'),
'DE' => ('Delaware'),
'DC' => ('District of Columbia'),
'FL' => ('Florida'),
'GA' => ('Georgia'),
'HI' => ('Hawaii'),
'ID' => ('Idaho'),
'IL' => ('Illinois'),
'IN' => ('Indiana'),
'IA' => ('Iowa'),
'KS' => ('Kansas'),
'KY' => ('Kentucky'),
'LA' => ('Louisiana'),
'ME' => ('Maine'),
'MD' => ('Maryland'),
'MA' => ('Massachusetts'),
'MI' => ('Michigan'),
'MN' => ('Minnesota'),
'MS' => ('Mississippi'),
'MO' => ('Missouri'),
'MY' => ('Montana'),
'NE' => ('Nebraska'),
'NV' => ('Nevada'),
'NH' => ('New Hampshire'),
'NJ' => ('New Jersey'),
'NM' => ('New Mexico'),
'NY' => ('New York'),
'NC' => ('North Carolina'),
'ND' => ('North Dakota'),
'OH' => ('Ohio'),
'OK' => ('Oklahoma'),
'OR' => ('Oregon'),
'PA' => ('Pennsylvania'),
'RI' => ('Rhode Island'),
'SC' => ('South Carolina'),
'SD' => ('South Dakota'),
'TN' => ('Tennessee'),
'TX' => ('Texas'),
'UT' => ('Utah'),
'VT' => ('Vermont'),
'VA' => ('Virginia'),
'WA' => ('Washington'),
'WV' => ('West Virginia'),
'WI' => ('Wisconsin'),
'WY' => ('Wyoming'));
//print_r($options);
?>
    <select class="form-select formbox3" name="field_state_province_region">
    <?php
foreach($options as $key=>$val) {
?>
        <option value="<?php print $key;?>" <?php if($profile->field_state_province_region['und'][0]['value'] == $key) { ?> selected="selected" <?php } ?>><?php print $val;?></option>
        <?php
}
?>
    </select>

Fetch All Rows from table drupal 7

 <select class="form-select formbox3" name="field_country"><?php 
$result = db_query('SELECT * FROM {countries_country}');
// Result is returned as a iterable object that returns a stdClass object on each iteration
foreach ($result as $record) {
?>
   
 <option value="<?php print $record->iso2;?>" <?php if($profile->field_country['und'][0]['value'] == $record->iso2) { ?> selected="selected" <?php } ?>><?php print $record->name;?></option>
   
   <?php
}
?>
</select>

Identify node type in template.php

function MYTHEME_preprocess_page(&$variables) {
  if (!empty($variables['node']) && $variables['node']->type == 'NODETYPE') {
    $variables['greeting'] = 'Custom Greeting';
  }
} 

Check whether a view is empty or not

$view = views_get_view('today_deal_cupon2');
$view->set_arguments(array($bid));
$view->execute();
$view->render();
$view->set_display('default');
$output = $view->preview();

Get total number of unread message for private msg module drupal

print $inbox=privatemsg_unread_count($account = NULL);

Show views Programitaclly

$view = views_get_view('advanced_forum_topic_list');
$view->set_arguments(array($term->tid));
$view->execute();
$view->render();
print $view->render();

Drupal 7 Profile 2 Module


See video

Export data from a specific date.

To export data from a specific date.

https://drupal.org/project/cmf module on live site and

https://drupal.org/project/node_export on live site  and development site

hook_node_presave in D7

hook_node_presave($node)
Act on a node being inserted or updated.

Get CCK field values or newly added field values in Drupal

$node->field_listing_id['und'][0]['value'];


Drupal 7 has another array structure for storing field values in $node. There is a new array key for the language.
For example:
<?php
$node->field_your_field['en'][0]['value'];
?>

This can be quite useful but makes it difficult to access the right data.
So here is a handy solution:
<?php
$items = field_get_items('node', $node, 'field_yourfield', $node->language);
?>

You can leave the $node->language argument empty. If done the current language will be called automatically:
<?php
$items = field_get_items('node', $node, 'field_yourfield');
?>

As a result you get an array like you had in Drupal 6. Cool, isn't it?
But be aware that you have to take care about sanitisation when using raw values like this!
Update: When you just want to display a single field value using field formatters (with all the provided html) and sanitation included you should use:
<?php
$items = field_get_items('node', $node, 'field_yourfield');
$output = field_view_value('node', $node, 'field_yourfield', $items[$delta]);
?>

How to use node_load

$node=node_load($nid);
dsm($node); //for devel module user
print "<pre>";
print_r($node);
print "</pre>";

Displaying Custom Profile Fields in Drupal 6

    global $user;
    profile_load_profile($user);
    print $user->{profile_dob};
    print "<br>";
    print $user->{profile_add};
    print "<br>";
    print $user->{profile_country};
    print "<br>";

Select query drupal

$query = db_select('members','m')->fields('m')->condition('membership_id',$record['membership_id'],'='); 
$result = $query->execute();
$row = $result->fetchAssoc();
$row['resort_name'];

Join query drupal

$query = db_select('leveltracking', 'n')->fields('n');
$query->join('resorts', 'r', 'n.resort_id = r.resort_id');
$query->join('members', 'm', 'n.membership_id  = m.membership_id');
$result = $query->execute();
while($record = $result->fetchAssoc()) 
{
   $record['fromdate'];
}

Display form fileds on radio button select using #states

$form['membership_type'] = array(
'#type' => 'select', 
'#title' => t('Select Membership type'), 
'#default_value' => $node->selectbox, 
'#options' => $membership_type, 
'#description' => t('Choose an option'),
'#required' => TRUE,
  );
 $form['conditions1'] = array(
    '#type' => 'radios',
    '#title' => t('Select Attribute'),
 '#options' => array(0 => 'Rooms', 1 => 'Others'),
  );
  
  $form['using_free_nights_discount_forguest'] = array(  '#type' => 'textfield',
'#type' => 'textfield',
'#title' => t('Discount For Guest When Using Free Nights'), 
'#default_value' => $using_free_nights_discount_forguest,
'#states' => array(
'visible' => array(
  ':input[name="conditions1"]' => array('value' => t('0')),
),
'invisible' => array(
  array('value' => t('1')),
),
), 
'#size' => 29,
);
$form['notusing_free_nights_discount_forguest'] = array(  '#type' => 'textfield',
'#type' => 'textfield',
'#title' => t('Discount For Guest When Not Using Free Nights'), 
'#default_value' => $notusing_free_nights_discount_forguest, 
'#states' => array(
'visible' => array(
  ':input[name="conditions1"]' => array('value' => t('0')),
),
'invisible' => array(
  array('value' => t('1')),
),
),
'#size' => 29,
);
$form['notusing_free_nights_discount_formembers'] = array(  '#type' => 'textfield',
'#type' => 'textfield',
'#title' => t('Discount For Members When Not Using Free Nights'), 
'#default_value' => $notusing_free_nights_discount_formembers, 
'#states' => array(
'visible' => array(
  ':input[name="conditions1"]' => array('value' => t('0')),
),
'invisible' => array(
  array('value' => t('1')),
),
),
'#size' => 29,
);
$form['room_attribute_id'] = array(  '#type' => 'textfield',
'#type' => 'hidden',
'#title' => t('Room Attribute Id'), 
'#default_value' => 6, 
'#states' => array(
'visible' => array(
  ':input[name="conditions1"]' => array('value' => t('0')),
),
'invisible' => array(
  array('value' => t('1')),
),
),
'#size' => 29,
);
  
  $form['room_attributes'] = array(
'#type' => 'select', 
'#title' => t('Select Room Atributes'), 
'#default_value' => $node->selectbox, 
'#options' => $room_attributes,
'#description' => t('Choose an option'),
'#states' => array(
'visible' => array(
 ':input[name="conditions1"]' => array('value' => t('1')),
),
'invisible' => array(
  array('value' => t('0')),
),
    ),
  );
 
 $form['discount'] = array(
'#type' => 'textfield',
'#title' => t('Discount Rate'), 
'#size' => 29,
'#states' => array(
'visible' => array(
  ':input[name="conditions1"]' => array('value' => t('1')),
),
'invisible' => array(
  array('value' => t('0')),
),
),
);

$form['submit'] = array(
'#name' => 'submit',
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;

Insert query using db_query

db_query('INSERT INTO {temp_payments_astro} 
   (session_id, amt) ' . "VALUES ('%s', '%s')", 
   $sid, $amt);

Changing default text in Drupal 7

Drupal Between query

$query = db_select('members', 'n')->fields('n');

 $query->condition('date_of_registration', array($search_start_date, $search_end_date), 'BETWEEN');

 $result = $query->execute();
 while($record = $result->fetchAssoc()) {
}

Custom search box for a specific content type

Make a custom search box on my site that would only search a single content type, with results identical to if the user had done an advanced search and selected one content type only. The code below works perfectly for me. Replace "your_content_type" with the content type to which you'd like to restrict the search.


<form action="/search/node" method="post" id="search-form" class="search-form">
<div class="form-item">
<input type="text" class="input-text" value="" size="25" name="keys" />
<input type="submit" value="Search" name="op" title="Search" alt="Search" />
<input type="hidden" value="<?php print drupal_get_token('search_form'); ?>" name="form_token" />
<input type="hidden" value="search_form" id="edit-search-form" name="form_id" />
<input type="hidden" name="type[your_content_type]" id="edit-type-your_content_type" value="your_content_type" />
</div>
</form>

Breadcrumb problem in Drupal 6

Install http://drupal.org/project/menu_breadcrumb  and
print $breadcrumb; in your tpl file.

Nodes and vocabulary database relationship Drupal 6

Tables required 

1-term_node
2-term_data
3-vocabulary

$query5="SELECT * FROM term_node a, term_data b WHERE a.nid='$row[nid]' AND a.tid=b.tid  ";
$res5=mysql_query($query5);
while($row5=mysql_fetch_array($res5))
{
        $query6="SELECT * FROM vocabulary WHERE `vid`='$row5[vid]'";
$res6=mysql_query($query6);
$row6=mysql_fetch_array($res6);
 echo $row6['name'];
 echo ":";
 echo $row5['name'];
}

Identify home page in Drupal

<?php if($is_front){ ?>
               
               <?php } ?>