modify('-7 day');
  $form['period'] = array(
    '#type' => 'select',
    '#title' => t('Period'),
    '#default_value' => 'week',
    '#required' => TRUE,
    '#options' => array(
      'day' => t('Last day'),
      'week' => t('Last week'),
      'month' => t('Last month'),
      'year' => t('Last year'),
      'custom' => t('Custom')
    )
  );
  $form['start'] = array(
    '#type' => 'date',
    '#title' => t('Custom start'),
    '#default_value' => array('year' => $start_date->format('Y'), 'month' => $start_date->format('n'), 'day' => $start_date->format('j')),
    '#required' => FALSE,
  );
  $form['end'] = array(
    '#type' => 'date',
    '#title' => t('Custom end'),
    '#required' => FALSE,
  );
  // use type 'button' instead of regular 'submit'. From FAPI:
  // "Format an action button. When the button is pressed, the form will be submitted to Drupal, where it is validated and rebuilt. The submit handler is not invoked."
  // http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/5#button
  $form['submit'] = array(
    '#type' => 'button',
    '#value' => t('Update Statistics')
  );
  return $form;
}
/**
 * Special theme function for our wizard form.
 * @param $form the form elements to template
 * @return the string of the themed form
 */
function theme_origo_statistics_form($form) {
  $output = '';
  $output .= '
| ';
  $output .= drupal_render($form['period']);
  $output .= ' | ';
  $output .= drupal_render($form['start']);
  $output .= ' | ';
  $output .= drupal_render($form['end']);
  $output .= ' | 
';
  $output .= drupal_render($form);
  // read the values for the statistics
  $project = variable_get('origo_project_name', '');
  $now = time();
  switch ($form['period']['#value']) {
    case 'day':
      $start = $now - 86400;
      $end = $now;
      break;
    case 'week':
      $start = $now - 604800;
      $end = $now;
      break;
    case 'month':
      $start = $now - 2592000;
      $end = $now;
      break;
    case 'year':
      $start = $now - 31536000;
      $end = $now;
      break;
    case 'custom':
      $start = _convert_to_unix_timestamp($form['start']['#value']);
      $end = _convert_to_unix_timestamp($form['end']['#value']);
      break;
  }
  // render the statistics
  $output .= '
';
  $output .= _add_statistic_image($project, $start, $end);
  return $output;
}
/**
 * Convert a form date array into a unix timestamp.
 * @param $date the form date array
 * @return unix timestamp
 */
function _convert_to_unix_timestamp($date) {
  return mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
}
/**
 * Generate the statistic image links.
 * @param $project project
 * @param $start start time
 * @param $end end time
 * @return the string with the html code for the image
 */
function _add_statistic_image($project, $start, $end) {
  $valid_templates = variable_get('origo_statistics_templates', array());
  foreach ($valid_templates as $template) {
    $output .= '
'."\n";
  }
  return $output;
}
/**
 * Image call.
 */
function origo_statistics_picture_page() {
  // fixed config
  $rrd_tool = variable_get('origo_statistics_rrdtool', '/usr/bin/rrdtool');
  $rrd_data_path = variable_get('origo_statistics_rrd_path', '');
  $template_path = drupal_get_path('module', 'origo_statistics') ."/templates/";
  $valid_templates = variable_get('origo_statistics_templates', array());
  // parameters
  $project = $_GET["p"];
  if (!preg_match("/^[-\w]{1,50}$/", $project)) {
    die("Invalid project specified");
  }
  $project_watermark = '';
  $base = base_path();
  if (!strcmp($base, '/')) {
    // The base path is '/', therefore we work with subdomains: display this
    $project_watermark = 'http://'. drupal_strtolower($project) .'.'. variable_get('origo_web_domain', '');
  }
  else {
    // The base path is something like '/projects/origo', thus we work with proper subdirectory - projects
    // In this case, we display only the project and oriact name
    $project_watermark = $project .' - powered by oriact.com';
  }
  $start = intval($_GET["s"]);
  $end = intval($_GET["e"]);
  $template = $_GET["t"];
  if (!in_array($template, $valid_templates, TRUE)) {
    die("Invalid template specified");
  }
  else {
    $graph_template = file_get_contents($template_path . $template .".template");
  }
  // build command
  $project_rrd_path = $rrd_data_path . $project ."/";
  $search = array("%PROJECT%", "%RRD%", "%START%", "%END%", "%PROJECTWATERMARK%");
  $replace = array($project, $project_rrd_path, $start, $end, $project_watermark);
  $graph = str_replace($search, $replace, $graph_template);
  $command = $rrd_tool ." graph -  ". $graph;
  //echo str_replace ("\\", "", $command);
  // header
  header("Content-type: image/png");
  // execute command
  passthru($command);
}