Skip to content

Instantly share code, notes, and snippets.

@ariews
Created March 13, 2013 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariews/5153872 to your computer and use it in GitHub Desktop.
Save ariews/5153872 to your computer and use it in GitHub Desktop.
<?php
defined('SYSPATH') or die('No direct script access.');
class Field_Slug extends Jelly_Field_Slug
{
public function save($model, $value, $loaded)
{
return $this->set_slug($value);
}
public function set($value)
{
if ($value === NULL OR ($this->null AND empty($value)))
return NULL;
return $value;
}
private function set_slug($value)
{
$temp = $this->_slug($value);
$model = Jelly::select($this->model)
->where(':slug_key', 'REGEXP', "^{$temp}(-[[:digit:]])?")
->execute();
if ($model->count() > 0)
{
$number = array();
$found = false;
foreach ($model as $m)
{
if ($m->{$this->column} == $temp)
$found = true;
if (preg_match('/-(\d+)$/i', $m->{$this->column}, $match))
$number[] = $match[1];
}
if (sizeof($number) > 0)
$value = "{$value}-".(max($number) + 1);
elseif ($found)
$value = "{$value} 2";
}
return $this->_slug($value);
}
private function _slug($value)
{
$value = preg_replace('/[^a-z0-9-\/]/', '-', strtolower($value));
$value = preg_replace('/-{2,}/', '-', $value);
$value = trim($value, '-');
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment