Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj authored and staabm committed May 16, 2013
1 parent b6ff130 commit bfad97e
Show file tree
Hide file tree
Showing 14 changed files with 985 additions and 293 deletions.
132 changes: 125 additions & 7 deletions src/Propel/Generator/Behavior/Sortable/SortableBehavior.php
Expand Up @@ -25,7 +25,7 @@ class SortableBehavior extends Behavior
protected $parameters = array(
'rank_column' => 'sortable_rank',
'use_scope' => 'false',
'scope_column' => 'sortable_scope',
'scope_column' => '',
);

protected $objectBuilderModifier;
Expand All @@ -46,12 +46,21 @@ public function modifyTable()
));
}

if ('true' === $this->getParameter('use_scope')
&& !$table->hasColumn($this->getParameter('scope_column'))) {
$table->addColumn(array(
'name' => $this->getParameter('scope_column'),
'type' => 'INTEGER'
));
if ($this->useScope()) {
if (!$this->hasMultipleScopes() && !$table->hasColumn($this->getParameter('scope_column'))) {
$table->addColumn(array(
'name' => $this->getParameter('scope_column'),
'type' => 'INTEGER'
));
}

$scopes = $this->getScopes();
if (0 === count($scopes)) {
throw new \InvalidArgumentException(sprintf(
'The sortable behavior in `%s` needs a `scope_column` parameter.',
$this->getTable()->getName()
));
}
}
}

Expand Down Expand Up @@ -86,4 +95,113 @@ public function useScope()
{
return 'true' === $this->getParameter('use_scope');
}

/**
* Generates the method argument signature, the appropriate phpDoc for @params,
* the scope builder php code and the scope variable builder php code/
*
* @return array ($methodSignature, $paramsDoc, $scopeBuilder, $buildScopeVars)
*/
public function generateScopePhp()
{

$methodSignature = '';
$paramsDoc = '';
$buildScope = '';
$buildScopeVars = '';

if ($this->hasMultipleScopes()) {

$methodSignature = array();
$buildScope = array();
$paramsDoc = array();

foreach ($this->getScopes() as $idx => $scope) {

$column = $this->table->getColumn($scope);
$param = '$scope'.$column->getPhpName();

$buildScope[] = " \$scope[] = $param;\n";
$buildScopeVars[] = " $param = \$scope[$idx];\n";
$paramsDoc[] = " * @param ".$column->getPhpType()." $param Scope value for column `".$column->getPhpName()."`";

if (!$column->isNotNull()) {
$param .= ' = null';
}
$methodSignature[] = $param;
}

$methodSignature = implode(', ', $methodSignature);
$paramsDoc = implode("\n", $paramsDoc);
$buildScope = "\n".implode('', $buildScope)."\n";
$buildScopeVars = "\n".implode('', $buildScopeVars)."\n";

} else if ($this->useScope()){
$methodSignature = '$scope';
if ($column = $this->table->getColumn($this->getParameter('scope_column'))) {
if (!$column->isNotNull()) {
$methodSignature .= ' = null';
}
$paramsDoc .= ' * @param '.$column->getPhpType().' $scope Scope to determine which objects node to return';
}
}

return array($methodSignature, $paramsDoc, $buildScope, $buildScopeVars);
}

/**
* Returns the getter method name.
*
* @param string $name
* @return string
*/
public function getColumnGetter($name)
{
return 'get' . $this->getTable()->getColumn($name)->getPhpName();
}

/**
* Returns the setter method name.
*
* @param string $name
* @return string
*/
public function getColumnSetter($name)
{
return 'set' . $this->getTable()->getColumn($name)->getPhpName();
}

/**
* {@inheritdoc}
*/
public function addParameter(array $parameter)
{
if ('scope_column' === $parameter['name']) {
$this->parameters['scope_column'] .= ($this->parameters['scope_column'] ? ',' : '') . $parameter['value'];
} else {
parent::addParameter($parameter);
}
}

/**
* Returns all scope columns as array.
*
* @return string[]
*/
public function getScopes()
{
return $this->getParameter('scope_column')
? explode(',', str_replace(' ', '', trim($this->getParameter('scope_column'))))
: array();
}

/**
* Returns true if the behavior has multiple scope columns.
*
* @return bool
*/
public function hasMultipleScopes()
{
return count($this->getScopes()) > 1;
}
}

0 comments on commit bfad97e

Please sign in to comment.