public function getUri($title, $id = 0, $contenttypeslug = "", $fulluri = true, $allowempty = true, $slugfield = 'slug')
{
$contenttype = $this->getContentType($contenttypeslug);
$tablename = $this->getContenttypeTablename($contenttype);
$id = intval($id);
$slug = $this->app['slugify']->slugify($title);
// Don't allow strictly numeric slugs, unless allow_numeric_slugs options is set
if (is_numeric($slug) && $contenttype['allow_numeric_slugs'] !== true) {
$slug = $contenttype['singular_slug'] . "-" . $slug;
}
// Only add '{contenttype}/' if $full is requested.
if ($fulluri) {
$prefix = '/' . $contenttype['singular_slug'] . '/';
} else {
$prefix = '';
}
$fields = $this->getContentTypeFields($contenttypeslug);
//check if the fieldname exists, otherwise use 'slug' as fallback
if (!in_array($slugfield, $fields)) {
$slugfield = 'slug';
}
$query = sprintf("SELECT id from %s WHERE %s=? and id!=?", $tablename, $slugfield);
$res = $this->app['db']->executeQuery($query, [$slug, $id], [\PDO::PARAM_STR, \PDO::PARAM_INT])->fetch();
if (!$res) {
$uri = $prefix . $slug;
} else {
for ($i = 1; $i <= 10; $i++) {
$newslug = Html::trimText($slug, 127 - strlen($i), false) . '-' . $i;
$res = $this->app['db']->executeQuery($query, [$newslug, $id], [\PDO::PARAM_STR, \PDO::PARAM_INT])->fetch();
if (!$res) {
$uri = $prefix . $newslug;
break;
}
}
// otherwise, just get a random slug.
if (empty($uri)) {
$suffix = '-' . $this->app['randomgenerator']->generateString(6, 'abcdefghijklmnopqrstuvwxyz01234567890');
$slug = Html::trimText($slug, 128 - strlen($suffix), false) . $suffix;
$uri = $prefix . $slug;
}
}
// When storing, we should never have an empty slug/URI. If we can't make a nice one, set it to 'slug-XXXX'.
if (!$allowempty && empty($uri)) {
$uri = 'slug-' . $this->app['randomgenerator']->generateString(6, 'abcdefghijklmnopqrstuvwxyz01234567890');
}
return $uri;
}