initial commit

This commit is contained in:
Chris Sewell
2012-11-28 03:55:08 -05:00
parent 7adb399b2e
commit cf140a2e97
3247 changed files with 492437 additions and 0 deletions

View File

@ -0,0 +1,788 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Admin generator.
*
* This class generates an admin module.
*
* This class calls two ORM specific methods:
* getAllColumns()
* and
* getAdminColumnForField($field, $flag = null)
*
* @package symfony
* @subpackage generator
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfAdminGenerator.class.php 5099 2007-09-15 06:44:50Z fabien $
*/
abstract class sfAdminGenerator extends sfCrudGenerator
{
protected
$fields = array();
/**
* Returns HTML code for a help icon.
*
* @param string The column name
* @param string The field type (list, edit)
*
* @return string HTML code
*/
public function getHelpAsIcon($column, $type = '')
{
$help = $this->getParameterValue($type.'.fields.'.$column->getName().'.help');
if ($help)
{
return "[?php echo image_tag(sfConfig::get('sf_admin_web_dir').'/images/help.png', array('align' => 'absmiddle', 'alt' => __('".$this->escapeString($help)."'), 'title' => __('".$this->escapeString($help)."'))) ?]";
}
return '';
}
/**
* Returns HTML code for a help text.
*
* @param string The column name
* @param string The field type (list, edit)
*
* @return string HTML code
*/
public function getHelp($column, $type = '')
{
$help = $this->getParameterValue($type.'.fields.'.$column->getName().'.help');
if ($help)
{
return "<div class=\"sf_admin_edit_help\">[?php echo __('".$this->escapeString($help)."') ?]</div>";
}
return '';
}
/**
* Returns HTML code for an action button.
*
* @param string The action name
* @param array The parameters
* @param boolean Whether to add a primary key link or not
*
* @return string HTML code
*/
public function getButtonToAction($actionName, $params, $pk_link = false)
{
$params = (array) $params;
$options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array();
$method = 'button_to';
$li_class = '';
$only_for = isset($params['only_for']) ? $params['only_for'] : null;
// default values
if ($actionName[0] == '_')
{
$actionName = substr($actionName, 1);
$default_name = strtr($actionName, '_', ' ');
$default_icon = sfConfig::get('sf_admin_web_dir').'/images/'.$actionName.'_icon.png';
$default_action = $actionName;
$default_class = 'sf_admin_action_'.$actionName;
if ($actionName == 'save' || $actionName == 'save_and_add' || $actionName == 'save_and_list')
{
$method = 'submit_tag';
$options['name'] = $actionName;
}
if ($actionName == 'delete')
{
$options['post'] = true;
if (!isset($options['confirm']))
{
$options['confirm'] = 'Are you sure?';
}
$li_class = 'float-left';
$only_for = 'edit';
}
}
else
{
$default_name = strtr($actionName, '_', ' ');
$default_icon = sfConfig::get('sf_admin_web_dir').'/images/default_icon.png';
$default_action = 'List'.sfInflector::camelize($actionName);
$default_class = '';
}
$name = isset($params['name']) ? $params['name'] : $default_name;
$icon = isset($params['icon']) ? sfToolkit::replaceConstants($params['icon']) : $default_icon;
$action = isset($params['action']) ? $params['action'] : $default_action;
$url_params = $pk_link ? '?'.$this->getPrimaryKeyUrlParams() : '\'';
if (!isset($options['class']))
{
if ($default_class)
{
$options['class'] = $default_class;
}
else
{
$options['style'] = 'background: #ffc url('.$icon.') no-repeat 3px 2px';
}
}
$li_class = $li_class ? ' class="'.$li_class.'"' : '';
$html = '<li'.$li_class.'>';
if ($only_for == 'edit')
{
$html .= '[?php if ('.$this->getPrimaryKeyIsSet().'): ?]'."\n";
}
else if ($only_for == 'create')
{
$html .= '[?php if (!'.$this->getPrimaryKeyIsSet().'): ?]'."\n";
}
else if ($only_for !== null)
{
throw new sfConfigurationException(sprintf('The "only_for" parameter can only takes "create" or "edit" as argument ("%s")', $only_for));
}
if ($method == 'submit_tag')
{
$html .= '[?php echo submit_tag(__(\''.$name.'\'), '.var_export($options, true).') ?]';
}
else
{
$phpOptions = var_export($options, true);
// little hack
$phpOptions = preg_replace("/'confirm' => '(.+?)(?<!\\\)'/", '\'confirm\' => __(\'$1\')', $phpOptions);
$html .= '[?php echo button_to(__(\''.$name.'\'), \''.$this->getModuleName().'/'.$action.$url_params.', '.$phpOptions.') ?]';
}
if ($only_for !== null)
{
$html .= '[?php endif; ?]'."\n";
}
$html .= '</li>'."\n";
return $html;
}
/**
* Returns HTML code for an action link.
*
* @param string The action name
* @param array The parameters
* @param boolean Whether to add a primary key link or not
*
* @return string HTML code
*/
public function getLinkToAction($actionName, $params, $pk_link = false)
{
$options = isset($params['params']) ? sfToolkit::stringToArray($params['params']) : array();
// default values
if ($actionName[0] == '_')
{
$actionName = substr($actionName, 1);
$name = $actionName;
$icon = sfConfig::get('sf_admin_web_dir').'/images/'.$actionName.'_icon.png';
$action = $actionName;
if ($actionName == 'delete')
{
$options['post'] = true;
if (!isset($options['confirm']))
{
$options['confirm'] = 'Are you sure?';
}
}
}
else
{
$name = isset($params['name']) ? $params['name'] : $actionName;
$icon = isset($params['icon']) ? sfToolkit::replaceConstants($params['icon']) : sfConfig::get('sf_admin_web_dir').'/images/default_icon.png';
$action = isset($params['action']) ? $params['action'] : 'List'.sfInflector::camelize($actionName);
}
$url_params = $pk_link ? '?'.$this->getPrimaryKeyUrlParams() : '\'';
$phpOptions = var_export($options, true);
// little hack
$phpOptions = preg_replace("/'confirm' => '(.+?)(?<!\\\)'/", '\'confirm\' => __(\'$1\')', $phpOptions);
return '<li>[?php echo link_to(image_tag(\''.$icon.'\', array(\'alt\' => __(\''.$name.'\'), \'title\' => __(\''.$name.'\'))), \''.$this->getModuleName().'/'.$action.$url_params.($options ? ', '.$phpOptions : '').') ?]</li>'."\n";
}
/**
* Returns HTML code for a column in edit mode.
*
* @param string The column name
* @param array The parameters
*
* @return string HTML code
*/
public function getColumnEditTag($column, $params = array())
{
// user defined parameters
$user_params = $this->getParameterValue('edit.fields.'.$column->getName().'.params');
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
$params = $user_params ? array_merge($params, $user_params) : $params;
if ($column->isComponent())
{
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
}
else if ($column->isPartial())
{
return "get_partial('".$column->getName()."', array('type' => 'edit', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
}
// default control name
$params = array_merge(array('control_name' => $this->getSingularName().'['.$column->getName().']'), $params);
// default parameter values
$type = $column->getCreoleType();
if ($type == CreoleTypes::DATE)
{
$params = array_merge(array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params);
}
else if ($type == CreoleTypes::TIMESTAMP)
{
$params = array_merge(array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'), $params);
}
// user sets a specific tag to use
if ($inputType = $this->getParameterValue('edit.fields.'.$column->getName().'.type'))
{
if ($inputType == 'plain')
{
return $this->getColumnListTag($column, $params);
}
else
{
return $this->getPHPObjectHelper($inputType, $column, $params);
}
}
// guess the best tag to use with column type
return parent::getCrudColumnEditTag($column, $params);
}
/**
* Returns all column categories.
*
* @param string The parameter name
*
* @return array The column categories
*/
public function getColumnCategories($paramName)
{
if (is_array($this->getParameterValue($paramName)))
{
$fields = $this->getParameterValue($paramName);
// do we have categories?
if (!isset($fields[0]))
{
return array_keys($fields);
}
}
return array('NONE');
}
/**
* Wraps content with a credential condition.
*
* @param string The content
* @param array The parameters
*
* @return string HTML code
*/
public function addCredentialCondition($content, $params = array())
{
if (isset($params['credentials']))
{
$credentials = str_replace("\n", ' ', var_export($params['credentials'], true));
return <<<EOF
[?php if (\$sf_user->hasCredential($credentials)): ?]
$content
[?php endif; ?]
EOF;
}
else
{
return $content;
}
}
/**
* Gets sfAdminColumn objects for a given category.
*
* @param string The parameter name
*
* @return array sfAdminColumn array
*/
public function getColumns($paramName, $category = 'NONE')
{
$phpNames = array();
// user has set a personnalized list of fields?
$fields = $this->getParameterValue($paramName);
if (is_array($fields))
{
// categories?
if (isset($fields[0]))
{
// simulate a default one
$fields = array('NONE' => $fields);
}
if (!$fields)
{
return array();
}
foreach ($fields[$category] as $field)
{
list($field, $flags) = $this->splitFlag($field);
$phpNames[] = $this->getAdminColumnForField($field, $flags);
}
}
else
{
// no, just return the full list of columns in table
return $this->getAllColumns();
}
return $phpNames;
}
/**
* Gets modifier flags from a column name.
*
* @param string The column name
*
* @return array An array of detected flags
*/
public function splitFlag($text)
{
$flags = array();
while (in_array($text[0], array('=', '-', '+', '_', '~')))
{
$flags[] = $text[0];
$text = substr($text, 1);
}
return array($text, $flags);
}
/**
* Gets a parameter value.
*
* @param string The key name
* @param mixed The default value
*
* @return mixed The parameter value
*/
public function getParameterValue($key, $default = null)
{
if (preg_match('/^([^\.]+)\.fields\.(.+)$/', $key, $matches))
{
return $this->getFieldParameterValue($matches[2], $matches[1], $default);
}
else
{
return $this->getValueFromKey($key, $default);
}
}
/**
* Gets a field parameter value.
*
* @param string The key name
* @param string The type (list, edit)
* @param mixed The default value
*
* @return mixed The parameter value
*/
protected function getFieldParameterValue($key, $type = '', $default = null)
{
$retval = $this->getValueFromKey($type.'.fields.'.$key, $default);
if ($retval !== null)
{
return $retval;
}
$retval = $this->getValueFromKey('fields.'.$key, $default);
if ($retval !== null)
{
return $retval;
}
if (preg_match('/\.name$/', $key))
{
// default field.name
return sfInflector::humanize(($pos = strpos($key, '.')) ? substr($key, 0, $pos) : $key);
}
else
{
return null;
}
}
/**
* Gets the value for a given key.
*
* @param string The key name
* @param mixed The default value
*
* @return mixed The key value
*/
protected function getValueFromKey($key, $default = null)
{
$ref =& $this->params;
$parts = explode('.', $key);
$count = count($parts);
for ($i = 0; $i < $count; $i++)
{
$partKey = $parts[$i];
if (!isset($ref[$partKey]))
{
return $default;
}
if ($count == $i + 1)
{
return $ref[$partKey];
}
else
{
$ref =& $ref[$partKey];
}
}
return $default;
}
/**
* Wraps a content for I18N.
*
* @param string The key name
* @param string The defaul value
*
* @return string HTML code
*/
public function getI18NString($key, $default = null, $withEcho = true)
{
$value = $this->escapeString($this->getParameterValue($key, $default));
// find %%xx%% strings
preg_match_all('/%%([^%]+)%%/', $value, $matches, PREG_PATTERN_ORDER);
$this->params['tmp']['display'] = array();
foreach ($matches[1] as $name)
{
$this->params['tmp']['display'][] = $name;
}
$vars = array();
foreach ($this->getColumns('tmp.display') as $column)
{
if ($column->isLink())
{
$vars[] = '\'%%'.$column->getName().'%%\' => link_to('.$this->getColumnListTag($column).', \''.$this->getModuleName().'/edit?'.$this->getPrimaryKeyUrlParams().')';
}
elseif ($column->isPartial())
{
$vars[] = '\'%%_'.$column->getName().'%%\' => '.$this->getColumnListTag($column);
}
else if ($column->isComponent())
{
$vars[] = '\'%%~'.$column->getName().'%%\' => '.$this->getColumnListTag($column);
}
else
{
$vars[] = '\'%%'.$column->getName().'%%\' => '.$this->getColumnListTag($column);
}
}
// strip all = signs
$value = preg_replace('/%%=([^%]+)%%/', '%%$1%%', $value);
$i18n = '__(\''.$value.'\', '."\n".'array('.implode(",\n", $vars).'))';
return $withEcho ? '[?php echo '.$i18n.' ?]' : $i18n;
}
/**
* Replaces constants in a string.
*
* @param string
*
* @return string
*/
public function replaceConstants($value)
{
// find %%xx%% strings
preg_match_all('/%%([^%]+)%%/', $value, $matches, PREG_PATTERN_ORDER);
$this->params['tmp']['display'] = array();
foreach ($matches[1] as $name)
{
$this->params['tmp']['display'][] = $name;
}
foreach ($this->getColumns('tmp.display') as $column)
{
$value = str_replace('%%'.$column->getName().'%%', '{'.$this->getColumnGetter($column, true, 'this->').'}', $value);
}
return $value;
}
/**
* Returns HTML code for a column in list mode.
*
* @param string The column name
* @param array The parameters
*
* @return string HTML code
*/
public function getColumnListTag($column, $params = array())
{
$user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
$params = $user_params ? array_merge($params, $user_params) : $params;
$type = $column->getCreoleType();
$columnGetter = $this->getColumnGetter($column, true);
if ($column->isComponent())
{
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
}
else if ($column->isPartial())
{
return "get_partial('".$column->getName()."', array('type' => 'list', '{$this->getSingularName()}' => \${$this->getSingularName()}))";
}
else if ($type == CreoleTypes::DATE || $type == CreoleTypes::TIMESTAMP)
{
$format = isset($params['date_format']) ? $params['date_format'] : ($type == CreoleTypes::DATE ? 'D' : 'f');
return "($columnGetter !== null && $columnGetter !== '') ? format_date($columnGetter, \"$format\") : ''";
}
elseif ($type == CreoleTypes::BOOLEAN)
{
return "$columnGetter ? image_tag(sfConfig::get('sf_admin_web_dir').'/images/tick.png') : '&nbsp;'";
}
else
{
return "$columnGetter";
}
}
/**
* Returns HTML code for a column in filter mode.
*
* @param string The column name
* @param array The parameters
*
* @return string HTML code
*/
public function getColumnFilterTag($column, $params = array())
{
$user_params = $this->getParameterValue('list.fields.'.$column->getName().'.params');
$user_params = is_array($user_params) ? $user_params : sfToolkit::stringToArray($user_params);
$params = $user_params ? array_merge($params, $user_params) : $params;
if ($column->isComponent())
{
return "get_component('".$this->getModuleName()."', '".$column->getName()."', array('type' => 'list'))";
}
else if ($column->isPartial())
{
return "get_partial('".$column->getName()."', array('type' => 'filter', 'filters' => \$filters))";
}
$type = $column->getCreoleType();
$default_value = "isset(\$filters['".$column->getName()."']) ? \$filters['".$column->getName()."'] : null";
$unquotedName = 'filters['.$column->getName().']';
$name = "'$unquotedName'";
if ($column->isForeignKey())
{
$params = $this->getObjectTagParams($params, array('include_blank' => true, 'related_class'=>$this->getRelatedClassName($column), 'text_method'=>'__toString', 'control_name'=>$unquotedName));
return "object_select_tag($default_value, null, $params)";
}
else if ($type == CreoleTypes::DATE)
{
// rich=false not yet implemented
$params = $this->getObjectTagParams($params, array('rich' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
return "input_date_range_tag($name, $default_value, $params)";
}
else if ($type == CreoleTypes::TIMESTAMP)
{
// rich=false not yet implemented
$params = $this->getObjectTagParams($params, array('rich' => true, 'withtime' => true, 'calendar_button_img' => sfConfig::get('sf_admin_web_dir').'/images/date.png'));
return "input_date_range_tag($name, $default_value, $params)";
}
else if ($type == CreoleTypes::BOOLEAN)
{
$defaultIncludeCustom = '__("yes or no")';
$option_params = $this->getObjectTagParams($params, array('include_custom' => $defaultIncludeCustom));
$params = $this->getObjectTagParams($params);
// little hack
$option_params = preg_replace("/'".preg_quote($defaultIncludeCustom)."'/", $defaultIncludeCustom, $option_params);
$options = "options_for_select(array(1 => __('yes'), 0 => __('no')), $default_value, $option_params)";
return "select_tag($name, $options, $params)";
}
else if ($type == CreoleTypes::CHAR || $type == CreoleTypes::VARCHAR || $type == CreoleTypes::TEXT || $type == CreoleTypes::LONGVARCHAR)
{
$size = ($column->getSize() < 15 ? $column->getSize() : 15);
$params = $this->getObjectTagParams($params, array('size' => $size));
return "input_tag($name, $default_value, $params)";
}
else if ($type == CreoleTypes::INTEGER || $type == CreoleTypes::TINYINT || $type == CreoleTypes::SMALLINT || $type == CreoleTypes::BIGINT)
{
$params = $this->getObjectTagParams($params, array('size' => 7));
return "input_tag($name, $default_value, $params)";
}
else if ($type == CreoleTypes::FLOAT || $type == CreoleTypes::DOUBLE || $type == CreoleTypes::DECIMAL || $type == CreoleTypes::NUMERIC || $type == CreoleTypes::REAL)
{
$params = $this->getObjectTagParams($params, array('size' => 7));
return "input_tag($name, $default_value, $params)";
}
else
{
$params = $this->getObjectTagParams($params, array('disabled' => true));
return "input_tag($name, $default_value, $params)";
}
}
/**
* Escapes a string.
*
* @param string
*
* @param string
*/
protected function escapeString($string)
{
return preg_replace('/\'/', '\\\'', $string);
}
}
/**
* Admin generator column.
*
* @package symfony
* @subpackage generator
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfAdminGenerator.class.php 5099 2007-09-15 06:44:50Z fabien $
*/
class sfAdminColumn
{
protected
$phpName = '',
$column = null,
$flags = array();
/**
* Constructor.
*
* @param string The column php name
* @param string The column name
* @param array The column flags
*/
public function __construct($phpName, $column = null, $flags = array())
{
$this->phpName = $phpName;
$this->column = $column;
$this->flags = (array) $flags;
}
/**
* Returns true if the column maps a database column.
*
* @return boolean true if the column maps a database column, false otherwise
*/
public function isReal()
{
return $this->column ? true : false;
}
/**
* Gets the name of the column.
*
* @return string The column name
*/
public function getName()
{
return sfInflector::underscore($this->phpName);
}
/**
* Returns true if the column is a partial.
*
* @return boolean true if the column is a partial, false otherwise
*/
public function isPartial()
{
return in_array('_', $this->flags) ? true : false;
}
/**
* Returns true if the column is a component.
*
* @return boolean true if the column is a component, false otherwise
*/
public function isComponent()
{
return in_array('~', $this->flags) ? true : false;
}
/**
* Returns true if the column has a link.
*
* @return boolean true if the column has a link, false otherwise
*/
public function isLink()
{
return (in_array('=', $this->flags) || $this->isPrimaryKey()) ? true : false;
}
/**
* Gets the php name of the column.
*
* @return string The php name
*/
public function getPhpName()
{
return $this->phpName;
}
// FIXME: those methods are only used in the propel admin generator
public function __call($name, $arguments)
{
return $this->column ? $this->column->$name() : null;
}
}

View File

@ -0,0 +1,448 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* CRUD generator.
*
* This class generates a basic CRUD module.
*
* @package symfony
* @subpackage generator
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfCrudGenerator.class.php 5099 2007-09-15 06:44:50Z fabien $
*/
abstract class sfCrudGenerator extends sfGenerator
{
protected
$singularName = '',
$pluralName = '',
$peerClassName = '',
$map = null,
$tableMap = null,
$primaryKey = array(),
$className = '',
$params = array();
/**
* Generates classes and templates in cache.
*
* @param array The parameters
*
* @return string The data to put in configuration cache
*/
public function generate($params = array())
{
$this->params = $params;
$required_parameters = array('model_class', 'moduleName');
foreach ($required_parameters as $entry)
{
if (!isset($this->params[$entry]))
{
$error = 'You must specify a "%s"';
$error = sprintf($error, $entry);
throw new sfParseException($error);
}
}
$modelClass = $this->params['model_class'];
if (!class_exists($modelClass))
{
$error = 'Unable to scaffold unexistant model "%s"';
$error = sprintf($error, $modelClass);
throw new sfInitializationException($error);
}
$this->setScaffoldingClassName($modelClass);
// generated module name
$this->setGeneratedModuleName('auto'.ucfirst($this->params['moduleName']));
$this->setModuleName($this->params['moduleName']);
// get some model metadata
$this->loadMapBuilderClasses();
// load all primary keys
$this->loadPrimaryKeys();
// theme exists?
$theme = isset($this->params['theme']) ? $this->params['theme'] : 'default';
$themeDir = sfLoader::getGeneratorTemplate($this->getGeneratorClass(), $theme, '');
if (!is_dir($themeDir))
{
$error = 'The theme "%s" does not exist.';
$error = sprintf($error, $theme);
throw new sfConfigurationException($error);
}
$this->setTheme($theme);
$templateFiles = sfFinder::type('file')->name('*.php')->relative()->in($themeDir.'/templates');
$configFiles = sfFinder::type('file')->name('*.yml')->relative()->in($themeDir.'/config');
$this->generatePhpFiles($this->generatedModuleName, $templateFiles, $configFiles);
// require generated action class
$data = "require_once(sfConfig::get('sf_module_cache_dir').'/".$this->generatedModuleName."/actions/actions.class.php');\n";
return $data;
}
/**
* Returns PHP code for primary keys parameters.
*
* @param integer The indentation value
*
* @return string The PHP code
*/
public function getRetrieveByPkParamsForAction($indent)
{
$params = array();
foreach ($this->getPrimaryKey() as $pk)
{
$params[] = "\$this->getRequestParameter('".sfInflector::underscore($pk->getPhpName())."')";
}
return implode(",\n".str_repeat(' ', max(0, $indent - strlen($this->singularName.$this->className))), $params);
}
/**
* Returns PHP code for getOrCreate() parameters.
*
* @return string The PHP code
*/
public function getMethodParamsForGetOrCreate()
{
$method_params = array();
foreach ($this->getPrimaryKey() as $pk)
{
$fieldName = sfInflector::underscore($pk->getPhpName());
$method_params[] = "\$$fieldName = '$fieldName'";
}
return implode(', ', $method_params);
}
/**
* Returns PHP code for getOrCreate() promary keys condition.
*
* @param boolean true if we pass the field name as an argument, false otherwise
*
* @return string The PHP code
*/
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."'");
}
return implode("\n || ", $test_pks);
}
/**
* Returns PHP code for primary keys parameters used in getOrCreate() method.
*
* @return string The PHP code
*/
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);
}
/**
* Gets the table map for the current model class.
*
* @return TableMap A TableMap instance
*/
public function getTableMap()
{
return $this->tableMap;
}
/**
* Sets the class name to use for scaffolding
*
* @param string class name
*/
protected function setScaffoldingClassName($className)
{
$this->singularName = sfInflector::underscore($className);
$this->pluralName = $this->singularName.'s';
$this->className = $className;
$this->peerClassName = $className.'Peer';
}
/**
* Gets the singular name for current scaffolding class.
*
* @return string
*/
public function getSingularName()
{
return $this->singularName;
}
/**
* Gets the plural name for current scaffolding class.
*
* @return string
*/
public function getPluralName()
{
return $this->pluralName;
}
/**
* Gets the class name for current scaffolding class.
*
* @return string
*/
public function getClassName()
{
return $this->className;
}
/**
* Gets the Peer class name.
*
* @return string
*/
public function getPeerClassName()
{
return $this->peerClassName;
}
/**
* Gets the primary key name.
*
* @return string
*/
public function getPrimaryKey()
{
return $this->primaryKey;
}
/**
* Gets the Map object.
*
* @return object
*/
public function getMap()
{
return $this->map;
}
/**
* Returns PHP code to add to a URL for primary keys.
*
* @param string The prefix value
*
* @return string PHP code
*/
public function getPrimaryKeyUrlParams($prefix = '')
{
$params = array();
foreach ($this->getPrimaryKey() as $pk)
{
$phpName = $pk->getPhpName();
$fieldName = sfInflector::underscore($phpName);
$params[] = "$fieldName='.".$this->getColumnGetter($pk, true, $prefix);
}
return implode(".'&", $params);
}
/**
* Gets PHP code for primary key condition.
*
* @param string The prefix value
*
* @return string PHP code
*/
public function getPrimaryKeyIsSet($prefix = '')
{
$params = array();
foreach ($this->getPrimaryKey() as $pk)
{
$params[] = $this->getColumnGetter($pk, true, $prefix);
}
return implode(' && ', $params);
}
/**
* Gets object tag parameters.
*
* @param array An array of parameters
* @param array An array of default parameters
*
* @return string PHP code
*/
protected function getObjectTagParams($params, $default_params = array())
{
return var_export(array_merge($default_params, $params), true);
}
/**
* Returns HTML code for a column in list mode.
*
* @param string The column name
* @param array The parameters
*
* @return string HTML code
*/
public function getColumnListTag($column, $params = array())
{
$type = $column->getCreoleType();
$columnGetter = $this->getColumnGetter($column, true);
if ($type == CreoleTypes::TIMESTAMP)
{
return "format_date($columnGetter, 'f')";
}
elseif ($type == CreoleTypes::DATE)
{
return "format_date($columnGetter, 'D')";
}
else
{
return "$columnGetter";
}
}
/**
* Returns HTML code for a column in edit mode.
*
* @param string The column name
* @param array The parameters
*
* @return string HTML code
*/
public function getCrudColumnEditTag($column, $params = array())
{
$type = $column->getCreoleType();
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 == CreoleTypes::DATE)
{
// rich=false not yet implemented
return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true));
}
else if ($type == CreoleTypes::TIMESTAMP)
{
// rich=false not yet implemented
return $this->getPHPObjectHelper('input_date_tag', $column, $params, array('rich' => true, 'withtime' => true));
}
else if ($type == CreoleTypes::BOOLEAN)
{
return $this->getPHPObjectHelper('checkbox_tag', $column, $params);
}
else if ($type == CreoleTypes::CHAR || $type == CreoleTypes::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 == CreoleTypes::INTEGER || $type == CreoleTypes::TINYINT || $type == CreoleTypes::SMALLINT || $type == CreoleTypes::BIGINT)
{
return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
}
else if ($type == CreoleTypes::FLOAT || $type == CreoleTypes::DOUBLE || $type == CreoleTypes::DECIMAL || $type == CreoleTypes::NUMERIC || $type == CreoleTypes::REAL)
{
return $this->getPHPObjectHelper('input_tag', $column, $params, array('size' => 7));
}
else if ($type == CreoleTypes::TEXT || $type == CreoleTypes::LONGVARCHAR)
{
return $this->getPHPObjectHelper('textarea_tag', $column, $params, array('size' => '30x3'));
}
else
{
return $this->getPHPObjectHelper('input_tag', $column, $params, array('disabled' => true));
}
}
/**
* Loads primary keys.
*
* This method is ORM dependant.
*
* @throws sfException
*/
abstract protected function loadPrimaryKeys();
/**
* Loads map builder classes.
*
* This method is ORM dependant.
*
* @throws sfException
*/
abstract protected function loadMapBuilderClasses();
/**
* Generates a PHP call to an object helper.
*
* This method is ORM dependant.
*
* @param string The helper name
* @param string The column name
* @param array An array of parameters
* @param array An array of local parameters
*
* @return string PHP code
*/
abstract function getPHPObjectHelper($helperName, $column, $params, $localParams = array());
/**
* Returns the getter either non-developped: 'getFoo' or developped: '$class->getFoo()'.
*
* This method is ORM dependant.
*
* @param string The column name
* @param boolean true if you want developped method names, false otherwise
* @param string The prefix value
*
* @return string PHP code
*/
abstract function getColumnGetter($column, $developed = false , $prefix = '');
/*
* Gets the PHP name of the related class name.
*
* Used for foreign keys only; this method should be removed when we use sfAdminColumn instead.
*
* This method is ORM dependant.
*
* @param string The column name
*
* @return string The PHP name of the related class name
*/
abstract function getRelatedClassName($column);
}

View File

@ -0,0 +1,234 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfGenerator is the abstract base class for all generators.
*
* @package symfony
* @subpackage generator
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfGenerator.class.php 3513 2007-02-19 13:42:16Z fabien $
*/
abstract class sfGenerator
{
protected
$generatorClass = '',
$generatorManager = null,
$generatedModuleName = '',
$theme = 'default',
$moduleName = '';
/**
* Initializes the current sfGenerator instance.
*
* @param sfGeneratorManager A sfGeneratorManager instance
*/
public function initialize($generatorManager)
{
$this->generatorManager = $generatorManager;
}
/**
* Generates classes and templates.
*
* @param array An array of parameters
*
* @return string The cache for the configuration file
*/
abstract public function generate($params = array());
/**
* Generates PHP files for a given module name.
*
* @param string The name of module name to generate
* @param array A list of template files to generate
* @param array A list of configuration files to generate
*/
protected function generatePhpFiles($generatedModuleName, $templateFiles = array(), $configFiles = array())
{
// eval actions file
$retval = $this->evalTemplate('actions/actions.class.php');
// save actions class
$this->getGeneratorManager()->getCache()->set('actions.class.php', $generatedModuleName.DIRECTORY_SEPARATOR.'actions', $retval);
// generate template files
foreach ($templateFiles as $template)
{
// eval template file
$retval = $this->evalTemplate('templates/'.$template);
// save template file
$this->getGeneratorManager()->getCache()->set($template, $generatedModuleName.DIRECTORY_SEPARATOR.'templates', $retval);
}
// generate config files
foreach ($configFiles as $config)
{
// eval config file
$retval = $this->evalTemplate('config/'.$config);
// save config file
$this->getGeneratorManager()->getCache()->set($config, $generatedModuleName.DIRECTORY_SEPARATOR.'config', $retval);
}
}
/**
* Evaluates a template file.
*
* @param string The template file path
*
* @return string The evaluated template
*/
protected function evalTemplate($templateFile)
{
$templateFile = sfLoader::getGeneratorTemplate($this->getGeneratorClass(), $this->getTheme(), $templateFile);
// eval template file
ob_start();
require($templateFile);
$content = ob_get_clean();
// replace [?php and ?]
$content = $this->replacePhpMarks($content);
$retval = "<?php\n".
"// auto-generated by ".$this->getGeneratorClass()."\n".
"// date: %s\n?>\n%s";
$retval = sprintf($retval, date('Y/m/d H:i:s'), $content);
return $retval;
}
/**
* Replaces PHP marks by <?php ?>.
*
* @param string The PHP code
*
* @return string The converted PHP code
*/
protected function replacePhpMarks($text)
{
// replace [?php and ?]
return str_replace(array('[?php', '[?=', '?]'), array('<?php', '<?php echo', '?>'), $text);
}
/**
* Gets the generator class.
*
* @return string The generator class
*/
public function getGeneratorClass()
{
return $this->generatorClass;
}
/**
* Sets the generator class.
*
* @param string The generator class
*/
public function setGeneratorClass($generator_class)
{
$this->generatorClass = $generator_class;
}
/**
* Gets the sfGeneratorManager instance.
*
* @return string The sfGeneratorManager instance
*/
protected function getGeneratorManager()
{
return $this->generatorManager;
}
/**
* Gets the module name of the generated module.
*
* @return string The module name
*/
public function getGeneratedModuleName()
{
return $this->generatedModuleName;
}
/**
* Sets the module name of the generated module.
*
* @param string The module name
*/
public function setGeneratedModuleName($module_name)
{
$this->generatedModuleName = $module_name;
}
/**
* Gets the module name.
*
* @return string The module name
*/
public function getModuleName()
{
return $this->moduleName;
}
/**
* Sets the module name.
*
* @param string The module name
*/
public function setModuleName($module_name)
{
$this->moduleName = $module_name;
}
/**
* Gets the theme name.
*
* @return string The theme name
*/
public function getTheme()
{
return $this->theme;
}
/**
* Sets the theme name.
*
* @param string The theme name
*/
public function setTheme($theme)
{
$this->theme = $theme;
}
/**
* Calls methods defined via the sfMixer class.
*
* @param string The method name
* @param array The method arguments
*
* @return mixed The returned value of the called method
*
* @see sfMixer
*/
public function __call($method, $arguments)
{
if (!$callable = sfMixer::getCallable('sfGenerator:'.$method))
{
throw new sfException(sprintf('Call to undefined method sfGenerator::%s', $method));
}
array_unshift($arguments, $this);
return call_user_func_array($callable, $arguments);
}
}

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfGeneratorManager helps generate classes, views and templates for scaffolding, admin interface, ...
*
* @package symfony
* @subpackage generator
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfGeneratorManager.class.php 3302 2007-01-18 13:42:46Z fabien $
*/
class sfGeneratorManager
{
protected $cache = null;
/**
* Initializes the sfGeneratorManager instance.
*/
public function initialize()
{
// create cache instance
$this->cache = new sfFileCache(sfConfig::get('sf_module_cache_dir'));
$this->cache->setSuffix('');
}
/**
* Returns the current sfCache implementation instance.
*
* @return sfCache A sfCache implementation instance
*/
public function getCache()
{
return $this->cache;
}
/**
* Generates classes and templates for a given generator class.
*
* @param string The generator class name
* @param array An array of parameters
*
* @return string The cache for the configuration file
*/
public function generate($generator_class, $param)
{
$generator = new $generator_class();
$generator->initialize($this);
$data = $generator->generate($param);
return $data;
}
}