<?php

function smarty_cms_function_qr_image($params, &$smarty) {
	$gCms = cmsms();

	$qr_image = '';
	$start_img = '<img src=';
	$close_img = ' />';
	$data_uri = $params['data_uri'];
	$path = $params['path'];

	// get image from google charts api
	$image_url_qr = 'https://chart.googleapis.com/chart?chs=' . $params['width'] . 'x' . $params['height'] . '&cht=qr&chl=' . $params['url'] . '&choe=UTF-8';
	// save image in specified folder relative to uploads
	if(!empty($params['path'])) {
		$image_path_qr = $gCms -> config['image_uploads_path'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $image_filename_qr;
	} else {
		// save image in configured image uploads folder
		$image_path_qr = $gCms -> config['image_uploads_path'] . DIRECTORY_SEPARATOR . $image_filename_qr;
	}
	$image_filename_qr = md5($params['url']) . '_' . date(Y . m . d) . '' . '.gif';
	if(!file_exists($image_path_qr . $image_filename_qr)) {

		$image_to_fetch = file_get_contents($image_url_qr);
		$local_image_file = fopen($image_path_qr . $image_filename_qr . $extension, 'w');
		chmod($image_path_qr . $image_filename_qr, 0755);
		fwrite($local_image_file, $image_to_fetch);
		fclose($local_image_file);
	}
	// if file existis construct image tag
	if(file_exists($image_path_qr)) {
		// if param data_uri is not set or 0 show base64 image
		if($data_uri != 0) {
			if(!empty($params['path'])) {
				$get_image = $gCms -> config['image_uploads_url'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $image_filename_qr;
				$qr_image = $start_img .= '"data:image/gif;base64,' . base64_encode(file_get_contents($get_image)) . '"';
			} else {
				$get_image = $gCms -> config['image_uploads_url'] . DIRECTORY_SEPARATOR . $image_filename_qr;
				$qr_image = $start_img .= '"data:image/gif;base64,' . base64_encode(file_get_contents($get_image)) . '"';
			}
			// else show gif image
		} else {
			if(!empty($params['path'])) {
				$qr_image = $start_img .= '"' . $gCms -> config['image_uploads_url'] . '/' . $path . '/' . $image_filename_qr . '"';
			} else {

				$qr_image = $start_img .= '"' . $gCms -> config['image_uploads_url'] . '/' . $image_filename_qr . '"';
			}
		}
		// set image width
		if(!empty($params['width'])) {
			$qr_image .= ' width="' . $params['width'] . '"';
		} else {
			$qr_image .= ' width="100"';
			// else set 100px
		}
		// set image height
		if(!empty($params['height'])) {
			$qr_image .= ' height="' . $params['height'] . '"';
		} else {
			$qr_image .= ' height="100"';
			// else set 100px

		}
		if(!empty($params['alt'])) {
			$alt = $params['alt'];
		} else {
			$alt = $params['url'];
		}
		$qr_image .= ' alt="' . $alt . '"';
		if(!empty($params['title'])) {
			$title = $params['title'];
		} else {
			$title = $params['url'];
		}
		$qr_image .= ' title="' . $params['title'] . '"';
		if(!empty($params['class'])) {
			$qr_image .= ' class="' . $params['class'] . '"';
		}
		$qr_image .= $close_img;
	} else {

		$qr_image = '<p style="color:#f00">Oh no. Something is wrong.<br />Image was not found.</p>';
	}
	if(isset($params['assign'])) {
		$smarty -> assign(trim($params['assign']), $qr_image);
		return ;
	}
	return $qr_image;

}
function smarty_cms_help_function_qr_image() {

?>
<h3>What does this do?</h3>
<p>
	This plugin generates a simple QR Code using
	<a href="http://code.google.com/intl/en/apis/chart/image/docs/gallery/qr_codes.html" target="_blank">Google Charts API</a>.
	<br />
	QR Codes are popular among smartphone user, where with help of QR Code you are able to easily access or save url specified in that code.
</p>
<h3>How do I use it?</h3>
<p>
	Just insert the tag into your template/page like:
	<code>
		{qr_image url='http://www.domain.com' widht='120' height='120' title='some title' alt='alt text'}
	</code>
	<br />
</p>
<h3>What parameters does it take?</h3>
<p>
</p>
<ul>
	<li>
		<em>
		<strong>(required)</strong>
		</em>
		<tt>
			url
		</tt> - specify url to generate QR code from.
		<br />
		If using it with News or CGBlog module you could simply user url=$entry->url for example.
	</li>
	<li>
		<em>
		<em>(optional)</em>
		</em>
		<tt>
			width
		</tt> - is optional, default setting is 100px.
	</li>
	<li>
		<em>(optional)</em>
		<tt>
			height
		</tt> - Sis optional, default setting is 100px.
	</li>
	<li>
		<em>(optional)</em>
		<tt>
			path
		</tt> -  is optional. You can specify a path where QR images should be saved.
		<br />
		.
		This path is relative to /uploads/images.
	</li>
	<li>
		<em>(optional)</em>
		<tt>
			alt
		</tt> - alt tag for generated QR image.
	</li>
	<li>
		<em>(optional)</em>
		<tt>
			title
		</tt> - title tag for generated QR image.
	</li>
	<li>
		<em>(optional)</em>
		<tt>
			data_uri
		</tt> - default is disabled. Setting data_uri='1' will generate a base64 encoded image (usefull if you want to reduce http requests.)
		<br />
		Please note that older browser do not support base64 encoded images.
	</li>
</ul>

<?php
}
function smarty_cms_about_function_qr_image() {
?>
<p>
	Author: Goran Ilic
	<a href="http://www.ich-mach-das.at" target="_blank">www.ich-mach-das.at</a>
</p>
<p>
	Version: 1.0
</p>
<?php
}
?>