/[drupal]/contributions/modules/stock_chart/stock_chart.module
ViewVC logotype

Contents of /contributions/modules/stock_chart/stock_chart.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Mon Nov 20 16:42:22 2006 UTC (3 years ago) by oadaeh
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +3 -3 lines
File MIME type: text/x-php
Fixed two unreported bugs: an extra quote mark and an unnecessary path slash (see CHANGLOG.txt for details).
1 <?php
2 // $Id: stock_chart.module,v 1.1 2006/10/15 06:26:13 oadaeh Exp $
3
4 // Copyright 2006 Jason Flatt http://www.oadaeh.net/ http://www.oadaeh.com/
5
6 /**
7 * @file
8 * Displays a stock chart based on configuration information provided by the user.
9 */
10
11 /**
12 * Display help and module information.
13 * @param section The section of the site we're displaying help on.
14 * @return The help text for the section.
15 */
16 function stock_chart_help($section='') {
17 switch ($section) {
18 case "admin/modules#description":
19 $output = t("Displays a chart for a given stock market ticker symbol.");
20 break;
21 case "admin/help#stock_chart":
22 $output = t("This module is used to display a form and stock chart. The form is used to modify what information is shown and how it is shown in the chart. The chart is an image that is downloaded from a stock reporting service.");
23 break;
24 }
25
26 return $output;
27 } // End function stock_chart_help.
28
29
30 /**
31 * The valid permissions for this module.
32 * @return array An array of valid permissions for the module.
33 */
34 function stock_chart_perm() {
35 return array('use stock chart');
36 } // End function stock_chart_perm()
37
38
39 /**
40 * The stub for the menu callback.
41 * @param may_cache A boolean indicating whether cacheable menu items should be returned.
42 * @return An array of menu items.
43 */
44 function stock_chart_menu($may_cache) {
45 $items = array();
46
47 $items[] = array(
48 'path' => 'stock_chart',
49 'title' => t('stock chart'),
50 'callback' => 'stock_chart_page',
51 'access' => user_access('use stock chart'),
52 );
53
54 return $items;
55 } // End function stock_chart_menu.
56
57
58 /**
59 * Display the actual chart and form to modify how the chart is displayed.
60 * @return The form created by this definition.
61 */
62 function stock_chart_page() {
63 $form['stock_chart_page_description'] = array(
64 '#value' => t('Specify the options which will show what and how the chart is to be displayed. If you wish to save a set of default display options, click on the "Save chart options" button. If you just want to see the chart displayed with the new options, click on the "Show chart" button.'),
65 '#prefix' => '<br />',
66 '#suffix' => '<br />',
67 );
68
69 // The options section:
70 $form['stock_chart_options'] = array(
71 '#type' => 'fieldset',
72 '#title' => t('Options'),
73 '#collapsible' => TRUE,
74 '#collapsed' => TRUE,
75 '#tree' => TRUE,
76 );
77
78 $stock_chart_range = array('1d' => t('1 day'), '5d' => t('5 days'), '3m' => t('3 months'), '6m' => t('6 months'), '1y' => t('1 year'), '2y' => t('2 years'), '5y' => t('5 years'), 'my' => t('Maximum'));
79 $form['stock_chart_options']['stock_chart_range'] = array(
80 '#type' => 'select',
81 '#title' => t('Range'),
82 '#default_value' => variable_get('stock_chart_range', ''),
83 '#options' => $stock_chart_range,
84 '#description' => t('The range or lookback.'),
85 '#prefix' => '<span class="chart-options-inline">',
86 '#suffix' => '</span>',
87 );
88
89 $stock_chart_type = array('b' => t('Bar'), 'l' => t('Line'), 'c' => t('Candle'));
90 $form['stock_chart_options']['stock_chart_type'] = array(
91 '#type' => 'select',
92 '#title' => t('Chart type'),
93 '#default_value' => variable_get('stock_chart_type', ''),
94 '#options' => $stock_chart_type,
95 '#description' => t('The type of chart.'),
96 '#prefix' => '<span class="chart-options-inline">',
97 '#suffix' => '</span>',
98 );
99
100 $stock_chart_scale = array('off' => t('Linear'), 'on' => t('Log'));
101 $form['stock_chart_options']['stock_chart_scale'] = array(
102 '#type' => 'select',
103 '#title' => t('Scale'),
104 '#default_value' => variable_get('stock_chart_scale', ''),
105 '#options' => $stock_chart_scale,
106 '#description' => t('The scale of the chart.'),
107 '#prefix' => '<span class="chart-options-inline">',
108 '#suffix' => '</span>',
109 );
110
111 $stock_chart_size = array('m' => t('Small'), 'l' => t('Large'));
112 $form['stock_chart_options']['stock_chart_size'] = array(
113 '#type' => 'select',
114 '#title' => t('Size'),
115 '#default_value' => variable_get('stock_chart_size', ''),
116 '#options' => $stock_chart_size,
117 '#description' => t('The size of the chart.'),
118 '#prefix' => '<span class="chart-options-inline">',
119 '#suffix' => '</span>',
120 );
121
122 $stock_chart_movingavg = array('m5' => t('5'), 'm10' => t('10'), 'm20' => t('20'), 'm50' => t('50'), 'm100' => t('100'), 'm200' => t('200'));
123 $form['stock_chart_options']['stock_chart_movingavg'] = array(
124 '#type' => 'checkbox_columns',
125 '#title' => t('Moving Average (MA)'),
126 '#default_value' => variable_get('stock_chart_movingavg', array('')),
127 '#columns' => 6,
128 '#options' => $stock_chart_movingavg,
129 '#prefix' => '<span class="chart-options-clear">',
130 '#suffix' => '</span>',
131 );
132 $form['stock_chart_options']['stock_chart_movingavg_description'] = array(
133 '#type' => 'item',
134 '#value' => t('The stock\'s moving average overlay (only a maximum of five of the combined selections from MA, EMA and overlays will be shown).'),
135 '#prefix' => '<span class="chart-options-clear">',
136 '#suffix' => '</span>',
137 );
138
139 $stock_chart_estmovavg = array('e5' => t('5'), 'e10' => t('10'), 'e20' => t('20'), 'e50' => t('50'), 'e100' => t('100'), 'e200' => t('200'));
140 $form['stock_chart_options']['stock_chart_estmovavg'] = array(
141 '#type' => 'checkbox_columns',
142 '#title' => t('Exponential Moving Average (EMA)'),
143 '#default_value' => variable_get('stock_chart_estmovavg', array('')),
144 '#columns' => 6,
145 '#options' => $stock_chart_estmovavg,
146 '#prefix' => '<span class="chart-options-clear">',
147 '#suffix' => '</span>',
148 );
149 $form['stock_chart_options']['stock_chart_estmovavg_description'] = array(
150 '#type' => 'item',
151 '#value' => t('The stock\'s EMA overlay (only a maximum of five of the combined selections from MA, EMA and overlays will be shown).'),
152 '#prefix' => '<span class="chart-options-clear">',
153 '#suffix' => '</span>',
154 );
155
156 $stock_chart_other_overlays = array('b' => t('Bollinger Bands'), 'p' => t('Parabolic SAR'), 's' => t('Splits'), 'v' => t('Volume'));
157 $form['stock_chart_options']['stock_chart_other_overlays'] = array(
158 '#type' => 'checkbox_columns',
159 '#title' => t('Overlays'),
160 '#default_value' => variable_get('stock_chart_other_overlays', array('')),
161 '#columns' => 4,
162 '#options' => $stock_chart_other_overlays,
163 '#prefix' => '<span class="chart-options-clear">',
164 '#suffix' => '</span>',
165 );
166 $form['stock_chart_options']['stock_chart_other_overlays_description'] = array(
167 '#type' => 'item',
168 '#value' => t('Other various overlays to show on the chart (only a maximum of five of the combined selections from MA, EMA and overlays will be shown).'),
169 '#prefix' => '<span class="chart-options-clear">',
170 '#suffix' => '</span>',
171 );
172
173 $stock_chart_indicators = array('m26-12-9' => t('MACD'), 'f14' => t('MFI'), 'p12' => t('ROC'), 'r14' => t('RSI'), 'ss' => t('Slow Stoch'), 'fs' => t('Fast Stoch'), 'v' => t('Vol'), 'vm' => t('Vol+MA'), 'w14' => t('W%R'));
174 $form['stock_chart_options']['stock_chart_indicators'] = array(
175 '#type' => 'checkbox_columns',
176 '#title' => t('Indicators'),
177 '#default_value' => variable_get('stock_chart_indicators', array('')),
178 '#columns' => 9,
179 '#options' => $stock_chart_indicators,
180 '#prefix' => '<span class="chart-options-clear">',
181 '#suffix' => '</span>',
182 );
183 $form['stock_chart_options']['stock_chart_indicators_description'] = array(
184 '#type' => 'item',
185 '#value' => t('Optional indicators to show underneath the chart.'),
186 '#prefix' => '<span class="chart-options-clear">',
187 '#suffix' => '</span>',
188 );
189
190
191 $form['stock_chart_options']['save_stock_chart_options'] = array(
192 '#type' => 'submit',
193 '#value' => t('Save chart options'),
194 '#prefix' => '<span class="chart-options-clear">',
195 '#suffix' => '</span>',
196 );
197
198
199 $form['stock_chart_symbol'] = array(
200 '#type' => 'textfield',
201 '#title' => t('Stock symbol'),
202 '#default_value' => variable_get('stock_chart_symbol', ''),
203 '#description' => t('Enter the stock symbol of the stock to show the chart for.'),
204 '#size' => '10',
205 '#maxlength' => '10',
206 '#required' => 'TRUE',
207 );
208
209
210 $form['stock_chart_image'] = array(
211 '#type' => 'markup',
212 '#value' => '<img src="' . variable_get('stock_chart_image_url', $stock_chart_image_url) . '" />',
213 '#suffix' => '<br /><br />',
214 );
215
216
217 $form['submit'] = array(
218 '#type' => 'submit',
219 '#value' => t('Show chart')
220 );
221
222 return drupal_get_form('stock_chart_page', $form);
223 } // End function stock_chart_page.
224
225
226 /**
227 * This is where the page's display is setup.
228 * @param form The form definition.
229 * @return The themed page.
230 */
231 function theme_stock_chart_page($form) {
232 $output = '';
233 $css_path = drupal_get_path('module', 'stock_chart') .'/stock_chart.css';
234 drupal_set_html_head('<style type="text/css">@import "'. $css_path .'";</style>');
235
236 $output .= form_render($form['stock_chart_page_description']);
237 $output .= form_render($form['stock_chart_options']);
238 $output .= form_render($form['stock_chart_symbol']);
239
240 if ($form_values['stock_chart_chart']['stock_chart_symbol'] != '') {
241 foreach ($form_values['stock_chart_options']['chart_movingavg'] as $key => $value) {
242 if ($value) {
243 $all_overlays .= $key . ',';
244 }
245 }
246 foreach ($form_values['stock_chart_options']['stock_chart_estmovavg'] as $key => $value) {
247 if ($value) {
248 $all_overlays .= $key . ',';
249 }
250 }
251 foreach ($form_values['stock_chart_options']['stock_chart_other_overlays'] as $key => $value) {
252 if ($value) {
253 $all_overlays .= $key . ',';
254 }
255 }
256 $all_overlays = substr($all_overlays, 0, strlen($all_overlays) - 1);
257
258 foreach ($form_values['stock_chart_options']['stock_chart_indicators'] as $key => $value) {
259 if ($value) {
260 $indicators .= $key . ',';
261 }
262 }
263 $indicators = substr($indicators, 0, strlen($indicators) - 1);
264
265 $stock_chart_image_url = 'http://ichart.finance.yahoo.com/z?s='. $form_values['stock_chart_chart']['stock_chart_symbol'] . '&amp;t=' . $form_values['stock_chart_options']['stock_chart_range'] . '&amp;q=' . $form_values['stock_chart_options']['stock_chart_type'] . '&amp;l=' . $form_values['stock_chart_options']['stock_chart_scale'] . '&amp;z=' . $form_values['stock_chart_options']['stock_chart_size'] . '&amp;p=' . $all_overlays . '&amp;a=' . $indicators;
266
267 $output .= '<img src="' . $stock_chart_image_url . '">';
268 }
269
270 $output .= form_render($form);
271
272 return $output;
273 } // End function theme_stock_chart_page.
274
275
276 /**
277 * Define a new type of form element: checkbox_columns.
278 * @return The checkbox_columns type definition.
279 */
280 function stock_chart_elements() {
281 $type['checkbox_columns'] = array(
282 '#input' => TRUE,
283 '#process' => array('expand_checkbox_columns' => array()),
284 '#tree' => TRUE
285 );
286
287 return $type;
288 } // End function stock_chart_elements.
289
290
291 /**
292 * Define how the new checkbox_columns element works.
293 * @param element The checkbox_columns element.
294 * @return The series of themed checkboxes.
295 */
296 function expand_checkbox_columns($element) {
297 $value = is_array($element['#value']) ? $element['#value'] : array();
298 $element['#type'] = 'checkboxes';
299 $element['#tree'] = TRUE;
300
301 if (count($element['#options']) > 0) {
302 if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
303 $element['#default_value'] = array();
304 }
305
306 foreach ($element['#options'] as $key => $choice) {
307 $class = ($column % $element['#columns']) && $column ? 'chart-options-inline' : 'chart-options-clear';
308
309 if (!isset($element[$key])) {
310 $element[$key] = array(
311 '#type' => 'checkbox',
312 '#processed' => TRUE,
313 '#title' => $choice,
314 '#default_value' => in_array($key, $value),
315 '#attributes' => $element['#attributes'],
316 '#prefix' => '<span class="' . $class . '">',
317 '#suffix' => '</span>',
318 );
319 }
320
321 $column++;
322 }
323 }
324
325 return $element;
326 } // End function expand_checkbox_columns.
327
328
329 /**
330 * Define what happens when any of the form's buttons are pressed.
331 * What happens is the $op variable is checked to see if the 'Save chart options' button was pressed. If it was, it saves the settings, then refreshes the chart. If it wasn't, it just refreshes the chart with whatever settings are set in the form.
332 * @param form_id The form identifier.
333 * @param form_values The fields of the form.
334 * @return The completed form.
335 */
336 function stock_chart_page_submit($form_id, $form_values) {
337 if ($_POST['op'] == 'Save chart options') {
338 variable_set('stock_chart_range', $form_values['stock_chart_options']['stock_chart_range']);
339 variable_set('stock_chart_type', $form_values['stock_chart_options']['stock_chart_type']);
340 variable_set('stock_chart_scale', $form_values['stock_chart_options']['stock_chart_scale']);
341 variable_set('stock_chart_size', $form_values['stock_chart_options']['stock_chart_size']);
342
343 $movingavgs = array();
344 $estmovavgs = array();
345 $other_overlays = array();
346 $indicators = array();
347
348 foreach ($form_values['stock_chart_options']['stock_chart_movingavg'] as $key => $value) {
349 if ($value) {
350 if ($key != '') {
351 $movingavgs[] = $key;
352 }
353 }
354 }
355 if (count($movingavgs) > 0) {
356 variable_set('stock_chart_movingavg', $movingavgs);
357 }
358 else {
359 variable_del('stock_chart_movingavg');
360 }
361
362 foreach ($form_values['stock_chart_options']['stock_chart_estmovavg'] as $key => $value) {
363 if ($value) {
364 if ($key != '') {
365 $estmovavgs[] = $key;
366 }
367 }
368 }
369 if (count($estmovavgs) > 0) {
370 variable_set('stock_chart_estmovavg', $estmovavgs);
371 }
372 else {
373 variable_del('stock_chart_estmovavg');
374 }
375
376 foreach ($form_values['stock_chart_options']['stock_chart_other_overlays'] as $key => $value) {
377 if ($value) {
378 if ($key != '') {
379 $other_overlays[] = $key;
380 }
381 }
382 }
383 if (count($other_overlays) > 0) {
384 variable_set('stock_chart_other_overlays', $other_overlays);
385 }
386 else {
387 variable_del('stock_chart_other_overlays');
388 }
389
390 foreach ($form_values['stock_chart_options']['stock_chart_indicators'] as $key => $value) {
391 if ($value) {
392 if ($key != '') {
393 $indicators[] = $key;
394 }
395 }
396 }
397 if (count($indicators) > 0) {
398 variable_set('stock_chart_indicators', $indicators);
399 }
400 else {
401 variable_del('stock_chart_indicators');
402 }
403 }
404
405 $all_overlays = '';
406 $indicators = '';
407
408 foreach ($form_values['stock_chart_options']['stock_chart_movingavg'] as $key => $value) {
409 if ($value) {
410 if ($key != '') {
411 $all_overlays .= $key . ',';
412 }
413 }
414 }
415
416 foreach ($form_values['stock_chart_options']['stock_chart_estmovavg'] as $key => $value) {
417 if ($value) {
418 if ($key != '') {
419 $all_overlays .= $key . ',';
420 }
421 }
422 }
423
424 foreach ($form_values['stock_chart_options']['stock_chart_other_overlays'] as $key => $value) {
425 if ($value) {
426 if ($key != '') {
427 $all_overlays .= $key . ',';
428 }
429 }
430 }
431 $all_overlays = substr($all_overlays, 0, strlen($all_overlays) - 1);
432
433 foreach ($form_values['stock_chart_options']['stock_chart_indicators'] as $key => $value) {
434 if ($value) {
435 if ($key != '') {
436 $indicators .= $key . ',';
437 }
438 }
439 }
440 $indicators = substr($indicators, 0, strlen($indicators) - 1);
441
442 // Yahoo!
443 $stock_chart_image_url = 'http://ichart.finance.yahoo.com/z?s='. $form_values['stock_chart_symbol'] . '&amp;t=' . $form_values['stock_chart_options']['stock_chart_range'] . '&amp;q=' . $form_values['stock_chart_options']['stock_chart_type'] . '&amp;l=' . $form_values['stock_chart_options']['stock_chart_scale'] . '&amp;z=' . $form_values['stock_chart_options']['stock_chart_size'] . '&amp;p=' . $all_overlays . '&amp;a=' . $indicators;
444
445 variable_set('stock_chart_symbol', $form_values['stock_chart_symbol']);
446 variable_set('stock_chart_image_url', $stock_chart_image_url);
447
448 drupal_get_form('stock_chart_page', $form);
449 } // End function stock_chart_page_submit.

  ViewVC Help
Powered by ViewVC 1.1.2