| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfPropelFormFilterGenerator extends sfPropelFormGenerator |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* Initializes the current sfGenerator instance. |
|---|
| 25 |
* |
|---|
| 26 |
* @param sfGeneratorManager $generatorManager A sfGeneratorManager instance |
|---|
| 27 |
*/ |
|---|
| 28 |
public function initialize(sfGeneratorManager $generatorManager) |
|---|
| 29 |
{ |
|---|
| 30 |
parent::initialize($generatorManager); |
|---|
| 31 |
|
|---|
| 32 |
$this->setGeneratorClass('sfPropelFormFilter'); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
* Generates classes and templates in cache. |
|---|
| 37 |
* |
|---|
| 38 |
* @param array $params The parameters |
|---|
| 39 |
* |
|---|
| 40 |
* @return string The data to put in configuration cache |
|---|
| 41 |
*/ |
|---|
| 42 |
public function generate($params = array()) |
|---|
| 43 |
{ |
|---|
| 44 |
$this->params = $params; |
|---|
| 45 |
|
|---|
| 46 |
if (!isset($this->params['connection'])) |
|---|
| 47 |
{ |
|---|
| 48 |
throw new sfParseException('You must specify a "connection" parameter.'); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
if (!isset($this->params['model_dir_name'])) |
|---|
| 52 |
{ |
|---|
| 53 |
$this->params['model_dir_name'] = 'model'; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
if (!isset($this->params['filter_dir_name'])) |
|---|
| 57 |
{ |
|---|
| 58 |
$this->params['filter_dir_name'] = 'filter'; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
$this->loadBuilders(); |
|---|
| 62 |
|
|---|
| 63 |
$this->dbMap = Propel::getDatabaseMap($this->params['connection']); |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
$file = sfConfig::get('sf_lib_dir').'/filter/BaseFormFilterPropel.class.php'; |
|---|
| 67 |
if (!file_exists($file)) |
|---|
| 68 |
{ |
|---|
| 69 |
if (!is_dir($directory = dirname($file))) |
|---|
| 70 |
{ |
|---|
| 71 |
mkdir($directory, 0777, true); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
file_put_contents($file, $this->evalTemplate('sfPropelFormFilterBaseTemplate.php')); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
foreach ($this->dbMap->getTables() as $tableName => $table) |
|---|
| 79 |
{ |
|---|
| 80 |
$behaviors = $table->getBehaviors(); |
|---|
| 81 |
if (isset($behaviors['symfony']['filter']) && 'false' === $behaviors['symfony']['filter']) |
|---|
| 82 |
{ |
|---|
| 83 |
continue; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
$this->table = $table; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
$packages = explode('.', constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT')); |
|---|
| 90 |
array_pop($packages); |
|---|
| 91 |
if (false === $pos = array_search($this->params['model_dir_name'], $packages)) |
|---|
| 92 |
{ |
|---|
| 93 |
throw new InvalidArgumentException(sprintf('Unable to find the model dir name (%s) in the package %s.', $this->params['model_dir_name'], constant(constant($table->getClassname().'::PEER').'::CLASS_DEFAULT'))); |
|---|
| 94 |
} |
|---|
| 95 |
$packages[$pos] = $this->params['filter_dir_name']; |
|---|
| 96 |
$baseDir = sfConfig::get('sf_root_dir').'/'.implode(DIRECTORY_SEPARATOR, $packages); |
|---|
| 97 |
|
|---|
| 98 |
if (!is_dir($baseDir.'/base')) |
|---|
| 99 |
{ |
|---|
| 100 |
mkdir($baseDir.'/base', 0777, true); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
file_put_contents($baseDir.'/base/Base'.$table->getClassname().'FormFilter.class.php', $this->evalTemplate('sfPropelFormFilterGeneratedTemplate.php')); |
|---|
| 104 |
if (!file_exists($classFile = $baseDir.'/'.$table->getClassname().'FormFilter.class.php')) |
|---|
| 105 |
{ |
|---|
| 106 |
file_put_contents($classFile, $this->evalTemplate('sfPropelFormFilterTemplate.php')); |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
* Returns a sfWidgetForm class name for a given column. |
|---|
| 113 |
* |
|---|
| 114 |
* @param ColumnMap $column A ColumnMap object |
|---|
| 115 |
* |
|---|
| 116 |
* @return string The name of a subclass of sfWidgetForm |
|---|
| 117 |
*/ |
|---|
| 118 |
public function getWidgetClassForColumn(ColumnMap $column) |
|---|
| 119 |
{ |
|---|
| 120 |
switch ($column->getType()) |
|---|
| 121 |
{ |
|---|
| 122 |
case PropelColumnTypes::BOOLEAN: |
|---|
| 123 |
$name = 'Choice'; |
|---|
| 124 |
break; |
|---|
| 125 |
case PropelColumnTypes::DATE: |
|---|
| 126 |
case PropelColumnTypes::TIME: |
|---|
| 127 |
case PropelColumnTypes::TIMESTAMP: |
|---|
| 128 |
$name = 'FilterDate'; |
|---|
| 129 |
break; |
|---|
| 130 |
default: |
|---|
| 131 |
$name = 'FilterInput'; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
if ($column->isForeignKey()) |
|---|
| 135 |
{ |
|---|
| 136 |
$name = 'PropelChoice'; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
return sprintf('sfWidgetForm%s', $name); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
* Returns a PHP string representing options to pass to a widget for a given column. |
|---|
| 144 |
* |
|---|
| 145 |
* @param ColumnMap $column A ColumnMap object |
|---|
| 146 |
* |
|---|
| 147 |
* @return string The options to pass to the widget as a PHP string |
|---|
| 148 |
*/ |
|---|
| 149 |
public function getWidgetOptionsForColumn(ColumnMap $column) |
|---|
| 150 |
{ |
|---|
| 151 |
$options = array(); |
|---|
| 152 |
|
|---|
| 153 |
$withEmpty = $column->isNotNull() && !$column->isForeignKey() ? array("'with_empty' => false") : array(); |
|---|
| 154 |
switch ($column->getType()) |
|---|
| 155 |
{ |
|---|
| 156 |
case PropelColumnTypes::BOOLEAN: |
|---|
| 157 |
$options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')"; |
|---|
| 158 |
break; |
|---|
| 159 |
case PropelColumnTypes::DATE: |
|---|
| 160 |
case PropelColumnTypes::TIME: |
|---|
| 161 |
case PropelColumnTypes::TIMESTAMP: |
|---|
| 162 |
$options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()"; |
|---|
| 163 |
$options = array_merge($options, $withEmpty); |
|---|
| 164 |
break; |
|---|
| 165 |
default: |
|---|
| 166 |
$options = array_merge($options, $withEmpty); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
if ($column->isForeignKey()) |
|---|
| 170 |
{ |
|---|
| 171 |
$options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $this->getForeignTable($column)->getClassname()); |
|---|
| 172 |
|
|---|
| 173 |
$refColumn = $this->getForeignTable($column)->getColumn($column->getRelatedColumnName()); |
|---|
| 174 |
if (!$refColumn->isPrimaryKey()) |
|---|
| 175 |
{ |
|---|
| 176 |
$options[] = sprintf('\'key_method\' => \'get%s\'', $refColumn->getPhpName()); |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
return count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''; |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
* Returns a sfValidator class name for a given column. |
|---|
| 185 |
* |
|---|
| 186 |
* @param ColumnMap $column A ColumnMap object |
|---|
| 187 |
* |
|---|
| 188 |
* @return string The name of a subclass of sfValidator |
|---|
| 189 |
*/ |
|---|
| 190 |
public function getValidatorClassForColumn(ColumnMap $column) |
|---|
| 191 |
{ |
|---|
| 192 |
switch ($column->getType()) |
|---|
| 193 |
{ |
|---|
| 194 |
case PropelColumnTypes::BOOLEAN: |
|---|
| 195 |
$name = 'Choice'; |
|---|
| 196 |
break; |
|---|
| 197 |
case PropelColumnTypes::DOUBLE: |
|---|
| 198 |
case PropelColumnTypes::FLOAT: |
|---|
| 199 |
case PropelColumnTypes::NUMERIC: |
|---|
| 200 |
case PropelColumnTypes::DECIMAL: |
|---|
| 201 |
case PropelColumnTypes::REAL: |
|---|
| 202 |
$name = 'Number'; |
|---|
| 203 |
break; |
|---|
| 204 |
case PropelColumnTypes::INTEGER: |
|---|
| 205 |
case PropelColumnTypes::SMALLINT: |
|---|
| 206 |
case PropelColumnTypes::TINYINT: |
|---|
| 207 |
case PropelColumnTypes::BIGINT: |
|---|
| 208 |
$name = 'Integer'; |
|---|
| 209 |
break; |
|---|
| 210 |
case PropelColumnTypes::DATE: |
|---|
| 211 |
case PropelColumnTypes::TIME: |
|---|
| 212 |
case PropelColumnTypes::TIMESTAMP: |
|---|
| 213 |
$name = 'DateRange'; |
|---|
| 214 |
break; |
|---|
| 215 |
default: |
|---|
| 216 |
$name = 'Pass'; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
if ($column->isPrimaryKey() || $column->isForeignKey()) |
|---|
| 220 |
{ |
|---|
| 221 |
$name = 'PropelChoice'; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
return sprintf('sfValidator%s', $name); |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
* Returns a PHP string representing options to pass to a validator for a given column. |
|---|
| 229 |
* |
|---|
| 230 |
* @param ColumnMap $column A ColumnMap object |
|---|
| 231 |
* |
|---|
| 232 |
* @return string The options to pass to the validator as a PHP string |
|---|
| 233 |
*/ |
|---|
| 234 |
public function getValidatorOptionsForColumn(ColumnMap $column) |
|---|
| 235 |
{ |
|---|
| 236 |
$options = array('\'required\' => false'); |
|---|
| 237 |
|
|---|
| 238 |
if ($column->isForeignKey()) |
|---|
| 239 |
{ |
|---|
| 240 |
$options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->getForeignTable($column)->getClassname(), $this->translateColumnName($column, true)); |
|---|
| 241 |
} |
|---|
| 242 |
else if ($column->isPrimaryKey()) |
|---|
| 243 |
{ |
|---|
| 244 |
$options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getTable()->getClassname(), $this->translateColumnName($column)); |
|---|
| 245 |
} |
|---|
| 246 |
else |
|---|
| 247 |
{ |
|---|
| 248 |
switch ($column->getType()) |
|---|
| 249 |
{ |
|---|
| 250 |
case PropelColumnTypes::BOOLEAN: |
|---|
| 251 |
$options[] = "'choices' => array('', 1, 0)"; |
|---|
| 252 |
break; |
|---|
| 253 |
case PropelColumnTypes::DATE: |
|---|
| 254 |
case PropelColumnTypes::TIME: |
|---|
| 255 |
case PropelColumnTypes::TIMESTAMP: |
|---|
| 256 |
$options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))"; |
|---|
| 257 |
break; |
|---|
| 258 |
} |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|
| 261 |
return count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
public function getValidatorForColumn($column) |
|---|
| 265 |
{ |
|---|
| 266 |
$format = 'new %s(%s)'; |
|---|
| 267 |
if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber'))) |
|---|
| 268 |
{ |
|---|
| 269 |
$format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))'; |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
return sprintf($format, $class, $this->getValidatorOptionsForColumn($column)); |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
public function getType(ColumnMap $column) |
|---|
| 276 |
{ |
|---|
| 277 |
if ($column->isForeignKey()) |
|---|
| 278 |
{ |
|---|
| 279 |
return 'ForeignKey'; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
switch ($column->getType()) |
|---|
| 283 |
{ |
|---|
| 284 |
case PropelColumnTypes::BOOLEAN: |
|---|
| 285 |
return 'Boolean'; |
|---|
| 286 |
case PropelColumnTypes::DATE: |
|---|
| 287 |
case PropelColumnTypes::TIME: |
|---|
| 288 |
case PropelColumnTypes::TIMESTAMP: |
|---|
| 289 |
return 'Date'; |
|---|
| 290 |
case PropelColumnTypes::DOUBLE: |
|---|
| 291 |
case PropelColumnTypes::FLOAT: |
|---|
| 292 |
case PropelColumnTypes::NUMERIC: |
|---|
| 293 |
case PropelColumnTypes::DECIMAL: |
|---|
| 294 |
case PropelColumnTypes::REAL: |
|---|
| 295 |
case PropelColumnTypes::INTEGER: |
|---|
| 296 |
case PropelColumnTypes::SMALLINT: |
|---|
| 297 |
case PropelColumnTypes::TINYINT: |
|---|
| 298 |
case PropelColumnTypes::BIGINT: |
|---|
| 299 |
return 'Number'; |
|---|
| 300 |
default: |
|---|
| 301 |
return 'Text'; |
|---|
| 302 |
} |
|---|
| 303 |
} |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|