<?php
// $Id$
/**
 * @file
 * Fonctions de ligne de commande pour FGCF
 *
 * Copyright 2010 Ouest Systemes Informatiques (OSInet)
 *
 * Licensed under the General Public License, version 2 or later
 */


/**
 * Implements hook_drush_command().
 *
 * @return array
 */
function fgcf_drush_command() {
  $items['fgcf-catalog'] = array(
    'description' => "Génère un catalogue de formations au format OpenDocument.",
    'arguments' => array(
      'template' => "Le fichier de template à utiliser (format ODT). Le fichier doit se trouver dans le répertoire d'installation de Drupal",
    ),
    'options' => array(
      'dest' => "Le nom du fichier à générer. Le chemin est relatif au répertoire d'installation Drupal.\nPar défaut un fichier catalogue.odt est généré dans le répertoire d'installation Drupal.",
    ),
    'examples' => array(
      'drush fgcf-catalog template.odt' => "Génère un fichier catalogue.otf dans le répertoire Drupal, à partir du template template.odt présent dans le répertoire d'installation Drupal",
      'drush fgcf-catalog template.odt --dest=../custom.otf' => "Génère un fichier custom.otf dans le dossier parent du répertoire Drupal, à partir du template template.odt présent dans le répertoire d'installation Drupal",
    ),
    'aliases' => array('fgcfc'),
  );
  return $items;
}

/**
 * Implements hook_drush_help().
 *
 * @param string $section
 */
function fgcf_drush_help($section) {
  $help = array(
    'drush:fgcf-catalog'   => dt('Génère un catalogue de formations à partir du fichier catalogue_template.odt présent dans le dossier du module fgcf.'),
  );

  return array_key_exists($key, $help) ? $help[$key] : NULL;
}

/**
 * Command callback for fgcf-catalog.
 *
 * @param string $filename
 */
function drush_fgcf_catalog($template = NULL) {
  $dest = drush_get_option('dest');
  if (empty($dest)) {
    $dest = 'catalogue.odt';
  }

  if (empty($template)) {
    return drush_set_error("Vous devez préciser le fichier de modèle à utiliser.");
  }
  if (!file_exists($template)) {
    return drush_set_error("Le fichier $template n'a pas été trouvé dans le dossier d'installation drupal.");
  }
  module_load_include('inc', 'fgcf', 'fgcf.odt');
  $odf = fgcf_odt_export_formations($template);
  $odf->saveToDisk($dest);
}