Horizontal Favorites Block

Started by Chen Zhen, September 10, 2024, 01:49:39 AM

Previous topic - Next topic

Chen Zhen

Horizontal Favorites Block

Version: SMF Arcade 2.7.0.2+



Use this as a top/bottom/header custom php block:

Mian Code:/**************************
set $gameTypes
0 = Permission based
1 = Flash/HTML
2 = ROM only
3 = All game types
**************************/
/* Display user's favorites for SMF Arcade */

global $user_info, $smcFunc, $settings, $arcadeModSettings, $scripturl, $boardurl, $boarddir, $sourcedir;

/* Adjust these */
$lang = 'Play ';
$none = 'No games selected';
$per_row = 4;
$rows = 1;
$char_length = 35;
$prev = 'Prev';
$next = 'Next';
$gameTypes = 0;

/* Do not edit below this line */
switch($gameTypes) {
case 1:
$where = ' AND game.rom_flag = {int:romflag}';
$romflag = 0;
break;
case 2:
$where = ' AND game.rom_flag = {int:romflag}';
$romflag = 1;
break;
case 3:
$where = '';
$romflag = 0;
break;
default:
$romflag = 0;
if (allowedTo('arcade_view_retro_arch')) {
$where = '';
}
else {
$where = ' AND game.rom_flag = {int:romflag}';
}
}
$query = '(favorite.id_member = ' . $user_info['id'] . ' AND enabled = 1)' . $where;
$datum = array('id_game', 'game_name', 'game_directory', 'thumbnail', 'rom_flag');
list($x, $count, $game) = array(0, 0, array());
$maindir = !empty($arcadeModSettings['gamesUrl']) ? $arcadeModSettings['gamesUrl'] : 'Games';

$pages = '
<script type="text/javascript">
var pager = new Pager("favs", ' . (int)$rows . ');
        pager.init();
        pager.showPageNav("pager", "favPagePosition");
        pager.showPage(1);
    </script>';

/* Some JavaScript to control the pages */
$arcade = '
<script type="text/javascript">
function Pager(tableName, itemsPerPage) {
this.tableName = tableName;
this.itemsPerPage = itemsPerPage;
this.currentPage = 1;
this.pages = 0;
this.inited = false;

this.showRecords = function(from, to) {
var rows = document.getElementById(tableName).children;
for (var i = 1; i < rows.length; i++) {
if (i < from || i > to)
rows[i].style.display = "none";
else
rows[i].style.display = "";
}
}

this.showPage = function(pageNumber) {
if (!this.inited) {
console.log("show page not initialized");
return;
}
var oldPageAnchor = document.getElementById("pg"+this.currentPage);
oldPageAnchor.className = "pg-normal";

this.currentPage = pageNumber;
var newPageAnchor = document.getElementById("pg"+this.currentPage);
newPageAnchor.className = "pg-selected";

var from = (pageNumber - 1) * itemsPerPage + 1;
var to = from + itemsPerPage - 1;
this.showRecords(from, to);
}

this.prev = function() {
if (this.currentPage > 1)
this.showPage(this.currentPage - 1);
}

this.next = function() {
if (this.currentPage < this.pages) {
this.showPage(this.currentPage + 1);
}
}

this.init = function() {
var rows = document.getElementById(tableName).children;
var records = (rows.length - 1);
this.pages = Math.ceil(records / itemsPerPage);
this.inited = true;
}
this.showPageNav = function(pagerName, positionId) {
if (!this.inited) {
console.log("page navigation not initialized");
return;
}
var element = document.getElementById(positionId);
var pagerHtml = \'<span onmouseover="resizePage(this, 125)" onmouseout="resizePage(this, 100)" style="cursor: pointer;" onclick="\' + pagerName + \'.prev();" class="pg-normal"> &larr; ' . $prev . ' </span> | \';

for (var page = 1; page <= this.pages; page++)
pagerHtml += \'<span onmouseover="resizePage(this, 125)" onmouseout="resizePage(this, 100)" style="cursor: pointer;" id="pg\' + page + \'" class="pg-normal" onclick="\' + pagerName + \'.showPage(\' + page + \');">\' + page + \'</span> | \';
pagerHtml += \'<span onmouseover="resizePage(this, 125)" onmouseout="resizePage(this, 100)" style="cursor: pointer;" onclick="\' + pagerName + \'.next();" class="pg-normal"> ' . $next . ' &rarr;</span>\';
element.innerHTML = pagerHtml;
element.style.margin = "0px auto";
}
}
function resizePage(elem, percent)
{
elem.style.fontSize = percent.toString() + "%";
}
function regenerate()
{
window.location.reload();
}

function regenerate3()
{
if (document.layers)
{
appear();
setTimeout("window.onresize=regenerate",450);
}
}

function changetext(whichcontent)
{
if (document.all||document.getElementById)
{
cross_el=document.getElementById? document.getElementById("descriptions"):document.all.descriptions;
cross_el.innerHTML = \'<div style="font-family:Arial Narrow Bold;font-size:small;">\'+whichcontent+\'</div>\';
}
else if (document.layers)
{
document.d1.document.d2.document.write(\'<div style="font-family:Arial Narrow Bold;font-size:small;">\'+whichcontent+\'</div>\');
document.d1.document.d2.document.close();
}

}

function appear()
{
document.d1.visibility="show";
}
</script>';
/* end of js controller */

$result = $smcFunc['db_query']('', "
SELECT favorite.id_favorite, favorite.id_member, favorite.id_game, game.game_name, game.game_directory, game.thumbnail, game.rom_flag, game.enabled FROM {db_prefix}arcade_favorite AS favorite
LEFT JOIN {db_prefix}arcade_games AS game ON (game.id_game = favorite.id_game)
WHERE {$query} ORDER BY favorite.id_favorite DESC", array('romflag' => $romflag));
while ($val = $smcFunc['db_fetch_assoc']($result)) {
foreach ($datum as $data) {
if (empty($val[$data]))
$val[$data] = false;
$game[$val['id_favorite']][$data] = $val[$data];
}
$count++;
}
$smcFunc['db_free_result']($result);
$arcade .= '
<script type="text/javascript">
if (window.addEventListener)
window.addEventListener("load", regenerate3, false);
else if (window.attachEvent)
window.attachEvent("onload", regenerate3);
else
window.onload = regenerate3();
</script>';
if ($count == 0)
$arcade .= '
<div style="text-align:center">' . $none . '</div>';
else
{
$arcade .= '
<div style="overflow: hidden;margin: 0 auto;width: 100%;" align="center">
<div id="favs" style="width: 100%;display: table;overflow: hidden;margin: 0 auto;">
<div style="display: table-row;margin: 0 auto;">
<div style="display: table-cell;">&nbsp;</div>
</div>
<div style="display: table-row;margin: 0 auto;">';
foreach ($game as $myfavs)
{
$x++;
$path = str_replace('\\', '/', (!empty($myfavs['rom_flag']) ? $arcadeModSettings['romGamesDirectory'] : $arcadeModSettings['gamesDirectory']));
$filepath = rtrim($path, '/');
$gamesUrl = !empty($myfavs['rom_flag']) ? $arcadeModSettings['romGamesUrl'] : $arcadeModSettings['gamesUrl'];
$action = !empty($myfavs['rom_flag']) ? 'retro_arch' : 'arcade';
$image = $settings['default_theme_url'] . '/arc_icons/Default.gif';
if (!empty($myfavs['thumbnail'])) {
$imgPath = empty($myfavs['game_directory']) ? $myfavs['thumbnail'] : $myfavs['game_directory'] . '/' . $myfavs['thumbnail'];
$image = file_exists($filepath . '/' . $imgPath) ? $gamesUrl . '/' . $imgPath : $image;
}
$title = strlen($myfavs['game_name']) >= ((int)$char_length) ? $title = substr($myfavs['game_name'], 0, ((int)$char_length - 1)) . '...' : $myfavs['game_name'];

$arcade .= '
<div style="display: table-cell;text-align:center;width:25%;">
<div style="height:80px;overflow:hidden;">
<a href="' . $scripturl . '?action=' . $action. ';sa=play;game=' . $myfavs['id_game'] . '" title="' . $lang . $myfavs['game_name'] . '">
<img src="' . $image . '" style="max-height:50px;max-width:50px;" alt="" />
</a>
<span style="display: block;line-height: 1em;"></span>
<a href="' . $scripturl . '?action=' . $action . ';sa=play;game=' . $myfavs['id_game'] . '" title="' . $lang . $myfavs['game_name'] . '">' . $title . '</a>
</div>
</div>';
if ($x > ((int)$per_row -1)) {
$x=0;
$arcade .= '
</div>
<div style="display: table-row;margin: 0 auto;">';
}
}

$arcade .= '
</div>
</div>
</div>
<span style="display: block;line-height: 1em;"></span>
<div id="favPagePosition" style="text-align:center;margin: 0 auto;overflow: hidden;"> </div>
<div style="text-align:center;margin: 0 auto;overflow: hidden;">' . $pages . '</div>';
}

if (!allowedTo('arcade_view') && $gameTypes == 0) {
$arcade = '';
}

echo $arcade;