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

Source for file DBKeySelect.class.php

Documentation is available at DBKeySelect.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/HTMLSelect.class.php" );
  22.  
  23. /**
  24.  * @package    forms
  25.  * @subpackage items
  26.  * @version    0.1.62
  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) 2005 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. class DBKeySelect extends HTMLSelect
  48. {
  49.    /**
  50.     * @var    object  $dbt 
  51.     * @access private
  52.     */
  53.    var $dbt;
  54.    /**
  55.     * @var    string  $key_field 
  56.     * @access private
  57.     */
  58.    var $key_field;
  59.    /**
  60.     * @var    array   $label_fields 
  61.     * @access private
  62.     */
  63.    var $label_fields;
  64.    /**
  65.     * @var    object  $where 
  66.     * @access private
  67.     */
  68.    var $where;
  69.  
  70.  
  71.  
  72.    /**
  73.     * Constructor
  74.     *
  75.     *
  76.     * @version 1.81
  77.     * @since   0.1.0
  78.     * @access  public
  79.     *
  80.     * @param   string       $name            The name-attribute this select
  81.     *                                         should get.
  82.     * @param   string       $selectedValue   The preselected value this select
  83.     *                                         should have.
  84.     * @param   object|array$dbt             Naturally you have to give the
  85.     *                                         reference of an object of a
  86.     *                                         database table or database view,
  87.     *                                         how they can be found in package
  88.     *                                         databases, as parameter $dbt.
  89.     *                                         You can also give the parameter dbt
  90.     *                                         by array!
  91.     *                                         If you use $dbt by array-syntax,
  92.     *                                         you have to put the handle of the
  93.     *                                         database connection at index zero,
  94.     *                                         the source databasetable at index
  95.     *                                         one and the database type
  96.     *                                         ( e. g. "mysql" ) at index two.
  97.     * @param   string|array$label_fields    You can give one fieldname of the
  98.     *                                         databasetable by string type or you
  99.     *                                         can give more fieldnames as an
  100.     *                                         array that contains them by string
  101.     *                                         type. This fieldnames builds the
  102.     *                                         labels of the option-tags they will
  103.     *                                         be separated by a space.
  104.     * @param   string       $key_field       This fieldname should be in best
  105.     *                                         case a primary key to identify the
  106.     *                                         meant tuple.
  107.     * @param   string       $where           The where-clause of the db-query.
  108.     * @param   string       $onChangeOrder   A javascript that should execute
  109.     *                                         after changing a value of this
  110.     *                                         select.
  111.     * @param   string       $preselect_field The dataset in which this field has
  112.     *                                         the value of the parameter
  113.     *                                         $preselected_value will be
  114.     *                                         preselect if the parameter
  115.     *                                         selectedValue is empty.
  116.     * @param   string     $preselected_value The value that the field, given
  117.     *                                         with the parameter
  118.     *                                         $preselect_field, has to have to be
  119.     *                                         preselect. The default value is 1.
  120.     *
  121.     * @return  void 
  122.     */
  123.    function DBKeySelect(
  124.                          $name,
  125.                          $selectedValue,
  126.                          &$dbt,
  127.                          $label_fields,
  128.                          $key_field,
  129.                          $where         "",
  130.                          $onChangeOrder "",
  131.                          $preselect_field "",
  132.                          $preselected_value 1
  133.                        )
  134.    {
  135.      $this->key_field = $key_field;
  136.      $this->where     = preg_replace"!^ *WHERE *!i"""$where );
  137.      $this->dbt       = &$dbt;
  138.  
  139.      if is_array$label_fields ) )
  140.      {  $this->label_fields = $label_fields}
  141.      else
  142.      {  $this->label_fields = array$label_fields )}
  143.  
  144.  
  145.      $error true;
  146.      if (
  147.           is_a$dbt"ABSTDatabaseTable" )
  148.        || is_subclass_of$dbt"ABSTDatabaseTable" )
  149.        || is_a$dbt"ABSTView" )
  150.        || is_subclass_of$dbt"ABSTView" )
  151.         )
  152.      {
  153.        $tmp_arr array();
  154.        if (
  155.             !empty$preselect_field )
  156.          && empty$selectedValue )
  157.           )
  158.        {
  159.           $tmp_arr array_merge(
  160.                              array$this->key_field ),
  161.                              $this->label_fields,
  162.                              array$preselect_field )
  163.                                 );
  164.        }
  165.        else
  166.        {
  167.           $tmp_arr array_merge(
  168.                              array$this->key_field ),
  169.                              $this->label_fields
  170.                                 );
  171.        }
  172.  
  173.  
  174.        $res_arr $dbt->getDatasetsWithFields(
  175.                                                $tmp_arr,
  176.                                                $this->where,
  177.                                                false,
  178.                                                MYSQL_OUTPUT_NUM
  179.                                              );
  180.        for $i 0$i count$res_arr )$i++ )
  181.        {
  182.          $tmp "";
  183.          if is_array$res_arr[$i) )
  184.          foreach $res_arr[$ias $str_fieldidx => $str_fieldval )
  185.               if (
  186.                    $str_fieldidx != 0
  187.                 && $str_fieldidx <= count$this->label_fields )
  188.                  )
  189.                  $tmp .= empty$tmp "" " " ).$str_fieldval;
  190.  
  191.          $labelArr[$tmp;
  192.          $valueArr[$res_arr[$i][0];
  193.  
  194.          $tmp_matches_arr array();
  195.          preg_match(
  196.                      "/^([<=!>]+) *+(.+)/is",
  197.                      $preselected_value,
  198.                      $tmp_matches_arr
  199.                    );
  200.          $str_operator $tmp_matches_arr[1];
  201.          $str_value $tmp_matches_arr[2];
  202.          if (
  203.               !empty$preselect_field )
  204.            && !in_array$selectedValue$valueArr )
  205.            && (
  206.                 $res_arr[$i]count$label_fields == $preselected_value
  207.              || preg_match"/^(?:<=|<|>|>=|==|===|!=|!==)$/"$str_operator )
  208.              && eval(
  209.                       $res_arr[$i]count$label_fields ]." "
  210.                      .$str_operator." ".$preselected_value
  211.                     )
  212.               )
  213.             )
  214.           {
  215.             $_GET[$name=
  216.             $_POST[$name=
  217.             $_REQUEST[$name=
  218.             $selectedValue $res_arr[$i][0];
  219.           }
  220.        }
  221.        $error false;
  222.      }
  223.      else
  224.      // Use this item in conventional php kind.
  225.      ifis_array$dbt ) )
  226.      {
  227.        $tmp_arr array_merge(
  228.                                array$this->key_field ),
  229.                                $this->label_fields
  230.                              );
  231.        switchstrtolower$dbt[2) )
  232.        {
  233.          case "mysql":
  234.               $sql "SELECT ".implode", "$tmp_arr )." "
  235.                       ."FROM ".$dbt[1]." "
  236.                       .(
  237.                          !empty$this->where )
  238.                          ? "WHERE ".$this->where
  239.                          : ""
  240.                        );
  241.                $res mysql_query$sql$dbt[0);
  242.                while$res_arr mysql_fetch_assoc$res ) )
  243.                {
  244.                  $tmp "";
  245.                  for$k 0$k count$this->label_fields )$k++ )
  246.                    $tmp .= $k == "" " " )
  247.                           .$res_arr[$this->label_fields[$k]];
  248.  
  249.                  $labelArr[$tmp;
  250.                  $valueArr[$res_arr[$this->key_field];
  251.                }
  252.                break;
  253.          default:
  254.                break;
  255.        }
  256.        $error false;
  257.      }
  258.  
  259.  
  260.      if(
  261.          $error
  262.       && (
  263.            defined"DEBUG" )
  264.         || SCRIPTDEBUGGING
  265.          )
  266.        )
  267.      {
  268.        echo "<pre>\r\n";
  269.        echo "<b>The given parameter is not a valid reference to a "
  270.               ."databasetable!</b>\r\n";
  271.        echo "<b>BACKTRACE DEBUG:</b>\r\n";
  272.        $debugArr debug_backtrace();
  273.        print_r$debugArr[count($debugArr)-1);
  274.        echo "</pre>\r\n";
  275.      }
  276.  
  277.      parent::HTMLSelect(
  278.                          $name,
  279.                          $labelArr,
  280.                          $valueArr,
  281.                          $selectedValue,
  282.                          $onChangeOrder
  283.                        );
  284.    }
  285.  
  286.  
  287.  
  288.   /**
  289.    * Returns the last stored database query.
  290.    *
  291.    * @version 1.0
  292.    * @since   0.1.5
  293.    * @author  Daniel Plücken <daniel@debakel.net>
  294.    * @access  public
  295.    * @return  array 
  296.    */
  297.    function getLastQuery()
  298.    return $this->dbt->getLastQuery()}
  299. // end of class DBKeySelect
  300. ?>

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