Elgg  Version master
DelayedEmailQueueTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 use Elgg\Traits\TimeUsing;
8 
16 
17  use TimeUsing;
18 
22  public const TABLE_NAME = 'delayed_email_queue';
23 
29  public function __construct(protected Database $db) {
30  }
31 
41  public function queueEmail(int $recipient_guid, string $delivery_interval, $item): bool {
42  $insert = Insert::intoTable(self::TABLE_NAME);
43  $insert->values([
44  'recipient_guid' => $insert->param($recipient_guid, ELGG_VALUE_GUID),
45  'delivery_interval' => $insert->param($delivery_interval, ELGG_VALUE_STRING),
46  'data' => $insert->param(serialize($item), ELGG_VALUE_STRING),
47  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
48  ]);
49 
50  return $this->db->insertData($insert) !== 0;
51  }
52 
60  public function getRow(int $id): ?DatabaseRecord {
61  $select = Select::fromTable(self::TABLE_NAME);
62  $select->select('*')
63  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
64 
65  return $this->db->getDataRow($select, [$this, 'rowToRecord']);
66  }
67 
78  public function getRecipientRows(int $recipient_guid, string $delivery_interval, ?int $timestamp = null, int $max_results = 0): array {
79  $select = Select::fromTable(self::TABLE_NAME);
80  $select->select('*')
81  ->where($select->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
82  ->andWhere($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
83  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
84  ->orderBy('timestamp', 'ASC')
85  ->addOrderBy('id', 'ASC');
86 
87  if ($max_results > 0) {
88  $select->setMaxResults($max_results);
89  }
90 
91  return $this->db->getData($select, [$this, 'rowToRecord']);
92  }
93 
102  public function getNextRecipientGUID(string $delivery_interval, ?int $timestamp = null): ?int {
103  $select = Select::fromTable(self::TABLE_NAME);
104  $select->select('recipient_guid')
105  ->where($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
106  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp()))
107  ->orderBy('timestamp', 'ASC')
108  ->addOrderBy('id', 'ASC')
109  ->setMaxResults(1);
110 
111  $row = $this->db->getDataRow($select);
112  if (empty($row)) {
113  return null;
114  }
115 
116  return (int) $row->recipient_guid;
117  }
118 
126  public function deleteRow(int $id): int {
127  $delete = Delete::fromTable(self::TABLE_NAME);
128  $delete->where($delete->compare('id', '=', $id, ELGG_VALUE_ID));
129 
130  return $this->db->deleteData($delete);
131  }
132 
143  public function deleteRecipientRows(int $recipient_guid, string $delivery_interval, ?int $timestamp = null, int $max_id = 0): int {
144  $delete = Delete::fromTable(self::TABLE_NAME);
145  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
146  ->andWhere($delete->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
147  ->andWhere($delete->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_INTEGER))
148  ->orderBy('timestamp', 'ASC')
149  ->addOrderBy('id', 'ASC');
150 
151  if ($max_id > 0) {
152  $delete->andWhere($delete->compare('id', '<=', $max_id, ELGG_VALUE_ID));
153  }
154 
155  return $this->db->deleteData($delete);
156  }
157 
165  public function deleteAllRecipientRows(int $recipient_guid): int {
166  $delete = Delete::fromTable(self::TABLE_NAME);
167  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
168 
169  return $this->db->deleteData($delete);
170  }
171 
180  public function updateRecipientInterval(int $recipient_guid, string $delivery_interval): bool {
181  $update = Update::table(self::TABLE_NAME);
182  $update->set('delivery_interval', $update->param($delivery_interval, ELGG_VALUE_STRING))
183  ->where($update->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
184 
185  return $this->db->updateData($update);
186  }
187 
195  public function rowToRecord(\stdClass $row): DatabaseRecord {
196  return new DatabaseRecord($row);
197  }
198 }
$id
Generic annotation delete action.
Definition: delete.php:6
$recipient_guid
Definition: mute.php:7
if(! $items) $item
Definition: delete.php:13
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
$delete
Interfaces with the database to perform operations on the delayed_email_queue table.
deleteRow(int $id)
Remove a queue items from the database.
getRow(int $id)
Get a row from the queue.
queueEmail(int $recipient_guid, string $delivery_interval, $item)
Insert a delayed email into the queue.
rowToRecord(\stdClass $row)
Convert a database row to a manageable object.
getRecipientRows(int $recipient_guid, string $delivery_interval, ?int $timestamp=null, int $max_results=0)
Get all the rows in the queue for a given recipient.
__construct(protected Database $db)
Create new service.
updateRecipientInterval(int $recipient_guid, string $delivery_interval)
Update the queued notifications for the recipient to a new delivery interval.
deleteRecipientRows(int $recipient_guid, string $delivery_interval, ?int $timestamp=null, int $max_id=0)
Delete all the queue items from the database for the given recipient and interval.
getNextRecipientGUID(string $delivery_interval, ?int $timestamp=null)
Fetch the GUID of the next recipient to process.
deleteAllRecipientRows(int $recipient_guid)
Deletes all the queue items from the database for the given recipient.
Query builder for updating data in the database.
Definition: Delete.php:8
Query builder for inserting data into the database.
Definition: Insert.php:8
orderBy(string $sort, ?string $order=null)
{}
Query builder for fetching data from the database.
Definition: Select.php:8
Query builder for updating data in the database.
Definition: Update.php:8
The Elgg database.
Definition: Database.php:26
const ELGG_VALUE_STRING
Definition: constants.php:112
const ELGG_VALUE_ID
Definition: constants.php:114
const ELGG_VALUE_GUID
Definition: constants.php:113
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
$timestamp
Definition: date.php:34