| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfDoctrineFormFilterGenerator extends sfDoctrineFormGenerator |
|---|
| 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('sfDoctrineFormFilter'); |
|---|
| 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 |
$models = $this->loadModels(); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
$file = sfConfig::get('sf_lib_dir').'/filter/doctrine/BaseFormFilterDoctrine.class.php'; |
|---|
| 65 |
if (!file_exists($file)) |
|---|
| 66 |
{ |
|---|
| 67 |
if (!is_dir(sfConfig::get('sf_lib_dir').'/filter/doctrine/base')) |
|---|
| 68 |
{ |
|---|
| 69 |
mkdir(sfConfig::get('sf_lib_dir').'/filter/doctrine/base', 0777, true); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
file_put_contents($file, $this->evalTemplate('sfDoctrineFormFilterBaseTemplate.php')); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
$pluginPaths = $this->generatorManager->getConfiguration()->getAllPluginPaths(); |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
foreach ($models as $model) |
|---|
| 79 |
{ |
|---|
| 80 |
$this->table = Doctrine::getTable($model); |
|---|
| 81 |
$this->modelName = $model; |
|---|
| 82 |
|
|---|
| 83 |
$baseDir = sfConfig::get('sf_lib_dir') . '/filter/doctrine'; |
|---|
| 84 |
|
|---|
| 85 |
$isPluginModel = $this->isPluginModel($model); |
|---|
| 86 |
if ($isPluginModel) |
|---|
| 87 |
{ |
|---|
| 88 |
$pluginName = $this->getPluginNameForModel($model); |
|---|
| 89 |
$baseDir .= '/' . $pluginName; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
if (!is_dir($baseDir.'/base')) |
|---|
| 93 |
{ |
|---|
| 94 |
mkdir($baseDir.'/base', 0777, true); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
file_put_contents($baseDir.'/base/Base'.$model.'FormFilter.class.php', $this->evalTemplate('sfDoctrineFormFilterGeneratedTemplate.php')); |
|---|
| 98 |
if ($isPluginModel) |
|---|
| 99 |
{ |
|---|
| 100 |
$pluginBaseDir = $pluginPaths[$pluginName].'/lib/filter/doctrine'; |
|---|
| 101 |
if (!file_exists($classFile = $pluginBaseDir.'/Plugin'.$model.'FormFilter.class.php')) |
|---|
| 102 |
{ |
|---|
| 103 |
if (!is_dir($pluginBaseDir)) |
|---|
| 104 |
{ |
|---|
| 105 |
mkdir($pluginBaseDir, 0777, true); |
|---|
| 106 |
} |
|---|
| 107 |
file_put_contents($classFile, $this->evalTemplate('sfDoctrineFormFilterPluginTemplate.php')); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
if (!file_exists($classFile = $baseDir.'/'.$model.'FormFilter.class.php')) |
|---|
| 111 |
{ |
|---|
| 112 |
if ($isPluginModel) |
|---|
| 113 |
{ |
|---|
| 114 |
file_put_contents($classFile, $this->evalTemplate('sfDoctrinePluginFormFilterTemplate.php')); |
|---|
| 115 |
} else { |
|---|
| 116 |
file_put_contents($classFile, $this->evalTemplate('sfDoctrineFormFilterTemplate.php')); |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
* Returns a sfWidgetForm class name for a given column. |
|---|
| 124 |
* |
|---|
| 125 |
* @param sfDoctrineColumn $column |
|---|
| 126 |
* @return string The name of a subclass of sfWidgetForm |
|---|
| 127 |
*/ |
|---|
| 128 |
public function getWidgetClassForColumn($column) |
|---|
| 129 |
{ |
|---|
| 130 |
switch ($column->getDoctrineType()) |
|---|
| 131 |
{ |
|---|
| 132 |
case 'boolean': |
|---|
| 133 |
$name = 'Choice'; |
|---|
| 134 |
break; |
|---|
| 135 |
case 'date': |
|---|
| 136 |
case 'datetime': |
|---|
| 137 |
case 'timestamp': |
|---|
| 138 |
$name = 'FilterDate'; |
|---|
| 139 |
break; |
|---|
| 140 |
case 'enum': |
|---|
| 141 |
$name = 'Choice'; |
|---|
| 142 |
break; |
|---|
| 143 |
default: |
|---|
| 144 |
$name = 'FilterInput'; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
if ($column->isForeignKey()) |
|---|
| 148 |
{ |
|---|
| 149 |
$name = 'DoctrineChoice'; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
return sprintf('sfWidgetForm%s', $name); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
* Returns a PHP string representing options to pass to a widget for a given column. |
|---|
| 157 |
* |
|---|
| 158 |
* @param sfDoctrineColumn $column |
|---|
| 159 |
* @return string The options to pass to the widget as a PHP string |
|---|
| 160 |
*/ |
|---|
| 161 |
public function getWidgetOptionsForColumn($column) |
|---|
| 162 |
{ |
|---|
| 163 |
$options = array(); |
|---|
| 164 |
|
|---|
| 165 |
$withEmpty = sprintf('\'with_empty\' => %s', $column->isNotNull() ? 'false' : 'true'); |
|---|
| 166 |
switch ($column->getDoctrineType()) |
|---|
| 167 |
{ |
|---|
| 168 |
case 'boolean': |
|---|
| 169 |
$options[] = "'choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no')"; |
|---|
| 170 |
break; |
|---|
| 171 |
case 'date': |
|---|
| 172 |
case 'datetime': |
|---|
| 173 |
case 'timestamp': |
|---|
| 174 |
$options[] = "'from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate()"; |
|---|
| 175 |
$options[] = $withEmpty; |
|---|
| 176 |
break; |
|---|
| 177 |
case 'enum': |
|---|
| 178 |
$values = array('' => ''); |
|---|
| 179 |
$values = array_merge($values, $column['values']); |
|---|
| 180 |
$values = array_combine($values, $values); |
|---|
| 181 |
$options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values)); |
|---|
| 182 |
break; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
if ($column->isForeignKey()) |
|---|
| 186 |
{ |
|---|
| 187 |
$options[] = sprintf('\'model\' => \'%s\', \'add_empty\' => true', $column->getForeignTable()->getOption('name')); |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
return count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
* Returns a sfValidator class name for a given column. |
|---|
| 195 |
* |
|---|
| 196 |
* @param sfDoctrineColumn $column |
|---|
| 197 |
* @return string The name of a subclass of sfValidator |
|---|
| 198 |
*/ |
|---|
| 199 |
public function getValidatorClassForColumn($column) |
|---|
| 200 |
{ |
|---|
| 201 |
switch ($column->getDoctrineType()) |
|---|
| 202 |
{ |
|---|
| 203 |
case 'boolean': |
|---|
| 204 |
$name = 'Choice'; |
|---|
| 205 |
break; |
|---|
| 206 |
case 'float': |
|---|
| 207 |
case 'decimal': |
|---|
| 208 |
$name = 'Number'; |
|---|
| 209 |
break; |
|---|
| 210 |
case 'integer': |
|---|
| 211 |
$name = 'Integer'; |
|---|
| 212 |
break; |
|---|
| 213 |
case 'date': |
|---|
| 214 |
case 'datetime': |
|---|
| 215 |
case 'timestamp': |
|---|
| 216 |
$name = 'DateRange'; |
|---|
| 217 |
break; |
|---|
| 218 |
case 'enum': |
|---|
| 219 |
$name = 'Choice'; |
|---|
| 220 |
break; |
|---|
| 221 |
default: |
|---|
| 222 |
$name = 'Pass'; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
if ($column->isPrimarykey() || $column->isForeignKey()) |
|---|
| 226 |
{ |
|---|
| 227 |
$name = 'DoctrineChoice'; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
return sprintf('sfValidator%s', $name); |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
* Returns a PHP string representing options to pass to a validator for a given column. |
|---|
| 235 |
* |
|---|
| 236 |
* @param sfDoctrineColumn $column |
|---|
| 237 |
* @return string The options to pass to the validator as a PHP string |
|---|
| 238 |
*/ |
|---|
| 239 |
public function getValidatorOptionsForColumn($column) |
|---|
| 240 |
{ |
|---|
| 241 |
$options = array('\'required\' => false'); |
|---|
| 242 |
|
|---|
| 243 |
if ($column->isForeignKey()) |
|---|
| 244 |
{ |
|---|
| 245 |
$columns = $column->getForeignTable()->getColumns(); |
|---|
| 246 |
foreach ($columns as $name => $col) |
|---|
| 247 |
{ |
|---|
| 248 |
if (isset($col['primary']) && $col['primary']) |
|---|
| 249 |
{ |
|---|
| 250 |
break; |
|---|
| 251 |
} |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
$options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getForeignTable()->getOption('name'), $column->getForeignTable()->getFieldName($name)); |
|---|
| 255 |
} |
|---|
| 256 |
else if ($column->isPrimaryKey()) |
|---|
| 257 |
{ |
|---|
| 258 |
$options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->table->getOption('name'), $this->table->getFieldName($columnName)); |
|---|
| 259 |
} |
|---|
| 260 |
else |
|---|
| 261 |
{ |
|---|
| 262 |
switch ($column->getDoctrineType()) |
|---|
| 263 |
{ |
|---|
| 264 |
case 'boolean': |
|---|
| 265 |
$options[] = "'choices' => array('', 1, 0)"; |
|---|
| 266 |
break; |
|---|
| 267 |
case 'date': |
|---|
| 268 |
case 'datetime': |
|---|
| 269 |
case 'timestamp': |
|---|
| 270 |
$options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))"; |
|---|
| 271 |
break; |
|---|
| 272 |
case 'enum': |
|---|
| 273 |
$values = array_combine($column['values'], $column['values']); |
|---|
| 274 |
$options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values)); |
|---|
| 275 |
break; |
|---|
| 276 |
} |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
return count($options) ? sprintf('array(%s)', implode(', ', $options)) : ''; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
public function getValidatorForColumn($column) |
|---|
| 283 |
{ |
|---|
| 284 |
$format = 'new %s(%s)'; |
|---|
| 285 |
if (in_array($class = $this->getValidatorClassForColumn($column), array('sfValidatorInteger', 'sfValidatorNumber'))) |
|---|
| 286 |
{ |
|---|
| 287 |
$format = 'new sfValidatorSchemaFilter(\'text\', new %s(%s))'; |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
return sprintf($format, $class, $this->getValidatorOptionsForColumn($column)); |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
public function getType($column) |
|---|
| 294 |
{ |
|---|
| 295 |
if ($column->isForeignKey()) |
|---|
| 296 |
{ |
|---|
| 297 |
return 'ForeignKey'; |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
switch ($column->getDoctrineType()) |
|---|
| 301 |
{ |
|---|
| 302 |
case 'enum': |
|---|
| 303 |
return 'Enum'; |
|---|
| 304 |
case 'boolean': |
|---|
| 305 |
return 'Boolean'; |
|---|
| 306 |
case 'date': |
|---|
| 307 |
case 'datetime': |
|---|
| 308 |
case 'timestamp': |
|---|
| 309 |
return 'Date'; |
|---|
| 310 |
case 'integer': |
|---|
| 311 |
case 'decimal': |
|---|
| 312 |
case 'float': |
|---|
| 313 |
return 'Number'; |
|---|
| 314 |
default: |
|---|
| 315 |
return 'Text'; |
|---|
| 316 |
} |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
* Array export. Export array to formatted php code |
|---|
| 321 |
* |
|---|
| 322 |
* @param array $values |
|---|
| 323 |
* @return string $php |
|---|
| 324 |
*/ |
|---|
| 325 |
protected function arrayExport($values) |
|---|
| 326 |
{ |
|---|
| 327 |
$php = var_export($values, true); |
|---|
| 328 |
$php = str_replace("\n", '', $php); |
|---|
| 329 |
$php = str_replace('array ( ', 'array(', $php); |
|---|
| 330 |
$php = str_replace(',)', ')', $php); |
|---|
| 331 |
$php = str_replace(' ', ' ', $php); |
|---|
| 332 |
return $php; |
|---|
| 333 |
} |
|---|
| 334 |
} |
|---|