eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
column.class.cpp
Go to the documentation of this file.
1 
2 
3 /*
4 * convention: the first column object is supposed to be the PK
5 */
6 class column
7 {
8 public:
9  string column_name; // name used in the database, eg. asset_class, asset_id
10  char col_type; // i=int, f=float, s=string, d=date
11 // bool show_it = true; // true if it should be shown to the user
12 // bool is_used = false; // true if if it is used by the relevant class
13  string input_type; // text, number, checkbox, etc.
14  // ==> set to "hidden" in order not to show it on the screen
15  // ==> set to "dropDown" for drop_down box
16  // ==> set to "date" for date
17  string drop_down_table; // eg. " + oConfig.tbl_prefix + "_asset_classes
18  string drop_down_keyField; // eg. asset_class_id
19  string drop_down_keyFieldQuote; // "" or "'"
20  string drop_down_showField; // eg. asset_class_name
21  string drop_down_default; // eg. USD
22  string label; // eg. description, amount, etc. (column header)
23  string placeholder; // eg. describe the goal, boss@universe.com
24  string post_code; // eg. desc, amnt, aid, iid, etc. NOTE: also used for the "id" (not only for "name") attributes
25  bool show_with_previous = false; // eg. from_Date, till_Date and frequency to be stacked in one table cell -->
26  // in that case show_with_previous should be true for the two last
27  string data_align = "center"; // data-align = left | right | center
28 
29  string quote();
30  int get_iValue();
31  string get_sValue();
32  float get_fValue();
33  struct tm get_dValue();
34  void set_value(int value);
35  void set_value(string value);
36  void set_value(float value);
37  void show_inputField(int iid, bool showExisting = false); // send the input field to stdout
38  void show(); // send the field value to stdout in a readable form (simply get_sValue or the dop_down_showField of the referenced table)
39 private:
40  int iValue;
41  string sValue;
42  float fValue;
43  struct tm dValue;
44 };
45 
46 int column::get_iValue() { return iValue;}
47 string column::get_sValue() { return sValue;}
48 float column::get_fValue() { return fValue;}
49 struct tm column::get_dValue() { return dValue;}
50 
51 void column::set_value(int value) {
52  iValue = value;
53  sValue = std::to_string(value);
54  fValue = value + 0.0;
55 }
56 void column::set_value(string value) {
57  sValue = value;
58  if (col_type == 'd') dateStr2tm(value, &dValue);
59  if (col_type != 'd' && col_type != 's')
60  {
61  iValue = atoi(value.c_str());
62  fValue = atof(value.c_str());
63  }
64 }
65 void column::set_value(float value) {
66  iValue = round(value);
67  sValue = to_string(value);
68  fValue = value;
69  }
70 
71  string column::quote() {
72  return (col_type == 's' || col_type == 'd') ? "'" : "";
73  }
74 
75  /* ************************************
76  * show
77  * ************************************
78  */
80 {
81  if (input_type != "hidden"){
82  if ((input_type != "dropDown") || (drop_down_keyField == drop_down_showField))
83  {
84  if (col_type == 'f') cout << curr_format(get_fValue());
85  else cout << get_sValue();
86  }
87  else
88  {
89  db.res2 = db.stmt->executeQuery("SELECT " + drop_down_showField + " FROM " + drop_down_table + " WHERE " + drop_down_keyField + " = " +drop_down_keyFieldQuote + get_sValue() + drop_down_keyFieldQuote + ";");
90  while (db.res2->next()) cout << db.res2->getString(drop_down_showField);
91  }
92  }
93 }
94 
95  /* ************************************
96  *show_inputField
97  * ************************************
98  */
99  void column::show_inputField(int iid, bool showExisting)
100  {
101  string input_type_fld;
102  if (input_type == "dropDown")
103  {
104  cout << "<select ";
105  if (input_type != "hidden") cout << "required ";
106  cout << "name=\"" + post_code + "\" id=\"" + post_code + "\">";
107  db.res = db.stmt->executeQuery("SELECT " + drop_down_keyField + ", " + drop_down_showField + " FROM " + drop_down_table + ";");
108  while (db.res->next())
109  {
110  cout << "<option value=\"" + db.res->getString(drop_down_keyField) + "\"";
111  if (showExisting)
112  {
113  if (db.res->getString(drop_down_keyField) == get_sValue()) cout << " selected ";
114  }
115  else
116  {
117  if (db.res->getString(drop_down_keyField) == drop_down_default) {cout << " selected "; }
118  }
119  cout << ">" << db.res->getString(drop_down_showField) + "</option>";
120  }
121  cout << "</select>";
122  }
123  else
124  {
125  input_type_fld = (input_type != "date")?input_type:"text\" data-date-format=\"yyyy-mm-dd";
126  cout << "<input type=\"" << input_type_fld << "\" name=\"" << post_code << "\" id=\"" << post_code << "\" ";
127  if (input_type != "hidden")
128  {
129  cout << " required ";
130  if (col_type == 'i') cout << " size=\"1\" ";
131  else if (col_type == 'f') cout << " size=\"5\" ";
132  else if (col_type == 'd') cout << " size=\"6\" ";
133  else cout << " size=\"12\" ";
134  }
135  if (post_code == "iid")
136  {
137  cout << " value=\"" << to_string(iid) << "\" ";
138  }
139  else
140  {
141  if (showExisting) cout << " value=\"" << get_sValue() << "\" ";
142  }
143  cout << " placeholder=\"" << placeholder << "\">\n";
144  }
145  }
146 
147 /* *********usage
148  tbl[1].column_name = "asset_id";
149  tbl[1].set_value(cgi("aid"));
150  tbl[1].col_type = 'i';
151  tbl[1].show_it = false;
152  tbl[1].drop_down = false;
153 
154  tbl[2].column_name = "description";
155  tbl[2].set_value(cgi("description"));
156  tbl[2].col_type = 's';
157  tbl[2].show_it = true;
158  tbl[2].drop_down = false;
159 
160 for (tbl::iterator it=tbl.begin(); it!=tbl.end(); ++it)
161  std::cout << it->column_name << " => " << it->get_value() << '\n';
162 
163 for (int i , i < 10, i++)
164 {cout << i << ": exists:" << exists(i) << "\n";}
165 
166 return o;
167  } //main
168  */
float fValue
bool show_with_previous
void show()
void set_value(int value)
void show_inputField(int iid, bool showExisting=false)
string input_type
sql::ResultSet * res
string drop_down_keyField
int iValue
sql::Statement * stmt
struct tm dValue
string drop_down_default
string drop_down_showField
string post_code
int get_iValue()
string curr_format(float dv, string the_curr="")
string sValue
sql::ResultSet * res2
string drop_down_table
string get_sValue()
string placeholder
db_helper db
Definition: efp.cpp:183
string data_align
string quote()
char col_type
void dateStr2tm(string dStr, tm *target_tm)
float get_fValue()
string drop_down_keyFieldQuote
string column_name
Definition: column.class.cpp:9
string label
struct tm get_dValue()