mambax7/marquee

View on GitHub
plugins/xoopspartners.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php declare(strict_types=1);
/**
 * ****************************************************************************
 * Marquee - MODULE FOR XOOPS
 * Copyright (c) Hervé Thouzard (https://www.herve-thouzard.com)
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright         Hervé Thouzard (https://www.herve-thouzard.com)
 * @license           GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
 * @author            Hervé Thouzard (https://www.herve-thouzard.com)
 *
 * Version :
 * ****************************************************************************
 *
 * @param $limit
 * @param $dateFormat
 * @param $itemsSize
 *
 * @return array
 */

use XoopsModules\Marquee\{
    Utility
};

// Script to list recent partners from the xoopspartners module (tested with version 1.1)
function b_marquee_xoopspartners($limit, $dateFormat, $itemsSize)
{
    $block    = [];
    $myts     = \MyTextSanitizer::getInstance();
    $arrayIds = [];
    $arrayIds = xoopspartners_random($limit);
    global $xoopsDB;
    foreach ($arrayIds as $id) {
        $sql = 'SELECT id, url, image, title FROM ' . $xoopsDB->prefix('partners') . " WHERE id=$id";
        $result = Utility::queryAndCheck($xoopsDB, $sql);
        [$id, $url, $image, $title] = $xoopsDB->fetchRow($result);
        $origtitle = $title;
        $title     = htmlspecialchars($title, ENT_QUOTES | ENT_HTML5);
        if ($itemsSize > 0) {
            $title = htmlspecialchars(mb_substr($origtitle, 0, 19), ENT_QUOTES | ENT_HTML5);
        } else {
            $title = htmlspecialchars($origtitle, ENT_QUOTES | ENT_HTML5);
        }
        $block[] = [
            'date'     => '',
            'category' => '',
            'author'   => '',
            'title'    => $title,
            'link'     => "<a href='" . XOOPS_URL . '/modules/xoopspartners/vpartner.php?id=' . $id . "'>" . $title . '</a>',
        ];
    }

    return $block;
}

/**
 * @param        $numberPartners
 * @param bool   $random
 * @param string $orden
 * @param string $desc
 *
 * @return array
 */
function xoopspartners_random($numberPartners, $random = true, $orden = '', $desc = '')
{
    global $xoopsDB;
    $PartnersId  = [];
    $ArrayReturn = [];
    $numrows     = 0;
    if ($random) {
        $sql  = 'SELECT id FROM ' . $xoopsDB->prefix('partners') . ' WHERE status = 1';
        $result = Utility::queryAndCheck($xoopsDB, $sql);
        $numrows = $xoopsDB->getRowsNum($result);
    } else {
        $result = $xoopsDB->query('SELECT id FROM ' . $xoopsDB->prefix('partners') . ' WHERE status = 1 ORDER BY ' . $orden . ' ' . $desc, $numberPartners);
    }
    while (false !== ($ret = $xoopsDB->fetchArray($result))) {
        $PartnersId[] = $ret['id'];
    }
    if (($numrows <= $numberPartners) || (!$random)) {
        return $PartnersId;
    }
    $numberTotal  = 0;
    $totalPartner = count($PartnersId) - 1;
    while ($numberPartners > $numberTotal) {
        $RandomPart = random_int(0, $totalPartner);
        if (!in_array($PartnersId[$RandomPart], $ArrayReturn, true)) {
            $ArrayReturn[] = $PartnersId[$RandomPart];
            ++$numberTotal;
        }
    }

    return $ArrayReturn;
}