<?php
// $Id$

$path = drupal_get_path('module', 'mapi_derivatives');
require_once($path .'/derivatives.inc');

/**
 *  Install Admin only when the mapi_admin is installed
 */
if (module_exists('mapi_admin')) {
  require_once($path .'/admin.inc');
}

/**
 *  Implementation of hook_mapi_transforms()
 */
function mapi_derivatives_mapi_transforms($op, $transform, $data, $file) {
  switch ($op) {
    // flushes transforms that have been generated by the mapi_derive function
    case 'flush':
      if (is_null($transform)) {
        $derivative = mapi_derivative_load($data);
        $results = db_query("SELECT source, dest FROM {mapi_generated} WHERE name='%s'", $derivatve->name);
        $dests = array();
        while ($object = db_fetch_object($results)) {
          // make sure only the appropriate derivatives are removed
          if (mapi_file_exists($object->dest) && in_array(mapi_file_ext($object->source), explode('|', $derivative->exts))) {
            @unlink($object->dest);
            $dests[] = $object->dest;
          }
        }

        // remove from database if they have been removed
        if (count($dests)) {
          db_query("DELETE FROM {mapi_generated} WHERE dest IN (". implode(',', array_pad(array(), count($dests), "'%s'")) .")", $dests);
        }
      }
      break;

    // for listing the placeholder transformation
    case 'list':
      switch ($transform) {
        case 'placeholder':
          return mapi_extension_list();
        default:
          return array('placeholder');
      }
      break;

    case 'info':
      return array(
        'placeholder' => array(
          'description' => t('Provides a generic placeholder with no transform abilities, but determines characteristics of resulting derivative'),
        )
      );

    // this needs to output the appropriate extension from the settings
    case 'test':
      return $data['output'];

    case 'defaults':
      return array('output' => array_shift(mapi_extension_list()), 'width' => 100, 'height' => 100);

    // basic form
    case 'form':
      $form['output'] = array(
        '#type' => 'select',
        '#title' => t('Output extension'),
        '#description' => t('Select the output extension for placeholder'),
        '#options' => drupal_map_assoc(mapi_extension_list()),
        '#default_value' => !empty($data['output']) ? $data['output'] : array_shift(mapi_extension_list()),
      );

      $form['width'] = array(
        '#type' => 'textfield',
        '#title' => t('Width'),
        '#default_value' => (isset($data['width']) ? $data['width'] : '100'),
        '#description' => t('Enter a width in pixels.'),
      );

      $form['height'] = array(
        '#type' => 'textfield',
        '#title' => t('Height'),
        '#default_value' => (isset($data['height']) ? $data['height'] : '100'),
        '#description' => t('Enter a height in pixels.'),
      );
      return $form;

    // validates that height and width are both numbers
    case 'validate':
      if (!is_numeric($data['width'])) {
        form_set_error('width', t('Width must be a number.'));
      }
      if (!is_numeric($data['height'])) {
        form_set_error('height', t('Height must be a number.'));
      }
      break;

    // doesn't perform any transform, it is after all a placeholder
    case 'transform':
      break;
  }
}
