Xataface 2.0
Xataface Application Framework
Dataface/FormTool/select.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class Dataface_FormTool_select {
00007         function &buildWidget(&$record, &$field, &$form, $formFieldName, $new=false){
00008                 $table =& $record->_table;
00009                 
00010                 $widget =& $field['widget'];
00011                 $factory = Dataface_FormTool::factory();
00012                 $attributes = array('class'=>$widget['class'], 'id'=>$field['name']);
00013                 if ( $field['repeat'] ){
00014                         $attributes['multiple'] = true;
00015                         $attributes['size'] = 5;
00016                 }
00017                 $options = $record->_table->getValuelist($field['vocabulary']);//Dataface_FormTool::getVocabulary($record, $field);
00018                 if ( !isset( $options) ) $options = array();
00019                 $emptyOpt = array(''=>df_translate('scripts.GLOBAL.FORMS.OPTION_PLEASE_SELECT',"Please Select..."));
00020                 $opts = $emptyOpt;
00021                 if ( $record and $record->val($field['name']) ){
00022                         if ( !@$field['repeat'] and !isset($options[$record->strval($field['name'])]) ){
00023                                 $opts[$record->strval($field['name'])] = $record->strval($field['name']);
00024                         } else if ( @$field['repeat'] ){
00025                         
00026                                 $vals = $record->val($field['name']);
00027                                 if ( is_array($vals) ){
00028                                         foreach ( $vals as $thisval){
00029                                                 if ( !isset($options[$thisval]) ){
00030                                                         $opts[$thisval] = $thisval;
00031                                                 }
00032                                         }
00033                                 }
00034                         }
00035                 }
00036                 foreach($options as $kopt=>$opt){
00037                         $opts[$kopt] = $opt;
00038                 }
00039                 
00040                 $el =  $factory->addElement('select', $formFieldName, $widget['label'], $opts, $attributes  );
00041                 
00042                 // Now to make it editable
00043                 if ( @$field['vocabulary'] ){
00044                         try {
00045                                 $rel =& Dataface_ValuelistTool::getInstance()->asRelationship($table, $field['vocabulary']);
00046                                 if ( $rel and !PEAR::isError($rel) ){
00047                                         $dtable = Dataface_Table::loadTable($rel->getDomainTable());
00048                                         if ( $dtable and !PEAR::isError($dtable) ){
00049                                                 $perms = $dtable->getPermissions();
00050                                                 if ( @$perms['new'] ){
00051                                                         $fields =& $rel->fields();
00052                                                         if ( count($fields) > 1 ) {
00053                                                                 $valfield = $fields[1];
00054                                                                 $keyfield = $fields[0];
00055                                                         }
00056                                                         else {
00057                                                                 $valfield = $fields[0];
00058                                                                 $keyfield = $fields[0];
00059                                                         }
00060                                                         if ( strpos($valfield, '.') !== false ) list($tmp, $valfield) = explode('.', $valfield);
00061                                                         if ( strpos($keyfield, '.') !== false ) list($tmp, $keyfield) = explode('.', $keyfield);
00062                                                 
00063                                                         $suffix =  '<script type="text/javascript" src="'.DATAFACE_URL.'/js/jquery-ui-1.7.2.custom.min.js"></script>';
00064                                                         $suffix .= '<script type="text/javascript" src="'.DATAFACE_URL.'/js/RecordDialog/RecordDialog.js"></script>';
00065                                                         $suffix .= '<a href="#" onclick="return false" id="'.htmlspecialchars($field['name']).'-other">Other..</a>';
00066                                                         $suffix .= '<script>
00067                                                         jQuery(\'head\').append(\'<link rel="stylesheet" type="text/css" href="\'+DATAFACE_URL+\'/css/smoothness/jquery-ui-1.7.2.custom.css"/>\');
00068                                                         jQuery(document).ready(function($){
00069                                                                 $("#'.$field['name'].'-other").each(function(){
00070                                                                         var tablename = "'.addslashes($dtable->tablename).'";
00071                                                                         var valfld = '.json_encode($valfield).';
00072                                                                         var keyfld = '.json_encode($keyfield).';
00073                                                                         var fieldname = '.json_encode($field['name']).';
00074                                                                         var btn = this;
00075                                                                         $(this).RecordDialog({
00076                                                                                 table: tablename,
00077                                                                                 callback: function(data){
00078                                                                                         var key = data[keyfld];
00079                                                                                         var val = data[valfld];
00080                                                                                         $("#"+fieldname).append(\'<option value="\'+key+\'">\'+val+\'</option>\');
00081                                                                                         $("#"+fieldname).val(key);
00082                                                                                         
00083                                                                                 }
00084                                                                         });
00085                                                                 });
00086                                                         });
00087                                                         </script>
00088                                                         ';
00089                                                         $widget['suffix'] = $suffix;
00090                                                 }
00091                                         }
00092                                         
00093                                         
00094                                 }
00095                         } catch (Exception $ex){
00096                                 error_log($ex->getMessage());
00097                         }
00098                 }
00099                 
00100                 //$el->setFieldDef($field);
00101                 //return $el;
00102                 return $el;
00103         }
00104         
00105         function pushValue(&$record, &$field, &$form, &$element, &$metaValues){
00106                 // quickform stores select fields as arrays, and the table schema will only accept 
00107                 // array values if the 'repeat' flag is set.
00108                 $table =& $record->_table;
00109                 $formTool =& Dataface_FormTool::getInstance();
00110                 //$formFieldName =& $element->getName();
00111                 
00112                 if ( !$field['repeat'] ){
00113                 
00114                         $val = $element->getValue();
00115                         
00116                         if ( count($val)>0 ){
00117                                 return $val[0];
00118                                 
00119                         } else {
00120                                 return null;
00121                                 
00122                         }
00123                 } else {
00124                         return $element->getValue();
00125                 }
00126                         
00127                 
00128                 
00129         }
00130         
00131         
00132 }
 All Data Structures Namespaces Files Functions Variables Enumerations