$data Columns without created_at. * @return int New id. */ public function record( array $data ): int { $data['created_at'] = gmdate( 'Y-m-d H:i:s' ); return $this->insert_row( Tables::name( 'emails' ), $data ); } /** * A page of log records without their HTML bodies, newest first. * * @since 0.1.0 * * @param int $session_id Session id (0 for all). * @param array $args page, per_page. * @return array{items: object[], total: int} */ public function query( int $session_id, array $args ): array { list( , $per_page, $offset ) = $this->paging( $args ); $table = Tables::name( 'emails' ); $clause = $session_id ? 'session_id = %d' : '1=1'; $params = $session_id ? array( $session_id ) : array(); $total = (int) $this->value( "SELECT COUNT(*) FROM {$table} WHERE {$clause}", $params ); $items = $this->rows( "SELECT id, session_id, candidate_id, recipient, subject, mail_type, delivery_status, detail, created_at FROM {$table} WHERE {$clause} ORDER BY id DESC LIMIT %d OFFSET %d", array_merge( $params, array( $per_page, $offset ) ) ); return array( 'items' => $items, 'total' => $total, ); } /** * One record including its HTML. * * @since 0.1.0 * * @param int $id Log id. * @return object|null */ public function find( int $id ): ?object { $table = Tables::name( 'emails' ); return $this->row( "SELECT * FROM {$table} WHERE id = %d", array( $id ) ); } /** * Deletes records older than a number of days. * * @since 0.1.0 * * @param int $days Days to keep. * @return int Rows deleted. */ public function purge_older_than( int $days ): int { $table = Tables::name( 'emails' ); return (int) $this->execute( "DELETE FROM {$table} WHERE created_at < %s", array( gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) ) ); } /** * Records that name one candidate (privacy export). * * @since 0.1.0 * * @param int $candidate_id Candidate id. * @return object[] */ public function for_candidate( int $candidate_id ): array { $table = Tables::name( 'emails' ); return $this->rows( "SELECT id, session_id, candidate_id, recipient, subject, mail_type, delivery_status, created_at FROM {$table} WHERE candidate_id = %d ORDER BY id ASC LIMIT 200", array( $candidate_id ) ); } }