Coder Social home page Coder Social logo

I need more examples about notify-plugin HOT 7 CLOSED

rainlab avatar rainlab commented on July 22, 2024
I need more examples

from notify-plugin.

Comments (7)

LukeTowers avatar LukeTowers commented on July 22, 2024

Registering event listeners for the plugin in Plugin.php -> boot():

\RainLab\Notify\Classes\Notifier::bindEvents([
    'captive.xm.shifts.staffmember.add' => \Captive\XM\Classes\Notify\Events\StaffAdded::class,
    'captive.xm.shifts.staffmember.remove' => \Captive\XM\Classes\Notify\Events\StaffRemoved::class
]);

Registering the custom plugin's support for the notify plugin in custom plugin's Plugin.php:

public function registerNotificationRules()
    {
        return [
            'events' => [
                \Captive\XM\Classes\Notify\Events\StaffAdded::class,
                \Captive\XM\Classes\Notify\Events\StaffRemoved::class,
            ],
            'actions' => [
                \Captive\XM\Classes\Notify\Actions\SendEmail::class,
                \Captive\XM\Classes\Notify\Actions\SendSMS::class,
            ],
            'conditions' => [],
            'groups' => [
                'shifts' => [
                    'label' => 'Shifts',
                    'icon' => 'icon-calendar'
                ],
            ],
            'presets' => '$/captive/xm/classes/notify/presets.yaml',
        ];
    }

presets.yaml

# ===================================
#  Event Presets
# ===================================

member_added:
    name: Member has been added to a shift
    event: Captive\XM\Classes\Notify\Events\StaffAdded
    items:
        - action: Captive\XM\Classes\Notify\Actions\SendSMS
          template: 'You are assigned to {{ shiftName }} - {{ eventName }} as a {{ role }} for {{ date }}. Details at {{ url }}'
        - action: Captive\XM\Classes\Notify\Actions\SendEmail
          mail_template: captive.xm::mail.shift.member_added
          file_name: myshift.ics
    conditions:
        - condition: RainLab\Notify\NotifyRules\ExecutionContextCondition
          subcondition: environment
          operator: is
          value: prod
          condition_text: Application environment <span class="operator">is</span> prod
member_removed:
    name: Member has been removed from a shift
    event: Captive\XM\Classes\Notify\Events\StaffRemoved
    items:
        - action: Captive\XM\Classes\Notify\Actions\SendSMS
          template: You have been removed from {{ shiftName }} - {{ eventName }} for {{ date }}.
        - action: Captive\XM\Classes\Notify\Actions\SendEmail
          mail_template: captive.xm::mail.shift.member_removed
    conditions:
        - condition: RainLab\Notify\NotifyRules\ExecutionContextCondition
          subcondition: environment
          operator: is
          value: prod
          condition_text: Application environment <span class="operator">is</span> prod

SendEmail.php action:

<?php namespace Captive\XM\Classes\Notify\Actions;

use Mail;
use System\Models\MailTemplate;

class SendEmail extends \RainLab\Notify\Classes\ActionBase
{
    /**
     * Returns information about this event, including name and description.
     */
    public function actionDetails()
    {
        return [
            'name'        => 'Send an email',
            'description' => 'Send an email to a staff member',
            'icon'        => 'icon-envelope-o'
        ];
    }

    /**
     * Field configuration for the action.
     */
    public function defineFormFields()
    {
        return 'fields.yaml';
    }

    public function getText()
    {
        $template = $this->host->mail_template;

        return 'Send a message using template: ' . $template;
    }

    public function getMailTemplateOptions()
    {
        $codes = array_keys(MailTemplate::listAllTemplates());
        return array_combine($codes, $codes);;
    }

    /**
     * Triggers this action.
     * @param array $params
     * @return void
     */
    public function triggerAction($params)
    {
        $emailCode = $this->host->mail_template;
        $fileName  = $this->host->file_name;
        $staffMember = $params['staffmember'];
        $mailData = $params['mailData'];
        $fileData = $params['fileData'];

        Mail::queue($emailCode, $mailData, function ($message) use ($staffMember, $fileData, $fileName) {
            $message->to($staffMember->email, $staffMember->full_name);
            if (!empty($fileData)) {
                $message->attachData($fileData, $fileName);
            }
        });
    }
}

SendEmail.php fields.yaml:

fields:
    mail_template:
        label: Mail template
        type: dropdown
        placeholder: Select template
    file_name:
        label: File Name
        type: text
        placeholder: The attached file name

StaffRemoved.php Event:

<?php namespace Captive\XM\Classes\Notify\Events;

class StaffRemoved extends \RainLab\Notify\Classes\EventBase
{
    /**
     * Returns information about this event, including name and description.
     */
    public function eventDetails()
    {
        return [
            'name'        => 'Staff member removed',
            'description' => 'Fired when a staff member has been removed from a shift',
            'group'       => 'shifts'
        ];
    }

    /**
     * Defines the usable parameters provided by this class.
     */
    public function defineParams()
    {
        return [
            'name' => [
                'title' => 'Name',
                'label' => 'Name of the staff member',
            ],
            'shiftName' => [
                'title' => 'Shift',
                'label' => 'Name of the shift',
            ],
            'eventName' => [
                'title' => 'Event',
                'label' => 'Name of the event',
            ],
            'role' => [
                'title' => 'Role',
                'label' => 'Name of the role',
            ],
            'date' => [
                'title' => 'Start time',
                'label' => "The shift's start time",
            ],
            'url' => [
                'title' => 'URL',
                'label' => 'The URL for the shift',
            ],
        ];
    }

    public static function makeParamsFromEvent(array $args, $eventName = null)
    {
        return [
            'shiftslot'     => array_get($args, 0),
            'staffmember'   => array_get($args, 1),
            'mailData'      => array_get($args, 2),
            'fileData'      => null,
        ];
    }
}

captive.xm.shifts.staffmember.remove event fired in the ShiftSlot model:

Event::fire('captive.xm.shifts.staffmember.remove', [$this, $member, $data]);

from notify-plugin.

XIOLog avatar XIOLog commented on July 22, 2024

Thank you, Luke :-)

Almost got it.

Answer 2 more questions, please.

  1. How do I send mail to all employees of the company so that they do not see each other?
  2. How can I make a record in my own database table?

from notify-plugin.

LukeTowers avatar LukeTowers commented on July 22, 2024

If the content of all the emails is the same then add each address as a BCC recipient.

How can I make a record in my own database table?

No idea what you mean by that and it seems like such a basic question that this isn't the right place to ask it.

from notify-plugin.

XIOLog avatar XIOLog commented on July 22, 2024

I will outline the situation.

I have a job site. The vacancy has a button "Respond to the vacancy".

I need to save vacancy ID and CV ID to a database. Then, send letters to the job seeker (URL of the vacancy) and to all employees of the company (URL of the resume of the job seeker).

How can I do this? Maybe you can give me a hint?

from notify-plugin.

LukeTowers avatar LukeTowers commented on July 22, 2024

Take a look at existing plugins for managing job listings: https://octobercms.com/plugins?search=job. It's also not that difficult to just create your own using the Builder plugin if you're comfortable doing some custom development as well.

from notify-plugin.

XIOLog avatar XIOLog commented on July 22, 2024

Thank you, Luke. You always help me out :-)

from notify-plugin.

LukeTowers avatar LukeTowers commented on July 22, 2024

This plugin is probably not a good fit for what you're trying to do, custom development or those other plugins would be a better fit. This is a plugin that other developers can integrate their plugins with to offload the responsibility of notification management onto, not really a plugin meant to handle the notification rules for one-off site-specific plugins, since that's usually much easier to handle just by writing a small amount of custom code to handle whatever notifications you need directly.

Thank you, Luke. You always help me out :-)

Consider supporting me by sponsoring me through GitHub 😉

from notify-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.