toArray(); $result = $this->wpdb->insert( $this->table, [ 'formId' => (int) $data['formId'], 'name' => $data['name'], 'sender' => $data['sender'], 'replyTo' => $data['replyTo'], 'receiver' => $data['receiver'], 'enabled' => (int) $data['enabled'], 'subject' => $data['subject'], 'message' => $data['message'], 'smartLogic' => (int) $data['smartLogic'], ], ['%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d'] ); if ($result === false) { throw new QueryExecutionException( BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ ); } return $this->wpdb->insert_id; } /** * Update form in the database * * @param int $id * @param Notification $entity * * @return bool * * @throws QueryExecutionException */ public function update(int $id, $entity): bool { $data = $entity->toArray(); $result = $this->wpdb->update( $this->table, [ 'formId' => (int) $data['formId'], 'name' => $data['name'], 'sender' => $data['sender'], 'replyTo' => $data['replyTo'], 'receiver' => $data['receiver'], 'enabled' => (int) $data['enabled'], 'subject' => $data['subject'], 'message' => $data['message'], 'smartLogic' => (int) $data['smartLogic'], ], ['id' => $id], ['%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d'], ['%d'] ); if ($result === false) { throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); } return $result; } /** * Get all notifications by form ID. * * @param int $id * * @return array * @throws QueryExecutionException */ public function getAllById(int $id): array { $rows = $this->wpdb->get_results( $this->wpdb->prepare( $this->selectQuery() . " WHERE $this->table.formId = %d", [$id] ), ARRAY_A ); if ($rows === false) { throw new QueryExecutionException( BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ ); } if (empty($rows)) { return []; } return array_map(function ($row) { return call_user_func([static::FACTORY, 'create'], $row); }, $rows); } /** * Get all active notifications by form ID. * * @param int $id * * @return array * @throws QueryExecutionException */ public function getAllActiveById(int $id): array { $rows = $this->wpdb->get_results( $this->wpdb->prepare( $this->selectQuery() . " WHERE $this->table.formId = %d AND $this->table.enabled = 1", $id ), ARRAY_A ); if ($rows === false) { throw new QueryExecutionException( BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ ); } if (empty($rows)) { return []; } return array_map(function ($row) { return call_user_func([static::FACTORY, 'create'], $row); }, $rows); } /** * Define searchable columns for notifications * * @return string[] */ protected function getSearchableColumns(): array { return ['name', 'receiver', 'subject']; } /** * Define filterable columns for notifications * * @return string[] */ protected function getFilterableColumns(): array { return ['formId']; } /** * Define sortable columns for notifications * * @return string[] */ protected function getSortableColumns(): array { return [ 'id', 'name', 'receiver', 'subject', 'enabled' ]; } }