source: pro-bachkim-filespace/sourcecode/api.violet.vn/www/plugins/sfPropel13Plugin/lib/propel/generator/sfPropel13AdminGenerator.class.php

Last change on this file was 19, checked in by dungnv, 11 years ago
File size: 6.4 KB
Line 
1<?php
2/*
3 * This file is part of the sfPropel13Plugin package.
4 * (c) 2007 Joshua May <notjosh@gmail.com>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10class sfPropel13AdminGenerator extends sfPropel13CrudGenerator
11{
12  /**
13   * Initializes the current sfGenerator instance.
14   *
15   * @param sfGeneratorManager A sfGeneratorManager instance
16   */
17  public function initialize($generatorManager)
18  {
19    parent::initialize($generatorManager);
20
21    $this->setGeneratorClass('sfPropelAdmin');
22  }
23
24  public function getAllColumns()
25  {
26    $phpNames = array();
27    foreach ($this->getTableMap()->getColumns() as $column)
28    {
29      $phpNames[] = new sfPropel13AdminColumn($column->getPhpName(), $column);
30    }
31
32    return $phpNames;
33  }
34
35  public function getAdminColumnForField($field, $flag = null)
36  {
37    $phpName = sfInflector::camelize($field);
38
39    return new sfPropel13AdminColumn($phpName, $this->getColumnForPhpName($phpName), $flag);
40  }
41
42  // returns a column phpName or null if none was found
43  public function getColumnForPhpName($phpName)
44  {
45    // search the matching column for this column name
46
47    foreach ($this->getTableMap()->getColumns() as $column)
48    {
49      if ($column->getPhpName() == $phpName)
50      {
51        return $column;
52      }
53    }
54
55    // not a "real" column, so we will simulate one
56    return null;
57  }
58 
59  public function getColumnFilterTag($column, $params = array())
60  {
61    $user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
62    $user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
63    $params      = $user_params ? array_merge($params, $user_params) : $params;
64
65    if ($column->isComponent())
66    {
67      return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list'))";
68    }
69    else if ($column->isPartial())
70    {
71      return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))";
72    }
73
74    $type = $column->getType();
75
76    $default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null";
77    $unquotedName = 'filters['.$column->getName().']';
78    $name = "'$unquotedName'";
79
80    if ($column->isForeignKey())
81    {
82      $params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName));
83      return "object_select_tag($default_value, null, $params)";
84
85    }
86    else if ($type == PropelColumnTypes::DATE)
87    {
88      // rich=false not yet implemented
89      $params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
90      return "input_date_range_tag($name, $default_value, $params)";
91    }
92    else if ($type == PropelColumnTypes::TIMESTAMP)
93    {
94      // rich=false not yet implemented
95      $params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
96      return "input_date_range_tag($name, $default_value, $params)";
97    }
98    else if ($type == PropelColumnTypes::BOOLEAN)
99    {
100      $defaultIncludeCustom = '__("yes or no")';
101
102      $option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom));
103      $params = $this->getObjectTagParams($params);
104
105      // little hack
106      $option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params);
107
108      $options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)";
109
110      return "select_tag($name, $options, $params)";
111    }
112    else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR || $type == PropelColumnTypes::LONGVARCHAR)
113    {
114      $size = ($column->getSize() < 15 ? $column->getSize() : 15);
115      $params = $this->getObjectTagParams($params, array('size' => $size));
116      return "input_tag($name, $default_value, $params)";
117    }
118    else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT)
119    {
120      $params = $this->getObjectTagParams($params, array('size' => 7));
121      return "input_tag($name, $default_value, $params)";
122    }
123    else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL)
124    {
125      $params = $this->getObjectTagParams($params, array('size' => 7));
126      return "input_tag($name, $default_value, $params)";
127    }
128    else
129    {
130      $params = $this->getObjectTagParams($params, array('disabled' => true));
131      return "input_tag($name, $default_value, $params)";
132    }
133  }
134}
135
136class sfPropel13AdminColumn extends sfAdminColumn
137{
138  // propel to creole type conversion
139  private static $propelToCreole = array(
140    PropelColumnTypes::BOOLEAN       => CreoleTypes::BOOLEAN,
141    PropelColumnTypes::BINARY        => CreoleTypes::BLOB,
142    PropelColumnTypes::BIGINT        => CreoleTypes::BIGINT,
143    PropelColumnTypes::BLOB          => CreoleTypes::BLOB,
144    PropelColumnTypes::CHAR          => CreoleTypes::CHAR,
145    PropelColumnTypes::CLOB          => CreoleTypes::CLOB,
146    PropelColumnTypes::DATE          => CreoleTypes::DATE,
147    PropelColumnTypes::DECIMAL       => CreoleTypes::DECIMAL,
148    PropelColumnTypes::DOUBLE        => CreoleTypes::DOUBLE,
149    PropelColumnTypes::FLOAT         => CreoleTypes::FLOAT,
150    PropelColumnTypes::INTEGER       => CreoleTypes::INTEGER,
151    PropelColumnTypes::LONGVARBINARY => CreoleTypes::LONGVARBINARY,
152    PropelColumnTypes::LONGVARCHAR   => CreoleTypes::TEXT,
153    PropelColumnTypes::NUMERIC       => CreoleTypes::NUMERIC,
154    PropelColumnTypes::REAL          => CreoleTypes::REAL,
155    PropelColumnTypes::SMALLINT      => CreoleTypes::SMALLINT,
156    PropelColumnTypes::TIMESTAMP     => CreoleTypes::TIMESTAMP,
157    PropelColumnTypes::TIME          => CreoleTypes::TIME,
158    PropelColumnTypes::TINYINT       => CreoleTypes::TINYINT,
159    PropelColumnTypes::VARCHAR       => CreoleTypes::VARCHAR,
160    PropelColumnTypes::VARBINARY     => CreoleTypes::VARBINARY
161  );
162 
163  public function getCreoleType()
164  {
165    return $this->column ? self::$propelToCreole[$this->column->getType()] : null;
166  }
167}
Note: See TracBrowser for help on using the repository browser.