mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
cb45815fbd
Problems resolved: 1) Case sensitivy of check on Code field of Group 2) MySQL and Postgres have different date functions 3) When BlogID is empty, explicitly set it to 0. If not then all tests break under Postgres
73 lines
1.6 KiB
PHP
73 lines
1.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Adds URLSegment functionality to Tags & Categories.
|
|
*
|
|
* @package silverstripe
|
|
* @subpackage blog
|
|
*/
|
|
class URLSegmentExtension extends DataExtension
|
|
{
|
|
/**
|
|
* @var array
|
|
*/
|
|
private static $db = array(
|
|
'URLSegment' => 'Varchar(255)',
|
|
);
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function onBeforeWrite()
|
|
{
|
|
if ($this->owner->BlogID) {
|
|
$this->owner->generateURLSegment();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generates a unique URLSegment from the title.
|
|
*
|
|
* @param int $increment
|
|
*
|
|
* @return string
|
|
*/
|
|
public function generateURLSegment($increment = null)
|
|
{
|
|
$filter = new URLSegmentFilter();
|
|
|
|
$this->owner->URLSegment = $filter->filter($this->owner->Title);
|
|
|
|
if (is_int($increment)) {
|
|
$this->owner->URLSegment .= '-' . $increment;
|
|
}
|
|
|
|
// Postgres use '' instead of 0 as an emtpy blog ID
|
|
// Without this all the tests fail
|
|
if (!$this->owner->BlogID) {
|
|
$this->owner->BlogID = 0;
|
|
}
|
|
|
|
$duplicate = DataList::create($this->owner->ClassName)->filter(array(
|
|
'URLSegment' => $this->owner->URLSegment,
|
|
'BlogID' => $this->owner->BlogID,
|
|
));
|
|
|
|
if ($this->owner->ID) {
|
|
$duplicate = $duplicate->exclude('ID', $this->owner->ID);
|
|
}
|
|
|
|
if ($duplicate->count() > 0) {
|
|
if (is_int($increment)) {
|
|
$increment += 1;
|
|
} else {
|
|
$increment = 0;
|
|
}
|
|
|
|
$this->owner->generateURLSegment((int) $increment);
|
|
}
|
|
|
|
return $this->owner->URLSegment;
|
|
}
|
|
}
|