Xataface 2.0
Xataface Application Framework
Dataface/QuickForm.php
Go to the documentation of this file.
00001 <?php
00002 /*-------------------------------------------------------------------------------
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *-------------------------------------------------------------------------------
00020  */
00021 
00022 /*******************************************************************************
00023  * File:        Dataface/QuickForm.php
00024  * Author:      Steve Hannah
00025  * Description:
00026  *      An extension of HTML_QuickForm to auto-generate a form for a particular table
00027  *      in an SQL database.
00028  *      
00029  *******************************************************************************/
00030  
00031 import( 'HTML/QuickForm.php');
00032 import( 'Dataface/Table.php');
00033 import( 'Dataface/Vocabulary.php');
00034 import( 'Dataface/QueryBuilder.php');
00035 import( 'Dataface/QueryTool.php');
00036 import( 'Dataface/IO.php');
00037 import( 'Dataface/SkinTool.php');
00038 import('HTML/QuickForm/Renderer/Dataface.php');
00039 import( 'Dataface/PermissionsTool.php');
00040 import('Dataface/FormTool.php');
00041 
00042 
00043 // Register our special types
00044 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['htmlarea'] = array('HTML/QuickForm/htmlarea.php', 'HTML_QuickForm_htmlarea');
00045 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['table'] = array('HTML/QuickForm/table.php', 'HTML_QuickForm_table');
00046 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['calendar'] = array('HTML/QuickForm/calendar.php', 'HTML_QuickForm_calendar');
00047 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['time'] = array('HTML/QuickForm/time.php', 'HTML_QuickForm_time');
00048 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['webcam'] = array('HTML/QuickForm/webcam.php', 'HTML_QuickForm_webcam');
00049 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']['portal'] = array('HTML/QuickForm/portal.php', 'HTML_QuickForm_portal');
00050 define( 'QUICKFORM_AMBIGUOUS_FIELD_ERROR', 2);
00051 define( 'QUICKFORM_NO_SUCH_FIELD_ERROR',3);
00052 
00057 class Dataface_QuickForm extends HTML_QuickForm {
00058 
00059         public static $TRACK_SUBMIT = true;
00060         
00064         var $tablename;
00065         
00069         var $db;
00070         
00075         var $_iniFile;
00076         
00080         var $_query;
00081         
00085         var $_exactMatches = false;
00086         
00090         var $_table;
00091         
00095         var $_record;
00096         
00100         var $_resultSet;
00101         
00105         var $_attributes = array();
00106         
00110         var $_renderer;
00111 
00115         var $_new = false;
00116         
00122         var $overrideNoQuery = false;
00123         
00124         
00129         var $_fields = array();
00130         
00135         var $_isBuilt = false;
00136         
00137         var $_fieldnames = null;
00138         
00139         var $_lang;
00140         
00145         var $_changed_fields=array();
00146         
00147         
00151         var $tab;
00152         
00153         var $app;
00154         
00172         function Dataface_QuickForm($tablename, $db='',  $query='', $formname='', $new=false, $fieldnames=null, $lang=null){
00173                 $app =& Dataface_Application::getInstance();
00174                 $this->app =& $app;
00175                 $appQuery =& $app->getQuery();
00176                 if ( !isset($lang) && !isset($this->_lang) ){
00177                         $this->_lang = $app->_conf['lang'];
00178                 } else if ( isset($lang) ){
00179                         $this->_lang = $lang;
00180                 }
00181                 if ( is_a($tablename, 'Dataface_Record') ){
00182                         if ( !$this->formSubmitted() ){
00183                                 $this->_record =& $tablename;
00184                                 $this->tablename = $this->_record->_table->tablename;
00185                                 $this->_table =& $this->_record->_table;
00186                                 unset($tablename);
00187                                 $tablename = $this->tablename;
00188                         } else {
00189                                 $this->_record =& Dataface_QuickForm::getRecord();
00190                                 $this->tablename = $tablename;
00191                                 $this->_table =& Dataface_Table::loadTable($this->tablename);
00192                         }
00193                                 
00194                 } else  if ( !$new ){
00195                         if ( $tablename == $appQuery['-table'] ){
00196                                 $this->_record =& Dataface_QuickForm::getRecord();
00197                         } else if ( $query ){
00198                                 $this->_record =& df_get_record($tablename, $query);
00199                         }
00200                         if ( !$this->_record ) $this->_record = new Dataface_Record($tablename, array());
00201                         
00202                         $this->tablename = $tablename;
00203                         
00204                         $this->_table =& Dataface_Table::loadTable($this->tablename);
00205                         
00206                         //$tablename = $this->tablename;
00207                 } else {
00208                         $this->tablename = $tablename;
00209                         $this->_table =& Dataface_Table::loadTable($this->tablename, $this->db);
00210                         $this->_record = new Dataface_Record($this->tablename, array());
00211                 }
00212                 
00213                 $this->_new = $new;
00214                 if ( !$formname ) {
00215                         if ( $new ){
00216                                 $formname = "new_".$tablename."_record_form";
00217                         } else {
00218                                 $formname = "existing_".$tablename."_record_form";
00219                         }
00220                 
00221                 }
00222                 if ( !$db and defined('DATAFACE_DB_HANDLE') ){
00223                         
00224                         $db = DATAFACE_DB_HANDLE;
00225                 } else {
00226                         $db = $app->_db;
00227                 }
00228                         
00229                 $this->db = $db;
00230                 $this->_query = is_array($query) ? $query : array();
00231                 // The cursor tells us which record in the dataset we will be editing.
00232                 if ( !isset( $this->_query['-cursor'] ) ){
00233                         $this->_query['-cursor'] = 0;
00234                 }
00235                 
00236                 // Load the results of the query.
00237                 $this->_resultSet =& Dataface_QueryTool::loadResult($tablename, $db, $this->_query);
00238                 
00239                 parent::HTML_QuickForm($formname, 'post', df_absolute_url($_SERVER['PHP_SELF']),'',array('accept-charset'=>$app->_conf['ie']),self::$TRACK_SUBMIT);
00240                 
00241                 //$this->_fields =& $this->_table->fields(false,false,true);
00242                 $this->_fields =& $this->_table->formFields(false,true);
00243                 
00244                 
00245                 //$this->_record = new Dataface_Record($this->_table->tablename, array());
00246                 $this->_renderer = new HTML_QuickForm_Renderer_Dataface($this); //$this->defaultRenderer();
00247                 $this->_renderer->setFormTemplate($this->getFormTemplate());
00248                 $this->_requiredNote = '';
00249                 
00250                 
00251                 if ( is_array($fieldnames) ){
00252                     /*
00253                      * $fieldnames were specified in the parameters.  We will use the provided
00254                      * field names but we must make sure that the fields exist.
00255                      */
00256                         $this->_fieldnames = array();
00257                         foreach ($fieldnames as $fieldname){
00258                                 if ( isset($this->_fields[$fieldname]) ){
00259                                         $this->_fieldnames[] = $fieldname;
00260                                 }
00261                         }
00262                 }
00263                 
00264                 //$this->_build();
00265 
00266                         
00267         }
00268         
00269         
00270         function formSubmitted(){
00271                 return ( isset( $_POST['__keys__']) and isset( $_POST['-table']) );
00272         }
00273         
00274         function &getRecord(){
00275                 
00276                 if ( Dataface_QuickForm::formSubmitted() ){
00277                         $record = new Dataface_Record($_POST['-table'], array());
00278                         $io = new Dataface_IO($_POST['-table']);
00279                         $query = $_POST['__keys__'];
00280                         
00281                         if ( is_array($query) ){
00282                                 foreach ( array_keys($query) as $postKey ){
00283                                         if ( $query[$postKey]{0} != '=' ){
00284                                                 $query[$postKey] = '='.$query[$postKey];
00285                                         }
00286                                 }
00287                         }
00288                         $io->read($query, $record);
00289                         return $record;
00290                 } else {
00291                 
00292                         $app =& Dataface_Application::getInstance();
00293                         
00294                         $qt =& Dataface_QueryTool::loadResult($app->_currentTable);
00295                                 
00296                         $curr =& $qt->loadCurrent();
00297                         
00298                         return $curr;
00299                 }
00300         }
00301         
00302         
00303         
00304         
00305         
00310         function _fillArray( &$arr1, $arr2 ){
00311                 $keys = array_keys($arr2);
00312                 foreach ($keys as $key){
00313                         if ( !isset( $arr1[ $key ] ) ){
00314                                 $arr1[ $key ] = $arr2[ $key ];
00315                         } else if ( is_array( $arr1[ $key ] ) and is_array( $arr2[ $key ] ) ){
00316                                 $this->_fillArray( $arr1[$key], $arr2[$key]);
00317                         }
00318                 }
00319         }
00320         
00321         
00331         function _buildWidget(&$field){
00332                 global $myctr;
00333                 
00334                 if ( func_num_args() > 1 ){
00335                         /*
00336                          *
00337                          * A second argument is present.  It must be a permissions array.
00338                          *
00339                          */
00340                         $permissions = func_get_arg(1);
00341                                 
00342                 } else {
00343                         /*
00344                          *
00345                          * No permissions were specified so we give Global permissions by default.
00346                          *
00347                          */
00348                         $permissions = Dataface_PermissionsTool::ALL();
00349                 }
00350                 
00351                 
00352                 
00353                 $formTool =& Dataface_FormTool::getInstance();
00354                 $el =& $formTool->buildWidget($this->_record, $field, $this, $field['name'], $this->_new, $permissions);
00355                 
00356                 
00357                 return $el;
00358                 
00359         
00360         
00361         }
00362         
00372         function tagFormAndSession(){
00373                 if ( session_id() ){
00374                         
00375                 }
00376         
00377         }
00378         
00379         
00385         function _build(){
00386                 if ( $this->_isBuilt ){
00387                         /*
00388                          *
00389                          * We only need to build the form once.  If it is already build, just return.
00390                          *
00391                          */
00392                         return;
00393                 }
00394                 
00395                 
00396                 /*
00397                  * Now to figure out which fields will be displayed on the form.
00398                  */
00399                 if ($this->_fieldnames === null or !is_array($this->_fieldnames) or count($this->_fieldnames)==0 ){
00400                     /*
00401                      * No fieldnames were explicitly provided (or they were improperly provided
00402                      * so we use all of the fields in the table.
00403                      */
00404                    // if ( isset( $query['--tab'] ) and !$new ){
00405                    //   $flds =& $this->_table->fields(true);
00406                     
00407                    //   $this->_fieldnames = array_keys($flds[$query['--tab']]);
00408                    // } else {
00409                         $this->_fieldnames = array();
00410                         foreach ($this->_fields as $field){
00411                                 if ( isset($this->tab) and ($this->tab != @$field['tab']) /*and ($this->tab != @$group['tab'])*/ ) continue;
00412                                 // If we are using tabs, and this field isn't in the current tab, then
00413                                 // we skip it.
00414                                 $this->_fieldnames[] = $field['name'];
00415                                 //$this->_fieldnames = array_keys($this->_fields);
00416                         }
00417                         //}
00418                 } 
00419                 
00420                 
00421                 
00422                 
00423                 $this->_isBuilt = true;
00424                         // set flag to indicate that the form has already been built.
00425                 
00426                 if ( !$this->_record || !is_a($this->_record, 'Dataface_Record') ){
00427                         return PEAR::raiseError(
00428                                 Dataface_LanguageTool::translate(
00429                                         'Cannot build quickform with no record',
00430                                         'Attempt to build quickform with no record set.'
00431                                 ),
00432                                 E_USER_ERROR
00433                         );
00434                 }
00435                 
00436                 $relationships =& $this->_table->relationships();
00437                         // reference to relationship descriptors for this table.
00438                 
00439                 
00440                 $formTool =& Dataface_FormTool::getInstance();
00441                 $groups = $formTool->groupFields($this->_fields);
00442                 foreach ( $groups as $sectionName => $fields ){
00443                         unset($group);
00444                         $group =& $this->_record->_table->getFieldgroup($sectionName);
00445                         if ( PEAR::isError($group) ){
00446                                 unset($group);
00447                                 $group = array('label'=>df_translate('scripts.Dataface_QuickForm.LABEL_EDIT_DETAILS', 'Edit Details'), 'order'=>1);
00448                                 
00449                         }
00450                         
00451                         $groupEmpty = true; // A flag to check when the group has at least one element
00452                         if ( !$fields ) continue;
00453                         foreach ( $fields as $field){
00454                                 if ( !in_array($field['name'], $this->_fieldnames) ) continue;
00455                                 //if ( isset($this->tab) and ($this->tab != @$field['tab']) and ($this->tab != @$group['tab']) ) continue;
00456                                         // If we are using tabs, and this field isn't in the current tab, then
00457                                         // we skip it.
00458                                 
00459                         
00460                                 $name = $field['name'];
00461                                         // reference to field descriptor array.
00462                                 $widget =& $field['widget'];
00463                                         // reference to widget descriptor array
00464                                 $vocabulary = $field['vocabulary'];
00465                                         // reference to field's vocabulary
00466                         
00467                                 /*
00468                                  * 
00469                                  * If the user does not have permission to view this field, we should not generate this widget.
00470                                  *
00471                                  */
00472                                 if ( !Dataface_PermissionsTool::view($this->_record, array('field'=>$name))
00473                                         and !($this->_new and Dataface_PermissionsTool::checkPermission('new',$this->_record->getPermissions(array('field'=>$name))))
00474                                 ){
00475                                         unset($widget);
00476                                         continue;
00477                                 
00478                                 }
00479                                 
00480                                 if ( $groupEmpty ){
00481                                         // This is the first field in the group, so we add a header for the 
00482                                         // group.
00483                                         $headerel =& $this->addElement('header', $group['label'], $group['label']);
00484                                         $headerel->setFieldDef($group);
00485                                         unset($headerel);
00486                                         $groupEmpty = false;
00487                                 }
00488                                 
00489                                 /*
00490                                  *
00491                                  * Build the widget for this field.  Note that we pass the permissions array
00492                                  * to the method to help it know which widget to build.
00493                                  *
00494                                  */
00495                                 $el = $this->_buildWidget($field, $this->_record->getPermissions(array('field'=>$name)));
00496                                 if ( PEAR::isError($el) ){
00497                                         $el->addUserInfo(
00498                                                 df_translate(
00499                                                         'scripts.Dataface.QuickForm._build.ERROR_FAILED_TO_BUILD_WIDGET',
00500                                                         "Failed to build widget for field $name ",
00501                                                         array('name'=>$name,'line'=>0,'file'=>'_')
00502                                                         )
00503                                                 );
00504                                         return $el;
00505                                 }
00506                                 
00507                                 
00508                                         
00509                                 //$this->addElement($el);
00510                                 
00511                                 unset($field);
00512                                 unset($el);
00513                                 unset($widget);
00514                                 
00515                         }
00516                 } // end foreach $groups
00517                 /*
00518                  *
00519                  * We need to add elements to the form to specifically store the keys for the current
00520                  * record.  These elements should not be changeable by the user as they are used upon 
00521                  * submission to find out which record is currently being updated.  We will store
00522                  * the keys for this record in a group of hidden fields where a key named "ID" would 
00523                  * be stored in a hidden field as follows:
00524                  * <input type="hidden" name="__keys__[ID]" value="10"/>  (assuming the value of the ID field for this record is 10)
00525                  *
00526                  */
00527                 $factory = new HTML_QuickForm('factory');
00528                         // a dummy quickform object to be used tgo create elements.
00529                 $keyEls = array();
00530                         // 
00531                 $keyDefaults = array();
00532                 foreach ( array_keys($this->_table->keys()) as $key ){
00533                         $keyEls[] = $factory->addElement('hidden', $key);
00534                         
00535                 }
00536                 $this->addGroup($keyEls,'__keys__');
00537                 
00538                 /*
00539                  *
00540                  * We add a field to flag whether or not we are creating a new record.
00541                  * This does not mean that we are always creating a new record.  That will
00542                  * depend on the value that is placed in this field as a default.
00543                  *
00544                  */
00545                 $this->addElement('hidden','-new');
00546                 
00547                 $this->setDefaults(array('-new'=>$this->_new));
00548                 if ( ($this->_new and Dataface_PermissionsTool::checkPermission('new',$this->_table) ) or 
00549                      (!$this->_new and Dataface_PermissionsTool::edit($this->_record) ) ){
00550                         $this->addElement('submit','--session:save',df_translate('save_button_label','Save'));
00551                         //$this->addGroup($formTool->createRecordButtons($this->_record, $this->tab));
00552                 }
00553                 
00554                 if ( $this->_new and !$this->overrideNoQuery){
00555                 
00556                         $this->addElement('hidden','--no-query',1);
00557                 }
00558                         // add the submit button.
00559                 
00560                 
00561                 
00562                 
00563                 /*
00564                  *
00565                  * We need to set the default values for this form now.
00566                  *
00567                  */
00568                 
00569                 $keys = $this->getKeys();
00570                         // may not be necessary -- not sure....
00571                         
00572                 if ( $this->isSubmitted() and !$this->_new){
00573                         /*
00574                          *
00575                          * This part is unnecessary because the record is not populated
00576                          * in the Dataface_QuickForm constructor.
00577                          *
00578                          */ 
00579                         $key_vals = $this->exportValues('__keys__');
00580                         $query = $key_vals['__keys__'];
00581                         //$io = new Dataface_IO($this->tablename, $this->db);
00582                         //$io->read($query, $this->_record);
00583                         
00584                 } else if ( !$this->_new ){
00585                         /*
00586                          *
00587                          * The form has not been submitted yet and we are not creating a new
00588                          * record, so we need to populate the form with values from the record.
00589                          *
00590                          */
00591                         foreach ( array_keys($this->_table->keys()) as $key ){
00592                                 $keyDefaults[$key] = $this->_record->strval($key);
00593                                 
00594                         }
00595                         
00596                         $this->setConstants( array('__keys__'=>$keyDefaults) );
00597                         $this->pull();
00598                         
00599                         
00600                 } else { // $this->_new
00601                         $defaults = array();
00602                         foreach ( array_keys($this->_fields) as $key ){
00603                                 $defaultValue = $this->_table->getDefaultValue($key);
00604                                 if ( isset($defaultValue) ){
00605                                         //if ( isset($this->_fields[$key]['group']) and $this->_fields[$key]['group'] ){
00606                                                 
00607                                         //      $defaults[$this->_fields[$key]['group']][$key] = $defaultValue;
00608                                         //} else {
00609                                                 $defaults[$key] = $defaultValue;
00610                                         //}
00611                                 }
00612                         }
00613                         
00614                         $this->setDefaults($defaults);
00615                 }
00616                 
00617         }
00618         
00619         
00620         
00628         function &getElementByFieldName($fieldname){
00629                 $field =& $this->_table->getField($fieldname);
00630                 
00631                 $formTool =& Dataface_FormTool::getInstance();
00632                 $el =& $formTool->getElement($this, $field, $fieldname);
00633                 return $el;
00634         }
00635         
00643         function pullField($fieldname){
00644                 // Step 1: Load references to objects that we will need to use
00645                 $s =& $this->_table;
00646                         // Reference to the table
00647                 $field =& $s->getField($fieldname);
00648                 
00649                 $formTool =& Dataface_FormTool::getInstance();
00650                 $res = $formTool->pullField($this->_record, $field, $this, $fieldname, $this->_new);
00651                 return $res;
00652                 
00653         
00654         }
00655         
00662         function pull(){
00663                 //$fields = array_keys($this->_fields);
00664                 $fields =& $this->_fieldnames;
00665                 foreach ($fields as $field){
00666                         $res = $this->pullField($field);
00667                         if ( PEAR::isError($res) ){
00668                                 continue;
00669                                 
00670                                 
00671                         }
00672                         
00673                 }
00674                 return true;
00675         
00676         }
00677         
00678         
00679         
00690         function push(){
00691                 //$fields = array_keys($this->_fields);
00692                 $fields =& $this->_fieldnames;
00693                 //$ctr = 0;
00694                 foreach ($fields as $field){
00695                         
00696                         $res = $this->pushField($field);
00697                         if ( Dataface_Error::isPermissionDenied($res) ){
00698                                 /*
00699                                  *
00700                                  * The user does not have permission to set this value for this field.
00701                                  * We return an error, that should result in a "PERMISSION DENIED" page if
00702                                  * if is propogated up properly.
00703                                  *
00704                                  */
00705                                 return $res;
00706                         }
00707                         if (PEAR::isError($res) ){
00708                                 
00709                                 
00710                                 
00711                                 continue;
00712                                 $res->addUserInfo(
00713                                         df_translate(
00714                                                 'scripts.Dataface.QuickForm.push.ERROR_PUSHING_DATA',
00715                                                 "Error pushing data onto field $field in QuickForm::push()",
00716                                                 array('field'=>$field,'line'=>0,'file'=>'_')
00717                                                 )
00718                                         );
00719                                 throw new Exception($res->toString(), E_USER_ERROR);
00720                                 
00721                         }
00722                 }
00723                 
00724                 return true;
00725         
00726         }
00727         
00728         
00735          function validate(){
00736                 $this->_build();
00737                 //$this->push();
00738                 if ( $this->isSubmitted() ){
00739                         $app =& Dataface_Application::getInstance();
00740                         $res = $app->fireEvent('Dataface_QuickForm_before_validate');
00741                         if ( PEAR::isError($res) ){
00742                                 
00743                                 $this->_errors[] = $res->getMessage();
00744                         }
00745                         /*
00746                          *
00747                          * We only need to validate if the form was submitted.
00748                          *
00749                          */
00750                         //foreach ( array_keys($this->_fields) as $field ){
00751                         $rec = new Dataface_Record($this->_record->_table->tablename, $this->getSubmitValues());
00752                         $rec->pouch = $this->_record->pouch;
00753                         foreach ($this->_fieldnames as $field){
00754                                 /*
00755                                  *
00756                                  * Go through each field (corresponding to a record field) in the form
00757                                  * and validate against the record's validation script.
00758                                  *
00759                                  */
00760                                 $el =& $this->getElementByFieldName($field);
00761                                 if ( PEAR::isError($el) ){
00762                                         unset($el);
00763                                         continue;
00764                                 }
00765                                 
00766                                 
00767                                 $params = array('message'=>df_translate('scripts.GLOBAL.MESSAGE.PERMISSION_DENIED',"Permission Denied"));
00768                                         // default error message to be displayed beside the field.
00769                                 
00770                                 $res = $rec->validate($field, $el->getValue(), $params );
00771                                 if ( !$res){
00772                                         /*
00773                                          *
00774                                          * The default validate() method checks to see if the form validates based
00775                                          * on the size of the _errors array.  (If it has count = 0, then it validates.
00776                                          * Adding an error to this array will cause the parent's validate method to return
00777                                          * false.
00778                                          *
00779                                          */
00780                                          
00781                                         $this->_errors[$el->getName()] = $params['message'];
00782                                 }
00783                                 
00784                                 
00785                                 unset($params);
00786                         }
00787                 }
00788                 
00789                 
00790                 
00791                 /*
00792                  *
00793                  * Now that we have done our work, we can let the default validate method do the rest
00794                  * of the work.
00795                  *
00796                  */
00797                 return parent::validate();
00798                 
00799          }
00800         
00807         function pushField($fieldname){
00808                 
00809                 $formTool =& Dataface_FormTool::getInstance();
00810                 $field =& $this->_table->getField($fieldname);
00811                 
00812                 $res = $formTool->pushField($this->_record, $field, $this, $fieldname, $this->_new);
00813                 
00814                 return $res;
00815                 
00816         }
00817         
00822         function pullValue($fieldname){
00823                 $fieldname = $this->_formatFieldName($fieldname);
00824         
00825         
00826         }
00827         
00831         function pushValue($fieldname, &$metaValues, $element=null){
00832         
00833                 $formTool =& Dataface_FormTool::getInstance();
00834                 $field =& $this->_table->getField($fieldname);
00835                 if ( !isset($element) ) $element =& $formTool->getElement($this, $field, $fieldname);
00836 
00837                 $res = $formTool->pushValue($this->_record, $field, $this, $element, $metaValues);
00838                 return $res;                    
00839         
00840         }
00841         
00842         
00843         
00844         
00845         function display(){
00846                 if ( $this->_resultSet->found()>0 || $this->_new ){
00847                         $res = $this->_build();
00848                         if ( PEAR::isError($res) ){
00849                                 return $res;
00850                         }
00851                         else {
00852                                 //$this->displayTabs();
00853                                 if ( !$this->_new and !Dataface_PermissionsTool::edit($this->_record) ){
00854                                         $this->freeze();
00855                                 }
00856                                 
00857                                 if ( $this->_new  and !Dataface_PermissionsTool::checkPermission('new',$this->_table) ){
00858                                         $this->freeze();
00859                                 }
00860                                 $formTool =& Dataface_FormTool::getInstance();
00861                                 
00862                                 
00863                                 if ( $this->_new || Dataface_PermissionsTool::view($this->_record) ){
00864                                         //echo $this->_renderer->toHtml();
00865                                         echo $formTool->display($this);
00866                                 } else {
00867                                         echo "<p>".df_translate('scripts.GLOBAL.INSUFFICIENT_PERMISSIONS_TO_VIEW_RECORD','Sorry you have insufficient permissions to view this record.')."</p>";
00868                                 }
00869                                 //parent::display();
00870                         }
00871                 } else {
00872                         echo "<p>".df_translate('scripts.GLOBAL.NO_RECORDS_MATCHED_REQUEST','No records matched your request.')."</p>";
00873                 }
00874         }
00875         
00876         
00877         function displayTabs(){
00878                 
00879                 if ( isset($this->_record) ){
00880                         echo "<ul id=\"quick-form-tabs\">";
00881                         foreach ( $this->_record->_table->getTabs() as $tab ){
00882                                 echo "<li><a href=\"".$this->app->url('-tab='.$tab)."\">".$tab."</a></li>";
00883                         }
00884                         echo "</ul>";
00885                 }
00886         }
00887         
00888         function getElementTemplate($fieldName=''){
00889                 $fieldname = $this->_formatFieldName($fieldname);
00890                 
00891                 if ( $fieldName && isset($this->_fields[$fieldName]) && isset($this->_fields[$fieldName]['widget']['description']) ){
00892                         $widget = $this->_fields[$fieldName]['widget'];
00893                         $description = $widget['description'];
00894                         
00895                 } else {
00896                         $description = '';
00897                 }
00898                         
00899                 $o = "<div class=\"field\">
00900                         <label>{label}</label>
00901                         <!-- BEGIN required --><span style=\"color: #ff0000\" class=\"fieldRequired\" title=\"required\">*</span><!-- END required -->
00902                         <!-- BEGIN error --><div class=\"fieldError\" style=\"color: #ff0000\">{error}</div><!-- END error -->
00903                         <div class=\"formHelp\">$description</div>
00904                         {element}
00905                         </div>
00906                         ";
00907                 return $o;
00908         
00909         }
00910         
00911         function getFormTemplate(){
00912                 $atts =& $this->_table->attributes();
00913                 $formname = $this->getAttribute('name');
00914                 return <<<END
00915                         <script language="javascript" type="text/javascript"><!--
00916                         function Dataface_QuickForm(){
00917                                 
00918                         }
00919                         Dataface_QuickForm.prototype.setFocus = function(element_name){
00920                                 document.{$formname}.elements[element_name].focus();
00921                                 document.{$formname}.elements[element_name].select();
00922                         }
00923                         var quickForm = new Dataface_QuickForm();
00924                         //--></script>
00925                 
00926                                 <form{attributes}>
00927                                         <fieldset>
00928                                         <legend>{$atts['label']}</legend>
00929                                         <table width="100%" class="Dataface_QuickForm-table-wrapper">
00930                                         
00931                                         {content}
00932                                         </table>
00933                                         </fieldset>
00934                                 </form>
00935 END;
00936         }
00937         
00938         function getFieldGroupTemplate($name){
00939                 $name = $this->_formatFieldName($name);
00940                 $group =& $this->_table->getFieldgroup($name);
00941                 
00942                 
00943                 $o = "<tr><td colspan=\"2\"><fieldset class=\"fieldgroup\" style=\"border: 1px solid #8cacbb; margin: 0.5em;\">
00944                         <legend>".$group['label']."</legend>
00945                         <div class=\"formHelp\">".$group['description']."</div>
00946                         <table width=\"100%\" border=\"0\" class=\"Dataface_QuickForm-group-table-wrapper\">
00947                         {content}
00948                         </table>
00949                         
00950                         </fieldset></td></tr>";
00951                 return $o;
00952         }
00953         
00954         function getGroupTemplate($name){
00955                 $name = $this->_formatFieldName($name);
00956                 $group =& $this->_table->getField($name);
00957                 
00958                 $context = array( 'group'=>&$group, 'content'=>'{content}');
00959                 $skinTool =& Dataface_SkinTool::getInstance();
00960                 ob_start();
00961                 $skinTool->display($context, 'Dataface_Quickform_group.html');
00962                 $o = ob_get_contents();
00963                 ob_end_clean();
00964                 
00965                 return $o;
00966         }
00967         
00968         function getGroupElementTemplate($groupName=''){
00969                 $groupName = $this->_formatFieldName($groupName);
00970                 $group =& $this->_table->getFieldgroup($groupName);
00971                 if ( PEAR::isError($group) ){
00972                         $group->addUserInfo(
00973                                 df_translate(
00974                                         'scripts.Dataface.QuickForm.getGroupElementTemplate.ERROR_GETTING_FIELD_GROUP',
00975                                         "Error getting field group '$groupName' in QuickForm::getGroupElementTemplate() ",
00976                                         array('groupname'=>$groupName,'line'=>0,'file'=>'_')
00977                                         )
00978                                 );
00979                         return $group;
00980                 }
00981                 
00982                 $description = '';
00983                 $label = '';
00984                 
00985                 if ( $group['element-description-visible'] ){
00986                         $description = "<div class=\"formHelp\" style=\"display: inline\">".$group['description']."</div>";
00987                 }
00988                 
00989                 if ( $group['element-lavel-visible'] ){
00990                         $label = '';
00991                 }
00992                 
00993                 if ( $fieldName && isset($this->_fields[$fieldName]) && isset($this->_fields[$fieldName]['widget']['description']) ){
00994                         $widget = $this->fields[$fieldName]['widget'];
00995                         $description = $widget['description'];
00996                         
00997                 } else {
00998                         $description = '';
00999                 }
01000                         
01001                 $o = "<div class=\"field\">
01002                         <label>{label}</label>
01003                         <!-- BEGIN required --><span style=\"color: #ff0000\" class=\"fieldRequired\" title=\"required\">*</span><!-- END required -->
01004                         <!-- BEGIN error --><div class=\"fieldError\" style=\"color: #ff0000\">{error}</div><!-- END error -->
01005                         <div class=\"formHelp\">$description</div>
01006                         {element}
01007                         </div>
01008                         ";
01009                         
01010                 return $o;
01011         
01012         }
01013         
01014         function save( $values ){
01015                 
01016                 // First let's find out if we should SAVE the data or if we should just be
01017                 // storing it in the session or if we are saving the data to the database
01018                 
01019                 
01020                 if (!$this->_new){
01021                         // Make sure that the correct form is being submitted.  
01022                         if ( !isset( $values['__keys__'] ) ){
01023                                 throw new Exception(
01024                                         df_translate(
01025                                                 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
01026                                                 "Error saving record in QuickForm::save().\n<br>"
01027                                                 ), E_USER_ERROR);
01028                         }
01029                         if ( array_keys($values['__keys__']) != array_keys($this->_table->keys()) ){
01030                                 throw new Exception(
01031                                         df_translate(
01032                                                 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
01033                                                 "Error saving record in QuickForm::save().\n<br>"
01034                                                 ), E_USER_ERROR);
01035                         }
01036                 }
01037 
01038                 if ( $this->_new ){
01039 
01040                         $this->_record->clearValues();
01041                 }
01042                 
01043                 $res = $this->push();
01044                 
01045                 if ( !$this->_new ){
01046                         if ( $this->_record->snapshotExists() ){
01047                                 
01048                                 $tempRecord = new Dataface_Record($this->_record->_table->tablename, $this->_record->getSnapshot());
01049                         } else {
01050                                 $tempRecord =& $this->_record;
01051                         }
01052                         if ( $values['__keys__'] != $tempRecord->strvals(array_keys($this->_record->_table->keys())) ){
01053                                 throw new Exception(
01054                                         df_translate(
01055                                                 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
01056                                                 "Error saving record in QuickForm::save().\n<br>"
01057                                                 ), E_USER_ERROR);
01058                         }
01059                 }
01060                 
01061                 
01062                 if (PEAR::isError($res) ){
01063                         
01064                         
01065                         $res->addUserInfo(
01066                                 df_translate(
01067                                         'scripts.Dataface.QuickForm.save.ERROR_PUSHING_DATA',
01068                                         "Error pushing data from form onto table in QuickForm::save() ",
01069                                         array('line'=>0,'file'=>"_")
01070                                         )
01071                                 );
01072                         
01073                         
01074                         return $res;
01075                 }
01076 
01077                 
01078                 // Let's take an inventory of which fields were changed.. because
01079                 // we are going to make their values available in the htmlValues()
01080                 // method which is used by the ajax form to gather updates.
01081                 foreach ( $this->_fields as $changedfield ){
01082                         if ( $this->_record->valueChanged($changedfield['name']) ){
01083                                 $this->_changed_fields[] = $changedfield['name'];
01084                         }
01085                 }
01086         
01087                 $io = new Dataface_IO($this->tablename, $this->db);
01088                 $io->lang = $this->_lang;
01089                 if ( $this->_new ) $keys = null;
01090                 else $keys = $values['__keys__'];
01091 
01092                 $res = $io->write($this->_record,$keys,null,true /*Adding security!!!*/);
01093                 if ( PEAR::isError($res) ){
01094                         if ( Dataface_Error::isDuplicateEntry($res) ){
01095                                 /*
01096                                  * If this is a duplicate entry (or just a notice - not fatal), we will propogate the exception up to let the application
01097                                  * decide what to do with it.
01098                                  */
01099                                 return $res;
01100                         } 
01101                         if ( Dataface_Error::isNotice($res) ){
01102                                 return $res;
01103                         } 
01104                 
01105                         $res->addUserInfo(
01106                                 df_translate(
01107                                         'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
01108                                         "Error saving form in QuickForm::save()",
01109                                         array('line'=>0,'file'=>"_")
01110                                         )
01111                                 );
01112                         throw new Exception($res->toString(), E_USER_ERROR);
01113                         
01114                 }
01115                 
01116                 
01117                 
01118                 if ( isset( $io->insertIds[$this->tablename]) and $this->_table->getAutoIncrementField() ){
01119                         $this->_record->setValue($this->_table->getAutoIncrementField(), $io->insertIds[$this->tablename]);
01120                         $this->_record->setSnapshot();
01121 
01122                 } 
01123                 
01124                 return true;
01125                 
01126                 
01127 
01128         }
01129                 
01130         
01134         function getKeys(){
01135                 $keys = array();
01136                 foreach ($this->_fields as $key=>$value){
01137                         if ( strtolower($value['Key']) == strtolower('PRI') ){
01138                                 $keys[$key] =& $this->_fields[$key];
01139                         }
01140                 }
01141                 return $keys;
01142         }
01143         
01144         
01148         function deserialize($field){
01149                 return Dataface_Table::_deserialize($field);
01150                 
01151                                         
01152         }
01153         
01154         
01158         function serialize($field){
01159         
01160                 return Dataface_Table::_serialize($field);
01161                 
01162                 
01163                 
01164         }
01165         
01169         function _formatFieldName($fieldname){
01170                 return $fieldname;
01171                 //return str_replace(':','.', $fieldname);
01172         }
01173         
01174         
01199         public static function &createNewRecordForm($tablename, $fieldnames=null){
01200         
01201                 $form = new Dataface_QuickForm($tablename, '','','',true, $fieldnames);
01202                 return $form;
01203         
01204         
01205         }
01206         
01233         public static function &createEditRecordForm(&$tablenameOrRecord, $fieldnames=null){
01234                 $form = new Dataface_QuickForm($tablenameOrRecord, '','','',false,$fieldnames);
01235                 return $form;
01236         }
01237         
01238         function htmlValues(){
01239                 $vals = array();
01240                 $record =& $this->_record;
01241                 foreach ($this->_changed_fields as $fieldname){
01242                         $vals[$fieldname] = $record->htmlValue($fieldname);
01243                 }
01244                 $vals['__id__'] = $record->getId();
01245                 $vals['__url__'] = $record->getURL('-action=view');
01246                 return $vals;
01247         }
01248         
01249 }
 All Data Structures Namespaces Files Functions Variables Enumerations