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

Diff of /contributions/modules/stock/stock.module

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

revision 1.20, Mon Nov 20 16:47:37 2006 UTC revision 1.21, Mon Sep 22 03:58:45 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id: stock.module$
3    
4  //$Id: stock.module,v 1.19 2006/09/12 14:47:24 kbahey Exp $  /**
5     * @defgroup stock Stock: Uses StockAPI to provide stock data to all modules
6  // Copyright 2004-2005 Khalid Baheyeldin http://2bits.com   *
7     *
8     */
9  function stock_help($section) {  
10    /**
11     * @file
12     * Main file for the Stock module
13     *
14     * @ingroup Stock
15     */
16    
17    /**
18     * Implementation of hook_help().
19     */
20    function stock_help($path, $arg) {
21    $output = '';    $output = '';
22    
23    switch ($section) {    switch ($path) {
24      case 'admin/modules#description':      case 'admin/settings#stock':
25        $output = t('Stock quote page and block');        return t('This module provides stock quotes. It either displays a page format or a block format. Users can store their own stock symbols, and have a block that tells them how their portfolio is performing.');
       break;  
   
     case 'admin/help#stock':  
26      case 'admin/settings/stock':      case 'admin/settings/stock':
27        $output = t('This module provides stock quotes. It either displays a page format or a block format. Users can store their own stock symbols, and have a block that tells them how their portfolio is performing.');        return t('This module provides stock quotes. It either displays a page format or a block format. Users can store their own stock symbols, and have a block that tells them how their portfolio is performing.');
       break;  
28    }    }
   
   return $output;  
29  }  }
30    
31  function stock_settings() {  function stock_admin_settings() {
   if (stock_check_dependancies()) {  
32      $form['stock_description'] = array(      $form['stock_description'] = array(
33        '#type' => 'textarea',        '#type' => 'textarea',
34        '#title' => t('Description'),        '#title' => t('Description'),
# Line 36  function stock_settings() { Line 41  function stock_settings() {
41      $form['stock_block_title'] = array(      $form['stock_block_title'] = array(
42        '#type' => 'textfield',        '#type' => 'textfield',
43        '#title' => t('Stock block title text'),        '#title' => t('Stock block title text'),
44        '#default_value' => variable_get('stock_block_title', t('stocks')),        '#default_value' => variable_get('stock_block_title', t('Stocks')),
45        '#size' => 35,        '#size' => 35,
46        '#maxlength' => 255,        '#maxlength' => 255,
47        '#description' => t('If configured as a block, this text shows as the block title.'),        '#description' => t('If configured as a block, this text shows as the block title.'),
# Line 45  function stock_settings() { Line 50  function stock_settings() {
50      $form['stock_overview_title'] = array(      $form['stock_overview_title'] = array(
51        '#type' => 'textfield',        '#type' => 'textfield',
52        '#title' => t('Navigation link text'),        '#title' => t('Navigation link text'),
53        '#default_value' => variable_get('stock_overview_title', t('stock quote')),        '#default_value' => variable_get('stock_overview_title', t('Stock quote')),
54        '#size' => 35,        '#size' => 35,
55        '#maxlength' => 255,        '#maxlength' => 255,
56        '#description' => t('The text in the navigation link which points to the stock quote page.'),        '#description' => t('The text in the navigation link which points to the stock quote page.'),
57      );      );
58    }  
59        $form['stock_block_default_symbols'] = array(
60    return $form;        '#type' => 'textfield',
61  }        '#title' => t('Block default symbols'),
62          '#default_value' => variable_get('stock_block_default_symbols', ''),
63          '#size' => 40,
64          '#maxlength' => 255,
65          '#description' => t('Default stock symbols to display in the block when the user has no stocks defined. Separate each stock symbol with a space.'),
66        );
67    
68      return system_settings_form($form);
69    }
70    
71  function stock_perm() {  function stock_perm() {
72    return array ('use stock');    return array('use stock');
73  }  }
74    
75  function stock_link($type, $node = 0, $main = 0) {  function stock_link($type, $node = 0, $main = 0) {
76    $links = array();    $links = array();
77    
78    if ($type == 'page' && user_access('use stock')) {    if ($type == 'page' && user_access('use stock')) {
79      $links[] = l(t('stock'), 'stock');      $links['stock_page_long'] = array(
80          'title' => t("Stock"),
81          'href' => "stock");
82    }    }
83    
84    return $links;    return $links;
85  }  }
86    
87  function stock_menu($may_cache) {  function stock_menu() {
88    $items = array();    $items['stock'] = array(
   
   $title  = t(variable_get('stock_overview_title', 'stock quote'));  
   $path   = 'stock';  
   
   $items[] = array(  
     'path'     => 'stock',  
89      'title'    => $title,      'title'    => $title,
90      'access'   => user_access('use stock'),      'access arguments'   => user_access('use stock'),
91      'callback' => 'stock_page',      'page callback' => 'stock_page',
92      'weight'   => 0,      'type' => MENU_LOCAL_TASK,
93        'weight'   => 0,
94      );
95    
96      $items['admin/settings/stock'] = array(
97        'title'              => 'Stock settings',
98        'description'        => 'Configure displayed titles and description.',
99        'access arguments'             => user_access('administer site configuration'),
100        'page callback'           => 'drupal_get_form',
101        'callback arguments' => array('stock_admin_settings'),
102    );    );
103    
104    return $items;    return $items;
# Line 88  function stock_menu($may_cache) { Line 106  function stock_menu($may_cache) {
106    
107  // Display the stock page  // Display the stock page
108  function stock_page() {  function stock_page() {
109    if (stock_check_dependancies()) {    $output = stock_contents('page');
     $output = stock_contents('page');  
   }  
   
110    return theme('stock_page', $output);    return theme('stock_page', $output);
111  }  }
112    
 function stock_check_dependancies() {  
   if (!module_exist('stockapi')) {  
     drupal_set_message('stockapi module is not configured');  
     return false;  
   }  
   return true;  
 }  
   
113  function stock_block($op = 'list', $delta = 0) {  function stock_block($op = 'list', $delta = 0) {
114    $title = variable_get('stock_block_title', 'stocks');    $title = variable_get('stock_block_title', 'Stocks');
115    
116    switch ($op) {    switch ($op) {
117      case 'list':      case 'list':
# Line 130  function stock_block($op = 'list', $delt Line 137  function stock_block($op = 'list', $delt
137    
138  // Display the form  // Display the form
139  function stock_form($symbols) {  function stock_form($symbols) {
140      $output .= drupal_get_form('stock_form_definition', $symbols);
141      return $output;
142    }
143    
144    function stock_form_definition($symbols) {
145    global $user;    global $user;
146    
147    $form['symbol'] = array(    $form['symbol'] = array(
# Line 140  function stock_form($symbols) { Line 152  function stock_form($symbols) {
152      '#maxlength' => 255,      '#maxlength' => 255,
153      '#description' => t('Enter one or more space separated stock symbols to receive quotes on.'),      '#description' => t('Enter one or more space separated stock symbols to receive quotes on.'),
154    );    );
155    
156    $form['button_quote'] = array(    $form['button_quote'] = array(
157      '#type' => 'button',      '#type' => 'button',
     '#input' => TRUE,  
     '#name' => 'op',  
     '#button_type' => 'submit',  
158      '#value' => t('Quote'),      '#value' => t('Quote'),
159      '#execute' => TRUE,    );
160    );  
   
161    // If user is logged in, then show him a Save button    // If user is logged in, then show him a Save button
162    if ($user->uid) {    if ($user->uid) {
163      $form['button_save'] = array(      $form['button_save'] = array(
164        '#type' => 'button',        '#type' => 'button',
       '#input' => TRUE,  
       '#name' => 'op',  
       '#button_type' => 'submit',  
165        '#value' => t('Save'),        '#value' => t('Save'),
166        '#execute' => TRUE,      );
     );  
167    }    }
   
   $form['#method'] = 'post';  
168    
169    return drupal_get_form('stock_form', $form);    return $form;
170  }  }
171    
172  // Database Functions  // Database Functions
# Line 186  function stock_save_user_quotes($symbols Line 188  function stock_save_user_quotes($symbols
188  }  }
189    
190  function stock_contents($format = 'block') {  function stock_contents($format = 'block') {
191    global $user;    global $user;
192    
193    switch ($format) {    switch ($format) {
194      case 'block':      case 'block':
195        if ( $user->uid ) {        if ( $user->uid ) {
196          // get the stored tickers          // get the stored tickers
197          $symbols = stock_get_user_quotes();          $symbols = stock_get_user_quotes();
198            if (!isset($symbols) || strlen($symbols) == 0) {
199              $symbols = variable_get('stock_block_default_symbols', '');
200            }
201    
202          // get the quotes          // get the quotes
203          $output = stock_do_quote('short', $symbols);          $output = stock_do_quote('short', $symbols);
204        }        }
205        else {        else {
206          $output = l('login', 'user/login') . ' or ' . l('register', 'user/register') . ' for portfolio';          $symbols = variable_get('stock_block_default_symbols', '');
207            if (strlen($symbols) > 0) {
208              $output = stock_do_quote('short', $symbols);
209            }
210            else {
211              $output = l('Login', 'user/login') .' or '. l('register', 'user/register') .' for portfolio.';
212            }
213        }        }
214        break;        break;
215    
216      case 'page':      case 'page':
217        $op = isset($_POST['op']) ? $_POST['op'] : '';        $op = isset($_POST['op']) ? $_POST['op'] : '';
218        $edit = isset($_POST['edit']) ? $_POST['edit'] : '';        $symbols = isset($_POST['symbol']) ? strtoupper($_POST['symbol']) : '';
       $symbols = isset($edit['symbol']) ? strtoupper($edit['symbol']) : '';  
219        $saved_symbols = '';        $saved_symbols = '';
220        $output = variable_get('stock_description', 'This is the default stock quote page.');        $output = variable_get('stock_description', 'This is the default stock quote page.');
221    
222        if ($op == t('Save') && $user->uid) {        if ($op == t('Save') && $user->uid) {
223          // If the user pressed the Save button, and is logged in, save their preferences          // If the user pressed the Save button, and is logged in, save their preferences
224          stock_save_user_quotes($symbols);          stock_save_user_quotes($symbols);
225          drupal_set_message(t('Your portfolio has been saved.'));          drupal_set_message(t('Your portfolio has been saved.'));
226        }        }
227        elseif ($op == t('Quote') && $user->uid) {        elseif ($op == t('Quote') && $user->uid) {
228          // If the user pressed the Quote button, and is logged in, show his saved portfolio also, if it exists.          // If the user pressed the Quote button, and is logged in, show his saved portfolio also, if it exists.
229          $saved_symbols = stock_get_user_quotes();          $saved_symbols = stock_get_user_quotes();
230        }        }
231        else{        else{
232          if (empty($symbols)) {          if (empty($symbols)) {
233            // If nothing is entered, then get those saved, if any            // If nothing is entered, then get those saved, if any
234            $symbols = stock_get_user_quotes();            $symbols = stock_get_user_quotes();
235          }          }
236        }        }
237    
238        if (!empty($symbols)) {        if (!empty($symbols)) {
239          $output .= stock_do_quote('long', $symbols);          $output .= stock_do_quote('long', $symbols);
240          }
241    
242          if (!empty($saved_symbols)) {
243            $output .= '<p>'. l(t('Your registered portfolio is'), 'stock') .': '. $saved_symbols .'</p>';
244        }        }
245    
246        if(!empty($saved_symbols)){        $output .= stock_form($symbols);
         $output .= '<p>'. l(t('Your registered portfolio is'), 'stock') .': '. $saved_symbols .'</p>';  
       }  
   
       $output .= stock_form($symbols);  
247        break;        break;
248    }    }
249    
250    return $output;    return $output;
251  }  }
252    
253  function stock_do_quote($format = 'long', $symbol_list) {  function stock_do_quote($format = 'long', $symbol_list) {
   
254    // Get the column headers    // Get the column headers
255    $headers = stock_get_headers($format);    $headers = stock_get_headers($format);
256    
257    // Convert the space separated list of symbols into an array    // Convert the space separated list of symbols into an array
258    $symbol_list = explode(' ', trim($symbol_list));    $symbol_list = preg_split("/[\s,]+/", $symbol_list);
259    
260    foreach ($symbol_list as $symbol) {    foreach ($symbol_list as $symbol) {
261      if (!empty($symbol)) {      if (!empty($symbol)) {
262        $stock = stockapi_load($symbol);        $stock = stockapi_load($symbol);
263        if ($stock[8] != 'N/A') {        if ($stock[8] != 'N/A') {
264          // Date is 'N/A' means an invalid stock symbol          // Date is 'N/A' means an invalid stock symbol
265          $rows[] = array('data'=> stock_process_data($stock, $headers));          $rows[] = array('data' => stock_process_data($stock, $headers));
266        }else{        }
267          drupal_set_message(t('Invalid symbol') .': '. $symbol);        else {
268            drupal_set_message(t('Invalid symbol') .': '. $symbol);
269        }        }
270      }      }
271    }    }
272    
273    if (!empty($rows)) {    if (!empty($rows)) {
274      return theme('table', $headers, $rows);      return theme('table', $headers, $rows);
275    }    }
276  }  }
277    function stock_theme() {
278      return array();
279    }
280    
281  function theme_stock_page($output) {  function theme_stock_page($output) {
282    $css_path = drupal_get_path('module', 'stock') .'/stock.css";';    drupal_add_css(drupal_get_path('module', 'stock') .'/stock.css');
   drupal_set_html_head('<style type="text/css">@import "'. $css_path .'</style>');  
283    return $output;    return $output;
284  }  }
285    
286  function stock_get_headers($format = 'long') {  function stock_get_headers($format = 'long') {
287    // keys in these arrays must match the column names in the    // keys in these arrays must match the column names in the
288    // stockapi table    // stockapi table
289    $stock_long_array = array (    $stock_long_array = array(
290      'symbol'        => "Symbol",      'symbol'        => "Symbol",
291      'name'          => "Name",      'name'          => "Name",
292      'current_price' => "Last",      'current_price' => "Last",
# Line 287  function stock_get_headers($format = 'lo Line 299  function stock_get_headers($format = 'lo
299      'last_time'     => "Time"      'last_time'     => "Time"
300    );    );
301    
302    $stock_short_array = array (    $stock_short_array = array(
303      'symbol'        => "Symbol",      'symbol'        => "Symbol",
304      'current_price' => "Last",      'current_price' => "Last",
305      'change_amt'    => "Change"      'change_amt'    => "Change"
# Line 297  function stock_get_headers($format = 'lo Line 309  function stock_get_headers($format = 'lo
309    if ($format == 'short') {    if ($format == 'short') {
310      $headers = $stock_short_array;      $headers = $stock_short_array;
311    }    }
312    
313    return $headers;    return $headers;
314  }  }
315    
316  function stock_process_data($data, $headers) {  function stock_process_data($data, $headers) {
317    $columns = array();    $columns = array();
318    
319    foreach($headers as $key => $value) {    foreach ($headers as $key => $value) {
320      $field = $data[$key];      $field = $data[$key];
321    
322      // Special handling for some fields      // Special handling for some fields
323      switch($key) {      switch ($key) {
324        case 'symbol':        case 'symbol':
325          $columns[] = _add_full_quote($field);          $columns[] = _add_full_quote($field);
326          break;          break;
# Line 338  function _add_full_quote($field) { Line 350  function _add_full_quote($field) {
350    $symbol = strtoupper($field);    $symbol = strtoupper($field);
351    
352    // Then we create a URL for the full quote page    // Then we create a URL for the full quote page
353    $url = 'http://finance.yahoo.com/q?s=' . $symbol;    $url = 'http://finance.yahoo.com/q?s='. $symbol;
354    
355    return l($symbol, $url, array('target' => '_blank'), NULL, NULL, TRUE);    return l($symbol, $url, array('target' => '_blank'));
356  }  }
357    
358  function _right_align($field) {  function _right_align($field) {
359    return '<div align="right">'.$field.'</div>';    return '<div align="right">'. $field .'</div>';
360  }  }
361    
362  function _decimals($field) {  function _decimals($field) {
# Line 357  function _up_down_tick($field) { Line 369  function _up_down_tick($field) {
369    $style='plus';    $style='plus';
370    if ($field < 0) {    if ($field < 0) {
371      $style='minus';      $style='minus';
372    }    }
373    return '<div class="' . $style .'">'. $field .'</div>';    return '<div class="'. $style .'">'. $field .'</div>';
374  }  }
375    
376  function stock_update_1() {  function stock_update_1() {

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.21

  ViewVC Help
Powered by ViewVC 1.1.2