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

Source for file PHPJQueryToggler.php

Documentation is available at PHPJQueryToggler.php

  1. <?php
  2. /*
  3.  *     PHP JQuery Toggler - A class library to manage toggle in JQuery
  4.  *  Copyright (C) 2010  Daniele Monti (monska13 [at] gmail.com)
  5.  *
  6.  *  This program is free software: you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation, either version 3 of the License, or
  9.  *  (at your option) any later version.
  10.  * 
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  * 
  19.  * */
  20. ?>
  21.  
  22. <?php
  23.  
  24. /* ERROR DEFINE */
  25. define("NO_TXT_DEFINIED","Ricontrolla di aver configurato i testi di apertura e chiusura.");
  26. define("NO_IMG_DEFINIED","Ricontrolla di aver configurato le immagini di apertura e chiusura.");
  27. /* END ERROR DEFINE */
  28.  
  29. define("STATUS_CLOSE",0);
  30. define("STATUS_OPEN",1);
  31.  
  32. define("DEFAULT_DURATION","slow");
  33.  
  34. define("BUTTON_TYPE_TEXT","T");
  35. define("BUTTON_TYPE_IMAGE","I");
  36.  
  37. define("BUTTON_OPEN_DEFAULT_TXT","Apri");
  38. define("BUTTON_CLOSE_DEFAULT_TXT","Chiudi");
  39.  
  40. /*
  41.  * @inline Class to implements slideUp and slideDown div
  42.  * */
  43.  
  44. class phpToggler
  45. {    
  46.     /**
  47.      * @var string contains button's div id
  48.      */
  49.     public $div_button;
  50.     /**
  51.      * @var string contains content's div id
  52.      */
  53.     public $div_content;
  54.     /**
  55.      * @var integer set motion duration in milliseconds
  56.      */
  57.     public $motion_duration;
  58.     /**
  59.      * @var char set button type [I|T] Image|Text
  60.      */
  61.     public $button_type;
  62.     /**
  63.      * @var string contains image path of open button
  64.      */
  65.     public $open_image;
  66.     /**
  67.      * @var string contains image path of closed button
  68.      */
  69.     public $close_image;
  70.     /**
  71.      * @var string contains text of open string
  72.      */
  73.     public $open_txt;
  74.     /**
  75.      * @var string contains text of close string
  76.      */
  77.     public $close_txt;
  78.     /**
  79.      * @var integer set the init status [1|0] open|close
  80.      */
  81.     public $start_status;
  82.     
  83.     /**
  84.      * @var mixed array of functions to call at open action
  85.      */
  86.     public $openFunctions = NULL;
  87.     /**
  88.      * @var mixed array of functions to call at close action
  89.      */
  90.     public $closeFunctions = NULL;
  91.     
  92.     /**
  93.      * @method void phpToggler() phpToggler($button,$content,$button_type=BUTTON_TYPE_TEXT,$start_status=STATUS_CLOSE,$motion_duration=DEFAULT_DURATION) is class costructor
  94.      * @param string $button contains button's div id
  95.      * @param string $content contains content's div id
  96.      * @param char $button_type [I|T] set button type Image|Text
  97.      * @param integer $start_status [1|0] set init status Open|Close
  98.      * @param integer motion_duration set motion duration in milliseconds
  99.      */
  100.     
  101.     function phpToggler($button,$content,$button_type=BUTTON_TYPE_TEXT,$start_status=STATUS_CLOSE,$motion_duration=DEFAULT_DURATION)
  102.     {
  103.         $this->div_button        = $button;
  104.         $this->div_content        = $content;
  105.         
  106.         $this->button_type        = $button_type;
  107.         $this->motion_duration    = $motion_duration;
  108.         $this->start_status        = $start_status;
  109.         
  110.         switch($this->button_type)
  111.         {
  112.         }
  113.         
  114.     }
  115.     
  116.     /**
  117.      * @method void|stringincludeJQuery() includeJQuery($path,$return=false) print or return the include string of JQuery
  118.      * @param string $path contains JQuery path
  119.      * @param bool $return set print|return type
  120.      */
  121.     
  122.     function includeJQuery($path,$return=false)
  123.     {
  124.         if(!$return)
  125.             echo "<script src=\"".$path."\"></script>\n\n";
  126.         else
  127.             return "<script src=\"".$path."\"></script>\n\n";
  128.     }
  129.     
  130.     /**
  131.      * @method void setOpenCloseImg() setOpenCloseImg($open_image,$close_image) set images used as open|close button
  132.      * @param string $open_image contains open image path
  133.      * @param string $close_image contains close image path
  134.      */
  135.      
  136.     function setOpenCloseImg($open_image,$close_image)
  137.     {
  138.         $this->open_image = $open_image;
  139.         $this->close_image = $close_image;
  140.         $this->button_type = BUTTON_TYPE_IMAGE;
  141.         
  142.         $this->addOpenFunction("toggleImg(\"".$this->div_button."\",\"".$this->close_image."\")");
  143.         $this->addCloseFunction("toggleImg(\"".$this->div_button."\",\"".$this->open_image."\")");
  144.         
  145.     }
  146.     
  147.     /**
  148.      * @method void setOpenCloseTxt() setOpenCloseTxt($open_txt,$close_txt) set text used as open|close text
  149.      * @param string $open_txt contains open text
  150.      * @param string $close_txt contains close text
  151.      */
  152.     
  153.     function setOpenCloseTxt($open_txt,$close_txt)
  154.     {
  155.         $this->open_txt = $open_txt;
  156.         $this->close_txt = $close_txt;
  157.         
  158.         $this->addOpenFunction("toggleTxt(\"".$this->div_button."\",\"".$this->close_txt."\")");
  159.         $this->addCloseFunction("toggleTxt(\"".$this->div_button."\",\"".$this->open_txt."\")");
  160.     }
  161.     
  162.     /**
  163.      * @method void setOpenFunction() setOpenFunction($array) set the open funcions
  164.      * @param mixed $array array of javascript functions to call at open action
  165.      */
  166.     
  167.     function setOpenFunction($array)
  168.     {
  169.         $this->openFunctions = $array;
  170.     }
  171.     
  172.     /**
  173.      * @method void setCloseFunction() setCloseFunction($array) set the close funcions
  174.      * @param mixed $array array of javascript functions to call at close action
  175.      */
  176.     
  177.     function setCloseFunction($array)
  178.     {
  179.         $this->closeFunctions = $array;
  180.     }
  181.     
  182.     /**
  183.      * @method void addOpenFunction() addOpenFunction($function) add function at openFunction array
  184.      * @param string $function call string of javascript function at open action
  185.      */
  186.     
  187.     function addOpenFunction($function)
  188.     {
  189.         $index count($this->openFunctions);
  190.         if($this->openFunctions == NULL)
  191.         {
  192.             $this->openFunctions = array();
  193.             $index 0;
  194.         }
  195.             
  196.         $this->openFunctions[$index$function;
  197.     }
  198.     
  199.     /**
  200.      * @method void addCloseFunction() addCloseFunction($function) add function at closeFunction array
  201.      * @param string $function call string of javascript function at close action
  202.      */
  203.     
  204.     function addCloseFunction($function)
  205.     {
  206.         $index count($this->closeFunctions);
  207.         if($this->closeFunctions == NULL)
  208.         {
  209.             $this->closeFunctions = array();
  210.             $index 0;
  211.         }
  212.             
  213.         $this->closeFunctions[$index$function;
  214.     }
  215.     
  216.     /**
  217.      * @method void|stringprintJs() addOpenFunction($return=false) print or return javascript code
  218.      * @param bool $return set print or return mode
  219.      */
  220.     
  221.     function printJs($return=false)
  222.     {
  223.         $err $this->checkAll();
  224.         if($err !== TRUE)
  225.             trigger_error($errE_USER_ERROR);
  226.         
  227.         $js "";
  228.         $js .= "<script type=\"text/javascript\">\n";
  229.             
  230.             if($this->button_type == BUTTON_TYPE_IMAGE)
  231.             {
  232.                 $js .= "function toggleImg(div,img)\n";
  233.                 $js .= "{\n";
  234.                     $js .= "document.getElementById(div).src=img;\n";
  235.                 $js .= "}\n";
  236.                 $js .= "\n";
  237.             }
  238.             else
  239.             {
  240.                 $js .= "function toggleTxt(div,txt)\n";
  241.                 $js .= "{\n";
  242.                     $js .= "document.getElementById(div).innerHTML=txt;\n";
  243.                 $js .= "}\n";
  244.                 $js .= "\n";
  245.             }
  246.             
  247.             switch($this->start_status)
  248.             {
  249.                 case STATUS_CLOSE$js .= $this->div_content."_status = \"close\";\n"break;
  250.                 case STATUS_OPEN$js .= $this->div_content."_status = \"open\";\n"break;
  251.             }
  252.             
  253.             $js .= "\$('#".$this->div_button."').click( function()\n";
  254.             $js .= "{\n";
  255.                 $js .= "if(".$this->div_content."_status == \"close\")\n";
  256.                 $js .= "{\n";
  257.                     $js .= $this->div_content."_status = \"open\";\n";
  258.                     $js .= "\$('#".$this->div_content."').slideDown('".$this->motion_duration."', function(){});\n";
  259.                     
  260.                     for($i=0;$i<count($this->openFunctions);$i++)
  261.                         if($this->openFunctions[$i]!="")
  262.                             $js .= $this->openFunctions[$i].";\n";
  263.                     
  264.                 $js .= "}\n";
  265.                 $js .= "else\n";
  266.                 $js .= "{\n";
  267.                     $js .= $this->div_content."_status = \"close\";\n";
  268.                     $js .= "\$('#".$this->div_content."').slideUp('".$this->motion_duration."', function(){});\n";
  269.                     
  270.                     for($i=0;$i<count($this->closeFunctions);$i++)
  271.                         if($this->closeFunctions[$i]!="")
  272.                             $js .= $this->closeFunctions[$i].";\n";
  273.                     
  274.                 $js .= "}\n";
  275.             $js .= "});\n";
  276.             
  277.             $js .= "\n";
  278.         
  279.         $js .= "</script>\n";
  280.         
  281.         $js .= "\n";
  282.         
  283.         if(!$return)
  284.             echo $js;
  285.         else
  286.             return $js;
  287.         
  288.     }
  289.     
  290.     /**
  291.      * @method void checkAll() checkAll() check configuration
  292.      */
  293.     
  294.     function checkAll()
  295.     {
  296.         return $this->checkButton();
  297.     }
  298.     
  299.     /**
  300.      * @method void checkButton() checkButton() check if image or text are set
  301.      */
  302.     
  303.     function checkButton()
  304.     {
  305.         if($this->button_type==BUTTON_TYPE_TEXT)
  306.         {
  307.             if( (!isset($this->open_txt|| $this->open_txt==""|| (!isset($this->close_txt|| $this->close_txt=="") )
  308.                 return NO_TXT_DEFINIED;
  309.             else
  310.                 return TRUE;
  311.         }
  312.         else if($this->button_type==BUTTON_TYPE_IMAGE)
  313.         {
  314.             if( (!isset($this->open_image|| $this->open_image==""|| (!isset($this->close_image|| $this->close_image=="") )
  315.                 return NO_IMG_DEFINIED;
  316.             else
  317.                 return TRUE;
  318.         }
  319.     }
  320.     
  321.     /**
  322.      * @method void printVars() printVars() debug function to print all configuration vars
  323.      */
  324.     
  325.     function printVars()
  326.     {
  327.         echo "BUTTON: ".$this->div_button."<br />\n";
  328.         echo "CONTENT: ".$this->div_content."<br />\n";
  329.         echo "BUTTON TYPE: ".$this->button_type."<br />\n";
  330.         echo "MOTION DURATION: ".$this->motion_duration."<br />\n";
  331.         echo "OPEN IMAGE: ".$this->open_image."<br />\n";
  332.         echo "CLOSE IMAGE: ".$this->close_image."<br />\n";
  333.         echo "OPEN TXT: ".$this->open_txt."<br />\n";
  334.         echo "CLOSE TXT: ".$this->close_txt."<br />\n";
  335.         echo "START STATUS: ".$this->start_status."<br />\n";        
  336.         
  337.         echo "OPEN FUNCTION:<br />\n";
  338.         echo "<pre>\n";
  339.         print_r($this->openFunctions);
  340.         echo "\n</pre>\n";
  341.         echo "CLOSE FUNCTION:<br />\n";
  342.         echo "<pre>\n";
  343.         print_r($this->closeFunctions);
  344.         echo "</pre><br />\n";
  345.     }
  346.     
  347. }
  348.  
  349. ?>

Documentation generated on Sun, 03 Oct 2010 11:32:36 +0200 by phpDocumentor 1.4.3