Permalink
Browse files

added disabled option;

added methods getI18nModel/doSelectJoinI18n/doSelectWithI18n to peer;
fixed  makeSlugUnique method for the translation class;
added filterBySlug/findOneBySlug methods to the translation class;
added getLocale/setLocale aliases for object;
added phpdoc;
  • Loading branch information...
1 parent 0e25af2 commit c8e459daa92a6997d684850638f3c0649a08a15d @elvenpath committed Aug 13, 2012
View
129 I18nWithSlugBehavior.php
@@ -15,20 +15,23 @@ class I18nWithSlugBehavior extends Behavior
// default parameters value
protected $parameters = array(
- 'i18n_table' => '%TABLE%_i18n',
- 'i18n_phpname' => '%PHPNAME%I18n',
- 'i18n_columns' => '',
+ 'i18n_table' => '%TABLE%_i18n',
+ 'i18n_phpname' => '%PHPNAME%I18n',
+ 'i18n_columns' => '',
'culture_column' => 'culture',
'default_culture' => null,
'culture_alias' => '',
- 'slug_column' => 'slug',
- 'slug_pattern' => '',
- 'replace_pattern' => '/\W+/', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
- 'replacement' => '-',
- 'separator' => '-',
- 'permanent' => 'false'
+ 'slug_column' => 'slug',
+ 'slug_pattern' => '',
+ 'replace_pattern' => '/\W+/', // Tip: use '/[^\\pL\\d]+/u' instead if you're in PHP5.3
+ 'replacement' => '-',
+ 'separator' => '-',
+ 'permanent' => 'false',
+ 'disabled' => false
);
+ protected $buildProperties = null;
+
protected $tableModificationOrder = 70;
protected
@@ -60,16 +63,17 @@ public function modifyTable()
$this->addSlugColumnToI18n();
$this->moveI18nColumns();
-
// 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')));
+ $parentBehavior->setParameters(array(
+ 'slug_column' => $this->getParameter('slug_column'),
+ 'culture_column' => $this->getParameter('culture_column'),
+ 'default_culture' => $this->getDefaultCulture
+ ));
$this->i18nTable->addBehavior($parentBehavior);
$parentBehavior->getTableModifier()->modifyTable();
$parentBehavior->setTableModified(true);
-
-
}
protected function addI18nTable()
@@ -198,25 +202,35 @@ protected function addSlugColumnToI18n()
$unique->addColumn($this->i18nTable->getColumn($this->getParameter('culture_column')));
$this->i18nTable->addUnique($unique);
}
-
-
}
+ /**
+ * @return string
+ */
protected function getI18nTableName()
{
return $this->replaceTokens($this->getParameter('i18n_table'));
}
+ /**
+ * @return string-
+ */
protected function getI18nTablePhpName()
{
return $this->replaceTokens($this->getParameter('i18n_phpname'));
}
+ /**
+ * @return string
+ */
protected function getCultureColumnName()
{
return $this->replaceTokens($this->getParameter('culture_column'));
}
+ /**
+ * @return array
+ */
protected function getI18nColumnNamesFromConfig()
{
$columnNames = explode(',', $this->getParameter('i18n_columns'));
@@ -236,6 +250,9 @@ protected function getI18nColumnNamesFromConfig()
return $columnNames;
}
+ /**
+ * @return null|string
+ */
public function getDefaultCulture()
{
if (!$defaultCulture = $this->getParameter('default_culture'))
@@ -245,11 +262,19 @@ public function getDefaultCulture()
return $defaultCulture;
}
+ /**
+ * Returns the current table's i18n translation table.
+ *
+ * @return Table
+ */
public function getI18nTable()
{
return $this->i18nTable;
}
+ /**
+ * @return ForeignKey
+ */
public function getI18nForeignKey()
{
foreach ($this->i18nTable->getForeignKeys() as $fk)
@@ -261,16 +286,25 @@ public function getI18nForeignKey()
}
}
+ /**
+ * @return Column
+ */
public function getCultureColumn()
{
return $this->getI18nTable()->getColumn($this->getCultureColumnName());
}
+ /**
+ * @return Column
+ */
public function getSlugColumn()
{
return $this->getI18nTable()->getColumn($this->getParameter('slug_column'));
}
+ /**
+ * @return array|Column[]
+ */
public function getI18nColumns()
{
$columns = array();
@@ -300,6 +334,11 @@ public function getI18nColumns()
return $columns;
}
+ /**
+ * @param $string
+ *
+ * @return string
+ */
public function replaceTokens($string)
{
$table = $this->getTable();
@@ -309,6 +348,9 @@ public function replaceTokens($string)
));
}
+ /**
+ * @return I18nWithSlugBehaviorObjectBuilderModifier
+ */
public function getObjectBuilderModifier()
{
if (is_null($this->objectBuilderModifier))
@@ -318,6 +360,9 @@ public function getObjectBuilderModifier()
return $this->objectBuilderModifier;
}
+ /**
+ * @return I18nWithSlugBehaviorQueryBuilderModifier
+ */
public function getQueryBuilderModifier()
{
if (is_null($this->queryBuilderModifier))
@@ -327,6 +372,9 @@ public function getQueryBuilderModifier()
return $this->queryBuilderModifier;
}
+ /**
+ * @return I18nWithSlugBehaviorPeerBuilderModifier
+ */
public function getPeerBuilderModifier()
{
if (is_null($this->peerBuilderModifier))
@@ -355,4 +403,55 @@ public function getColumnSetter()
{
return 'set' . $this->i18nTable->getColumn($this->getParameter('slug_column'))->getPhpName();
}
+
+
+ /**
+ * Returns a build property from propel.ini.
+ *
+ * @param string $name
+ *
+ * @return string
+ */
+ public function getBuildProperty($name)
+ {
+ if (null === $this->buildProperties)
+ {
+ $this->buildProperties = new Properties();
+ $this->buildProperties->load(new PhingFile(sfConfig::get('sf_config_dir') . '/propel.ini'));
+ }
+
+ return $this->buildProperties->getProperty($name);
+ }
+
+ /**
+ * Returns true if the current behavior has been disabled.
+ *
+ * @return boolean
+ */
+ protected function isDisabled()
+ {
+ return isset($this->parameters['disabled']) && 'true' == $this->getParameter('disabled');
+ }
+
+ /**
+ * Returns the column on the current model referenced by the translation model.
+ *
+ * @return Column
+ */
+ public function getLocalColumn()
+ {
+ $columns = $this->getI18nForeignKey()->getForeignColumns();
+ return $this->getTable()->getColumn($columns[0]);
+ }
+
+ /**
+ * Returns the column on the translation table the references the current model.
+ *
+ * @return Column
+ */
+ public function getForeignColumn()
+ {
+ $columns = $this->getI18nForeignKey()->getLocalColumns();
+ return $this->getI18nTable()->getColumn($columns[0]);
+ }
}
View
113 I18nWithSlugBehaviorObjectBuilderModifier.php
@@ -10,13 +10,21 @@ class I18nWithSlugBehaviorObjectBuilderModifier
{
protected $behavior, $table, $builder;
- public function __construct(I18nWithSlugBehavior $behavior)
+ /**
+ * @param I18nWithSlugBehavior $behavior
+ */
+ public function __construct(I18nWithSlugBehavior $behavior)
{
$this->behavior = $behavior;
$this->table = $behavior->getTable();
}
- public function postDelete($builder)
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function postDelete($builder)
{
$this->builder = $builder;
if (!$builder->getPlatform()->supportsNativeDeleteTrigger() && !$builder->getBuildProperty('emulateForeignKeyConstraints')) {
@@ -47,22 +55,37 @@ public function preSave($builder)
));
}
- public function objectAttributes($builder)
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function objectAttributes($builder)
{
return $this->behavior->renderTemplate('objectAttributes', array(
'defaultCulture' => $this->behavior->getDefaultCulture(),
'objectClassname' => $builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname(),
));
}
- public function objectClearReferences($builder)
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function objectClearReferences($builder)
{
return $this->behavior->renderTemplate('objectClearReferences', array(
'defaultCulture' => $this->behavior->getDefaultCulture(),
));
}
- public function objectMethods($builder)
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function objectMethods($builder)
{
$this->builder = $builder;
$script = '';
@@ -87,20 +110,31 @@ public function objectMethods($builder)
return $script;
}
- protected function addSetCulture()
+ /**
+ * @return string
+ */
+ protected function addSetCulture()
{
return $this->behavior->renderTemplate('objectSetCulture', array(
'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
'defaultCulture' => $this->behavior->getDefaultCulture(),
));
}
- protected function addGetCulture()
+ /**
+ * @return string
+ */
+ protected function addGetCulture()
{
return $this->behavior->renderTemplate('objectGetCulture');
}
- protected function addSetCultureAlias($alias)
+ /**
+ * @param $alias
+ *
+ * @return string
+ */
+ protected function addSetCultureAlias($alias)
{
return $this->behavior->renderTemplate('objectSetCultureAlias', array(
'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
@@ -109,14 +143,22 @@ protected function addSetCultureAlias($alias)
));
}
- protected function addGetCultureAlias($alias)
+ /**
+ * @param $alias
+ *
+ * @return string
+ */
+ protected function addGetCultureAlias($alias)
{
return $this->behavior->renderTemplate('objectGetCultureAlias', array(
'alias' => ucfirst($alias),
));
}
- protected function addGetTranslation()
+ /**
+ * @return string
+ */
+ protected function addGetTranslation()
{
$i18nTable = $this->behavior->getI18nTable();
$fk = $this->behavior->getI18nForeignKey();
@@ -130,7 +172,10 @@ protected function addGetTranslation()
));
}
- protected function addRemoveTranslation()
+ /**
+ * @return string
+ */
+ protected function addRemoveTranslation()
{
$i18nTable = $this->behavior->getI18nTable();
$fk = $this->behavior->getI18nForeignKey();
@@ -143,16 +188,25 @@ protected function addRemoveTranslation()
));
}
- protected function addGetCurrentTranslation()
+ /**
+ * @return string
+ */
+ protected function addGetCurrentTranslation()
{
return $this->behavior->renderTemplate('objectGetCurrentTranslation', array(
'i18nTablePhpName' => $this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname(),
));
}
- // FIXME: the connection used by getCurrentTranslation in the generated code
- // cannot be specified by the user
- protected function addTranslatedColumnGetter(Column $column)
+ /**
+ * @param Column $column
+ *
+ * @return string
+ */
+
+ // FIXME: the connection used by getCurrentTranslation in the generated code
+ // cannot be specified by the user
+ protected function addTranslatedColumnGetter(Column $column)
{
$objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable());
$comment = '';
@@ -175,9 +229,14 @@ protected function addTranslatedColumnGetter(Column $column)
));
}
- // FIXME: the connection used by getCurrentTranslation in the generated code
- // cannot be specified by the user
- protected function addTranslatedColumnSetter(Column $column)
+ /**
+ * @param Column $column
+ *
+ * @return string
+ */
+ // FIXME: the connection used by getCurrentTranslation in the generated code
+ // cannot be specified by the user
+ protected function addTranslatedColumnSetter(Column $column)
{
$i18nTablePhpName = $this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname();
$tablePhpName = $this->builder->getStubObjectBuilder()->getClassname();
@@ -203,7 +262,11 @@ protected function addTranslatedColumnSetter(Column $column)
));
}
- public function objectFilter(&$script, $builder)
+ /**
+ * @param $script
+ * @param $builder
+ */
+ public function objectFilter(&$script, $builder)
{
$i18nTable = $this->behavior->getI18nTable();
$i18nTablePhpName = $this->builder->getNewStubObjectBuilder($i18nTable)->getUnprefixedClassname();
@@ -218,6 +281,9 @@ public function objectFilter(&$script, $builder)
$script = preg_replace($pattern, $replacement, $script);
}
+ /**
+ * @return string
+ */
protected function addSlugSetter()
{
return $this->behavior->renderTemplate('objectSlugSetter', array(
@@ -226,17 +292,24 @@ protected function addSlugSetter()
));
}
+ /**
+ * @return string
+ */
protected function addSlugGetter()
{
return $this->behavior->renderTemplate('objectSlugGetter', array(
'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'),
View
131 I18nWithSlugBehaviorPeerBuilderModifier.php
@@ -16,20 +16,143 @@
*/
class I18nWithSlugBehaviorPeerBuilderModifier
{
- protected $behavior;
+ protected $behavior, $table, $builder;
- public function __construct(I18nWithSlugBehavior $behavior)
+ /**
+ * @param I18nWithSlugBehavior $behavior
+ */
+ public function __construct(I18nWithSlugBehavior $behavior)
{
$this->behavior = $behavior;
}
- public function staticConstants()
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function staticConstants($builder)
{
- return "
+ return "
/**
* The default culture to use for translations
* @var string
*/
const DEFAULT_CULTURE = '{$this->behavior->getDefaultCulture()}';";
}
+
+
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function staticMethods($builder)
+ {
+ /** @var $foreignKey ForeignKey */
+ $foreignKey = $this->behavior->getI18nForeignKey();
+ $refPhpName = $foreignKey->getRefPhpName() ? $foreignKey->getRefPhpName() : $this->behavior->getI18nTable()->getPhpName();
+ $join = in_array($this->behavior->getBuildProperty('propel.useLeftJoinsInDoJoinMethods'), array(true, null), true) ? 'LEFT' : 'INNER';
+
+ $behaviors = $this->behavior->getTable()->getBehaviors();
+ $mixerHook = !isset($behaviors['symfony_behaviors']) ? '' : <<<EOF
+
+ foreach (sfMixer::getCallables('Base{$this->behavior->getTable()->getPhpName()}:doSelectJoin:doSelectJoin') as \$sf_hook)
+ {
+ call_user_func(\$sf_hook, '{$this->behavior->getTable()->getPhpName()}', \$criteria, \$con);
+ }
+
+EOF;
+
+ if ($this->behavior->getTable()->getChildrenColumn())
+ {
+ $newObject = "\$cls = {$this->behavior->getTable()->getPhpName()}Peer::getOMClass(\$row, 0, false)";
+ }
+ else
+ {
+ $newObject = "\$cls = {$this->behavior->getTable()->getPhpName()}Peer::getOMClass(false)";
+ }
+
+ return <<<EOF
+
+/**
+ * Returns the i18n model class name.
+ *
+ * @return string The i18n model class name
+ */
+static public function getI18nModel()
+{
+ return '{$this->behavior->getI18nTable()->getPhpName()}';
+}
+
+/**
+ * Selects a collection of {@link {$this->behavior->getTable()->getPhpName()}} objects with a {@link {$this->behavior->getI18nTable()->getPhpName()}} translation populated.
+ *
+ * @param Criteria \$criteria
+ * @param string \$culture
+ * @param PropelPDO \$con
+ * @param string \$join_behavior
+ *
+ * @return array
+ */
+static public function doSelectWithI18n(Criteria \$criteria, \$culture = null, \$con = null, \$join_behavior = Criteria::{$join}_JOIN)
+{
+ \$criteria = clone \$criteria;
+
+ if (null === \$culture)
+ {
+ \$culture = sfPropel::getDefaultCulture();
+ }
+
+ // Set the correct dbName if it has not been overridden
+ if (\$criteria->getDbName() == Propel::getDefaultDB()) {
+ \$criteria->setDbName(self::DATABASE_NAME);
+ }
+
+ {$this->behavior->getTable()->getPhpName()}Peer::addSelectColumns(\$criteria);
+ \$startcol = ({$this->behavior->getTable()->getPhpName()}Peer::NUM_COLUMNS - {$this->behavior->getTable()->getPhpName()}Peer::NUM_LAZY_LOAD_COLUMNS);
+ {$this->behavior->getI18nTable()->getPhpName()}Peer::addSelectColumns(\$criteria);
+ \$criteria->addJoin({$this->behavior->getLocalColumn()->getConstantName()}, {$this->behavior->getForeignColumn()->getConstantName()}, \$join_behavior);
+ \$criteria->add({$this->behavior->getCultureColumn($this->behavior->getI18nTable())->getConstantName()}, \$culture);
+{$mixerHook}
+ \$stmt = {$builder->getBasePeerClassname()}::doSelect(\$criteria, \$con);
+ \$results = array();
+
+ while (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
+ \$key1 = {$this->behavior->getTable()->getPhpName()}Peer::getPrimaryKeyHashFromRow(\$row, 0);
+ if (null !== (\$obj1 = {$this->behavior->getTable()->getPhpName()}Peer::getInstanceFromPool(\$key1))) {
+ // We no longer rehydrate the object, since this can cause data loss.
+ // See http://propel.phpdb.org/trac/ticket/509
+ // \$obj1->hydrate(\$row, 0, true); // rehydrate
+ } else {
+ {$newObject};
+ \$obj1 = new \$cls();
+ \$obj1->hydrate(\$row);
+ {$this->behavior->getTable()->getPhpName()}Peer::addInstanceToPool(\$obj1, \$key1);
+ } // if \$obj1 already loaded
+
+ \$key2 = {$this->behavior->getI18nTable()->getPhpName()}Peer::getPrimaryKeyHashFromRow(\$row, \$startcol);
+ if (\$key2 !== null) {
+ \$obj2 = {$this->behavior->getI18nTable()->getPhpName()}Peer::getInstanceFromPool(\$key2);
+ if (!\$obj2) {
+ \$cls = {$this->behavior->getI18nTable()->getPhpName()}Peer::getOMClass(false);
+ \$obj2 = new \$cls();
+ \$obj2->hydrate(\$row, \$startcol);
+ {$this->behavior->getI18nTable()->getPhpName()}Peer::addInstanceToPool(\$obj2, \$key2);
+ } // if obj2 already loaded
+
+ \$obj1->set{$refPhpName}ForCulture(\$obj2, \$culture);
+ } // if joined row was not null
+
+ \$results[] = \$obj1;
+ }
+
+ \$stmt->closeCursor();
+
+ return \$results;
+}
+
+EOF;
+ }
+
}
View
34 I18nWithSlugBehaviorQueryBuilderModifier.php
@@ -9,13 +9,21 @@ class I18nWithSlugBehaviorQueryBuilderModifier
{
protected $behavior, $table, $builder;
- public function __construct(I18nWithSlugBehavior $behavior)
+ /**
+ * @param I18nWithSlugBehavior $behavior
+ */
+ public function __construct(I18nWithSlugBehavior $behavior)
{
$this->behavior = $behavior;
$this->table = $behavior->getTable();
}
- public function queryMethods($builder)
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function queryMethods($builder)
{
$this->builder = $builder;
@@ -29,7 +37,10 @@ public function queryMethods($builder)
return $script;
}
- protected function addJoinI18n()
+ /**
+ * @return string
+ */
+ protected function addJoinI18n()
{
$fk = $this->behavior->getI18nForeignKey();
return $this->behavior->renderTemplate('queryJoinI18n', array(
@@ -40,7 +51,10 @@ protected function addJoinI18n()
));
}
- protected function addJoinWithI18n()
+ /**
+ * @return string
+ */
+ protected function addJoinWithI18n()
{
$fk = $this->behavior->getI18nForeignKey();
return $this->behavior->renderTemplate('queryJoinWithI18n', array(
@@ -50,7 +64,10 @@ protected function addJoinWithI18n()
));
}
- protected function addUseI18nQuery()
+ /**
+ * @return string
+ */
+ protected function addUseI18nQuery()
{
$i18nTable = $this->behavior->getI18nTable();
$fk = $this->behavior->getI18nForeignKey();
@@ -63,6 +80,9 @@ protected function addUseI18nQuery()
));
}
+ /**
+ * @return string
+ */
protected function addFilterBySlug()
{
$i18nTable = $this->behavior->getI18nTable();
@@ -75,9 +95,11 @@ protected function addFilterBySlug()
));
}
+ /**
+ * @return string
+ */
protected function addFindOneBySlug()
{
- $i18nTable = $this->behavior->getI18nTable();
return $this->behavior->renderTemplate('queryFindOneBySlug', array(
'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
'defaultCulture' => $this->behavior->getDefaultCulture(),
View
51 I18nWithSlugChildBehavior.php
@@ -10,6 +10,9 @@ class I18nWithSlugChildBehavior extends Behavior
$queryBuilderModifier;
+ /**
+ * @return I18nWithSlugChildBehaviorObjectBuilderModifier
+ */
public function getObjectBuilderModifier()
{
if (is_null($this->objectBuilderModifier))
@@ -19,8 +22,56 @@ public function getObjectBuilderModifier()
return $this->objectBuilderModifier;
}
+ /**
+ * @return I18nWithSlugChildBehaviorQueryBuilderModifier
+ */
+ public function getQueryBuilderModifier()
+ {
+ if (is_null($this->queryBuilderModifier))
+ {
+ $this->queryBuilderModifier = new I18nWithSlugChildBehaviorQueryBuilderModifier($this);
+ }
+ return $this->queryBuilderModifier;
+ }
+
+ /**
+ * @return Column
+ */
public function getSlugColumn()
{
return $this->table->getColumn($this->getParameter('slug_column'));
}
+
+ /**
+ * Returns the foreign key that references the translated model.
+ *
+ * @throws Exception
+ * @return ForeignKey
+ *
+ */
+ public function getForeignKey()
+ {
+ foreach ($this->getTable()->getForeignKeys() as $fk)
+ {
+ /** @var $fk ForeignKey */
+ /** @var $behaviors array|Behavior[] */
+ $behaviors = $fk->getForeignTable()->getBehaviors();
+ if (isset($behaviors['i18n_with_slug']))
+ {
+ return $fk;
+ }
+ }
+
+ throw new Exception('The foreign key that references the I18N model could not be found.');
+ }
+
+ public function getCultureColumn()
+ {
+ return $this->table->getColumn($this->getParameter('culture_column'));
+ }
+
+ public function getDefaultCulture()
+ {
+ return $this->getParameter('default_culture');
+ }
}
View
18 I18nWithSlugChildBehaviorObjectBuilderModifier.php
@@ -3,13 +3,21 @@ class I18nWithSlugChildBehaviorObjectBuilderModifier
{
protected $behavior, $table, $builder;
- public function __construct(I18nWithSlugChildBehavior $behavior)
+ /**
+ * @param I18nWithSlugChildBehavior $behavior
+ */
+ public function __construct(I18nWithSlugChildBehavior $behavior)
{
$this->behavior = $behavior;
$this->table = $behavior->getTable();
}
- public function objectMethods($builder)
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function objectMethods($builder)
{
$this->builder = $builder;
$script = '';
@@ -23,6 +31,9 @@ public function objectMethods($builder)
return $script;
}
+ /**
+ * @return string
+ */
protected function addSlugSetter()
{
return $this->behavior->renderTemplate('objectChildSlugSetter', array(
@@ -31,6 +42,9 @@ protected function addSlugSetter()
));
}
+ /**
+ * @return string
+ */
protected function addSlugGetter()
{
return $this->behavior->renderTemplate('objectChildSlugGetter', array(
View
63 I18nWithSlugChildBehaviorQueryBuilderModifier.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ * Allows translation of text columns through transparent one-to-many relationship.
+ * Modifier for the query builder.
+ *
+ * @author Vlad Jula-Nedelcu <vlad.nedelcu@mfq.ro>
+ */
+class I18nWithSlugChildBehaviorQueryBuilderModifier
+{
+ protected $behavior, $table, $builder;
+
+ /**
+ * @param I18nWithSlugBehavior $behavior
+ */
+ public function __construct(I18nWithSlugChildBehavior $behavior)
+ {
+ $this->behavior = $behavior;
+ $this->table = $behavior->getTable();
+ }
+
+ /**
+ * @param $builder
+ *
+ * @return string
+ */
+ public function queryMethods($builder)
+ {
+ $this->builder = $builder;
+
+ $script = '';
+ $script .= $this->addFilterBySlug();
+ $script .= $this->addFindOneBySlug();
+
+ return $script;
+ }
+
+ /**
+ * @return string
+ */
+ protected function addFilterBySlug()
+ {
+ $table = $this->behavior->getTable();
+ return $this->behavior->renderTemplate('queryChildFilterBySlug', array(
+ 'queryClass' => $this->builder->getNewStubQueryBuilder($this->table)->getClassname(),
+ 'defaultCulture' => $this->behavior->getDefaultCulture(),
+ 'cultureColumn' => $this->behavior->getCultureColumn()->getPhpName(),
+ 'slugColumn' => $this->behavior->getSlugColumn()->getPhpName(),
+ ));
+ }
+
+ /**
+ * @return string
+ */
+ protected function addFindOneBySlug()
+ {
+ return $this->behavior->renderTemplate('queryChildFindOneBySlug', array(
+ 'objectClassname' => $this->builder->getStubObjectBuilder($this->table)->getClassname(),
+ 'defaultCulture' => $this->behavior->getDefaultCulture(),
+ 'cultureColumn' => $this->behavior->getCultureColumn()->getPhpName(),
+ 'slugColumn' => $this->behavior->getSlugColumn()->getPhpName(),
+ ));
+ }
+}
View
11 templates/objectCreateSlug.php
@@ -96,10 +96,13 @@ protected static function limitSlugSize($slug, $incrementReservedSpace = 3)
protected function makeSlugUnique($slug, $culture = '<?php echo $defaultCulture ?>', $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)
- // watch out: some of the columns may be hidden by the soft_delete behavior
- <?php if ($softDeleteBehaviour)?>->includeDeleted()
- ->count();
+ $slugAlreadyExists = <?php echo $i18nQueryName?>::create()
+ ->filterBy<?php echo $cultureColumnName?>($culture)
+ ->filterBySlug($slug2)->prune($this)
+ <?php if ($softDeleteBehaviour):?>
+ ->includeDeleted() // watch out: some of the columns may be hidden by the soft_delete behavior
+ <?php endif ?>
+ ->count();
if ($slugAlreadyExists)
{
View
12 templates/objectGetCulture.php
@@ -6,5 +6,15 @@
*/
public function getCulture()
{
- return $this->currentCulture;
+ return $this->currentCulture;
+}
+
+/**
+ * alias for @getCulture
+ *
+ * @return string $culture Culture to use for the translation, e.g. 'fr_FR'
+ */
+public function getLocale()
+{
+ return $this->getCulture();
}
View
12 templates/objectSetCulture.php
@@ -11,3 +11,15 @@ public function setCulture($culture = '<?php echo $defaultCulture ?>')
return $this;
}
+
+/**
+ * alias for @setCulture
+ *
+ * @param string $culture Culture to use for the translation, e.g. 'fr_FR'
+ * @return <?php echo $objectClassname ?> The current object (for fluent API support)
+ */
+public function setLocale($culture = '<?php echo $defaultCulture ?>')
+{
+ return $this->setCulture($culture);
+}
+
View
14 templates/queryChildFilterBySlug.php
@@ -0,0 +1,14 @@
+
+/**
+* Filter the query on the slug column
+*
+* @see filterBy()
+*
+* @param string $slug The value to use as filter.
+* @return <?php echo $queryClass ?> The current query, for fluid interface
+*/
+public function filterBySlug($slug, $culture = '<?php echo $defaultCulture ?>')
+{
+ return $this->filterBy('<?php echo $cultureColumn ?>', $culture)->filterBy('<?php echo $slugColumn?>', $slug);
+}
+
View
16 templates/queryChildFindOneBySlug.php
@@ -0,0 +1,16 @@
+
+/**
+* Find one object based on its slug
+*
+* @param string $slug The value to use as filter.
+* @param string $culture Culture to use, e.g. 'en_GB'
+* @param PropelPDO $con The optional connection object
+*
+* @return <?php echo $objectClassname?> the result, formatted by the current formatter
+*/
+public function findOneBySlug($slug, $culture = '<?php echo $defaultCulture ?>', $con = null)
+{
+ return $this->filterBy('<?php echo $cultureColumn ?>', $culture)
+ ->filterBy('<?php echo $slugColumn?>', $slug)
+ ->findOne($con);
+}

0 comments on commit c8e459d

Please sign in to comment.