FundacionLQDVI_WebCongresos/www/administrator/components/com_sql2excel/models/sqlexceldatabase.php

319 lines
6.9 KiB
PHP

<?php
/*
* @component SQL 2 Excel Component
* @copyright Copyright (C) Joomla-R-Us, joomla-r-us.com
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
*/
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.model');
class Sql2excelModelSqlexceldatabase extends JModel
{
var $_dbID;
var $_dbData;
/**
* Constructor that retrieves the ID from the request
*/
function __construct()
{
parent::__construct();
$array = JRequest::getVar('cid', 0, '', 'array');
$this->setId((int)$array[0]);
}
function setId($id)
{
// Set id and wipe data
$this->_dbID = $id;
$this->_dbData = null;
}
/**
* Method to get data
* @return object with data
*/
function &getData()
{
// Load the data
if (empty( $this->_dbData )) {
$query = ' SELECT * FROM #__sql2excel_databases ' .
' WHERE id = '.$this->_dbID;
$this->_db->setQuery( $query );
$this->_dbData = $this->_db->loadObject();
}
if (!$this->_dbData) {
$sql2excel = new stdClass();
$sql2excel->id = 0;
$sql2excel->db_name = null;
$sql2excel->db_type = null;
$sql2excel->db_host = null;
$sql2excel->db_username = null;
$sql2excel->db_password = null;
$sql2excel->db_database = null;
$sql2excel->db_prefix = null;
$this->_dbData = $sql2excel;
}
return $this->_dbData;
}
/**
* Method to check if a database is checked out or not
*
* @access public
* @return boolean True if it is checked out
*/
function isCheckedOut( $uid=0 ) {
if ($this->_loadData()) {
if ($uid) {
return ($this->_dbData->checked_out && $this->_dbData->checked_out != $uid);
} else {
return $this->_dbData->checked_out;
}
}
}
/**
* Method to checkin a database
*
* @access public
* @return boolean True on success
*/
function checkin() {
return true;
}
/**
* Method to checkout a database
*
* @access public
* @return boolean True on success
*/
function checkout($uid = null) {
return true;
}
/**
* Method to checkout a database
*
* @access public
* @return boolean True on success
*/
function fixDatabaseTable() {
$db = & JFactory::getDBO();
$query = 'SHOW COLUMNS FROM #__sql2excel_databases';
$db->setQuery( $query );
$cols = $db->loadObjectList();
foreach($cols as $col) {
if ( $col->Field == 'db_prefix' && $col->Type != 'varchar(255)' ) {
// Alter the db_prefix column
$query = "ALTER TABLE #__sql2excel_databases CHANGE db_prefix db_prefix varchar(255) NOT NULL default ''";
$db->setQuery( $query );
$db->Query();
}
}
// Fix the db_prefix field
return true;
}
/**
* Method to get list of supported database types
* @return select list (html)
*/
function getDatabaseTypes($name, $selected)
{
// Supported colors for Excel Writer
$mydb[] = JHTML::_('select.option', '2', 'MySQL');
$mydb[] = JHTML::_('select.option', '4', 'MSSQL');
$mydb[] = JHTML::_('select.option', '3', 'Oracle');
$mydb[] = JHTML::_('select.option', '5', 'PostgreSQL');
$mydb[] = JHTML::_('select.option', '6', 'Custom');
$mydbOpt = JHTML::_( 'select.genericlist',
$mydb,
$name,
'class="inputbox" size="1" onChange="dbtypeChange(this);"' ,
'value',
'text',
$selected );
return $mydbOpt;
}
/**
* Method to move database up or down
*
* @access public
* @return boolean True on success
*/
function move($direction) {
$row =& $this->getTable();
if (!$row->load($this->_dbID)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
if (!$row->move( $direction, ' published >= 0 ' )) {
$this->setError($this->_db->getErrorMsg());
return false;
}
return true;
}
/**
* Method to reorder the database
*
* @access public
* @return void
*/
function saveorder($cid = array(), $order) {
$row =& $this->getTable();
$total = count( $cid );
// update ordering values
for( $i=0; $i < $total; $i++ )
{
$row->load( (int) $cid[$i] );
if ($row->ordering != $order[$i]) {
$row->ordering = $order[$i];
if (!$row->store()) {
JError::raiseError(500, $db->getErrorMsg() );
}
}
}
$row->reorder( );
}
/**
* Method to store a record
*
* @access public
* @return boolean True on success
*/
function store($data)
{
$row =& $this->getTable();
// Bind the form fields to the #__sql2excel_databases 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 web link table to the database
if (!$row->store()) {
$this->setError( $row->getErrorMsg() );
return false;
}
return $row->id;
}
/**
* Method to update item access
*
* @access public
* @return boolean True on success
*/
function accessmenu($id, $access)
{
global $mainframe;
$row =& $this->getTable();
$row->load($id);
$row->id = $id;
$row->access = $access;
if ( !$row->check() ) {
$this->setError($this->_db->getErrorMsg());
return false;
}
if ( !$row->store() ) {
$this->setError($this->_db->getErrorMsg());
return false;
}
return true;
}
/**
* Method to delete record(s)
*
* @access public
* @return empty string on success, otherwise error message
*/
function delete()
{
$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
$row =& $this->getTable();
if (count( $cids )) {
// Check that we are not trying to delete Joomla! database!
foreach($cids as $cid) {
if ( $cid == 1 ) {
return JText::_('DB_JOOMLA_ERR_MSG');
}
}
$db = & JFactory::getDBO();
$errMsg = '';
if (count( $cids )) {
foreach($cids as $cid) {
// Check that there are no worksheets using this database
$query = 'SELECT COUNT(*) AS cnt ' .
' FROM #__sql2excel_worksheets ' .
' WHERE `database`=' . $cid;
$db->setQuery($query);
$dbCount = $db->loadResult();
if ( $dbCount > 0 ) {
$errMsg .= JText::_('DB_DELETE_ERR_MSG') . ' : ' . $cid;
} else {
if (!$row->delete( $cid )) {
$this->setError( $row->getErrorMsg() );
return false;
}
}
}
}
return $errMsg;
}
}
/**
* Method to retrieve current database record
*
* @access Private
* @return boolean True on success
*/
function _getData() {
if (empty($this->_dbData)) {
$query = 'SELECT p.* '.
' FROM #__sql2excel_databases' .
' WHERE id = ' . (int) $this->_dbID;
$this->_db->setQuery($query);
$this->_dbData = $this->_db->loadObject();
return (boolean) $this->_dbData;
}
return true;
}
}