Location: PHPKode > projects > QuickTicket > quickticket/qti_topics_csv.php
<?php

/**
* PHP versions 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to hide@address.com so we can mail you a copy immediately.
*
* @package    QuickTicket
* @author     Philippe Vandenberghe <hide@address.com>
* @copyright  2008-2012 The PHP Group
* @version    2.5.1 build:20110304
*/

session_start();
require_once('bin/qti_init.php');
if ( !$oVIP->CanView('V2') ) HtmlPage(11);

$s = -1; QThttpvar('s','int'); if ( $s<0 ) die('Missing section id...');

// ---------
// INITIALISE
// ---------

// Check arguments

$size = strip_tags($_GET['size']);
$intCount = intval($_GET['n']);

if ( empty($size) || $intCount <= $_SESSION[QT]['topics_per_page'] ) $size='all';
if ( strlen($size)>6 || strlen($size)<2 ) die('Invalid argument');
if ( substr($size,0,1)!='p' && substr($size,0,1)!='m' && $size!='all') die('Invalid argument');
if ( substr($size,0,1)=='p' )
{
  $i = intval(substr($size,1));
  if ( empty($i) ) die('Invalid argument');
  if ( ($i-1) > $intCount/$_SESSION[QT]['topics_per_page'] ) die('Invalid argument');
}
if ( substr($size,0,1)=='m' )
{
  if ( $size!='m1' && $size!='m2' && $size!='m5' && $size!='m10' ) die('Invalid argument');
}
if ( $intCount>1000 && $size=='all' ) die('Invalid argument');
if ( $intCount<=1000 && substr($size,0,1)=='m' ) die('Invalid argument');
if ( $intCount>1000 && substr($size,0,1)=='p' ) die('Invalid argument');

// apply page argument

if ( substr($size,0,1)=='p' ) $_GET['page'] = substr($size,1);

// Initialise

$strCSV = '';
$arrMe[] = array();

include('bin/qti_fn_sql.php');

$oSEC = new cSection($s);
if ( isset($_SESSION[QT]['lastcolumn']) ) $oSEC->d_last=$_SESSION[QT]['lastcolumn'];

if ( $oSEC->type==1 && ($oVIP->role=='V' || $oVIP->role=='U') )
{
  // exit
  $oVIP->selfname = $L['Section'];
  $oVIP->exitname = ObjTrans('index','i',$_SESSION[QT]['index_name']);
  $oVIP->EndMessage(NULL,$L['R_staff'],$_SESSION[QT]['skin_dir'],0);
}
if ( $oSEC->type==2 && $oVIP->role=='V' )
{
  // exit
  $oVIP->selfname = $L['Section'];
  $oVIP->exitname = ObjTrans('index','i',$_SESSION[QT]['index_name']);
  $oVIP->EndMessage(NULL,$L['R_member'],$_SESSION[QT]['skin_dir'],0);
}

$oVIP->selfurl = 'qti_topics.php';
$oVIP->selfname = $L['Section'].': '.$oSEC->name;

$strOrder = 'lastpostdate';
$strDirec = 'DESC';
$intLimit = 0;
$intLen = $_SESSION[QT]['topics_per_page'];
if ( isset($_GET['page']) ) $intLimit = (intval($_GET['page'])-1)*$intLen;
if ( isset($_GET['order']) ) $strOrder = $_GET['order'];
if ( isset($_GET['dir']) ) $strDirec = $_GET['dir'];

// apply argument

if ( $size=='all') { $intLimit=0; $intLen=$intCount; }
if ( $size=='m1' ) { $intLimit=0; $intLen=999; }
if ( $size=='m2' ) { $intLimit=1000; $intLen=1000; }
if ( $size=='m5' ) { $intLimit=0; $intLen=4999; }
if ( $size=='m10') { $intLimit=5000; $intLen=5000; }

// Criteria sql: topics visible for current user ONLY

$strWhere = '';
if ( $oSEC->type==2 && !$oVIP->IsStaff() ) $strWhere = ' AND t.firstpostuser='.$oVIP->id;
if ( $_SESSION[QT]['show_closed']=='0' ) $strWhere.=' AND t.status<>"Z"'; 
$strOnTop = '';
if ( $_SESSION[QT]['news_on_top']==1 )
{ 
  $strOnTop = 'CASE t.type WHEN "A" THEN "A" ELSE "Z" END as typea,';
  if ( $oDB->type=='access' ) $strOnTop = 't.type as typea,';
}

// --------
// HTML START
// --------

// Last column: can be '0' (moderator requests no-field)
switch($oSEC->d_last)
{
case 'replies': $str=$L['Replys']; break;
case 'views': $str=$L['Views']; break;
case 'status': $str=$L['Status']; break;
case 'wisheddate': $str=$L['Wisheddate']; break;
case 'id': $str='Id'; break;
case 'actorname': $str=$L['Userrole_MA']; break;
case 'notifiedname': $str=$L['Userrole_UN']; break;
case 'tags': $str=$L['Tags']; break;
case 'coord': $str=$L['Coord']; break;
default: $str=$oSEC->d_last;
}

// ========
$arrFLD = array();
$oFLD = new cFLD('csvdate',$L['Date']); $arrFLD['csvdate']=$oFLD;
$oFLD = new cFLD('csvtime',$L['Time']); $arrFLD['csvtime']=$oFLD;
$oFLD = new cFLD('status','Status'); $arrFLD['status']=$oFLD;
if ( $oSEC->numfield!='N' ) { $oFLD = new cFLD('numid',$L['Ref']); $arrFLD['numid']=$oFLD; }
if ( !empty($oSEC->prefix) ) { $oFLD = new cFLD('smile','Ico'); $arrFLD['smile']=$oFLD; }
$oFLD = new cFLD('title',$L['Topics']); $arrFLD['title']=$oFLD;
$oFLD = new cFLD('text','Text'); $arrFLD['text']=$oFLD;
$oFLD = new cFLD('firstpostname',$L['Author']); $arrFLD['firstpostname']=$oFLD;
if ( $oSEC->d_last=='none' || empty($oSEC->d_last) || $oSEC->d_last=='N' )
{
$oFLD = new cFLD('lastpostdate',$L['Last_message']); $arrFLD['lastpostdate']=$oFLD;
}
else
{
$oFLD = new cFLD('lastpostdate',$L['Last_message']); $arrFLD['lastpostdate']=$oFLD;
$oFLD = new cFLD($oSEC->d_last,$str); $arrFLD[$oSEC->d_last]=$oFLD;
}
$oFLD = new cFLD('uid','UID'); $arrFLD['uid']=$oFLD;
// ========
foreach($arrFLD as $strKey=>$oFLD) $strCSV .= ToCsv($oFLD->name);
$strCSV = substr($strCSV,0,-1)."\r\n";
// ========
$strState  = $strOnTop.'t.*, p.title, p.icon, p.textmsg FROM '.TABTOPIC.' t INNER JOIN '.TABPOST.' p ON t.firstpostid=p.id WHERE t.forum='.$oSEC->id.$strWhere;
if ( $strOrder=='title' ) { $strFullOrder='p.title'; } else { $strFullOrder='t.'.$strOrder; }
$oDB->Query( LimitSQL($strState,(empty($strOnTop) ? '' : 'typea DESC, ').$strFullOrder.' '.$strDirec,$intLimit,$_SESSION[QT]['topics_per_page'],$oSEC->items) );
// ========
$intWhile=0;
while($row=$oDB->Getrow())
{
  $str = implode('',TableRowFormat($arrFLD,$row,true,$oSEC,false,true));
  if ( substr($str,-1,1)==';' ) $str = substr($str,0,-1);
  $strCSV .= $str."\r\n";

  $intWhile++; 
  //odbcbreak
  if ( $intWhile>=$intCount ) break;  
}
// ========

// OUPUT

if ( isset($_GET['debug']) )
{
  echo $strCSV;
  exit;
}

if ( !headers_sent() )
{
  header('Content-Type: text/csv; charset='.QTI_HTML_CHAR);
  header('Content-Disposition: attachment; filename="qti_'.date('YmdHi').'.csv"');
}

echo $strCSV;
Return current item: QuickTicket