add a global option to mix posts with identical tags - usefull when used with the api to get diverse posts: TODO - add transaltion for the global advanced settings

This commit is contained in:
krasi georgiev 2016-11-02 16:21:09 +00:00
parent 8aa7c7c56d
commit 0a54c06712
6 changed files with 136 additions and 18 deletions

View File

@ -102,6 +102,32 @@ class HTML {
$args['type'] = 'password'; $args['type'] = 'password';
self::formInputText($args); self::formInputText($args);
} }
public static function formCheckbox($args)
{
$id = 'js'.$args['name'];
$class = empty($args['class']) ? '' : 'class="'.$args['class'].'"';
$placeholder = empty($args['placeholder']) ? '' : 'placeholder="'.$args['placeholder'].'"';
$html = '<div class="uk-form-row">';
if(!empty($args['label'])) {
$html .= '<label for="'.$id.'" class="uk-form-label">'.$args['label'].'</label>';
}
$html .= '<div class="uk-form-controls">';
// without the hidden field it doesn't send any post value when the checkbox is not checked
$html .= "<input type='hidden' value='0' name='".$args['name']."'>";
$html .= '<input type="checkbox" id="'.$id.'" name="'.$args['name'].'" '.$class.' '.$placeholder.' value=1 '.($args['checked']?"checked":"").' />';
if(!empty($args['tip'])) {
$html .= '<p class="uk-form-help-block">'.$args['tip'].'</p>';
}
$html .= '</div>';
$html .= '</div>';
echo $html;
}
public static function formTextarea($args) public static function formTextarea($args)
{ {

View File

@ -20,6 +20,13 @@ HTML::formOpen(array('class'=>'uk-form-horizontal'));
'tip'=>$L->g('number-of-posts-to-show-per-page') 'tip'=>$L->g('number-of-posts-to-show-per-page')
)); ));
HTML::formCheckbox(array(
'name'=>'shuffleIdeticalTags',
'label'=>$L->g('Shufle posts with identical tags'),
'checked'=>$Site->shuffleIdeticalTags(),
'class'=>'uk-width-1-3 uk-form-medium',
));
HTML::formSelect(array( HTML::formSelect(array(
'name'=>'homepage', 'name'=>'homepage',
'label'=>$L->g('Default home page'), 'label'=>$L->g('Default home page'),

View File

@ -103,6 +103,7 @@ class dbPosts extends dbJSON
public function add($args) public function add($args)
{ {
global $Site;
$dataForDb = array(); // This data will be saved in the database $dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file $dataForFile = array(); // This data will be saved in the file
@ -190,6 +191,10 @@ class dbPosts extends dbJSON
// Sort posts before save // Sort posts before save
$this->sortByDate(); $this->sortByDate();
if($Site->shuffleIdeticalTags()){
$this->mixTags();
}
if( $this->save() === false ) { if( $this->save() === false ) {
// Trying to rollback // Trying to rollback
@ -402,6 +407,58 @@ class dbPosts extends dbJSON
return true; return true;
} }
// Mix the posts so that neighbour posts have different Tags -
// helpfull if you want to show diverse content. Case studies for example
public function mixTags()
{
$previousValue = null;
$finalArray=array();
$buffeArray=array();
// Get array keys
$arrayKeys = array_keys($this->db);
// Fetch last array key
$lastArrayKey = array_pop($arrayKeys);
foreach($this->db as $key=>$row){
foreach($buffeArray as $bufferKey=>$bufferRow){
// Get buffer array keys
$finalArrayKeys = array_keys($finalArray);
// Fetch last array key
$finalArraylastArrayKey = array_pop($finalArrayKeys);
if($bufferRow['tags']!=$finalArray[$finalArraylastArrayKey]['tags']){
$finalArray[$bufferKey]=$bufferRow;
unset($buffeArray[$bufferKey]);
}
}
$finalArrayKeys = array_keys($finalArray);
// Fetch last array key
$finalArraylastArrayKey = array_pop($finalArrayKeys);
if(empty($finalArray)){
$finalArray[$key]=$row;
}
elseif($row['tags']==$finalArray[$finalArraylastArrayKey]['tags']){
$buffeArray[$key]=$row;
}
else{
$finalArray[$key]=$row;
}
//reached the end so no other choice but to add the remaining buffer
if($key == $lastArrayKey) {
$finalArray=array_merge($finalArray,$buffeArray);
}
$previousValue = $row;
}
$this->db=$finalArray;
return true;
}
private function sortLowToHigh($a, $b) { private function sortLowToHigh($a, $b) {
return $a['date']>$b['date']; return $a['date']>$b['date'];
} }

View File

@ -8,6 +8,7 @@ class dbSite extends dbJSON
'description'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''),
'footer'=> array('inFile'=>false, 'value'=>'I wanna be a pirate!'), 'footer'=> array('inFile'=>false, 'value'=>'I wanna be a pirate!'),
'postsperpage'=> array('inFile'=>false, 'value'=>''), 'postsperpage'=> array('inFile'=>false, 'value'=>''),
'shuffleIdeticalTags'=> array('inFile'=>false, 'value'=>''),
'language'=> array('inFile'=>false, 'value'=>'en'), 'language'=> array('inFile'=>false, 'value'=>'en'),
'locale'=> array('inFile'=>false, 'value'=>'en_US'), 'locale'=> array('inFile'=>false, 'value'=>'en_US'),
'timezone'=> array('inFile'=>false, 'value'=>'America/Argentina/Buenos_Aires'), 'timezone'=> array('inFile'=>false, 'value'=>'America/Argentina/Buenos_Aires'),
@ -239,6 +240,12 @@ class dbSite extends dbJSON
return $this->getField('postsperpage'); return $this->getField('postsperpage');
} }
// Returns if tags mix is enabled.
public function shuffleIdeticalTags()
{
return $this->getField('shuffleIdeticalTags');
}
// Returns the current language. // Returns the current language.
public function language() public function language()
{ {

View File

@ -105,6 +105,27 @@ function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeU
array_push($posts, $Post); array_push($posts, $Post);
} }
} }
return $posts;
}
function buildTagPosts($tag,$amount=3, $pageNumber=0){
global $dbTags;
$posts=array();
$tag=str_replace(" ", "-",$tag);
$keys= $dbTags->getList($pageNumber, $amount, $tag);
foreach($keys as $key=>$value) {
$Post = buildPost($key);
if($Post==false){
break;
}
$posts['itms'][$key]=$Post->vars;
$posts['itms'][$key]['permalink']=$Post->permalink();
}
$posts['count']=$dbTags->countPostsByTag($tag);
$posts['page']=$pageNumber;
return $posts; return $posts;
} }
@ -193,7 +214,7 @@ function buildAllPages()
// Remove the error page // Remove the error page
unset($list['error']); unset($list['error']);
// Sorte pages // Sort pages
uasort($list, 'sortPages'); uasort($list, 'sortPages');
foreach($list as $key=>$db) foreach($list as $key=>$db)