[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * CRUD generator. 13 * 14 * This class generates a basic CRUD module. 15 * 16 * @package symfony 17 * @subpackage generator 18 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 19 * @version SVN: $Id: sfPropelCrudGenerator.class.php 2342 2006-10-06 07:19:22Z chtito $ 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 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 $error = 'You must specify a "%s"'; 50 $error = sprintf($error, $entry); 51 52 throw new sfParseException($error); 53 } 54 } 55 56 $modelClass = $this->params['model_class']; 57 58 if (!class_exists($modelClass)) 59 { 60 $error = 'Unable to scaffold unexistant model "%s"'; 61 $error = sprintf($error, $modelClass); 62 63 throw new sfInitializationException($error); 64 } 65 66 $this->setScaffoldingClassName($modelClass); 67 68 // generated module name 69 $this->setGeneratedModuleName('auto'.ucfirst($this->params['moduleName'])); 70 $this->setModuleName($this->params['moduleName']); 71 72 // get some model metadata 73 $this->loadMapBuilderClasses(); 74 75 // load all primary keys 76 $this->loadPrimaryKeys(); 77 78 // theme exists? 79 $theme = isset($this->params['theme']) ? $this->params['theme'] : 'default'; 80 $themeDir = sfLoader::getGeneratorTemplate($this->getGeneratorClass(), $theme, ''); 81 if (!is_dir($themeDir)) 82 { 83 $error = 'The theme "%s" does not exist.'; 84 $error = sprintf($error, $theme); 85 throw new sfConfigurationException($error); 86 } 87 88 $this->setTheme($theme); 89 $templateFiles = sfFinder::type('file')->name('*.php')->relative()->in($themeDir.'/templates'); 90 $configFiles = sfFinder::type('file')->name('*.yml')->relative()->in($themeDir.'/config'); 91 92 $this->generatePhpFiles($this->generatedModuleName, $templateFiles, $configFiles); 93 94 // require generated action class 95 $data = "require_once(sfConfig::get('sf_module_cache_dir').'/".$this->generatedModuleName."/actions/actions.class.php');\n"; 96 97 return $data; 98 } 99 100 /** 101 * Returns PHP code for primary keys parameters. 102 * 103 * @param integer The indentation value 104 * 105 * @return string The PHP code 106 */ 107 public function getRetrieveByPkParamsForAction($indent) 108 { 109 $params = array(); 110 foreach ($this->getPrimaryKey() as $pk) 111 { 112 $params[] = "\$this->getRequestParameter('".sfInflector::underscore($pk->getPhpName())."')"; 113 } 114 115 return implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params); 116 } 117 118 /** 119 * Returns PHP code for getOrCreate() parameters. 120 * 121 * @return string The PHP code 122 */ 123 public function getMethodParamsForGetOrCreate() 124 { 125 $method_params = array(); 126 foreach ($this->getPrimaryKey() as $pk) 127 { 128 $fieldName = sfInflector::underscore($pk->getPhpName()); 129 $method_params[] = "\$$fieldName = '$fieldName'"; 130 } 131 132 return implode(', ', $method_params); 133 } 134 135 /** 136 * Returns PHP code for getOrCreate() promary keys condition. 137 * 138 * @param boolean true if we pass the field name as an argument, false otherwise 139 * 140 * @return string The PHP code 141 */ 142 public function getTestPksForGetOrCreate($fieldNameAsArgument = true) 143 { 144 $test_pks = array(); 145 foreach ($this->getPrimaryKey() as $pk) 146 { 147 $fieldName = sfInflector::underscore($pk->getPhpName()); 148 $test_pks[] = sprintf("!\$this->getRequestParameter(%s)", $fieldNameAsArgument ? "\$$fieldName" : "'".$fieldName."'"); 149 } 150 151 return implode("\n || ", $test_pks); 152 } 153 154 /** 155 * Returns PHP code for primary keys parameters used in getOrCreate() method. 156 * 157 * @return string The PHP code 158 */ 159 public function getRetrieveByPkParamsForGetOrCreate() 160 { 161 $retrieve_params = array(); 162 foreach ($this->getPrimaryKey() as $pk) 163 { 164 $fieldName = sfInflector::underscore($pk->getPhpName()); 165 $retrieve_params[] = "\$this->getRequestParameter(\$$fieldName)"; 166 } 167 168 return implode(",\n".str_repeat(' ', max(0, 45 - strlen($this->singularName.$this->className))), $retrieve_params); 169 } 170 171 /** 172 * Gets the table map for the current model class. 173 * 174 * @return TableMap A TableMap instance 175 */ 176 public function getTableMap() 177 { 178 return $this->tableMap; 179 } 180 181 /** 182 * Sets the class name to use for scaffolding 183 * 184 * @param string class name 185 */ 186 protected function setScaffoldingClassName($className) 187 { 188 $this->singularName = sfInflector::underscore($className); 189 $this->pluralName = $this->singularName.'s'; 190 $this->className = $className; 191 $this->peerClassName = $className.'Peer'; 192 } 193 194 /** 195 * Gets the singular name for current scaffolding class. 196 * 197 * @return string 198 */ 199 public function getSingularName() 200 { 201 return $this->singularName; 202 } 203 204 /** 205 * Gets the plural name for current scaffolding class. 206 * 207 * @return string 208 */ 209 public function getPluralName() 210 { 211 return $this->pluralName; 212 } 213 214 /** 215 * Gets the class name for current scaffolding class. 216 * 217 * @return string 218 */ 219 public function getClassName() 220 { 221 return $this->className; 222 } 223 224 /** 225 * Gets the Peer class name. 226 * 227 * @return string 228 */ 229 public function getPeerClassName() 230 { 231 return $this->peerClassName; 232 } 233 234 /** 235 * Gets the primary key name. 236 * 237 * @return string 238 */ 239 public function getPrimaryKey() 240 { 241 return $this->primaryKey; 242 } 243 244 /** 245 * Gets the Map object. 246 * 247 * @return object 248 */ 249 public function getMap() 250 { 251 return $this->map; 252 } 253 254 /** 255 * Returns PHP code to add to a URL for primary keys. 256 * 257 * @param string The prefix value 258 * 259 * @return string PHP code 260 */ 261 public function getPrimaryKeyUrlParams($prefix = '') 262 { 263 $params = array(); 264 foreach ($this->getPrimaryKey() as $pk) 265 { 266 $phpName = $pk->getPhpName(); 267 $fieldName = sfInflector::underscore($phpName); 268 $params[] = "$fieldName='.".$this->getColumnGetter($pk, true, $prefix); 269 } 270 271 return implode(".'&", $params); 272 } 273 274 /** 275 * Gets PHP code for primary key condition. 276 * 277 * @param string The prefix value 278 * 279 * @return string PHP code 280 */ 281 public function getPrimaryKeyIsSet($prefix = '') 282 { 283 $params = array(); 284 foreach ($this->getPrimaryKey() as $pk) 285 { 286 $params[] = $this->getColumnGetter($pk, true, $prefix); 287 } 288 289 return implode(' && ', $params); 290 } 291 292 /** 293 * Gets object tag parameters. 294 * 295 * @param array An array of parameters 296 * @param array An array of default parameters 297 * 298 * @return string PHP code 299 */ 300 protected function getObjectTagParams($params, $default_params = array()) 301 { 302 return var_export(array_merge($default_params, $params), true); 303 } 304 305 /** 306 * Returns HTML code for a column in list mode. 307 * 308 * @param string The column name 309 * @param array The parameters 310 * 311 * @return string HTML code 312 */ 313 public function getColumnListTag($column, $params = array()) 314 { 315 $type = $column->getCreoleType(); 316 317 $columnGetter = $this->getColumnGetter($column, true); 318 319 if ($type == CreoleTypes::TIMESTAMP) 320 { 321 return "format_date($columnGetter, 'f')"; 322 } 323 elseif ($type == CreoleTypes::DATE) 324 { 325 return "format_date($columnGetter, 'D')"; 326 } 327 else 328 { 329 return "$columnGetter"; 330 } 331 } 332 333 /** 334 * Returns HTML code for a column in edit mode. 335 * 336 * @param string The column name 337 * @param array The parameters 338 * 339 * @return string HTML code 340 */ 341 public function getCrudColumnEditTag($column, $params = array()) 342 { 343 $type = $column->getCreoleType(); 344 345 if ($column->isForeignKey()) 346 { 347 if (!$column->isNotNull()) 348 { 349 $params['include_blank'] = true; 350 } 351 return $this->getPHPObjectHelper('select_tag', $column, $params, array('related_class' => $this->getRelatedClassName($column))); 352 } 353 else if ($type == CreoleTypes::DATE) 354 { 355 // rich=false not yet implemented 356 return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true)); 357 } 358 else if ($type == CreoleTypes::TIMESTAMP) 359 { 360 // rich=false not yet implemented 361 return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true)); 362 } 363 else if ($type == CreoleTypes::BOOLEAN) 364 { 365 return $this->getPHPObjectHelper('checkbox_tag', $column, $params); 366 } 367 else if ($type == CreoleTypes::CHAR || $type == CreoleTypes::VARCHAR) 368 { 369 $size = ($column->getSize() > 20 ? ($column->getSize() < 80 ? $column->getSize() : 80) : 20); 370 return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => $size)); 371 } 372 else if ($type == CreoleTypes::INTEGER || $type == CreoleTypes::TINYINT || $type == CreoleTypes::SMALLINT || $type == CreoleTypes::BIGINT) 373 { 374 return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7)); 375 } 376 else if ($type == CreoleTypes::FLOAT || $type == CreoleTypes::DOUBLE || $type == CreoleTypes::DECIMAL || $type == CreoleTypes::NUMERIC || $type == CreoleTypes::REAL) 377 { 378 return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7)); 379 } 380 else if ($type == CreoleTypes::TEXT || $type == CreoleTypes::LONGVARCHAR) 381 { 382 return $this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3')); 383 } 384 else 385 { 386 return $this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true)); 387 } 388 } 389 390 /** 391 * Loads primary keys. 392 * 393 * This method is ORM dependant. 394 * 395 * @throws sfException 396 */ 397 abstract protected function loadPrimaryKeys(); 398 399 /** 400 * Loads map builder classes. 401 * 402 * This method is ORM dependant. 403 * 404 * @throws sfException 405 */ 406 abstract protected function loadMapBuilderClasses(); 407 408 /** 409 * Generates a PHP call to an object helper. 410 * 411 * This method is ORM dependant. 412 * 413 * @param string The helper name 414 * @param string The column name 415 * @param array An array of parameters 416 * @param array An array of local parameters 417 * 418 * @return string PHP code 419 */ 420 abstract function getPHPObjectHelper($helperName, $column, $params, $localParams = array()); 421 422 /** 423 * Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'. 424 * 425 * This method is ORM dependant. 426 * 427 * @param string The column name 428 * @param boolean true if you want developped method names, false otherwise 429 * @param string The prefix value 430 * 431 * @return string PHP code 432 */ 433 abstract function getColumnGetter($column, $developed = false , $prefix = ''); 434 435 /* 436 * Gets the PHP name of the related class name. 437 * 438 * Used for foreign keys only; this method should be removed when we use sfAdminColumn instead. 439 * 440 * This method is ORM dependant. 441 * 442 * @param string The column name 443 * 444 * @return string The PHP name of the related class name 445 */ 446 abstract function getRelatedClassName($column); 447 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |