Xataface 2.0
Xataface Application Framework
HTML/QuickForm/grid.php
Go to the documentation of this file.
00001 <?php
00002 require_once 'HTML/QuickForm/input.php';
00003 function df_clone($object){
00004         if (version_compare(phpversion(), '5.0') < 0) {
00005                 return $object;
00006          } else {
00007                 return @clone($object);
00008         }
00009  
00010 }
00011 
00012 class HTML_QuickForm_grid extends HTML_QuickForm_input {
00013 
00014 
00015         var $fields=array();
00016         var $elements=array();
00017         var $next_row_id=0;
00018         var $addNew=true;
00019         var $addExisting=true;
00020         var $addExistingFilters;
00021         var $delete=true;
00022         var $reorder=true;
00023         var $edit=true;
00024         var $table=null;
00025         var $defaults = array();
00026 
00027         
00028         
00029         
00030         function getName(){ return $this->name;}
00031         
00032         function HTML_QuickForm_grid($elementName=null, $elementLabel=null, $attributes=null)
00033     {
00034         $this->HTML_QuickForm_input($elementName, $elementLabel, $attributes);
00035         $this->_persistantFreeze = true;
00036         $this->name = $elementName;
00037         $this->setName($elementName);
00038         
00039         $this->_type = 'grid';
00040        
00041     } //end constructor
00042     
00043     function addField(&$fieldDef, $element){
00044         $this->fields[$fieldDef['name']] =& $fieldDef;
00045         $this->elements[$fieldDef['name']] =& $element;
00046     }
00047     
00048     function getColumnFieldDef($name){
00049         return @$this->fields[$name];
00050     }
00051     
00052     function getColumnElement($name){
00053         return @$this->elements[$name];
00054     }
00055     
00056     function getColumnLabels(){
00057         $out = array();
00058         foreach ( $this->fields as $field ){
00059                 $out[] = $field['widget']['label'];
00060         }
00061         return $out;
00062     }
00063     
00064     function getColumnIds(){
00065         $out = array();
00066         foreach ($this->fields as $field ){
00067                 $out[] = $field['name'];
00068         }
00069         return $out;
00070     }
00071     
00072     
00073     function getCellTemplate($column, $fieldId, $value=null, $permissions=array('view'=>1,'edit'=>1)){
00074         $element = df_clone($this->elements[$column]);
00075         $properties = $this->getProperties();
00076         
00077         $element->setName($this->name.'['.($this->next_row_id).']['.$column.']');
00078         $element->updateAttributes(
00079                 array(
00080                         'id'=>$column.'_'.$fieldId,
00081                         'onchange'=>( @$properties['onFieldChange'] ? $properties['onFieldChange'].'(this);':'').(($this->addNew or $this->addExisting)?'dataGridFieldFunctions.addRowOnChange(this);':'').$element->getAttribute('onchange'),
00082                         'style'=>'width:100%;'.$element->getAttribute('style')
00083                         )
00084                 );
00085         
00086         if ($this->isFrozen() or !Dataface_PermissionsTool::checkPermission('edit', $permissions)) {
00087             $element->freeze();
00088         } else {
00089                 $element->unfreeze();
00090         }
00091         if ( isset($value) ){
00092          $element->setValue($value);
00093         }
00094         
00095         return $element->toHtml();
00096     }
00097     
00098     function getEmptyCellTemplate($column, $fieldId){
00099         $value = null;
00100         if ( isset($this->defaults[$column]) ){
00101                 $value = $this->defaults[$column];
00102         }
00103         return $this->getCellTemplate($column, $fieldId, $value);
00104     
00105     }
00106     
00107     
00108     
00109     
00110     
00111     function toHtml(){
00112         
00113         //print_r($this->getProperties());
00114         ob_start();
00115         if ( !defined('HTML_QuickForm_grid_displayed') ){
00116                 define('HTML_QuickForm_grid_displayed',true);
00117                 echo '<script type="text/javascript" language="javascript" src="'.DATAFACE_URL.'/HTML/QuickForm/grid.js"></script>';
00118         }
00119         
00120         
00121         $columnNames = $this->getColumnLabels();
00122         $columnIds = $this->getColumnIds();
00123         $fielddata = $this->getValue();
00124         
00125         
00126         
00127         if ( !is_array($fielddata) ){
00128                 $fielddata = array();
00129         }
00130         $fieldName = $this->name;
00131 ?>
00132                         <table id="xf-grid-table-<?php echo $this->getName();?>" class="xf-grid-table-<?php echo $this->getName();?>" style="width: 100%; <?php echo $this->getAttribute('style');?>">
00133                 <thead>
00134                     <tr>
00135                     <?php foreach ( $columnNames as $i=>$columnName):?>
00136                         <th class="discreet" style="text-align: left">
00137                                 <?php echo $columnName?>
00138                                 
00139                                 
00140                         </th>
00141                      <?php endforeach;?>
00142                         <th ></th> 
00143                         <th ></th>
00144                         <th ></th>
00145                     </tr>
00146                 </thead>            
00147                 <tbody>
00148                         <?php $emptyRow = false; $count=0; foreach ( $fielddata as $rows ):?>
00149                         
00150                         <?php if ( !is_array($rows) /*or !isset($rows['__permissions__'])*/ ) continue; ?>
00151                     <?php ob_start();?>
00152                     <tr df:row_id="<?php echo $this->next_row_id;?>" class="xf-form-group">
00153                         <?php $fieldId = $fieldName.'_'.($this->next_row_id); ?>
00154                       
00155                      
00156                      <?php
00157                         //IE doesn't seem to respect em unit paddings here so we
00158                         //use absolute pixel paddings.
00159                      ?>
00160                        <?php $rowEmpty = true; foreach ( $columnIds as $column ): ?>
00161                        <?php $fieldDef =& $this->getColumnFieldDef($column);$fieldTable = Dataface_Table::loadTable($fieldDef['tablename']);?>
00162                        
00163                                 
00164                        <td style="padding-right: 10px;" valign="top" data-xf-grid-default-value="<?php echo htmlspecialchars($fieldTable->getDefaultValue($fieldDef['name']));?>">
00165                        <?php unset($fieldDef, $fieldTable);?>
00166                         <?php
00167                                 //$column_definition = $this->getColumnDefinition($column);
00168                                 $cell_value = $rows[$column];
00169                                 if ( trim($cell_value) ) $rowEmpty = false;
00170                                 if ( isset($rows['__permissions__']) and @$rows['__permissions__'][$column] ){
00171                                         $perms = $rows['__permissions__'][$column];
00172                                 } else {
00173                                         $perms = array('view'=>1,'edit'=>1);
00174                                 }
00175                                 //if ( isset($this->filters[$column]) ) $cell_value = $this->filters[$column]->pullValue($cell_value);
00176                                 $cell_html = $this->getCellTemplate($column, $fieldId, $cell_value, $perms);
00177                         ?>
00178                                 
00179                         <span>
00180                              <?php echo $cell_html;?>
00181                         </span>
00182                        </td>
00183                        <?php endforeach;
00184                        if ( $rowEmpty ) $emptyRow = true;
00185 
00186                        ?>
00187                         <td style="width: 20px">
00188                                 <input type="hidden" name="<?php echo $fieldName.'['.$this->next_row_id.'][__id__]';?>" value="<?php echo $rows['__id__'];?>"/>
00189                             <?php if ( $this->delete ): ?>
00190                             <img src="<?php echo DATAFACE_URL.'/images/delete_icon.gif';?>" 
00191                                style="cursor: pointer;"
00192                                   
00193                                  alt="Delete row"  
00194                                  onclick="dataGridFieldFunctions.removeFieldRow(this);return false"/>
00195                              <?php endif; ?>
00196                         </td>
00197                                                 <td style="width: 20px">
00198                 
00199                             <?php if ( $this->reorder and $this->addNew ): ?>
00200                             <img src="<?php echo DATAFACE_URL.'/images/add_icon.gif';?>" 
00201                                style="cursor: pointer;"
00202                                   
00203                                  alt="Insert Row"  
00204                                  onclick="dataGridFieldFunctions.addRowOnChange(this,true);return false"/>
00205                              <?php endif; ?>
00206                         </td>
00207                        
00208                         <td style="width: 20px">
00209                            <?php if ($this->reorder):?>
00210                            <img src="<?php echo DATAFACE_URL.'/images/arrowUp.gif';?>" 
00211                                style="cursor: pointer; display: block;"
00212                                  
00213                                  alt="Move row up"  
00214                                  onclick="dataGridFieldFunctions.moveRowUp(this);return false"/>
00215                             <img src="<?php echo DATAFACE_URL.'/images/arrowDown.gif';?>" 
00216                                style="cursor: pointer; display: block;"
00217                             
00218                                  alt="Move row up"  
00219                                  onclick="dataGridFieldFunctions.moveRowDown(this);return false"/> 
00220                           <?php endif;?>
00221                            <input type="hidden"
00222                                   name="<?php echo $fieldName.'['.$this->next_row_id.'][__order__]';?>"
00223                                   id="<?php echo 'orderindex__'.$fieldId;?>"
00224                                   value="<?php echo $this->next_row_id;?>"
00225                                   />                         
00226                         </td>
00227                        
00228                         
00229                     
00230                     </tr>
00231                     <?php $lastRowHtml = ob_get_contents(); ob_end_flush(); ?>
00232                     <?php $this->next_row_id++; endforeach;?>
00233                     <?php if (!$emptyRow and ($this->addNew or $this->addExisting)): ?>
00234                     <?php ob_start();?>
00235                     <tr class="xf-form-group" df:row_id="<?php echo $this->next_row_id;?>" <?php if ( !$this->addNew ):?>style="display:none"<?php endif;?>>
00236                     <?php
00237                         $fieldId = $fieldName.'_'.$this->next_row_id;
00238                         
00239                     ?>
00240                         <?php foreach ($columnIds as $column):?>
00241                        <?php $fieldDef =& $this->getColumnFieldDef($column);$fieldTable = Dataface_Table::loadTable($fieldDef['tablename']);?>
00242                        
00243                                 
00244                        <td style="padding-right: 10px;" valign="top" data-xf-grid-default-value="<?php echo htmlspecialchars($fieldTable->getDefaultValue($fieldDef['name']));?>">
00245                        <?php unset($fieldDef, $fieldTable);?>
00246                            <span >
00247                            <?php
00248                                 $cell_html = $this->getEmptyCellTemplate($column, $fieldId);
00249                                 echo $cell_html;
00250                            ?>
00251                                                                        
00252                               </span>
00253                         </td>
00254                         <?php endforeach;?>
00255                         <td style="width: 20px">
00256                         <?php if (!$this->_flagFrozen):?>
00257                            <input type="hidden" name="<?php echo $fieldName.'['.$this->next_row_id.'][__id__]';?>" value="new"/>
00258                            
00259                             <img style="display: none; cursor: pointer" 
00260                                  src="<?php echo DATAFACE_URL.'/images/delete_icon.gif';?>" 
00261                                  alt="Delete row"  
00262                                  onclick="dataGridFieldFunctions.removeFieldRow(this); return false"/>
00263                         <?php endif;?>
00264                         </td>
00265                         <td style="width: 20px">
00266                 
00267                             <?php if ( !$this->_flagFrozen ): ?>
00268                             <img src="<?php echo DATAFACE_URL.'/images/add_icon.gif';?>" 
00269                                style="cursor: pointer; display: none"
00270                                   
00271                                  alt="Insert Row"  
00272                                  onclick="dataGridFieldFunctions.addRowOnChange(this,true);return false"/>
00273                              <?php endif; ?>
00274                         </td>
00275                         <td style="width: 20px">
00276                         <?php if (!$this->_flagFrozen):?>
00277                            <img src="<?php echo DATAFACE_URL.'/images/arrowUp.gif';?>" 
00278                                 style="display: none; cursor: pointer;"
00279                                 alt="Move row up"  
00280                                 onclick="dataGridFieldFunctions.moveRowUp(this); return false"/>
00281                            <img src="<?php echo DATAFACE_URL.'/images/arrowDown.gif';?>" 
00282                                  style="display: none; cursor: pointer;"
00283                                  alt="Move row down"  
00284                                  onclick="dataGridFieldFunctions.moveRowDown(this); return false"/>
00285                           
00286                                    
00287                            <input type="hidden"
00288                                    value="<?php echo ( $this->getValue() ? 999999 : 0);?>"
00289                                    name="<?php echo $fieldName.'['.$this->next_row_id.'][__order__]';?>"
00290                                    id="<?php echo 'orderindex__'.$fieldId;?>"
00291                                     />
00292                         <?php endif;?>
00293                         </td>
00294                     </tr>
00295                     <?php $lastRowHtml = ob_get_contents(); ob_end_flush(); ?>
00296                     <?php endif;?>
00297                 </tbody>
00298                 <tfoot style="display:none" class="xf-disable-decorate">
00299                         
00300                 </tfoot>
00301             </table>
00302             <script>
00303                 jQuery('table.xf-grid-table-<?php echo $this->getName();?>').each(function(){
00304                         var rows = jQuery('tbody tr', this);
00305                         var lastRow = rows.get(rows.size()-1);
00306                         var template = jQuery(lastRow).clone();
00307                         jQuery('script[src]', template).remove();
00308                         
00309                         var scripts = lastRow.getElementsByTagName('SCRIPT');
00310                                         var scriptTexts = [];
00311                                         for ( var i=0,imax=scripts.length; i<imax; i++){
00312                                                 scriptTexts[scriptTexts.length] = dataGridFieldFunctions.getScriptText(scripts[i]);
00313                                         }
00314                                         
00315                                         scripts = scriptTexts;
00316                                         jQuery(this).attr('data-script-text', scripts.join("\n"));
00317                                         if ( typeof(dataGridFieldFunctions.templates) == 'undefined' ){
00318                                                 dataGridFieldFunctions.templates = {};
00319                                         }
00320                                         
00321                                         dataGridFieldFunctions.templates['xf-grid-table-<?php echo $this->getName();?>'] = template.get(0);
00322                 });
00323             
00324             </script>
00325             
00326             <input type="hidden" name="<?php echo $fieldName.'[__loaded__]';?>" value="1"/>
00327             
00328             <?php if ( $this->addExisting ): ?>
00329             <input type="button" class="xf-lookup-grid-row-button-<?php echo $fieldName;?>" value="Add Existing Record"/>
00330             <script type="text/javascript">
00331                 jQuery(document).ready(function($){
00332                         $('.xf-lookup-grid-row-button-<?php echo $fieldName;?>').each(function(){
00333                                 $(this).RecordBrowser({
00334                                         <?php if ($this->addExistingFilters):?>filters: <?php echo json_encode($this->addExistingFilters);?>,<?php endif;?>
00335                                         table: <?php echo json_encode($this->table);?>,
00336                                         callback: function(values){
00337                                                 // After we select the records we need to place them
00338                                                 // in the grid
00339                                                 var ids = [];
00340                                                 for ( var id in values ){
00341                                                         ids[ids.length] = encodeURIComponent('-id[]')+'='+encodeURIComponent(id);
00342 
00343                                                 }
00344                                                 var url = DATAFACE_SITE_HREF+'?-action=RecordBrowser_lookup_single&'+ids.join('&')+'&-table='+encodeURIComponent(<?php echo json_encode($this->table);?>)+'&-text=__json__&-return-type=array';
00345                                                 $.getJSON(url, function(data){
00346                                                         
00347                                                         for ( var i=0; i<data.length; i++ ){
00348                                                                 var row = data[i];
00349                                                                 if ( row['appointment_id'] ){
00350                                                                                         // if this position is already associated with an
00351                                                                                         // appointment, we cannot add it to this appointment
00352                                                                                         alert('This position is already associated with another appointment.  It cannot be added to a second appointment.');
00353                                                                                         return;
00354                                                                                 }
00355                                                                 var selector = '.xf-grid-table-<?php echo $this->getName();?> tr:last';
00356                                                                 var lastRow = $(selector);
00357                                                                 for ( var j in row ){
00358                                                                         lastRow.find("input[name$='["+j+"]']").each(function(){
00359                                                                                 //alert($(this).attr('name'));
00360                                                                                 $(this).val(row[j]);
00361                                                                                 $(this).trigger("change");
00362                                                                         });
00363                                                                 }
00364                                                                 
00365                                                                 lastRow.find("input[name$='[__id__]']").each(function(){
00366                                                                         $(this).val('new:'+$(this).val());
00367                                                                 });
00368                                                                 
00369                                                                         
00370                                                         }
00371                                                 });
00372                                                 //alert('now');
00373                                         
00374                                         }
00375                                 });
00376                                 $(this).css({
00377                                         'padding-left': '25px',
00378                                         'background-image': 'url('+DATAFACE_URL+'/images/search_icon.gif)',
00379                                         'background-repeat': 'no-repeat',
00380                                         'background-position': '3px 3px'
00381                                 });
00382                                 
00383                         });
00384                 });
00385             </script>
00386             
00387             <?php endif;?>
00388            
00389 
00390 <?php
00391                 $out = ob_get_contents();
00392                 ob_end_clean();
00393                 return $out;
00394     
00395     }
00396 
00397 }
 All Data Structures Namespaces Files Functions Variables Enumerations