Jump to content


Photo

News Feed v.0.1, with tags

news managment tags

  • Please log in to reply
10 replies to this topic

#1 rödgladan

rödgladan

    Newbie

  • Members
  • Pip
  • 9 posts
  • 1

  • LocationThe Westcoast of Sweden

Posted 10 May 2012 - 10:41 AM

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...

Attached Thumbnails

  • Editing NewsPost.png
  • The Main List.png
  • Filter by Processwire-tag.png


#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,416 posts
  • 1942

  • LocationSH, Switzerland

Posted 10 May 2012 - 10:45 AM

Long story short. :) You're most likely looking for wire(). You can use also in function scope.

wire("pages")->find(...)
wire("sanitizer")->
etc...

@somartist | modules created | support me, flattr my work flattr.com


#3 rödgladan

rödgladan

    Newbie

  • Members
  • Pip
  • 9 posts
  • 1

  • LocationThe Westcoast of Sweden

Posted 10 May 2012 - 02:02 PM

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?

#4 Soma

Soma

    Hero Member

  • Moderators
  • 3,416 posts
  • 1942

  • LocationSH, Switzerland

Posted 10 May 2012 - 02:04 PM

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.

@somartist | modules created | support me, flattr my work flattr.com


#5 rödgladan

rödgladan

    Newbie

  • Members
  • Pip
  • 9 posts
  • 1

  • LocationThe Westcoast of Sweden

Posted 10 May 2012 - 02:06 PM

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

#6 Soma

Soma

    Hero Member

  • Moderators
  • 3,416 posts
  • 1942

  • LocationSH, Switzerland

Posted 10 May 2012 - 02:09 PM

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.

@somartist | modules created | support me, flattr my work flattr.com


#7 rödgladan

rödgladan

    Newbie

  • Members
  • Pip
  • 9 posts
  • 1

  • LocationThe Westcoast of Sweden

Posted 10 May 2012 - 02:13 PM

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...

#8 Soma

Soma

    Hero Member

  • Moderators
  • 3,416 posts
  • 1942

  • LocationSH, Switzerland

Posted 10 May 2012 - 02:22 PM

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(...)

@somartist | modules created | support me, flattr my work flattr.com


#9 Soma

Soma

    Hero Member

  • Moderators
  • 3,416 posts
  • 1942

  • LocationSH, Switzerland

Posted 10 May 2012 - 03:13 PM

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();

@somartist | modules created | support me, flattr my work flattr.com


#10 rödgladan

rödgladan

    Newbie

  • Members
  • Pip
  • 9 posts
  • 1

  • LocationThe Westcoast of Sweden

Posted 10 May 2012 - 05:33 PM

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");


#11 Soma

Soma

    Hero Member

  • Moderators
  • 3,416 posts
  • 1942

  • LocationSH, Switzerland

Posted 15 May 2012 - 05:02 AM

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.

@somartist | modules created | support me, flattr my work flattr.com






Also tagged with one or more of these keywords: news managment, tags

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users