Your IP : 216.73.216.55


Current Path : /home/giteleslfp/www/administrator/components/com_templateck/helpers/
Upload File :
Current File : /home/giteleslfp/www/administrator/components/com_templateck/helpers/helper.php

<?php

/**
 * @name		Template Creator CK
 * @copyright	Copyright (C) since 2011. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 * @author		Cedric Keiflin - http://www.template-creator.com - http://www.joomlack.fr
 */
// No direct access
defined('TCK_LOADED') or die;

/**
 * Templateck helper.
 */
class TemplateckHelper {

	/**
	 * Configure the Linkbar.
	 */
	public static function addSidebar($vName = '') {
		$input = new TCK_Input();
		if (!$vName) $vName = $input->get('view', 'templates', 'cmd');
		$doc = JFactory::getDocument();
		// load jQuery for joomla 2.5
		if (version_compare(JVERSION, '3.0.0') < 0) {
			$doc->addScript(JUri::base(true) . '/components/com_templateck/assets/jquery.min.js');
			$doc->addScript(JUri::base(true) . '/components/com_templateck/assets/jquery-noconflict.js');
		}

		JHtmlSidebar::addEntry(
				TCK_Text::_('COM_TEMPLATECK_TITLE_TEMPLATES'), 'index.php?option=com_templateck&view=templates', $vName == 'templates'
		);
		JHtmlSidebar::addEntry(
				TCK_Text::_('CK_SUBMENU_FONTS'), 'index.php?option=com_templateck&view=fonts', $vName == 'fonts'
		);
		JHtmlSidebar::addEntry(
				TCK_Text::_('CK_ABOUT') . '<span class="templateckchecking"></span>', 'index.php?option=com_templateck&view=about', $vName == 'about'
		);
		JHtmlSidebar::addEntry(
				TCK_Text::_('CK_HELP'), 'index.php?option=com_templateck&view=help', $vName == 'help'
		);
		echo '<div class="ckadminsidebar">' . JHtmlSidebar::render() . '</div>';
	
		// add the ajax method to check for an update
		$js_checking = 'jQuery(document).ready(function (){
				jQuery(\'.templateckchecking\').each(function(i ,el){
					jQuery.ajax({
						type: "POST",
						url: \'' . JUri::root(true) . '/administrator/index.php?option=com_templateck&task=check_update\',
						data: {
	}
					}).done(function(response) {
						response = response.trim();
						if ( response.substring(0,7).toLowerCase() == \'error\' ) {
							// alert(response);
							// show_ckmodal(response);
						} else {
							jQuery(el).append(response);
	}
					}).fail(function() {
						// alert(Joomla.TCK_Text._(\'CK_FAILED\', \'Failed\'));
					});
				});
			});';
		$doc->addScriptDeclaration($js_checking);
		}

	/**
	 * Gets a list of the actions that can be performed.
	 *
	 * @return	JObject
	 * @since	1.6
	 */
	public static function getActions() {
		$user = JFactory::getUser();
		$result = new JObject;

		$assetName = 'com_templateck';

		$actions = array(
			'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.own', 'core.edit.state', 'core.delete'
		);

		foreach ($actions as $action) {
			$result->set($action, $user->authorise($action, $assetName));
		}

		return $result;
	}

	/**
	 * Test if there is already a unit, else add the px
	 *
	 * @param string $value
	 * @return string
	 */
	public static function testUnit($value, $defaultunit = "px") {

		if ((stristr($value, 'px')) 
				OR (stristr($value, 'em')) 
				OR (stristr($value, '%')) 
				OR (stristr($value, 'rem')) 
				OR (stristr($value, 'vh')) 
				OR (stristr($value, 'vw')) 
				OR $value == 'auto'
				)
			return $value;

		return $value . $defaultunit;
	}

	/**
	 * Check the token for security reason
	 * @return boolean
	 */
	public static function checkAjaxToken() {
		if (! JSession::checkToken('get')) {
			$msg = TCK_Text::_('CK_INVALID_TOKEN');
			echo '{"status": "0", "message": "' . $msg . '"}';
			exit();
		}
		return true;
	}

	/*
	 * Load the JS and CSS files needed to use CKBox
	 *
	 * Return void
	 */
	public static function loadCkbox() {
		$doc = JFactory::getDocument();
		$doc->addScript(JUri::root(true) . '/media/jui/js/jquery.min.js');
		$doc->addStyleSheet(TEMPLATECREATORCK_MEDIA_URI . '/assets/ckbox.css');
		$doc->addScript(TEMPLATECREATORCK_MEDIA_URI . '/assets/ckbox.js');
	}

	/*
	 * Load the JS and CSS files needed to use CKBox
	 *
	 * Return void
	 */
	public static function loadCKFramework() {
		$doc = JFactory::getDocument();
		$doc->addScript(JUri::root(true) . '/media/jui/js/jquery.min.js');
		$doc->addStyleSheet(TEMPLATECREATORCK_MEDIA_URI . '/assets/ckframework.css');
	}

	/*
	 * Load the JS and CSS files needed to use CKBox
	 *
	 * Return void
	 */
	/*public static function loadInlineCKFramework() {
	?>
		<script src="<?php echo JUri::root(true) ?>/media/jui/js/jquery.min.js" type="text/javascript"></script>
		<link rel="stylesheet" href="<?php echo TEMPLATECREATORCK_MEDIA_URI ?>/assets/ckframework.css" type="text/css" />
	<?php
	}*/

	/*
	 * Replace the variables to store the file
	 * 
	 * @return string, the json encoded item
	 */
	public static function getExportFile($item) {
		$item->htmlcode = str_replace(TEMPLATECREATORCK_URI_ROOT, "|URIROOT|", $item->htmlcode);
		$exportfiletext = json_encode($item);

		return $exportfiletext;
	}

	/**
	 * Give the file directly for download in the browser
	 * 
	 * @param type $file
	 */
	public static function pushFileForDownload($filepath) {
		$filepath = TEMPLATECREATORCK_PATH . $filepath;
		$filename = basename($filepath);
		header('Content-type: application/zip');
		header("Content-Disposition: attachment; filename=$filename");                             
		header("Content-Length: " . filesize($filepath));

		readfile($filepath);

		exit();
	}

	/**
	 * Utility function to map an array to a stdClass object.
	 *
	 * @param   array    $array      The array to map.
	 * @param   string   $class      Name of the class to create
	 * @param   boolean  $recursive  Convert also any array inside the main array
	 *
	 * @return  object
	 *
	 * @since   1.0
	 */
	public static function toObject(array $array, $class = 'stdClass', $recursive = true)
	{
		$obj = new $class;

		foreach ($array as $k => $v)
		{
			if ($recursive && is_array($v))
			{
				$obj->$k = static::toObject($v, $class);
			}
			else
			{
				$obj->$k = $v;
			}
		}

		return $obj;
	}

	public static function getToken() {
		return JSession::getFormToken();
	}

	public static function checkToken() {
		// Check for request forgeries.
		JSession::checkToken() or jexit(TCK_Text::_('JINVALID_TOKEN'));
	}

	public static function redirect($url) {
		$app = JFactory::getApplication();
		$app->redirect($url);
	}

	/**
	 * Create the list of all modules published as Object
	 *
	 * $file string the image path
	 * $x integer the new image width
	 * $y integer the new image height
	 *
	 * @return Boolean True on Success
	 */
	static function resizeImage($file, $x, $y = '', $thumbpath = 'th', $thumbsuffix = '_th') {

		if (!$file)
			return;

		$thumbext = explode(".", $file);
		$thumbext = end($thumbext);
		$thumbfile = str_replace(TCK_File::getName($file), $thumbpath . "/" . TCK_File::getName($file), $file);
		$thumbfile = str_replace("." . $thumbext, $thumbsuffix . "." . $thumbext, $thumbfile);
		
		$filetmp = $file;
		$filetmp = str_replace("%20", " ", $filetmp);

		if (!file_exists($filetmp))
			return;
		$size = getimagesize($filetmp);

		if ($size[0] > $size[1] || ! $y) // paysage
		{
			$y = $x * $size[1] / $size[0];
		} else 
		{
//			$tmpx = $x;
//			$x = $y;
//			$y = $tmpx * $size[0] / $size[1];
			$x = $y * $size[0] / $size[1];
		}

		if ($size) {
			// if (file_exists($thumbfile)) {
				// return $thumbfile;
				// $thumbsize = getimagesize(JPATH_ROOT . '/' . $thumbfile);
				// if ($thumbsize[0] == $x || $thumbsuffix == '') {
					// return $thumbfile;
				// }
			// }
			
			$thumbfolder = str_replace(TCK_File::getName($file), $thumbpath . "/", $filetmp);
			if (!file_exists($thumbfolder)) { 
				TCK_Folder::create($thumbfolder);
				// TCK_File::copy(JPATH_ROOT . '/modules/mod_slideshowck/index.html', $thumbfolder . 'index.html' );
			}

			if ($size['mime'] == 'image/jpeg') {
				$img_big = imagecreatefromjpeg($filetmp); # On ouvre l'image d'origine
				$img_new = imagecreate($x, $y);
				# création de la miniature
				$img_mini = imagecreatetruecolor($x, $y) or $img_mini = imagecreate($x, $y);
				// copie de l'image, avec le redimensionnement.
				imagecopyresized($img_mini, $img_big, 0, 0, 0, 0, $x, $y, $size[0], $size[1]);

				imagejpeg($img_mini, $thumbfile);
			} else if ($size['mime'] == 'image/png') {
				$img_big = imagecreatefrompng($filetmp); # On ouvre l'image d'origine
				$img_new = imagecreate($x, $y);
				# création de la miniature
				$img_mini = imagecreatetruecolor($x, $y) or $img_mini = imagecreate($x, $y);
				// copie de l'image, avec le redimensionnement.
				imagecopyresized($img_mini, $img_big, 0, 0, 0, 0, $x, $y, $size[0], $size[1]);

				imagepng($img_mini, $thumbfile);
			} else if ($size['mime'] == 'image/gif') {
				$img_big = imagecreatefromgif($filetmp); # On ouvre l'image d'origine
				$img_new = imagecreate($x, $y);
				# création de la miniature
				$img_mini = imagecreatetruecolor($x, $y) or $img_mini = imagecreate($x, $y);
				// copie de l'image, avec le redimensionnement.
				imagecopyresized($img_mini, $img_big, 0, 0, 0, 0, $x, $y, $size[0], $size[1]);

				imagegif($img_mini, $thumbfile);
			}
		}

		return $thumbfile;
	}
}