fix create slug
This commit is contained in:
parent
7ff9d45fe7
commit
571782368c
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace kernel\helpers;
|
namespace kernel\helpers;
|
||||||
|
|
||||||
|
use kernel\FormModel;
|
||||||
|
use kernel\modules\post\models\Post;
|
||||||
|
|
||||||
class Slug
|
class Slug
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -123,6 +126,15 @@ class Slug
|
|||||||
return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str;
|
return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function recursiveCreateSlug($model, $slug, $tmpSlug = '', $id = 1): string
|
||||||
|
{
|
||||||
|
if ($tmpSlug === '') $tmpSlug = $slug;
|
||||||
|
if ($model::where(['slug' => $tmpSlug])->exists()) {
|
||||||
|
$tmpSlug = $slug . '-' . $id++;
|
||||||
|
$tmpSlug = Slug::recursiveCreateSlug($model, $slug, $id);
|
||||||
|
}
|
||||||
|
return $tmpSlug;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -163,3 +175,4 @@ chto-delat-esli-ya-ne-hochu-utf-8
|
|||||||
This is an Example String. What's Going to Happen to Me?
|
This is an Example String. What's Going to Happen to Me?
|
||||||
This_is_a_Test_String_What_s_Going_to_Ha
|
This_is_a_Test_String_What_s_Going_to_Ha
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ class PostService
|
|||||||
$model->content = $form_model->getItem('content');
|
$model->content = $form_model->getItem('content');
|
||||||
$model->user_id = $form_model->getItem('user_id');
|
$model->user_id = $form_model->getItem('user_id');
|
||||||
$model->title = $form_model->getItem('title');
|
$model->title = $form_model->getItem('title');
|
||||||
$model->slug = $this->createSlug($form_model);
|
$model->slug = Slug::recursiveCreateSlug(Post::class, $this->createSlug($form_model));
|
||||||
if ($model->save()){
|
if ($model->save()){
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
@ -25,22 +25,7 @@ class PostService
|
|||||||
|
|
||||||
public function createSlug(FormModel $form_model): string
|
public function createSlug(FormModel $form_model): string
|
||||||
{
|
{
|
||||||
$slug = Slug::url_slug($form_model->getItem('title'), ['transliterate' => true, 'lowercase' => true]);
|
return Slug::url_slug($form_model->getItem('title'), ['transliterate' => true, 'lowercase' => true]);
|
||||||
if (Post::where(['slug' => $slug])->exists()) {
|
|
||||||
$id = 1;
|
|
||||||
$tmpSlug = $slug . '-' . $id++;
|
|
||||||
$slug = $this->recursiveCreateSlug($slug, $tmpSlug, $id);
|
|
||||||
}
|
|
||||||
return $slug;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function recursiveCreateSlug($slug, $tmpSlug, $id): string
|
|
||||||
{
|
|
||||||
if (Post::where(['slug' => $tmpSlug])->exists()) {
|
|
||||||
$tmpSlug = $slug . '-' . $id++;
|
|
||||||
$tmpSlug = $this->recursiveCreateSlug($slug, $tmpSlug, $id);
|
|
||||||
}
|
|
||||||
return $tmpSlug;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(FormModel $form_model, Post $post): false|Post
|
public function update(FormModel $form_model, Post $post): false|Post
|
||||||
|
Loading…
Reference in New Issue
Block a user