Skip to content

Commit 753fe8a

Browse files
committed
Added compatibility for both J3 and J4
1 parent 3ea0760 commit 753fe8a

3 files changed

Lines changed: 58 additions & 10 deletions

File tree

helper.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class ModJDSimpleContactFormHelper {
1313

14+
const JOOMLA_VERSION = \Joomla\CMS\Version::MAJOR_VERSION;
15+
1416
public static function renderForm($params, $module) {
1517
$fields = $params->get('fields', []);
1618
foreach ($fields as $field) {
@@ -82,17 +84,35 @@ public static function submitForm($ajax = false) {
8284

8385
$captchaType = $params->get('captchaPlugins') == "" ? JFactory::getConfig()->get('captcha') : $params->get('captchaPlugins');
8486
JPluginHelper::importPlugin('captcha', $captchaType);
85-
$dispatcher = \Joomla\CMS\Factory::getApplication();
87+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
88+
$dispatcher = JEventDispatcher::getInstance();
89+
} else {
90+
$dispatcher = \Joomla\CMS\Factory::getApplication();
91+
}
8692

8793
if ( $captchaType == "recaptcha" ) {
88-
$check_captcha = $dispatcher->triggerEvent('onCheckAnswer', [ $jinput->get('recaptcha_response_field') ] );
94+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
95+
$check_captcha = $dispatcher->trigger('onCheckAnswer', $jinput->get('recaptcha_response_field'));
96+
} else {
97+
$check_captcha = $dispatcher->triggerEvent('onCheckAnswer', [ $jinput->get('recaptcha_response_field') ] );
98+
}
99+
89100
if (!$check_captcha[0]) {
90101
throw new \Exception(JText::_('Invalid Captcha'), 0);
91102
}
92103
} elseif ( $captchaType == "recaptcha_invisible" ) {
93-
$check_captcha = $dispatcher->triggerEvent('onCheckAnswer', [ $jinput->get('g-recaptcha-response') ] );
104+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
105+
$check_captcha = $dispatcher->trigger('onCheckAnswer', $jinput->get('g-recaptcha-response'));
106+
} else {
107+
$check_captcha = $dispatcher->triggerEvent('onCheckAnswer', [ $jinput->get('g-recaptcha-response') ] );
108+
}
109+
94110
} elseif (!empty($captchaType)) {
95-
$check_captcha = $dispatcher->triggerEvent('onCheckAnswer', [] );
111+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
112+
$check_captcha = $dispatcher->trigger('onCheckAnswer');
113+
} else {
114+
$check_captcha = $dispatcher->triggerEvent('onCheckAnswer', [] );
115+
}
96116
}
97117
}
98118

@@ -447,7 +467,12 @@ public static function uploadFile($name, $src) {
447467
$filename = JFile::makeSafe($fullFileName."_".mt_rand(10000000,99999999).".".$filetype);
448468

449469
$params = JComponentHelper::getParams('com_media');
450-
$allowable = array_map('trim', explode(',', $params->get('restrict_uploads_extensions')));
470+
471+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
472+
$allowable = array_map('trim', explode(',', $params->get('upload_extensions')));
473+
} else {
474+
$allowable = array_map('trim', explode(',', $params->get('restrict_uploads_extensions')));
475+
}
451476

452477
if ($filetype == '' || $filetype == false || (!in_array($filetype, $allowable) ))
453478
{
@@ -472,4 +497,9 @@ public static function uploadFile($name, $src) {
472497
return $return;
473498
}
474499
}
500+
501+
public static function getJoomlaVersion() {
502+
$jversion = new JVersion();
503+
return $jversion::MAJOR_VERSION;
504+
}
475505
}

layouts/fields/file.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121

2222
// fetching allowed types
2323
$params = JComponentHelper::getParams('com_media');
24-
$allowable = array_map('trim', explode(',', $params->get('restrict_uploads_extensions')));
24+
25+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
26+
$allowable = array_map('trim', explode(',', $params->get('upload_extensions')));
27+
} else {
28+
$allowable = array_map('trim', explode(',', $params->get('restrict_uploads_extensions')));
29+
}
30+
2531
$allowedMaxSize = $params->get('upload_maxsize');
2632
$document = JFactory::getDocument();
2733
$style = '.filesize-err {'
@@ -43,7 +49,6 @@
4349
File size is too big!
4450
</div>
4551

46-
4752
<?php
4853
// File size validation
4954
$js = 'var uploadField_'.$field->name.' = document.getElementById("' . $field->name . '-' .$module->id .'");';

tmpl/default.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@
4848
if ($captcha) {
4949
$captchaType = $params->get('captchaPlugins') == "" ? JFactory::getConfig()->get('captcha') : $params->get('captchaPlugins');
5050
JPluginHelper::importPlugin('captcha', $captchaType);
51-
$dispatcher = \Joomla\CMS\Factory::getApplication();
52-
$dispatcher->triggerEvent('onInit', ['jdscf_recaptcha_' . $module->id]);
51+
52+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
53+
$dispatcher = JEventDispatcher::getInstance();
54+
$dispatcher->trigger('onInit', ['jdscf_recaptcha_' . $module->id]);
55+
} else {
56+
$dispatcher = \Joomla\CMS\Factory::getApplication();
57+
$dispatcher->triggerEvent('onInit', ['jdscf_recaptcha_' . $module->id]);
58+
}
59+
5360
$plugin = JPluginHelper::getPlugin('captcha', $captchaType);
5461

5562
if ( $captchaType == "recaptcha" ) {
@@ -83,7 +90,13 @@
8390
// Display captcha plugin fields
8491
if (!empty($plugin)) {
8592
$plugin_params = new JRegistry($plugin->params);
86-
$captchaHtml = $dispatcher->triggerEvent('onDisplay', ['jdscf_recaptcha_' . $module->id, 'jdscf_recaptcha_' . $module->id]);
93+
94+
if( ModJDSimpleContactFormHelper::getJoomlaVersion() < 4 ) {
95+
$captchaHtml = $dispatcher->trigger('onDisplay', ['jdscf_recaptcha_' . $module->id, 'jdscf_recaptcha_' . $module->id]);
96+
} else {
97+
$captchaHtml = $dispatcher->triggerEvent('onDisplay', ['jdscf_recaptcha_' . $module->id, 'jdscf_recaptcha_' . $module->id]);
98+
}
99+
87100
if (!empty($captchaHtml)) {
88101
?>
89102
<div class="jdscf-col-md-12">

0 commit comments

Comments
 (0)