Jump to content

News Feed v.0.1, with tags


maxa11an
 Share

Recommended Posts

Today I tried to create a News feed, and I think it became pretty good!

But there is one problem, I have marked it in the code - so if anyone have a clue, tell me!

This one requires two template; news.root (title) and news.article(title, body, summary, news_tags[comma separated values], images[optional]).

CSS:

.news-item .news-image{
width:250px;
border:1px solid #CCC;
text-align:center;
vertical-align:middle;
padding-top:2px;
display:block;
}
.news-item .news-summary{
width:auto;
}
.news-item .news-image p{
padding:1px;
margin:0px !important;
text-align:center !important;
}
.news-item .news-read-more{
text-align:right;
}
.news-item .news-read-more a{
color:#373737 !important;
}
.news-item .news-read-more a:hover{
background:none !important;
text-decoration:underline !important;
}
.news-item{
border-bottom:.4px dashed #CCC;
}
.news-item p{
text-align:left;
}
.news-pager {
clear: both;
margin: 1em 0;
font-family: Arial, sans-serif;
}
.news-pager li {
display: inline;
list-style: none !important;
list-style-type:none !important;
margin: 0;
}
.news-pager li a,
.news-pager li.separator {
display: block;
float: left;
padding: 2px 9px;
color: #fff;
background: #2f4248;
margin-right: 3px;
font-size: 10px;
font-weight: bold;
text-transform: uppercase;
}
.news-pager li.active a,
.news-pager li a:hover {
color: #fff;
background: #db1174;
text-decoration: none;
}
.news-pager li.separator {
display: inline;
color: #777;
background: #d2e4ea;
padding-left: 3px;
padding-right: 3px;
}

the news.root.php template

<?php
include("./head.inc");
/**
*  News template, the base part!
*
* By Max Allan Niklasson
*/
$tagQuery = $sanitizer->text($input->get->tag);
$label = (object) array(
'altTag' => 'Bild',
'postedUnder' => 'Postad under',
'readMore' => 'Läs mer »»',
'published' => 'Publicerad den',
'by'	=> 'av',
'prev'	=> 'Föregående',
'next'	=> 'Nästa',
'tagExpl' => 'Filtrering av nyheter med taggen: <em>'.$tagQuery.'</em>, visar %s träff(ar) av %s träff(ar). <br/> <a href="'.$page->url().'">Visa alla nyheter</a>'
);
/**
Here I have one issue
when set limit = 1, the pager don't work...
Solution?
**/
$limit = 3;
//To fix the pager
$start = ($sanitizer->text($input->get->page) ? $limit * $sanitizer->text($input->get->page) - $limit : 0);
function thumbGet(Page $page, $label){
if($page->images){
	$image = $page->images->first();
	if($image){
		if($image->width() > 250){
			$thumb = $image->size(250);
		}elseif($image->height() > 250){
			$thumb = $image->size(0, 250);
		}else{
			$thumb = $image;
		}
		$return =  
		"\n\t\t\t\t\t\t\t\t".
		'<div class="news-image">'.
		"\n\t\t\t\t\t\t\t\t\t".
		'<img src="'.$thumb->url().'" alt="'.$label->altTag.'" />'.
		"\n\t\t\t\t\t\t\t\t\t".				
		'<p>'.$image->get('description').'</p>'.
		"\n\t\t\t\t\t\t\t\t".				
		'</div>';
		return $return;
	}else{return false;}
}
else{
	return false;
}
}
function processNewsItem(Page $news, $label, Users $users){
$writer = $news->createdUser;
$tags = split(',',$news->news_tags);
echo "\n\t\t\t\t\t".'<div class="news-item">';
	echo "\n\t\t\t\t\t\t".'<h2>'.$news->title.'</h2>';
	echo "\n\t\t\t\t\t\t\t".'<p>';
	echo "\n\t\t\t\t\t\t\t\t".'<div class="news-summary">'.$news->summary.'</div>';
	if(thumbGet($news, $label)){
		echo thumbGet($news, $label);
	}
	echo "\n\t\t\t\t\t\t\t".'</p>';
	if($tags[0]){
		echo "\n\t\t\t\t\t\t\t".
		"<div class='news-info'>";
		echo $label->postedUnder.": ";
		$tagCount = 0;
		foreach($tags as $tag){
			if($tagCount != 0) {echo ' | ';}
			echo "\n\t\t\t\t\t\t\t\t".
			'<a href="?tag='.$tag.'">'.$tag.'</a>';
			$tagCount++;
		}
		echo "\n\t\t\t\t\t\t\t\t".
		'<div class="news-published">'.$label->published.': '.date("Y-m-d \k\l\. H:i:s", $news->created).' '. $label->by .' '.$writer->name.'</div>';
		echo "\n\t\t\t\t\t\t\t".
		'</div>';
	}
	echo "\n\t\t\t\t\t\t\t".'<div class="news-read-more">'.
	"\n\t\t\t\t\t\t\t\t".'<a href="'.$news->url.'">'.$label->readMore.'</a>'.
	"\n\t\t\t\t\t\t\t".'</div>';
echo "\n\n\t\t\t\t\t".'</div> <!-- news-item -->';
}
function pagination(PageArray $cPages, PageArray $tPages,  $label, $limit, $sanitizer, $input){
//The URL fix
$URL = ($sanitizer->text($input->get->tag) ? '?tag='.$sanitizer->text($input->get->tag).'&page=#NR#' : '?page=#NR#');
//Calculate number of matches that should be viewing
$calc = $cPages->count();
//The Totally number of pages
$tot = $tPages->count();
$pages = round(($tot / $limit) + 1);
if($tot <= $limit){
}else{
	echo '<ul class="news-pager">';
	if($pages <= $sanitizer->text($input->get->page) && 0 < ($sanitizer->text($input->get->page) -1) ){
		echo '<li><a href="'.str_replace('#NR#', ($sanitizer->text($input->get->page)-1), $URL).'">'.$label->prev.'</a>';
	}
	for($i = 1; $i <= $pages; $i++){
		$class = '';
		if($sanitizer->text($input->get->page) == $i){
			$class = 'class="active"';
		}elseif($sanitizer->text($input->get->page) == 0 && $i == 1){
			$class = 'class="active"';
		}
		echo '<li '. $class .'><a href="'.str_replace('#NR#', $i, $URL).'">'.$i.'</a>';
	}
	if($pages >= $sanitizer->text($input->get->page) && $pages >= ($sanitizer->text($input->get->page) +1) ){
		echo '<li><a href="'.str_replace('#NR#', ($sanitizer->text($input->get->page)+1), $URL).'">'.$label->next.'</a>';
	}
	echo '</ul>';
}
}



###### Let's Print it out! #######
/**
The viewer from tag query
**/
if($tagQuery){
$counter = $page->children('template=news.article, news_tags*='.$tagQuery.', sort=-date, sort=-id ');
$newsItems = $page->children('template=news.article, news_tags*='.$tagQuery.', sort=-date, sort=-id, start='.$start.', limit='.$limit);
if($newsItems->count()) {
	printf($label->tagExpl, $newsItems->count(), $counter->count() );
	echo "\t\t\t\t".'<div class="news-list">';	
	foreach($newsItems as $news){
		processNewsItem($news, $label, $users);
	}
	echo "\n\n\t\t\t\t".'</div><!-- news-list -->';
}
}else{
/**
The Normal News viewer
**/
$counter = $page->children('template=news.article');
$newsItems = $page->children('template=news.article, sort=-date, sort=-id, start='.$start.', limit='.$limit);
if($newsItems->count()) {
	echo "\t\t\t\t".'<div class="news-list">';	
	foreach($newsItems as $news){
		processNewsItem($news, $label, $users);
	}
	echo "\n\n\t\t\t\t".'</div><!-- news-list -->';

}
}
pagination($newsItems, $counter, $label, $limit, $sanitizer, $input);
include("./foot.inc");

But there is one thing that I wonder about, is it necessary to attach ($label, $sanitizer, $input etc.) in the function?

When I tried

global

but it didn't work...

post-196-0-70716700-1336664382_thumb.png

post-196-0-40379400-1336664651_thumb.png

post-196-0-34954000-1336664663_thumb.png

Link to comment
Share on other sites

Thank's for the reply!

It worked perfect at PW variables but on (for example) $label, it didn't work. Any solution or expl. on why?

I don't know if I understand. $label? That isn't a PW template variable.

Link to comment
Share on other sites

Here I have one issue
when set limit = 1, the pager don't work...
Solution?

It says in the docs that limit needs n= greater 1. This came up recently and in the past, so Ryan mentioned he will make it work with 1 any time rather sooner than later down the road. ;)

No it's a variable created by me (in top of the script).

Yes and it's not in the scope of the function. You either need to pass it to the function or make it global inside the function.

Link to comment
Share on other sites

Yes and it's not in the scope of the function. You either need to pass it to the function or make it global inside the function.

Well I've tried to make it global, but it results in a empty array... I gues I need to pass it over then...

Link to comment
Share on other sites

Ahhh sorry for being so dumb :D... about the pagination. I assumed you're using the PW pagination modul... I have to take back what I said about the limit greater than 1 to work.

I'm not sure about the global, I rarely use it. Could it be that you define it an object? It's not a real array this way. Try just $label = array(...)

Link to comment
Share on other sites

You could also use the php $GLOBALS to define global variables throughout the script if you want to.

$GLOBALS['label'] = (object) array('text'=>'Das ist ein text');

function renderLabel(){
   return $GLOBALS['label']->text;
}

echo renderLabel();
Link to comment
Share on other sites

Well even that did not seem to work, so I attach the $label in the function.

Thanks for the help, and this is the result. Any way to make it better?


<?php
include("./head.inc");
/**
*  News template, the base part!
*
* By Max Allan Niklasson
*/
$tagQuery = $sanitizer->text($input->get->tag);
$label = array(
'altTag' => 'Bild',
'postedUnder' => 'Postad under',
'readMore' => 'Läs mer »»',
'published' => 'Publicerad den',
'by' => 'av',
'prev' => 'Föregående',
'next' => 'Nästa',
'tagExpl' => 'Filtrering av nyheter med taggen: <em>'.$tagQuery.'</em>, visar %s träff(ar) av %s träff(ar). <br/> <a href="'.$page->url().'">Visa alla nyheter</a>'
);
/**
Here I have one issue
when set $limit = 1, the pager don't work...
Solution?
**/
$limit = 3;
//To fix the pager
$start = ($sanitizer->text($input->get->page) ? $limit * $sanitizer->text($input->get->page) - $limit : 0);
function thumbGet(Page $page, $label =null){
if($page->images){
 $image = $page->images->first();
 if($image){
  if($image->width() > 250){
$thumb = $image->size(250);
  }elseif($image->height() > 250){
$thumb = $image->size(0, 250);
  }else{
$thumb = $image;
  }
  $return =
  "\n\t\t\t\t\t\t\t\t".
  '<div class="news-image">'.
  "\n\t\t\t\t\t\t\t\t\t".
  '<img src="'.$thumb->url().'" alt="'.$label[altTag].'" />'.
  "\n\t\t\t\t\t\t\t\t\t".  
  '<p>'.$image->get('description').'</p>'.
  "\n\t\t\t\t\t\t\t\t".  
  '</div>';
  return $return;
 }else{return false;}
}
else{
 return false;
}
}
function processNewsItem(Page $news, Users $users, $label){
$writer = $news->createdUser;
$tags = split(',',$news->news_tags);
echo "\n\t\t\t\t\t".'<div class="news-item">';
 echo "\n\t\t\t\t\t\t".'<h2>'.$news->title.'</h2>';
 echo "\n\t\t\t\t\t\t\t".'<p>';
 echo "\n\t\t\t\t\t\t\t\t".'<div class="news-summary">'.$news->summary.'</div>';
 if(thumbGet($news)){
  echo thumbGet($news, $label);
 }
 echo "\n\t\t\t\t\t\t\t".'</p>';
 if($tags[0]){
  echo "\n\t\t\t\t\t\t\t".
  "<div class='news-info'>";
  echo $label[postedUnder].": ";
  $tagCount = 0;
  foreach($tags as $tag){
if($tagCount != 0) {echo ' | ';}
echo "\n\t\t\t\t\t\t\t\t".
'<a href="?tag='.$tag.'">'.$tag.'</a>';
$tagCount++;
  }
  echo "\n\t\t\t\t\t\t\t\t".
  '<div class="news-published">'.$label[published].': '.date("Y-m-d \k\l\. H:i:s", $news->created).' '. $label[by] .' '.$writer->name.'</div>';
  echo "\n\t\t\t\t\t\t\t".
  '</div>';
 }
 echo "\n\t\t\t\t\t\t\t".'<div class="news-read-more">'.
 "\n\t\t\t\t\t\t\t\t".'<a href="'.$news->url.'">'.$label[readMore].'</a>'.
 "\n\t\t\t\t\t\t\t".'</div>';
echo "\n\n\t\t\t\t\t".'</div> <!-- news-item -->';
}
function pagination(PageArray $cPages, PageArray $tPages, $limit, $label){
//The URL fix
$input = wire("input")->get;
$sanitizer = wire("sanitizer");
$URL = ($sanitizer->text($input->tag) ? '?tag='.$sanitizer->text($input->tag).'&page=#NR#' : '?page=#NR#');
//The Totally number of pages
$tot = $tPages->count();
$pages = round(($tot / $limit) + 1);
if($tot <= $limit){
}else{
 echo '<ul class="news-pager">';
 if($pages <= $sanitizer->text($input->page) && 0 < ($sanitizer->text($input->page) -1) ){
  echo '<li><a href="'.str_replace('#NR#', ($sanitizer->text($input->page)-1), $URL).'">'.$label[prev].'</a>';
 }
 for($i = 1; $i <= $pages; $i++){
  $class = '';
  if($sanitizer->text($input->page) == $i){
$class = 'class="active"';
  }elseif($sanitizer->text($input->page) == 0 && $i == 1){
$class = 'class="active"';
  }
  echo '<li '. $class .'><a href="'.str_replace('#NR#', $i, $URL).'">'.$i.'</a>';
 }
 if($pages >= $sanitizer->text($input->page) && $pages >= ($sanitizer->text($input->page) +1) ){
  echo '<li><a href="'.str_replace('#NR#', ($sanitizer->text($input->page)+1), $URL).'">'.$label["next"].'</a>';
 }
 echo '</ul>';
}
}

###### Let's Print it out! #######
/**
The viewer from tag query
**/
if($tagQuery){
$counter = $page->children('template=news.article, news_tags*='.$tagQuery.', sort=-date, sort=-id ');
$newsItems = $page->children('template=news.article, news_tags*='.$tagQuery.', sort=-date, sort=-id, start='.$start.', limit='.$limit);
if($newsItems->count()) {
 printf($label->tagExpl, $newsItems->count(), $counter->count() );
 echo "\t\t\t\t".'<div class="news-list">';
 foreach($newsItems as $news){
  processNewsItem($news, $users, $label);
 }
 echo "\n\n\t\t\t\t".'</div><!-- news-list -->';
}
}else{
/**
The Normal News viewer
**/
$counter = $page->children('template=news.article');
$newsItems = $page->children('template=news.article, sort=-date, sort=-id, start='.$start.', limit='.$limit);
if($newsItems->count()) {
 echo "\t\t\t\t".'<div class="news-list">';
 foreach($newsItems as $news){
  processNewsItem($news, $users, $label);
 }
 echo "\n\n\t\t\t\t".'</div><!-- news-list -->';
}
}
pagination($newsItems, $counter, $limit, $label);
include("./foot.inc"); 
Link to comment
Share on other sites

Hey there.

The $GLOBALS does work. Maybe you got it wrong something. A variable defined in $GLOBALS is available in the script no matter where.

Regarding your question, there is always room to improve. It certainly is ok and does already a lot, and I think you're learning a lot. Good job. But I think there's other ways to archive it much simpler code-wise. I for myself I would use pages for tagging, categories and not a textfield. And maybe use urlSegments for the tag instead of url param. Also I would use the pager module that PW already has to do the pagination.

Maybe something that's not my thing is all the \n\ \ ... it's too much for my liking. It's ok to use at places, or completely leave them out. It makes it hard to maintain and busy to read. Just my 2 cents.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...