widgetfactory / jce

JCE - A Content Editor for Joomla
https://www.joomlacontenteditor.net
GNU General Public License v2.0
35 stars 12 forks source link

Please add a trigger to call system plugins. #92

Closed korenevskiy closed 3 years ago

korenevskiy commented 3 years ago

Please add a trigger to call system plugins. I use a plugin to rename files with the name of the date, time, and transliterate the name from Russian to English character. Add a trigger to call system plugins when dragging an image to the text area. So that you can change the file names on the fly while the files are in a temporary folder.

JDispatcher::getInstance()->trigger('onImageLoadBefore', [$images] ); and JDispatcher::getInstance()->trigger('onImageLoadAfter', [$images] );

ryandemmer commented 3 years ago

A number of custom events are available. A sample plugin is available that demonstates some of them - https://github.com/widgetfactory/wf_filesystem_events

You can also use the Joomla onContentAfterSave event to access an uploaded file for processing.

korenevskiy commented 3 years ago

@ryandemmer Do you think this is normal? You have made your own component and created triggers for yourself exclusively for your built-in plugins. Do you think developers should thoroughly study the guts of your component to create another plugin? Do you suggest using the onContentAfterSave method to parse and search for image tags in the article markup? Do you think a meaningless analysis of the article markup is needed to change the name of the image? Name the method call so that we in the plugin can simply rename the file name:

$_FILES['Filedata']['name'] = rename($_FILES['Filedata']['name'])

or

$files = JFactory::getApplication()->input->files; 
$file   = $files->get('file')[0];   
$file   = rename($file);
$files->set('file', [$file] );   

Is this possible? In Tinymce, this is possible, moreover, I use it everywhere.

korenevskiy commented 3 years ago

@ryandemmer I renamed the files when loading using the code specified in the system plugin constructor. But attempts to rename files in your component do not work either in the constructor or in another method of the system plugin. I understand that you don't owe anyone anything. But a normal component should support system plugins. And I really ask you to support the component of changing the names of images from the system plugins.

ryandemmer commented 3 years ago

Do you think this is normal?

Yes.

You have made your own component and created triggers for yourself exclusively for your built-in plugins.

No, these events are triggered via the global Joomla Event Dispatcher, so are available in any system plugin.

Do you think developers should thoroughly study the guts of your component to create another plugin?

I never said this was necessary.

Do you suggest using the onContentAfterSave method to parse and search for image tags in the article markup?

onContentAfterSave is one event you can use after upload to access the uploaded image.

Do you think a meaningless analysis of the article markup is needed to change the name of the image?

I don't know what you mean with this question.

korenevskiy commented 3 years ago

@ryandemmer I have already written above. What's using the construction $_FILES['Filedata']['name'] = rename($_FILES['Filedata']['name']) And a similar construction using JFactory::getApplication()->input->files.. I won't be able to change the name even before uploading it to the article. Using the code above, I can change the file name in TinyMCE . This code is working. But it is not working in your component., I could change the file name in the system plugin constructor, at the moment when the user drags the file with the mouse into the editor. .

The onContentAfterSave()method you suggest is very heavy. Because the onContentAfterSave() method accepts the HTML code of the article in the attributes. To change the image name, I need to find the image tag in the HTML code, find the image name in the tag. Then cut the HTML code several times to replace the file name and then glue the HTML code. And then I need to find this file in the Joomla folder and rename it there. The most difficult thing here is the analysis of the HTML code of the article. I'm trying to explain to you that it makes no sense to analyze the HTML code of the image in order to change the file name in the article. But you don't give me a choice. Your component can't give me the method of the system plugin so that I can change the name of the image without HTML analysis. If I specify this code in the onContentAfterSave() method, will the file name change? $_FILES['Filedata']['name'] = rename($_FILES['Filedata']['name'])

ryandemmer commented 3 years ago

The onContentAfterSave() method you suggest is very heavy. Because the onContentAfterSave() method accepts the HTML code of the article in the attributes. To change the image name, I need to find the image tag in the HTML code, find the image name in the tag. Then cut the HTML code several times to replace the file name and then glue the HTML code.

No, onContentBeforeSave and onContentAfterSave is a generic events used in a number of different scenarios. The important thing is the $context variable, which shows which component triggered the event. These events are used when an image is uploaded with the Joomla Media Manager - https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_media/controllers/file.php#L184-L203

I have made the method available via an event triggered in the JCE upload routine - https://github.com/widgetfactory/jce/blob/master/components/com_jce/editor/extensions/filesystem/joomla.php#L895-L909

So, you could do something like this, which would be used for both the Joomla Media Manager upload and JCE:

public function onContentAfterSave($context, &$object_file)
{
    if ($context !== 'com_jce.file' && $context !== 'com_media.file') {
        return;
    }

    // check it is an image...
    if (!preg_match('#\.(jpg|jpeg|png)#i', $object_file->filepath)) {
        return;
    }

    $path       = dirname($object_file->filepath);
    $extension  = pathinfo($path, PATHINFO_EXTENSION);

    $object_file->filepath = rename($object_file->filepath, $path . '/new_name.' . $extension);
}
korenevskiy commented 3 years ago

@ryandemmer Thank you, thank you. This can be done in the content plugin. And what method can be used in the system plugin?

korenevskiy commented 3 years ago

public function onContentPrepare($context, &$item, &$params, $page = 0) I used your recommendation in the system plugin in this method. Is it possible to do this in the system plugin? For earlier, thank you.

ryandemmer commented 3 years ago

onContentAfterSave and onContentBeforeSave can both be used in any system plugin.

korenevskiy commented 3 years ago

Thanks. For your help. In the system plugin, we can actually get the file name in the onContentAfterSave method. I rename it in this method. And to make sure, I save the new name in log.txt. I do it like this: $object_file->filepath = rename($object_file->filepath); Was: /home/e/exoffice/orelmusizo. ru/public_html/images/nova.png nova.png Became: /home/e/exoffice/orelmusizo. ru/public_html/images/20210512_nova.png I'm happy now to open the link in the browser: https://orelmusizo.ru/images/20210512_nova.png There is no picture!!!! The image still has the original path. And also in the TinyMCE editor, the image tag also has the original path. Why doesn't the path in the editor change to the new one?

The picture is still at the address https://orelmusizo.ru/images/nova.png