Your IP : 216.73.216.74


Current Path : /home/bonneesp/www/administrator/components/com_tabulizer/models/
Upload File :
Current File : /home/bonneesp/www/administrator/components/com_tabulizer/models/rulesetarchives.php

<?php
/**
 * @version		6.5.0 tabulizer $
 * @package		tabulizer
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author		Dimitrios Mourloukos
 * @author mail	info@alterora.gr
 * @website		www.tabulizer.com
 * 
 */


// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.model' );

TabulizerPath::requireLib('ruleset','admin');

class tabulizerModelRulesetarchives extends JModelLegacy {
	var $ruleset_archives = null;	
	var $css_code = null;
	var $user_preferences = null;
	var $user_permissions = null;
	var $system_status = null;
	var $limit = null;
	var $limitstart = null;	
	var $total_count = null;
	
	function __construct(){
		parent::__construct();		
	}	
	
	function loadRulesetArchives($limit=0, $limitstart=0) {
		$ruleset = new Ruleset();		
		$this->ruleset_archives = $ruleset->getRulesets(null,$limit,$limitstart,$total_count);
		$this->limit = $limit;
		$this->limitstart = $limitstart;
		$this->total_count = $total_count;
		return $this->ruleset_archives;
	}		
	
	function getRulesetArchives() {
		if (empty($this->ruleset_archives)) {
			$this->loadRulesetArchives();
		}
		
		return $this->ruleset_archives;
	}

    function exportRulesetArchives($ids, &$error_msg) {

        $ra_associated_files = array();
        foreach ($ids as $archive_filename) {
            $associated_files = TabulizerRulesetAssociation::loadArchiveFiles($archive_filename);
            if ($associated_files) {
                $ra_associated_files = TabulizerRulesetAssociation::mergeAssociations($ra_associated_files,$associated_files);
            }
        }

        $archive_dir = TabulizerPath::getDirBase('temp');
        $archive_filename = $archive_dir .DIRECTORY_SEPARATOR. 'archive_download.zip';

        $zip = new ZipArchive;
        $return_code = $zip->open($archive_filename,ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
        if ($return_code === TRUE) {
            foreach ($ra_associated_files as $zipdir => $files) {
                if (empty($files)) continue;
                $zip->addEmptyDir($zipdir);
                foreach ($files as $filename => $filepath) {
                    $zip->addFile($filepath, $zipdir .DIRECTORY_SEPARATOR. $filename);
                }
            }
            $zip->close();
        } else {
            $error_desc = TabulizerUtils::getZipArchiveErrorDescription($return_code);
            $error_msg = sprintf(JText::_('COM_TABULIZER_DATA_FILE_ZIP_LIBRARY_MISSING'),$error_desc);
            return false;
        }

        // finally output the temp zip file
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private', false); // required for certain browsers
        header('Content-Type: application/zip');

        header('Content-Disposition: attachment; filename="'. basename($archive_filename) . '";');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($archive_filename));

        ob_clean();
        flush();
        readfile($archive_filename);

        // remove temporary files
        @unlink($archive_filename);

        exit;
    }

	
	function getPagination() {
		$pagination = null;
		if (!empty($this->limit) && !empty($this->total_count)) {
			if ($this->total_count > $this->limit) {
				jimport('joomla.html.pagination');
				$pagination = new JPagination($this->total_count, $this->limitstart, $this->limit );
			}	
		}
		return $pagination;
	}
	
	function getReturnURLs(&$urls) {
        $jinput = JFactory::getApplication()->input;
		$option = $jinput->getCmd('option');
		
		$urls = array();
		$url = str_replace('&amp;','&',JRoute::_('index.php?option='.$option.'&task=viewrulesetarchives&limit='.$this->limit.'&limitstart='.$this->limitstart));
		$urls['archives'] = $url;
	}
	
	function getRulesetConflicts() {
		TabulizerPath::requireLib('ruleset','admin');
		$ruleset = new Ruleset();				
		return $ruleset->getRulesetConflicts();				
	}
	
	function loadUserPreferences(&$error_msg) {				
		$preferences = TabulizerInfo::getUserPreferences($error_msg);
		if ($preferences) {
			$this->user_preferences = $preferences;
			return true;
		} else return false; 
	}
	
	function getUserPreferences() {
		return $this->user_preferences;
	}
	
	function saveUserPreferences(&$error_msg) {
		$db = JFactory::getDbo();
		$user = JFactory::getUser();
        $jinput = JFactory::getApplication()->input;
		
		if ($user->guest) {
			$error_msg = JText::_('COM_TABULIZER_USER_PREFERENCES_NOT_LOGGED_ERROR');
			return false;
		} 
		$user_id = $user->id;
		
		$action = 'system-save';
		$allow_save = (TabulizerPermissions::isAllowed($action))?1:0;
		if ($allow_save) {
			if (!$this->saveSystemPreferences($error_msg)) return false;
		}

        $preferences = TabulizerInfo::getUserPreferences($error_msg, 'none');
		
		// update or insert new preferences entry ?
		$new_entry = true;
		$update_keys = array();				
		
		$query = 'SELECT pref.* FROM #__tabulizer_user_preferences as pref WHERE user_id = '.$user_id;
		$db->setQuery($query);			
		
		$rows = $db->loadObjectList();
		if (!empty($rows)) {
			$new_entry = false;			
			foreach ($rows as $row) {
				if (!empty($row->key)) {
					$preferences[$row->key] = $row->value;
					$update_keys[$row->key] = true;
				}
			}
		}				
		
		$queries = array();	
		foreach ($preferences as $key => $value) {
			$value = $jinput->getString($key,0);
			if ($new_entry || empty($update_keys[$key])) {
				$query = 'INSERT INTO #__tabulizer_user_preferences (`user_id`, `key`, `value`) VALUES ('.$user_id.','.$db->quote($key).','.$db->quote($value).')';
			} else {
				$query = 'UPDATE #__tabulizer_user_preferences SET `value` = '.$db->quote($value).' WHERE `user_id` = '.$user_id .' AND `key` = '. $db->quote($key);
			}
			$queries[] = $query;						
		}												
				
		foreach ($queries as $query) {
            if (!TabulizerDatabase::executeQuery($query,$error_msg,$db)) {
                $error_msg = JText::_('COM_TABULIZER_USER_PREFERENCES_DB_SAVE_ERROR').$error_msg;
                return false;
            }
		}
				
		return true;
	}

	function saveSystemPreferences(&$error_msg) {
		$db = JFactory::getDbo();
		$user = JFactory::getUser();
        $jinput = JFactory::getApplication()->input;
		
		if ($user->guest) {
			$error_msg = JText::_('COM_TABULIZER_USER_PREFERENCES_NOT_LOGGED_ERROR');
			return false;
		} 
		$user_id = 0; // 0 -> System user
		
		$preferences = TabulizerInfo::getUserPreferences($error_msg, 'only');
		
		// update or insert new preferences entry ?
		$new_entry = true;
		$update_keys = array();				
		
		$query = 'SELECT pref.* FROM #__tabulizer_user_preferences as pref WHERE user_id = ' . $user_id;
		$db->setQuery($query);			
		
		$rows = $db->loadObjectList();
		if (!empty($rows)) {
			$new_entry = false;			
			foreach ($rows as $row) {
				if (!empty($row->key)) {
					$preferences[$row->key] = $row->value;
					$update_keys[$row->key] = true;
				}
			}
		}				
		
		$queries = array();	
		foreach ($preferences as $key => $value) {
			$value = $jinput->getString($key,0);
			if ($new_entry || empty($update_keys[$key])) {
				$query = 'INSERT INTO #__tabulizer_user_preferences (`user_id`, `key`, `value`) VALUES ('.$user_id.','.$db->quote($key).','.$db->quote($value).')';
			} else {
				$query = 'UPDATE #__tabulizer_user_preferences SET `value` = '.$db->quote($value).' WHERE `user_id` = '.$user_id .' AND `key` = '. $db->quote($key);
			}
			$queries[] = $query;						
		}												
				
		foreach ($queries as $query) {		
			$db->setQuery($query);
			if (defined('JOOMLA_3_MODE')) {
				if (!$db->execute()) {
					$error_msg = JText::_('COM_TABULIZER_USER_PREFERENCES_DB_SAVE_ERROR');
					return false;
				}
			} else {				
				if (!$db->query()) {
					$error_msg = JText::_('COM_TABULIZER_USER_PREFERENCES_DB_SAVE_ERROR') . $db->getErrorMsg();
					return false;
				}

			}		
		}
				
		return true;
	}	
	
	function loadAccessPermissions(&$error_msg) {
        // placeholder for other platforms
	}
	
	function saveAccessPermissions(&$error_msg) {
        // placeholder for other platforms
	}

    function getAccessPermissions() {
        return $this->user_permissions;
    }
	
	function loadDataSources($limit, $limitstart, &$error_msg) {
        $jinput = JFactory::getApplication()->input;
		$option = $jinput->getCmd('option');
		$db = JFactory::getDbo();				
								
		$query = 'SELECT * FROM #__tabulizer_data_sources LIMIT '.intval($limitstart).', '.intval($limit);
		$db->setQuery($query);
		$rows = $db->loadObjectList();
		if (!empty($rows)) {
			foreach ($rows as $row) {
				if (!empty($row->key)) {
					$data_sources[$row->key] = $row->value;
				}
			}
		} else {
			$error_msg = JText::_('COM_TABULIZER_DATA_SOURCES_DB_LOAD_ERROR');
			return false;
		}				
	
		$this->data_sources = $data_sources;
		return true;	
	}
	
	function getDataSources() {
		return $this->data_sources;
	}
	
	function saveDataSource(&$error_msg) {
		$db = JFactory::getDbo();
		$user = JFactory::getUser();
        $jinput = JFactory::getApplication()->input;
		
		if ($user->guest) {
			$error_msg = JText::_('COM_TABULIZER_DATA_SOURCES_NOT_LOGGED_ERROR');
			return false;
		} 
		$user_id = $user->id;									
		
		$id = $jinput->getInt('ds_id', null);
		$tag = $jinput->getString('ds_tag', null);
		$source_type = $jinput->getString('ds_source_type', null);
		$source_params = $jinput->getString('ds_source_params', null);

		$new_entry = (empty($id))?true:false;
		
		if (empty($tag)||empty($source_type)||empty($source_params)) {
			$error_msg = JText::_('COM_TABULIZER_DATA_SOURCES_INVALID_PARAMS_ERROR');
			return false;
		}
		
		if ($new_entry) {
			$query = 'INSERT INTO #__tabulizer_data_sources (`tag`, `source_type`, `source_params`) VALUES ('.$db->quote($tag).','.$db->quote($source_type).','.$db->quote($source_params).')';
		} else {
			$query = 'UPDATE #__tabulizer_data_sources SET `tag` = '.$db->quote($tag).', `source_type`='.$db->quote($source_type).', `source_params`='.$db->quote($source_params);
		}
				
		$db->setQuery($query);
		if (defined('JOOMLA_3_MODE')) {
			if (!$db->execute()) {
				$error_msg = JText::_('COM_TABULIZER_DATA_SOURCES_DB_SAVE_ERROR') . $db->getErrorMsg();
				return false;
			}
		} else {				
			if (!$db->query()) {
				$error_msg = JText::_('COM_TABULIZER_DATA_SOURCES_DB_SAVE_ERROR') . $db->getErrorMsg();
				return false;
			}
		}		
				
		return true;
	}
	
	function loadStatus() {
		TabulizerInfo::getSystemStatus($this->system_status);				
	}
	
	function getStatus() {
		return $this->system_status;
	}
	
	function exportRulesetArchivesSkeletonCSSCode($export_archive_filenames = array()) {
        /* obsolete code */
		return $this->css_code;
	}
	
	function getRulesetArchivesSkeletonCSSCode() {		
		return $this->css_code;
	}
		
}
?>