<?php

/**
 * Random Link module for phpWebSite
 *
 * @author rck <http://www.kiesler.at/>
 */

class PHPWS_recentlink {

	var $_active;


	function PHPWS_recentlink() {

		include($GLOBALS['core']->source_dir.
			'mod/recentlink/conf/config.php');

		$this->_active=$active;

	}


	function getLinks($how_many) {


		$prefix=$GLOBALS['core']->tbl_prefix;

		$sql = 'SELECT title, id, url, description ';
		$sql.= "FROM ${prefix}mod_linkman_links ";
		$sql.= 'WHERE active!=0 ';
		$sql.= 'ORDER BY id DESC ';
		$sql.= "LIMIT $how_many";

		return($GLOBALS['core']->getAllAssoc($sql));
		
	}



	function renderNewLink() {
		$isDeity=$_SESSION["OBJ_user"]->isDeity();

		$linka=array();

		if(!$isDeity) {
			$linka['LMN_op']='userMenuAction';
			$linka['LMN_submitLink']=1;
		} else {
			$linka['LMN_op']='adminMenuAction';
			$linka['LMN_addLink']=1;
		}

		$new=$_SESSION['translate']->it('Submit');

		return(PHPWS_Text::moduleLink(
			$new, 'linkman', $linka));
	}


	function renderShowAll() {
		$linka=array();
		$linka['LMN_op']='userMenuAction';

		$all=$_SESSION['translate']->it('Show All');

		return(PHPWS_Text::moduleLink(
			$all, 'linkman', $linka));
	}



	function genTeaser($row) {


		$linka=array();
		$linka['LMN_op']='visitLink';
		$linka['LMN_id']=$row['id'];

		$img='<img src="http://img.simpleapi.net/small/'.$row['url'].'" '.
			'width="128" height="128" title="'.$row['title'].'" alt="'.$row['title'].'" />';

		return(PHPWS_Text::moduleLink($img, 'linkman', $linka));

	}



	function renderLinks($data) {

		$new_link=$this->renderNewLink();

		if(empty($data)) {

			$html ="<p>No links found!</p>\n";

			$html.="<p>$new_link</p>\n";

		} else {
			$count=sizeof($data);

			$html ='<div style="text-align:center">';
			$html.=$this->genTeaser($data[0]);
			$html.='</div><br />';

			$html.="<ul>\n";

			$linkb=array();
			$linkb['LMN_op']='visitLink';

			foreach($data as $nr => $row) {
				$linkb['LMN_id']=$row['id'];
				$title=$row['title'];

				$description=trim(strip_tags($row['description']));

				if(strlen($description)>40)
					$description=substr($description,0,40).'...';

				if(strlen($description)>0)
					$title_tag="title='$description'";
				else
					$title_tag=null;

				$link=PHPWS_Text::moduleLink(
					$title, 'linkman', $linkb, null, null, null,
					$title_tag);

				$html.="<li>$link</li>\n";
			}

			$html.="</ul>\n";

			$all_link=$this->renderShowAll();

			$html.="<p>";
			$html.="$new_link&nbsp;|&nbsp;";
			$html.="$all_link</p>\n";
		}

		

		return($html);
		
	}


	function showLinks() {

		if(!$this->_active)
			return('inactive');

		$data=$this->getLinks(5);
		return($this->renderLinks($data));
		
	}

} 


?>
