-
Notifications
You must be signed in to change notification settings - Fork 1
/
stockapi.module
167 lines (149 loc) · 4.69 KB
/
stockapi.module
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
<?php
/**
* @file
* API for retrieving stock information from Alpha Vantage.
*/
// TODO: Add historical data option.
// TODO: Updates to default stockapi symbols
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Render\FormattableMarkup;
module_load_include('inc', 'stockapi');
/**
* Implements hook_cron().
*/
function stockapi_cron() {
if (REQUEST_TIME - \Drupal::config('stockapi.settings')->get('stockapi_fetch_last') >= \Drupal::config('stockapi.settings')->get('stockapi_fetch')) {
if (intval(\Drupal::config('stockapi.settings')->get('stockapi_fetch_first')) < 1) {
stockapi_fetch_symbols();
}
else {
$symbols = array();
$result = db_query('SELECT symbol FROM {stockapi} ORDER BY updated');
if (count($result)) {
foreach ($result as $data) {
$symbols[] = $data->symbol;
}
}
$default_symbols = \Drupal::config('stockapi.settings')->get('stockapi_default_symbols');
if (\Drupal\Component\Utility\Unicode::strlen($default_symbols)) {
$default_symbols_array = explode(' ', $default_symbols);
$symbols = array_unique(array_merge($default_symbols_array, $symbols));
}
if (count($symbols)) {
$stocks = stockapi_fetch($symbols);
foreach ($stocks as $key => $stock) {
stockapi_save(_stockapi_to_object($stock));
}
\Drupal::moduleHandler()->invokeAll('stockapi_post_update', [$stocks]);
\Drupal::configFactory()->getEditable('stockapi.settings')->set('stockapi_fetch_last', REQUEST_TIME)->save();
\Drupal::cache()->delete('variables');
}
}
}
}
/**
* Create the basic StockAPI page.
*/
function stockapi_fetch_symbols() {
if (intval(\Drupal::config('stockapi.settings')->get('stockapi_fetch_first')) < 1) {
$symbols = array();
$default_symbols = \Drupal::config('stockapi.settings')->get('stockapi_default_symbols');
if (\Drupal\Component\Utility\Unicode::strlen($default_symbols)) {
$symbols = explode(' ', $default_symbols);
}
if (!count($symbols)) {
$symbols = stockapi_get_default_symbols();
}
$stocks = stockapi_fetch($symbols);
foreach ($stocks as $stock) {
$stock = _stockapi_to_object($stock);
stockapi_save($stock);
}
\Drupal::moduleHandler()->invokeAll('stockapi_post_update', [$stocks]);
\Drupal::configFactory()->getEditable('stockapi.settings')->set('stockapi_fetch_first', REQUEST_TIME)->save();
\Drupal::logger('stockapi')->notice(sprintf("Stock API has successfully loaded all default symbols: %s", strftime('Y-m-d H:i:s', REQUEST_TIME)), []);
}
}
/**
* Generate link to full quote for the stock sybol
*
* @param string $field
* @return string
*/
function stockapi_format_symbol($field, $title = '', $exchange = NULL) {
if (empty($exchange)) {
$result = db_query("SELECT exchange FROM {stockapi} WHERE symbol = :symbol", array(':symbol' => $field))->fetchField();
if (empty($result)) {
$exchange = 'TSE';
}
else {
$exchange = $result;
}
}
switch ($exchange) {
case 'NYSE':
$exchange = 'NYSE';
break;
case 'TSE':
$exchange = 'TSE';
break;
case 'Toronto Stock Exchange':
$exchange = 'TSE';
break;
case 'New York Stock Exchange':
$exchange = 'NYSE';
break;
case 'NASDAQ':
$exchange = 'NASDAQ';
break;
case '':
$exchange = 'TSE';
break;
case NULL:
$exchange = 'TSE';
break;
default:
$exchange = 'TSE';
break;
}
$options = ['query' => ['q' => $exchange . ':' . $field], 'attributes' => ['target' => '_blank', 'title' => (Unicode::strlen($title)) ? $title : $field]];
$url = Url::fromUri('http://finance.google.com/finance', $options);
$link = Link::fromTextAndUrl(Unicode::strtoupper($field), $url);
return $link;
}
/**
* Right-align data in the table cell
*
* @param string $field
* @return string
*/
function stockapi_format_align($field) {
return new FormattableMarkup('<span align="right">' . $field .'</span>', ['@field' => '$field']);
}
/**
* Format decimals
*
* @param string $field
* @return double
*/
function stockapi_format_decimals($field) {
return number_format(doubleval($field), \Drupal::config('stockapi.settings')->get('stockapi_decimals'));
}
/**
* Style the quote to show whether there's been an upward or downward trend
* on the stock quote
*
* @param string $field
* @return string
*/
function stockapi_format_change($field) {
// Add a style for the change field, so we can
// add color, up tick/down tick, ...etc.
$style = 'plus';
if ($field < 0) {
$style = 'minus';
}
return new FormattableMarkup('<span class="' . $style . '">' . $field . '</span>', ['@field' => '$field']);
}