Location: PHPKode > scripts > array2combo > array2combo/array2combo.class.php
<?
/*

Last modified: 24/02/2003
PHP class, creates HTML comboboxes from monodimensional arrays
Copyright (C) 2003 Gerosa Stefano (gerry_at_gerry_dot_it)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

GNU home page: http://www.gnu.org
To contact author, please use this form:
http://www.gerry.it/serve/mail/

*/

class array2combo{

/* #######################################################
   GENERATE A COMBO/LIST BOX FROM AN ARRAY
   ####################################################### */
   
   
   function generate_combo($name,$data=false,$size=1,$type=0,$sel=false,$attr=''){
/* #######################################################
   CLASS CONSTRUCTOR
   initializes the class assigning some internal variable
   -- PARAMS
   $name    -> (string) name of SELECT object
   $data    -> (Array) Array to be processed (can be added later)
   $size    -> (int) Select type (1 -> combobox, n ->listbox with n elements showed)
   $attr    -> (string) Attributes in Select tag (such as class, javascripts events and all
               others that may occour except name & size)
   $sel     -> (any) if not false, checks the value of the option to determine
               if it should be selected
               the option will be selcted if $sel==option value checking method
               may be overridden to provide custom selection
   $type    -> (int) array interpretation, there can be 3 array interpretation
               $type== 0 -> only-values array, the values of array are assigned
                            both to value and label of OPTION tag
               $type > 0 -> key+values array, keys are assigned to OPTION value
                            and corresponding values are assigned to OPTION label
               $type < 0 -> key+values swapped, sames as above but key and values
                            are swapped
   ####################################################### */
   
   //calling set_data method if data has been submitted
   if(is_array($data)){
       $this->set_data($name,$data,$size,$type,$sel,$attr);
       }
   
   }//END generate_combo
   
   function set_data($name,$data,$size=1,$type=0,$sel=false,$attr=''){
/* #######################################################
   SETS OR RESETS INTERNAL DATA STRUCTURES
   -- PARAMS
   same as class constructor but data is compulsory
   ####################################################### */
   
   //setting internal variables
   //SELECT name
   $this->name=$name;
   //data
   $this->data=$data;
   //size
   $this->size=abs((int)$size);
   //type
   $this->type=$type;
   //selection
   $this->sel=$sel;
   //attributes
   $this->attr=$attr;
   
   //reset internal cache
   $this->cached='';
   
   //reset lasterr value
   $this->lasterr='';
   

   }//END set_data
   
   function check_selected($value,$label){
/* #######################################################
   CHECK IF AN OPTION SHOULD BE SELECTED
   -- PARAMS
   $value      -> string assigned to value attribute of OPTION
   $label      -> OPTION label
   this method may be overridden if custom selecting is needed
   selection method is based upon OPTION value and label.
   Method must return the 'selected' string if item should be selected and empty
   string in the other case
   this string will be directly placed in OPTION tag attributes, thus this method
   can also be overriden to provide custom class or other attributes for OPTION
   Custom method must always accept two params
   ####################################################### */
   
   //check first if selection is required
   if(!$this->sel){
       //if no selection
       //return empty string
       return('');
       }
       
   //if there is a selection, returns selected if it equals $value
   if($this->sel==$value){
       //return selected
       return('selected');
       }
       
   //returns the empty string
   return('');
   }//END check_selected
   
   function assembly_option($value,$label){
/* #######################################################
   GENERATE AN OPTION TAG AND RETURN IT
   -- PARAMS
   $value      -> (string) sets the value param of OPTION
   $label      -> (string) sets the OPTION label
   ####################################################### */
   //returns teh complete tag
   return('<OPTION value="'.
          //setting value
          htmlspecialchars($value).'" '.
          //select check
          $this->check_selected($value,$label).
          //setting label
          '>'.htmlentities($label).'</OPTION>'
          );//return end
          
   }//END assembly_option
   
   function assembly_element(){
/* #######################################################
   GENERATES THE ELEMENT, RETURNS AND CACHE IT
   ####################################################### */

   //first, it checks if submitted data is an array
   if(!is_array($this->data)){
       //if data is invalid error is logged
       $this->lasterr='Data submitted is not an array';
       //and empty string is returned
       return('');
       }
       
   //composing SELECT start tag
   $element =
            //tag name
            '<SELECT name="'.htmlspecialchars($this->name).
            //tag size
            '" size="'.$this->size.
            //attributes
            '" '.$this->attr.
            //closign tag
            ' >';
            
   //assembling options
   foreach($this->data as $key => $value){
       //choosing array interpretation
       if($this->type==0){
           //array value both name+label
           $element.=$this->assembly_option($value,$value);
           }
       elseif($this->type>0){
           //array keys to values, array values to labels
           $element.=$this->assembly_option($key,$value);
           }
       else{
           //array values to values, array keys to labels
           $element.=$this->assembly_option($value,$key);
           }
       }//END foreach
       
   //SELECT close tag
   $element.='</SELECT>';
   
   //result caching
   $this->cached=$element;
   //result return
   return($element);
   }//END assembly_element


   function output(){
/* #######################################################
   RETURNS THE ASSEMBLED ELEMENT
   ####################################################### */
   
   //first checks if element is already in cache, if it is cache is returned
   if($this->cached!=''){
       return($this->cached);
       }
       
   //if not assembler method is called, and result is returned
   return($this->assembly_element());
   
   }//END output
   



}//END class generate_combo


class array2combo_predef extends array2combo{

/* #######################################################
   ADD SOME PREDEFINED DATA TYPES THAT CAN BE USEFUL
   these methods sets data to some predefined that can be useful,
   such as days, months or years
   name, size and attr fo SELECT should be already setted by class constructor
   ####################################################### */
   
   function generate_combo_predef($name,$data=false,$size=1,$type=0,$sel=false,$attr=''){
/* #######################################################
   CLASS CONSTRUCTOR
   same as parent
   ####################################################### */

   //calling set_data method if data has been submitted
   if(is_array($data)){
       $this->set_data($name,$data,$size,$type,$sel,$attr);
       }

   }//END generate_combo
   
   
   function chose_sel($sel,$current){
/* #######################################################
   STANDARD ELEMENT SELECTION
   if $sel = 0      -> $sel stay 0
      $sel > 0      -> $sel become $current
      $sel < 0      -> $sel become abs($sel)
   ####################################################### */

   //if $sel is indicated, will select current day
   if($sel>0){
       //sel >0, current
       $sel=$current;
       }
   elseif($sel<0){
       //sel < 0, sel=abs($sel);
       $sel=abs($sel);
       }
   else{
       $sel=0;
       }
   
   return($sel);
   }
   

   function predef_day($sel=0,$limit=31){
/* #######################################################
   SETS DATA MONTH DAYS, 01 TO 31
   --PARAMS
   $sel    -> if 0   selects none
              if > 0 selects current day
              if < 0 selects abs($sel)
   $limit  -> set day limit
   ####################################################### */
   
   //converting limit to int
   $limit=(int)$limit;
   
   //check limit to be more than 0
   if($limit<1){$limit=1;}
   
   //crating array
   for($i=1;$i<=$limit;$i++){
       $days[$i]=sprintf('%02d',$i);
       }

   //setting the data
   $this->set_data(
                   $this->name,
                   $days,
                   $this->size,
                   0,
                   $this->chose_sel($sel,(int)date('d',time())),
                   $this->attr
                   );
   //outputting the result
   return($this->output());
   }
   
   function predef_mon_num($sel=0){
/* #######################################################
   SETS DATA TO MONTH 01 TO 12
   --PARAMS
   $sel    -> if 0   selects none
              if > 0 selects current day
              if < 0 selects abs($sel)
   ####################################################### */

   //crating array
   for($i=1;$i<=12;$i++){
       $mon[$i]=sprintf('%02d',$i);
       }

   //if $sel is indicated, will select current mon
   if($sel>0){
       //sel >0, current day
       $sel=(int)date('d',time());
       }
   elseif($sel<0){
       //sel < 0, sel=abs($sel);
       $sel=abs($sel);
       }
   else{
       $sel=0;
       }

   //setting the data
   $this->set_data(
                   $this->name,
                   $mon,
                   $this->size,
                   0,
                   $this->chose_sel($sel,(int)date('m',time())),
                   $this->attr
                   );
                   
   //outputting the result
   return($this->output());
   }
   
   
   function predef_mon_ita($sel=0){
/* #######################################################
   SETS DATA TO MONTH WITH ITALIAN LABELS
   --PARAMS
   $sel    -> if 0   selects none
              if > 0 selects current day
              if < 0 selects abs($sel)
   ####################################################### */

   //crEating array
   $mon=array(
              1=>'Gennaio',
              2=>'Febbraio',
              3=>'Marzo',
              4=>'Aprile',
              5=>'Maggio',
              6=>'Giugno',
              7=>'Luglio',
              8=>'Agosto',
              9=>'Settembre',
              10=>'Ottobre',
              11=>'Novembre',
              12=>'Dicembre',
              ) ;

   //if $sel is indicated, will select current mon
   if($sel>0){
       //sel >0, current day
       $sel=(int)date('d',time());
       }
   elseif($sel<0){
       //sel < 0, sel=abs($sel);
       $sel=abs($sel);
       }
   else{
       $sel=0;
       }

   //setting the data
   $this->set_data(
                   $this->name,
                   $mon,
                   $this->size,
                   1,
                   $this->chose_sel($sel,(int)date('m',time())),
                   $this->attr
                   );

   //outputting the result
   return($this->output());
   }//END predef_mon_ita
   
   function predef_mon_en($sel=0){
/* #######################################################
   SETS DATA TO MONTH WITH ITALIAN LABELS
   --PARAMS
   $sel    -> if 0   selects none
              if > 0 selects current day
              if < 0 selects abs($sel)
   ####################################################### */

   //crEating array
   $mon=array(
              1=>'January',
              2=>'February',
              3=>'March',
              4=>'April',
              5=>'May',
              6=>'June',
              7=>'July',
              8=>'August',
              9=>'September',
              10=>'October',
              11=>'November',
              12=>'December',
              ) ;

   //if $sel is indicated, will select current mon
   if($sel>0){
       //sel >0, current day
       $sel=(int)date('d',time());
       }
   elseif($sel<0){
       //sel < 0, sel=abs($sel);
       $sel=abs($sel);
       }
   else{
       $sel=0;
       }

   //setting the data
   $this->set_data(
                   $this->name,
                   $mon,
                   $this->size,
                   1,
                   $this->chose_sel($sel,(int)date('m',time())),
                   $this->attr
                   );

   //outputting the result
   return($this->output());
   }//END predef_mon_ita
   

   function predef_year($from,$to,$sel=0){
/* #######################################################
   SETS DATA TO A RAGE OF YEARS
   --PARAMS
   $from   -> from year
   $to     -> to year
   $sel    -> if 0   selects none
              if > 0 selects current year
              if < 0 selects abs($sel)
   ####################################################### */
   
   //check range limits are both unsigned integers
   $from=abs((int)$from);
   $to=abs((int)$to);
   
   //avoid endless loop
   if($to < $from){
       return('');
       }
       
   //crEating array
   for($i=$from;$i<=$to;$i++){
       $year[$i]=$i;
       }

   //setting the data
   $this->set_data(
                   $this->name,
                   $year,
                   $this->size,
                   0,
                   $this->chose_sel($sel,(int)date('Y',time())),
                   $this->attr
                   );

   //outputting the result
   return($this->output());
   }//END predef_mon_ita
   

}
?>
Return current item: array2combo