diff --git a/bl-kernel/dbpages.class.php b/bl-kernel/dbpages.class.php index 2018f3c5..73c04db4 100644 --- a/bl-kernel/dbpages.class.php +++ b/bl-kernel/dbpages.class.php @@ -31,7 +31,20 @@ 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 + 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 description if empty + if( empty($args['description']) ) { + $args['description'] = Text::truncate($args['content'], 100); + } + // Generate key $key = $this->generateKey($args['slug'], $args['parent']); diff --git a/bl-kernel/helpers/text.class.php b/bl-kernel/helpers/text.class.php index 9955ca2c..6fba3855 100644 --- a/bl-kernel/helpers/text.class.php +++ b/bl-kernel/helpers/text.class.php @@ -227,4 +227,30 @@ class Text { create_function('$input', 'return "
".htmlentities($input[2])."
";'),
$string);
}
+
+ // 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;
+ }
}
\ No newline at end of file