eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
bona.class.cpp
Go to the documentation of this file.
1 /*
2  *
3  * class bona
4  *
5  * copyright: (c) Philippe De Brouwer 2014
6  *
7  * last modification:
8  *
9  */
10 
11 class bona
12 {
13 public:
14  std::map<int, column> tbl;
15  string tbl_name = oConfig.tbl_prefix + "_assets";
16  string class_name = ""; // eg. goal, cash flow, investment
17  string action_ref; // eg. g, c, i
18  void list (int iid, bool editable = true);
19  bool save();
20  void show(bool editable = true);
21  void show_edit_form(int iid, bool showExisting = false);
22  bool delete_bona();
23  bool exists ();
24  bool load_fromEnv();
25  bool load_fromDB();
26  std::map<string, int> defList;
27 protected:
28  string order_by = "";
29 private:
30  bool verify();
31  void load_fromRecordSet();
32  void show_th(bool editable);
33  void show_tc();
34  string s_get(string lbl);
35  float f_get(string lbl);
36  int i_get(string lbl);
37  struct tm d_get(string lbl);
38 };
39 
40 /**********************************************
41  * load
42  * loads the bona from the database if the key is set
43  * ****************************************** */
45 {
46  bool rc = false;
47  string s = "";
48  s = "SELECT * FROM `" + tbl_name + "` WHERE " + tbl[1].column_name + " = " + tbl[1].quote() + tbl[1].get_sValue() + tbl[1].quote() + ";";
49  db.res = db.stmt->executeQuery(s);
50  while (db.res->next())
51  {
53  }
54  return rc;
55 }
56 
57 /**********************************************
58  * load_fromRecordSet
59  * ****************************************** */
61 {
62  for (std::map<int, column>::iterator it=tbl.begin(); it!=tbl.end(); ++it)
63  {
64  it->second.set_value(db.res->getString(it->second.column_name));
65  }
66 }
67 
68 /**********************************************
69  * show_th == table header
70  * ****************************************** */
71 void bona::show_th(bool editable)
72 {
73  cout << "<table class=\"table table-hover " + class_name + "\">\n";
74  // the table header
75  cout << "<thead><tr>";
76  for (std::map<int, column>::iterator it=tbl.begin(); it!=tbl.end(); ++it)
77  {
78  if (it->second.input_type != "hidden")
79  {
80  if (it->second.show_with_previous)
81  { cout << "<br>";}
82  else
83  { cout << "<th>";}
84  cout << it->second.label;
85  std::map<int, column>::iterator it_2 = it;
86  if (it!=tbl.end()) it_2++;
87  if (it==tbl.end() || !(it_2->second.show_with_previous))
88  {
89  cout << "</th>";
90  }
91 // cout << "<th>" << it->second.label << "</th>";
92  }
93  }
94  if (editable) cout << "<th>Actions</th>";
95  cout << "</tr></thead><tbody>\n";
96 }
97 /**********************************************
98  * show_tc == table close
99  * ****************************************** */
101 {
102  cout << "</tbody></table>\n";
103 }
104 
105 /**********************************************
106  * show_edit
107  * ****************************************** */
108 void bona::show_edit_form(int iid, bool showExisting)
109 {
110  std::map<int, column>::iterator it;
111  cout << "<span id=\"is" << this->class_name << "\"></span>";
112  cout << "<form method=\"post\" action=\"" + oConfig.soft_url + "?a=" + action_ref + "s\">";
113  show_th(true); //of course editable ;-)
114  // the input form
115  cout << "<tr>";
116  for (it=tbl.begin(); it!=tbl.end(); ++it)
117  {
118  if (it->second.show_with_previous)
119  { cout << "<br>";}
120  else
121  { if (it->second.input_type != "hidden") cout << "<td>";}
122  it->second.show_inputField(iid, showExisting);
123  std::map<int, column>::iterator it_2 = it;
124  if (it!=tbl.end()) it_2++;
125  if (it==tbl.end() || !(it_2->second.show_with_previous))
126  {
127  if (it->second.input_type != "hidden") cout << "</td>";
128  }
129  }
130  switch (oConfig.layout)
131  {
132  case '1' :
133  cout << "<td><button type=\"submit\" value=\"Submit\"><img src=\"" << oConfig.css_dir << "check.svg\" alt=\"add\" width=\"16\" alt=\"Save\"></button>";
134  cout << "<a href=\"" + oConfig.soft_url + "?a=" << action_ref << "l&amp;iid=" << iid << "\" class=\"button\"><img src=\"" << oConfig.css_dir << "arrow_left.svg\" alt=\"Go back without saving the changes!\" width=\"16\" height=\"16\"></a></td>" ;
135  break;
136  case '2' :
137  cout << "<td><button type=\"submit\" value=\"Submit\" data-toggle=\"tooltip\" title=\"" << t_save << "\"><span class=\"glyphicon glyphicon-ok\" style=\"color:green\"></span></button>";
138  cout << "<a data-toggle=\"tooltip\" title=\"" << t_cancel << "\" href=\"" + oConfig.soft_url + "?a=" << action_ref << "l&amp;iid=" << iid << "\"><span class=\"glyphicon glyphicon-chevron-left\" style=\"color:blue\"></span></a></td>" ;
139  break;
140  default :
141  cout << "<td><button type=\"submit\" value=\"Submit\"><img src=\"" << oConfig.css_dir << "check.svg\" alt=\"add\" width=\"16\" alt=\"Save\"></button>";
142  cout << "<a href=\"" + oConfig.soft_url + "?a=" << action_ref << "l&amp;iid=" << iid << "\" class=\"button\"><img src=\"" << oConfig.css_dir << "arrow_left.svg\" alt=\"Go back without saving the changes!\" width=\"16\" height=\"16\"></a></td>" ;
143  break;
144  }
145 
146  cout << "</tr>\n";
147  show_tc();
148  cout << "</form>";
149 }
150 
151 
152 
157 void bona::list(int iid, bool editable)
158 {
159  std::map<int, column>::iterator it;
160 
161  cout << "<span id=\"is" << this->class_name << "\"></span>";
162 
164  if (class_name != "")
165  {
166  cout << "<div class=\"alert alert-info\">";
167  cout << "<p>" << t_before_list[this->class_name] << "</p>";
168  cout << "</div>\n";
169  }
170 
171  if (editable) cout << "<form method=\"post\" action=\"" + oConfig.soft_url + "?a=" + action_ref + "a\">";
172  show_th(editable);
173 
174  // the list of existing bonas
175  string s = "";
176  s = "SELECT * FROM " + tbl_name + " WHERE investor = " + to_string(iid);
177  if (this->order_by != "") s = s + " ORDER BY " + this->order_by;
178  s = s + ";";
179  db.res = db.stmt->executeQuery(s);
180  while (db.res->next())
181  {
183  show(true); // includes <tr> tags
184  }
185 
186  // the input form
187  if (editable)
188  {
189  cout << "<tr>";
190  for (it=tbl.begin(); it!=tbl.end(); ++it)
191  {
192  if (it->second.input_type != "hidden")
193  {
194 
195  if (it->second.show_with_previous)
196  { cout << "<br>";}
197  else
198  { cout << "<td>";}
199  it->second.show_inputField(iid, false);
200  std::map<int, column>::iterator it_2 = it;
201  if (it!=tbl.end()) it_2++;
202  if (it==tbl.end() || !(it_2->second.show_with_previous))
203  {
204  cout << "</td>";
205  }
206  }
207  }
208  cout << "<td>";
209  switch (oConfig.layout)
210  {
211  case '1' :
212  cout << "<button type=\"submit\" value=\"Submit\"><img src=\"" << oConfig.css_dir << "plus.svg\" alt=\"add\" width=\"16\" height=\"16\" alt=\"Save\"></button>";
213  cout << "<button type=\"reset\"><img src=\"" << oConfig.css_dir << "undo.svg\" alt=\"Reset!\" width=\"16\" height=\"16\"></button></td>" ;
214  break;
215  case '2' :
216  cout << "<button type=\"submit\" value=\"Submit\" data-toggle=\"tooltip\" title=\"" << t_save << "\"><span class=\"glyphicon glyphicon-ok\" style=\"color:green\"></span></button>";
217  cout << "<button type=\"reset\" data-toggle=\"tooltip\" title=\"" << t_reset << "\"><span class=\"glyphicon glyphicon-chevron-left\" style=\"color:blue\"></span></button></td>" ;
218  break;
219  default :
220  cout << "<button type=\"submit\" value=\"Submit\"><img src=\"" << oConfig.css_dir << "plus.svg\" alt=\"add\" width=\"16\" height=\"16\" alt=\"Save\"></button>";
221  cout << "<button type=\"reset\"><img src=\"" << oConfig.css_dir << "undo.svg\" alt=\"Reset!\" width=\"16\" height=\"16\"></button></td>" ;
222  break;
223  }
224  cout << "</tr>\n";
225  }
226  show_tc();
227  cout << "<input name=\"iid\" type=\"hidden\" value=\"" << to_string(iid) << "\">";
228  cout << "</form>";
229 }
230 
231 /******************************************
232  * show
233  * ****************************************/
234 void bona::show(bool editable)
235 {
236  cout << "<tr>";
237  for (std::map<int, column>::iterator it=tbl.begin(); it!=tbl.end(); ++it)
238  {
239  if (it->second.input_type != "hidden")
240  {
241  if (it->second.show_with_previous)
242  { cout << "<br>";}
243  else
244  { cout << "<td>";}
245  it->second.show();
246  std::map<int, column>::iterator it_2 = it;
247  if (it!=tbl.end()) it_2++;
248  if (it==tbl.end() || !(it_2->second.show_with_previous))
249  {
250  cout << "</td>";
251  }
252  }
253  }
254  if (editable)
255  {
256  switch (oConfig.layout)
257  {
258  case '1' :
259  cout << "<td><a href=\"" + oConfig.soft_url + "?a=" + action_ref + "e&amp;" + action_ref + "id=" + tbl[1].get_sValue() + "\"><img src=\"" << oConfig.css_dir << "pen.svg\" width=\"16\" height=\"16\" ></a>&nbsp;<a href=\"" + oConfig.soft_url + "?a=" + action_ref + "d&amp;" + action_ref + "id=" + tbl[1].get_sValue() + "\"><img src=\"" << oConfig.css_dir << "x.svg\" width=\"16\" height=\"16\" ></a></td>";
260  break;
261  case '2' :
262  cout << "<td><a class=\"btn btn-default\" href=\"" + oConfig.soft_url + "?a=" + action_ref + "e&amp;" + action_ref + "id=" + tbl[1].get_sValue() + "\" data-toggle=\"tooltip\" title=\"" << t_edit << "\"><span class=\"glyphicon glyphicon-pencil\" style=\"color:blue\"></span></a>&nbsp;";
263  cout << "<a href=\"" + oConfig.soft_url + "?a=" + action_ref + "d&amp;" + action_ref + "id=" + tbl[1].get_sValue() + "\" data-toggle=\"tooltip\" title=\"" << t_delete << "\"><span class=\"glyphicon glyphicon-remove\" style=\"color:red\"></span></a></td>";
264  break;
265  default :
266  cout << "<td><a href=\"" + oConfig.soft_url + "?a=" + action_ref + "e&amp;" + action_ref + "id=" + tbl[1].get_sValue() + "\"><img src=\"" << oConfig.css_dir << "pen.svg\" width=\"16\" height=\"16\" ></a>&nbsp;<a href=\"" + oConfig.soft_url + "?a=" + action_ref + "d&amp;" + action_ref + "id=" + tbl[1].get_sValue() + "\"><img src=\"" << oConfig.css_dir << "x.svg\" width=\"16\" height=\"16\" ></a></td>";
267  break;
268  }
269  }
270  cout << "</tr>\n";
271 }
272 
273 /******************************************
274  * save
275  * ****************************************/
277 {
278  std::map<int, column>::iterator it;
279  string s;
280  if (exists())
281  {
282  s = "UPDATE `" + tbl_name + "` SET ";
283  for (it=tbl.begin(); it!=tbl.end(); ++it)
284  {
285  s = s + it->second.column_name + " = " + it->second.quote() + it->second.get_sValue() + it->second.quote();
286  if (++it!=tbl.end()) {s = s + ",";}
287  --it;
288  }
289  s = s + " WHERE " + tbl[1].column_name + "=" + tbl[1].quote() + tbl[1].get_sValue() + tbl[1].quote() + ";";
290  }
291  else
292  {
293  s = "INSERT INTO `" + tbl_name + "` (";
294  for (it=tbl.begin(); it!=tbl.end(); ++it)
295  {
296  if (it->second.get_sValue().length() > 0)
297  {
298  s = s + it->second.column_name;
299  if (++it!=tbl.end()) {s = s + ",";}
300  it--;
301  }
302  }
303  s = s + ") value (" ;
304  for (it=tbl.begin(); it!=tbl.end(); ++it)
305  {
306  if (it->second.get_sValue().length() > 0)
307  {
308  s = s + it->second.quote() + it->second.get_sValue() + it->second.quote();
309  if (++it!=tbl.end()) {s = s + ",";}
310  it--;
311  }
312  }
313  s = s + ");";
314  }
315  #ifdef DEBUG
316 debug_info = debug_info + "<br>goal.save: " + s;
317 #endif
318 
319  if (db.runSQL(s))
320  {return true;}
321  else
322  {
323 #ifdef DEBUG
324 debug_info = debug_info + "<br>" + s;
325 #endif
326  error_message = error_message + "Sorry, I failed to add the " + class_name + "to the database.";
327  return false;
328  }
329 return true;
330 
331 }
332 
333 /******************************************
334  * load_fromEnv
335  * ****************************************/
337 {
338  for (std::map<int, column>::iterator it=tbl.begin(); it!=tbl.end(); ++it)
339  {
340 // cout << it->second.post_code << "<BR>";
341  it->second.set_value(cgi(it->second.post_code));
342 // debug_info = debug_info + "<br>" + it->second.get_sValue();
343  }
344  if (verify()) {return true;} else {return false;}
345 }
346 
347 /**********************************************
348  * delete_bona
349  * ****************************************** */
351 {
352  string s = "";
353  s = "DELETE FROM `" + tbl_name + "` WHERE " + tbl[1].column_name + " = " + tbl[1].quote() + tbl[1].get_sValue() + tbl[1].quote() + ";";
354  if (db.runSQL(s))
355  {return true;}
356  else
357  {
358 #ifdef DEBUG
359  cout << s;
360 #endif
361  error_message = error_message + "Sorry, I failed to delete the goal :-/";
362  return false;
363  }
364 }
365 
366 /******************************************
367  * verify
368  * ****************************************/
370 {
371  bool rc = true;
372  for (std::map<int, column>::iterator it=tbl.begin(); it!=tbl.end(); ++it)
373  {
374  // if (tbl[1].get_iValue() > 0) {rc = true;} else {rc = false;}
375  }
376  return rc;
377 }
378 
379 /************************
380  * exits
381  *************************/
383  {
384  string s, q;
385  q = tbl[1].quote();
386  bool rc = false;
387  s = "SELECT COUNT(*) AS CNT FROM `" + tbl_name + "` WHERE " + tbl[1].column_name + " = " + tbl[1].quote() + tbl[1].get_sValue() + tbl[1].quote() + ";";
388 #ifdef DEBUG
389 debug_info = debug_info + "<br>bona.exists(): " + s;
390 debug_info = debug_info + "<BR>to_string(tbl[1].get_iValue()): " + to_string(tbl[1].get_iValue()) + "<br>";
391 #endif
392  if (tbl[1].get_iValue() != 0)
393  {
394  db.res = db.stmt->executeQuery(s);
395  while (db.res->next()) {rc = (db.res->getString("CNT") == "1") ? true : false;}
396  return rc;
397  }
398  else
399  {return false;}
400  } //exists
401 
402 
403 /* **************************************************
404  * functions to accesss members via their label (such as description)
405  * **************************************************
406  * usage: descr = theBona.s_get('description');
407  */
408 string bona::s_get(string lbl) {return tbl[defList[lbl]].get_sValue();}
409 float bona::f_get(string lbl) {return tbl[defList[lbl]].get_fValue();}
410 int bona::i_get(string lbl) {return tbl[defList[lbl]].get_iValue();}
411 struct tm bona::d_get(string lbl) {return tbl[defList[lbl]].get_dValue();}
string soft_url
const string css_dir
bool exists()
Definition: bona.class.cpp:382
void load_fromRecordSet()
Definition: bona.class.cpp:60
string action_ref
Definition: bona.class.cpp:17
sql::ResultSet * res
const string t_save
Definition: texts_Eng_UK.h:20
string order_by
should be the column name eventually followed by DESC/ASC, eg. "priority ASC"
Definition: bona.class.cpp:28
bool delete_bona()
Definition: bona.class.cpp:350
sql::Statement * stmt
int i_get(string lbl)
Definition: bona.class.cpp:410
void show_th(bool editable)
Definition: bona.class.cpp:71
string error_message
global message variables
Definition: efp.cpp:132
const string t_cancel
Definition: texts_Eng_UK.h:23
string s_get(string lbl)
Definition: bona.class.cpp:408
const string tbl_prefix
tables start with "" + oConfig.tbl_prefix + "_" if the prefix is eg. "tbl" then we have "tbl_invesotr...
void show_edit_form(int iid, bool showExisting=false)
Definition: bona.class.cpp:108
const string t_edit
Definition: texts_Eng_UK.h:21
bool runSQL(string s)
db_helper db
Definition: efp.cpp:183
std::map< int, column > tbl
Definition: bona.class.cpp:14
const string t_reset
Definition: texts_Eng_UK.h:19
bool load_fromEnv()
Definition: bona.class.cpp:336
unordered_map< string, string > t_before_list
Definition: texts_Eng_UK.h:171
const string t_delete
Definition: texts_Eng_UK.h:22
Cgicc cgi
Definition: efp.cpp:129
float f_get(string lbl)
Definition: bona.class.cpp:409
bool save()
Definition: bona.class.cpp:276
bool verify()
Definition: bona.class.cpp:369
bool load_fromDB()
Definition: bona.class.cpp:44
string tbl_name
Definition: bona.class.cpp:15
std::map< string, int > defList
list holding the column names in the table connected to the correct id of the column ...
Definition: bona.class.cpp:26
void show(bool editable=true)
Definition: bona.class.cpp:234
struct tm d_get(string lbl)
Definition: bona.class.cpp:411
config oConfig
mysql connection mysql connector from dev.mysql.com
Definition: efp.cpp:149
char layout
Presentation.
void list(int iid, bool editable=true)
Definition: bona.class.cpp:157
string class_name
Definition: bona.class.cpp:16
void show_tc()
Definition: bona.class.cpp:100