Merge pull request #419 from acrox999/master

Get relative time from content & Keep UUID
This commit is contained in:
Diego Najar 2017-04-29 20:25:37 +02:00 committed by GitHub
commit dc553bae2f
2 changed files with 358 additions and 319 deletions

View File

@ -1,318 +1,357 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); <?php defined('BLUDIT') or die('Bludit CMS.');
class Content { class Content {
public $vars; public $vars;
function __construct($path) function __construct($path)
{ {
if($this->build($path)===false) { if($this->build($path)===false) {
$this->vars = false; $this->vars = false;
} }
} }
// Return TRUE if the content is loaded correctly // Return TRUE if the content is loaded correctly
public function isValid() public function isValid()
{ {
return($this->vars!==false); return($this->vars!==false);
} }
// Returns the value from the $field, FALSE if the field doesn't exist // Returns the value from the $field, FALSE if the field doesn't exist
public function getField($field) public function getField($field)
{ {
if(isset($this->vars[$field])) { if(isset($this->vars[$field])) {
return $this->vars[$field]; return $this->vars[$field];
} }
return false; return false;
} }
// Set a value to a field // Set a value to a field
public function setField($field, $value, $overwrite=true) public function setField($field, $value, $overwrite=true)
{ {
if($overwrite || empty($this->vars[$field])) { if($overwrite || empty($this->vars[$field])) {
$this->vars[$field] = $value; $this->vars[$field] = $value;
} }
return true; return true;
} }
// Parse the content from the file index.txt // Parse the content from the file index.txt
private function build($path) private function build($path)
{ {
if( !Sanitize::pathFile($path.FILENAME) ) { if( !Sanitize::pathFile($path.FILENAME) ) {
return false; return false;
} }
$tmp = 0; $tmp = 0;
$lines = file($path.FILENAME); $lines = file($path.FILENAME);
foreach($lines as $lineNumber=>$line) foreach($lines as $lineNumber=>$line)
{ {
$parts = array_map('trim', explode(':', $line, 2)); $parts = array_map('trim', explode(':', $line, 2));
// Lowercase variable // Lowercase variable
$parts[0] = Text::lowercase($parts[0]); $parts[0] = Text::lowercase($parts[0]);
// If variables is content then break the foreach and process the content after. // If variables is content then break the foreach and process the content after.
if($parts[0]==='content') if($parts[0]==='content')
{ {
$tmp = $lineNumber; $tmp = $lineNumber;
break; break;
} }
if( !empty($parts[0]) && !empty($parts[1]) ) { if( !empty($parts[0]) && !empty($parts[1]) ) {
// Sanitize all fields, except Content. // Sanitize all fields, except Content.
$this->vars[$parts[0]] = Sanitize::html($parts[1]); $this->vars[$parts[0]] = Sanitize::html($parts[1]);
} }
} }
// Process the content. // Process the content.
if($tmp!==0) if($tmp!==0)
{ {
// Next line after "Content:" variable // Next line after "Content:" variable
$tmp++; $tmp++;
// Remove lines after Content // Remove lines after Content
$output = array_slice($lines, $tmp); $output = array_slice($lines, $tmp);
if(!empty($parts[1])) { if(!empty($parts[1])) {
array_unshift($output, "\n"); array_unshift($output, "\n");
array_unshift($output, $parts[1]); array_unshift($output, $parts[1]);
} }
$implode = implode($output); $implode = implode($output);
$this->vars['content'] = $implode; $this->vars['content'] = $implode;
// Sanitize content. // Sanitize content.
//$this->vars['content'] = Sanitize::html($implode); //$this->vars['content'] = Sanitize::html($implode);
} }
} }
// Returns the title field // Returns the title field
public function title() public function title()
{ {
return $this->getField('title'); return $this->getField('title');
} }
// Returns the content // Returns the content
// This content is markdown parser // This content is markdown parser
// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content // (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content
// (boolean) $noSanitize, TRUE returns the content without sanitized // (boolean) $noSanitize, TRUE returns the content without sanitized
public function content($fullContent=true, $noSanitize=true) public function content($fullContent=true, $noSanitize=true)
{ {
// This content is not sanitized. // This content is not sanitized.
$content = $this->getField('content'); $content = $this->getField('content');
if(!$fullContent) { if(!$fullContent) {
$content = $this->getField('breakContent'); $content = $this->getField('breakContent');
} }
if($noSanitize) { if($noSanitize) {
return $content; return $content;
} }
return Sanitize::html($content); return Sanitize::html($content);
} }
// Returns the content // Returns the content
// This content is not markdown parser // This content is not markdown parser
// (boolean) $noSanitize, TRUE returns the content without sanitized // (boolean) $noSanitize, TRUE returns the content without sanitized
public function contentRaw($noSanitize=true) public function contentRaw($noSanitize=true)
{ {
// This content is not sanitized. // This content is not sanitized.
$content = $this->getField('contentRaw'); $content = $this->getField('contentRaw');
if($noSanitize) { if($noSanitize) {
return $content; return $content;
} }
return Sanitize::html($content); return Sanitize::html($content);
} }
// Returns TRUE if the content has the text splited // Returns TRUE if the content has the text splited
public function readMore() public function readMore()
{ {
return $this->getField('readMore'); return $this->getField('readMore');
} }
// //
public function category() public function category()
{ {
return $this->getField('category'); return $this->getField('category');
} }
public function uuid() public function uuid()
{ {
return $this->getField('uuid'); return $this->getField('uuid');
} }
// Returns the field key // Returns the field key
public function key() public function key()
{ {
return $this->getField('key'); return $this->getField('key');
} }
// Returns TRUE if the post/page is published, FALSE otherwise. // Returns TRUE if the post/page is published, FALSE otherwise.
public function published() public function published()
{ {
return ($this->getField('status')==='published'); return ($this->getField('status')==='published');
} }
// Returns TRUE if the post/page is scheduled, FALSE otherwise. // Returns TRUE if the post/page is scheduled, FALSE otherwise.
public function scheduled() public function scheduled()
{ {
return ($this->getField('status')==='scheduled'); return ($this->getField('status')==='scheduled');
} }
// Returns TRUE if the post/page is draft, FALSE otherwise. // Returns TRUE if the post/page is draft, FALSE otherwise.
public function draft() public function draft()
{ {
return ($this->getField('status')=='draft'); return ($this->getField('status')=='draft');
} }
// Returns the file name of the cover image, FALSE there isn't a cover image setted // Returns the file name of the cover image, FALSE there isn't a cover image setted
// (boolean) $absolute, TRUE returns the absolute path and file name, FALSE just the file name // (boolean) $absolute, TRUE returns the absolute path and file name, FALSE just the file name
public function coverImage($absolute=true) public function coverImage($absolute=true)
{ {
$fileName = $this->getField('coverImage'); $fileName = $this->getField('coverImage');
if(empty($fileName)) { if(empty($fileName)) {
return false; return false;
} }
if($absolute) { if($absolute) {
return HTML_PATH_UPLOADS.$fileName; return HTML_PATH_UPLOADS.$fileName;
} }
return $fileName; return $fileName;
} }
/* /*
DEPRECATED ? DEPRECATED ?
public function profilePicture() public function profilePicture()
{ {
return HTML_PATH_UPLOADS_PROFILES.$this->username().'.jpg'; return HTML_PATH_UPLOADS_PROFILES.$this->username().'.jpg';
} }
*/ */
// Returns the user object // Returns the user object
// (boolean) $field, TRUE returns the value of the field, FALSE returns the object // (boolean) $field, TRUE returns the value of the field, FALSE returns the object
public function user($field=false) public function user($field=false)
{ {
// Get the user object. // Get the user object.
$User = $this->getField('user'); $User = $this->getField('user');
if($field) { if($field) {
return $User->getField($field); return $User->getField($field);
} }
return $User; return $User;
} }
// Returns the username who created the post/page // Returns the username who created the post/page
public function username() public function username()
{ {
return $this->getField('username'); return $this->getField('username');
} }
// Returns the description field // Returns the description field
public function description() public function description()
{ {
return $this->getField('description'); return $this->getField('description');
} }
// Returns the date according to locale settings and format settings // Returns the date according to locale settings and format settings
public function date() public function date()
{ {
return $this->getField('date'); return $this->getField('date');
} }
// Returns the date according to locale settings and format as database stored // Returns the date according to locale settings and format as database stored
// (string) $format, you can specify the date format // (string) $format, you can specify the date format
public function dateRaw($format=false) public function dateRaw($format=false)
{ {
$date = $this->getField('dateRaw'); $date = $this->getField('dateRaw');
if($format) { if($format) {
return Date::format($date, DB_DATE_FORMAT, $format); return Date::format($date, DB_DATE_FORMAT, $format);
} }
return $date; return $date;
} }
// Returns the tags // Returns relative time (e.g. "1 minute ago")
// (boolean) $returnsArray, TRUE to get the tags as an array, FALSE to get the tags separeted by comma // Based on http://stackoverflow.com/a/18602474
public function tags($returnsArray=false) // Modified for Bludit
{ // $complete = false : short version
global $Url; // $complete = true : full version
public function relativeTime($complete = false) {
$tags = $this->getField('tags'); $current = new DateTime;
$past = new DateTime($this->getField('date'));
if($returnsArray) { $elapsed = $current->diff($past);
if($tags==false) { $elapsed->w = floor($elapsed->d / 7);
return array(); $elapsed->d -= $elapsed->w * 7;
}
$string = array(
return $tags; 'y' => 'year',
} 'm' => 'month',
else { 'w' => 'week',
if($tags==false) { 'd' => 'day',
return false; 'h' => 'hour',
} 'i' => 'minute',
's' => 'second',
// Return string with tags separeted by comma. );
return implode(', ', $tags);
} foreach($string as $key => &$value) {
} if($elapsed->$key) {
$value = $elapsed->$key . ' ' . $value . ($elapsed->$key > 1 ? 's' : ' ');
// Returns the permalink } else {
// (boolean) $absolute, TRUE returns the post/page link with the DOMAIN, FALSE without the DOMAIN unset($string[$key]);
public function permalink($absolute=false) }
{ }
global $Url;
global $Site; if(!$complete) {
$string = array_slice($string, 0 , 1);
$filterType = $this->getField('filterType'); }
$url = trim(DOMAIN_BASE,'/'); return $string ? implode(', ', $string) . ' ago' : 'Just now';
$key = $this->key();
$filter = trim($Url->filters($filterType), '/'); }
$htmlPath = trim(HTML_PATH_ROOT,'/');
// Returns the tags
if(empty($filter)) { // (boolean) $returnsArray, TRUE to get the tags as an array, FALSE to get the tags separeted by comma
$tmp = $key; public function tags($returnsArray=false)
} {
else { global $Url;
$tmp = $filter.'/'.$key;
} $tags = $this->getField('tags');
if($absolute) { if($returnsArray) {
return $url.'/'.$tmp;
} if($tags==false) {
return array();
if(empty($htmlPath)) { }
return '/'.$tmp;
} return $tags;
}
return '/'.$htmlPath.'/'.$tmp; else {
} if($tags==false) {
return false;
public function json($returnsArray=false) }
{
$tmp['key'] = $this->key(); // Return string with tags separeted by comma.
$tmp['title'] = $this->title(); return implode(', ', $tags);
$tmp['content'] = $this->content(); // Markdown parsed }
$tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed }
$tmp['description'] = $this->description();
$tmp['date'] = $this->dateRaw(); // Returns the permalink
$tmp['permalink'] = $this->permalink(true); // (boolean) $absolute, TRUE returns the post/page link with the DOMAIN, FALSE without the DOMAIN
public function permalink($absolute=false)
if($returnsArray) { {
return $tmp; global $Url;
} global $Site;
return json_encode($tmp); $filterType = $this->getField('filterType');
}
} $url = trim(DOMAIN_BASE,'/');
$key = $this->key();
$filter = trim($Url->filters($filterType), '/');
$htmlPath = trim(HTML_PATH_ROOT,'/');
if(empty($filter)) {
$tmp = $key;
}
else {
$tmp = $filter.'/'.$key;
}
if($absolute) {
return $url.'/'.$tmp;
}
if(empty($htmlPath)) {
return '/'.$tmp;
}
return '/'.$htmlPath.'/'.$tmp;
}
public function json($returnsArray=false)
{
$tmp['key'] = $this->key();
$tmp['title'] = $this->title();
$tmp['content'] = $this->content(); // Markdown parsed
$tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed
$tmp['description'] = $this->description();
$tmp['date'] = $this->dateRaw();
$tmp['permalink'] = $this->permalink(true);
if($returnsArray) {
return $tmp;
}
return json_encode($tmp);
}
}

View File

@ -212,7 +212,7 @@ class dbPosts extends dbJSON
$args['dateModified'] = Date::current(DB_DATE_FORMAT); $args['dateModified'] = Date::current(DB_DATE_FORMAT);
// Keep UUID // Keep UUID
$args['uuid'] = $this->uuid(); $args['uuid'] = $this->db[$args['key']]['uuid'];
if( $this->delete($args['key']) ) { if( $this->delete($args['key']) ) {
return $this->add($args); return $this->add($args);