<?php
/*
 * This file is part of the sfPropel13Plugin package.
 * (c) 2007 Joshua May <notjosh@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

class sfPropel13AdminGenerator extends sfPropel13CrudGenerator
{
  /**
   * Initializes the current sfGenerator instance.
   *
   * @param sfGeneratorManager A sfGeneratorManager instance
   */
  public function initialize($generatorManager)
  {
    parent::initialize($generatorManager);

    $this->setGeneratorClass('sfPropelAdmin');
  }

  public function getAllColumns()
  {
    $phpNames = array();
    foreach ($this->getTableMap()->getColumns() as $column)
    {
      $phpNames[] = new sfPropel13AdminColumn($column->getPhpName(), $column);
    }

    return $phpNames;
  }

  public function getAdminColumnForField($field, $flag = null)
  {
    $phpName = sfInflector::camelize($field);

    return new sfPropel13AdminColumn($phpName, $this->getColumnForPhpName($phpName), $flag);
  }

  // returns a column phpName or null if none was found
  public function getColumnForPhpName($phpName)
  {
    // search the matching column for this column name

    foreach ($this->getTableMap()->getColumns() as $column)
    {
      if ($column->getPhpName() == $phpName)
      {
        return $column;
      }
    }

    // not a "real" column, so we will simulate one
    return null;
  }
  
  public function getColumnFilterTag($column, $params = array())
  {
    $user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
    $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
    $params      = $user_params ? array_merge($params, $user_params) : $params;

    if ($column->isComponent())
    {
      return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list'))";
    }
    else if ($column->isPartial())
    {
      return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))";
    }

    $type = $column->getType();

    $default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null";
    $unquotedName = 'filters['.$column->getName().']';
    $name = "'$unquotedName'";

    if ($column->isForeignKey())
    {
      $params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName));
      return "object_select_tag($default_value, null, $params)";

    }
    else if ($type == PropelColumnTypes::DATE)
    {
      // rich=false not yet implemented
      $params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
      return "input_date_range_tag($name, $default_value, $params)";
    }
    else if ($type == PropelColumnTypes::TIMESTAMP)
    {
      // rich=false not yet implemented
      $params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
      return "input_date_range_tag($name, $default_value, $params)";
    }
    else if ($type == PropelColumnTypes::BOOLEAN)
    {
      $defaultIncludeCustom = '__("yes or no")';

      $option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom));
      $params = $this->getObjectTagParams($params);

      // little hack
      $option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params);

      $options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)";

      return "select_tag($name, $options, $params)";
    }
    else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR || $type == PropelColumnTypes::LONGVARCHAR)
    {
      $size = ($column->getSize() < 15 ? $column->getSize() : 15);
      $params = $this->getObjectTagParams($params, array('size' => $size));
      return "input_tag($name, $default_value, $params)";
    }
    else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT)
    {
      $params = $this->getObjectTagParams($params, array('size' => 7));
      return "input_tag($name, $default_value, $params)";
    }
    else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL)
    {
      $params = $this->getObjectTagParams($params, array('size' => 7));
      return "input_tag($name, $default_value, $params)";
    }
    else
    {
      $params = $this->getObjectTagParams($params, array('disabled' => true));
      return "input_tag($name, $default_value, $params)";
    }
  }
}

class sfPropel13AdminColumn extends sfAdminColumn
{
  // propel to creole type conversion
  private static $propelToCreole = array(
    PropelColumnTypes::BOOLEAN       => CreoleTypes::BOOLEAN,
    PropelColumnTypes::BINARY        => CreoleTypes::BLOB,
    PropelColumnTypes::BIGINT        => CreoleTypes::BIGINT,
    PropelColumnTypes::BLOB          => CreoleTypes::BLOB,
    PropelColumnTypes::CHAR          => CreoleTypes::CHAR,
    PropelColumnTypes::CLOB          => CreoleTypes::CLOB,
    PropelColumnTypes::DATE          => CreoleTypes::DATE,
    PropelColumnTypes::DECIMAL       => CreoleTypes::DECIMAL,
    PropelColumnTypes::DOUBLE        => CreoleTypes::DOUBLE,
    PropelColumnTypes::FLOAT         => CreoleTypes::FLOAT,
    PropelColumnTypes::INTEGER       => CreoleTypes::INTEGER,
    PropelColumnTypes::LONGVARBINARY => CreoleTypes::LONGVARBINARY,
    PropelColumnTypes::LONGVARCHAR   => CreoleTypes::TEXT,
    PropelColumnTypes::NUMERIC       => CreoleTypes::NUMERIC,
    PropelColumnTypes::REAL          => CreoleTypes::REAL,
    PropelColumnTypes::SMALLINT      => CreoleTypes::SMALLINT,
    PropelColumnTypes::TIMESTAMP     => CreoleTypes::TIMESTAMP,
    PropelColumnTypes::TIME          => CreoleTypes::TIME,
    PropelColumnTypes::TINYINT       => CreoleTypes::TINYINT,
    PropelColumnTypes::VARCHAR       => CreoleTypes::VARCHAR,
    PropelColumnTypes::VARBINARY     => CreoleTypes::VARBINARY
  );
  
  public function getCreoleType()
  {
    return $this->column ? self::$propelToCreole[$this->column->getType()] : null;
  }
}
