| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Repository; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 16 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 17 |
use IvyForms\Services\Translations\BackendStrings; |
| 18 |
|
| 19 |
/** |
| 20 |
* Trait ColumnRepositoryTrait |
| 21 |
* |
| 22 |
* Handles all column-related operations and bulk column actions for repositories. |
| 23 |
* Use this trait in repositories that extend AbstractRepository. |
| 24 |
* |
| 25 |
* Column configuration methods (getSearchableColumns, getFilterableColumns, etc.) |
| 26 |
* should be overridden in child classes. |
| 27 |
* |
| 28 |
* @package IvyForms\Repository |
| 29 |
*/ |
| 30 |
trait ColumnRepositoryTrait |
| 31 |
{ |
| 32 |
/** |
| 33 |
* Bulk update a column for multiple entities by their IDs. |
| 34 |
* |
| 35 |
* @param string $column Column name to update |
| 36 |
* @param mixed $value Value to set |
| 37 |
* @param array<int> $ids Array of entity IDs |
| 38 |
* @return int Number of updated entities |
| 39 |
* @throws InvalidArgumentException If column is not allowed or IDs are empty |
| 40 |
* @throws QueryExecutionException |
| 41 |
*/ |
| 42 |
public function bulkUpdateColumn(string $column, $value, array $ids): int |
| 43 |
{ |
| 44 |
$ids = array_map('intval', $ids); |
| 45 |
$placeholders = implode(',', array_fill(0, count($ids), '%d')); |
| 46 |
$query = "UPDATE {$this->table} SET {$column} = %s, dateEdited = %s WHERE id IN ($placeholders)"; |
| 47 |
$params = array_merge([$value, current_time('mysql')], $ids); |
| 48 |
$result = $this->wpdb->query($this->wpdb->prepare($query, ...$params)); |
| 49 |
|
| 50 |
return $result; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Get allowed columns for bulk update operations. |
| 56 |
* Override this method in child repositories to specify allowed columns. |
| 57 |
* |
| 58 |
* @return string[] |
| 59 |
*/ |
| 60 |
protected function getAllowedBulkUpdateColumns(): array |
| 61 |
{ |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get searchable columns for the repository. |
| 67 |
* Override this method in child repositories to specify which columns are searchable. |
| 68 |
* |
| 69 |
* Default: only 'id' column |
| 70 |
* |
| 71 |
* @return string[] |
| 72 |
*/ |
| 73 |
protected function getSearchableColumns(): array |
| 74 |
{ |
| 75 |
return ['id']; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get filterable columns for the repository. |
| 80 |
* Override this method in child repositories to specify which columns are filterable. |
| 81 |
* |
| 82 |
* Default: no columns |
| 83 |
* |
| 84 |
* @return array<string, string> |
| 85 |
*/ |
| 86 |
protected function getFilterableColumns(): array |
| 87 |
{ |
| 88 |
return []; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get sortable columns for the repository. |
| 93 |
* Override this method in child repositories to specify which columns are sortable. |
| 94 |
* |
| 95 |
* Default: only 'id' column |
| 96 |
* |
| 97 |
* @return string[] |
| 98 |
*/ |
| 99 |
protected function getSortableColumns(): array |
| 100 |
{ |
| 101 |
return ['id']; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get the date column name for the repository. |
| 106 |
* Override this method in child repositories to specify the date column. |
| 107 |
* |
| 108 |
* Default: 'dateCreated' |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
protected function getDateColumn(): string |
| 113 |
{ |
| 114 |
return 'dateCreated'; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get allowed foreign key columns for deletion operations. |
| 119 |
* Override this method in child repositories to specify allowed columns. |
| 120 |
* |
| 121 |
* Default: formId, entryId, fieldId |
| 122 |
* |
| 123 |
* @return string[] |
| 124 |
*/ |
| 125 |
protected function getAllowedForeignColumns(): array |
| 126 |
{ |
| 127 |
return ['formId', 'entryId', 'fieldId']; |
| 128 |
} |
| 129 |
} |
| 130 |
|