# ivyforms/0.4/backend/src/Repository/Entry/EntryQueryHelper.php

The Innovative Form Builder – IvyForms, version 0.4. 110 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.4/code/backend/src/Repository/Entry/EntryQueryHelper.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.4/raw/backend/src/Repository/Entry/EntryQueryHelper.php
- Modified: 2025-09-25T14:53:02+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.4/code/backend/src/Repository/Entry/EntryQueryHelper.php#L10-L20`.

```php
<?php

namespace IvyForms\Repository\Entry;

use IvyForms\Common\Exceptions\QueryExecutionException;
use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Services\Translations\BackendStrings;
use IvyForms\Services\InstallActions\DB\EntryField\EntryFieldsTable;
use IvyForms\Services\InstallActions\DB\Entry\EntriesTable;
use IvyForms\Services\InstallActions\DB\Form\FormsTable;
use wpdb;

class EntryQueryHelper
{
    /**
     * Helper to return search conditions for entries (for complexity reduction)
     *
     * @param array<string, mixed>|null $params
     * @return array<string>
     */
    public static function getEntrySearchConditions(?array $params = null): array
    {
        $conditions = [
            'e.id LIKE %s',
            'e.formId LIKE %s',
            'f.name LIKE %s',
        ];
        // Only include fieldValue if searchFieldValue is true (default true for BC)
        if (!isset($params['searchFieldValue']) || $params['searchFieldValue']) {
            $conditions[] = 'eft.fieldValue LIKE %s';
        }
        return $conditions;
    }

    /**
     * Get entry fields SQL and params for the given entries.
     *
     * @param array<int, array<string, mixed>> $entries
     * @return array{sql: string, params: array<int, int>}
     * @throws InvalidArgumentException
     */
    public static function getEntryFieldsQuery(array $entries): array
    {
        if (empty($entries)) {
            return ['sql' => '', 'params' => []];
        }
        $entryIds = array_column($entries, 'id');
        $placeholders = implode(',', array_fill(0, count($entryIds), '%d'));
        $entryFieldsTable = EntryFieldsTable::getTableName();
        $sql = "SELECT * FROM {$entryFieldsTable} WHERE entryId IN ($placeholders)";
        return ['sql' => $sql, 'params' => $entryIds];
    }

    /**
     * Get the count of entries for a specific form (SQL and params).
     *
     * @param string $table
     * @param int $formId
     * @return array{sql: string, params: array<int, int>}
     */
    public static function getCountByFormIdQuery(string $table, int $formId): array
    {
        $sql = "SELECT COUNT(*) FROM {$table} WHERE formId = %d";
        return ['sql' => $sql, 'params' => [$formId]];
    }

    /**
     * Get the count of entries for the filter dropdown (SQL and params).
     *
     * @param string $table
     * @param array<string, mixed>|null $params
     * @return array{sql: string, params: array<int, int>}
     */
    public static function getFilterCountQuery(string $table, ?array $params = null): array
    {
        $where = '';
        $queryParams = [];
        if (!empty($params['filters']['formId'])) {
            $where = 'WHERE formId = %d';
            $queryParams[] = (int)$params['filters']['formId'];
        }
        $sql = "SELECT 
            SUM(status = 'read') as readTrueCount,
            SUM(status = 'unread') as readFalseCount,
            SUM(starred = 1) as starredTrueCount,
            SUM(starred = 0) as starredFalseCount
        FROM {$table} $where";
        return ['sql' => $sql, 'params' => $queryParams];
    }

    /**
     * Get the count of entries for multiple form IDs (SQL and params).
     *
     * @param int[] $formIds
     * @return array{sql: string, params: array<int, int>, formIds: array<int, int>}
     * @throws InvalidArgumentException
     */
    public static function getEntryCountByFormIdsQuery(array $formIds): array
    {
        if (empty($formIds)) {
            return ['sql' => '', 'params' => [], 'formIds' => []];
        }
        $formIds = array_map('intval', $formIds);
        $placeholders = implode(',', array_fill(0, count($formIds), '%d'));
        $tableName = EntriesTable::getTableName();
        $sql = "SELECT formId, COUNT(*) as count FROM {$tableName} WHERE formId IN ($placeholders) GROUP BY formId";
        return ['sql' => $sql, 'params' => $formIds, 'formIds' => $formIds];
    }
}

```
