Skip to content

Commit

Permalink
method toSlug
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2f8f committed Mar 6, 2015
1 parent becef45 commit 4a94122
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
57 changes: 56 additions & 1 deletion Behavior/ExtendedSluggableBehavior.php
Expand Up @@ -138,6 +138,7 @@ public function objectMethods(\PHP5ObjectBuilder $builder)
$this->addMakeSlugUnique($script);

$this->addSortI18ns($script);
$this->addToSlug($script);

return $script;
}
Expand Down Expand Up @@ -192,6 +193,60 @@ protected function createSlug()
";
}


/**
* Метод для формирования Slug
*
* @param $script
*/
protected function addToSlug(&$script)
{

$primary_string = $this->getParameter('primary_string');
$i18n_languages = $this->getContainer()->getParameter("it_blaster_translation.slug_locales");
$primary_string_column = count($i18n_languages) ? $primary_string : $this->getColumnForParameter('primary_string');
$get_primary_string = 'get'.(count($i18n_languages) ? $this->CamelCase($primary_string) : $primary_string_column->getPhpName());

if(!$primary_string_column) {
throw new \Exception('<------ERROR------- Not found column "'.$primary_string.'" in table '.$this->getTable()->getName().' ------ERROR------->');
}

$toSlug = '
/**
* Метод для формирования Slug
*
* @return string
*/
protected function toSlug() {';

//есть языковые версии
if (count($i18n_languages)) {
$languages = 'array(';
foreach ($i18n_languages as $lang) {
$languages.='"'.$lang.'",';
}
$languages.=')';

$toSlug .= '
$to_string = $this->isNew() ? "Новая запись" : "";
$languages = '.$languages.';
foreach ($languages as $language) {
$str = $this->setLocale($language)->'.$get_primary_string.'();
if ($str) {
return $str;
}
}
return $to_string;';
} else { //нет языковых версий
$toSlug .= '
return $this->'.$get_primary_string.'() ? $this->'.$get_primary_string.'() : "Новая запись";';
}
$toSlug .= '
}
';
$script .= $toSlug;
}

protected function addCreateRawSlug(&$script)
{
$pattern = $this->getParameter('slug_pattern');
Expand All @@ -207,7 +262,7 @@ protected function createRawSlug()
if ($pattern) {
$script .= "return '" . str_replace(array('{', '}'), array('\' . $this->cleanupSlugPart($this->get', '()) . \''), $pattern) . "';";
} else {
$script .= "return \$this->cleanupSlugPart(\$this->__toString());";
$script .= "return \$this->cleanupSlugPart(\$this->toSlug());";
}
$script .= "
}
Expand Down
5 changes: 2 additions & 3 deletions Behavior/TranslationBehavior.php
Expand Up @@ -67,7 +67,7 @@ protected function sortI18ns($elements) {
protected function getToStringMethod()
{
$primary_string = $this->getParameter('primary_string');
$i18n_languages = $this->getContainer()->getParameter("it_blaster_translation.slug_locales");
$i18n_languages = $this->getContainer()->getParameter("it_blaster_translation.locales");
$primary_string_column = count($i18n_languages) ? $primary_string : $this->getColumnForParameter('primary_string');
$get_primary_string = 'get'.(count($i18n_languages) ? $this->CamelCase($primary_string) : $primary_string_column->getPhpName());

Expand All @@ -82,8 +82,7 @@ protected function getToStringMethod()
*
* @return string
*/
public function __toString()
{';
public function __toString() {';

//есть языковые версии
if (count($i18n_languages)) {
Expand Down

0 comments on commit 4a94122

Please sign in to comment.