XF 2.2 Issue with customized alert, I'd appreciate any assistance.

bytrislasis

Member
Hello everyone,

I want to send an alert to the user with the ID 1 when our members initiate a withdrawal request to their Ethereum addresses.

Here's what I've done so far:I defined a type named wallet_transfer in the content type section of the admin panel. You can see it in the image below.
1695056306767.png

Then, I defined an entity.
1695056336340.png

I created a directory named Alert in the root directory of my plugin and added the following codes inside.
PHP:
<?php

namespace SatoshiTURK\Wallet\Alert;

use XF\Alert\AbstractHandler;

class WalletTransfer extends AbstractHandler
{

    public function getEntityWith(): array
    {
        return ['transfer_id', 'user_id','User'];
    }

    public function getOptOutActions(): array
    {
     
  return ['withdraw'];
    }
 
}

I'm sending the alert notification as below:
PHP:
// ID'si 1 olan kullanıcıya bildirim gönder
        $receiver = $this->em()->find('XF:User', 1);

        if ($receiver) {
            $sender = \XF::finder('XF:User')->where('user_id', 10)->fetchOne();
            $alertRepo = $this->repository('XF:UserAlert');

            $alertRepo->alert(
                $receiver,
                $sender->user_id,
                $sender->username,
                'wallet_transfer',
                $newTransfer->transfer_id,
                'withdraw',
                ["amount" => $input['withdraw_amount'], "address" => $input['withdraw_address'], "status" => "pending"]
            );
        }

I also created a template with the name alert_wallet_transfer_withdraw.

I do receive the notification, but when I click on it, the content appears empty. I'd appreciate any assistance in identifying where I might be going wrong.

1695056470393.png



Empty alert loading...

1695056502750.png



Entity File

PHP:
<?php
namespace SatoshiTURK\Wallet\Entity;

use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Structure;

class WalletTransfer extends Entity
{
    public static function getStructure(Structure $structure)
    {
        $structure->table = 'xf_satoshiturk_wallet_transfers';
        $structure->shortName = 'SatoshiTURK\Wallet:WalletTransfer';
        $structure->contentType = 'wallet_transfer';
        $structure->primaryKey = 'transfer_id';
        $structure->columns = [
            'transfer_id' => ['type' => self::UINT, 'autoIncrement' => true],
            'user_id' => ['type' => self::UINT, 'required' => true],
            'amount' => ['type' => self::FLOAT, 'required' => true],
            'ethereum_address' => ['type' => self::STR, 'maxLength' => 255, 'required' => true],
            'status' => ['type' => self::STR, 'default' => 'pending'],
            'created_at' => ['type' => self::UINT, 'default' => \XF::$time],
            'updated_at' => ['type' => self::UINT, 'default' => \XF::$time]
        ];


        $structure->relations = [
            'User' => [
                'entity' => 'XF:User',
                'type' => self::TO_ONE,
                'conditions' => 'user_id',
                'primary' => true
            ],
        ];
        return $structure;
    }
 
    public function whereIds(array $ids)
    {
        return $this->finder('SatoshiTURK\Wallet:WalletTransfer')->where('transfer_id', $ids);
    }

    public function with()
    {
        return $this->with('User');
    }

}


Database xf_alert
1695056960352.png
 
Last edited:
Try this:

PHP:
<?php

namespace SatoshiTURK\Wallet\Alert;

use XF\Alert\AbstractHandler;
use XF\Mvc\Entity\Entity;

class WalletTransfer extends AbstractHandler
{
     public function getEntityWith(): array
     {
        return ['transfer_id', 'user_id','User'];
     }
     public function getOptOutActions(): array
     {
          return ['withdraw'];
     }
     public function canViewContent(Entity $entity, &$error = null): bool
     {
         return true;
     }
}
 
Try this:

PHP:
<?php

namespace SatoshiTURK\Wallet\Alert;

use XF\Alert\AbstractHandler;
use XF\Mvc\Entity\Entity;

class WalletTransfer extends AbstractHandler
{
     public function getEntityWith(): array
     {
        return ['transfer_id', 'user_id','User'];
     }
     public function getOptOutActions(): array
     {
          return ['withdraw'];
     }
     public function canViewContent(Entity $entity, &$error = null): bool
     {
         return true;
     }
}


tried this before, and the result was the same. I've been racking my brain over this problem for 2 days, and I'm about to give up. Everything is fine, but the content appears empty. :cry:
 
For the entity content type try using the short name instead of the path: SatoshiTURK\Wallet:WalletTransfer
 
When I change the content type to 'post,' it works.It also uses the template as 'post.'I think I'm experiencing an issue related to the content type.
 
PHP:
public function canViewAlert(UserAlert $alert, &$error = null): bool
    {
       return true;
    }

When I added this method it worked, thank you very much.
 
I think I explained it wrong. When I added the following code to the entity file, it worked.

PHP:
public function canView(): bool
    {
        return true;
    }


The final versions of the files might be needed by someone in the future.

PHP:
<?php
namespace SatoshiTURK\Wallet\Entity;

use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Structure;

class Transfer extends Entity
{
    public static function getStructure(Structure $structure)
    {
        $structure->table = 'xf_satoshiturk_wallet_transfers';
        $structure->shortName = 'SatoshiTURK\Wallet:Transfer';
        $structure->contentType = 'transaction';
        $structure->primaryKey = 'transfer_id';
        $structure->columns = [
            'transfer_id' => ['type' => self::UINT, 'autoIncrement' => true],
            'user_id' => ['type' => self::UINT, 'required' => true],
            'amount' => ['type' => self::FLOAT, 'required' => true],
            'ethereum_address' => ['type' => self::STR, 'maxLength' => 255, 'required' => true],
            'status' => ['type' => self::STR, 'default' => 'pending'],
            'created_at' => ['type' => self::UINT, 'default' => \XF::$time],
            'updated_at' => ['type' => self::UINT, 'default' => \XF::$time]
        ];

        $structure->relations = [
            'User' => [
                'entity' => 'XF:User',
                'type' => self::TO_ONE,
                'conditions' => 'user_id',
                'primary' => true
            ]
        ];

        return $structure;
    }

    public function canView(): bool
    {
        return true;
    }

}



PHP:
<?php

namespace  SatoshiTURK\Wallet\Alert;
use XF\Alert\AbstractHandler;
use XF\Entity\UserAlert;


class Transfer extends AbstractHandler
{
    public function getEntityWith(): array
    {
        return ['User'];
    }

    public function getOptOutActions(): array
    {
        return [
            'withdraw',
            'cancelled',
            'success'
        ];
    }

    public function canViewAlert(UserAlert $alert, &$error = null): bool
    {
        return true;
    }


    public function getOptOutDisplayOrder(): int
    {
        return 89999;
    }
}
 
Back
Top Bottom