I have a module that creates thumbnails from the main image and over time the hosting fills with images, for that reason I want to add a maintenance button that deletes the previous images. It is from the administrator, not from the front-end. I have implemented ajax, but for some reason it doesn't work for me and it sends me the following message in broswer console:
Object { success: false, message: "The file at mod_my_module/helper.php does not exist.", messages: null, data: null }
I need your help, I leave the code used in module.
helper.php
use Joomla\String\StringHelper;
$com_path = JPATH_SITE . '/components/com_content/';
JLoader::register('ContentHelperRoute', $com_path . 'helpers/route.php');
JModelLegacy::addIncludePath($com_path . 'models', 'ContentModel');
abstract class ModMyModuleHelper
{
public function deleteImagesAjax() {
$minified_path = JPATH_ROOT . '/cache/mod_my_module_images';
if(JFolder::exists($minified_path)) {
JFolder::delete($minified_path);
}
die();
}
}
cleanimages.php This file is a custom field type for create button to delete images from module settings
jimport('joomla.form.formfield');
class JFormFieldCleanimages extends JFormField {
protected $type = 'Cleanimages';
protected function getInput() {
Jhtml::_('jquery.framework');
$doc = JFactory::getDocument();
$doc->addScriptDeclaration('jQuery(function($) {
$("#btn-reset-css").on("click", function(event) {
event.preventDefault();
var $this = $(this);
$this.text($this.data("loading"));
$.ajax({
url: "index.php?option=com_ajax&module=my_module&method=deleteImages&format=json",
type : "POST",
success: function (data) {
$this.text($this.data("text"));
console.log(data);
}
});
});
});');
return '<button id="btn-reset-css" class="btn btn-info" data-text="'. JText::_('Cleaned Images') .'" data-loading="'. JText::_('Cleaning Images') .'">'. JText::_('Clean Images') .'</button>';
}
}
UPDATE
Doing my own research and reviewing the component I have discovered that com_ajax looks for the helper.php file using JPATH_BASE
so when executing the button in the administrator it would be looking for helper.php inside the /administrator/modules/mod_my_module/helper.php
folder , If JPATH_BASE
were changed to JPATH_ROOT
, this would work in both admin and front-end but it is core and we cannot change it.
Given this, is there any way to do it without having to create a module for the administrator?
UPDATE 2 I have solved the problem by calling the full url from the front-end, the code below in the answers.