JoomGalleryfriends / JG4-dev

Development repository for JoomGallery v4.x
GNU General Public License v3.0
10 stars 6 forks source link

Messenger service #73

Closed Elfangor93 closed 1 year ago

Elfangor93 commented 1 year ago

What is this service for

The Messenger service will be used to send messenges to inform users about tasks beeing perfomed within the gallery. This is a direct replacement of the JoomMessenger helper class of JGv3. This service can send messages eighter via email or via personal messaging system of Joomla. The content of the message is created using the mail templating system of joomla.

How does it work

When you instantiate the service, you have to select a template and provide the values for the variables to be replaced in the chosen template. Additionally you can select a language in which the message should be sent. For this to work there has to be a mail template created for this specific language. By default only an english mail template is installed.

How to use this service

You can load the service anywhere in Joomla by doing

$component = Factory::getApplication()->bootComponent('com_joomgallery');  // boot JoomGallery component
$component->createMessenger('mail');   // instantiate service by choosing a sending method (available: 'mail','pm')
$msg = $component->getMessenger();   // load the service into your scope
$msg->selectTemplate('com_joomgallery.newimage'); // select the template by providing a template id
$msg->addTemplateData(array(...)); // provide all needed template variables as key value pairs
$msg->send('test@example.com'); // send message to the provided mail adress

As you can see, you first need to add the corresponding template before you can send messages with its content. With this service only one template is delivered: 'com_joomgallery.newimage'. This is a template to inform users about a new image beeing added (uploaded) to the gallery.

How to test the service

This service need the database to be modified in order to add the new mail template!

Make sure "Debug System" is "Yes" in Joomla Global configuration. Open the file /administrator/com_joomgallery/src/View/Faulties/HtmlView.php and insert the following code on line 62 after throw new \Exception(implode("\n", $errors)); }

Send a message via email

$this->component->createMessenger('mail');
$msg = $this->component->getMessenger();
$msg->selectTemplate('com_joomgallery.newimage');
$msg->addTemplateData(array('user' => '<USERNAME>', 'title' => '<IMAGENAME>', 'category' => '<CATEGORYNAME>'));
$msg->send('<test@example.ch>');

Send a message via personal messaging system of joomla

$this->component->createMessenger('pm');
$msg = $this->component->getMessenger();
$msg->selectTemplate('com_joomgallery.newimage');
$msg->addTemplateData(array('user' => '<USERNAME>', 'title' => '<IMAGENAME>', 'category' => '<CATEGORYNAME>'));
$msg->send('<USERID>');

Change content of mail template

Navigate in the Joomla backend to "System/Templates/Mail Templates/JoomGallery: New Image" and change the content of the template. When sending a new message the nes content should be sent...

MrMusic commented 1 year ago

Using the mail templates of Joomla is a cool feature. 👍

While testing, I noticed two things:

  1. Typo in language file: COM_JOOMGALLERY_MAIL_NEWIMAGE_TITLE="JoomGallery: New Iamge"

  2. I would like to be able to use 'more' variables The following variables would be helpful for use in the mail template for new images: Image-ID - Since the image titles are often not unique, then the ID would be helpful. Image-Title Category-ID - Since the category titles are often not unique, then the ID would be helpful. Category-Title User-ID User-Name User-Username (Login Name (Username))

Elfangor93 commented 1 year ago

The following variables would be helpful for use in the mail template for new images

What would be the new content of the mail template using the additional variables you suggested?

MrMusic commented 1 year ago

What would be the new content of the mail template using the additional variables you suggested?

Suggestion: COM_JOOMGALLERY_MAIL_NEWIMAGE_BODY="A new image has been added by \"{USER_USERNAME}\" at {SITEURL}.\nTitle of the image is \"{IMG_TITLE}\" and the ID is {IMG_ID}.\nIt has been added to the category \"{CAT_TITLE}\" with the ID {CAT_ID}." The admin can then still decide for himself whether other things such as the user id should also be shown.

There is one more thing: When uninstalling JoomGallery, all JoomGallery mail templates in the table #__mail_templates should be deleted.

MrMusic commented 1 year ago

@Elfangor93: Thanks. It looks good for me now (except the two conflicting files).