<?php
/**
* Free Wallpaper Script
*
* Free Wallpaper Script by Vepa Halliyev is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
*
* @package Free Wallpaper Script
* @author Vepa Halliyev
* @copyright Copyright (c) 2009, Vepa Halliyev, veppa.com.
* @license http://www.veppa.com/free-wallpaper-script/
* @link http://www.veppa.com/free-wallpaper-script/
* @since Version 1.0
* @filesource
*/
class Thumbgrid
{
static public $max_downloads = 0;
static public $max_hotness = 0;
/**
* render wallpapers as thumbs
*
* @param array $data
* @param string $format (front,admin,user,admin_cell,user_cell...)
* @param int $tbl_cols number of table columns
*/
static function thumbView($data, $format = 'table', $tbl_cols=5)
{
/*
type: table - front end
edit - profile
admin - accept as publig or leave as private wallpaper
*/
//echo (" thumbView($data,$format,$tbl_cols)");
// if only cell requierd then return only cell
if ($format === 'admin_cell' || $format === 'user_cell')
{
return self::makeThumbCell($data, str_replace("_cell", "", $format));
}
if (count($data) && is_array($data))
{
$i_rec = 0;
$r = '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="thumb_grid">';
foreach ($data as $val)
{
if ($i_rec % $tbl_cols == 0)
{
$r .= "<tr>";
}
++$i_rec;
$r .= "<td rel=\"" . $val->id . "\">" . self::makeThumbCell($val, $format) . "</td>";
if ($i_rec % $tbl_cols == 0)
{
$r .= "</tr>";
}
}
//complete cells
$last_rec = $i_rec % $tbl_cols;
if ($last_rec)
{
$token_count = 0;
while ($last_rec < $tbl_cols)
{
$r .= "<td></td>";
$last_rec++;
$token_count++;
if ($token_count > 50)
{
exit("infinite loop in thumbView(...)");
}
}
$r .= "</tr>";
}
$r .= '</table>';
}
else
{
$r = ''; //'No wallpapers to display.';
}
return $r;
}
static public function imgAlt($cell)
{
$arr_suffix = array('wallpaper', 'wallpaper', 'wallpaper', 'wallpaper');
$num = 4;
return htmlspecialchars($cell->name . ' ' . $arr_suffix[($cell->id % $num)]);
}
/**
* create html elements for this wallpaper
*
* @param $cell wallpaper object
* @param $format string. (admin,user,mini,widget)
*/
static function makeThumbCell($cell, $format)
{
/* $r = '<!--';
ob_start();
print_r($cell);
$r .= ob_get_contents();
ob_end_clean();
$r .= '-->';
*/
switch ($format)
{
case 'admin':
$r .= self::makeThumbCellDefault($cell, $format);
// $r .= self::addAdminControls($cell);
break;
case 'user':
$r .= self::makeThumbCellDefault($cell, $format);
// $r .= self::addUserControls($cell);
break;
case 'mini':
$r .= '<a href="' . self::makeGoodUrl($cell->id, 'wallpaper', $cell->name) . '"
title="' . Thumbgrid::imgAlt($cell) . '" rel="permalink">
<img width="50" height="37" src="' . Wallpaper::img($cell, 'thumb') . '"
alt="' . Thumbgrid::imgAlt($cell) . '" border="0" /></a>';
break;
case 'widget':
$r .= self::makeThumbCellWidget($cell, $format);
break;
default:
$r .= self::makeThumbCellDefault($cell, $format);
}
return $r;
}
function makeThumbCellDefault($cell, $format)
{
$link = self::makeGoodUrl($cell->id, 'wallpaper', $cell->name);
$r .= '<a href="' . $link . '" title="' . Thumbgrid::imgAlt($cell) . '" rel="permalink">
<img width="100" height="75" src="' . Wallpaper::img($cell, 'thumb') . '" alt="' . Thumbgrid::imgAlt($cell) . '" border="0" /></a>';
$r .= self::addStats($cell);
if ($format == 'front')
{
$r .= self::addRate($cell);
}
$r .= '<a href="' . $link . '">' . View::escape($cell->name) . '</a>';
$r .= self::addAuthor($cell);
$r .= self::addCategory($cell);
$r .= self::addTags($cell, 3);
return $r;
}
function makeThumbCellWidget($cell, $format)
{
$link = self::makeGoodUrl($cell->id, 'wallpaper', $cell->name);
$r .= '<a href="' . trim(URL_ABS, "/") . $link . '" target="_blank" rel="permalink" title="' . Thumbgrid::imgAlt($cell) . '">
<img width="100" height="75" src="' . Wallpaper::img($cell, 'thumb') . '" alt="' . Thumbgrid::imgAlt($cell) . '" border="0" /></a>';
$r .= $this->addRate($cell);
return $r;
}
function addRate($cell, $with_info=false)
{
// rating placeholder
if ($with_info)
{
$info = ' - Rate this wallpeper';
}
$r .= '<div class="rating" rel="' . $cell->id . '">
<span class="controls">
<a href="#" class="rate_down" rel="' . $cell->id . '" rate="-1" title="vote DOWN :("> </a>
<a href="#" class="rate_up" rel="' . $cell->id . '" rate="+1" title="vote UP :)"> </a>
</span>
<span class="rank">' . ($cell->rank > 0 ? '+' : '') . $cell->rank . '</span>
' . $info . '
</div>';
return $r;
}
function addMoreInfo($cell)
{
// add other info for wallpaper
$r .= $cell->artist ? 'Artist: ' . View::escape($cell->artist) . '<br />' : '';
$r .= $cell->site ? '<a href="' . View::escape($cell->site) . '" target="_blank">' . View::escape($cell->site) . '</a><br />' : '';
$r .= $cell->description ? View::escape($cell->description) : '';
$r = '<div class="more">' . $r . '</div>';
return $r;
}
function addTags($cell, $limit=0)
{
// tags
if (strlen($cell->tags))
{
$tags = explode(',', $cell->tags);
$r .= '<div class="tags">';
$i = 1;
foreach ($tags as $tag)
{
if ($limit > 0 && $limit < $i)
{
continue;
}
$r .= '<a href="' . Tag::url($tag) . '">' . $tag . '</a>, ';
$i++;
}
$r = trim($r, ", ");
$r .= '</div>';
}
return $r;
}
function addCategory($cell)
{
// categories
if (is_array($cell->arr_cat) && count($cell->arr_cat))
{
$r .= '<div class="cat">';
foreach ($cell->arr_cat as $key => $val)
{
$rc = '<a href="' . self::makeGoodUrl($key, 'category', $val->name) . '">' . View::escape($val->name) . '</a>, ' . $rc;
}
$rc = trim($rc, ", ");
$r .= $rc . '</div>';
}
return $r;
}
function depricated_addAdminControls($cell)
{
// add buttons to move wallpaper as pending, public, private
// 0: private, 1:public, 2: pending
$arr_r = Array(0 => '<a href="#private"state="0" title="private">private</a>',
1 => '<a href="#private" state="1" title="public">public</a>',
2 => '<a href="#private" state="2" title="pending">pending</a>');
$state_selector = '<div class="update_state_select" style="display:none">
<a href="#private" state="0" title="private">private</a><br />
<a href="#private" state="1" title="public">public</a><br />
<a href="#private" state="2" title="pending">pending</a>
</div>';
//unset($arr_r[$cell->state]);
return '<div class="update_state">' . $arr_r[$cell->state] . ' | ' . $this->addEdit($cell) . '</div>' . $state_selector;
}
function depricated_addUserControls($cell)
{
// add buttons to move wallpaper as pending, public, private
// 0: private, 1:public, 2: pending
$arr_r = Array(0 => '<a href="#private" state="0" title="private">private</a>',
1 => '<a href="#private" state="1" title="public">public</a>',
2 => '<a href="#private" state="2" title="pending">pending</a>');
$state_selector = '<div class="update_state_select" style="display:none">
<a href="#private" state="0" title="private">private</a><br />
<a href="#private" state="1" title="public">public</a><br />
</div>';
return '<div class="update_state">' . $arr_r[$cell->state] . ' | ' . $this->addEdit($cell) . '</div>' . $state_selector;
}
function addAuthor($cell)
{
if (strlen($cell->user->username))
{
$r = '<div class="by"><a href="' . self::makeGoodUrl($cell->uid, 'profile', $cell->user->username) . '">' . $cell->user->username . '</a></div>';
}
return $r;
}
function addStats($cell)
{
$r = '<div style="width: ' . self::getPercentage($cell->week, self::$max_hotness) . 'px;" class="hot_bar">download for last week ' . $cell->week . '</div>
<div style="width: ' . self::getPercentage($cell->dwn, self::$max_downloads) . 'px;" class="rating_bar">total downloads ' . $cell->dwn . '</div>';
return $r;
}
function addEdit($cell)
{
$r = '<a href="#edit" class="edit" rel="' . $cell->id . '">edit</a> | <a href="#delete" class="delete" rel="' . $cell->id . '">delete</a>';
return $r;
}
/* ROW FUNCTIONS */
function rowView($data, $mode = 'general')
{
/*
type: table - front end
edit - profile
admin - accept as publig or leave as private wallpaper
*/
switch ($mode)
{
case 'index':
foreach ($data as $row)
{
$r .= $this->makeIndexRow($row);
}
break;
case 'wallpaper':
$r = $this->makeWallpaperRow($data[0], $this->makeMiniRow($data[1]));
break;
case 'download':
$r = $this->makeDownloadRow($data[0]);
break;
case 'widget':
$r = $this->makeWidgetRow($data);
break;
default:
// first row with ful header
// second simple header
$r .= $this->makeMainRow($data[0]);
$r .= $this->makeIndexRow($data[1]);
}
return $r;
}
function makeWallpaperRow($row, $minirow='')
{
// gecici olarak bunu duzelt
global $view;
$cell = array_pop($row['arr']);
/* echo '<!--';
print_r($cell);
echo '-->';
*/
//$r = '<h1>'.$cell->name.'</h1>';
$r = '<h1>' . self::imgAlt($cell) . '</h1>';
$r .= '<table class="wallpaper_grid" width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td width="300"><img src="' . Wallpaper::img($cell, 'mid') . '" alt="' . Thumbgrid::imgAlt($cell) . '" border="0" class="mid" />';
$r .= $minirow;
//$r .= $view->render('ad/300x250.php');
$r .='</td><td>';
$r .= '<div class="download"><a href="' . self::makeGoodUrl($cell->id, 'download', $cell->name) . '" target="_blank">Download (' . $cell->size . ')</a></div>';
$r .= $this->downloadExtra($cell);
$r .= $this->addStats($cell);
$r .= $this->addRate($cell, true);
$r .= $this->addAuthor($cell);
$r .= $this->addCategory($cell);
$r .= $this->addTags($cell, 3);
// add more wallpaper links
$r .= $this->addMoreInfo($cell);
// add link ads
//$r .= '<br /><br />'.$view->render('ad/200x90.php');
//comments
$r .= $this->displayComments($row['comments']);
// comment form
$r .= $this->commentForm($cell);
//
$r .='</td></tr></table>';
return $r;
}
function makeDownloadRow($row)
{
// FIXME depricated. moved to download view.
// gecici olarak bunu duzelt
$cell = array_pop($row['arr']);
// increase download count
global $db;
$w = new Wallpaper($db, $cell->id);
$w->incDownloadCount();
/*
echo '<!--';
print_r($cell);
echo '-->';
*/
$r = '<table class="title_nav"><tr><td><h1>' . $cell->name . '</h1></td>
<td>' . $this->addAuthor($cell) . '</td>
<td>' . $this->addCategory($cell) . '</td>
<td>' . $this->addTags($cell, 3) . '</td></tr></table>';
$r.='<p>
<strong>Windows users:</strong>
Allow the photo to fully load on this page.
Right-click on the photo.
Select "Set As Wallpaper" from the drop-down menu.
Check desktop for new wallpaper. (You may need to restart your computer for wallpaper to be installed on your desktop.)<br />
<br />
<strong>Mac Users (OS X): </strong>
Allow image to fully load on this page. Move the cursor over the image. Hold down the mouse button over the image and drag the image to your desktop. Open "System Preferences" and select "Desktop." Drag the image into desktop pictures window. Select the image as your desktop. (You may need to restart your computer for wallpaper to be installed on your desktop.)
</p>';
$r .= '<img src="' . LIBURL . 'wall/o/' . $cell->img . '" alt="' . Thumbgrid::imgAlt($cell) . ' download" border="0" class="mid" />';
return $r;
}
function makeMainRow($row)
{
$r = $this->makeMainHeader($row['title'], $row['links'], $row['paginator']);
$r .= $this->thumbView($row['arr'], 'front', 5);
$r .= $this->makeMainFooter($row['paginator']);
return $r;
}
function makeIndexRow($row)
{
$r = $this->makeSimpleHeader($row['title'], $row['view_all']);
$r .= $this->thumbView($row['arr'], 'front', 5);
return $r;
}
function makeMiniRow($row)
{
$r = $this->makeSimpleHeader($row['title'], $row['view_all']);
$r .= $this->thumbView($row['arr'], 'mini', 5);
return $r;
}
function makeWidgetRow($row)
{
$r = $this->thumbView($row, 'widget', 1);
return $r;
}
function makeSimpleHeader($title, $link='')
{
if (strlen($link))
{
$r = '<table class="page_navigation" width="100%"><tr><td><h2>' . $title . '</h2></td><td align="right">' . $link . '</td></tr></table>';
}
else
{
$r = '<h2 class="page_navigation_single">' . $title . '</h2>';
}
return $r;
}
function makeMainHeader($title, $links, $paginator)
{
$r = '<h1>' . $title . '</h1>';
if ($links || $paginator)
{
$r .= '<table class="page_navigation" width="100%"><tr><td>' . $links . '</td><td align="right">' . $paginator . '</td></tr></table>';
}
return $r;
}
function makeMainFooter($paginator)
{
// paginater at bottom of page
if ($paginator)
{
$r = '<table class="page_navigation page_navigation_bottom" width="100%"><tr><td align="right">' . $paginator . '</td></tr></table>';
}
return $r;
}
function downloadExtra($cell)
{
$arr_sizes = Wallpaper::arrSizes();
$r = '';
foreach ($arr_sizes as $val)
{
if (self::if_size_smaller($cell->size, $val))
{
$r .= '<option value="' . $val . '">' . $val . '</option>';
}
}
if ($r)
{
$r = '<form action="' . get_url('resize/') . '" method="post" name="" target="_blank" onsubmit="return validate_form(this)">
<p>This wallpaper can be downloaded in different sizes.
Please select download size from box below.<p>
<select name="size" id="size" style="width:180px;">
<option value="">download different size</option>
' . $r;
$r.='</select>
<input type="hidden" name="id" value="' . $cell->id . '" />
<input type="submit" name="resize_download" value="' . __('Download') . '" class="button" />
</form>
<script>
var sel_resolution = screen.width+"x"+screen.height;
$("#size").val(sel_resolution);
</script>
';
}
return $r;
}
function addEmbed($cell)
{
// TODO : finish adding EMBED link
// FIXME : finish adding EMBED link
/**
* http://www.wallpaperbase.com/makecode.php?tag=first&path=/wallpapers/cars/pontiac/&pic=pontiac_9.jpg&title=Free%20wallpaper
*/
// rating placeholder
global $view;
$thumb = LIBURL . 'wall/mid/' . $cell->img;
$link = self::makeGoodUrl($cell->id, 'wallpaper', $cell->name);
$link = trim(URL_ABS, "/") . $link;
$embed_html = '<a href="' . $link . '" target="_blank"><img src="' . $thumb . '" border="0"></a>';
$embed_forum = '[url=' . $link . '][img]$thumb[/img][/url]';
$r = '<div class="more">' . $r . '</div>';
return $r;
}
function if_size_smaller($size, $size2)
{
list($cw, $ch) = explode('x', $size);
list($cw2, $ch2) = explode('x', $size2);
return ($cw * $ch > $cw2 * $ch2);
}
function displayComments($comments)
{
// add comments
if ($comments)
{
$r = '<br /><br />' . self::makeSimpleHeader('Comments') . '<ul class="comments">';
foreach ($comments as $cm)
{
$r .= '<li class="r' . ($comment_row++ % 2) . '"><p>' . View::escape($cm->cm_body) . '</p><p class="comment_author">';
if ($cm->User)
{
// registered user
$link = self::makeGoodUrl($cm->User->id, 'profile');
// $r .= '<a href="'.$link.'">'.View::escape($cm->User->username).'</a>';
$r .= View::escape($cm->User->username);
}
else
{
// random visitor
$link = self::makeValidUrl($cm->cm_website);
if ($link)
{
$r .= '<a href="' . $link . '" target="_blank" rel="nofollow">' . View::escape($cm->cm_name) . '</a>';
}
else
{
$r .= View::escape($cm->cm_name);
}
}
$r .= ' on ' . date("n F Y", $cm->cm_added_at) . '</p></li>';
}
$r.='</ul>';
}
return $r;
}
/*
display comments and comment form
*/
function commentForm($cell)
{
// check if number of max comments exceeded
// add form
$r = '
<br />
<form action="" method="post">' . self::makeSimpleHeader(__('Leave a comment')) . '
<table width="300" class="grid">';
if (AuthUser::isLoggedIn(false))
{
$r .= '<tr>
<td colspan="2"><div class="log_in"><label>User:</label>
' . View::escape(AuthUser::$user->username) . '</div></td>
</tr>';
}
else
{
$r .= '<tr>
<td colspan="2"><div class="log_in"><a href="?login=1" rel="nofollow">Please login if you have an account with us.</a></div></td>
</tr>
<tr>
<td><label for="cm_name">Name:</label></td>
<td><input type="text" name="cm_name" id="cm_name" value="' . View::escape($comment->cm_name) . '" /></td>
</tr>
<tr>
<td><label for="cm_website">Website:</label></td>
<td><input type="text" name="cm_website" id="cm_website" value="' . View::escape($comment->cm_website) . '" /></td>
</tr>';
}
$r .= '
<tr>
<td colspan="2"><label for="cm_body">Comment:</label><br />
<textarea name="cm_body" id="cm_body"></textarea>
</td>
</tr>';
// display securioty image if not logged in
if (!AuthUser::isLoggedIn(false))
{
$r .= '<tr>
<td><label for="vImageCodP">' . __('Security code') . '</label></td>
<td>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="65" valign="top"> <input size="3" maxlength="4" type="text" name="vImageCodP" /></td>
<td valign="top"><img src="' . get_url('login/securityImage/' . REQUEST_TIME . '/') . '" /></td>
</tr>
</table>
' . View::validation()->vImageCodP_error . '
</td>
</tr>';
}
$r .= '<tr>
<td colspan="2"><input type="submit" name="add_comment" class="add_comment button large" value="' . __('Submit Comment') . '" />
<input type="hidden" name="cm_wid" value="' . $cell->id . '" />
</td>
</tr>
</table>
</form>';
return $r;
}
static function permalinkWallpaper($wallpaper)
{
return self::makeGoodUrl($wallpaper->id, 'wallpaper', $wallpaper->name);
}
/**
* make permlaink to different page types
*
* @param int $id number|string for category
* @param int $type string (profile,category,front,tag,wallpaper,download)
* @param int $name string
* @param int $page number|{page}
* @param int $order string (downloads,rating,hottest,latest)
*
*/
static function makeGoodUrl($id, $type, $name='', $page=1, $order='')
{
$r = '';
if ($page > 1 || $page == '{page}')
{
$r_extra = '_p' . $page;
}
// add order
$arr_order = array('downloads' => 1, 'rating' => 1, 'hottest' => 1, 'latest' => 1);
if ($arr_order[$order])
{
$r_extra .= '_' . $order;
}
switch ($type)
{
case 'profile':
$w_permalink = ($name ? Inflector::slugify($name) : 'user') . '_w' . $id . '/';
$r .= 'profile/' . $id . '/';
break;
case 'category':
$w_permalink = ($name ? Inflector::slugify($name) : 'category') . '_w' . $id . '/';
$r .= 'category/' . $w_permalink . ($r_extra ? trim($r_extra, '_') . '.html' : '');
break;
case 'front':
$r .= $id . ($r_extra ? trim($r_extra, '_') . '.html' : '');
break;
case 'tag':
$r .= 'tag/' . rawurlencode($id) . '/' . ($order ? $order . '/' . $page . '/' : '');
break;
case 'wallpaper':
$w_permalink = ($name ? Inflector::slugify($name) : 'wallpaper') . '_w' . $id;
$r .= 'wallpaper/' . $w_permalink . '.html';
break;
case 'download':
$w_permalink = ($name ? Inflector::slugify($name) : 'wallpaper') . '_w' . $id;
$r .= 'download/' . $w_permalink . '.html';
break;
}
return get_url($r);
}
static function getPercentage($value, $max_value)
{
if ($max_value)
{
$r = round(($value * 100) / $max_value);
}
else
{
$r = 0;
}
return $r;
}
static function makeValidUrl($url, $skip_email=true)
{
if ($skip_email && strpos($url, "@") !== false)
{
$url = '';
}
if ($url)
{
if (strpos($url, 'http') === false)
{
$url = 'http://' . $url;
}
}
return $url;
}
}