NotificationPreferences
TsgcHTMLComponent_NotificationPreferences: una tabella con una riga per categoria e un checkbox per canale (in app, push, email), in Delphi, C++ Builder e .NET. La tua applicazione carica e salva i valori.
TsgcHTMLComponent_NotificationPreferences: una tabella con una riga per categoria e un checkbox per canale (in app, push, email), in Delphi, C++ Builder e .NET. La tua applicazione carica e salva i valori.
La libreria non memorizza nulla. OnLoadPreferences chiede alla tua applicazione i valori di un utente, OnSavePreferences restituisce ciò che l’utente ha selezionato, e salvarlo spetta a te. I valori sono coppie name=value con 1 oppure 0, e ogni coppia offerta è sempre presente. Funziona in ogni edizione.
TsgcHTMLComponent_NotificationPreferences (unit sgcHTML_Component_NotificationPreferences)
Una tabella: una riga per categoria, un checkbox per canale e un pulsante di salvataggio
Delphi, C++ Builder, .NET
Aggiungi un elemento a Categories per ogni cosa di cui un utente può essere avvisato, rispondi a OnLoadPreferences dal tuo archivio, salva ciò che arriva in OnSavePreferences, e instrada il modulo inviato a ProcessAction con l’utente preso dalla sessione.
uses
sgcHTML_Session, sgcHTML_Component_NotificationPreferences;
var
oPrefs: TsgcHTMLComponent_NotificationPreferences;
begin
oPrefs := TsgcHTMLComponent_NotificationPreferences.Create(nil);
try
oPrefs.PreferencesID := 'prefs';
oPrefs.Title := 'How you want to hear about it';
oPrefs.SaveCaption := 'Save preferences';
with oPrefs.Categories.Add do
begin
Name := 'orders';
Caption := 'A new order arrives';
end;
with oPrefs.Categories.Add do
begin
Name := 'billing';
Caption := 'A payment fails';
end;
oPrefs.OnLoadPreferences := PrefsLoad;
oPrefs.OnSavePreferences := PrefsSave;
// the user comes from the session, never from the form
oPrefs.LoadPreferences(sgcHTMLRequestSession.UserID);
Response := oPrefs.HTML; // one row per category, one checkbox per channel
finally
oPrefs.Free;
end;
end;
// the application owns the values: one category.channel=1 or =0 per pair
procedure TMain.PrefsLoad(Sender: TObject; const aUserID: string;
aValues: TStrings);
begin
aValues.Add('orders.inapp=1');
aValues.Add('orders.push=0');
aValues.Add('orders.email=1');
end;
procedure TMain.PrefsSave(Sender: TObject; const aUserID: string;
aValues: TStrings);
var
i: Integer;
begin
// every offered pair is here, with 1 or 0
for i := 0 to aValues.Count - 1 do
MyStore.Save(aUserID, aValues.Names[i], aValues.Values[aValues.Names[i]] = '1');
end;
// in the message handler that receives the form the table posts
if oPrefs.ProcessAction(vUser, vAction, vPostedFields) then
oHTMX.PushFragment(vGuid, oPrefs.HTML);
// includes: sgcHTML_Session.hpp, sgcHTML_Component_NotificationPreferences.hpp
TsgcHTMLComponent_NotificationPreferences *oPrefs = new TsgcHTMLComponent_NotificationPreferences(NULL);
try
{
oPrefs->PreferencesID = "prefs";
oPrefs->Title = "How you want to hear about it";
oPrefs->SaveCaption = "Save preferences";
TsgcHTMLPreferenceCategory *orders = oPrefs->Categories->Add();
orders->Name = "orders";
orders->Caption = "A new order arrives";
TsgcHTMLPreferenceCategory *billing = oPrefs->Categories->Add();
billing->Name = "billing";
billing->Caption = "A payment fails";
oPrefs->OnLoadPreferences = PrefsLoad;
oPrefs->OnSavePreferences = PrefsSave;
// the user comes from the session, never from the form
oPrefs->LoadPreferences(sgcHTMLRequestSession()->UserID);
String html = oPrefs->HTML; // one row per category, one checkbox per channel
}
__finally
{
delete oPrefs;
}
// the application owns the values: one category.channel=1 or =0 per pair
void __fastcall TMain::PrefsLoad(TObject *Sender, const String aUserID,
TStrings *aValues)
{
aValues->Add("orders.inapp=1");
aValues->Add("orders.push=0");
aValues->Add("orders.email=1");
}
void __fastcall TMain::PrefsSave(TObject *Sender, const String aUserID,
TStrings *aValues)
{
// every offered pair is here, with 1 or 0
for (int i = 0; i < aValues->Count; i++)
MyStore->Save(aUserID, aValues->Names[i], aValues->Values[aValues->Names[i]] == "1");
}
// in the message handler that receives the form the table posts
if (oPrefs->ProcessAction(vUser, vAction, vPostedFields))
oHTMX->PushFragment(vGuid, oPrefs->HTML);
using esegece.sgcWebSockets;
var prefs = new TsgcHTMLComponent_NotificationPreferences();
prefs.PreferencesID = "prefs";
prefs.Title = "How you want to hear about it";
prefs.SaveCaption = "Save preferences";
var orders = prefs.Categories.Add();
orders.Name = "orders";
orders.Caption = "A new order arrives";
var billing = prefs.Categories.Add();
billing.Name = "billing";
billing.Caption = "A payment fails";
prefs.OnLoadPreferences += PrefsLoad;
prefs.OnSavePreferences += PrefsSave;
// the user comes from the session, never from the form
string user = sgcHTMLSessionHelpers.sgcHTMLRequestSession()?.UserID ?? "";
prefs.LoadPreferences(user);
string html = prefs.HTML; // one row per category, one checkbox per channel
// the application owns the values: one category.channel=1 or =0 per pair
void PrefsLoad(object sender, string userID, List<string> values)
{
values.Add("orders.inapp=1");
values.Add("orders.push=0");
values.Add("orders.email=1");
}
void PrefsSave(object sender, string userID, List<string> values)
{
// every offered pair is here, with 1 or 0
foreach (var line in values)
{
int eq = line.IndexOf('=');
myStore.Save(userID, line.Substring(0, eq), line.Substring(eq + 1) == "1");
}
}
// in the message handler that receives the form the table posts
if (prefs.ProcessAction(user, action, postedFields))
htmx.PushFragment(guid, prefs.HTML);
I membri che utilizzerai più spesso.
Categories è una collezione TsgcHTMLPreferenceCategories; ogni TsgcHTMLPreferenceCategory ha un Name, la chiave stabile usata nei valori e come nome del checkbox, e una Caption, che è ciò che legge l’utente e viene sottoposta a escape quando viene renderizzata. Una Caption vuota ripiega su Name, e una categoria con un Name vuoto viene saltata. IndexOfName ne trova una.
Channels sceglie quali tra ncInApp, ncPush e ncEmail ottengono una colonna, tutti e tre per impostazione predefinita. I nomi dei canali sono condivisi con TsgcHTMLComponent_Notification, che li richiede alla tua applicazione in OnGetChannel prima di consegnare qualsiasi cosa.
I valori sono coppie name=value, una per ogni categoria e canale offerti: orders.inapp=1, orders.push=0, orders.email=1. La chiave è il Name della categoria, un punto e la chiave del canale inapp, push oppure email. Un valore è 1 quando il canale è attivo e 0 quando è disattivato. ValueKey(aCategory, aChannel) costruisce la chiave.
LoadPreferences(aUserID) svuota Values e scatena OnLoadPreferences(aUserID, aValues): riempi aValues con le righe category.channel=1 di quell’utente. Values è una vista di sola lettura di ciò che viene renderizzato, o di ciò che è stato salvato per ultimo. Una coppia che ometti viene mostrata non selezionata.
OnSavePreferences(aUserID, aValues) riceve ogni coppia offerta con il suo nuovo valore, così la tua applicazione non deve mai indovinare che cosa significasse una chiave assente. SavePreferences(aUserID, aPosted) prende il modulo così come è arrivato, dove sono presenti solo i checkbox selezionati, e ProcessAction(aUserID, aAction, aPosted) lo instrada, restituendo False quando l’azione non è prefsSave.
Il modulo salvato viaggia come dato inviato dal client, quindi nessun id utente viene scritto nel markup, e tutto ciò che nomina una categoria o un canale che il componente non offre viene ignorato. Un modulo contraffatto non può ampliare l’insieme delle preferenze. Il componente non applica alcuna autorizzazione: prendi l’utente che agisce dalla sessione della richiesta (sgcHTMLRequestSession), mai dal modulo.
IsChecked(aCategory, aChannel) indica se una coppia è attiva, SetChecked(aCategory, aChannel, aValue) scrive 1 oppure 0 in Values, e IsOffered(aCategory, aChannel) è True quando la categoria esiste e il canale ha una colonna.
PreferencesID è l’id dell’elemento radice e il valore inviato nel campo prefs, così una pagina con più tabelle sa quale ha risposto. Title, SaveCaption e EmptyText impostano i testi, e ShowSave (attivo per impostazione predefinita) mostra il pulsante. Il modulo invia prefsSave come modulo data-sgc-ws-send, con un campo category.channel impostato a 1 per ogni checkbox selezionato.
L’unit compila quando è definito SGC_HTML, cosa che sgcVer.inc non fa per Android e iOS. Funziona in ogni edizione, e sgcHTML è un pack autonomo, venduto indipendentemente da sgcWebSockets. Il checkbox push registra soltanto una scelta: per consegnare un push serve WebPush, disponibile solo nelle edizioni Enterprise e All-Access.
Le notifiche vere e proprie sono mostrate da NotificationInbox, e Notification decide se un utente viene avvisato nella pagina o tramite un push.
| Guida in lineaRiferimento API completo e guida all’uso per questo componente. | Apri | |
| Tutti i componenti sgcHTMLEsplora la matrice completa delle funzionalità di oltre 80 componenti. | Apri | |
| Scarica la Prova GratuitaLa prova di 30 giorni include i progetti demo 60.HTML, tra cui 17.FieldService, che usa la tabella delle preferenze. | Apri | |
| PrezziLicenze Single, Team e Site con codice sorgente completo. | Apri |