Graham's Blog

Categories:

Assign user to a role programmatically

<?php
function _assign_user_role($order, $account) // This assigns the user to "Consumer" role.
{
	$usr = user_load($order->uid);
	$details = array(
		'uid' => $order->uid,
		'roles' => array(
			6 => 'Consumer' // 6 is the role ID, may need to update this on going live
			)
		);
	user_save($usr, $details);
}
?>

Add conversion script to Ubercart's checkout completion page

<?php
 
function phptemplate_preprocess_page(&$vars) {
 
	if(drupal_get_path_alias($_GET['q']) == 'cart/checkout/complete') {
 
		$vars['closure'] .= 'conversion script';
 
	}
}
 
?>

Output Footer links from primary nav links

<?php
	$links = menu_primary_links(); 
	foreach($links as $link):
		$x++;
		echo('<a href="' . url($link['href']) . '">' . $link['title'] . '</a> ' . ((count($links) == $x) ? "" : "|") );
	endforeach;
?>

or

<?php
$links = menu_navigation_links("primary-links");
	foreach($links as $link):
		$x++;
		echo('<li><a href="' . url($link['href']) . '">' . $link['title'] . '</a></li> ');
	endforeach;
?>

Change Drupal Blog Title tag and first H1 Title

Add this to template.php

<?php
 
function phptemplate_preprocess_page(&$vars) {
 
	$args = arg();
	if($args[0] == 'blog')
	{
		$vars['head_title'] = preg_replace("^Blogs^", "User's Blog", $vars['head_title']);
		$vars['title'] = "User's Blog";
	}
 
}
 
?>

Delete all nodes from specific node type

<?php
  $node_type = 'page';
 
  //fetch the nodes we want to delete
  $result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);
  while ($row = db_fetch_object($result)){
    node_delete($row->nid);
    $deleted_count+=1;
  }
  //simple debug message so we can see what had been deleted.
  drupal_set_message("$deleted_count nodes have been deleted");
?>

Rsync mirror files excluding svn files

This rsyncs from a ssh server to your local machine, excluding svn files, and it has delete flag so its a mirror copy.

rsync -av --delete --progress --exclude '.svn/' username@domain.com:/some/directory ~/Desktop/backup/

Block access to SVN files via apache .htaccess

# Block access to SVN files to the outside world
<IfModule mod_rewrite.c>
  RewriteRule ^(.*/)?\.svn/ - [F,L]
  ErrorDocument 403 "Access Forbidden"
</IfModule>

Add shareasale tracking code to Drupal Ubercart order completion page

<?php
 
function phptemplate_preprocess_page(&$vars) {
 
	if(drupal_get_path_alias($_GET['q']) == 'cart/checkout/complete') {
 
		// Getting order id
		preg_match("^Your order number is (\d*).^", $vars['content'], $matches);
		$order_id = $matches[1];
 
		$order = uc_order_load($order_id);
 
		$vars['closure'] .= '<img src="https://shareasale.com/sale.cfm?amount=' . $order->order_total . '&tracking=' . $order->order_id . '&transtype=sale&merchantID=XXXXXX" width="1" height="1">';
 
	}
}
 
?>

Login programmatically on register

function _login(&$form_state, &$edit)
{	
	$email = $form_state['account']['name']['#post']['mail'];
	$user = user_load(array('mail' => $email));
	user_external_login($user);
}
 
function custom_form_alter(&$form, $form_state, $form_id)
{
	if($form_id == "user_register"):
		$form['#submit'][] = '_login';
	endif;
 
	return $form;
}

Modify submitted values from user form before processing

function custom_user($op, &$edit, &$account, $category=NULL)
{
 
	switch($op) {
		case 'validate':
			if(is_array($account)) // checking to make sure we're only doing this on register.
			{
				$edit['profile_name'] = $edit['name'];  // setting profile name as username (name)
				$edit['name'] = $edit['mail']; // setting username (name) as mail address
			}
		break;
	}
}