Location: PHPKode > projects > VideoDB > videodb/index.php
<?php
/**
 * Browse View
 *
 * Lets you browse through your movie collection
 *
 * @package videoDB
 * @author  Andreas Gohr <hide@address.com>
 * @author  Andreas Götz <hide@address.com>
 * @author  Chinamann <hide@address.com>
 * @link	http://videodb.sf.net
 * @version $Id: index.php,v 2.96 2008/10/19 10:44:55 andig2 Exp $
 */

require_once './core/session.php';
require_once './core/functions.php';
require_once './core/output.php';

/**
 * Update item list asynchronously
 *
 * @author  Andreas Goetz   <hide@address.com>
 */ 
function ajax_render()
{
    global $smarty, $result;
    global $pageno, $totalpages, $totalresults;
    
    // add some delay for debugging
    if ($config['debug'] && $_SERVER['SERVER_ADDR'] == '127.0.0.1')  usleep(rand(200,1000)*1000);

    // load languages and config into Smarty
    tpl_language();
    tpl_list($result);

    $content = $smarty->fetch('list.tpl',  null, null, false);

    header('X-JSON: '.json_encode(array('totalresults' => $totalresults ? $totalresults : count($result), 
                                        'maxpageno' => $totalpages
                                        )));
    echo $content;
    
    exit;
}

// set defaults and update session
session_default('filter', $config['filterdefault']);
session_default('showtv', $config['showtv']);
session_default('listcolumns', $config['listcolumns']);

// enable redirects to last list view for delete.php
session_set('listview', 'index.php');

// standard filters
$filter_expr = array(
  'NUM'  => '^["\\\' ]*[^A-Za-zÄäÖöÜüß]',
  'ABC'  => '^["\\\' ]*[ABCabcÄä]',
  'DEF'  => '^["\\\' ]*[DEFdef]',
  'GHI'  => '^["\\\' ]*[GHIghi]',
  'JKL'  => '^["\\\' ]*[JKLjkl]',
  'MNO'  => '^["\\\' ]*[MNOmnoÖö]',
  'PQRS' => '^["\\\' ]*[PQRSpqrsß]',
  'TUV'  => '^["\\\' ]*[TUVtuvÜü]',
  'WXYZ' => '^["\\\' ]*[WXZwxy]'
);

// create SQL according to selected filter
switch ($filter)
{
    case 'all':
                    $WHERES = 'mediatype != '.MEDIA_WISHLIST;
                    $ORDER  = ($config['orderallbydisk'])  ? 'diskid, ' : '';
                    $ORDER .= 'title, subtitle';
                    break;
    case 'seen':
                    $WHERES = '!ISNULL('.TBL_USERSEEN.'.video_id) AND mediatype != '.MEDIA_WISHLIST;
                    break;
    case 'unseen':
                    $WHERES = 'ISNULL('.TBL_USERSEEN.'.video_id) AND mediatype != '.MEDIA_WISHLIST;
                    break;
    case 'new':
                    $WHERES = 'mediatype != '.MEDIA_WISHLIST;
                    $ORDER  = 'created DESC, lastupdate DESC ';
                    $LIMIT  = ' LIMIT '.$config['shownew'];
                    break;
    case 'wanted':
                    $WHERES = 'mediatype = '.MEDIA_WISHLIST;
                    break;
    case 'full':
                    $WHERES = '1=1';  // secret filter for exposing all data
                    break;
    default:
                    // make sure filter is valid
                    if (!array_key_exists($filter, $filter_expr)) $filter = 'ABC';
                    // apply filter
                    $WHERES = 'title RLIKE \''.utf8_encode($filter_expr[$filter]).'\' AND mediatype != '.MEDIA_WISHLIST;
}

// default order
if (!$ORDER)  $ORDER   = 'title, subtitle';
if (!$showtv) $WHERES .= ' AND istv = 0';

// owner selection for multiuser mode- by default this is the logged in user
// any user has automatically read permissions for his personal data
if ($config['multiuser']) 
{
    // get owner from session- or use current user
    session_default('owner', get_username(get_current_user_id()));
    
    // build html select box
    $all = strtoupper($lang['radio_all']);
    $smarty->assign('owners', out_owners(array($all => $all), PERM_READ));
    $smarty->assign('owner', $owner);

    // if we don't have read all permissions, limit visibility using cross-user permissions
    if (!check_permission(PERM_READ))
    {
        $JOINS   = ' LEFT JOIN '.TBL_PERMISSIONS.' ON '.TBL_DATA.'.owner_id = '.TBL_PERMISSIONS.'.to_uid';
        $WHERES .= ' AND '.TBL_PERMISSIONS.'.from_uid = '.get_current_user_id().' AND '.TBL_PERMISSIONS.'.permissions & '.PERM_READ.' != 0';
    }
        
    // further limit to single owner
    if ($owner != $all) $WHERES .= " AND ".TBL_USERS.".name = '".addslashes($owner)."'";
}

// async request for quick-searching within current spec
if ($ajax_quicksearch)
{
    $qs      = mysql_escape_string($ajax_quicksearch);
    $WHERES .= ' AND (title LIKE "%'.$qs.'%" OR subtitle LIKE "%'.$qs.'%")';

    // do hard work
    $SQL    = 'SELECT '.TBL_DATA.'.id, title, subtitle
                 FROM '.TBL_DATA.'
            LEFT JOIN '.TBL_USERS.' ON '.TBL_DATA.'.owner_id = '.TBL_USERS.'.id 
            LEFT JOIN '.TBL_USERSEEN.' ON '.TBL_DATA.'.id = '.TBL_USERSEEN.'.video_id AND '.TBL_USERSEEN.'.user_id = '.get_current_user_id()."
               $JOINS 
                WHERE $WHERES
             ORDER BY $ORDER
                LIMIT 20";
    $result = runSQL($SQL);

    foreach ($result as $item)
    {
        $title  = preg_replace('/('.$ajax_quicksearch.')/i', '<em>\1</em>', $item['title']);
        if ($item['subtitle']) $title .= ' - '.$item['subtitle'];
        $ret   .= "<li id='".$item['id']."'>".$title."</li>";
    }
    $ret = "<ul>$ret</ul>";
    
    echo $ret;
    exit;
}

// XML / RSS / PDF export
if ($export && $config[$export])
{
	// either (xml|rss|pdf)export
    $func = $export.'export';
    if ($export == 'rss') $export = 'xml';
    require_once './core/'.$export.'.php';

    if (function_exists($func)) $func("$JOINS WHERE $WHERES ORDER BY $ORDER $LIMIT");
    exit;
}

/*
    Calculate pagination
    
    Check to see if user has selected the New items tab.
    This is seperately assigned as a LIMIT so, if this exists, 
    lets just skip page numbers and carry on
*/
if (!$LIMIT && ($config['pageno'] > 0) &! ($pageno == 'all'))
{
    // start at first page
    if (!$pageno) $pageno = 1;

	// define Max Results Per Page
	$maxresults = $config['pageno'];

	// define the Start Number
	$from   = (($pageno * $maxresults) - $maxresults);

    $LIMIT  = ' LIMIT '.$from.', '.$maxresults;

	// get total amount of results from DB
	$totalresults = runSQL('SELECT count(*) AS num 
                              FROM '.TBL_DATA.'
                         LEFT JOIN '.TBL_USERS.' ON '.TBL_DATA.'.owner_id = '.TBL_USERS.'.id 
                         LEFT JOIN '.TBL_USERSEEN.' ON '.TBL_DATA.'.id = '.TBL_USERSEEN.'.video_id AND '.TBL_USERSEEN.'.user_id = '.get_current_user_id()."
                            $JOINS WHERE $WHERES");
    $totalresults = (count($totalresults) > 0) ? (int)$totalresults[0]['num'] : 0;
    
	// calculate total amount of pages
	$totalpages = ceil($totalresults / $maxresults);

	$smarty->assign('pageno', $pageno);               // assign current Page Number
	$smarty->assign('maxpageno', $totalpages);		  // set Maximum Pages
	$smarty->assign('totalresults', $totalresults);   // set Total Records Returned
}


// do hard work
$SQL    = 'SELECT '.TBL_DATA.'.id, '.TBL_DATA.'.diskid, 
                  title, subtitle, language, year,
                  director, plot, imgurl, 
                  owner_id, '.TBL_USERS.'.name AS owner, '.TBL_LENT.'.who, 
                  md5, comment, disklabel, imdbID, actors, runtime,
                  country, filename, filesize, filedate, audio_codec,
                  video_codec, video_width, video_height, istv,
                  lastupdate, mediatype,
                  custom1, custom2, custom3, custom4, 
                  created, !ISNULL('.TBL_USERSEEN.'.video_id) AS seen
             FROM '.TBL_DATA.'
            LEFT JOIN '.TBL_USERS.' ON '.TBL_DATA.'.owner_id = '.TBL_USERS.'.id 
            LEFT JOIN '.TBL_USERSEEN.' ON '.TBL_DATA.'.id = '.TBL_USERSEEN.'.video_id AND '.TBL_USERSEEN.'.user_id = '.get_current_user_id().'
        LEFT JOIN '.TBL_LENT.' ON '.TBL_DATA.'.diskid = '.TBL_LENT.'.diskid'."
           $JOINS 
            WHERE $WHERES
         ORDER BY $ORDER
		   $LIMIT";
$result = runSQL($SQL);

// store query result in session for prev/next navigation
session_set('query_result', array_extract($result, 'id'));

// process asynchronous refresh
if ($ajax_render)
{
    ajax_render();
}

// prepare
tpl_page('browse');
tpl_list($result);
tpl_filters($filter, $showtv);

// caching enabled?
if ($config['http_caching'])
{
    require_once('./core/httpcache.php');
    cache_start('index');
}

$smarty->assign('moreless', true);           // show more/less control in list view

// allow XML and RSS export
foreach (array('xls','pdf','xml','rss') as $export)
{
	if ($config[$export]) $smarty->assign($export, 'index.php?');
}

// display templates
smarty_display('header.tpl');
smarty_display('filters.tpl');
if (!$config['http_caching']) flush();

smarty_display('list.tpl');

smarty_display('footer.tpl');

// caching enabled?
if ($config['httpcaching'])
{
    httpCacheOutput('index', httpCacheCaptureEnd());
}

?>
Return current item: VideoDB