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

Source for file DBKeyRadioGroup.class.php

Documentation is available at DBKeyRadioGroup.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package    forms
  9.  * @subpackage items
  10.  */
  11. if!defined"CLASSPATH" ) )
  12. {
  13.   echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
  14.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  15.   exit();
  16. }
  17.  
  18. /**
  19.  *
  20.  */
  21. require_onceCLASSPATH."forms/items/HTMLRadioGroup.class.php" );
  22.  
  23. /**
  24.  * @package    forms
  25.  * @subpackage items
  26.  * @version    0.1.0
  27.  * @author     Daniel Plücken <daniel@debakel.net>
  28.  * @license    http://www.gnu.org/copyleft/lesser.html
  29.  *              GNU Lesser General Public License
  30.  * @copyright  Copyright (C) 2006 Daniel Plücken <daniel@debakel.net>
  31.  *
  32.  *  This library is free software; you can redistribute it and/or
  33.  *  modify it under the terms of the GNU Lesser General Public
  34.  *  License as published by the Free Software Foundation; either
  35.  *  version 2.1 of the License.
  36.  *
  37.  *  This library is distributed in the hope that it will be useful,
  38.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  39.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40.  *  GNU Lesser General Public License for more details.
  41.  *
  42.  *  You should have received a copy of the GNU Lesser General
  43.  *  Public License along with this library; if not, write to the
  44.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  45.  *  Boston, MA 02111-1307 USA
  46.  */
  47. {
  48.    /**
  49.     * @var    object  $dbt 
  50.     * @access private
  51.     */
  52.    var $dbt;
  53.    /**
  54.     * @var    string  $key_field 
  55.     * @access private
  56.     */
  57.    var $key_field;
  58.    /**
  59.     * @var    array   $label_fields 
  60.     * @access private
  61.     */
  62.    var $label_fields;
  63.    /**
  64.     * @var    object  $where 
  65.     * @access private
  66.     */
  67.    var $where;
  68.    
  69.    
  70.    
  71.    /**
  72.     * Constructor
  73.     * 
  74.     *
  75.     * @version 1.6
  76.     * @since   0.1.0
  77.     * @author  Daniel Plücken <daniel@debakel.net>
  78.     * @access  public
  79.     * @param   string       $name            The name-attribute this select
  80.     *                                         should get.
  81.     * @param   string       $selectedValue   The preselected value this select
  82.     *                                         should have.
  83.     * @param   object|array$dbt             Naturally you have to give the
  84.     *                                         reference of an object of a
  85.     *                                         database table, how they can be
  86.     *                                         found in package databases, as
  87.     *                                         parameter $dbt.
  88.     *                                         You can also give the parameter dbt
  89.     *                                         by array!
  90.     *                                         If you use $dbt by array-syntax,
  91.     *                                         you have to put the handle of the
  92.     *                                         database connection at index zero,
  93.     *                                         the source databasetable at index
  94.     *                                         one and the database type
  95.     *                                         ( e. g. "mysql" ) at index two.
  96.     * @param   string|array$label_fields    You can give one fieldname of the
  97.     *                                         databasetable by string type or you
  98.     *                                         can give more fieldnames as an
  99.     *                                         array that contains them by string
  100.     *                                         type. This fieldnames builds the
  101.     *                                         labels of the option-tags they will
  102.     *                                         be separated by a space.
  103.     * @param   string       $key_field       This fieldname should be in best
  104.     *                                         case a primary key to identify the
  105.     *                                         meant tuple.
  106.     * @param   string       $where           The where-clause of the db-query.
  107.     * @param   string       $onChangeOrder   A javascript that should execute
  108.     *                                         after changing a value of this
  109.     *                                         select.
  110.     * @param   string       $preselect_field The dataset in which this field is
  111.     *                                         greater than zero will be preselect
  112.     *                                         if the parameter $selectedValue is
  113.     *                                         left empty.
  114.     * @return  void 
  115.     */
  116.    function DBKeyRadioGroup(
  117.                          $name,
  118.                          $selectedValue,
  119.                          &$dbt,
  120.                          $label_fields,
  121.                          $key_field,
  122.                          $where           "",
  123.                          $onChangeOrder   "",
  124.                          $preselect_field ""
  125.                            )
  126.    {
  127.      $this->key_field = $key_field;
  128.      $this->where     = preg_replace"!^ *WHERE *!i"""$where );
  129.      $this->dbt       = &$dbt;
  130.      
  131.      ifis_array$label_fields ) )
  132.        $this->label_fields = $label_fields;
  133.      else
  134.        $this->label_fields = array$label_fields );
  135.      
  136.      
  137.      $error true;
  138.      if(
  139.         is_a$dbt"ABSTDatabaseTable" )
  140.      || is_subclass_of$dbt"ABSTDatabaseTable" )
  141.      || is_a$dbt"ABSTView" )
  142.      || is_subclass_of$dbt"ABSTView" )
  143.       )
  144.      {
  145.        if (
  146.             !empty$preselect_field )
  147.          && empty$selectedValue )
  148.           )
  149.           $tmp_arr array_merge(
  150.                              array$this->key_field ),
  151.                              $this->label_fields,
  152.                              $preselect_field
  153.                                 );
  154.        else
  155.           $tmp_arr array_merge(
  156.                              array$this->key_field ),
  157.                              $this->label_fields
  158.                                 );
  159.           
  160.        
  161.        $resArr $this->dbt->getDatasetsWithFields$tmp_arr$this->where );
  162.        
  163.        for $i 0$i count$resArr )$i++ )
  164.        {
  165.          $tmp "";
  166.          for $k 0$k count$this->label_fields )$k++ )
  167.              if (
  168.                   preg_match(
  169.                        "!^[-_a-z0-9]*\.[-_a-z0-9]+$!",
  170.                        $this->label_fields[$k]
  171.                             )
  172.                && $tmp_pos strpos$this->label_fields[$k]"." )
  173.                 )
  174.                 $tmp .= $k == "" " " )
  175.                        .$resArr[$i]
  176.                                [substr$this->label_fields[$k]$tmp_pos+)];
  177.              else
  178.                 $tmp .= $k == "" " " )
  179.                        .$resArr[$i][$this->label_fields[$k]];
  180.          
  181.          $labelArr[$tmp;
  182.          
  183.          $key_field preg_replace(
  184.                           "!^[-_a-z0-9]*\.([-_a-z0-9]+)$!",
  185.                           "$1"$key_field
  186.                                   );
  187.          $valueArr[$resArr[$i][$key_field];
  188.          
  189.          if (
  190.               !empty$preselect_field )
  191.            && !in_array$selectedValue$valueArr )
  192.            && $resArr[$i][$preselect_field0
  193.             )
  194.           {
  195.             $_GET[$name=
  196.             $_POST[$name=
  197.             $_REQUEST[$name=
  198.             $selectedValue $resArr[$i][$key_field];
  199.           }
  200.        }
  201.        $error false;
  202.      }
  203.      else
  204.      // Use this item in conventional php kind.
  205.      ifis_array$dbt ) )
  206.      {
  207.        $tmp_arr array_merge(
  208.                                array$this->key_field ),
  209.                                $this->label_fields
  210.                              );
  211.        switchstrtolower$dbt[2) )
  212.        {
  213.          case "mysql":
  214.               $sql "SELECT ".implode", "$tmp_arr )." "
  215.                       ."FROM ".$dbt[1]." "
  216.                       .(
  217.                          !empty$this->where )
  218.                          ? "WHERE ".$this->where
  219.                          : ""
  220.                        );
  221.                $res mysql_query$sql$dbt[0);
  222.                while$res_arr mysql_fetch_assoc$res ) )
  223.                {
  224.                  $tmp "";
  225.                  for$k 0$k count$this->label_fields )$k++ )
  226.                    $tmp .= $k == "" " " )
  227.                           .$res_arr[$this->label_fields[$k]];
  228.                  
  229.                  $labelArr[$tmp;
  230.                  $valueArr[$res_arr[$this->key_field];
  231.                }
  232.                break;
  233.          default:
  234.                break;
  235.        }
  236.        $error false;
  237.      }
  238.      
  239.      
  240.      if(
  241.          $error 
  242.       && (
  243.            defined"DEBUG" )
  244.         || SCRIPTDEBUGGING
  245.          )
  246.        )
  247.      {
  248.        echo "<pre>\r\n";
  249.        echo "<b>The given third parameter is not a valid reference to a "
  250.               ."databasetable!</b>\r\n";
  251.        echo "<b>BACKTRACE DEBUG:</b>\r\n";
  252.        $debugArr debug_backtrace();
  253.        print_r$debugArr[count($debugArr)-1);
  254.        echo "</pre>\r\n";
  255.      }
  256.      
  257.      parent::HTMLRadioGroup(
  258.                          $name,
  259.                          $labelArr,
  260.                          $valueArr,
  261.                          $selectedValue
  262.                            );
  263.    }
  264.   
  265.   
  266.   
  267.   /**
  268.    * Returns the last stored database query.
  269.    *
  270.    * @version 1.0
  271.    * @since   0.1.5
  272.    * @author  Daniel Plücken <daniel@debakel.net>
  273.    * @access  public
  274.    * @return  array 
  275.    */
  276.    function getLastQuery()
  277.    return $this->dbt->getLastQuery()}
  278. // end of class DBKeyRadioGroup
  279. ?>

Documentation generated on Thu, 05 Jun 2008 19:11:00 +0200 by phpDocumentor 1.4.1