swad-core/swad_form.c

324 lines
10 KiB
C
Raw Normal View History

2018-11-09 20:48:17 +01:00
// swad_form.c: forms to go to actions
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2022 Antonio Ca<EFBFBD>as Vargas
2018-11-09 20:48:17 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
2019-04-20 21:11:44 +02:00
#define _GNU_SOURCE // For asprintf
#include <stdio.h> // For asprintf
2019-05-14 09:03:48 +02:00
#include <stdlib.h> // For free
2019-04-20 21:11:44 +02:00
#include "swad_error.h"
2018-11-09 20:48:17 +01:00
#include "swad_form.h"
#include "swad_global.h"
#include "swad_hierarchy_level.h"
2019-11-10 12:36:37 +01:00
#include "swad_HTML.h"
2018-11-09 20:48:17 +01:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Private global variables **************************/
/*****************************************************************************/
static bool Frm_Inside = false;
2018-11-09 20:48:17 +01:00
/*****************************************************************************/
/**************************** Private prototypes *****************************/
/*****************************************************************************/
static void Frm_BeginFormInternal (Act_Action_t NextAction,bool PutParameterLocationIfNoSesion,
2018-11-09 20:48:17 +01:00
const char *Id,const char *Anchor,const char *OnSubmit);
static inline void Frm_SetInside (bool Inside);
2018-11-09 20:48:17 +01:00
/*****************************************************************************/
/******************************** Begin a form *******************************/
2018-11-09 20:48:17 +01:00
/*****************************************************************************/
void Frm_BeginFormGoTo (Act_Action_t NextAction)
2018-11-09 20:48:17 +01:00
{
Frm_BeginFormInternal (NextAction,false,NULL,NULL,NULL); // Do not put now parameter location
2018-11-09 20:48:17 +01:00
}
void Frm_BeginForm (Act_Action_t NextAction)
2018-11-09 20:48:17 +01:00
{
Frm_BeginFormAnchorOnSubmit (NextAction,NULL,NULL);
2018-11-09 20:48:17 +01:00
}
void Frm_BeginFormAnchor (Act_Action_t NextAction,const char *Anchor)
2018-11-09 20:48:17 +01:00
{
Frm_BeginFormAnchorOnSubmit (NextAction,Anchor,NULL);
2018-11-09 20:48:17 +01:00
}
void Frm_BeginFormOnSubmit (Act_Action_t NextAction,const char *OnSubmit)
2018-11-09 20:48:17 +01:00
{
Frm_BeginFormAnchorOnSubmit (NextAction,NULL,OnSubmit);
2018-11-09 20:48:17 +01:00
}
void Frm_BeginFormAnchorOnSubmit (Act_Action_t NextAction,const char *Anchor,const char *OnSubmit)
2018-11-09 20:48:17 +01:00
{
Frm_BeginFormInternal (NextAction,true,NULL,Anchor,OnSubmit); // Do put now parameter location (if no open session)
2018-11-09 20:48:17 +01:00
}
void Frm_BeginFormId (Act_Action_t NextAction,const char *Id)
2018-11-09 20:48:17 +01:00
{
Frm_BeginFormInternal (NextAction,true,Id,NULL,NULL); // Do put now parameter location (if no open session)
2018-11-09 20:48:17 +01:00
}
static void Frm_BeginFormInternal (Act_Action_t NextAction,bool PutParameterLocationIfNoSesion,
2018-11-09 20:48:17 +01:00
const char *Id,const char *Anchor,const char *OnSubmit)
{
2018-12-08 16:43:13 +01:00
extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES];
2018-11-09 20:48:17 +01:00
char ParamsStr[Frm_MAX_BYTES_PARAMS_STR + 1];
if (!Frm_CheckIfInside ())
2018-11-09 20:48:17 +01:00
{
2019-10-20 22:00:28 +02:00
/* Begin form */
2019-11-11 00:15:44 +01:00
HTM_TxtF ("<form method=\"post\" action=\"%s/%s",
Cfg_URL_SWAD_CGI,
Lan_STR_LANG_ID[Gbl.Prefs.Language]);
2018-11-09 20:48:17 +01:00
if (Anchor)
if (Anchor[0])
2019-11-11 00:15:44 +01:00
HTM_TxtF ("#%s",Anchor);
if (Id)
{
if (Id[0])
HTM_TxtF ("\" id=\"%s\"",Id);
else
HTM_Txt ("\"");
}
else
HTM_Txt ("\"");
2018-11-09 20:48:17 +01:00
if (OnSubmit)
if (OnSubmit[0])
2019-11-18 08:38:34 +01:00
HTM_TxtF (" onsubmit=\"%s\"",OnSubmit);
2018-11-09 20:48:17 +01:00
switch (Act_GetBrowserTab (NextAction))
{
case Act_BRW_NEW_TAB:
case Act_DOWNLD_FILE:
2019-11-11 00:15:44 +01:00
HTM_Txt (" target=\"_blank\"");
2018-11-09 20:48:17 +01:00
break;
default:
break;
}
if (Act_GetContentType (NextAction) == Act_CONT_DATA)
2019-11-11 00:15:44 +01:00
HTM_Txt (" enctype=\"multipart/form-data\"");
HTM_Txt (" accept-charset=\"windows-1252\">");
2018-11-09 20:48:17 +01:00
/* Put basic form parameters */
Frm_SetParamsForm (ParamsStr,NextAction,PutParameterLocationIfNoSesion);
2019-11-10 12:36:37 +01:00
HTM_Txt (ParamsStr);
2018-11-09 20:48:17 +01:00
Frm_SetInside (true);
2018-11-09 20:48:17 +01:00
}
}
2020-05-21 04:02:29 +02:00
/*
Form without action are used in exams.
The accept-charset attribute specifies the character encodings that are to be used for the form submission
But this type of form is sent via AJAX ==>
==> we use the value property of input fields to build the parameters sent using XMLHttp.send ==>
==> the value property is always codified in UTF-8 ==> accept-charset is irrelevant
*/
void Frm_BeginFormNoAction (void)
2020-05-11 02:28:38 +02:00
{
if (!Frm_CheckIfInside ())
2020-05-11 02:28:38 +02:00
{
/* Begin form */
2020-05-21 04:02:29 +02:00
HTM_Txt ("<form accept-charset=\"UTF-8\""
2020-05-11 02:28:38 +02:00
" onsubmit=\"return false;\">"); // Form that can not be submitted, to avoid enter key to send it
Frm_SetInside (true);
2020-05-11 02:28:38 +02:00
}
}
2018-11-09 20:48:17 +01:00
void Frm_SetParamsForm (char ParamsStr[Frm_MAX_BYTES_PARAMS_STR + 1],Act_Action_t NextAction,
bool PutParameterLocationIfNoSession)
2018-11-09 20:48:17 +01:00
{
char ParamAction[Frm_MAX_BYTES_PARAM_ACTION + 1];
char ParamSession[Frm_MAX_BYTES_PARAM_SESSION + 1];
char ParamLocation[Frm_MAX_BYTES_PARAM_LOCATION + 1];
ParamAction[0] = '\0';
ParamSession[0] = '\0';
ParamLocation[0] = '\0';
if (NextAction != ActUnk)
2019-02-24 21:45:46 +01:00
{
2018-11-09 20:48:17 +01:00
snprintf (ParamAction,sizeof (ParamAction),
"<input type=\"hidden\" name=\"act\" value=\"%ld\" />",
Act_GetActCod (NextAction));
2019-02-24 21:45:46 +01:00
if (Gbl.Session.Id[0])
snprintf (ParamSession,sizeof (ParamSession),
"<input type=\"hidden\" name=\"ses\" value=\"%s\" />",
Gbl.Session.Id);
else if (PutParameterLocationIfNoSession)
2019-02-24 21:45:46 +01:00
// Extra parameters necessary when there's no open session
{
/* If session is open, course code will be get from session data,
but if there is not an open session, and next action is known,
it is necessary to send a parameter with course code */
2019-04-03 20:57:04 +02:00
switch (Gbl.Hierarchy.Level)
{
case HieLvl_CTY: // Country
2019-04-03 20:57:04 +02:00
snprintf (ParamLocation,sizeof (ParamLocation),
"<input type=\"hidden\" name=\"cty\" value=\"%ld\" />",
Gbl.Hierarchy.Cty.CtyCod);
break;
case HieLvl_INS: // Institution
2019-04-03 20:57:04 +02:00
snprintf (ParamLocation,sizeof (ParamLocation),
"<input type=\"hidden\" name=\"ins\" value=\"%ld\" />",
Gbl.Hierarchy.Ins.InsCod);
break;
case HieLvl_CTR: // Center
2019-04-03 20:57:04 +02:00
snprintf (ParamLocation,sizeof (ParamLocation),
"<input type=\"hidden\" name=\"ctr\" value=\"%ld\" />",
Gbl.Hierarchy.Ctr.CtrCod);
break;
case HieLvl_DEG: // Degree
2019-04-03 20:57:04 +02:00
snprintf (ParamLocation,sizeof (ParamLocation),
"<input type=\"hidden\" name=\"deg\" value=\"%ld\" />",
Gbl.Hierarchy.Deg.DegCod);
break;
case HieLvl_CRS: // Course
2019-04-03 20:57:04 +02:00
snprintf (ParamLocation,sizeof (ParamLocation),
"<input type=\"hidden\" name=\"crs\" value=\"%ld\" />",
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod);
2019-04-03 20:57:04 +02:00
break;
default:
break;
}
2019-02-24 21:45:46 +01:00
}
2018-11-09 20:48:17 +01:00
}
snprintf (ParamsStr,Frm_MAX_BYTES_PARAMS_STR + 1,"%s%s%s",
2018-11-09 20:48:17 +01:00
ParamAction,ParamSession,ParamLocation);
}
void Frm_EndForm (void)
{
if (Frm_CheckIfInside ())
2018-11-09 20:48:17 +01:00
{
2019-11-11 00:15:44 +01:00
HTM_Txt ("</form>");
Frm_SetInside (false);
2018-11-09 20:48:17 +01:00
}
}
/*****************************************************************************/
/***************************** Get unique Id *********************************/
/*****************************************************************************/
void Frm_SetUniqueId (char UniqueId[Frm_MAX_BYTES_ID + 1])
{
static unsigned CountForThisExecution = 0;
/***** Create Id. The id must be unique,
the page content may be updated via AJAX.
So, Id uses:
- a name for this execution (Gbl.UniqueNameEncrypted)
- a number for each element in this execution (CountForThisExecution) *****/
snprintf (UniqueId,Frm_MAX_BYTES_ID + 1,"id_%s_%u",
2018-11-09 20:48:17 +01:00
Gbl.UniqueNameEncrypted,
++CountForThisExecution);
}
2019-04-20 21:11:44 +02:00
/*****************************************************************************/
/****************** Build/free anchor string given a code ********************/
/*****************************************************************************/
void Frm_SetAnchorStr (long Cod,char **Anchor)
{
if (Cod > 0)
{
if (asprintf (Anchor,"cod_%ld",
Cod) < 0)
Err_NotEnoughMemoryExit ();
2019-04-20 21:11:44 +02:00
}
else
*Anchor = NULL;
}
void Frm_FreeAnchorStr (char *Anchor)
{
if (Anchor)
{
2019-11-06 19:45:20 +01:00
free (Anchor);
2019-04-20 21:11:44 +02:00
Anchor = NULL;
}
}
2019-12-26 22:29:04 +01:00
/*****************************************************************************/
/************************* Show label column in form *************************/
/*****************************************************************************/
2019-12-27 19:22:48 +01:00
// Id == NULL ==> label class = data
// Id[0] == '\0' ==> label class = form, no label for
// Id[0] != '\0' ==> label class = form, label for
2019-12-27 15:45:19 +01:00
void Frm_LabelColumn (const char *TDClass,const char *Id,const char *Label)
2019-12-26 22:29:04 +01:00
{
2019-12-27 15:45:19 +01:00
/***** Column/cell begin *****/
if (TDClass)
HTM_TD_Begin ("class=\"%s\"",TDClass);
else
HTM_TD_Begin (NULL);
/***** Label *****/
2019-12-26 22:29:04 +01:00
if (Id)
2019-12-27 19:22:48 +01:00
{
if (Id[0])
HTM_LABEL_Begin ("for=\"%s\" class=\"FORM_IN_%s\"",
Id,The_GetSuffix ());
2019-12-27 19:22:48 +01:00
else
HTM_LABEL_Begin ("class=\"FORM_IN_%s\"",
The_GetSuffix ());
2019-12-27 19:22:48 +01:00
}
2019-12-26 22:29:04 +01:00
else
HTM_LABEL_Begin ("class=\"DAT_%s\"",The_GetSuffix ());
2020-06-18 20:06:17 +02:00
HTM_TxtColon (Label);
2019-12-26 22:29:04 +01:00
HTM_LABEL_End ();
2019-12-27 15:45:19 +01:00
/***** Column/cell end *****/
2019-12-26 22:29:04 +01:00
HTM_TD_End ();
}
/*****************************************************************************/
/************** Set to true inside a form to avoid nested forms **************/
/*****************************************************************************/
static inline void Frm_SetInside (bool Inside)
{
Frm_Inside = Inside;
}
bool Frm_CheckIfInside (void)
{
return Frm_Inside;
}