<?php
// $Id: client_portal_pathauto.inc, v1.0 2005/08/31 17:56:15 GMT Poetro Exp $

/*
 * Implementation of hook_pathauto().
 */ 
function client_portal_pathauto($op) {
  switch ($op) {
    case 'settings': 
      $settings = array();
      $settings['module'] = 'client_portal'; 
      $settings['groupheader'] = t('Client Portal path settings');
      $settings['patterndescr'] = t('Default path pattern');
      $settings['patterndefault'] = t('[title]');
      $settings['bulkname'] = t('Bulk update client portal paths');
      $settings['bulkdescr'] = 
        t('Generate aliases for all existing client portal items which do not already have aliases.'); 
      $settings['placeholders'] = array(
        t('[title]') => t('The title of the client portal item, with spaces and punctuation.'),
        t('[nid]') => t('The id number of the client portal item.'),
        t('[user]') => t('The name of the user who created the client portal item.'),
        t('[client]') => t('The name of the client whom the client portal is applied to.'),
        t('[yyyy]') => t('The year the client portal item was created.'),
        t('[mm]') => t('The two-digit month (01-12) the client portal item was created.'),
        t('[mon]') => t('The three-letter month (jan-dec) the client portal item was created.'),
        t('[dd]') => t('The two-digit day of the month (00-31) the client portal item was created.'),
        t('[day]') => t('The three-letter day of the week (sun-sat) that the client portal item was created.'),
        t('[hour]') => t('The two-digit hour (00-23) the client portal item was created.'),
        t('[min]') => t('The two-digit minute (00-59) the client portal item was created.'),
        t('[sec]') => t('The two-digit second (00-59) the client portal item was created.'),
        t('[week]') => t('The week number (1-52) of the year the client portal item was created.')
      );
		$settings['patternitems']['client_portal_portal'] = t('Pattern for client portal opening pages');
      return array2object($settings);
    default:
      break;
  } 
}

/**
 * Insert path_auto path for the node if path_auto module exists.
 */
function client_portal_pathauto_insert($node, $op) {
  if(module_exist('pathauto')) {
	  $placeholders = client_portal_get_placeholders($node);
	  $src = "user/$node->cid/portal";
	  $alias = pathauto_create_alias('client_portal', $op, $placeholders, $src, 'client_portal_portal');
	  $src = "node/$node->nid";
	  $alias = pathauto_create_alias('client_portal', $op, $placeholders, $src, $node->type);
  }
}

/**
 * Get the placeholders for the path_auto module.
 * Use the title and other node attributes to create an alias.
 */
function client_portal_get_placeholders($node) {
  // Do the simple stuff first
  $placeholders[t('[title]')] = $node->title;
  $placeholders[t('[yyyy]')] = date('Y', $node->created);
  $placeholders[t('[mm]')] = date('m', $node->created);
  $placeholders[t('[mon]')] = date('M', $node->created);
  $placeholders[t('[dd]')] = date('d', $node->created);
  $placeholders[t('[day]')] = date('D', $node->created);
  $placeholders[t('[hour]')] = date('H', $node->created);
  $placeholders[t('[min]')] = date('i', $node->created);
  $placeholders[t('[sec]')] = date('s', $node->created);
  $placeholders[t('[week]')] = date('W', $node->created);
  $placeholders[t('[nid]')] = $node->nid;
  $placeholders[t('[cid]')] = $node->cid;

  // Then the slightly less simple stuff
  // username information for the node creator
  $result = db_query("SELECT name FROM {users} WHERE uid='%u'", $node->uid);
  $userinfo = db_fetch_object($result);
  $placeholders[t('[user]')] = $userinfo->name;

  // username information for the client
  $result = db_query("SELECT name FROM {users} WHERE uid='%u'", $node->cid);
  $clientinfo = db_fetch_object($result);
  $placeholders[t('[client]')] = $clientinfo->name;
  return $placeholders;
}

/*
 * Implementation of hook_pathauto_bulkupdate().
 * Generate aliases for all nodes without aliases.
 */ 
function client_portal_pathauto_bulkupdate() {
  $query = 'SELECT DISTINCT (p.cid), n.nid, n.type, n.title, n.uid, n.created, u.src, u.dst FROM {node} n '.
    'LEFT JOIN {client_portal} p ON n.nid = p.nid '.
    "LEFT JOIN {url_alias} u ON CONCAT('node/', n.nid) = u.src ".
    "WHERE n.type = 'client_portal'";
  $result = db_query($query);
  $node = db_fetch_object($result);

  $count = 0;
  $placeholders = array();
  while ($node) {
    $placeholders = client_portal_get_placeholders($node);
    $src = "node/$node->nid";
    if ($alias = pathauto_create_alias('client_portal', 'bulkupdate', $placeholders, 
                  $src, $node->type)) {
      $count++;
    }
    $node = db_fetch_object($result);
  }
  
  drupal_set_message(format_plural($count,
    "Bulk update of nodes completed, one alias generated.",
    "Bulk update of nodes completed, %count aliases generated."));
} 
?>
