Your IP : 216.73.216.55


Current Path : /home/giteleslfp/www/administrator/components/com_templateck/models/
Upload File :
Current File : /home/giteleslfp/www/administrator/components/com_templateck/models/font.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 to this file
defined('TCK_LOADED') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.application.component.modeladmin');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');

class TemplateckModelFont extends JModelAdmin {

	/**
	 * path to the fonts foler
	 *
	 * @var string
	 */
	var $_path;

	/**
	 * Theme object
	 *
	 * @var object
	 */
	var $_data;

	/**
	 * Constructor that retrieves the name from the request
	 *
	 * @access	public
	 * @return	void
	 */
	function __construct() {
		parent::__construct();

		$this->_path = TEMPLATECREATORCK_PATH . '/fonts';
		$this->_data = null;
	}

	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param	type	The table type to instantiate
	 * @param	string	A prefix for the table class name. Optional.
	 * @param	array	Configuration array for model. Optional.
	 * @return	JTable	A database object
	 * @since	1.6
	 */
	public function getTable($type = 'Font', $prefix = 'Table', $config = array()) {
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to get the record form.
	 *
	 * @param	array	$data		An optional array of data for the form to interogate.
	 * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.
	 * @return	JForm	A JForm object on success, false on failure
	 * @since	1.6
	 */
	public function getForm($data = array(), $loadData = true) {
		// Initialise variables.
		$app = JFactory::getApplication();

		// Get the form.
		$form = $this->loadForm('com_templateck.font', 'font', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return	mixed	The data for the form.
	 * @since	1.6
	 */
	protected function loadFormData() {
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState('com_templateck.edit.font.data', array());

		if (empty($data)) {
			$data = $this->getItem();
		}

		return $data;
	}

	/**
	 * Method to get a single record.
	 *
	 * @param	integer	The id of the primary key.
	 *
	 * @return	mixed	Object on success, false on failure.
	 * @since	1.6
	 */
	public function getItem($pk = null) {

		if ($pk == null) {
			$input = new TCK_Input();
			$input = $input->get('id', '', 'array');
			$pk = isset($input[0]) ? (int) $input[0] : null;
			$this->setState('font.id', $pk);
		}
		if ($item = parent::getItem($pk)) {

			//Do any procesing on fields here if needed
		}

		return $item;
	}
	/*public function getItem() {
		$input = JFactory::getApplication()->input;
		$id = $input->get('id', 0, 'int');
		$db = JFactory::getDBO();
		$query = "SELECT * FROM #__templateck_fonts WHERE id=" . (int)$id;
		$db->setQuery($query);
		$item = $db->loadObject();
		
		return $item;
	}*/
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store($name, $styles, $fontfamilies) {

		$row = $this->getTable();
		$data['name'] = $name;
		$data['styles'] = $styles;
		$data['fontfamilies'] = $fontfamilies;
		$additional_fonts = JFactory::getApplication()->input->get('additional_fonts', '', 'string');
		$data['additional_fonts'] = $additional_fonts;

		// Bind the form fields to the table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the table to the database
		if (!$row->store()) {
			$this->setError($row->getErrorMsg());
			return false;
		}

		return true;
	}

	/**
	 * Method to delete record(s)
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function delete(&$pks) {
		$app = JFactory::getApplication();
		$cids = $app->input->get('cid', array(0), 'post', 'array');
		$destpath = $this->_path;

		$row = $this->getTable();
		if (count($cids)) {
			foreach ($cids as $cid) {
				$query = ' SELECT name '
						. ' FROM #__templateck_fonts WHERE id=' . $cid;

				// retrieves the data
				$this->_db->setQuery($query);
				$fontname = $this->_db->loadResult();

				// delete the records from the db
				if (!$row->delete($cid)) {
					$this->setError($row->getErrorMsg());
					return false;
				}

				// delete the folder
				if ($fontname && TCK_Folder::exists($destpath . '/' . $fontname)) {
					if (!TCK_Folder::delete($destpath . '/' . $fontname)) {
						$app->enqueueMessage('CK_ERROR_DELETING_FONT_FOLDER', 'warning');
						// $this->setError('CK_ERROR_DELETING_FONT', 'warning');
						return false;
					}
				}
			}
		}
		return true;
	}

	/**
	 * Method to install a font squirrel
	 *
	 * @access	public
	 * @return	true on success
	 */
	function installFontsquirrel() {
		$app = JFactory::getApplication();
		$fileInput = new TCK_Input($_FILES);
		$file = $fileInput->get('file', null, 'array');

		if (is_array($file)) {

			// include file and zip archive libraries
			jimport('joomla.filesystem.file');
			// require_once JPATH_LIBRARIES . '/' . 'joomla' . '/' . 'archive' . '/' . 'zip.php';
			require_once TEMPLATECREATORCK_PATH . '/helpers/zip.php';

			//Clean up filename to get rid of strange characters like spaces etc
//			$file = $app->input->files->get('file', '', 'array');
			$filename = TCK_File::makeSafe($file['name']);

			// declare some variables
			$destpath = $this->_path . '/' . TCK_File::stripExt($filename);

			// check if the theme already exists
			if (TCK_Folder::exists($destpath)) {
				$msg = TCK_Text::_('CK_INSTALL_FONT_ALREADY_EXISTS');
				$app->redirect("index.php?option=com_templateck&view=fonts&layout=install", $msg, 'error');
				return false;
			}

			//Set up the source and destination of the file
			$src = $file['tmp_name'];

			//First check if the file has the right extension, we need jpg only
			if (strtolower(TCK_File::getExt($filename)) != 'zip') {
				$msg = TCK_Text::_('CK_INSTALL_FONT_MUST_BE_ZIP');
				$app->redirect("index.php?option=com_templateck&view=fonts&layout=install", $msg, 'error');
				return false;
			}

			// extract the files
			$archiver = new CKArchiveZip();
			if (!$isSuccess = $archiver->extract($src, $destpath)) {
				$msg = TCK_Text::_('CK_INSTALL_FONT_EXTRACT_ERROR');
				$app->redirect("index.php?option=com_templateck&view=fonts&layout=install", $msg, 'error');
				return false;
			}

			// get the content of the file styles.css
			if (!$stylescontent = file_get_contents($destpath . '/stylesheet.css')) {
				// delete the folder
				if (!TCK_Folder::delete($destpath)) {
					$this->setError('CK_ERROR_DELETING_FONT');
					return false;
				}

				$msg = TCK_Text::_('CK_GET_STYLESCONTENT_FAILED');
				$app->redirect("index.php?option=com_templateck&view=fonts&layout=install", $msg, 'error');
				return false;
			}

			$regex = "#font-family: '(.*?)';#s"; // masque de recherche
			preg_match_all($regex, $stylescontent, $fontfamilies);
			foreach ($fontfamilies[1] as $fontfamily) {
				$stylescontent = str_replace($fontfamily, strtolower($fontfamily), $stylescontent);
			}

			// store in db
			$this->store(TCK_File::stripExt($filename), $stylescontent, strtolower(implode(",", $fontfamilies[1])));
		}
		return true;
	}

	/**
	 * Method to install a font google
	 *
	 * @access	public
	 * @return	true on success
	 */
	function installFontgoogle() {
		$fonturl = JFactory::getApplication()->input->get('fonturl', '', 'string');

		$fontfamily = str_replace('https://fonts.googleapis.com/css?family=', '', $fonturl);
		$fontfamily = str_replace('http://fonts.googleapis.com/css?family=', '', $fontfamily);
		$fontfamily = str_replace('//fonts.googleapis.com/css?family=', '', $fontfamily);
		$fontname = str_replace('+', ' ', $fontfamily);
		$fontname = preg_replace('#(.*?):(.*)#s', '$1', $fontname);
//		$fontfamilies = $fontname;
		$stylescontent = "@import url(" . $fonturl . ");\n";

		return $this->store($fontname, $stylescontent, $fontname);
	}

}