mambax7/marquee

View on GitHub
plugins/smartpartner.php

Summary

Maintainability
B
5 hrs
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
 */

// Script to list recent partners from the smartpartner module (tested with smartparnter 1.2)
function b_marquee_smartpartner($limit, $dateFormat, $itemsSize)
{
    $block = $newObjects = [];
    if (!defined('SMARTPARTNER_DIRNAME')) {
        define('SMARTPARTNER_DIRNAME', 'smartpartner');
    }
    require_once XOOPS_ROOT_PATH . '/modules/' . SMARTPARTNER_DIRNAME . '/include/common.php';
    // Creating the partner handler object
    $smartpartnerPartnerHandler  = smartpartner_gethandler('partner');
    $smartpartnerCategoryHandler = smartpartner_gethandler('category');
    // Randomize
    $partnersObj = $smartpartnerPartnerHandler->getPartners(0, 0, _SPARTNER_STATUS_ACTIVE);
    if (count($partnersObj) > 1) {
        $keyArray = array_keys($partnersObj);
        $keyRand  = array_rand($keyArray, count($keyArray));
        for ($i = 0; ($i < count($partnersObj)) && ($i < $limit); ++$i) {
            $newObjects[$i] = $partnersObj[$keyRand[$i]];
        }
        $partnersObj = $newObjects;
    }
    $catId = [];
    foreach ($partnersObj as $partnerObj) {
        if (!in_array($partnerObj->categoryid(), $catId, true)) {
            $catId[] = $partnerObj->categoryid();
        }
    }
    if ($partnersObj) {
        foreach ($catId as $j => $jValue) {
            $categoryObj = $smartpartnerCategoryHandler->get($jValue);
            for ($i = 0, $iMax = count($partnersObj); $i < $iMax; ++$i) {
                if ($partnersObj[$i]->categoryid() == $jValue) {
                    $smartConfig = smartpartner_getModuleConfig();
                    if ($itemsSize > 0) {
                        $title = xoops_substr($partnersObj[$i]->title(), 0, $itemsSize + 3);
                    } else {
                        $title = $partnersObj[$i]->title();
                    }
                    $block[] = [
                        'date'     => '',
                        'category' => '',
                        'author'   => '',
                        'title'    => $title,
                        'link'     => "<a href='" . XOOPS_URL . '/modules/smartpartner/partner.php?id=' . $partnersObj[$i]->id() . "'>" . $title . '</a>',
                    ];
                }
            }
        }
    }

    return $block;
}