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

730 lines
20 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');
require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'sql2excel.php' );
class Sql2excelModelSqlexcelschedule extends JModel
{
var $_schedID;
var $_schedData;
/**
* 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->_schedID = $id;
$this->_schedData = null;
}
/**
* Method to get list of Sections
* @return select list (html)
*/
function getSectionOptions($selected)
{
// Get list of sections
$db = & JFactory::getDBO();
$query = ' SELECT ID AS value, TITLE AS text ' .
' FROM #__sql2excel_sections ' .
' ORDER BY TITLE';
$this->_db->setQuery( $query );
$mysections[] = JHTML::_('select.option', '0', '- '.JText::_('Please Select').' -');
$sections = array_merge($mysections, $db->loadObjectList());
$sectionsOpt = JHTML::_( 'select.genericlist',
$sections,
'section',
'class="inputbox" size="1" onchange="changeDynaList( \'category\', categoryArray, document.adminForm.section.options[document.adminForm.section.selectedIndex].value, 0, 0);"',
'value',
'text',
$selected );
return $sectionsOpt;
}
/**
* Method to get list of Categories
* @return select list (html)
*/
function getCategoryOptions($sectionid, $catid)
{
// Get list of categories for the selected section
$mycategories[] = JHTML::_('select.option', '0', '- '.JText::_('Please Select').' -');
if ( $sectionid > 0 ) {
$db = & JFactory::getDBO();
$query = ' SELECT ID AS value, TITLE AS text ' .
' FROM #__sql2excel_categories ' .
' WHERE section=' . $sectionid .
' ORDER BY TITLE';
$this->_db->setQuery( $query );
$categories = array_merge($mycategories, $db->loadObjectList());
} else { $categories = $mycategories; }
$categoryOpt = JHTML::_( 'select.genericlist',
$categories,
'category',
'class="inputbox" size="1"' ,
'value',
'text',
$catid );
return $categoryOpt;
}
/**
* Method to get Javascript array with all Categories vs Sections
* @return Javascript snippet
*/
function getCategoryArray()
{
// Get list of categories for the selected section
$mycategories = "var categoryArray = new Array;\ncategoryArray[0] = new Array( '0','0','- Please Select -' );";
$db = & JFactory::getDBO();
$query = ' SELECT ID, TITLE, SECTION ' .
' FROM #__sql2excel_categories ' .
' ORDER BY SECTION, TITLE';
$this->_db->setQuery( $query );
$categories = $db->loadObjectList();
$i=1;
foreach ( $categories as $cat ) {
$mycategories = $mycategories . "\ncategoryArray[" . $i . "]=new Array('" . $cat->SECTION . "','" . $cat->ID . "','" . str_replace("'", '',$cat->TITLE) . "');";
$i++;
}
return $mycategories;
}
/**
* Method to get Javascript array with all Workbook Names
* @return Javascript snippet
*/
function getWBNameArray()
{
// Get Sheetnames for all Worksheets
$mysheets = "var sheetNames = new Array;";
$db = & JFactory::getDBO();
$query = ' SELECT id, title ' .
' FROM #__sql2excel_workbooks ' .
' ORDER BY ID';
$this->_db->setQuery( $query );
$sheets = $db->loadObjectList();
foreach ( $sheets as $sheet ) {
$mysheets .= "\nsheetNames[" . $sheet->id . "]=\"" . str_replace('"', '&quote;',$sheet->title) . "\";";
}
return $mysheets;
}
/**
* Method to get list of Workbooks
* @return select list (html)
*/
function getWorkbookList($id, $width)
{
if ( $width == '' ) { $width = 300; };
// Get list of workbooks
$db = & JFactory::getDBO();
if ( $id == 0 ) {
$query = " SELECT ID AS value, CONCAT(ID, ' : ', TITLE, ' : ', link_title ) AS text " .
' FROM #__sql2excel_workbooks ' .
' ORDER BY ID';
} else {
$query = " SELECT ID AS value, CONCAT(ID, ' : ', TITLE, ' : ', link_title ) AS text " .
' FROM #__sql2excel_workbooks ' .
' WHERE ID NOT IN ( SELECT wb_id AS ID
FROM #__sql2excel_schedule2book
WHERE sched_id=' . $id . ') ' .
' ORDER BY ID';
}
$this->_db->setQuery( $query );
$workSheets = JHTML::_( 'select.genericlist',
$db->loadObjectList(),
'available_worksheets',
'class="inputbox" style="width: ' . $width . 'px" size="20" onDblClick="addWS();"',
'value',
'text');
return $workSheets;
}
/**
* Method to get list of Workbooks for a schedule
* @return select list (html)
*/
function getSelWorkbookList($currSchedID, $width)
{
if ( $width == '' ) { $width = 300; };
// Get list of sections
$db = & JFactory::getDBO();
$query = " SELECT a.ID AS value, CONCAT(a.ID, ' : ', a.TITLE, ' : ', a.link_title ) AS text " .
' FROM #__sql2excel_workbooks a, #__sql2excel_schedule2book b' .
' WHERE a.id = b.wb_id AND b.sched_id=' . $currSchedID .
' ORDER BY b.ordering';
$this->_db->setQuery( $query );
$workBooks = JHTML::_( 'select.genericlist',
$db->loadObjectList(),
'selected_worksheets',
'class="inputbox" style="width: ' . $width . 'px" size="20" onDblClick="remWS();"',
'value',
'text');
return $workBooks;
}
/**
* Method to get list of available Schedule Types
* @return select list (html)
*/
function getScheduleTypes($name, $selected)
{
if ( $selected == '' || $selected < 10 ) { $selected = 30; }
// Supported colors for Excel Writer
$myopts[] = JHTML::_('select.option', '10', 'Hourly');
$myopts[] = JHTML::_('select.option', '20', 'Daily');
$myopts[] = JHTML::_('select.option', '30', 'Weekly');
$myopts[] = JHTML::_('select.option', '40', 'Monthly');
$myopts[] = JHTML::_('select.option', '50', 'Yearly');
$typesOpt = JHTML::_( 'select.genericlist',
$myopts,
$name,
'class="inputbox" size="1"' ,
'value',
'text',
$selected );
return $typesOpt;
}
/**
* Method to get Run-If Modes
* @return select list (html)
*/
function getRunIfMode($name, $selected)
{
// Supported colors for Excel Writer
$myopts[] = JHTML::_('select.option', '0', 'Disabled');
$myopts[] = JHTML::_('select.option', '1', 'SQL query returns one or more rows');
$myopts[] = JHTML::_('select.option', '2', 'SQL query returns no rows');
$myopts[] = JHTML::_('select.option', '3', 'SQL query returns X rows');
$myopts[] = JHTML::_('select.option', '9', 'SQL query does not return X rows');
$myopts[] = JHTML::_('select.option', '13', 'SQL query returns &gt;X rows');
$myopts[] = JHTML::_('select.option', '14', 'SQL query returns &gt;=X rows');
$myopts[] = JHTML::_('select.option', '15', 'SQL query returns &lt;X rows');
$myopts[] = JHTML::_('select.option', '16', 'SQL query returns &lt;=X rows');
$myopts[] = JHTML::_('select.option', '4', 'SQL query returns between X and Y rows');
$myopts[] = JHTML::_('select.option', '10', 'SQL query does not return between X and Y rows');
$myopts[] = JHTML::_('select.option', '5', 'SQL query returns value X');
$myopts[] = JHTML::_('select.option', '11', 'SQL query does not return value X');
$myopts[] = JHTML::_('select.option', '17', 'SQL query returns value &gt;X');
$myopts[] = JHTML::_('select.option', '18', 'SQL query returns value &gt;=X');
$myopts[] = JHTML::_('select.option', '19', 'SQL query returns value &lt;X');
$myopts[] = JHTML::_('select.option', '20', 'SQL query returns value &lt;=X');
$myopts[] = JHTML::_('select.option', '6', 'SQL query returns value between X and Y');
$myopts[] = JHTML::_('select.option', '12', 'SQL query does not return value between X and Y');
$myopts[] = JHTML::_('select.option', '7', 'SQL query generates error');
$myopts[] = JHTML::_('select.option', '8', 'SQL query does not generate error');
$typesOpt = JHTML::_( 'select.genericlist',
$myopts,
$name,
'class="inputbox" size="1" onChange="runifModeChange(this);"' ,
'value',
'text',
$selected );
return $typesOpt;
}
/**
* Method to get data
* @return object with data
*/
function &getData()
{
// Load the data
if (empty( $this->_schedData )) {
$query = ' SELECT * FROM #__sql2excel_schedules ' .
' WHERE id = '.$this->_schedID;
$this->_db->setQuery( $query );
$this->_schedData = $this->_db->loadObject();
}
if (!$this->_schedData) {
$sql2excel = new stdClass();
$sql2excel->id = 0;
$sql2excel->frequency = 30;
$sql2excel->title = null;
$sql2excel->next_date = null;
$sql2excel->end_date = null;
$sql2excel->count = 0;
$sql2excel->max_count = null;
$sql2excel->action = null;
$sql2excel->email_to = null;
$sql2excel->email_cc = null;
$sql2excel->email_bcc = null;
$sql2excel->email_title = null;
$sql2excel->email_body = null;
$sql2excel->email_attach = 1;
$sql2excel->html = 1;
$sql2excel->sdata = null;
$sql2excel->sql_db = 1;
$sql2excel->published = 1;
$sql2excel->ordering = 0;
$sql2excel->failures = 0;
$sql2excel->last_status = null;
$sql2excel->created = null;
$sql2excel->compress_attachments = 0;
$sql2excel->run_if_type = 0;
$sql2excel->run_if_sql = null;
$sql2excel->run_if_db = 1;
$sql2excel->run_if_op = null;
$sql2excel->run_if_val_1 = null;
$sql2excel->run_if_val_2 = null;
$sql2excel->on_success_sql = null;
$sql2excel->on_success_db = 1;
$sql2excel->on_success_email = null;
$sql2excel->on_failure_sql = null;
$sql2excel->on_failure_db = 1;
$sql2excel->on_failure_email = null;
$this->_schedData = $sql2excel;
}
return $this->_schedData;
}
/**
* Method to get list of Databases
* @return object with data
*/
function getDatabases($name, $selected)
{
// Get list of databases
$db = & JFactory::getDBO();
$query = ' SELECT ID AS value, DB_NAME AS text ' .
' FROM #__sql2excel_databases ' .
' ORDER BY ID';
$this->_db->setQuery( $query );
$dbList =$db->loadObjectList();
$databaseOpt = JHTML::_( 'select.genericlist',
$dbList,
$name,
'class="inputbox" size="1"',
'value',
'text',
$selected );
return $databaseOpt;
}
/**
* Method to check if a schedule 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->_schedData->checked_out && $this->_schedData->checked_out != $uid);
} else {
return $this->_schedData->checked_out;
}
}
}
/**
* Method to checkin a schedule
*
* @access public
* @return boolean True on success
*/
function checkin() {
if ($this->_schedID ) {
$cattable = & $this->getTable();
if(! $cattable->checkin($this->_schedID)) {
$this->setError('<p>Checkin failed!</p><p>' . $this->_db->getErrorMsg());
return false;
}
}
return false;
}
/**
* Method to checkout a schedule
*
* @access public
* @return boolean True on success
*/
function checkout($uid = null) {
if ($this->_schedID )
{
// Make sure we have a user id to checkout the article with
if (is_null($uid)) {
$user =& JFactory::getUser();
$uid = $user->get('id');
}
// Lets get to it and checkout the thing...
$cattable = & $this->getTable();
if(!$cattable->checkout($uid, $this->_schedID)) {
$this->setError('<p>Checkout failed!</p><p>' . $this->_db->getErrorMsg() );
return false;
}
return true;
}
return false;
}
/**
* Method to move schedule up or down
*
* @access public
* @return boolean True on success
*/
function move($direction) {
$row =& $this->getTable();
if (!$row->load($this->_schedID)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
if (!$row->move( $direction, ' published >= 0 ' )) {
$this->setError($this->_db->getErrorMsg());
return false;
}
return true;
}
/**
* Method to get list of schedule Cache types
* @return select list (html)
*/
function getCacheOptions($name, $selected)
{
$mytypes[] = JHTML::_('select.option', 'Global', JText::_( 'Use Global'));
$mytypes[] = JHTML::_('select.option', 'Yes', JText::_( 'Yes'));
$mytypes[] = JHTML::_('select.option', 'No', JText::_( 'No'));
$typesOpt = JHTML::_( 'select.genericlist',
$mytypes,
$name,
'class="inputbox" size="1"',
'value',
'text',
$selected );
return $typesOpt;
}
/**
* Method to get list of action types
* @return select list (html)
*/
function getActionTypes($name, $selected)
{
$mytypes[] = JHTML::_('select.option', '0', JText::_( '-- Please Select --'));
$mytypes[] = JHTML::_('select.option', '20', JText::_( 'Update Cache for All Workbooks'));
$mytypes[] = JHTML::_('select.option', '10', JText::_( 'Update Cache for Selected Workbooks'));
$mytypes[] = JHTML::_('select.option', '25', JText::_( 'Send Email'));
$mytypes[] = JHTML::_('select.option', '30', JText::_( 'Send Email with Links to Workbooks'));
$mytypes[] = JHTML::_('select.option', '40', JText::_( 'Send Email with Attached Workbooks'));
$mytypes[] = JHTML::_('select.option', '50', JText::_( 'Run SQL'));
//$mytypes[] = JHTML::_('select.option', '60', JText::_( 'Run PHP code'));
$typesOpt = JHTML::_( 'select.genericlist',
$mytypes,
$name,
'class="inputbox" size="1" onChange="schedActionChange(this);"' ,
'value',
'text',
$selected );
return $typesOpt;
}
/**
* Method to get list of email types
* @return select list (html)
*/
function getEmailTypes($name, $selected)
{
if ( $selected == '' ) { $selected=1; }
$mytypes[] = JHTML::_('select.option', '0', JText::_( 'Text'));
$mytypes[] = JHTML::_('select.option', '1', JText::_( 'HTML'));
$typesOpt = JHTML::_( 'select.genericlist',
$mytypes,
$name,
'class="inputbox" size="1" onChange="schedEmailChange(this);"' ,
'value',
'text',
$selected );
return $typesOpt;
}
/**
* Method to copy schedule(s)
*
* @access public
* @return boolean True on success
*/
function copy($cid = array())
{
if (count( $cid ))
{
$db = & JFactory::getDBO();
JArrayHelper::toInteger($cid);
foreach ( $cid as $wbID ) {
$query = 'INSERT INTO #__sql2excel_schedules '
. '(`title`,`frequency`,`next_date`,`end_date`,`count`,`max_count`,`action`,`email_to`, `email_cc`, `email_bcc`,`email_title`, `email_body`,`email_attach`, `html`, `sdata`, `published`, `ordering`, `created` ) '
. 'SELECT CONCAT(`title`, \' - Copy\') AS `Title`,`frequency`,`next_date`,`end_date`,`count`,`max_count`,`action`,`email_to`, `email_cc`, `email_bcc`,`email_title`, `email_body`,`email_attach`, `html`, `sdata`, 0 AS `published`, `ordering`, now() AS `created` '
. 'FROM #__sql2excel_schedules '
. 'WHERE ID=' . $wbID;
$db->setQuery( $query );
if (!$db->query()) {
$this->setError($db->getErrorMsg());
return false;
} else {
$newid = $db->insertid();
$query = 'INSERT INTO #__sql2excel_schedule2book '
. '(`sched_id`,`wb_id`,`ordering`) '
. 'SELECT \'' . $newid . '\' as `sched_id`,`wb_id`,`ordering` '
. 'FROM #__sql2excel_schedule2book '
. 'WHERE sched_id=' . $wbID;
$db->setQuery( $query );
if (!$db->query()) {
$this->setError($db->getErrorMsg());
return false;
}
}
}
}
return $newid;
}
/**
* Method to publish schedule(s)
*
* @access public
* @return boolean True on success
*/
function publish($cid = array(), $publish = 1)
{
$user =& JFactory::getUser();
if (count( $cid ))
{
JArrayHelper::toInteger($cid);
$cids = implode( ',', $cid );
$query = 'UPDATE #__sql2excel_schedules'
. ' SET published = '. (int) $publish
. ' WHERE id IN ( '.$cids.' )';
$this->_db->setQuery( $query );
if (!$this->_db->query()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
return true;
}
/**
* Method to reorder the categories
*
* @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_schedules 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 store workbooks for a schedule
*
* @access public
* @return boolean True on success
*/
function storeWB($schedID, $workbooks)
{
// Delete previous worksheets
$query = "DELETE FROM #__sql2excel_schedule2book " .
" WHERE sched_id=" . $schedID;
$this->_db->setQuery( $query );
if ( $this->_db->query() ) {
// Add the new Workbooks
$wbArr = explode(',',$workbooks);
$i=0;
foreach ( $wbArr as $wbID ) {
if ( $wbID > 0 ) {
$query = "INSERT INTO #__sql2excel_schedule2book (`sched_id`,`wb_id`,`ordering`) VALUES (" . $schedID . "," . $wbID . "," . $i . ")";
$this->_db->setQuery( $query );
$res = $this->_db->query();
$i++;
}
}
return $res;
} else {
return false;
}
}
/**
* 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 boolean True on success
*/
function delete()
{
$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
$row =& $this->getTable();
if (count( $cids )) {
foreach($cids as $cid) {
if (!$row->delete( $cid )) {
$this->setError( $row->getErrorMsg() );
return false;
}
}
}
return true;
}
/**
* Method to retrieve current schedule record
*
* @access Private
* @return boolean True on success
*/
function _getData() {
if (empty($this->_schedData)) {
$query = 'SELECT p.* '.
' FROM #__sql2excel_schedules' .
' WHERE id = ' . (int) $this->_schedID;
$this->_db->setQuery($query);
$this->_schedData = $this->_db->loadObject();
return (boolean) $this->_schedData;
}
return true;
}
}