Created
March 13, 2013 16:35
-
-
Save ariews/5153872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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