00001
00002
00003
00004
00005
00006
00007 #ifndef Forms_Base_Hpp
00008 #define Forms_Base_Hpp
00009
00010 #include "cgiClass.h"
00011 #include "cgiTemplates.h"
00012 #include "cgiCookie.h"
00013 #include "forms.h"
00014
00015 string persistingKey = "key";
00016
00017 class forms_base
00018 {
00019 protected:
00020 cgiTemplates formTemplate;
00021 changeMap changes;
00022 string mode;
00023 string m_warn;
00024 string m_keyName;
00025 cgiScript & script;
00026
00027
00028 void stringFXfer( string in, string & out );
00029 void shortFXfer( string in, short & out );
00030 void intFXfer( string in, int & out );
00031 void unfmtLongFXfer( string in, long & out );
00032 void longFXfer( string in, long & out );
00033 void llongFXfer( string in, long long & out );
00034 void doubleFXfer( string in, double & out );
00035 void moneyFXfer( string in, money & out );
00036 void boolFXfer( string in, bool & out );
00037 void dateFXfer( string in, oc_date & out );
00038 void dateFXfer( string in, time_date & out );
00039 void timeFXfer( string in, oc_time & out );
00040 public:
00041 forms_base(cgiScript & in);
00042 virtual ~forms_base();
00043
00044
00045 cgiScript & Script(void){return script;}
00046
00047
00048 virtual bool form_action( void );
00049
00050
00051 virtual void form_id_transfer( void ) = 0;
00052 virtual void form_data_transfer( void ) = 0;
00053 virtual bool dbf_action( string mode, changeMap & changes ) = 0;
00054 virtual bool form_display( void ) = 0;
00055
00056
00057 bool getInstructions( void );
00058
00059
00060 bool loadControlTemplates( string path );
00061 cgiTemplates & getTemplates( void ) { return formTemplate; }
00062
00063 virtual string makeTop(string uri, string name );
00064 virtual string makeBottom(string status );
00065
00066 virtual string makeButtons( long long key,
00067 string iLabel=" Insert ",
00068 string uLabel=" Update ",
00069 string dLabel=" Delete " );
00070
00071
00072
00073
00074 template<class valType>
00075 string makeComboBox( string label, string name, valType value,
00076 string sql = "", string defaulted = "",
00077 string attributes = "")
00078 {
00079 ocString ret = formTemplate.getParagraph("control_group");
00080 ocString val;val.append(value);
00081 ocSelect * pCombo = cmboEntry( name, sql, defaulted, attributes );
00082 if( pCombo )
00083 {
00084 pCombo->setContent(val);
00085 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00086 .replace("$control$", pCombo->getHtml().c_str());
00087 delete pCombo;
00088 }
00089 return ret;
00090 }
00091 template<class valType>
00092 string makeManualComboBox( string label, string name, valType value,
00093 ocString labelValuePair = "", string defaulted = "",
00094 string attributes = "")
00095 {
00096 ocString ret = formTemplate.getParagraph("control_group");
00097 ocString val;val.append(value);
00098 ocSelect * pCombo = cmboManualEntry( name, labelValuePair, defaulted, attributes );
00099 if( pCombo )
00100 {
00101 pCombo->setContent(val);
00102 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00103 .replace("$control$", pCombo->getHtml().c_str());
00104 delete pCombo;
00105 }
00106 return ret;
00107 }
00108 template<class valType>
00109 string makeCheckBoxes( string label, string name, valType value,
00110 ocString labelValuePair = "", string defaulted = "",
00111 string attributes = "")
00112 {
00113 ocString ret = formTemplate.getParagraph("control_group");
00114 ocString val;val.append(value);
00115
00116 ocCheckBoxes * pChecks = checksEntry( name, labelValuePair, defaulted, attributes );
00117 if( pChecks )
00118 {
00119 pChecks->setContent(val);
00120 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00121 .replace("$control$", pChecks->getHtml().c_str());
00122 delete pChecks;
00123 }
00124 return ret;
00125 }
00126 template<class valType>
00127 string makeRadios( string label, string name, valType value,
00128 ocString labelValuePair = "", string defaulted = "",
00129 string attributes = "")
00130 {
00131 ocString ret = formTemplate.getParagraph("control_group");
00132 ocString val;val.append(value);
00133
00134 ocRadio * pRadios = radioEntry( name, labelValuePair, defaulted, attributes );
00135 if( pRadios )
00136 {
00137 pRadios->setContent(val);
00138 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00139 .replace("$control$", pRadios->getHtml().c_str());
00140 delete pRadios;
00141 }
00142 return ret;
00143 }
00144 template<class valType>
00145 string makeManualComboBox( string label, string name, valType value,
00146 const char ** Values, size_t val_count, string defaulted = "",
00147 string attributes = "")
00148 {
00149 ocString ret = formTemplate.getParagraph("control_group");
00150 ocString val;val.append(value);
00151 ocSelect * pCombo = cmboManualEntry( name, "", defaulted, attributes );
00152
00153 if( pCombo )
00154 {
00155 for( int i=0;i<val_count;++i)
00156 {
00157 ocString sid="";
00158 sid.append(i);
00159 pCombo->addOption( Values[i], sid);
00160 }
00161 pCombo->setContent(val);
00162 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00163 .replace("$control$", pCombo->getHtml().c_str());
00164 delete pCombo;
00165 }
00166 return ret;
00167 }
00168
00169
00170
00171 template<class valType>
00172 string makeGenericBox( string label, string name, valType value,
00173 string attr, string size, string length = "")
00174 {
00175 ocString ret = formTemplate.getParagraph("control_group");
00176 ocString val;val.append(value);
00177 ocTextbox * pText = textEntry( name, size, length, attr );
00178 if( pText )
00179 {
00180 pText->setContent(val);
00181 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00182 .replace("$control$", pText->getHtml().c_str());
00183 delete pText;
00184 }
00185 return ret;
00186 }
00187
00188
00189
00190 template<class valType>
00191 string makeTextBox( string label, string name, valType value,
00192 string size, string length = "")
00193 {
00194 ocString ret = formTemplate.getParagraph("control_group");
00195 ocString val;val.append(value);
00196 ocTextbox * pText = textEntry( name, size, length );
00197 if( pText )
00198 {
00199 pText->setContent(val);
00200 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00201 .replace("$control$", pText->getHtml().c_str());
00202 delete pText;
00203 }
00204 return ret;
00205 }
00206
00207
00208
00209 template<class valType>
00210 string makeHiddenBox( string name, valType value )
00211 {
00212 ocString ret;
00213 ocString val;val.append(value);
00214 ocHidden * pText = hiddenEntry( name );
00215 if( pText )
00216 {
00217 pText->setContent(val);
00218 ret = pText->getHtml();
00219 delete pText;
00220 }
00221 return ret;
00222 }
00223
00224
00225
00226 template<class valType>
00227 string makeStaticBox( string label, string name, valType value,
00228 string size, string length = "")
00229 {
00230 ocString ret = formTemplate.getParagraph("control_group");
00231 ocString val;val.append(value);
00232 ocTextbox * pText = staticEntry( name, size, length );
00233 if( pText )
00234 {
00235 pText->setContent(val);
00236 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00237 .replace("$control$", pText->getHtml().c_str());
00238 delete pText;
00239 }
00240 return ret;
00241 }
00242
00243
00244
00245
00246 template<class valType>
00247 string makeDateBox( string label, string name, valType value, string size)
00248 {
00249 ocString ret = formTemplate.getParagraph("control_group");
00250 ocString val; val.append(value);
00251 ocTextbox * pText = dateEntry( name, size );
00252 if( pText )
00253 {
00254 pText->setContent(val);
00255 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00256 .replace("$control$", pText->getHtml().c_str());
00257 delete pText;
00258 }
00259 return ret;
00260 }
00261
00262
00263
00264
00265 template<class valType>
00266 string makeTimeBox( string label, string name, valType value, string size)
00267 {
00268 ocString ret = formTemplate.getParagraph("control_group");
00269 ocString val; val.append(value);
00270 ocTextbox * pText = timeEntry( name, size );
00271 if( pText )
00272 {
00273 pText->setContent(val);
00274 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00275 .replace("$control$", pText->getHtml().c_str());
00276 delete pText;
00277 }
00278 return ret;
00279 }
00280
00281
00282
00283 template<class valType>
00284 string makeNumberBox( string label, string name, valType value, string size)
00285 {
00286 ocString ret = formTemplate.getParagraph("control_group");
00287 ocString val; val.append(value);
00288
00289
00290 int i;
00291 int len = val.length();
00292 string rev;
00293 for(i=1; i<=len; i++)
00294 {
00295 rev += val[len-i];
00296 if( i%3 == 0 ) rev +=",";
00297 }
00298 val="";
00299 len =rev.length();
00300 for(i=1; i<=len; i++)
00301 {
00302 val += rev[len-i];
00303 }
00304 if(val[0]==',') val = val.replace(",","");
00305
00306 ocTextbox * pText = longNumberEntry( name, size );
00307 if( pText )
00308 {
00309 pText->setContent(val);
00310 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00311 .replace("$control$", pText->getHtml().c_str());
00312 delete pText;
00313 }
00314 return ret;
00315 }
00316 template<class valType>
00317 string makeShortBox( string label, string name, valType value, string size)
00318 {
00319 ocString ret = formTemplate.getParagraph("control_group");
00320 ocString val; val.append(value);
00321
00322
00323
00324 ocTextbox * pText = shortNumberEntry( name, size );
00325 if( pText )
00326 {
00327 pText->setContent(val);
00328 ret = ret.replace("$label$",label.c_str()).replace("$name$",name.c_str())
00329 .replace("$control$", pText->getHtml().c_str());
00330 delete pText;
00331 }
00332 return ret;
00333 }
00334
00335
00336
00337 string makeEmailBox( string label, string name, string value,
00338 string size, string length = "");
00339
00340 string makePasswordBox( string label, string name,
00341 string value, string size);
00342
00343 string makeZipBox( string label, string name, string value, string size);
00344 string makePhoneBox( string label, string name, string value, string size);
00345 string makeImageBox( string label, string name, string value, string size);
00346
00347
00348 string makeFileBox( string label, string name, string value, string size);
00349
00350
00351 string makeFileBox( string label, string name, string value, string size, string maxlen, string attr);
00352
00353 string makeMetaBox( string label, string name, string value, string size,
00354 string metaFile = "", string context = "");
00355 string makeTextArea( string label, string name,
00356 string value, string rows="12", string cols="80");
00357 string makeStateBox( string label, string name, string value);
00358 string makeBoolBox( string label, string name, bool value );
00359
00360
00361
00362 string makeDateGroup( string label, string name, time_date & value, int yearStart = 2004, int yearEnd = 2020);
00363
00364
00365
00366
00367
00368 string & getMode( void )
00369 {
00370 return mode;
00371 }
00372 string & keyName( void )
00373 {
00374 return m_keyName;
00375 }
00376 };
00377
00378
00379
00380
00381
00382 template <class db_form_object>
00383 void setKey ( db_form_object & dbfo )
00384 {
00385 if( dbfo.getMode() != "r" || dbfo.getMode() != "n" )
00386 {
00387 string keyName = dbfo. keyName();
00388 if( keyName.length() && dbfo.Script().ClientArguments().count( keyName.c_str() ) )
00389 {
00390 dbfo.key(atoll(dbfo.Script().ClientArguments()[keyName.c_str()].c_str()));
00391 }
00392 else if( dbfo.Script().ClientArguments().count("Id") )
00393 {
00394 dbfo.key(atoll(dbfo.Script().ClientArguments()["Id"].c_str()));
00395 }
00396 else if( dbfo.Script().ClientArguments().count("id") )
00397 {
00398 dbfo.key(atoll(dbfo.Script().ClientArguments()["id"].c_str()));
00399 }
00400 }
00401 if(!dbfo.key())
00402 {
00403
00404
00405 if( dbfo.Script().RequestMethod().upper() == "POST" )
00406 {
00407 cgiCookie cookie;
00408 dbfo.key( atoll(cookie.get( persistingKey.c_str() ).c_str()) );
00409 }
00410 }
00411 }
00412
00413
00414 template <class db_form_object>
00415 void setKey ( db_form_object & dbfo, const char * keyName )
00416 {
00417 if( dbfo.getMode() != "r" || dbfo.getMode() != "n" )
00418 {
00419 if( dbfo.Script().ClientArguments().count(keyName) )
00420 {
00421 dbfo.key(atoll(dbfo.Script().ClientArguments()[keyName].c_str()));
00422 }
00423 }
00424 if(!dbfo.key())
00425 {
00426
00427
00428 if( dbfo.Script().RequestMethod().upper() == "POST" )
00429 {
00430 cgiCookie cookie;
00431 dbfo.key( atoll(cookie.get(persistingKey.c_str()).c_str()) );
00432 }
00433 }
00434 }
00435 #endif
00436