? stock/stock.install
Index: stock/INSTALL.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/stock/INSTALL.txt,v
retrieving revision 1.2
diff -u -r1.2 INSTALL.txt
--- stock/INSTALL.txt	23 Oct 2005 18:37:18 -0000	1.2
+++ stock/INSTALL.txt	28 Jun 2006 20:45:37 -0000
@@ -7,28 +7,24 @@
 Requirements:
 -------------
 	
-This module requires drupal 4.5.x or greater.
+This module requires drupal 4.7.x or greater.
 
 The stockapi module must be installed and configured for this module to run.
 
 Installation:
 -------------
 
-1. Copy the stock.module to the Drupal modules/ directory.
+1. Copy the stock module directory to the Drupal modules/ directory.
 
-2. Create the database table stock. Use the database.mysql file to do so
-   either from MySQL prompt, or from phpmyadmin. The table is very simple,
-   and adapting it to other databases should be pretty straightforward.
-
-3. Allow access to the module under admin/user/configure/permission. You can
+2. Allow access to the module under admin/user/configure/permission. You can
    enable it for authenticated and/or anonymous users.
 
-4. Configure the module under admin/settings/sitemenu
+3. Configure the module under admin/settings/sitemenu
    - Provide a description. It can be any text you want for your stock quote page
 
    - You can change the navigation link text if you want. Normally leave that as it is.
 
-5. For users to configure a portfolio, the administrator has to first enable the 
+4. For users to configure a portfolio, the administrator has to first enable the 
    block for this module.
 
      . Go to /admin/settings/stock, and choose a title for the block text (e.g. 'portfolio')
Index: stock/README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/stock/README.txt,v
retrieving revision 1.5
diff -u -r1.5 README.txt
--- stock/README.txt	23 Oct 2005 18:33:56 -0000	1.5
+++ stock/README.txt	28 Jun 2006 20:45:37 -0000
@@ -22,10 +22,6 @@
 * Can be configured as a block, providing a custom portfolio for logged in
   users
 
-Database:
----------
-This module requires that the database table 'stock' be created.
-
 Dependancies:
 -------------
 
Index: stock/stock.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/stock/stock.module,v
retrieving revision 1.16
diff -u -r1.16 stock.module
--- stock/stock.module	21 Jan 2006 02:47:20 -0000	1.16
+++ stock/stock.module	28 Jun 2006 20:45:37 -0000
@@ -6,40 +6,57 @@
 
 
 function stock_help($section) {
-  $output ="";
+  $output = '';
 
   switch ($section) {
     case 'admin/modules#description':
-      $output = t("Stock quote page and block");
+      $output = t('Stock quote page and block');
       break;
+      
     case 'admin/help#stock':
     case 'admin/settings/stock':
-      $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.");
+      $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.');
       break;
   }
+  
   return $output;
 }
   
 function stock_settings() {
-  if (stock_check_dependancies()) {
-    $output = form_textarea(t("Description"), "stock_description",
-      variable_get("stock_description", t("This is the stock quote page.")), 70, 7,
-      t('This text will be displayed at the top of the stock quote page'));
-
-    $output .= form_textfield(t("Stock block title text"), "stock_block_title",
-      variable_get("stock_block_title", t("stocks")), 35, 255,
-      t("If configured as a block, this text shows as the block title."));  
-
-    $output .= form_textfield(t("Navigation link text"), "stock_overview_title",
-      variable_get("stock_overview_title", t("stock quote")), 35, 255,
-      t("The text in the navigation link which points to the stock quote page."));  
-  }
+  if (stock_check_dependancies()) {   
+    $form['stock_description'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Description'),
+      '#default_value' =>  variable_get('stock_description', t('This is the stock quote page.')),
+      '#cols' => 70,
+      '#rows' => 7,
+      '#description' => t('This text will be displayed at the top of the stock quote page'),
+    );
 
-  return $output;
+    $form['stock_block_title'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Stock block title text'),
+      '#default_value' => variable_get('stock_block_title', t('stocks')),
+      '#size' => 35,
+      '#maxlength' => 255,
+      '#description' => t('If configured as a block, this text shows as the block title.'),
+    );
+
+    $form['stock_overview_title'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Navigation link text'),
+      '#default_value' => variable_get('stock_overview_title', t('stock quote')),
+      '#size' => 35,
+      '#maxlength' => 255,
+      '#description' => t('The text in the navigation link which points to the stock quote page.'),
+    );
+  }  
+    
+  return $form;
 }  
 
 function stock_perm() {
-  return array ("use stock");
+  return array ('use stock');
 }
 
 function stock_link($type, $node = 0, $main = 0) {
@@ -55,15 +72,16 @@
 function stock_menu($may_cache) {
   $items = array();
 
-  $title  = t(variable_get("stock_overview_title", "stock quote"));
+  $title  = t(variable_get('stock_overview_title', 'stock quote'));
   $path   = 'stock';
 
   $items[] = array(
     'path'     => 'stock',
     'title'    => $title,
-    'access'   => user_access('use stock'),
+    'access'   => user_access('use stock'),    
     'callback' => 'stock_page',
-    'weight'   => 0);
+    'weight'   => 0,    
+  );
 
   return $items;
 }
@@ -74,7 +92,7 @@
     $output = stock_contents('page');
   }
 
-  print theme("stock_page", $output);
+  return theme('stock_page', $output);
 }
 
 function stock_check_dependancies() {
@@ -86,7 +104,7 @@
 }
 
 function stock_block($op = 'list', $delta = 0) {
-  $title = variable_get("stock_block_title", "stocks");
+  $title = variable_get('stock_block_title', 'stocks');
 
   switch ($op) {
     case 'list':
@@ -97,11 +115,11 @@
       switch ($delta) {
         case 0:
           $block['subject'] = t($title);
-         if ( user_access ('use stock')) {
-           $block['content'] = stock_contents( 'block' );
+         if (user_access('use stock')) {
+           $block['content'] = stock_contents('block');
          }
          else {
-           $block['content'] = t("You do not have permission to access this feature");
+           $block['content'] = t('You do not have permission to access this feature');
          }
           break;
         }
@@ -112,19 +130,40 @@
 
 // Display the form
 function stock_form($symbols) {
- global $user;
-
- $output .= form_textfield ( t("Stock Symbol(s)"), "symbol", "$symbols", 40, 255);
- $output .= form_submit (t("Quote"), "button_quote");
-
- // If user is logged in, then show them a Save button
- if ( $user->uid ) {
-  $output .= form_submit (t("Save"), "button_save");
- }
+  global $user;
 
- $form = form($output);
+  $form['symbol'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Stock Symbol(s)'),
+    '#default_value' => $symbols,
+    '#size' => 40,
+    '#maxlength' => 255,    
+  );
+     
+  $form['button_quote'] = array(
+    '#type' => 'button',
+    '#input' => TRUE,
+    '#name' => 'op',
+    '#button_type' => 'submit',
+    '#value' => t('Quote'),
+    '#execute' => TRUE,
+  );        
+   
+  // If user is logged in, then show him a Save button
+  if ($user->uid) {  
+    $form['button_save'] = array(
+      '#type' => 'button',
+      '#input' => TRUE,
+      '#name' => 'op',
+      '#button_type' => 'submit',
+      '#value' => t('Save'),
+      '#execute' => TRUE,
+    );      
+  }
+  
+  $form['#method'] = 'post';
 
- return $form;
+  return drupal_get_form('stock_form', $form);
 }
 
 // Database Functions
@@ -146,8 +185,7 @@
 }
 
 function stock_contents($format = 'block') {
-  global $user;
-
+  global $user;  
 
   switch ($format) {
     case 'block':
@@ -163,41 +201,42 @@
       }
       break;
 
-    case 'page':
-      $edit = $_POST['edit'];
-      if (!isset($edit['symbol'])) {
-        // If nothing is entered, then get those saved, if any
-        $symbols = stock_get_user_quotes();
-      }
+    case 'page':      
+      $op = isset($_POST['op']) ? $_POST['op'] : '';
+      $edit = isset($_POST['edit']) ? $_POST['edit'] : '';  
+      $symbols = isset($edit['symbol']) ? strtoupper($edit['symbol']) : '';          
+      $saved_symbols = '';
+      $output = variable_get('stock_description', 'This is the default stock quote page.');            
       
-      if (isset($_POST['button_quote'])) {
-        // If the user pressed 'Quote', then get the symbols they entered
-        $symbols = strtoupper($edit['symbol']);
-        if (isset($symbols)) {
-          $output .= stock_do_quote('long', $symbols);
-        }
+      if ($op == t('Save') && $user->uid) {
+        // If the user pressed the Save button, and is logged in, save their preferences        
+        stock_save_user_quotes($symbols);        
+        drupal_set_message(t('Your portfolio has been saved.'));
       }
-      // If the user pressed the Save button, and is logged in, save their preferences
-      elseif (isset($_POST['button_save']) && $user->uid ) {
-        // Save the user symbols 
-        $symbols = $edit['symbol'];
-        if ($symbols) {
-          stock_save_user_quotes($symbols);
+      elseif ($op == t('Quote') && $user->uid) {
+        // If the user pressed the Quote button, and is logged in, show his saved portfolio also, if it exists.
+        $saved_symbols = stock_get_user_quotes();               
+      }
+      else{
+        if (empty($symbols)) {
+          // If nothing is entered, then get those saved, if any
+          $symbols = stock_get_user_quotes();        
         }
       }
-
-      $output = variable_get("stock_description", "This is the default stock quote page.");
-
-      if ($symbols) {
-        $output .= stock_do_quote('long', $symbols);
+      
+      if (!empty($symbols)) {      
+      	$output .= stock_do_quote('long', $symbols);
       }
-     
-      $output .= stock_form($symbols);
-     
+      
+      if(!empty($saved_symbols)){
+        $output .= '<p>'. l(t('Your registered portfolio is'), 'stock') .': '. $saved_symbols .'</p>';    
+      } 
+      
+      $output .= stock_form($symbols);          
       break;
-  }
-     
- return $output;
+  }     
+  
+  return $output;
 }
 
 function stock_do_quote($format = 'long', $symbol_list) {
@@ -206,20 +245,29 @@
   $headers = stock_get_headers($format);
 
   // Convert the space separated list of symbols into an array
-  $symbol_list = explode(' ',trim($symbol_list));
+  $symbol_list = explode(' ', trim($symbol_list));
 
   foreach ($symbol_list as $symbol) {
-    if ($symbol) {
-      $rows[] = array('data'=> stock_process_data(stockapi_load($symbol), $headers));
+    if (!empty($symbol)) {
+      $stock = stockapi_load($symbol);
+      if ($stock[8] != 'N/A') {
+        // Date is 'N/A' means an invalid stock symbol  
+        $rows[] = array('data'=> stock_process_data($stock, $headers));
+      }else{
+        drupal_set_message(t('Invalid symbol') .': '. $symbol);       
+      }
     }
   }
-  return theme('table', $headers, $rows);
+  
+  if (!empty($rows)) {
+    return theme('table', $headers, $rows);
+  }
 }
 
 function theme_stock_page($output) {
-  $css_path = drupal_get_path('module','stock').'/stock.css";';
-  drupal_set_html_head("\n" . '<style type="text/css">@import "'.$css_path.'</style>');
-  return theme('page', $output);
+  $css_path = drupal_get_path('module', 'stock') .'/stock.css";';
+  drupal_set_html_head('<style type="text/css">@import "/'. $css_path .'</style>');   
+  return $output;
 }
 
 function stock_get_headers($format = 'long') {
@@ -236,27 +284,28 @@
     'volume'        => "Volume",
     'last_date'     => "Date",
     'last_time'     => "Time"
-    );
+  );
 
   $stock_short_array = array (
     'symbol'        => "Symbol",
     'current_price' => "Last",
     'change_amt'    => "Change"
-    );
+  );
 
   $headers = $stock_long_array;
   if ($format == 'short') {
     $headers = $stock_short_array;
   }
+  
   return $headers;
 }
 
 function stock_process_data($data, $headers) {
-  $columns = array();
-
+  $columns = array(); 
  
   foreach($headers as $key => $value) {
-    $field = $data[$key];
+    $field = $data[$key];        
+    
     // Special handling for some fields
     switch($key) {
       case 'symbol':
@@ -289,8 +338,8 @@
 
   // Then we create a URL for the full quote page
   $url = 'http://finance.yahoo.com/q?s=' . $symbol;
-
-  return '<a href="' . $url . '">' . $symbol . '</a>';
+  
+  return l($symbol, $url, array('target' => '_blank'), NULL, NULL, TRUE);
 }
 
 function _right_align($field) {
@@ -307,7 +356,7 @@
   $style='plus';
   if ($field < 0) {
     $style='minus';
-  }
+  }  
   return '<div class="' . $style .'">'. $field .'</div>';
 }
 
