123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- abstract class sfCrudGenerator extends sfGenerator
- {
- protected
- $singularName = '',
- $pluralName = '',
- $map = null,
- $tableMap = null,
- $primaryKey = array(),
- $className = '',
- $params = array();
-
- public function generate($params = array())
- {
- $this->params = $params;
- $required_parameters = array('model_class', 'moduleName');
- foreach ($required_parameters as $entry)
- {
- if (!isset($this->params[$entry]))
- {
- throw new sfParseException(sprintf('You must specify a "%s".', $entry));
- }
- }
- $modelClass = $this->params['model_class'];
- if (!class_exists($modelClass))
- {
- throw new sfInitializationException(sprintf('Unable to scaffold nonexistent model "%s".', $modelClass));
- }
- $this->setScaffoldingClassName($modelClass);
-
- $this->setGeneratedModuleName('auto'.ucfirst($this->params['moduleName']));
- $this->setModuleName($this->params['moduleName']);
-
- $this->loadMapBuilderClasses();
-
- $this->loadPrimaryKeys();
-
- $theme = isset($this->params['theme']) ? $this->params['theme'] : 'default';
- $themeDir = $this->generatorManager->getConfiguration()->getGeneratorTemplate($this->getGeneratorClass(), $theme, '');
- if (!is_dir($themeDir))
- {
- throw new sfConfigurationException(sprintf('The theme "%s" does not exist.', $theme));
- }
- $this->setTheme($theme);
- $files = sfFinder::type('file')->relative()->in($themeDir);
- $this->generatePhpFiles($this->generatedModuleName, $files);
-
- $data = "require_once(sfConfig::get('sf_module_cache_dir').'/".$this->generatedModuleName."/actions/actions.class.php');\n";
- return $data;
- }
-
- public function getRetrieveByPkParamsForAction($indent, $callee = '$this->getRequestParameter')
- {
- $params = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $params[] = "$callee('".sfInflector::underscore($pk->getPhpName())."')";
- }
- return implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params);
- }
-
- public function getRetrieveByPkParamsForEdit($indent, $prefix)
- {
- $params = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $params[] = sprintf("\$request->getParameter('%s')", sfInflector::underscore($pk->getPhpName()));
- }
- return implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params);
- }
-
- public function getMethodParamsForGetOrCreate()
- {
- $method_params = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $fieldName = sfInflector::underscore($pk->getPhpName());
- $method_params[] = "\$$fieldName = '$fieldName'";
- }
- return implode(', ', $method_params);
- }
-
- public function getTestPksForGetOrCreate($fieldNameAsArgument = true)
- {
- $test_pks = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $fieldName = sfInflector::underscore($pk->getPhpName());
-
- $test_pks[] = sprintf("\$this->getRequestParameter(%s) === ''", $fieldNameAsArgument ? "\$$fieldName" : "'".$fieldName."'");
- $test_pks[] = sprintf("\$this->getRequestParameter(%s) === null", $fieldNameAsArgument ? "\$$fieldName" : "'".$fieldName."'");
- }
- return implode("\n || ", $test_pks);
- }
-
- public function getRetrieveByPkParamsForGetOrCreate()
- {
- $retrieve_params = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $fieldName = sfInflector::underscore($pk->getPhpName());
- $retrieve_params[] = "\$this->getRequestParameter(\$$fieldName)";
- }
- return implode(",\n".str_repeat(' ', max(0, 45 - strlen($this->singularName.$this->className))), $retrieve_params);
- }
-
- public function getTableMap()
- {
- return $this->tableMap;
- }
-
- protected function setScaffoldingClassName($className)
- {
- $this->singularName = isset($this->params['singular']) ? $this->params['singular'] : sfInflector::underscore($className);
- $this->pluralName = isset($this->params['plural']) ? $this->params['plural'] : $this->singularName.'s';
- $this->className = $className;
- }
-
- public function getSingularName()
- {
- return $this->singularName;
- }
-
- public function getPluralName()
- {
- return $this->pluralName;
- }
-
- public function getClassName()
- {
- return $this->className;
- }
-
- public function getPrimaryKey()
- {
- return $this->primaryKey;
- }
-
- public function getMap()
- {
- return $this->map;
- }
-
- public function getPrimaryKeyUrlParams($prefix = '', $full = false)
- {
- $params = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $phpName = $pk->getPhpName();
- $fieldName = sfInflector::underscore($phpName);
- if ($full)
- {
- $params[] = "$fieldName='.".$prefix.'->'.$this->getColumnGetter($pk, false).'()';
- }
- else
- {
- $params[] = "$fieldName='.".$this->getColumnGetter($pk, true, $prefix);
- }
- }
- return implode(".'&", $params);
- }
-
- public function getPrimaryKeyIsSet($prefix = '')
- {
- $params = array();
- foreach ($this->getPrimaryKey() as $pk)
- {
- $params[] = $this->getColumnGetter($pk, true, $prefix);
- }
- return implode(' && ', $params);
- }
-
- protected function getObjectTagParams($params, $default_params = array())
- {
- return var_export(array_merge($default_params, $params), true);
- }
-
- public function getColumnListTag($column, $params = array())
- {
- $type = $column->getType();
-
- $columnGetter = $this->getColumnGetter($column, true);
- if ($type == PropelColumnTypes::TIMESTAMP)
- {
- return "format_date($columnGetter, 'f')";
- }
- elseif ($type == PropelColumnTypes::DATE)
- {
- return "format_date($columnGetter, 'D')";
- }
- else
- {
- return "$columnGetter";
- }
- }
-
- public function getCrudColumnEditTag($column, $params = array())
- {
- $type = $column->getType();
- if ($column->isForeignKey())
- {
- if (!$column->isNotNull() && !isset($params['include_blank']))
- {
- $params['include_blank'] = true;
- }
- return $this->getPHPObjectHelper('select_tag', $column, $params, array('related_class' => $this->getRelatedClassName($column)));
- }
- else if ($type == PropelColumnTypes::DATE)
- {
-
- return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true));
- }
- else if ($type == PropelColumnTypes::TIMESTAMP)
- {
-
- return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true));
- }
- else if ($type == PropelColumnTypes::BOOLEAN)
- {
- return $this->getPHPObjectHelper('checkbox_tag', $column, $params);
- }
- else if ($type == PropelColumnTypes::CHAR || $type == PropelColumnTypes::VARCHAR)
- {
- $size = ($column->getSize() > 20 ? ($column->getSize() < 80 ? $column->getSize() : 80) : 20);
- return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => $size));
- }
- else if ($type == PropelColumnTypes::INTEGER || $type == PropelColumnTypes::TINYINT || $type == PropelColumnTypes::SMALLINT || $type == PropelColumnTypes::BIGINT)
- {
- return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
- }
- else if ($type == PropelColumnTypes::FLOAT || $type == PropelColumnTypes::DOUBLE || $type == PropelColumnTypes::DECIMAL || $type == PropelColumnTypes::NUMERIC || $type == PropelColumnTypes::REAL)
- {
- return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
- }
- else if ($type == PropelColumnTypes::LONGVARCHAR)
- {
- return $this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3'));
- }
- else
- {
- return $this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true));
- }
- }
-
- abstract protected function loadPrimaryKeys();
-
- abstract protected function loadMapBuilderClasses();
-
- abstract function getPHPObjectHelper($helperName, $column, $params, $localParams = array());
-
- abstract function getColumnGetter($column, $developed = false , $prefix = '');
-
- abstract function getRelatedClassName($column);
- }
|