Skip to content

Instantly share code, notes, and snippets.

@cornernote
Last active October 18, 2023 19:31
Show Gist options
  • Select an option

  • Save cornernote/23f970210577d9c25dd906bcc5c53891 to your computer and use it in GitHub Desktop.

Select an option

Save cornernote/23f970210577d9c25dd906bcc5c53891 to your computer and use it in GitHub Desktop.

Revisions

  1. cornernote revised this gist Feb 16, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -32,9 +32,9 @@ Finally run `composer update` to generate the `autoload.php`.

    ## Usage

    1. Mark an entity (book, chapter, page) as a Favourite
    1. Favourite an entity (shelf, book, chapter, page)
    2. Edit the entity, then save
    3. An email should have been sent
    3. An email will be sent


    ## Reources
  2. cornernote revised this gist Feb 16, 2023. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -19,15 +19,11 @@ Add the namespace to your `composer.json` autoload section:

    ```json
    {
    ...
    "autoload": {
    "psr-4": {
    ....
    "Custom\\": "themes/custom/"
    },
    ...
    },
    ...
    }
    }
    }
    ```

  3. cornernote revised this gist Feb 16, 2023. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    This guide will show you how to setup BookStack to send an email notification, when an entity changes, to anyone who marked the entity as a favourite.


    ## Installation

    Create the files below in your `bookstack/themes/custom/` folder.
    @@ -14,11 +15,38 @@ Add the theme to your `.env` file:
    APP_THEME=custom
    ```

    Add the namespace to your `composer.json` autoload section:

    ```json
    {
    ...
    "autoload": {
    "psr-4": {
    ....
    "Custom\\": "themes/custom/"
    },
    ...
    },
    ...
    }
    ```

    Finally run `composer update` to generate the `autoload.php`.


    ## Usage

    1. Mark an entity (book, chapter, page) as a Favourite
    2. Edit the entity, then save
    3. An email should have been sent


    ## Reources

    - [Hacking BookStack](https://www.bookstackapp.com/docs/admin/hacking-bookstack/)
    - [BookStack theme documentation](https://github.com/BookStackApp/BookStack/blob/development/dev/docs/logical-theme-system.md)


    ## Thanks To

    This code was sponsored by [Aussie Broadband: Australia's Leading nbn™ Internet Provider](https://www.aussiebroadband.com.au/)
  4. cornernote revised this gist Jan 20, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -18,3 +18,7 @@ APP_THEME=custom

    - [Hacking BookStack](https://www.bookstackapp.com/docs/admin/hacking-bookstack/)
    - [BookStack theme documentation](https://github.com/BookStackApp/BookStack/blob/development/dev/docs/logical-theme-system.md)

    ## Thanks To

    This code was sponsored by [Aussie Broadband: Australia's Leading nbn™ Internet Provider](https://www.aussiebroadband.com.au/)
  5. cornernote created this gist Jan 20, 2023.
    41 changes: 41 additions & 0 deletions FavouriteNotification.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <?php

    namespace Custom;

    use BookStack\Auth\User;
    use BookStack\Entities\Models\Entity;
    use BookStack\Notifications\MailNotification;
    use Illuminate\Notifications\Messages\MailMessage;

    class FavouriteNotification extends MailNotification
    {
    private string $type;
    private Entity $entity;

    public function __construct(string $type, Entity $entity)
    {
    $this->type = $type;
    $this->entity = $entity;
    }

    /**
    * Get the mail representation of the notification.
    */
    public function toMail(User $notifiable): MailMessage
    {
    $language = setting()->getUser($notifiable, 'language');

    $replace = [
    'appName' => setting('app-name'),
    'entityName' => $this->entity->name,
    'entityType' => class_basename($this->entity),
    'changeType' => $this->type,
    ];

    return $this->newMailMessage()
    ->subject(trans('custom::favourite_notification.email_subject', $replace, $language))
    ->greeting(trans('custom::favourite_notification.email_greeting', $replace, $language))
    ->line(trans('custom::favourite_notification.email_text', $replace, $language))
    ->action(trans('custom::favourite_notification.email_action', $replace, $language), $this->entity->getUrl());
    }
    }
    20 changes: 20 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # BookStack Favourite Change Notification

    This guide will show you how to setup BookStack to send an email notification, when an entity changes, to anyone who marked the entity as a favourite.

    ## Installation

    Create the files below in your `bookstack/themes/custom/` folder.

    The `favourite_notification.php` must be in the folder `themes/custom/resources/lang/en/'. Add other languages as needed.

    Add the theme to your `.env` file:

    ```shell
    APP_THEME=custom
    ```

    ## Reources

    - [Hacking BookStack](https://www.bookstackapp.com/docs/admin/hacking-bookstack/)
    - [BookStack theme documentation](https://github.com/BookStackApp/BookStack/blob/development/dev/docs/logical-theme-system.md)
    36 changes: 36 additions & 0 deletions SendFavouriteNotification.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?php

    namespace Custom;

    use Custom\FavouriteNotification;
    use BookStack\Actions\Favourite;
    use BookStack\Auth\User;
    use BookStack\Entities\Models\Entity;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Support\Facades\Notification;

    class SendFavouriteNotification
    {
    use Dispatchable;

    private string $type;
    private mixed $detail;

    public function __construct($type, $detail)
    {
    $this->type = $type;
    $this->detail = $detail;
    }

    public function handle(): void
    {
    if ($this->detail instanceof Entity) {
    $users = $this->detail->favourites->map(function (Favourite $favourite) {
    return User::find($favourite->user_id);
    });
    if ($users->count()) {
    Notification::send($users, new FavouriteNotification($this->type, $this->detail));
    }
    }
    }
    }
    12 changes: 12 additions & 0 deletions favourite_notification.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <?php
    /**
    * Favourite Alert text strings.
    * Is used for all the text within favourite alerts.
    */
    return [
    // Emails
    'email_subject' => 'Favourite Notification for :changeType to :entityName',
    'email_greeting' => ':entityName had :changeType on :appName.',
    'email_text' => 'Click the button below to view the :entityType.',
    'email_action' => 'View :entityType',
    ];
    14 changes: 14 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php

    use BookStack\Facades\Theme;
    use BookStack\Theming\ThemeEvents;
    use Custom\SendFavouriteNotification;
    use Illuminate\Support\Facades\Lang;

    Theme::listen(ThemeEvents::APP_BOOT, function ($app) {
    Lang::addNamespace('custom', __DIR__ . '/resources/lang');
    });

    Theme::listen(ThemeEvents::ACTIVITY_LOGGED, function ($type, $detail) {
    SendFavouriteNotification::dispatch($type, $detail);
    });