-
Notifications
You must be signed in to change notification settings - Fork 206
/
helper.php
187 lines (159 loc) · 5.58 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @package Gantry 5
* @author RocketTheme http://www.rockettheme.com
* @copyright Copyright (C) 2007 - 2021 RocketTheme, LLC
* @license GNU/GPLv2 and later
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
defined('_JEXEC') or die;
use Gantry\Component\Content\Block\ContentBlockInterface;
use Gantry\Component\Content\Block\HtmlBlock;
use Gantry\Debugger;
use Gantry\Framework\Gantry;
use Gantry\Framework\Platform;
use Gantry\Framework\Theme;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ModuleHelper;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;
/**
* Class ModGantry5ParticleHelper
*/
class ModGantry5ParticleHelper
{
/**
* Serve module AJAX requests in 'index.php?option=com_ajax&module=gantry5_particle&format=json'.
*
* @return array|null|string
*/
public static function getAjax()
{
/** @var CMSApplication $app */
$app = Factory::getApplication();
$input = $app->input;
$format = strtolower($input->getCmd('format', 'html'));
$id = $input->getInt('id');
$props = $_GET;
unset($props['option'], $props['module'], $props['format'], $props['id']);
return static::ajax($id, $props, $format);
}
/**
* @param $id
* @param array $props
* @param string $format
* @return array|null|string
*/
public static function ajax($id, $props = [], $format = 'raw')
{
if (!in_array($format, ['json', 'raw', 'debug'])) {
throw new \RuntimeException(Text::_('JERROR_PAGE_NOT_FOUND'), 404);
}
$gantry = Gantry::instance();
/** @var Platform $platform */
$platform = $gantry['platform'];
$module = $platform->getModule($id);
// Make sure that module really exists.
if (!is_object($module) || strpos($module->module, 'gantry5') === false) {
throw new \RuntimeException(Text::_('JERROR_PAGE_NOT_FOUND'), 404);
}
$attribs = ['style' => 'gantry'];
/** @var CMSApplication $app */
$app = Factory::getApplication();
// Trigger the onRenderModule event.
if ($app->get('dispatcher')) {
$app->get('dispatcher')->triggerEvent('onRenderModule', ['module' => &$module, 'attribs' => &$attribs]);
}
$params = new Registry($module->params);
$params->set('ajax', $props);
$block = static::render($module, $params);
$data = json_decode($params->get('particle'), true);
$type = $data['type'] . '.' . $data['particle'];
$identifier = static::getIdentifier($data['particle'], $module->id);
$html = (string) $block;
if ($format === 'raw') {
return $html;
}
return ['code' => 200, 'type' => $type, 'id' => $identifier, 'props' => (object) $props, 'html' => $html];
}
/**
* @param object $module
* @param object $params
* @return ContentBlockInterface
*/
public static function render($module, $params)
{
if (\GANTRY_DEBUGGER) {
Debugger::addMessage("Particle Module #{$module->id} was not cached");
}
$data = json_decode($params->get('particle'), true);
$type = $data['type'];
$particle = $data['particle'];
$gantry = Gantry::instance();
if ($gantry->debug()) {
$enabled_outline = $gantry['config']->get("particles.{$particle}.enabled", true);
$enabled = isset($data['options']['particle']['enabled']) ? $data['options']['particle']['enabled'] : true;
$location = (!$enabled_outline ? 'Outline' : (!$enabled ? 'Module' : null));
if ($location) {
$block = HtmlBlock::create();
$block->setContent(sprintf('<div class="alert alert-error">The Particle has been disabled from the %s and won\'t render.</div>', $location));
return $block;
}
}
$id = static::getIdentifier($particle, $module->id);
$object = (object) array(
'id' => $id,
'type' => $type,
'subtype' => $particle,
'attributes' => $data['options']['particle'],
);
$context = array(
'gantry' => $gantry,
'inContent' => true,
'ajax' => $params->get('ajax'),
);
/** @var Theme $theme */
$theme = $gantry['theme'];
$block = $theme->getContent($object, $context);
// Create outer block with the particle ID for AJAX calls.
$outer = \Gantry\Component\Content\Block\HtmlBlock::create();
$outer->setContent('<div id="' . $id . '-particle" class="g-particle">' . $block->getToken() . '</div>');
$outer->addBlock($block);
return $outer;
}
/**
* @param $module
* @param $params
* @return array
*/
public static function cache($module, $params)
{
return static::render($module, $params)->toArray();
}
/**
* @param $module
* @param $params
* @param $cacheparams
* @return ContentBlockInterface|null
*/
public static function moduleCache($module, $params, $cacheparams)
{
$block = (array) ModuleHelper::moduleCache($module, $params, $cacheparams);
try {
return $block ? HtmlBlock::fromArray($block) : null;
} catch (Exception $e) {
return null;
}
}
/**
* @param string $particle
* @param string $id
* @return string
*/
public static function getIdentifier($particle, $id)
{
return "module-{$particle}-{$id}";
}
}