| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
abstract class sfCrudGenerator extends sfGenerator |
|---|
| 22 |
{ |
|---|
| 23 |
protected |
|---|
| 24 |
$singularName = '', |
|---|
| 25 |
$pluralName = '', |
|---|
| 26 |
$peerClassName = '', |
|---|
| 27 |
$map = null, |
|---|
| 28 |
$tableMap = null, |
|---|
| 29 |
$primaryKey = array(), |
|---|
| 30 |
$className = '', |
|---|
| 31 |
$params = array(); |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
* Generates classes and templates in cache. |
|---|
| 35 |
* |
|---|
| 36 |
* @param array $params The parameters |
|---|
| 37 |
* |
|---|
| 38 |
* @return string The data to put in configuration cache |
|---|
| 39 |
*/ |
|---|
| 40 |
public function generate($params = array()) |
|---|
| 41 |
{ |
|---|
| 42 |
$this->params = $params; |
|---|
| 43 |
|
|---|
| 44 |
$required_parameters = array('model_class', 'moduleName'); |
|---|
| 45 |
foreach ($required_parameters as $entry) |
|---|
| 46 |
{ |
|---|
| 47 |
if (!isset($this->params[$entry])) |
|---|
| 48 |
{ |
|---|
| 49 |
throw new sfParseException(sprintf('You must specify a "%s".', $entry)); |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
$modelClass = $this->params['model_class']; |
|---|
| 54 |
|
|---|
| 55 |
if (!class_exists($modelClass)) |
|---|
| 56 |
{ |
|---|
| 57 |
throw new sfInitializationException(sprintf('Unable to scaffold nonexistent model "%s".', $modelClass)); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
$this->setScaffoldingClassName($modelClass); |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
$this->setGeneratedModuleName('auto'.ucfirst($this->params['moduleName'])); |
|---|
| 64 |
$this->setModuleName($this->params['moduleName']); |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
$this->loadMapBuilderClasses(); |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
$this->loadPrimaryKeys(); |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
$theme = isset($this->params['theme']) ? $this->params['theme'] : 'default'; |
|---|
| 74 |
$themeDir = $this->generatorManager->getConfiguration()->getGeneratorTemplate($this->getGeneratorClass(), $theme, ''); |
|---|
| 75 |
if (!is_dir($themeDir)) |
|---|
| 76 |
{ |
|---|
| 77 |
throw new sfConfigurationException(sprintf('The theme "%s" does not exist.', $theme)); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
$this->setTheme($theme); |
|---|
| 81 |
$files = sfFinder::type('file')->relative()->in($themeDir); |
|---|
| 82 |
|
|---|
| 83 |
$this->generatePhpFiles($this->generatedModuleName, $files); |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
$data = "require_once(sfConfig::get('sf_module_cache_dir').'/".$this->generatedModuleName."/actions/actions.class.php');\n"; |
|---|
| 87 |
|
|---|
| 88 |
return $data; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
* Returns PHP code for primary keys parameters. |
|---|
| 93 |
* |
|---|
| 94 |
* @param integer $indent The indentation value |
|---|
| 95 |
* @param string $callee The function to call |
|---|
| 96 |
* |
|---|
| 97 |
* @return string The PHP code |
|---|
| 98 |
*/ |
|---|
| 99 |
public function getRetrieveByPkParamsForAction($indent, $callee = '$this->getRequestParameter') |
|---|
| 100 |
{ |
|---|
| 101 |
$params = array(); |
|---|
| 102 |
foreach ($this->getPrimaryKey() as $pk) |
|---|
| 103 |
{ |
|---|
| 104 |
$params[] = "$callee('".sfInflector::underscore($pk->getPhpName())."')"; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
return implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Returns PHP code for primary keys parameters. |
|---|
| 112 |
* |
|---|
| 113 |
* @param integer $indent The indentation value |
|---|
| 114 |
* @param string $prefix No effect at this time |
|---|
| 115 |
* |
|---|
| 116 |
* @return string The PHP code |
|---|
| 117 |
*/ |
|---|
| 118 |
public function getRetrieveByPkParamsForEdit($indent, $prefix) |
|---|
| 119 |
{ |
|---|
| 120 |
$params = array(); |
|---|
| 121 |
foreach ($this->getPrimaryKey() as $pk) |
|---|
| 122 |
{ |
|---|
| 123 |
$name = sfInflector::underscore($pk->getPhpName()); |
|---|
| 124 |
|
|---|
| 125 |
$params[] = sprintf("\$request->getParameter('%s')", $name); |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params); |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
public function getMethodParamsForGetOrCreate() |
|---|
| 137 |
|
|---|
| 138 |
$method_params = array(); |
|---|
| 139 |
$this->getPrimaryKey() as $pk) |
|---|
| 140 |
|
|---|
| 141 |
$fieldName = sfInflector::underscore($pk->getPhpName()); |
|---|
| 142 |
$method_params[] = "\$$fieldName = '$fieldName'"; |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
implode(', ', $method_params); |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
public function getTestPksForGetOrCreate($fieldNameAsArgument = true) |
|---|
| 156 |
|
|---|
| 157 |
$test_pks = array(); |
|---|
| 158 |
$this->getPrimaryKey() as $pk) |
|---|
| 159 |
|
|---|
| 160 |
$fieldName = sfInflector::underscore($pk->getPhpName()); |
|---|
| 161 |
|
|---|
| 162 |
$test_pks[] = sprintf("\$this->getRequestParameter(%s) === ''", $fieldNameAsArgument ? "\$$fieldName" : "'".$fieldName."'"); |
|---|
| 163 |
$test_pks[] = sprintf("\$this->getRequestParameter(%s) === null", $fieldNameAsArgument ? "\$$fieldName" : "'".$fieldName."'"); |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
implode("\n || ", $test_pks); |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
public function getRetrieveByPkParamsForGetOrCreate() |
|---|
| 175 |
|
|---|
| 176 |
$retrieve_params = array(); |
|---|
| 177 |
$this->getPrimaryKey() as $pk) |
|---|
| 178 |
|
|---|
| 179 |
$fieldName = sfInflector::underscore($pk->getPhpName()); |
|---|
| 180 |
$retrieve_params[] = "\$this->getRequestParameter(\$$fieldName)"; |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
implode(",\n".str_repeat(' ', max(0, 45 - strlen($this->singularName.$this->className))), $retrieve_params); |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
public function getTableMap() |
|---|
| 192 |
|
|---|
| 193 |
$this->tableMap; |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
protected function setScaffoldingClassName($className) |
|---|
| 202 |
|
|---|
| 203 |
$this->singularName = sfInflector::underscore($className); |
|---|
| 204 |
$this->pluralName = $this->singularName.'s'; |
|---|
| 205 |
$this->className = $className; |
|---|
| 206 |
$this->peerClassName = $className.'Peer'; |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
public function getSingularName() |
|---|
| 215 |
|
|---|
| 216 |
$this->singularName; |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
public function getPluralName() |
|---|
| 225 |
|
|---|
| 226 |
$this->pluralName; |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
public function getClassName() |
|---|
| 235 |
|
|---|
| 236 |
$this->className; |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
public function getPeerClassName() |
|---|
| 245 |
|
|---|
| 246 |
$this->peerClassName; |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
public function getPrimaryKey() |
|---|
| 255 |
|
|---|
| 256 |
$this->primaryKey; |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
public function getMap() |
|---|
| 265 |
|
|---|
| 266 |
$this->map; |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
public function getPrimaryKeyUrlParams($prefix = '') |
|---|
| 277 |
|
|---|
| 278 |
$params = array(); |
|---|
| 279 |
$this->getPrimaryKey() as $pk) |
|---|
| 280 |
|
|---|
| 281 |
$phpName = $pk->getPhpName(); |
|---|
| 282 |
$fieldName = sfInflector::underscore($phpName); |
|---|
| 283 |
$params[] = "$fieldName='.".$this->getColumnGetter($pk, true, $prefix); |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
implode(".'&", $params); |
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
public function getPrimaryKeyIsSet($prefix = '') |
|---|
| 297 |
|
|---|
| 298 |
$params = array(); |
|---|
| 299 |
$this->getPrimaryKey() as $pk) |
|---|
| 300 |
|
|---|
| 301 |
$params[] = $this->getColumnGetter($pk, true, $prefix); |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
implode(' && ', $params); |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
protected function getObjectTagParams($params, $default_params = array()) |
|---|
| 316 |
|
|---|
| 317 |
var_export(array_merge($default_params, $params), true); |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
public function getColumnListTag($column, $params = array()) |
|---|
| 329 |
|
|---|
| 330 |
$type = $column->getCreoleType(); |
|---|
| 331 |
|
|---|
| 332 |
$columnGetter = $this->getColumnGetter($column, true); |
|---|
| 333 |
|
|---|
| 334 |
$type == CreoleTypes::TIMESTAMP) |
|---|
| 335 |
|
|---|
| 336 |
"format_date($columnGetter, 'f')"; |
|---|
| 337 |
|
|---|
| 338 |
$type == CreoleTypes::DATE) |
|---|
| 339 |
|
|---|
| 340 |
"format_date($columnGetter, 'D')"; |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
"$columnGetter"; |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
public function getCrudColumnEditTag($column, $params = array()) |
|---|
| 357 |
|
|---|
| 358 |
$type = $column->getCreoleType(); |
|---|
| 359 |
|
|---|
| 360 |
$column->isForeignKey()) |
|---|
| 361 |
|
|---|
| 362 |
$column->isNotNull() && !isset($params['include_blank'])) |
|---|
| 363 |
|
|---|
| 364 |
$params['include_blank'] = true; |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
$this->getPHPObjectHelper('select_tag', $column, $params, array('related_class' => $this->getRelatedClassName($column))); |
|---|
| 368 |
|
|---|
| 369 |
$type == CreoleTypes::DATE) |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true)); |
|---|
| 373 |
|
|---|
| 374 |
$type == CreoleTypes::TIMESTAMP) |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true)); |
|---|
| 378 |
|
|---|
| 379 |
$type == CreoleTypes::BOOLEAN) |
|---|
| 380 |
|
|---|
| 381 |
$this->getPHPObjectHelper('checkbox_tag', $column, $params); |
|---|
| 382 |
|
|---|
| 383 |
$type == CreoleTypes::CHAR || $type == CreoleTypes::VARCHAR) |
|---|
| 384 |
|
|---|
| 385 |
$size = ($column->getSize() > 20 ? ($column->getSize() < 80 ? $column->getSize() : 80) : 20); |
|---|
| 386 |
$this->getPHPObjectHelper('input_tag', $column, $params, array('size' => $size)); |
|---|
| 387 |
|
|---|
| 388 |
$type == CreoleTypes::INTEGER || $type == CreoleTypes::TINYINT || $type == CreoleTypes::SMALLINT || $type == CreoleTypes::BIGINT) |
|---|
| 389 |
|
|---|
| 390 |
$this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7)); |
|---|
| 391 |
|
|---|
| 392 |
$type == CreoleTypes::FLOAT || $type == CreoleTypes::DOUBLE || $type == CreoleTypes::DECIMAL || $type == CreoleTypes::NUMERIC || $type == CreoleTypes::REAL) |
|---|
| 393 |
|
|---|
| 394 |
$this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7)); |
|---|
| 395 |
|
|---|
| 396 |
$type == CreoleTypes::TEXT || $type == CreoleTypes::LONGVARCHAR) |
|---|
| 397 |
|
|---|
| 398 |
$this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3')); |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
$this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true)); |
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
abstract protected function loadPrimaryKeys(); |
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
abstract protected function loadMapBuilderClasses(); |
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
abstract function getPHPObjectHelper($helperName, $column, $params, $localParams = array()); |
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
abstract function getColumnGetter($column, $developed = false , $prefix = ''); |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
abstract function getRelatedClassName($column); |
|---|
| 463 |
|
|---|
| 464 |
|
|---|