| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfPropelCrudGenerator extends sfAdminGenerator |
|---|
| 22 |
{ |
|---|
| 23 |
protected |
|---|
| 24 |
$peerClassName = ''; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Initializes the current sfGenerator instance. |
|---|
| 28 |
* |
|---|
| 29 |
* @param sfGeneratorManager $generatorManager A sfGeneratorManager instance |
|---|
| 30 |
*/ |
|---|
| 31 |
public function initialize(sfGeneratorManager $generatorManager) |
|---|
| 32 |
{ |
|---|
| 33 |
parent::initialize($generatorManager); |
|---|
| 34 |
|
|---|
| 35 |
$this->setGeneratorClass('sfPropelCrud'); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
* Sets the class name to use for scaffolding |
|---|
| 40 |
* |
|---|
| 41 |
* @param string $className class name |
|---|
| 42 |
*/ |
|---|
| 43 |
protected function setScaffoldingClassName($className) |
|---|
| 44 |
{ |
|---|
| 45 |
parent::setScaffoldingClassName($className); |
|---|
| 46 |
|
|---|
| 47 |
$this->peerClassName = constant($className.'::PEER'); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
* Loads primary keys. |
|---|
| 52 |
* |
|---|
| 53 |
* This method is ORM dependant. |
|---|
| 54 |
* |
|---|
| 55 |
* @throws sfException |
|---|
| 56 |
*/ |
|---|
| 57 |
protected function loadPrimaryKeys() |
|---|
| 58 |
{ |
|---|
| 59 |
foreach ($this->tableMap->getColumns() as $column) |
|---|
| 60 |
{ |
|---|
| 61 |
if ($column->isPrimaryKey()) |
|---|
| 62 |
{ |
|---|
| 63 |
$this->primaryKey[] = $column; |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
if (!count($this->primaryKey)) |
|---|
| 68 |
{ |
|---|
| 69 |
throw new sfException(sprintf('Cannot generate a module for a model without a primary key (%s)', $this->className)); |
|---|
| 70 |
} |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
* Loads map builder classes. |
|---|
| 75 |
* |
|---|
| 76 |
* This method is ORM dependant. |
|---|
| 77 |
* |
|---|
| 78 |
* @throws sfException |
|---|
| 79 |
*/ |
|---|
| 80 |
protected function loadMapBuilderClasses() |
|---|
| 81 |
{ |
|---|
| 82 |
|
|---|
| 83 |
$classes = sfFinder::type('file')->name('*MapBuilder.php')->in($this->generatorManager->getConfiguration()->getModelDirs()); |
|---|
| 84 |
foreach ($classes as $class) |
|---|
| 85 |
{ |
|---|
| 86 |
$omClass = basename($class, 'MapBuilder.php'); |
|---|
| 87 |
if (class_exists($omClass) && is_subclass_of($omClass, 'BaseObject')) |
|---|
| 88 |
{ |
|---|
| 89 |
$class_map_builder = basename($class, '.php'); |
|---|
| 90 |
$maps[$class_map_builder] = new $class_map_builder(); |
|---|
| 91 |
if (!$maps[$class_map_builder]->isBuilt()) |
|---|
| 92 |
{ |
|---|
| 93 |
$maps[$class_map_builder]->doBuild(); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
if ($this->className == $omClass) |
|---|
| 97 |
{ |
|---|
| 98 |
$this->map = $maps[$class_map_builder]; |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
} |
|---|
| 102 |
if (!$this->map) |
|---|
| 103 |
{ |
|---|
| 104 |
throw new sfException(sprintf('The model class "%s" does not exist.', $this->className)); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
$this->tableMap = $this->map->getDatabaseMap()->getTable(constant($this->peerClassName.'::TABLE_NAME')); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Generates a PHP call to an object helper. |
|---|
| 112 |
* |
|---|
| 113 |
* @param string $helperName The helper name |
|---|
| 114 |
* @param string $column The column name |
|---|
| 115 |
* @param array $params An array of parameters |
|---|
| 116 |
* @param array $localParams An array of local parameters |
|---|
| 117 |
* |
|---|
| 118 |
* @return string PHP code |
|---|
| 119 |
*/ |
|---|
| 120 |
function getPHPObjectHelper($helperName, $column, $params, $localParams = array()) |
|---|
| 121 |
{ |
|---|
| 122 |
$params = $this->getObjectTagParams($params, $localParams); |
|---|
| 123 |
|
|---|
| 124 |
return sprintf('object_%s($%s, \'%s\', %s)', $helperName, $this->getSingularName(), $this->getColumnGetter($column, false), $params); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'. |
|---|
| 129 |
* |
|---|
| 130 |
* @param string $column The column name |
|---|
| 131 |
* @param boolean $developed true if you want developped method names, false otherwise |
|---|
| 132 |
* @param string $prefix The prefix value |
|---|
| 133 |
* |
|---|
| 134 |
* @return string PHP code |
|---|
| 135 |
*/ |
|---|
| 136 |
function getColumnGetter($column, $developed = false, $prefix = '') |
|---|
| 137 |
{ |
|---|
| 138 |
$getter = 'get'.$column->getPhpName(); |
|---|
| 139 |
if ($developed) |
|---|
| 140 |
{ |
|---|
| 141 |
$getter = sprintf('$%s%s->%s()', $prefix, $this->getSingularName(), $getter); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
return $getter; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
* Gets the PHP name of the related class name. |
|---|
| 149 |
* |
|---|
| 150 |
* Used for foreign keys only; this method should be removed when we use sfAdminColumn instead. |
|---|
| 151 |
* |
|---|
| 152 |
* @param string $column The column name |
|---|
| 153 |
* |
|---|
| 154 |
* @return string The PHP name of the related class name |
|---|
| 155 |
*/ |
|---|
| 156 |
function getRelatedClassName($column) |
|---|
| 157 |
{ |
|---|
| 158 |
$relatedTable = $this->getMap()->getDatabaseMap()->getTable($column->getRelatedTableName()); |
|---|
| 159 |
|
|---|
| 160 |
return $relatedTable->getPhpName(); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
* Returns HTML code for a column in edit mode. |
|---|
| 165 |
* |
|---|
| 166 |
* @param string The column name |
|---|
| 167 |
* @param array The parameters |
|---|
| 168 |
* |
|---|
| 169 |
* @return string HTML code |
|---|
| 170 |
*/ |
|---|
| 171 |
public function getCrudColumnEditTag($column, $params = array()) |
|---|
| 172 |
{ |
|---|
| 173 |
$type = $column->getType(); |
|---|
| 174 |
|
|---|
| 175 |
if ($column->isForeignKey()) |
|---|
| 176 |
{ |
|---|
| 177 |
if (!$column->isNotNull() && !isset($params['include_blank'])) |
|---|
| 178 |
{ |
|---|
| 179 |
$params['include_blank'] = true; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
return $this->getPHPObjectHelper('select_tag', $column, $params, array('related_class' => $this->getRelatedClassName($column))); |
|---|
| 183 |
} |
|---|
| 184 |
else if ($type == PropelColumnTypes::DATE) |
|---|
| 185 |
{ |
|---|
| 186 |
|
|---|
| 187 |
return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true)); |
|---|
| 188 |
} |
|---|
| 189 |
else if ($type == PropelColumnTypes::TIMESTAMP) |
|---|
| 190 |
{ |
|---|
| 191 |
|
|---|
| 192 |
return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true)); |
|---|
| 193 |
} |
|---|
| 194 |
else if ($type == PropelColumnTypes::BOOLEAN) |
|---|
| 195 |
{ |
|---|
| 196 |
return $this->getPHPObjectHelper('checkbox_tag', $column, $params); |
|---|
| 197 |
} |
|---|
| 198 |
else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR) |
|---|
| 199 |
{ |
|---|
| 200 |
$size = ($column->getSize() > 20 ? ($column->getSize() < 80 ? $column->getSize() : 80) : 20); |
|---|
| 201 |
return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => $size)); |
|---|
| 202 |
} |
|---|
| 203 |
else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT) |
|---|
| 204 |
{ |
|---|
| 205 |
return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7)); |
|---|
| 206 |
} |
|---|
| 207 |
else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL) |
|---|
| 208 |
{ |
|---|
| 209 |
return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7)); |
|---|
| 210 |
} |
|---|
| 211 |
else if ($type == PropelColumnTypes::LONGVARCHAR || $type == PropelColumnTypes::CLOB) |
|---|
| 212 |
{ |
|---|
| 213 |
return $this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3')); |
|---|
| 214 |
} |
|---|
| 215 |
else |
|---|
| 216 |
{ |
|---|
| 217 |
return $this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true)); |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
* Returns HTML code for a column in filter mode. |
|---|
| 223 |
* |
|---|
| 224 |
* @param string The column name |
|---|
| 225 |
* @param array The parameters |
|---|
| 226 |
* |
|---|
| 227 |
* @return string HTML code |
|---|
| 228 |
*/ |
|---|
| 229 |
public function getColumnFilterTag($column, $params = array()) |
|---|
| 230 |
{ |
|---|
| 231 |
$user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params'); |
|---|
| 232 |
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params); |
|---|
| 233 |
$params = $user_params ? array_merge($params, $user_params) : $params; |
|---|
| 234 |
|
|---|
| 235 |
if ($column->isComponent()) |
|---|
| 236 |
{ |
|---|
| 237 |
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))"; |
|---|
| 238 |
} |
|---|
| 239 |
else if ($column->isPartial()) |
|---|
| 240 |
{ |
|---|
| 241 |
return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))"; |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
$type = $column->getType(); |
|---|
| 245 |
|
|---|
| 246 |
$default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null"; |
|---|
| 247 |
$unquotedName = 'filters['.$column->getName().']'; |
|---|
| 248 |
$name = "'$unquotedName'"; |
|---|
| 249 |
|
|---|
| 250 |
if ($column->isForeignKey()) |
|---|
| 251 |
{ |
|---|
| 252 |
$params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName)); |
|---|
| 253 |
return "object_select_tag($default_value, null, $params)"; |
|---|
| 254 |
|
|---|
| 255 |
} |
|---|
| 256 |
else if ($type == PropelColumnTypes::DATE) |
|---|
| 257 |
{ |
|---|
| 258 |
|
|---|
| 259 |
$params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png')); |
|---|
| 260 |
return "input_date_range_tag($name, $default_value, $params)"; |
|---|
| 261 |
} |
|---|
| 262 |
else if ($type == PropelColumnTypes::TIMESTAMP) |
|---|
| 263 |
{ |
|---|
| 264 |
|
|---|
| 265 |
$params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png')); |
|---|
| 266 |
return "input_date_range_tag($name, $default_value, $params)"; |
|---|
| 267 |
} |
|---|
| 268 |
else if ($type == PropelColumnTypes::BOOLEAN) |
|---|
| 269 |
{ |
|---|
| 270 |
$defaultIncludeCustom = '__("yes or no")'; |
|---|
| 271 |
|
|---|
| 272 |
$option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom)); |
|---|
| 273 |
$params = $this->getObjectTagParams($params); |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
$option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params); |
|---|
| 277 |
|
|---|
| 278 |
$options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)"; |
|---|
| 279 |
|
|---|
| 280 |
return "select_tag($name, $options, $params)"; |
|---|
| 281 |
} |
|---|
| 282 |
else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR || $type == PropelColumnTypes::LONGVARCHAR || $type == PropelColumnTypes::CLOB) |
|---|
| 283 |
{ |
|---|
| 284 |
$size = ($column->getSize() < 15 ? $column->getSize() : 15); |
|---|
| 285 |
$params = $this->getObjectTagParams($params, array('size' => $size)); |
|---|
| 286 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 287 |
} |
|---|
| 288 |
else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT) |
|---|
| 289 |
{ |
|---|
| 290 |
$params = $this->getObjectTagParams($params, array('size' => 7)); |
|---|
| 291 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 292 |
} |
|---|
| 293 |
else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL) |
|---|
| 294 |
{ |
|---|
| 295 |
$params = $this->getObjectTagParams($params, array('size' => 7)); |
|---|
| 296 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 297 |
} |
|---|
| 298 |
else |
|---|
| 299 |
{ |
|---|
| 300 |
$params = $this->getObjectTagParams($params, array('disabled' => true)); |
|---|
| 301 |
return "input_tag($name, $default_value, $params)"; |
|---|
| 302 |
} |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
* Returns HTML code for a column in edit mode. |
|---|
| 307 |
* |
|---|
| 308 |
* @param string The column name |
|---|
| 309 |
* @param array The parameters |
|---|
| 310 |
* |
|---|
| 311 |
* @return string HTML code |
|---|
| 312 |
*/ |
|---|
| 313 |
public function getColumnEditTag($column, $params = array()) |
|---|
| 314 |
{ |
|---|
| 315 |
|
|---|
| 316 |
$user_params = $this->getParameterValue('edit.fields.'.$column->getName().'.params'); |
|---|
| 317 |
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params); |
|---|
| 318 |
$params = $user_params ? array_merge($params, $user_params) : $params; |
|---|
| 319 |
|
|---|
| 320 |
if ($column->isComponent()) |
|---|
| 321 |
{ |
|---|
| 322 |
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 323 |
} |
|---|
| 324 |
else if ($column->isPartial()) |
|---|
| 325 |
{ |
|---|
| 326 |
return "get_partial('".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
$params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params); |
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
$type = $column->getType(); |
|---|
| 334 |
if ($type == PropelColumnTypes::DATE) |
|---|
| 335 |
{ |
|---|
| 336 |
$params = array_merge(array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params); |
|---|
| 337 |
} |
|---|
| 338 |
else if ($type == PropelColumnTypes::TIMESTAMP) |
|---|
| 339 |
{ |
|---|
| 340 |
$params = array_merge(array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params); |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
if ($inputType = $this->getParameterValue('edit.fields.'.$column->getName().'.type')) |
|---|
| 345 |
{ |
|---|
| 346 |
if ($inputType == 'plain') |
|---|
| 347 |
{ |
|---|
| 348 |
return $this->getColumnListTag($column, $params); |
|---|
| 349 |
} |
|---|
| 350 |
else |
|---|
| 351 |
{ |
|---|
| 352 |
return $this->getPHPObjectHelper($inputType, $column, $params); |
|---|
| 353 |
} |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
return $this->getCrudColumnEditTag($column, $params); |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
* Returns HTML code for a column in list mode. |
|---|
| 362 |
* |
|---|
| 363 |
* @param string The column name |
|---|
| 364 |
* @param array The parameters |
|---|
| 365 |
* |
|---|
| 366 |
* @return string HTML code |
|---|
| 367 |
*/ |
|---|
| 368 |
public function getColumnListTag($column, $params = array()) |
|---|
| 369 |
{ |
|---|
| 370 |
$user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params'); |
|---|
| 371 |
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params); |
|---|
| 372 |
$params = $user_params ? array_merge($params, $user_params) : $params; |
|---|
| 373 |
|
|---|
| 374 |
$type = $column->getType(); |
|---|
| 375 |
|
|---|
| 376 |
$columnGetter = $this->getColumnGetter($column, true); |
|---|
| 377 |
|
|---|
| 378 |
if ($column->isComponent()) |
|---|
| 379 |
{ |
|---|
| 380 |
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 381 |
} |
|---|
| 382 |
else if ($column->isPartial()) |
|---|
| 383 |
{ |
|---|
| 384 |
return "get_partial('".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))"; |
|---|
| 385 |
} |
|---|
| 386 |
else if ($type == PropelColumnTypes::DATE || $type == PropelColumnTypes::TIMESTAMP) |
|---|
| 387 |
{ |
|---|
| 388 |
$format = isset($params['date_format']) ? $params['date_format'] : ($type == PropelColumnTypes::DATE ? 'D' : 'f'); |
|---|
| 389 |
return "($columnGetter !== null && $columnGetter !== '') ? format_date($columnGetter, \"$format\") : ''"; |
|---|
| 390 |
} |
|---|
| 391 |
elseif ($type == PropelColumnTypes::BOOLEAN) |
|---|
| 392 |
{ |
|---|
| 393 |
return "$columnGetter ? image_tag(sfConfig::get('sf_admin_web_dir').'/images/tick.png') : ' '"; |
|---|
| 394 |
} |
|---|
| 395 |
else |
|---|
| 396 |
{ |
|---|
| 397 |
return "$columnGetter"; |
|---|
| 398 |
} |
|---|
| 399 |
} |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
* Gets the Peer class name. |
|---|
| 403 |
* |
|---|
| 404 |
* @return string |
|---|
| 405 |
*/ |
|---|
| 406 |
public function getPeerClassName() |
|---|
| 407 |
{ |
|---|
| 408 |
return $this->peerClassName; |
|---|
| 409 |
} |
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
* Returns the URL for a given action. |
|---|
| 413 |
* |
|---|
| 414 |
* @return string The URL related to a given action |
|---|
| 415 |
*/ |
|---|
| 416 |
public function getUrlForAction($action) |
|---|
| 417 |
{ |
|---|
| 418 |
if (isset($this->params['route_prefix'])) |
|---|
| 419 |
{ |
|---|
| 420 |
return 'list' == $action ? $this->params['route_prefix'] : $this->params['route_prefix'].'_'.$action; |
|---|
| 421 |
} |
|---|
| 422 |
else |
|---|
| 423 |
{ |
|---|
| 424 |
return $this->getModuleName().'/'.$action; |
|---|
| 425 |
} |
|---|
| 426 |
} |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|