toArray(); $result = $this->wpdb->insert( $this->table, [ 'name' => $data['name'], 'author' => wp_get_current_user()->display_name, 'starred' => (int) $data['starred'], 'published' => (int) $data['published'], 'dateCreated' => current_time('mysql'), 'dateEdited' => current_time('mysql'), 'description' => $data['description'], 'settings' => json_encode([ 'showTitle' => (int) $data['showTitle'], 'showDescription' => (int) $data['showDescription'], ]) ], ['%s', '%s', '%d', '%d', '%s', '%s', '%s'] ); 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 Form $entity * * @return bool * * @throws QueryExecutionException */ public function update(int $id, $entity): bool { $data = $entity->toArray(); $result = $this->wpdb->update( $this->table, [ 'name' => $data['name'], 'starred' => (int) $data['starred'], 'published' => (int) $data['published'], 'description' => $data['description'], 'dateEdited' => current_time('mysql'), 'settings' => json_encode([ 'showTitle' => (int)$data['showTitle'], 'showDescription' => (int)$data['showDescription'], ]) ], ['id' => $id], ['%s', '%d', '%d', '%s', '%s'], ['%d'] ); if ($result === false) { throw new QueryExecutionException('Unable to save data in ' . __CLASS__); } return $result; } /** * Update the form starred value by ID. * * @param int $id * @param string $value * * @return bool * @throws QueryExecutionException */ public function updateFormStarred(int $id, string $value): bool { // Update the starred column in the database $result = $this->wpdb->update( $this->table, ['starred' => $value], ['id' => $id], ['%d'], ['%d'] ); if ($result === false) { throw new QueryExecutionException('Unable to save data in ' . __CLASS__); } return $result; } /** * Update the published status of a form by ID. * * @param int $formId * @param string $value * * @return bool * @throws QueryExecutionException */ public function updateFormStatus(int $formId, string $value): bool { // Update the published column in the database $result = $this->wpdb->update( $this->table, ['published' => $value], ['id' => $formId], ['%d'], ['%d'] ); if ($result === false) { throw new QueryExecutionException('Unable to save data in ' . __CLASS__); } return $result; } /** * Check if a form exists by its ID. * * @param int $formId * * @return bool */ public function exists(int $formId): bool { $query = $this->wpdb->prepare( "SELECT COUNT(*) FROM " . $this->table . " WHERE id = %d", $formId ); $count = $this->wpdb->get_var($query); return (bool) $count; } /** * Get searchable columns * * @return array */ protected function getSearchableColumns(): array { return ['name', 'author', 'description']; } /** * Get filterable columns * * @return array */ protected function getFilterableColumns(): array { return ['starred', 'published']; } /** * Get sortable columns * * @return array */ protected function getSortableColumns(): array { return ['id', 'name', 'dateCreated']; } /** * Get the date column * * @return string */ protected function getDateColumn(): string { return 'dateCreated'; } }