/
profile.php
358 lines (292 loc) · 12 KB
/
profile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* This page does all of the real hard work - taking database values and translating them
* into graphs and such.
*/
require(__DIR__ . "/../layout/graphs.php");
require_login();
require(__DIR__ . "/../layout/templates.php");
$user = get_user(user_id());
require_user($user);
$messages = array();
$errors = array();
// is there a command to the page?
// TODO eventually replace this with ajax stuff
$enable_editing = false;
require(__DIR__ . "/_profile_move.php");
// do we need to replace/update managed graphs?
require(__DIR__ . "/../graphs/managed.php");
if ($user['needs_managed_update']) {
update_user_managed_graphs($user);
}
require(__DIR__ . "/_profile_common.php");
// a user might not have any pages displayed
$page_title_prefix = t("Your Reports:") . " ";
$enable_editing_dialog = true;
if ($pages) {
if (require_get("securities", false)) {
// displaying securities page?
$graphs = array();
$page_title = t("Your Securities");
$page_id = "securities";
$page_title_prefix = "";
$enable_editing_dialog = false;
$id_counter = 0; // $graph[id] needs to be set for unique graph HTML IDs
// premium check
if (get_premium_value($user, 'your_securities')) {
// assumes each securities_XXX table has a 'name'
$tables = get_security_exchange_tables();
$account_data = account_data_grouped();
foreach (get_security_exchange_pairs() as $exchange => $currencies) {
$table = $tables[$exchange];
$q = db()->prepare("SELECT securities.* " . (count($currencies) > 1 ? ", ss.currency" : "") . " FROM securities
JOIN " . $table . " AS ss ON securities.security_id=ss.id
WHERE exchange=? AND securities.user_id=? AND is_recent=1 ORDER BY exchange ASC, ss.name ASC");
$q->execute(array($exchange, user_id()));
$securities = $q->fetchAll();
// also get all individual securities
if (isset($account_data['Individual Securities']['individual_' . $exchange])) {
$ind_account = $account_data['Individual Securities']['individual_' . $exchange];
$table_name = $ind_account['table'];
$security_table_name = $ind_account['securities_table'];
$q = db()->prepare("SELECT t.* " . (count($currencies) > 1 ? ", ss.currency" : "") . " FROM $table_name AS t
JOIN $security_table_name AS ss ON t.security_id=ss.id
WHERE t.user_id=? ORDER BY ss.name ASC");
$q->execute(array(user_id()));
$securities2 = $q->fetchAll();
$securities = array_merge($securities, $securities2);
}
if ($securities) {
// insert heading (also functions as linebreak)
$graphs[] = array(
'id' => $id_counter++,
'graph_type' => 'heading',
'string0' => get_exchange_name($exchange),
'page_order' => 0,
'public' => false, // headings are actually private graphs to help with layout
'width' => 1,
'height' => 1,
'days' => 0,
'delta' => '',
'arg0' => 0,
'no_technicals' => true,
);
$cache = array();
// go through each security
foreach ($currencies as $c) {
foreach ($securities as $sec) {
if (!isset($sec['currency'])) {
$sec['currency'] = $c;
}
if ($sec['currency'] == $c) {
if (isset($cache[$sec['security_id']])) {
continue; // we've already displayed this security
}
$cache[$sec['security_id']] = true;
$graphs[] = array(
'id' => $id_counter++,
'graph_type' => 'securities_' . $exchange . '_' . $sec['currency'],
'arg0' => $sec['security_id'],
'width' => 4,
'height' => 2,
'public' => true,
'page_order' => 0,
'days' => 45,
'delta' => '',
'no_technicals' => true,
);
}
}
}
}
// no need to make a fake graph_page - add graph form will never be shown
}
}
} else {
// get this current page's graphs
$page_id = require_get("page", $pages[0]['id']);
$q = db()->prepare("SELECT * FROM graph_pages
JOIN graphs ON graph_pages.id=graphs.page_id
WHERE graph_pages.user_id=? AND graphs.page_id=? AND graphs.is_removed=0
ORDER BY graphs.page_order ASC, graphs.id ASC");
$q->execute(array(user_id(), $page_id));
$graphs = $q->fetchAll();
if (!$graphs) {
// make sure this is actually our page
$is_mine = false;
foreach ($pages as $page) {
if ($page['id'] == $page_id) {
$is_mine = true;
break;
}
}
if (!$is_mine) {
$errors[] = t("Unknown page.");
set_temporary_messages($messages);
set_temporary_errors($errors);
redirect(url_for('profile')); // redirect back to our home page
}
}
$page_title = t("Unknown");
foreach ($pages as $p) {
if ($p['id'] == $page_id) {
$page_title = $p['title'];
$graph_page = $p;
}
}
}
// issue #58: we need to generate a hash of (user_id, selected currencies) so that the add_graph js is
// correctly refreshed when necessary
$hash = md5($user['id'] . ":" . $user['last_summaries_update']);
page_header($page_title_prefix . $page_title, "page_profile", array('jsapi' => true, 'js' => array('profile', 'calculator', 'add_graph?hash=' . $hash), 'class' => 'report_page'));
?>
<div id="page<?php echo htmlspecialchars($page_id); ?>">
<!-- page list -->
<?php require(__DIR__ . "/_profile_pages.php"); ?>
<?php if ($enable_editing_dialog) { ?>
<div class="enable_editing">
<label><input type="checkbox" id="enable_editing"<?php if ($enable_editing) echo " checked"; ?>> <?php echo ht("Enable layout editing"); ?></label>
</div>
<?php } ?>
<!-- graphs for this page -->
<div class="graph_collection">
<?php foreach ($graphs as $graph) {
if ($graph['graph_type'] == "linebreak" || $graph['graph_type'] == "heading") { ?>
<?php if ($graph['graph_type'] == 'heading') {
echo "<h2 class=\"graph_heading\">" . htmlspecialchars($graph['string0']) . "</h2>\n";
} ?>
<div style="clear:both;">
<div class="graph_controls">
<?php } ?>
<?php
// load technicals from the database as necessary
// (we can only load one at this stage)
$q = db()->prepare("SELECT * FROM graph_technicals WHERE graph_id=? LIMIT 1");
$q->execute(array($graph['id']));
if ($technical = $q->fetch()) {
$graph['technical_type'] = $technical['technical_type'];
$graph['technical_period'] = $technical['technical_period'];
}
render_graph_new($graph, true /* TODO we should not force user data unless we actually need it */);
?>
<?php if ($graph['graph_type'] == "linebreak" || $graph['graph_type'] == "heading") { ?>
</div>
</div>
<?php } ?>
<?php }
if (!$graphs) { ?>
<div class="graph_collection_empty">
<?php if (require_get("securities", false)) {
if (get_premium_value($user, 'your_securities')) {
echo t("No securities to display! You might want to add details about :your_securities, if you have any.",
array(
':your_securities' => link_to(url_for('wizard_accounts_securities'), t("your securities")),
));
} else {
echo t('To display historical value graphs of your securities, please :upgrade, or add them as
normal "security value" graphs on one of your other :reports.',
array(
':upgrade' => link_to(url_for('premium'), t("purchase a premium account")),
':reports' => link_to(url_for('profile'), t("report pages")),
));
}
} else {
echo t("No graphs to display! You might want to add one below.");
} ?>
</div>
<?php if (require_get("securities", false)) { ?>
<div class="graph_collection_screenshot"><a href="<?php echo htmlspecialchars(url_for('screenshots#screenshots_profile_summary')); ?>" title="Illustration of Your Securities page"><img src="<?php echo htmlspecialchars(url_for('img/screenshots/profile_securities.png')); ?>" class="screenshot_image"></a></div>
<?php } ?>
<?php } ?>
</div>
</div>
<div style="clear:both;"></div><?php /* try and fix tab linebreak on Android web browser */ ?>
<?php if ($enable_editing_dialog) { ?>
<div class="tabs" id="tabs_profile">
<ul class="tab_list">
<?php /* each <li> must not have any whitespace between them otherwise whitespace will appear when rendered */ ?>
<li id="tab_profile_addgraph"><?php echo t("Add Graph"); ?></li><li id="tab_profile_addpage"><?php echo t("Add Page"); ?></li><?php if (!$graph_page['is_managed']) { ?><li id="tab_profile_deletepage"><?php echo t("Remove Page"); ?></li><?php } ?><li id="tab_profile_reset"><?php echo t("Reset"); ?></li><?php if (is_admin()) { ?><li id="tab_profile_addall"><?php echo t("Add All Graphs"); ?></li><?php } ?>
</ul>
<ul class="tab_groups">
<li id="tab_profile_addgraph_tab">
<div class="add_graph">
<h2><?php echo t("Add new graph"); ?></h2>
<?php if ($graph_page['is_managed'] && $user['graph_managed_type'] == 'auto') { ?>
<div><?php echo t("These graphs are currently :managed.", array(':managed' => link_to(url_for('wizard_reports'), t("managed automatically")))); ?></div>
<?php } else { ?>
<?php require(__DIR__ . "/_profile_add_graph.php"); ?>
<?php } ?>
</div>
</li>
<?php } ?>
<?php } else {
/* no pages */
page_header(t("Your Reports"), "page_profile", array('jsapi' => true, 'js' => 'profile', 'class' => 'report_page'));
?>
<div class="message">
<ul>
<li><?php echo ht("You have not defined any report pages - you should add a new page, or reset your graphs and pages to the site default."); ?></li>
</ul>
</div>
<div class="tabs" id="tabs_profile">
<ul class="tab_list">
<?php /* each <li> must not have any whitespace between them otherwise whitespace will appear when rendered */ ?>
<li id="tab_profile_addpage"><?php echo t("Add Page"); ?></li><li id="tab_profile_reset"><?php echo t("Reset"); ?></li>
</ul>
<ul class="tab_groups">
<?php } ?>
<?php if ($enable_editing_dialog) { ?>
<?php require(__DIR__ . "/_profile_add_page.php"); ?>
<li id="tab_profile_reset_tab" style="display:none;">
<h2><?php echo ht("Reset User Graphs"); ?></h2>
<p>
<?php echo ht("Using the button below, you can reset the layout of graphs and all graph pages to the site default. This action is permanent, but will not delete
any historical summary data associated with this account."); ?>
</p>
<form action="<?php echo htmlspecialchars(url_for('reset_graphs')); ?>" method="post">
<table class="form">
<tr>
<td>
<label>
<input type="checkbox" name="confirm" value="1"> <?php echo ht("Reset all of my graphs and pages."); ?>
</label>
</td>
</tr>
<tr>
<td class="buttons">
<input type="submit" value="<?php echo ht("Reset graphs and pages"); ?>">
</td>
</tr>
</table>
</form>
</li>
<?php if (is_admin()) { ?>
<li id="tab_profile_addall_tab" style="display:none;">
<h2><?php echo ht("Add All Graphs"); ?></h2>
<p>
<?php echo ht("Using the button below, you can reset this page and add a collection of example graphs."); ?>
</p>
<form action="<?php echo htmlspecialchars(url_for('add_all_graphs')); ?>" method="post">
<input type="hidden" name="page" value="<?php echo htmlspecialchars($page_id); ?>">
<input type="submit" value="<?php echo ht("Reset page with all graphs"); ?>">
</form>
</li>
<?php } /* is_admin */ ?>
</ul>
<?php } ?>
<?php
if (require_get("move_up", false) || require_get("move_down", false) || require_get("graph", false)) {
?>
<script type="text/javascript">
$(document).ready(function() {
var e = $("#graph_<?php echo htmlspecialchars(require_get("move_up", require_get("move_down", require_get("graph", false)))); ?>");
if (e) {
e.css({ backgroundColor: '#221111' });
// can't fade backgroundColor without JQuery color plugin
window.scrollTo(e.position().left, e.position().top);
}
});
</script>
<?php
}
page_footer();