<?php
/**
* @version@1.0.beta.1.1
* for Mambo_4.5.2
* @copyright (C) 2005 Jetze van der Wal 2005
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @project site http:://mambo.jetze.com
*/

//@version@1.1 //transform coding using classes

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'botMosZoomThumb' );

global $zt_param;

function botMosZoomThumb( $published, &$row, &$params, $page=0  ) {
	global $mosConfig_absolute_path, $mosConfig_live_site, $database, $zt_param;

	if (!$published ) {
	    return ''; //tag is not displayed in view mode
	}
	
	//Get parameters from Database
	$query = "SELECT id FROM #__mambots WHERE element = 'moszoomthumb' AND folder = 'content'";

	$database->setQuery( $query );
	$id = $database->loadResult();
	$mambot = new mosMambot( $database );
	$mambot->load( $id );
	$param =& new mosParameters( $mambot->params );
	
	//parameters are configured by user in the mambot configuration.
	//for explanation of values: just check the xml file of the mambot: description of field
	$zt_param = array(
		'path_zoom' => $param->get('path_zoom', 'images/zoom/'),
		'div_mother_1' => $param->get('div_mother_1', 'class="mosimage"'),
		'div_mother_2' => $param->get('div_mother_2', 'class="mosimage"'),
		'div_mother_3' => $param->get('div_mother_3', 'class="mosimage"'),
		'div_mother_4' => $param->get('div_mother_4', 'class="mosimage"'),
		'image_1' => $param->get('image_1', 'class="zoomthumbimage"'),
		'image_2' => $param->get('image_2', 'class="zoomthumbimage"'),
		'div_caption_1' => $param->get('div_caption_1', 'class="mosimage_caption"'),
		'div_caption_2' => $param->get('div_caption_2', 'class="mosimage_caption"'),
		'div_order' => $param->get('div_order', '1'),
		'caption_show' => $param->get('caption_show', '1'),
		'caption_length' => $param->get('caption_length', '0'),
		'debug_info' => $param->get('debug_info', '0'),
		'publish_override' => $param->get('publish_override', '0'),
		'thumbnails_link' => $param->get('thumbnails_link', '0'),
        'itemid' => $param->get('itemid', '')
	);
	
	
	//find pattern for matching mambot tags;
	$regex = '#{moszoomthumb .*}#sU';

	include_once($mosConfig_absolute_path.'/mambots/content/moszoomthumb/moszoomthumb.class.php');
	
	// perform the replacement
	$row->text = preg_replace_callback( $regex, 'botMosZoomThumb_replacer', $row->text );

	//clean up
	unset($zt_param);
	
	return true;
}

/**
* Replaces the matched tags with an image
* @return string
*/
function botMosZoomThumb_replacer( &$matches ) {
	global $mosConfig_absolute_path, $mosConfig_live_site, $database, $zt_param, $version;
	
	
//define important arrays to store values for this process
	//store values provided in mambot tag
	$this_zt_param = array( 'catid' => 0,
		'imgid' => 0,
		'caption' => '',
		'style_mother' => '',
		'style_image' => '',
		'style_caption' => '',
	);	
	
	//store computed/derived/resulting values
	$this_zt_process = array (
		'catid' => '',
		'catpubished' => '',
		'catdir' => '',
		'imgfilename'=> '',
		'imgcaption'=> '',
		'imgpublished'=> '',
		'debuginfo' => '',
		'makelink' => '',
	);
	
	//store resulting html for output
	$outputhtml = array(
		'html-mother' => '',
		'html-img' => '',
		'html-imghref' => '',
		'html-caption' => '',
		'html-final' => '',
	);
	
	//create object to compute img href code
	$imghtmlcode = new ZoomImgHTML;
	
	//okay lets put all the tag parameters in an array;
	$tags = array();
	//@version@1.2 //remove global version info: just overhead
	$this_zt_process['debuginfo'] .= ':: current mambo version='.$version.':: process '.$matches[0];

	//remove outer '{'  '}';
	$mambotcode = substr($matches[0],1,strlen($matches[0])-2);

	$this_zt_process['debuginfo'] .= $mambotcode;
	//split up into array
	$tags = explode( " ", $mambotcode );


    //
    $this_zt_process['debuginfo'] .= ':: global itemid='.$itemid;
// extract values from mambot-tag;
	
	// analyze parameters and their values and put them in array;
	
	for ( $c = 0; $c < count($tags); $c++ ) {
		$tagkeyvalue = explode ("=", $tags[$c]);
		switch (strtolower($tagkeyvalue[0])) {
			case "imgid":
	   			$this_zt_param['imgid']=$tagkeyvalue[1];
				break;
				
			case "caption": //string to process looks like ....caption="caption here"....;
				$this_zt_process['debuginfo'] .= ':: caption found, try extracting it';
				preg_match('#caption=(.*)#', $mambotcode, $xummy );
				$this_zt_param['caption']=substr($xummy[0], 9, strlen($xummy[0])-10);
				$this_zt_process['debuginfo'] .='::found custom caption in:'.$xummy[0];
				break;
			case "style_m":
				$this_zt_param['style_mother']=$tagkeyvalue[1];
				break;
			case "style_i":
				$this_zt_param['style_image']=$tagkeyvalue[1];
				break;
			case "style_c":
				$this_zt_param['style_caption']=$tagkeyvalue[1];
				break;
		};
	}

// find defaults if needed and validate parameter values;

	//imgid must be present, otherwise exit;
	if ($this_zt_param['imgid']==''){
        $this_zt_process['debug_info'].= ":: Error:: required imgid not provided";
        if ($zt_param['debug_info']=='1'){return $this_zt_process['debug_info'];} else {return "";};
	};

	//process styles, if none use defaults
	if ($this_zt_param['style_mother']==''){$this_zt_param['style_mother']='1';};
	
	if ($this_zt_param['style_img']==''){$this_zt_param['style_img']='1';};
	
	if ($this_zt_param['style_caption']==''){$this_zt_param['style_caption']='1';};

    // handle link option in mambot config
    if ($zt_param['thumbnails_link']=='0'){
        $this_zt_param['makelink'] = '0';
    } else {
        $this_zt_param['makelink'] = '1';
    };

	// using imgid get other data:
	$query = "SELECT * FROM #__zoomfiles WHERE imgid=".$this_zt_param['imgid'];
	$database->setQuery( $query );
	$database->loadObject($queryresult);
	$this_zt_process['catid'] = $queryresult->catid;
	$this_zt_process['imgpublished'] = $queryresult->published;
	$this_zt_process['imgfilename'] = $queryresult->imgfilename;
	$this_zt_process['imgcaption'] = $queryresult->imgname;
	
	$catquery = "SELECT * FROM #__zoom WHERE catid=".$this_zt_process['catid'];
	$database->setQuery( $catquery );
	$database->loadObject( $catqueryresult );
	$this_zt_process['catdir'] = $catqueryresult->catdir;
	$this_zt_process['catpublished'] = $catqueryresult->published;

    //next statemtn $zt_param['publish_override'] efect results;
    if (($this_zt_process['imgpublished']=='0')or($this_zt_process['catpublished']=='0')){//unpublished: 
    
        if ($zt_param['publish_override']=='0') {
                   $this_zt_process['debug_info'].= ':: image cannot be shown because it, or its category is unpublished.';
        if ($zt_param['debug_info']=='1'){return $this_zt_process['debug_info'];} else {return "";};
        } else {
        $this_zt_param['makelink'] = '0';
      };
    };

    $this_zt_process['debug_info'].= ':: get caption from zoom?'.$zt_param['caption_show'];
    
//    echo '204::'.$zt_param['caption_show'].$this_zt_process['imgcaption'];
	//process caption for image
	if (($zt_param['caption_show']=='1') and ($this_zt_param['caption']=='') ) {
		//get caption text from zoomtables field imgdescr;
		$this_zt_process['debug_info'].= ":: get caption form zoom image name";
		$this_zt_param['caption'] = $this_zt_process['imgcaption'];
		
	} else {
		//mambot param may say: captio="none" overides caption settings and thus show no caption.;
		if ($this_zt_param['caption']=='none'){$this_zt_param['caption']=='';}
		
	};

	//limit caption length
	if ($zt_param['caption_length']<>'0') {$this_zt_param['caption']=substr($this_zt_param['caption'],0,intval($zt_param['caption_length']));};

	//create final html elements;
	$outputhtml['html-mother']= '<div '.$zt_param['div_mother_'.$this_zt_param['style_mother']].'>';
	
	//create html for thumb image;
	$outputhtml['html-img']= '<img '.$zt_param['image_'.$this_zt_param['style_image']].' src="'.$mosConfig_live_site.$zt_param['path_zoom'].$this_zt_process['catdir'].'/thumbs/'.$this_zt_process['imgfilename'].'" alt="Image here:'.$this_zt_param['caption'].'">';
	
	// caption html
	if ($this_zt_param['caption']<>'') {
		$outputhtml['html-caption']= '<div  '.$zt_param['div_caption_'.$this_zt_param['style_caption']].'>'.$this_zt_param['caption'].'</div>';
	};

	// make thumb clickable?
	if ($this_zt_param['makelink'] == '1') {
		//img with link
		$imghtmlcode->makecode($this_zt_param['imgid'], $this_zt_process['catid'],$zt_param['itemid']);
		$outputhtml['html-imghref'] = '<a '.$imghtmlcode->href.'">'.$outputhtml['html-img'].'</a>';
	} else {
		//img without link
		$outputhtml['html-imghref'] = $outputhtml['html-img'];
	};

	//echo debug info
	if ($zt_param['debug_info']=='1'){
		echo $this_zt_process['debuginfo'].'(end of debuginfo)';
	};

	//start putting it all together into good html
	//image or caption first?
	if ($zt_param['div_order']=='0') {
		$outputhtml['html-final'] = $outputhtml['html-mother'].$outputhtml['html-imghref'].$outputhtml['html-caption'].'</div>';
	} else {
		$outputhtml['html-final'] = $outputhtml['html-mother'].$outputhtml['html-caption'].$outputhtml['html-imghref'].'</div>';
	};

	return $outputhtml['html-final'];

}


?>
