# ivyforms/0.1.2/backend/src/Repository/Notification/NotificationRepository.php

The Innovative Form Builder – IvyForms, version 0.1.2. 148 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Repository/Notification/NotificationRepository.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.1.2/raw/backend/src/Repository/Notification/NotificationRepository.php
- Modified: 2025-06-12T10:29:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Repository/Notification/NotificationRepository.php#L10-L20`.

```php
<?php

/**
 * @copyright © TMS-Plugins. All rights reserved.
 * @licence   See LICENCE.md for license details.
 */

namespace IvyForms\Repository\Notification;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\QueryExecutionException;
use IvyForms\Entity\Notification\Notification;
use IvyForms\Factory\Notification\NotificationFactory;
use IvyForms\Repository\AbstractRepository;
use IvyForms\Repository\Notification\NotificationRepositoryInterface;

class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface
{
    public const FACTORY = NotificationFactory::class;

    /**
     * Add notification to the database
     *
     * @param Notification $entity
     *
     * @return int
     *
     * @throws QueryExecutionException
     */
    public function add($entity): int
    {
        $data = $entity->toArray();

        $result = $this->wpdb->insert(
            $this->table,
            [
                'formId'      => (int) $data['formId'],
                'name'        => $data['name'],
                'sender'      => $data['sender'],
                'receiver'    => $data['receiver'],
                'status'      => (int) $data['status'],
                'subject'     => $data['subject'],
                'message'     => $data['message'],
                'smartLogic' => (int) $data['smartLogic'],
            ],
            ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d']
        );

        if ($result === false) {
            throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
        }

        return $this->wpdb->insert_id;
    }

    /**
     * Update form in the database
     *
     * @param int  $id
     * @param Notification $entity
     *
     * @return bool
     *
     * @throws QueryExecutionException
     */
    public function update(int $id, $entity): bool
    {
        $data = $entity->toArray();

        $result = $this->wpdb->update(
            $this->table,
            [
                'formId'      => (int) $data['formId'],
                'name'        => $data['name'],
                'sender'      => $data['sender'],
                'receiver'    => $data['receiver'],
                'status'      => (int) $data['status'],
                'subject'     => $data['subject'],
                'message'     => $data['message'],
                'smartLogic' => (int) $data['smartLogic'],
            ],
            ['id' => $id],
            ['%d', '%s', '%s', '%s', '%d', '%s', '%s', '%d'],
            ['%d']
        );

        if ($result === false) {
            throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
        }

        return $result;
    }
    /**
     * Get all notifications by form ID.
     *
     * @param int $id
     *
     * @return array<Notification>
     */
    public function getAllById(int $id): array
    {
        $rows = $this->wpdb->get_results(
            $this->wpdb->prepare(
                $this->selectQuery() . " WHERE $this->table.formId = %d",
                [$id]
            ),
            ARRAY_A
        );

        if (empty($rows)) {
            return [];
        }

        return array_map(function ($row) {
            return call_user_func([static::FACTORY, 'create'], $row);
        }, $rows);
    }
    /**
     * Get all active notifications by form ID.
     *
     * @param int $id
     *
     * @return array<Notification>
     */
    public function getAllActiveById(int $id): array
    {
        $rows = $this->wpdb->get_results(
            $this->wpdb->prepare(
                $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.status = 1",
                $id
            ),
            ARRAY_A
        );

        if (empty($rows)) {
            return [];
        }

        return array_map(function ($row) {
            return call_user_func([static::FACTORY, 'create'], $row);
        }, $rows);
    }
}

```
