13/03/2017 11:42am

PHP | Problem - DO Url


  public function onBeforeWrite() {

parent::onBeforeWrite();
$filter = URLSegmentFilter::create();
if((!$this->URLSegment || $this->Valid !== 1) && $this->Title) {
$this->URLSegment = $this->generateURLSegment($this->Title);
} else if($this->isChanged('URLSegment', 2)) {
$this->URLSegment = $filter->filter($this->URLSegment);
if(!$this->URLSegment) {
$this->URLSegment = 'produkt-' . $this->ID;
}
}

$count = 2;
while(!$this->validURLSegment()) {
$this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
$count++;
}
if($this->isChanged('Sku', 2) || !$this->ID) {
$this->Sku = $filter->filter($this->Sku);
}
if($this->Valid !== 1 && !$this->InitialWrite) {
$this->Valid = 1;
}
}

public function validURLSegment() {
$existingItem = ShopItem::get()
->filter('URLSegment', $this->URLSegment)
->exclude('ID', $this->ID)
->first();
return !($existingItem);
}

public function generateURLSegment($title){
$filter = URLSegmentFilter::create();
$t = $filter->filter($title);
if(!$t || $t == '-' || $t == '-1') $t = 'produkt-' . $this->ID;
$this->extend('updateURLSegment', $t, $title);
return $t;
}