<?php
/**
*
* @package - phpbb3 UPS Easy Shop
* @version $Id: functions_shop.php 169 2010-03-04 06:31:32Z Wuerzi $
* @copyright (c) Wuerzi (http://spieleresidenz.de), (c) femu (http://die-muellers.org)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/*
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Set shop config value. Creates missing config entry.
*/
function set_shop_config($config_name, $config_value)
{
global $db, $cache, $config, $shop_config;
$sql = 'UPDATE ' . SHOP_CONFIG_TABLE . "
SET config_value = '" . $db->sql_escape($config_value) . "'
WHERE config_name = '" . $db->sql_escape($config_name) . "'";
$db->sql_query($sql);
if (!$db->sql_affectedrows() && !isset($shop_config[$config_name]))
{
$sql = 'INSERT INTO ' . SHOP_CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
'config_name' => $config_name,
'config_value' => $config_value,
));
$db->sql_query($sql);
}
$shop_config[$config_name] = $config_value;
}
class item
{
var $data; //holds everything taken from phpbb_items (eg name, description, price, quantity)
var $notrash = false; //set to true by buy function when instant_used so it isn't removed from inventory
function item($db_info)
{
$this->data = $db_info;
// Here, we load custom config values
$this->init();
}
function init()
{
// Blank by default
}
function get_actions()
{
global $user, $shop, $db, $config, $shop_config;
$actions = array();
if ($this->data['can_sellback'])
{
$actions['sellback'] = array(
'name' => sprintf($user->lang['SHOP_ITEM_SELLBACK'], $shop->cash->format_cash_string($this->data['sell_price']), $config['points_name']),
'confirm' => sprintf($user->lang['SHOP_ITEM_CONFIRM_SELL'], $this->data['name'], $shop->cash->format_cash_string($this->data['sell_price']), $config['points_name']),
'function' => 'sellback'
);
}
if ($this->data['can_trash'])
{
$actions['trash'] = array(
'name' => $user->lang['SHOP_ITEM_TRASH'],
'confirm' => sprintf($user->lang['SHOP_ITEM_CONFIRM_TRASH'], $this->data['name']),
'function' => 'trash'
);
}
if ($this->data['can_display'] && $config['shop_shelf_size_sig'] != 0)
{
// Find out how many items are currently on the shelf
$sql = 'SELECT *
FROM ' . SHOP_ITEMS_USER_TABLE . '
WHERE user = ' . (int) $user->data['user_id'] . '
AND shelf_display = true';
$db->sql_query($sql);
if ($this->data['shelf_display'])
{
$actions['display'] = array(
'name' => $user->lang['SHOP_ITEM_DISPLAY_REMOVE'],
'confirm' => false,
'function' => 'display',
);
}
elseif ($db->sql_affectedrows() == $config['shop_shelf_size_sig'])
{
$actions['display'] = array(
'name' => $user->lang['SHOP_ITEM_DISPLAY_FULL'],
'confirm' => false,
'function' => 'none',
);
}
else
{
$actions['display'] = array(
'name' => $user->lang['SHOP_ITEM_DISPLAY'],
'confirm' => false,
'function' => 'display',
);
}
}
return $actions;
}
// This removes one instance of an item from the user's inventory.
function remove_item()
{
global $db, $user, $shop, $phpbb_root_path, $phpEx;
if ($this->notrash)
{
return false;
}
$sql = 'UPDATE ' . SHOP_ITEMS_USER_TABLE . '
SET quantity_user = quantity_user - 1
WHERE item=' . (int) $this->data['item'] . '
AND user=' . (int) $user->data['user_id'];
$db->sql_query($sql);
$sql = 'DELETE FROM ' . SHOP_ITEMS_USER_TABLE . '
WHERE quantity_user = 0';
$db->sql_query($sql);
}
function action_handler($action, $confirm = true)
{
$actions = $this->get_actions();
if (!isset($actions[$action]))
{
return false;
}
if (!$confirm or !$actions[$action]['confirm'] or confirm_box(true))
{
if ($actions[$action]['function'] == 'none')
{
return;
}
else
{
return $this->$actions[$action]['function'](); // Greatest single line of code I've ever written
}
}
else
{
$s_hidden_fields = build_hidden_fields(array(
'submit' => true,
)
);
confirm_box(false, $actions[$action]['confirm'], $s_hidden_fields);
return false;
}
}
function display()
{
global $db, $user;
if ($this->data['shelf_display'] == 1)
{
$this->data['shelf_display'] = 0;
}
else
{
$this->data['shelf_display'] = 1;
}
$sql = 'UPDATE ' . SHOP_ITEMS_USER_TABLE . '
SET shelf_display=' . $this->data['shelf_display'] . '
WHERE item=' . (int) $this->data['item'] . '
AND user=' . (int) $user->data['user_id'];
$db->sql_query($sql);
return sprintf(($this->data['shelf_display'] == 1)?$user->lang['SHOP_ITEM_SHELF_ADDED']:$user->lang['SHOP_ITEM_SHELF_REMOVED'], $this->data['name']);
}
function trash()
{
global $db, $user, $shop, $phpbb_root_path, $phpEx;
$this->remove_item();
return sprintf($user->lang['SHOP_ITEM_TRASHED'], $this->data['name']);
}
function sellback()
{
global $db, $user, $shop, $phpbb_root_path, $phpEx;
// This part is exactly like throwing the item out
$this->remove_item();
// But then, they get points!!!
$shop->cash->deposit_user($this->data['sell_price']);
// And put it back in the shop
$sql = 'UPDATE ' . SHOP_ITEMS_TABLE . '
SET quantity = quantity + 1
WHERE item=' . (int) $this->data['item'];
$db->sql_query($sql);
return sprintf($user->lang['SHOP_ITEM_SOLD'], $this->data['name']);
}
// This function sees if the user is able to buy and item and charges the user if so
// it doesn't move the item from the shop to the user's inventory
// if item can't be bought, it won't return
function prebuy()
{
global $user, $shop, $db, $phpEx, $phpbb_root_path, $config, $shop_config;
// Eventually, we'll have an auth check here too. for now, all registered users can use the shop
if (!$user->data['is_registered'])
{
$db->sql_transaction('rollback');
trigger_error('SHOP_NOT_PERMITTED');
}
if (!$this->data['quantity'])
{
$db->sql_transaction('rollback');
$meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
meta_refresh(3, $meta_info);
$message = $user->lang['SHOP_SOLD_OUT'] . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
trigger_error($message);
}
// Charge the user
if (!$shop->cash->charge_user($this->data['price']))
{
$db->sql_transaction('rollback');
$meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
meta_refresh(3, $meta_info);
$message = sprintf($user->lang['SHOP_INSUFFICIENT_FUNDS'], $config['points_name']) . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
trigger_error($message);
}
// Decrease the quantity in the shop
$sql = 'UPDATE ' . SHOP_ITEMS_TABLE . '
SET quantity = quantity - 1
WHERE item=' . (int) $this->data['item'];
$db->sql_query($sql);
}
function use_item()
{
// We only define this function so the program doesn't crash
// if somebody decides to make an item that doesn't have a use action set
// an instant use item
return false;
}
function buy()
{
global $template, $user, $shop, $db, $phpEx, $phpbb_root_path, $config, $shop_config;
$db->sql_transaction('begin');
$this->prebuy(); // Returns if purchase is allowed, user was charged, and quantity was decreased
// Read out config data
$sql = 'SELECT config_name, config_value
FROM ' . SHOP_CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$shop_config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
// Send PM to user if that option is on
if ($shop_config['shop_pm_on_buy'] == 1)
{
require($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
$my_subject = utf8_normalize_nfc($user->lang['SHOP_ITEM_PM_SUBJECT']);
$my_text = utf8_normalize_nfc(sprintf($user->lang['SHOP_ITEM_PM_MESSAGE'], $user->data['username'], $this->data['name'], $shop->cash->format_cash_string($this->data['price']), $config['points_name'], $config['shop_name']));
$pm_admin = $shop_config['shop_pm_admin'];
$poll = $uid = $bitfield = $options = '';
generate_text_for_storage($my_subject, $uid, $bitfield, $options, false, false, false);
generate_text_for_storage($my_text, $uid, $bitfield, $options, true, true, true);
$data = array(
'address_list' => array ('u' => array($user->data['user_id'] => 'to')),
'from_user_id' => $pm_admin,
'from_username' => $user->data['username'],
'icon_id' => 0,
'from_user_ip' => '',
'enable_bbcode' => true,
'enable_smilies' => true,
'enable_urls' => true,
'enable_sig' => true,
'message' => $my_text,
'bbcode_bitfield' => $bitfield,
'bbcode_uid' => $uid,
);
submit_pm('post', $my_subject, $data, false);
}
if ($this->data['instant_use'])
{
$this->notrash = true;
// This way, if it returns false, we still buy the item like normal
if ($message = $this->use_item())
{
$meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
meta_refresh(3, $meta_info);
$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
trigger_error($message);
}
}
$sql = 'UPDATE ' . SHOP_ITEMS_USER_TABLE . '
SET quantity_user = quantity_user + 1
WHERE item=' . (int) $this->data['item'] . '
AND user=' . (int) $user->data['user_id'];
$db->sql_query($sql);
if ($db->sql_affectedrows() != 1)
{
$sql = 'INSERT INTO ' . SHOP_ITEMS_USER_TABLE . ' ' . $db->sql_build_array('INSERT', array(
'item' => $this->data['item'],
'user' => $user->data['user_id'],
'quantity_user' => 1));
$db->sql_query($sql);
}
$db->sql_transaction('commit');
// Everything worked, send a success message
$meta_info = append_sid("{$phpbb_root_path}shop.$phpEx");
meta_refresh(3, $meta_info);
$message = sprintf($user->lang['SHOP_ITEM_BOUGHT'], $this->data['name']) . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $meta_info . '">', '</a>');
trigger_error($message);
}
}
class shop
{
var $cash;
// Constructor
function shop()
{
global $config;
// Select money system
$this->cash = new ups_points;
}
function get_item_by_row($row)
{
global $phpbb_root_path, $phpEx;
include_once($phpbb_root_path . 'includes/shop/items/' . $row['backend'] . '.' . $phpEx);
return new $row['backend']($row);
}
function get_item($item_id, $condition = false)
{
global $db;
$condition = $condition?' && ' . $condition:'';
$sql = 'SELECT *
FROM ' . SHOP_ITEMS_TABLE . '
WHERE item=' . (int) $item_id . $condition;
$result = $db->sql_query($sql);
if ($db->sql_affectedrows() != 1)
{
return false;
}
return $this->get_item_by_row($db->sql_fetchrow($result));
}
function get_item_list($condition = false)
{
global $db, $shop_config;
// Read out config data
$sql = 'SELECT config_name, config_value
FROM ' . SHOP_CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$shop_config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$start = request_var('start', 0);
$number = $shop_config['items_per_page'];
$condition = $condition?' WHERE ' . $condition:'';
$sql = 'SELECT *
FROM ' . SHOP_ITEMS_TABLE . $condition . '
ORDER BY item_order';
$result = $db->sql_query_limit($sql, $number, $start);
$items = array();
if ($db->sql_affectedrows() != 0)
{
while($item_row = $db->sql_fetchrow($result))
{
$items[] = $this->get_item_by_row($item_row);
}
return $items;
}
else
{
return false;
}
}
function get_item_user($item_id, $user_id = NULL, $condition = false)
{
global $db, $user;
$user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
$condition = $condition?' && ' . $condition:'';
$sql = 'SELECT *
FROM ( ' . SHOP_ITEMS_TABLE . '
INNER JOIN ' . SHOP_ITEMS_USER_TABLE . '
ON ' . SHOP_ITEMS_USER_TABLE . '.item = ' . SHOP_ITEMS_TABLE . '.item)
WHERE user = ' . (int) $user_id . '
AND item_user=' . (int) $item_id . $condition;
$result = $db->sql_query($sql);
if ($db->sql_affectedrows() != 1)
{
return false;
}
return $this->get_item_by_row($db->sql_fetchrow($result));
}
function get_item_list_ucp($user_id = NULL, $condition = false)
{
global $db, $user;
// Read out config data
$sql = 'SELECT config_name, config_value
FROM ' . SHOP_CONFIG_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$shop_config[$row['config_name']] = $row['config_value'];
}
$db->sql_freeresult($result);
$start = request_var('start', 0);
$number = $shop_config['items_per_page'];
$user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
$condition = $condition?' && ' . $condition:'';
$sql = 'SELECT *
FROM ( ' . SHOP_ITEMS_TABLE . '
INNER JOIN ' . SHOP_ITEMS_USER_TABLE . '
ON ' . SHOP_ITEMS_USER_TABLE . '.item = ' . SHOP_ITEMS_TABLE . '.item)
WHERE user = ' . (int) $user_id . $condition . '
ORDER BY item_order';
$result = $db->sql_query_limit($sql, $number, $start);
$items = array();
if ($db->sql_affectedrows() != 0)
{
while ($item_row = $db->sql_fetchrow($result))
{
$items[] = $this->get_item_by_row($item_row);
}
return $items;
}
else
{
return false;
}
}
function get_item_list_user($user_id = NULL, $condition = false)
{
global $db, $user;
$user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
$condition = $condition?' && ' . $condition:'';
$sql = 'SELECT *
FROM ( ' . SHOP_ITEMS_TABLE . '
INNER JOIN ' . SHOP_ITEMS_USER_TABLE . '
ON ' . SHOP_ITEMS_USER_TABLE . '.item = ' . SHOP_ITEMS_TABLE . '.item)
WHERE user = ' . (int) $user_id . $condition . '
ORDER BY item_order';
$result = $db->sql_query($sql);
$items = array();
if ($db->sql_affectedrows() != 0)
{
while ($item_row = $db->sql_fetchrow($result))
{
$items[] = $this->get_item_by_row($item_row);
}
return $items;
}
else
{
return false;
}
}
}
class ups_points
{
// Constructor
function ups_points()
{
global $phpbb_root_path, $phpEx;
if (!class_exists('ups_points'))
{
require($phpbb_root_path . 'includes/points/functions_points.' . $phpEx);
}
$this->money_on = true;
}
function format_cash_string($amount = NULL)
{
$amount = is_null($amount) ? $this->user_get_cash_amount() : $amount;
return number_format_points($amount);
}
function deposit_user($amount, $user_id = NULL)
{
global $user;
$user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
add_points($user_id, $amount);
return true;
}
function charge_user($amount, $user_id = NULL)
{
global $user;
$user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
if ($amount > $user->data['user_points'])
{
return false;
}
substract_points($user_id, $amount);
return true;
}
function user_get_cash_amount($user_id = NULL)
{
global $user;
$user_id = is_null($user_id) ? $user->data['user_id'] : $user_id;
return $user->data['user_points'];
}
}
?>