![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00005 class Dataface_FormTool_checkbox { 00006 function &buildWidget(&$record, &$field, &$form, $formFieldName, $new=false){ 00007 $table =& $record->_table; 00008 $widget =& $field['widget']; 00009 00010 if ( !@$widget['separator'] ) $widget['separator'] = '<br />'; 00011 $factory =& Dataface_FormTool::factory(); 00012 if ( (isset( $field['repeat']) and $field['repeat'] and isset($field['vocabulary']) and $field['vocabulary']) or 00013 (isset($field['transient']) and isset($field['relationship']) )){ 00014 $boxes = array(); 00015 $options = array(); 00016 if ( @$field['vocabulary'] ){ 00017 $options =& Dataface_FormTool::getVocabulary($record, $field); 00018 $options__classes = Dataface_FormTool::getVocabularyClasses($record, $field); 00019 } else if ( isset($field['relationship']) ){ 00020 $relationship =& $record->_table->getRelationship($field['relationship']); 00021 $options = $relationship->getAddableValues($record); 00022 $options__classes = array(); 00023 00024 // Now let's add the ability to add an option that isn't already there 00025 // but only if the user has permission 00026 if ( !@$widget['suffix'] ) $widget['suffix'] = ''; 00027 $dtable = & Dataface_Table::loadTable($relationship->getDomainTable()); 00028 if ( !PEAR::isError($dtable) and $record->checkPermission('add new related record', array('relationship'=>$relationship->getName()) )){ 00029 00030 00031 $suffix = '<script type="text/javascript" src="'.DATAFACE_URL.'/js/jquery-ui-1.7.2.custom.min.js"></script>'; 00032 $suffix .= '<script type="text/javascript" src="'.DATAFACE_URL.'/js/RecordDialog/RecordDialog.js"></script>'; 00033 $suffix .= '<a href="#" onclick="return false" id="'.htmlspecialchars($field['name']).'-other">Other..</a>'; 00034 $suffix .= '<script> 00035 $(\'head\').append(\'<link rel="stylesheet" type="text/css" href="\'+DATAFACE_URL+\'/css/smoothness/jquery-ui-1.7.2.custom.css"/>\'); 00036 jQuery(document).ready(function($){ 00037 $("#'.$field['name'].'-other").each(function(){ 00038 var tablename = "'.addslashes($dtable->tablename).'"; 00039 var fldname = "'.addslashes(htmlspecialchars($field['name'])).'"; 00040 var keys = '.json_encode(array_keys($dtable->keys())).'; 00041 var btn = this; 00042 $(this).RecordDialog({ 00043 table: tablename, 00044 callback: function(data){ 00045 var val = []; 00046 for ( var i=0; i<keys.length; i++){ 00047 val.push(encodeURIComponent(keys[i])+\'=\'+encodeURIComponent(data[keys[i]])); 00048 } 00049 val = val.join(\'&\'); 00050 fldname = tablename+\'[\'+val+\']\'; 00051 00052 00053 $(btn).before(\'<input type="checkbox" name="\'+fldname+\'" value="\'+val+\'" checked="1"/>\'+data["__title__"]+\'<br/>\'); 00054 } 00055 }); 00056 }); 00057 }); 00058 </script> 00059 '; 00060 $widget['suffix'] = $suffix; 00061 } 00062 } 00063 00064 00065 if ( $record and $record->val($field['name']) ){ 00066 $vals = $record->val($field['name']); 00067 if ( is_array($vals) ){ 00068 foreach ( $vals as $thisval){ 00069 if ( !isset($options[$thisval]) ){ 00070 $options[$thisval] = $thisval; 00071 } 00072 } 00073 } 00074 00075 } 00076 $dummyForm = new HTML_QuickForm(); 00077 foreach ($options as $opt_val=>$opt_text){ 00078 if ( !$opt_val ) continue; 00079 $boxes[] =& $dummyForm->createElement('checkbox',$opt_val , null, $opt_text, array('class'=>'checkbox-of-'.$field['name'].' '.@$options__classes[$opt_val])); 00080 //$boxes[count($boxes)-1]->setValue($opt_val); 00081 00082 } 00083 $el =& $factory->addGroup($boxes, $field['name'], $widget['label']); 00084 00085 } else { 00086 00087 00088 00089 $el =& $factory->addElement('advcheckbox', $formFieldName, $widget['label']); 00090 if ( $field['vocabulary'] ){ 00091 $yes = ''; 00092 $no = ''; 00093 if ( $table->isYesNoValuelist($field['vocabulary'], $yes, $no) ){ 00094 $el->setValues(array($no,$yes)); 00095 } 00096 } 00097 } 00098 return $el; 00099 } 00100 00101 function &pushValue(&$record, &$field, &$form, &$element, &$metaValues){ 00102 $table =& $record->_table; 00103 $formTool =& Dataface_FormTool::getInstance(); 00104 $formFieldName = $element->getName(); 00105 00106 $val = $element->getValue(); 00107 if ( $field['repeat'] ){ 00108 00109 //print_r(array_keys($val)); 00110 // eg value array('value1'=>1, 'value2'=>1, ..., 'valueN'=>1) 00111 if ( is_array($val) ){ 00112 $out = array_keys($val); 00113 } else { 00114 $out = array(); 00115 } 00116 //$res =& $s->setValue($fieldname, array_keys($val)); 00117 } else { 00118 if ( preg_match('/int/', @$field['Type']) ){ 00119 $out = intval($val); 00120 } else { 00121 $out = $val; 00122 } 00123 //$res =& $s->setValue($fieldname, $val); 00124 } 00125 if (PEAR::isError($val) ){ 00126 $val->addUserInfo( 00127 df_translate( 00128 'scripts.Dataface.QuickForm.pushValue.ERROR_PUSHING_VALUE', 00129 "Error pushing value for field '$field[name]' in QuickForm::pushWidget() on line ".__LINE__." of file ".__FILE__, 00130 array('name'=>$field['name'],'file'=>__FILE__,'line'=>__LINE__) 00131 ) 00132 ); 00133 return $val; 00134 } 00135 return $out; 00136 } 00137 00138 function pullValue(&$record, &$field, &$form, &$element, $new=false){ 00139 00140 /* 00141 * 00142 * Checkbox widgets store values as associative array $a where 00143 * $a[$x] == 1 <=> element named $x is checked. 00144 * Note: See _buildWidget() for information about how the checkbox widget is 00145 * created. It is created differently for repeat fields than it is for individual 00146 * fields. For starters, individual fields are advcheckbox widgets, whereas 00147 * repeat fields are just normal checkbox widgets. 00148 * 00149 */ 00150 $formFieldName = $element->getName(); 00151 $raw =& $record->getValue($field['name']); 00152 if ( $field['repeat'] and is_array($raw)){ 00153 // If the field is a repeat field $raw will be an array of 00154 // values. 00155 $v = array(); 00156 foreach ($raw as $key=>$value){ 00157 $v[$value] = 1; 00158 } 00159 /* 00160 * 00161 * In this case we set this checkbox to the array of values that are currently checked. 00162 * 00163 */ 00164 $val = $v; 00165 } else { 00166 /* 00167 * 00168 * If the field is not a repeat, then it is only one value 00169 * 00170 */ 00171 $val = $record->getValueAsString($field['name']); 00172 } 00173 00174 00175 return $val; 00176 } 00177 }