HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]

Source for file common_class.php

Documentation is available at common_class.php

  1. <?php
  2. /**
  3.  * The common class, used in various files.
  4.  * This file contains the common class, a container for commonly used functions.
  5.  * @package HRDIODB
  6.  */
  7.  
  8. /**
  9.  * This file is part of HRDIODB.
  10.  *
  11.  * HRDIODB is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * HRDIODB is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with HRDIODB; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24.  */
  25.  
  26. /**
  27.  * Common class.
  28.  * A common class containing functions used in various files.
  29.  * @package HRDIODB
  30.  */
  31. class common {
  32.   /**
  33.    * Returns value for key in first or second array.
  34.    * This function returns the value for a key from the first or from the
  35.    * second array. The first array will be looked at first, the second array
  36.    * will be looked at second. If the key exists for the array, its value will
  37.    * be returned. The value will be stripped of slashes, trimmed and any HTML
  38.    * special characters will be encoded. The second array is optional. If
  39.    * neither array contains the key, an empty string will be returned. This
  40.    * function works really well for getting the default value of an input
  41.    * field from either the $_POST array or the stored data.
  42.    * @param string $key The key to look up
  43.    * @param array $array1 The first array
  44.    * @param array $array2 The optional second array
  45.    * @return string The value for the key or an empty string
  46.    */
  47.   function get_value($key$array1$array2 NULL{
  48.     if (is_array($array1&& isset($array1[$key])) {
  49.       return htmlspecialchars(trim(stripslashes($array1[$key]))ENT_QUOTES);
  50.     else if (is_array($array2&& isset($array2[$key])) {
  51.       return htmlspecialchars(trim(stripslashes($array2[$key]))ENT_QUOTES);
  52.     else {
  53.       return "";
  54.     }
  55.   }
  56.  
  57.   /**
  58.    * Prints out table cell for specific coverage.
  59.    * The function prints out a table cell for a specific coverage type. It is
  60.    * used to quickly create the composition table based on the standardized
  61.    * naming convention.
  62.    * @param day $day The reference to a uars_day object
  63.    * @param string $str A part of the field name: cov_{$str}_{$suffix}
  64.    */
  65.   function coverage(&$day$str{
  66.     $suffixes array("WDA""WDD""WNA""WND",
  67.                       "CDA""CDD""CNA""CND");
  68.  
  69.     foreach ($suffixes as $suffix{
  70.       print "<td class=\"data\" align=right>".@$day->data["cov_{$str}_{$suffix}"]."</td>\n";
  71.     }
  72.   }
  73.  
  74.   /**
  75.    * Prints a definition link.
  76.    * The function prints out a link which calls the JavaScript pop() routine.
  77.    * It is used to pop a definition for a data label, which is stored in a
  78.    * static JavaScript associative array.
  79.    * @param string $string The link string
  80.    * @param string $key The key into the JavaScript array of definitions
  81.    */
  82.   function deflink($string$key{
  83.     print "<a href=\"javascript:void(0);\" class=\"def\" 
  84.            onClick=\"return pop(thisevent, '{$key}');\">$string</a>";
  85.   }
  86.  
  87.   /**
  88.    * Returns the "human readable" form of a file size.
  89.    * This function returns the "human readable" form of the size of a file. It
  90.    * is based on a function posted in the comments of the
  91.    * {@link PHP_MANUAL#filesize} function.
  92.    * @param integer $size File size in bytes
  93.    * @param integer $decimals Number of decimal places to use for rounding
  94.    * @return string Size in "human readable" form
  95.    */
  96.   function human_size($size$decimals 1{
  97.     $suffix array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB','NB','DB');
  98.     $i 0;
  99.  
  100.     while ($size >= 1024 && ($i count($suffix1)){
  101.       $size /= 1024;
  102.       $i++;
  103.     }
  104.  
  105.     return round($size$decimals).' '.$suffix[$i];
  106.   }
  107.  
  108.   /**
  109.    * Converts Day of Year to UARS day.
  110.    * This function takes the year and day of year in the format 'YYYY-DDD'
  111.    * and converts them to the UARS day.
  112.    * @param string $doy The year and day of year in the 'YYYY-DDD' format
  113.    * @return integer The UARS day
  114.    */
  115.   function doy_to_uars_day($doy){
  116.     global $day1_ts;
  117.  
  118.     $year_day split("-",$doy);
  119.  
  120.     echo "asdf";
  121.  
  122.     if (count($year_day!=|| !is_numeric($year_day[0]|| !is_numeric($year_day[1]) ) {
  123.       return error ("Year - Day of Year requires the following format: YYYY-DDD");
  124.     }
  125.  
  126.     $year $year_day[0];
  127.     $day $year_day[1];
  128.     $stamp mktime(0,0,0,1,$day,$year);
  129.  
  130.     return (($stamp $day1_ts24 60 60)  1
  131.   }
  132.  
  133.   function doy_to_timestamp($doy){
  134.     global $day1_ts;
  135.  
  136.     $year_day split("-",$doy);
  137.  
  138.     if (count($year_day!=2{
  139.       return error ("Year - Day of Year requires the following format: YYYY-DDD");
  140.     }
  141.  
  142.     $year $year_day[0];
  143.     $day $year_day[1];
  144.     $stamp mktime(0,0,0,1,$day,$year);
  145.     return $stamp;
  146.   }
  147.  
  148.   function timestamp_to_datetime($timestamp){
  149.     return date("Y-m-d H:i:s",$timestamp);
  150.   }
  151.  
  152.   function uars_day_to_timestamp($uars_day){
  153.       global $day1_ts;
  154.  
  155.       return $day1_ts (($uars_day-124 60 60);
  156.  
  157.   }
  158.  
  159.   /**
  160.    * Converts a calendar date string to UARS day.
  161.    * This function takes a calendar date string and converts it to the UARS
  162.    * day. The date string has to be compatible with the PHP
  163.    * {@link PHP_MANUAL#strtotime} function.
  164.    * @param string $calendar The calendar date string
  165.    * @return integer The UARS day
  166.    */
  167.   function calendar_to_uars_day ($calendar{
  168.     global $day1_ts;
  169.  
  170.     if (strlen($calendar)) {
  171.       $stamp strtotime($calendar);
  172.  
  173.       if (!$stamp{
  174.         return error ("Your date format could not be understood$calendar");
  175.       }
  176.       
  177.       return floor((($stamp $day1_ts24 60 60)  1)
  178.     }
  179.   }
  180.  
  181.   /**
  182.    * Print select dropdown options.
  183.    * This is a helper function to print out HTML option tags for a select
  184.    * dropdown.
  185.    * @param array Array of values and descriptions
  186.    * @param mixed The selected value
  187.    */
  188.   function print_select_options (&$values$selected{
  189.     foreach($values as $value => $description{
  190.       print "<option value=\"$value\" ";
  191.       if($value == $selected){
  192.         print " selected ";
  193.       }
  194.       print ">$description</option>";
  195.     }
  196.   }
  197.  
  198.   /**
  199.    * Returns the run mode of the script.
  200.    * This function detects and returns the run mode of the script. If the
  201.    * the script is being executed at the command line (offline), it returns
  202.    * TRUE. If the script is being accessed via HTTP (online), it returns
  203.    * FALSE.
  204.    * @return TRUE|FALSETRUE if offline, FALSE if online.
  205.    */
  206.   function offline({
  207.     return !isset($_SERVER['HTTP_HOST']);
  208.   }
  209.  
  210.   /**
  211.    * Performs an HTTP header redirect.
  212.    * This function performs an HTTP header redirect, taking the user to the
  213.    * specified page. The exit() is needed in order to make sure that the code
  214.    * below the redirect isn't executed, see {@link PHP_MANUAL#header}.
  215.    * @param string $page The page to redirect to
  216.    */
  217.   function redirect($page{
  218.     header("Location$page");
  219.     exit(1);
  220.   }
  221. }
  222. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:43 -0500 by phpDocumentor 1.3.0RC6