-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
107 lines (90 loc) · 2.17 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* @package Random Video Module
* @version 1.0.1
* @author Nerudas - nerudas.ru
* @copyright Copyright (c) 2013 - 2018 Nerudas. All rights reserved.
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
* @link https://nerudas.ru
*/
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;
use Joomla\CMS\Language\Text;
use Joomla\Utilities\ArrayHelper;
use Joomla\CMS\Uri\Uri;
jimport('joomla.filesystem.file');
class modRandomVideoHelper
{
/**
* Module params object
*
* @var Registry
*
* @since 1.0.0
*/
protected static $_params = null;
/**
* Form submit method
*
* @return bool|object
*
* @since 1.0.0
*/
public static function getAjax()
{
$params = self::getParams();
if (!$params)
{
throw new Exception(Text::_('MOD_JFORM_ERROR_MODULE_NOT_FOUND'), 404);
return false;
}
if (!$videos = $params->get('videos', 0))
{
throw new Exception(Text::_('MOD_JFORM_ERROR_VIDEOS_NOT_FOUND'), 404);
return false;
}
$videos = ArrayHelper::fromObject($videos, false);
$count = count($videos);
$keys = array_keys($videos);
$key = $keys[rand(0, ($count - 1))];
$video = $videos[$key];
$video->src = trim($video->src, '/\/');
$video->type = JFile::getExt(JPATH_ROOT . '/' . $video->src);
$video->src = Uri::root(true) . '/' . $video->src;
return $video;
}
/**
* Method to get Module object
*
* @param int $pk Module id
*
* @return bool|Registry Module params object on success, false on failure
*
* @since 1.0.0
*/
protected static function getParams($pk = null)
{
$pk = (empty($pk)) ? Factory::getApplication()->input->get('module_id', 0) : $pk;
if (empty(self::$_params))
{
try
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('params')
->from('#__modules')
->where('id =' . $pk)
->where('module = ' . $db->quote('mod_randomvideo'));
$db->setQuery($query);
$params = $db->loadResult();
self::$_params = (!empty($params)) ? new Registry($params) : false;
}
catch (Exception $e)
{
self::$_params = false;
}
}
return self::$_params;
}
}