{RELATED_SUMMARY} replacement tag - NOW WORKING!

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Post Reply
coopersred
Posts: 35
Joined: Thu 8. Apr 2004, 14:40

{RELATED_SUMMARY} replacement tag - NOW WORKING!

Post by coopersred »

Code: Select all

    // Tested with phpwcms ver 1.2.5 DEV
    //
    // Copy this file to phpwcms_template\inc_script\frontend_render
    //
    // Syntax: {RELATED_SUMMARY:number_of_related_articles:number_of_summaries_to_display:keyword}
This replacement tag allows you to generate a number of article links based on a keyword the same way the {RELATED} tag does . Plus it also allows you to display the summary information.

You can download the tag here:
http://www.red-heeler.com/
User avatar
DeXXus
Posts: 2168
Joined: Fri 28. Nov 2003, 06:20
Location: USA - Florida

Post by DeXXus »

Code: Select all

<?php
    // releated Summary Tag
    //
    // Tested with phpwcms ver 1.2.5 DEV
    //
    // copy this file to phpwcms_template\inc_script\frontend_render
    //
    // Syntax: {RELATED_SUMMARY:number_of_related_article:number_of_summaries_to_display:keyword}
    
    
    //----------------------------------------------
    // Mainpart
    //
    //----------------------------------------------
    
function get_related_articles_summary($keywords, $current_article_id, $template_default, $max_cnt_links=0, $num_summaries_to_show, $dbcon) {
	// find keyword for current article used for RELATED_SUMMARY replacement tag
	// prepared and inspired by Magnar Stav Johanssen

	$keyword_links = ""; 
	$data = array ();
	$max_cnt_links = intval($max_cnt_links);
		
	// replace unwanted chars and convert to wanted
	$keywords = str_replace(";", ",", $keywords);
	$keywords = str_replace("'", "", $keywords);
	$keywords = str_replace(" ", ",", $keywords);
	
	if($keywords == 'ALLKEYWORDS') {
		$keywords = $GLOBALS['content']['all_keywords'];
	}
	// choose comma separated keywords
	$keywordarray = explode (",", $keywords);
	$keywordarray = array_map('trim', $keywordarray);
	$keywordarray = array_diff($keywordarray, array(''));
	$keywordarray = array_unique($keywordarray);
	// check for empty keywords or keywords smaller than 3 chars
	if(is_array($keywordarray) && count($keywordarray)) {
		foreach($keywordarray as $key => $value) {
			if(strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key])) {
				unset($keywordarray[$key]);
			}
		}
	}
	//Selects the data based on keywwords
	if(is_array($keywordarray) && count($keywordarray)) {
		$where = "";
		foreach($keywordarray as $value) {
			//build where keyword = blabla
			$where .= ($where) ? " OR " : "";
			//replace every "'" to "''" for security reasons with aporeplace()
			$where .= "article_keyword LIKE '%".aporeplace($value)."%'";		
		}
		$limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
//$sql  =	"SELECT article_id, article_title, article_cid, article_subtitle, article_summary ";
		$sql  =	"SELECT * FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";

//		$sql .=	"FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
		$sql .=	"article_id<>".intval($current_article_id)." AND ";
		// VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
		switch(VISIBLE_MODE) {
			case 0: $sql .=	"article_public=1 AND article_aktiv=1 AND ";
					break;
			case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
					break;
			//case 2: admin mode no additional neccessary
		}
		$sql .=	"article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
		$sql .=	"ORDER BY article_tstamp DESC".$limit;

		// get the article data
		// copied from get_actcat_articles_data function in front.func.inc.php
		if($result = mysql_query($sql, $dbcon)) 
		{
			while($row = mysql_fetch_assoc($result)) 
			{
				$data[$row["article_id"]] = array(
										"article_id"		=> $row["article_id"],
										"article_cid"		=> $row["article_cid"],
										"article_title"		=> $row["article_title"],
										"article_subtitle"	=> $row["article_subtitle"],
										"article_keyword"	=> $row["article_keyword"],
										"article_summary"	=> $row["article_summary"],
										"article_redirect"	=> $row["article_redirect"],
										"article_date"		=> $row["article_date"],
										"article_username"	=> $row["article_username"],
										"article_sort"		=> $row["article_sort"],
										"article_notitle"	=> $row["article_notitle"],
										"article_created"	=> $row["article_created"],
										"article_image"		=> unserialize($row["article_image"]),
										"article_timeout"	=> $row["article_cache"],
										"article_nosearch"	=> $row["article_nosearch"],
										"article_nositemap"	=> $row["article_nositemap"],
										"article_aliasid"	=> $row["article_aliasid"],
										"article_headerdata"=> $row["article_headerdata"],
										"article_morelink"	=> $row["article_morelink"]
												);
				// now check for article alias ID
				if($row["article_aliasid"]) {
					$aid = $row["article_id"];
					$alias_sql  = "SELECT *, UNIX_TIMESTAMP(article_tstamp) AS article_date FROM ".DB_PREPEND."phpwcms_article ";
					$alias_sql .= "WHERE article_deleted=0 AND article_id=".intval($row["article_aliasid"]);
					if(!$row["article_headerdata"]) {
						switch(VISIBLE_MODE) {
							case 0: $alias_sql .= " AND article_public=1 AND article_aktiv=1";
									break;
							case 1: $alias_sql .= " AND article_uid=".$_SESSION["wcs_user_id"];
									break;
						}
						$alias_sql .= " AND article_begin < NOW() AND article_end > NOW()";
					}
					$alias_sql .= " AND article_deleted=0 LIMIT 1";
					if($alias_result = mysql_query($alias_sql, $dbcon)) {
						if($alias_row = mysql_fetch_assoc($alias_result)) {
							$data[$aid]["article_id"] = $alias_row["article_id"];
							// use alias article header data
							if(!$row["article_headerdata"]) {
								$data[$aid]["article_title"]	= $alias_row["article_title"];
								$data[$aid]["article_subtitle"]	= $alias_row["article_subtitle"];
								$data[$aid]["article_keyword"]	= $alias_row["article_keyword"];
								$data[$aid]["article_summary"]	= $alias_row["article_summary"];
								$data[$aid]["article_redirect"]	= $alias_row["article_redirect"];
								$data[$aid]["article_date"]		= $alias_row["article_date"];
								$data[$aid]["article_image"]	= unserialize($alias_row["article_image"]);
							}
						}
						mysql_free_result($alias_result);
					}
				}
			}
			mysql_free_result($result);
		}

		//Generate the summary - taken from list_article_summary from front.func.inc.php
		
		$article_list = $data;

		$listing = $template_default["space_top"]; //start with space at top
		
		$tmpllist = array(); //temporary array for storing templates to minimize load
		
		$temp_topcount = intval($num_summaries_to_show);
		
		$temp_counter = 0;
		foreach($article_list as $key => $value) {
			
			//$article_link  = $link_to."?id=".$cat_id.",".$article_list[$key]["article_id"].",0,0,1,0";
			$article_link  = "index.php?id=".$article_list[$key]["article_cid"].",".$article_list[$key]["article_id"].",0,0,1,0";
			
			//add available keywords to page wide keyword field
			$GLOBALS['content']['all_keywords'] .= $article_list[$key]["article_keyword"].',';
			
			//if($temp_counter < $template_default["top_count"]) {
			if($temp_counter < $temp_topcount) {
				// as long as the counter is lower than the default "top_count" value
				// show the complete article summary listing
				
				// build image/image link
				$article_list[$key]["article_image"]["poplink"] = '';
				$thumb_image = false;
				$thumb_img = '';
				
				if(!empty($article_list[$key]["article_image"]["caption"])) {
					$caption	= explode('|', $article_list[$key]["article_image"]["caption"]);
				} else {
					$caption[0] = '';
				}
				$article_list[$key]["article_image"]["caption"] = $caption[0]; //$caption[0]
				
				if(!empty($article_list[$key]["article_image"]["hash"])) {
				
					$thumb_image = get_cached_image(
										array(	"target_ext"	=>	$article_list[$key]["article_image"]['ext'],
												"image_name"	=>	$article_list[$key]["article_image"]['hash'] . '.' . $article_list[$key]["article_image"]['ext'],
												"max_width"		=>	$article_list[$key]["article_image"]['width'],
												"max_height"	=>	$article_list[$key]["article_image"]['height'],
												"thumb_name"	=>	md5($article_list[$key]["article_image"]['hash'].$article_list[$key]["article_image"]['width'].$article_list[$key]["article_image"]['height'].$GLOBALS['phpwcms']["sharpen_level"])
	        							  ));
									
					if($thumb_image != false) {
					
						$caption[1] = (!isset($caption[1])) ? '' : html_specialchars($caption[1]);
						if(!isset($caption[2])) {
							$caption[2] = array('',' target="_blank"');
						} else {
							//proof target of link
							$caption[2] = explode(' ', trim($caption[2]));
							$caption[2][1] = (!isset($caption[2][1])) ? '' : ' target="'.$caption[2][1].'"';
						}
					
						$thumb_img = '<img src="'.PHPWCMS_IMAGES . $thumb_image[0] .'" border="0" '.$thumb_image[3].' alt="'.$caption[1].'" title="'.$caption[1].'">';
							
						if($article_list[$key]["article_image"]["zoom"]) {
							
							$zoominfo = get_cached_image(
												array(	"target_ext"	=>	$article_list[$key]["article_image"]['ext'],
														"image_name"	=>	$article_list[$key]["article_image"]['hash'] . '.' . $article_list[$key]["article_image"]['ext'],
														"max_width"		=>	$GLOBALS['phpwcms']["img_prev_width"],
														"max_height"	=>	$GLOBALS['phpwcms']["img_prev_height"],
														"thumb_name"	=>	md5($article_list[$key]["article_image"]['hash'].$GLOBALS['phpwcms']["img_prev_width"].$GLOBALS['phpwcms']["img_prev_height"].$GLOBALS['phpwcms']["sharpen_level"])
	        					  						)
													);
							
							if($zoominfo != false) {
							
								$article_list[$key]["article_image"]["poplink"] =	'image_zoom.php?show='.base64_encode($zoominfo[0].'?'.$zoominfo[3]);
								
								if(!empty($caption[2][0])) {
									$open_link = $caption[2][0];
									$return_false = '';
								} else {
									$open_link = $article_list[$key]["article_image"]["poplink"];
									$return_false = 'return false;';
								}
								
								$article_list[$key]["article_image"]["poplink"] =	'<a href="'.$article_list[$key]["article_image"]["poplink"].'" '.
																		 			//'onClick="window.open(\''.$open_link.
																					'onClick="checkClickZoom();clickZoom(\''.$open_link.
																					"','previewpic','width=".$zoominfo[1].
																					",height=".$zoominfo[2]."');".$return_false.
																					'"'.$caption[2][1].'>'.$thumb_img.'</a>';
							}
							
							unset($caption);
							
						}
					
					}
				
				}
				
				
			
				// article list based template check
				if(!empty($article_list[$key]["article_image"]['tmpllist']) && $article_list[$key]["article_image"]['tmpllist']!='default') {
					// try to read the template files
					// 1. try to check if template was read
					if(!isset($tmpllist[ $article_list[$key]["article_image"]['tmpllist'] ])) {
						$tmpllist[ $article_list[$key]["article_image"]['tmpllist'] ] = include_ext_php(PHPWCMS_ROOT.$GLOBALS['phpwcms']['templates'].'inc_cntpart/articlesummary/list/'.$article_list[$key]["article_image"]['tmpllist'], 1);
					}
					if($tmpllist[ $article_list[$key]["article_image"]['tmpllist'] ]) {
						//rendering
						$tmpl = $tmpllist[ $article_list[$key]["article_image"]['tmpllist'] ];
						$tmpl = render_cnt_template($tmpl, 'TITLE', html_specialchars($article_list[$key]["article_title"]));
						$tmpl = render_cnt_template($tmpl, 'SUB', html_specialchars($article_list[$key]["article_subtitle"]));
						$tmpl = render_cnt_template($tmpl, 'SUMMARY', $article_list[$key]["article_summary"]);
						$tmpl = render_cnt_template($tmpl, 'IMAGE', $thumb_img);
						$tmpl = render_cnt_template($tmpl, 'ZOOMIMAGE', ($article_list[$key]["article_image"]["poplink"] ? $article_list[$key]["article_image"]["poplink"] : $thumb_img));  // fix by jens
						$tmpl = render_cnt_template($tmpl, 'CAPTION', nl2br(html_specialchars($article_list[$key]["article_image"]["caption"])));
						$tmpl = render_cnt_template($tmpl, 'ARTICLELINK', $article_link);
						$tmpl = render_cnt_template($tmpl, 'EDITOR', $article_list[$key]["article_username"]);
						$tmpl = render_cnt_template($tmpl, 'ARTICLEID', $article_list[$key]["article_id"]);					
						$tmpl = render_cnt_template($tmpl, 'MORE', $template_default["top_readmore_link"]);
						$tmpl = render_cnt_template($tmpl, 'BEFORE', '<!--before//-->');
						$tmpl = render_cnt_template($tmpl, 'AFTER', '<!--after//-->');
						$tmpl = render_cnt_date($tmpl, $article_list[$key]["article_date"]);
						if($temp_counter) {
							$tmpl = render_cnt_template($tmpl, 'SPACE', '<!--space//-->');
						} else {
							$tmpl = render_cnt_template($tmpl, 'SPACE', '');
						}
						$listing .= $tmpl;
						$article_list[$key]["article_image"]['tmpllist'] = 1;
					} else {
						$article_list[$key]["article_image"]['tmpllist'] = 0;
					}
				}
				
				if (empty($article_list[$key]["article_image"]['tmpllist']) || $article_list[$key]["article_image"]['tmpllist'] == 'default') {
				
					$listing .= $template_default["top_listentry_before"];
				
					// space between summary listings
					if($temp_counter) {
						$listing .= $template_default["space_between_sum"];
					}
				
					if($article_list[$key]["article_title"] && !$article_list[$key]["article_notitle"]) {
						//$listing .= get_html_part($article_list[$key]["article_title"], $template_default["top_headline_class"], $article_link);
						$listing .= $template_default["top_headline_before"];
						if($article_list[$key]["article_morelink"]) {
							$listing .= '<a href="'.$article_link.'">';
							$listing .= html_specialchars($article_list[$key]["article_title"]);
							$listing .= '</a>';
						} else {
							$listing .= html_specialchars($article_list[$key]["article_title"]);
						}
						$listing .= $template_default["top_headline_after"];
						$listing .= $template_default["top_headline_space"];
					}
					if($article_list[$key]["article_subtitle"]) {
						//$listing .= get_html_part($article_list[$key]["article_subtitle"], $template_default["top_subheadline_class"], $article_link);
						$listing .= $template_default["top_subheadline_before"];
						if($article_list[$key]["article_morelink"]) {
							$listing .= '<a href="'.$article_link.'">';
							$listing .= html_specialchars($article_list[$key]["article_subtitle"]);
							$listing .= '</a>';
						} else {
							$listing .= html_specialchars($article_list[$key]["article_subtitle"]);
						}
						$listing .= $template_default["top_subheadline_after"];
						$listing .= $template_default["top_subheadline_space"];
					}
					if($article_list[$key]["article_summary"] || !empty($article_list[$key]["article_image"]['cname'])) {
						//$listing .= "<span class=\"".$template_default["top_text_class"]."\">";
						$listing .= $template_default["top_text_before"];
					
						//build image table when image available
						if(!empty($thumb_img)) {
							$listing .= '<table width="1%" cellspacing="0" border="0" cellpadding="0" align="left" ';
							if($template_default["article"]["image_table_bgcolor"]) {
								$listing .= 'bgcolor="'.$template_default["article"]["image_table_bgcolor"].'" ';
							}
							$listing .= 'style="float:left;margin:2px 5px 3px 0;">';
							$listing .= "\n<tr><td";
							if($template_default["article"]["image_class"]) {
								$listing .= ' class="'.$template_default["article"]["image_class"].'"';
							}
							$listing .= '>';
							if($article_list[$key]["article_morelink"]) {
								$listing .= '<a href="'.$article_link.'">'.$thumb_img."</a>";
							} else {
								$listing .= $thumb_img;
							}
							$listing .= "</td></tr>\n";
							
							if($article_list[$key]["article_image"]["caption"]) {
								//$listing .= '<tr><td><img src="img/leer.gif" alt="" width="1" height="3"></td></tr>';
								$listing .= "\n<tr><td";
								if($template_default["article"]["image_caption_class"]) {
									$listing .= ' class="'.$template_default["article"]["image_caption_class"].'"';
								}
								if($template_default["article"]["image_caption_bgcolor"]) {
									$listing .= ' bgcolor="'.$template_default["article"]["image_caption_bgcolor"].'"';
								}
								if($template_default["article"]["image_caption_valign"]) {
									$listing .= ' valign="'.$template_default["article"]["image_caption_valign"].'"';
								}
								if($template_default["article"]["image_caption_align"]) {
									$listing .= ' align="'.$template_default["article"]["image_caption_align"].'"';
								}
								$listing .= '>';
								$listing .= $template_default["article"]["image_caption_before"];
								$listing .= nl2br(html_specialchars($article_list[$key]["article_image"]["caption"]));
								$listing .= $template_default["article"]["image_caption_after"];
								$listing .= "</td></tr>\n";
							}
							$listing .= '</table>';
						}
					
						$listing .= $article_list[$key]["article_summary"];
						if($article_list[$key]["article_morelink"]) {
							$listing .= $template_default["top_readmore_before"];
							$listing .= '<a href="'.$article_link.'">';
							$listing .= $template_default["top_readmore_link"];
							$listing .= '</a>'.$template_default["top_readmore_after"];
						}
						$listing .= $template_default["top_text_after"];
					}			
					$listing .= $template_default["top_listentry_after"];
				}
			
			} else { 
				// if "top_count" value is equal or larger
				// show only the article headline listing
				//if($temp_counter && $temp_counter == $template_default["top_count"]) {
				if($temp_counter && $temp_counter == $temp_topcount) {
					$listing .= $template_default["space_aftertop_text"];
				} elseif ($temp_counter) {
					$listing .= $template_default["space_between_list"];
				}
				$listing .= $template_default["list_headline_before"];
				$listing .= '<a href="'.$article_link.'">';
				$listing .= $template_default["list_startimage"];
				$listing .= html_specialchars($article_list[$key]["article_title"]);
				$listing .= '</a>'.$template_default["list_headline_after"];
			
			}
			$temp_counter++;
		}
		
		$listing .= $template_default["space_bottom"]; //ends with space at bottom
		return str_replace("<br /><br />", "<br />", $listing);

	}

}

// related articles based on keywords, inspired by Magnar Stav Johanssen
if( ! ( strpos($content["all"],'{RELATED_SUMMARY:')===false ) ) {
	if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
		$related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
	} else {
		$related_keywords = '';
	}

	$content["all"] = preg_replace('/\{RELATED_SUMMARY:(\d+):(\d+):(.*?)\}/e','get_related_articles_summary("$3",$aktion[1],$template_default,"$1","$2",$db);',$content["all"]);
}

?>
Post Reply