Permalink
Browse files

added culture fixes

removed useless i18n object methods
fixed some phpdoc
changed join from LEFT_JOIN to INNER_JOIN
  • Loading branch information...
elvenpath committed Sep 3, 2012
1 parent fa0d309 commit 4c0222c7509916623b10ae85a358be51e15a1550
View
@@ -23,7 +23,7 @@ class I18nWithSlugBehavior extends Behavior
'culture_alias' => '',
'slug_column' => 'slug',
'slug_pattern' => '',
- 'replace_pattern' => '/\W+/', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
+ 'replace_pattern' => '/[^\\pL\\d]+/u', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
'replacement' => '-',
'separator' => '-',
'permanent' => 'false',
@@ -66,11 +66,24 @@ public function modifyTable()
// tell the parent table that it has a descendant
$parentBehavior = new I18nWithSlugChildBehavior();
$parentBehavior->setName('i18n_with_slug_child');
- $parentBehavior->setParameters(array(
- 'slug_column' => $this->getParameter('slug_column'),
- 'culture_column' => $this->getParameter('culture_column'),
- 'default_culture' => $this->getDefaultCulture()
- ));
+
+ $parent_columns = array();
+ foreach ($this->getTable()->getColumns() as $column)
+ {
+ /** @var $column Column */
+ if ($column->isForeignKey() or $column->isPrimaryKey())
+ {
+ continue;
+ }
+
+ $parent_columns[] = $column;
+ }
+ $parentBehavior->setParameters(array_merge(
+ $this->getParameters(),
+ array(
+ 'caller_class_name' => $this->getTable()->getPhpName(),
+ 'parent_columns' => $parent_columns
+ )));
$this->i18nTable->addBehavior($parentBehavior);
$parentBehavior->getTableModifier()->modifyTable();
$parentBehavior->setTableModified(true);
@@ -36,25 +36,6 @@ public function postDelete($builder)
}
}
- /**
- * Add code in ObjectBuilder::preSave
- *
- * @param $builder ObjectBuilder
- * @return string The code to put at the hook
- */
- public function preSave($builder)
- {
- $const = $builder->getColumnConstant($this->behavior->getI18nTable()->getColumn($this->behavior->getParameter('slug_column')));
-
- return $this->behavior->renderTemplate('objectPreSave', array(
- 'defaultCulture' => $this->behavior->getDefaultCulture(),
- 'const' => $const,
- 'columnGetter' => $this->behavior->getColumnGetter(),
- 'columnSetter' => $this->behavior->getColumnSetter(),
- 'permanent' => $this->behavior->getParameter('permanent')
- ));
- }
-
/**
* @param $builder
*
@@ -63,7 +44,6 @@ public function preSave($builder)
public function objectAttributes($builder)
{
return $this->behavior->renderTemplate('objectAttributes', array(
- 'defaultCulture' => $this->behavior->getDefaultCulture(),
'objectClassname' => $builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname(),
));
}
@@ -105,7 +85,6 @@ public function objectMethods($builder)
$script .= $this->addSlugSetter();
$script .= $this->addSlugGetter();
- $script .= $this->addCreateSlug();
return $script;
}
@@ -117,7 +96,6 @@ protected function addSetCulture()
{
return $this->behavior->renderTemplate('objectSetCulture', array(
'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
- 'defaultCulture' => $this->behavior->getDefaultCulture(),
));
}
@@ -181,7 +159,6 @@ protected function addRemoveTranslation()
$fk = $this->behavior->getI18nForeignKey();
return $this->behavior->renderTemplate('objectRemoveTranslation', array(
'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
- 'defaultCulture' => $this->behavior->getDefaultCulture(),
'i18nQueryName' => $this->builder->getNewStubQueryBuilder($i18nTable)->getClassname(),
'i18nCollection' => $this->builder->getRefFKCollVarName($fk),
'cultureColumnName' => $this->behavior->getCultureColumn()->getPhpName(),
@@ -301,26 +278,4 @@ protected function addSlugGetter()
'slugColumnName' => $this->behavior->getSlugColumn()->getPhpName(),
));
}
-
- /**
- * @return string
- */
- protected function addCreateSlug()
- {
- $i18nTable = $this->behavior->getI18nTable();
-//var_dump($i18nTable->getName());
-//var_dump($i18nTable->hasBehavior('soft_delete'));
- return $this->behavior->renderTemplate('objectCreateSlug', array(
- 'replacement' => $this->behavior->getParameter('replacement'),
- 'pattern' => $this->behavior->getParameter('slug_pattern'),
- 'replace_pattern' => $this->behavior->getParameter('replace_pattern'),
- 'size' => $this->behavior->getI18nTable()->getColumn($this->behavior->getParameter('slug_column'))->getSize(),
- 'separator' => $this->behavior->getParameter('separator'),
- 'i18nQueryName' => $this->builder->getNewStubQueryBuilder($i18nTable)->getClassname(),
- 'softDeleteBehaviour' => $i18nTable->hasBehavior('soft_delete'),
- 'cultureColumnName' => $this->behavior->getCultureColumn()->getPhpName(),
- 'defaultCulture' => $this->behavior->getDefaultCulture(),
- ));
-
- }
}
@@ -1,5 +1,6 @@
<?php
require_once dirname(__FILE__) . '/I18nWithSlugChildBehaviorObjectBuilderModifier.php';
+require_once dirname(__FILE__) . '/I18nWithSlugChildBehaviorQueryBuilderModifier.php';
class I18nWithSlugChildBehavior extends Behavior
{
@@ -74,4 +75,25 @@ public function getDefaultCulture()
{
return $this->getParameter('default_culture');
}
+
+
+ /**
+ * Get the getter of the column of the behavior
+ *
+ * @return string The related getter, e.g. 'getSlug'
+ */
+ public function getColumnGetter()
+ {
+ return 'get' . $this->table->getColumn($this->getParameter('slug_column'))->getPhpName();
+ }
+
+ /**
+ * Get the setter of the column of the behavior
+ *
+ * @return string The related setter, e.g. 'setSlug'
+ */
+ public function getColumnSetter()
+ {
+ return 'set' . $this->table->getColumn($this->getParameter('slug_column'))->getPhpName();
+ }
}
@@ -28,17 +28,39 @@ public function objectMethods($builder)
$script .= $this->addSlugGetter();
}
+ $script .= $this->addCreateSlug();
+ $script .= $this->addParentGetters();
+
return $script;
}
+ /**
+ * Add code in ObjectBuilder::preSave
+ *
+ * @param $builder ObjectBuilder
+ * @return string The code to put at the hook
+ */
+ public function preSave($builder)
+ {
+ $const = $builder->getColumnConstant($this->behavior->getTable()->getColumn($this->behavior->getParameter('slug_column')));
+
+ return $this->behavior->renderTemplate('objectChildPreSave', array(
+ 'defaultCulture' => $this->behavior->getDefaultCulture(),
+ 'const' => $const,
+ 'columnGetter' => $this->behavior->getColumnGetter(),
+ 'columnSetter' => $this->behavior->getColumnSetter(),
+ 'permanent' => $this->behavior->getParameter('permanent')
+ ));
+ }
+
/**
* @return string
*/
protected function addSlugSetter()
{
return $this->behavior->renderTemplate('objectChildSlugSetter', array(
'slugColumnName' => $this->behavior->getSlugColumn()->getPhpName(),
- 'objectClassname' => $this->builder->getNewStubObjectBuilder($this->behavior->getTable())->getClassname(),
+ 'objectClassname' => $this->behavior->getTable()->getPhpName()
));
}
@@ -51,4 +73,36 @@ protected function addSlugGetter()
'slugColumnName' => $this->behavior->getSlugColumn()->getPhpName(),
));
}
+
+ /**
+ * @return string
+ */
+ protected function addCreateSlug()
+ {
+ $i18nTable = $this->behavior->getTable();
+
+ return $this->behavior->renderTemplate('objectChildCreateSlug', array(
+ 'replacement' => $this->behavior->getParameter('replacement'),
+ 'pattern' => $this->behavior->getParameter('slug_pattern'),
+ 'replace_pattern' => $this->behavior->getParameter('replace_pattern'),
+ 'size' => $this->behavior->getTable()->getColumn($this->behavior->getParameter('slug_column'))->getSize(),
+ 'separator' => $this->behavior->getParameter('separator'),
+ 'i18nQueryName' => $this->builder->getNewStubQueryBuilder($i18nTable)->getClassname(),
+ 'softDeleteBehaviour' => $i18nTable->hasBehavior('soft_delete'),
+ 'cultureColumnName' => $this->behavior->getCultureColumn()->getPhpName(),
+ 'defaultCulture' => $this->behavior->getDefaultCulture(),
+ ));
+ }
+
+ /**
+ * @return string
+ */
+ protected function addParentGetters()
+ {
+ return $this->behavior->renderTemplate('objectChildAddParentGetters', array(
+ 'columns' => $this->behavior->getParameter('parent_columns'),
+ 'callerClassName' => $this->behavior->getParameter('caller_class_name'),
+ 'table' => $this->behavior->getTable()
+ ));
+ }
}
@@ -3,7 +3,7 @@
* Current culture
* @var string
*/
-protected $currentCulture = '<?php echo $defaultCulture?>';
+protected $currentCulture = null;
/**
* Current translation objects
@@ -0,0 +1,17 @@
+<?php /** @var $columns Column[] */ ?>
+<?php /** @var $table Table */ ?>
+<?php foreach ($columns as $column):?>
+<?php if (!$table->hasColumn($column->getName())): ?>
+
+/**
+* @see <?php echo $callerClassName ?>::get<?php echo $column->getPhpName() ?>
+*
+* @return string
+*/
+protected function get<?php echo $column->getPhpName() ?>()
+{
+return $this->get<?php echo $callerClassName ?>()->get<?php echo $column->getPhpName() ?>();
+}
+
+ <?php endif ?>
+<?php endforeach ?>
@@ -3,11 +3,11 @@
*
* @return string The object slug
*/
-protected function createSlug($culture = '<?php $defaultCulture ?>')
+protected function createSlug()
{
$slug = $this->createRawSlug();
$slug = $this->limitSlugSize($slug);
- $slug = $this->makeSlugUnique($slug, $culture);
+ $slug = $this->makeSlugUnique($slug);
return $slug;
}
@@ -19,19 +19,21 @@ protected function createSlug($culture = '<?php $defaultCulture ?>')
*/
protected function createRawSlug()
{
- <?php if ($pattern):?>
- <?php echo "return '" . str_replace(array('{', '}'), array('\' . $this->cleanupSlugPart($this->get', '()) . \''), $pattern). "';";?>
- <?php else:?>
- return $this->cleanupSlugPart($this->__toString());
- <?php endif ?>
+<?php if ($pattern):?>
+<?php echo "return '" . str_replace(array('{', '}'), array('\' . $this->cleanupSlugPart($this->get', '()) . \''), $pattern). "';";?>
+<?php else:?>
+ return $this->cleanupSlugPart($this->__toString());
+<?php endif ?>
+
}
/**
* Cleanup a string to make a slug of it
* Removes special characters, replaces blanks with a separator, and trim it
*
-* @param string $text the text to slugify
-* @param string $separator the separator used by slug
+* @param $slug
+* @param string $replacement
+*
* @return string the slugified text
*/
protected static function cleanupSlugPart($slug, $replacement = '<?php echo $replacement?>')
@@ -56,7 +58,7 @@ protected static function cleanupSlugPart($slug, $replacement = '<?php echo $rep
$slug = str_replace(array('\'', '`', '^'), '', $slug);
// replace non letter or digits with separator
- $slug = preg_replace('<?php echo $replace_pattern?>>', $replacement, $slug);
+ $slug = preg_replace('<?php echo $replace_pattern?>', $replacement, $slug);
// trim
$slug = trim($slug, $replacement);
@@ -72,8 +74,10 @@ protected static function cleanupSlugPart($slug, $replacement = '<?php echo $rep
/**
* Make sure the slug is short enough to accomodate the column size
*
-* @param string $slug the slug to check
-* @return string the truncated slug
+* @param string $slug the slug to check
+* @param int $incrementReservedSpace
+*
+* @return string the truncated slug
*/
protected static function limitSlugSize($slug, $incrementReservedSpace = 3)
{
@@ -89,27 +93,33 @@ protected static function limitSlugSize($slug, $incrementReservedSpace = 3)
/**
* Get the slug, ensuring its uniqueness
*
-* @param string $slug the slug to check
-* @param string $separator the separator used by slug
-* @return string the unique slug
+* @param string $slug the slug to check
+* @param string $separator the separator used by slug
+* @param int $increment int
+*
+* @return string the unique slug
*/
-protected function makeSlugUnique($slug, $culture = '<?php echo $defaultCulture ?>', $separator = '<?php echo $separator?>', $increment = 0)
+protected function makeSlugUnique($slug, $separator = '<?php echo $separator?>', $increment = 0)
{
$slug2 = empty($increment) ? $slug : $slug . $separator . $increment;
$slugAlreadyExists = <?php echo $i18nQueryName?>::create()
- ->filterBy<?php echo $cultureColumnName?>($culture)
- ->filterBySlug($slug2)->prune($this)
+ ->filterBySlug($slug2, $this->get<?php echo $cultureColumnName?>())
+ ->_if(!$this->isNew())
+ ->filterById($this->getId(), Criteria::NOT_EQUAL)
+ ->_endif()
<?php if ($softDeleteBehaviour):?>
->includeDeleted() // watch out: some of the columns may be hidden by the soft_delete behavior
<?php endif ?>
->count();
if ($slugAlreadyExists)
{
- return $this->makeSlugUnique($slug, $culture, $separator, ++$increment);
+ return $this->makeSlugUnique($slug, $separator, ++$increment);
}
else
{
return $slug2;
}
-}
+}
+
+
@@ -2,7 +2,7 @@
if ($this->isColumnModified(<?php echo $const ?>) && $this-><?php echo $columnGetter?>())
{
- $this-><?php echo $columnSetter?>($this->makeSlugUnique($this-><?php echo $columnGetter?>(), '<?php echo $defaultCulture?>'));
+ $this-><?php echo $columnSetter?>($this->makeSlugUnique($this-><?php echo $columnGetter?>(), $this->getCulture()));
<?php if (permanent == 'true'):?>
}
elseif (!$this-><?php echo $columnGetter?>())
@@ -13,6 +13,6 @@
}
else
{
- $this-><?php echo $columnSetter?>($this->createSlug());
+ $this-><?php echo $columnSetter?>($this->createSlug($this->getCulture()));
}
<?php endif ?>
@@ -1,3 +1,4 @@
+
/**
* Wrap the getter for slug
*
@@ -7,3 +8,4 @@ public function getSlug()
{
return $this->get<?php echo $slugColumnName?>();
}
+
Oops, something went wrong.

0 comments on commit 4c0222c

Please sign in to comment.