diff --git a/bl-kernel/dbpages.class.php b/bl-kernel/dbpages.class.php index 74736edd..f5eec294 100644 --- a/bl-kernel/dbpages.class.php +++ b/bl-kernel/dbpages.class.php @@ -28,9 +28,17 @@ class dbPages extends dbJSON { $dataForDb = array(); // This data will be saved in the database $dataForFile = array(); // This data will be saved in the file - + + // Generate title + if( empty($args['title']) ) { + $args['title'] = Text::truncate($args['content'], 60); + + // Assign the new title to the slug as well. + $args['slug'] = $args['title']; + } + $key = $this->generateKey($args['slug'], $args['parent']); - + // Generate UUID $args['uuid'] = md5(time().DOMAIN); diff --git a/bl-kernel/dbposts.class.php b/bl-kernel/dbposts.class.php index d14f7945..37cba8a8 100644 --- a/bl-kernel/dbposts.class.php +++ b/bl-kernel/dbposts.class.php @@ -111,7 +111,15 @@ class dbPosts extends dbJSON // Current date, format of DB_DATE_FORMAT $currentDate = Date::current(DB_DATE_FORMAT); - + + // Generate title + if( empty($args['title']) ) { + $args['title'] = Text::truncate($args['content'], 60); + + // Assign the new title to the slug as well. + $args['slug'] = $args['title']; + } + // Generate the database key / index $key = $this->generateKey($args['slug']); diff --git a/bl-kernel/helpers/text.class.php b/bl-kernel/helpers/text.class.php index da532f43..f2e6de04 100644 --- a/bl-kernel/helpers/text.class.php +++ b/bl-kernel/helpers/text.class.php @@ -212,6 +212,32 @@ class Text { return $cut; } + + // Truncates the string under the limit specified by the limit parameter. + public static function truncate($string, $limit, $end = '...') + { + // Check if over $limit + if(mb_strlen($string) > $limit) { + + // Check if string is only one word + if(preg_match('/\s/', $string)) { + + // Append the string specified by the end parameter to the end of the string as it is over the limit. + $truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, 'UTF-8'), 'UTF-8')); + } else { + $truncate = trim(mb_substr($string, 0, $limit, 'UTF-8')); + } + $truncate = $truncate.$end; + } else { + $truncate = $string; + } + + if(empty($truncate)) { + return ''; + } + + return $truncate; + } // Return string length public static function length($string)