PHP code example of tastyigniter / ti-ext-automation

1. Go to this page and download the library: Download tastyigniter/ti-ext-automation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tastyigniter / ti-ext-automation example snippets


namespace Author\Extension\AutomationRules\Events;

class CustomerRegisteredEvent extends \Igniter\Automation\Classes\BaseEvent
{
    public function eventDetails(): array
    {
        return [
            'name' => 'Registered',
            'description' => 'When a customer registers',
            'group' => 'customer'
        ];
    }

    public static function makeParamsFromEvent(array $args, $eventName = null): array
    {
        return [
            'user' => array_get($args, 0)
        ];
    }
}

namespace Author\Extension\AutomationRules\Actions;

class SendMailTemplate extends \Igniter\Automation\Classes\BaseAction
{
    public function actionDetails(): array
    {
        return [
            'name' => 'Compose a mail message',
            'description' => 'Send a message to a recipient',
        ];
    }

    public function defineFormFields(): array
    {
        return [
            'fields' => [
                'template' => [
                    'label' => 'lang:igniter.user::default.label_template',
                    'type' => 'select',
                ],
                'send_to' => [
                    'label' => 'lang:igniter.user::default.label_send_to',
                    'type' => 'select',
                ],
            ],
        ];
    }

    public function triggerAction($params)
    {
        $email = $this->model->send_to;
        $template = $this->model->template;

        // Send mail
    }
}

namespace Author\Extension\AutomationRules\Conditions;

class MyCondition extends \Igniter\Automation\Classes\BaseCondition
{
    public function conditionDetails(): array
    {
        return [
            'name' => 'Condition',
            'description' => 'My Condition is checked',
        ];
    }

    public function isTrue(&$params): bool
    {
        return true;
    }
}

namespace Author\Extension\AutomationRules\Conditions;

class CustomerAttribute extends \Igniter\Automation\Classes\BaseCondition
{
    public function conditionDetails(): array
    {
        return [
            'name' => 'Customer attribute',
        ];
    }
    
    public function defineModelAttributes(): array
    {
        return [
            'first_name' => [
                'label' => 'First Name',
            ],
            'last_name' => [
                'label' => 'Last Name',
            ],
        ];
    }

    public function isTrue(&$params): bool
    {
        return true;
    }
}

public function registerAutomationRules(): array
{
    return [
        'events' => [
            \Igniter\User\AutomationRules\Events\CustomerRegistered::class,
        ],
        'actions' => [
            \Igniter\User\AutomationRules\Actions\SendMailTemplate::class,
        ],
        'conditions' => [
            \Igniter\User\AutomationRules\Conditions\CustomerAttribute::class
        ],
        'presets' => [
            'registration_email' => [
                'name' => 'Send customer registration email',
                'event' => \Igniter\User\AutomationRules\Events\CustomerRegistered::class,
                'actions' => [
                    \Igniter\User\AutomationRules\Actions\SendMailTemplate::class => [
                        'template' => 'igniter.user::mail.registration_email'
                    ],
                ]
            ]
        ],
    ];
}

use Igniter\User\Facades\Auth;
use Igniter\Automation\Classes\EventManager;

public function boot()
{
    resolve(EventManager::class)->registerGlobalParams([
        'customer' => Auth::customer()
    ]);
}
bash
php artisan igniter:up