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

Source for file dbgrids.inc.php

Documentation is available at dbgrids.inc.php

  1. <?php
  2. /**
  3. *  This file is part of the VCL for PHP project
  4. *
  5. *  Copyright (c) 2004-2007 qadram software <support@qadram.com>
  6. *
  7. *  Checkout AUTHORS file for more information on the developers
  8. *
  9. *  This library is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU Lesser General Public
  11. *  License as published by the Free Software Foundation; either
  12. *  version 2.1 of the License, or (at your option) any later version.
  13. *
  14. *  This library is distributed in the hope that it will be useful,
  15. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. *  Lesser General Public License for more details.
  18. *
  19. *  You should have received a copy of the GNU Lesser General Public
  20. *  License along with this library; if not, write to the Free Software
  21. *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  22. *  USA
  23. *
  24. */
  25.  
  26. use_unit("grids.inc.php");
  27. use_unit("comctrls.inc.php");
  28.  
  29. /**
  30.  * CustomDBGrid class
  31.  *
  32.  * Base class for DBGrids
  33.  */
  34. class CustomDBGrid extends CustomGrid
  35. {
  36.         protected $_database;
  37.         protected $_deletelink='';
  38.  
  39.         /**
  40.         * If true, each record will show a delete link to allow the user delete that record
  41.         * @return boolean 
  42.         */
  43.         function getDeleteLink(return $this->_deletelink}
  44.         function setDeleteLink($value$this->_deletelink=$value}
  45.         function defaultDeleteLink(return ""}
  46.  
  47.         function __construct($aowner=null)
  48.         {
  49.                 //Calls inherited constructor
  50.                 parent::__construct($aowner);
  51.  
  52.                 $this->Width=400;
  53.                 $this->Height=200;
  54.         }
  55. }
  56.  
  57. /**
  58.  * DBGrid class
  59.  *
  60.  * A component to show a dataset in a tabular way
  61.  */
  62.  /*
  63. class DBGrid extends CustomDBGrid
  64. {
  65.         protected $_datasource=null;
  66.  
  67.                 function __construct($aowner=null)
  68.         {
  69.                 //Calls inherited constructor
  70.                 parent::__construct($aowner);
  71.         }
  72.  
  73.         function dumpHeaderCode()
  74.         {
  75.                 parent::dumpHeaderCode();
  76. ?>
  77. <script type="text/JavaScript">
  78. <!--
  79. function verifydelete()
  80. {
  81. var agree=confirm("¿Seguro que quieres borrar este registro?");
  82. if (agree)
  83.         return true ;
  84. else
  85.         return false ;
  86. }
  87.  
  88. //-->
  89. </script>
  90. <?php
  91.         }
  92.  
  93.         //TODO: Datasource must link with controls to update their state when the dataset changes
  94.         function updateControl()
  95.         {
  96.                 $this->columns->clear();
  97.                 $this->rows->clear();
  98.                                 
  99.                 if ($this->_datasource!=null)
  100.                 {
  101.                         $ds=$this->_datasource->DataSet;
  102.  
  103.                         if ($ds->Active)
  104.                         {
  105.                                 for ($i=0;$i<=$ds->fields->count()-1;$i++)
  106.                                 {
  107.                                         $fld=$ds->fields->items[$i];
  108.                                         $col=new Column();
  109.                                         $col->Title=$fld->DisplayLabel;
  110.                                         $this->columns->add($col);
  111.                                         //print_r($fld);
  112.                                 }
  113.  
  114.                                 $col=new Column();
  115.                                 $col->Title="&nbsp;";
  116.                                 $this->columns->add($col);
  117.  
  118.                                 $ds->first();
  119.  
  120.                                 while (!$ds->eof())
  121.                                 {
  122.  
  123.                                         $row=new Row();
  124.                                         $row->values=$ds->readFieldValues();
  125.                                         $record_id=$row->values[0];
  126.                                         $row->values[]='<A onclick="return verifydelete();" HREF="'.$this->_deletelink.'&record_id='.$record_id.'">Borrar</A>';
  127.                                         $this->rows->add($row);
  128.                                         $ds->next();
  129.                                 }
  130.                         }
  131.                 }
  132.         }
  133.  
  134.         function getFont() { return $this->readFont(); }
  135.         function setFont($value) { $this->writeFont($value); }
  136.  
  137.         function loaded()
  138.         {
  139.                 parent::loaded();
  140.                 $this->setDataSource($this->_datasource);
  141.         }
  142.  
  143.         //DataSource property
  144.         function getDataSource() { return $this->_datasource;   }
  145.         function setDataSource($value)
  146.         {
  147.                 $this->_datasource=$this->fixupProperty($value);
  148.         }
  149.  
  150.         function dumpContents()
  151.         {
  152.                 //Dump here code if any
  153.                 $this->updateControl();
  154.                 parent::dumpContents();
  155.         }
  156. }
  157. */
  158.  
  159. /**
  160.  * DBGrid class
  161.  *
  162.  * A component to show a dataset in a tabular way
  163.  */
  164. class DBGrid extends CustomListView
  165. {
  166.         //Publish common properties
  167.         
  168.         function getVisible(return $this->readVisible()}
  169.         function setVisible($value$this->writeVisible($value)}
  170.         
  171.         protected $_datasource=null;
  172.         private $_latestheader=null;
  173.  
  174.         function __construct($aowner=null)
  175.         {
  176.                 //Calls inherited constructor
  177.                 parent::__construct($aowner);
  178.  
  179.                 $this->Width=400;
  180.                 $this->Height=200;
  181.                 $this->ControlStyle="csSlowRedraw=1";
  182.         }
  183.  
  184.         function init()
  185.         {
  186.                 parent::init();
  187.  
  188.                 //Include the RPC to handle any request
  189.                 use_unit("rpc/rpc.inc.php");
  190.         }
  191.  
  192.         /**
  193.         * To give permission to execute certain methods
  194.         * @param string $method Method for which we want to know the accessibility
  195.         * @param integer $defaccesibility If the method is not found, this will be returned
  196.         * @return integer 
  197.         */
  198.         function readAccessibility($method$defaccessibility)
  199.         {
  200.                 switch($method)
  201.                 {
  202.                 case "updateRow"return Accessibility_Domain;
  203.                 }
  204.  
  205.                 return(parent::readAccessibility($method$defaccessibility));
  206.         }
  207.  
  208.         /**
  209.         *  Updates a row of the attached datasource->dataset, this method is called
  210.         *  using ajax from the client
  211.         *  @param array $params Array of parameters to this method
  212.         *  @param object $error RPC Error object to return if anything goes wrong
  213.         *  @return mixed 
  214.         */
  215.         function updateRow($params$error)
  216.         {
  217.                 if (count($params!= 4)
  218.                 {
  219.                     $error->SetError(JsonRpcError_ParameterMismatch"Expected 4 parameter; got " count($params));
  220.                     return $error;
  221.                 }
  222.                 else
  223.                 {
  224.                         $fieldnames=$params[0];
  225.                         $fieldvalues=$params[1];
  226.                         $origvalues=$params[2];
  227.                         $dbgridrow=$params[3];
  228.  
  229.                         reset($fieldnames);
  230.                         reset($fieldvalues);
  231.  
  232.                         if ($this->_datasource!=null)
  233.                         {
  234.                                 if ($this->_datasource->Dataset!=null)
  235.                                 {
  236.                                         if ($this->_datasource->Dataset->Database!=null)
  237.                                         {
  238.                                                 try
  239.                                                 {
  240.                                                     //Get an array with the keys and fields to change
  241.                                                     $output=array();
  242.  
  243.                                                     $keys=$this->_datasource->DataSet->_keyfields;
  244.                                                     while (list($k,$v)=each($fieldnames))
  245.                                                     {
  246.                                                         if ((in_array($v,$keys)) || ($fieldvalues[$k]!=$origvalues[$k]))
  247.                                                         {
  248.                                                             $output[$v]=$fieldvalues[$k];
  249.                                                         }
  250.                                                     }
  251.                                                     $this->_datasource->DataSet->edit();
  252.                                                     $this->_datasource->DataSet->fieldbuffer=$output;
  253.                                                     $this->_datasource->DataSet->Post();
  254.                                                 }
  255.                                                 catch (Exception $e)
  256.                                                 {
  257.                                                         $error->SetError(-104'Caught exception: '.$e->getMessage());
  258.                                                         return $error;
  259.                                                 }
  260.                                                 return $dbgridrow;
  261.                                         }
  262.                                         else
  263.                                         {
  264.                                             $error->SetError(-103"Datasource->Dataset->Database not assigned");
  265.                                             return $error;
  266.                                         }
  267.                                 }
  268.                                 else
  269.                                 {
  270.                                     $error->SetError(-102"Datasource->Dataset not assigned");
  271.                                     return $error;
  272.                                 }
  273.                         }
  274.                         else
  275.                         {
  276.                             $error->SetError(-101"Datasource not assigned");
  277.                             return $error;
  278.                         }
  279.                 }
  280.  
  281.                 return -1;
  282.  
  283.         }
  284.  
  285.         /**
  286.         * Dump javascript code to attach the updateRow method to the grid using ajax
  287.         */
  288.         function dumpRPC()
  289.         {
  290.             if (($this->ControlState csDesigning)!=csDesigning)
  291.             {
  292. ?>
  293.                 function <?php echo $this->Name?>_rpcdatachanged(event)
  294.                 {
  295.  
  296. <?php
  297.                         if (($this->_jsondatachanged!=""&& ($this->_jsondatachanged!=null))
  298.                         {
  299.                                 echo $this->_jsondatachanged."(event);\n";
  300.                         }
  301. ?>
  302.                         var obj;
  303.                         obj=this;
  304.                         data=event.getData();
  305.                         modifiedRow=data.firstRow;
  306.  
  307.                         row=this.getRowData(modifiedRow);
  308.                         orow=this.originalData[modifiedRow];
  309.  
  310.                         qx.Settings.setCustomOfClass("qx.io.Json", "enableDebug", true);
  311.  
  312.                         var rpc = new qx.io.remote.Rpc();
  313.                         rpc.setTimeout(10000);
  314.                         var mycall = null;
  315.                         rpc.setUrl("<?php echo $_SERVER['PHP_SELF']?>");
  316.                         rpc.setServiceName("<?php echo $this->owner->Name?>.<?php echo $this->Name?>");
  317.                         rpc.setCrossDomain(false);
  318.  
  319.                         mycall = rpc.callAsync
  320.                         (
  321.                                 function(result, ex, id)
  322.                                 {
  323.                                     mycall = null;
  324.                                     event=new Object();
  325.                                     event.result=result;
  326.                                     event.ex=ex;
  327.                                     event.id=id;
  328.  
  329.                                     if (result>=0)
  330.                                     {
  331.                                         if (obj)
  332.                                         {
  333.                                             row=obj.getRowData(result);
  334.                                             if (row)
  335.                                             {
  336.                                                 obj.originalData[result]=row.slice();
  337.                                             }
  338.                                         }
  339.                                     }
  340. <?php
  341.                                         if (($this->_jsonrowsaved!=""&& ($this->_jsonrowsaved!=null))
  342.                                         {
  343.                                                 echo $this->_jsonrowsaved."(event);";
  344.                                         }
  345. ?>
  346.                                     send.setEnabled(true);
  347.                                     abort.setEnabled(false);
  348.                                 }
  349.                         , "updateRow", this.ColumnNames, row, orow, modifiedRow
  350.                         );
  351.                 }
  352. <?php
  353.             }
  354.         }
  355.  
  356.         /**
  357.         * Creates the DBGrid Table Model
  358.         */
  359.         function createTableModel()
  360.         {
  361. ?>
  362.             // table model
  363.             var <?php echo $this->Name?>_tableModel = new qx.ui.table.SimpleTableModel();
  364. <?php
  365.         }
  366.         //TODO: Datasource must link with controls to update their state when the dataset changes
  367.         
  368.         function updateControl()
  369.         {
  370.             if (($this->ControlState csDesigning)!=csDesigning)
  371.             {
  372.                 //Ensure there is a valid datasource
  373.                 $this->setDataSource($this->_datasource);
  374.  
  375.                 if (is_object($this->_datasource))
  376.                 {
  377.                         $ds=$this->_datasource->DataSet;
  378.  
  379.                         if ($ds->Active)
  380.                         {
  381.                             $ds->first();
  382.                             $fields=$ds->Fields;
  383.  
  384. //        $this->createTableModel();
  385. ?>
  386.         <?php echo $this->Name?>_tableModel.setColumns([
  387. <?php
  388.         if (is_array($fields))
  389.         {
  390.         reset($fields);
  391.         $i=0;
  392.         while(list($fname$value)=each($fields))
  393.         {
  394.                 $props=$this->_datasource->DataSet->readFieldProperties($fname);
  395.                 $dlabel=$fname;
  396.  
  397.                 if ($props)
  398.                 {
  399.                         if (array_key_exists('displaylabel',$props))
  400.                         {
  401.                                 $dlabel=$props['displaylabel'][0];
  402.                         }
  403.                 }
  404.  
  405.                 if ($i>0echo ",";
  406.                 echo '"'.$dlabel.'"';
  407.                 $i++;
  408.         }
  409.         }
  410. ?>
  411.  ]);
  412.  
  413.  
  414.         <?php echo $this->Name?>_tableModel.ColumnNames=new Array(
  415. <?php
  416.         $cnames=$ds->Fields;
  417.         if (is_array($cnames))
  418.         {
  419.         reset($cnames);
  420.         $i=0;
  421.         while(list($fname$value)=each($cnames))
  422.         {
  423.                 if ($i>0echo ",";
  424.                 echo '"'.$fname.'"';
  425.                 $i++;
  426.         }
  427.         }
  428. ?>
  429. );
  430.  
  431.  
  432.  
  433.         var rowData = [];
  434.         var oData = [];
  435. <?php
  436.  
  437.                                 $ds->first();
  438.                                 while (!$ds->EOF)
  439.                                 {
  440.                                         $rvalues=$ds->Fields;
  441. ?>
  442.                         rowData.push([
  443.                         <?php
  444.                                         reset($rvalues);
  445.                                         $i=0;
  446.                                         while (list($k,$v)=each($rvalues))
  447.                                         {
  448.                                                 $v=str_replace("\n\r",'\n',$v);
  449.                                                 $v=str_replace("\n",'\n',$v);
  450.                                                 $v=str_replace("\r",'',$v);
  451.                                                 $v=str_replace('"','\"',$v);
  452.                                                 $v=str_replace('<','\<',$v);
  453.                                                 $v=str_replace('>','\>',$v);
  454. //                                                $v=htmlentities($v);
  455.                                                 if ($i>0echo ",";
  456.                                                 echo '"'.$v.'"';
  457.                                                 $i++;
  458.  
  459.                                         }
  460.                         ?>
  461.                         ]);
  462.                         oData.push([
  463.                         <?php
  464.                                         reset($rvalues);
  465.                                         $i=0;
  466.                                         while (list($k,$v)=each($rvalues))
  467.                                         {
  468.                                                 $v=str_replace("\n\r",'\n',$v);
  469.                                                 $v=str_replace("\n",'\n',$v);
  470.                                                 $v=str_replace("\r",'',$v);
  471.                                                 $v=str_replace('"','\"',$v);
  472.                                                 $v=str_replace('<','\<',$v);
  473.                                                 $v=str_replace('>','\>',$v);
  474. //                                                $v=htmlentities($v);
  475.                                                 if ($i>0echo ",";
  476.                                                 echo '"'.$v.'"';
  477.                                                 $i++;
  478.  
  479.                                         }
  480.                         ?>
  481.                         ]);
  482.                         <?php
  483.                                         $ds->next();
  484.                                 }
  485.  
  486. ?>
  487.         <?php echo $this->Name?>_tableModel.originalData=oData;
  488.         <?php echo $this->Name?>_tableModel.setData(rowData);
  489. <?php
  490.         $this->_latestheader=$fields;
  491.         if (is_array($fields))
  492.         {
  493.             reset($fields);
  494.             $i=0;
  495.             while(list($fname$value)=each($fields))
  496.             {
  497. ?>
  498.             <?php echo $this->Name?>_tableModel.setColumnEditable(<?php echo $i?>, true);
  499. <?php
  500.             $i++;
  501.             }
  502.  
  503.  
  504.         }
  505.                       }
  506.                 }
  507.             }
  508.         }
  509.  
  510.         function getFont(return $this->readFont()}
  511.         function setFont($value$this->writeFont($value)}
  512.  
  513.         function getDataSource(return $this->_datasource;   }
  514.         function setDataSource($value)
  515.         {
  516.                 $this->_datasource=$this->fixupProperty($value);
  517.         }
  518.  
  519.         function loaded()
  520.         {
  521.                 parent::loaded();
  522.                 $this->setDataSource($this->_datasource);
  523.         }
  524.  
  525.             protected $_jsondatachanged=null;
  526.  
  527.         /**
  528.         * This event is fired whenever the contents of a cell is changed
  529.         */
  530.             function getjsOnDataChanged(return $this->_jsondatachanged}
  531.             function setjsOnDataChanged($value$this->_jsondatachanged=$value}
  532.             function defaultjsOnDataChanged(return ""}
  533.  
  534.             protected $_jsonrowsaved=null;
  535.  
  536.         /**
  537.         * This event is fired whenever the contents of a row are saved
  538.         */
  539.             function getjsOnRowSaved(return $this->_jsonrowsaved}
  540.             function setjsOnRowSaved($value$this->_jsonrowsaved=$value}
  541.             function defaultjsOnOnRowSaved(return ""}
  542.  
  543.             protected $_jsonrowchanged=null;
  544.  
  545.         /**
  546.         * This event is fired whenever the active row is changed
  547.         */
  548.             function getjsOnRowChanged(return $this->_jsonrowchanged}
  549.             function setjsOnRowChanged($value$this->_jsonrowchanged=$value}
  550.             function defaultjsOnOnRowChanged(return ""}
  551.  
  552.         function getjsOnClick                   (return $this->readjsOnClick()}
  553.         function setjsOnClick                   ($value$this->writejsOnClick($value)}
  554.  
  555.         function getjsOnDblClick                (return $this->readjsOnDblClick()}
  556.         function setjsOnDblClick                ($value$this->writejsOnDblClick($value)}
  557.  
  558.           /**
  559.          * Dump Javascript events
  560.          *
  561.          */
  562.         function dumpJsEvents()
  563.         {
  564.                 parent::dumpJsEvents();
  565.                 $this->dumpJSEvent($this->_jsondatachanged);
  566.                 $this->dumpJSEvent($this->_jsonrowsaved);
  567.                 $this->dumpJSEvent($this->_jsonrowchanged);
  568.         }
  569.  
  570.         /**
  571.         * Dump the common code for this control
  572.         */
  573.         function commonScript()
  574.         {
  575.                 echo "        ".$this->Name.".setBorder(qx.renderer.border.BorderPresets.getInstance().shadow);\n";
  576.                 echo "        ".$this->Name.".setBackgroundColor(\"white\");\n";
  577.                 echo "        $this->Name.setLeft(0);\n";
  578.                 echo "        $this->Name.setTop(0);\n";
  579. //                echo "        $this->Name.setOverflow(\"auto\");\n";
  580.                 echo "        $this->Name.setWidth($this->Width);\n";
  581.                 echo "        $this->Name.setHeight($this->Height);\n";
  582.                 echo "        $this->Name.getSelectionModel().setSelectionMode(qx.ui.table.SelectionModel.MULTIPLE_INTERVAL_SELECTION);\n";
  583.  
  584.  
  585.             $fields=$this->_latestheader;
  586.  
  587.             if (is_array($fields))
  588.             {
  589.                 reset($fields);
  590.                 $i=0;
  591.                 while(list($fname$value)=each($fields))
  592.                 {
  593.                     $props=$this->_datasource->DataSet->readFieldProperties($fname);
  594.                     $dwidth=100;
  595.  
  596.                     if ($props)
  597.                     {
  598.                             if (array_key_exists('displaywidth',$props))
  599.                             {
  600.                                     $dwidth=$props['displaywidth'][0];
  601.                             }
  602.                     }
  603.  
  604.                     echo "        $this->Name.getTableColumnModel().setColumnWidth($i,$dwidth);\n";
  605.                 $i++;
  606.                 }
  607.             }
  608.         }
  609.  
  610.         function dumpContents()
  611.         {
  612.                 $this->dumpCommonContentsTop();
  613.                 $this->createTableModel();
  614.                 $this->updateControl();
  615.                 echo "        var ".$this->Name."    = new qx.ui.table.Table(".$this->Name."_tableModel);\n";
  616.                 $this->commonScript();
  617.                 $this->callEvent('onshow'array());
  618.  
  619.                 if (($this->ControlState csDesigning)!=csDesigning)
  620.                 {
  621.                         $this->dumpRPC();
  622.  
  623.                         echo "        ".$this->Name."_tableModel.addEventListener(\"dataChanged\", ".$this->Name."_rpcdatachanged, ".$this->Name."_tableModel);\n";
  624.  
  625.                         if (($this->_jsonclick!=""&& ($this->_jsonclick!=null))
  626.                         {
  627.                                 echo "        $this->Name.addEventListener(\"click\", $this->_jsonclick);\n";
  628.                         }
  629.  
  630.                         if (($this->_jsonrowchanged!=""&& ($this->_jsonrowchanged!=null))
  631.                         {
  632.                                 echo "        $this->Name.getSelectionModel().addEventListener(\"changeSelection\", $this->_jsonrowchanged);\n";
  633.                         }
  634.  
  635.                         if (($this->_jsondblclick!=""&& ($this->_jsondblclick!=null))
  636.                         {
  637.                                 echo "        $this->Name.addEventListener(\"dblclick\", $this->_jsondblclick);\n";
  638.                         }
  639.                 }
  640.  
  641.                 $this->dumpCommonContentsBottom();
  642.         }
  643.  
  644.         /**
  645.         * This method is called to know the code to be executed to update the control
  646.         * using javascript
  647.         */
  648.         function dumpForAjax()
  649.         {
  650.                 $this->updateControl();
  651.                 $this->commonScript();
  652.         }
  653. }
  654.  
  655. ?>

Documentation generated on Tue, 27 Mar 2007 13:34:16 +0200 by phpDocumentor 1.3.1