Skip to content

About

Displays randomly items from the store in a sidebox.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

FusionCMS Random Store Item Sidebox

A custom FusionCMS 9.x sidebox that displays a random item from the FusionCMS Store.

The sidebox reads directly from the existing store_items table and does not require a separate shop or item table.

image

Features

  • 🎲 Displays a random Store item
  • 🌐 Works across all realms
  • 🏰 Displays the realm belonging to the selected item
  • 💰 Supports Vote Points (vp_price)
  • 💎 Supports Donation Points (dp_price)
  • 📦 Displays stack size when greater than 1
  • 🖼️ Supports item icons
  • 🛒 Includes a link to the Store
  • 🔄 Selects a new random item on page load
  • 🚫 No modification of the existing Store module required

Requirements

  • FusionCMS 9.x
  • CodeIgniter 4 / FusionCMS framework
  • Existing FusionCMS Store module
  • store_items database table
  • A configured FusionCMS realm system

Installation

1. Copy the module

Copy the following directory into your FusionCMS installation:

application/modules/sidebox_random_store/

The module structure should look like:

sidebox_random_store/
├── controllers/
│   └── Random_store.php
├── models/
│   └── Random_store_model.php
├── views/
│   └── sidebox_random_store.tpl
└── css/
    └── random_store.css

2. Controller

controllers/Random_store.php

<?php

use MX\MX_Controller;

/**
 * Random Store Item Sidebox
 *
 * @property random_store_model $random_store_model
 */
class Random_store extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('sidebox_random_store/random_store_model');
    }

    public function view()
    {
        $item = $this->random_store_model->getRandomItem();

        $realm = null;

        if ($item && !empty($item['realm'])) {
            $realmObject = $this->realms->getRealm((int) $item['realm']);

            if ($realmObject) {
                $realm = [
                    'id'   => $realmObject->getId(),
                    'name' => $realmObject->getName()
                ];
            }
        }

        $data = [
            'module' => 'sidebox_random_store',
            'item'   => $item,
            'realm'  => $realm
        ];

        return $this->template->loadPage(
            'sidebox_random_store/sidebox_random_store.tpl',
            $data
        );
    }
}

3. Model

models/Random_store_model.php

<?php

class Random_store_model extends CI_Model
{
    /**
     * Get a random Store item from any realm.
     */
    public function getRandomItem(): ?array
    {
        $item = $this->db
            ->table('store_items')
            ->orderBy('RAND()', '', false)
            ->get(1)
            ->getRowArray();

        return $item ?: null;
    }
}

The query intentionally has no realm restriction:

SELECT *
FROM store_items
ORDER BY RAND()
LIMIT 1;

This means an item can be selected from any realm.

Template

views/sidebox_random_store.tpl

Example:

<link rel="stylesheet" href="{base_url()}application/modules/sidebox_random_store/css/random_store.css">
{if $item}

<div class="sidebox-random-store">

    <div class="sidebox-random-store__item">

        {if !empty($item.icon)}
            <div class="sidebox-random-store__icon">
				<img src="{$CI->config->item('api_item_icons')}/large/{$item.icon|escape}.jpg">

				{if !empty($item.itemcount) && $item.itemcount > 1}
					<span class="sidebox-random-store__stack">
						{$item.itemcount|escape}
					</span>
				{/if}
			</div>
        {/if}

        <div class="sidebox-random-store__info">

            <div class="sidebox-random-store__name">
                {$item.name|escape}
            </div>

            {if !empty($realm.name)}
                <div class="sidebox-random-store__realm">
                    {$realm.name|escape}
                </div>
            {/if}

            <div class="sidebox-random-store__price">

                {if !empty($item.vp_price)}
                    <div>
                        <strong>{$item.vp_price}</strong> Vote Points
                    </div>
                {/if}

                {if !empty($item.dp_price)}
				 or
                    <div>
                        <strong>{$item.dp_price}</strong> Donation Points
                    </div>
                {/if}

            </div>

        </div>

    </div>

    <a href="{base_url()}store" class="btn btn-primary btn-block">
        View in Store
    </a>

</div>

{else}

<div class="sidebox-random-store">
    <div class="sidebox-random-store__empty">
        No store items are available.
    </div>
</div>

{/if}

Note: The itemcount and icon field names may need to be adjusted if your FusionCMS installation uses different column names.

CSS

css/random_store.css

.sidebox-random-store {
    padding: 12px;
}

.sidebox-random-store__item {
    padding: 10px;
    margin-bottom: 12px;

    display: flex;
    align-items: center;
    gap: 12px;

    border: 1px solid rgba(255, 255, 255, .08);
    border-radius: 5px;
    background: rgba(0, 0, 0, .18);

    transition: .2s ease;
}

.sidebox-random-store__item:hover {
    border-color: rgba(255, 193, 7, .35);
    background: rgba(0, 0, 0, .28);
}

.sidebox-random-store__icon {
    position: relative;

    width: 58px;
    height: 58px;

    flex: 0 0 58px;
}

.sidebox-random-store__icon img {
    display: block;

    width: 58px;
    height: 58px;

    object-fit: contain;

    border-radius: 5px;
    border: 1px solid rgba(255, 255, 255, .15);
}

.sidebox-random-store__stack {
    position: absolute;

    right: 2px;
    bottom: 1px;

    min-width: 18px;
    height: 18px;
    padding: 0 4px;

    display: flex;
    align-items: center;
    justify-content: center;

    color: #fff;
    font-size: 12px;
    font-weight: 700;
    line-height: 18px;

    text-shadow:
        1px 1px 2px #000,
        -1px -1px 2px #000;

    pointer-events: none;
}

.sidebox-random-store__info {
    min-width: 0;
    flex: 1;
}

.sidebox-random-store__name {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebox-random-store__realm {
    margin-top: 3px;

    font-size: 11px;
    opacity: .65;
}

.sidebox-random-store__price {
    margin-top: 6px;

    font-size: 12px;
    line-height: 1.5;
}

.sidebox-random-store__price strong {
    font-weight: 700;
}

.sidebox-random-store .btn {
    width: 100%;
    display: block;
    text-align: center;
}

.sidebox-random-store__empty {
    padding: 10px 0;

    text-align: center;
    font-size: 12px;
    opacity: .65;
}

Loading the CSS

The CSS can be loaded from the sidebox template:

<link
    rel="stylesheet"
    href="{base_url()}application/modules/sidebox_random_store/css/random_store.css"
>

Alternatively, add it to the active FusionCMS theme's main stylesheet.

Enable the Sidebox

After installing the module:

  1. Log into the FusionCMS administration panel.
  2. Open the Sideboxes configuration.
  3. Add/enable the Random Store Item sidebox.
  4. Select the desired sidebar position.
  5. Configure the pages where it should appear.
  6. Save the configuration.

How Realm Selection Works

The sidebox does not use the current visitor's realm.

Instead, it selects one random item from the entire store_items table:

Realm 1 ─┐
Realm 2 ─┤
Realm 3 ─┼──> store_items ──> random item
Realm 4 ─┤
Realm 5 ─┘

After selecting the item, the item's realm value is used to retrieve the corresponding FusionCMS realm name.

For example:

Item:    Thunderfury
Realm:   Azeroth
Price:   250 Vote Points
Stack:   2

Database

No additional database table is required.

The sidebox uses the existing:

store_items

table.

Important fields used by the sidebox include:

realm
name
vp_price
dp_price

The icon and stack fields depend on the Store schema used by your FusionCMS installation.

Troubleshooting

Only Realm 1 appears

Make sure the model does not contain:

->where('realm', 1)

or:

->where('realm', $realmId)

The random query must not filter by realm.

You can check the database with:

SELECT realm, COUNT(*) AS items
FROM store_items
GROUP BY realm
ORDER BY realm;

Template cannot be found

If FusionCMS reports:

modules/news/views/sidebox_random_store.tpl

make sure the controller uses the module-qualified path:

$this->template->loadPage(
    'sidebox_random_store/sidebox_random_store.tpl',
    $data
);

The template should exist at:

application/modules/sidebox_random_store/views/sidebox_random_store.tpl

Price is not displayed

Check that your store_items table actually contains:

vp_price
dp_price

For debugging, temporarily add:

<pre>{$item|@print_r}</pre>

to the template and inspect the available fields.

Smarty cache

After changing the template, clear the FusionCMS template cache:

writable/cache/templates/

Then reload the page.

License

This project is licensed under the MIT License.

See the LICENSE file for the full license text.

About

Displays randomly items from the store in a sidebox.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages