Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
select.php
Go to the documentation of this file.
1 <?php
2 
7  function &buildWidget(&$record, &$field, &$form, $formFieldName, $new=false){
8  $table =& $record->_table;
9 
10  $widget =& $field['widget'];
11  $factory = Dataface_FormTool::factory();
12  $attributes = array('class'=>$widget['class'], 'id'=>$field['name']);
13  if ( $field['repeat'] ){
14  $attributes['multiple'] = true;
15  $attributes['size'] = 5;
16  }
17  $options = $record->_table->getValuelist($field['vocabulary']);//Dataface_FormTool::getVocabulary($record, $field);
18  if ( !isset( $options) ) $options = array();
19  $emptyOpt = array(''=>df_translate('scripts.GLOBAL.FORMS.OPTION_PLEASE_SELECT',"Please Select..."));
20  $opts = $emptyOpt;
21  if ( $record and $record->val($field['name']) ){
22  if ( !@$field['repeat'] and !isset($options[$record->strval($field['name'])]) ){
23  $opts[$record->strval($field['name'])] = $record->strval($field['name']);
24  } else if ( @$field['repeat'] ){
25 
26  $vals = $record->val($field['name']);
27  if ( is_array($vals) ){
28  foreach ( $vals as $thisval){
29  if ( !isset($options[$thisval]) ){
30  $opts[$thisval] = $thisval;
31  }
32  }
33  }
34  }
35  }
36  foreach($options as $kopt=>$opt){
37  $opts[$kopt] = $opt;
38  }
39 
40  $el = $factory->addElement('select', $formFieldName, $widget['label'], $opts, $attributes );
41 
42  // Now to make it editable
43  if ( @$field['vocabulary'] ){
44  try {
45  $rel =& Dataface_ValuelistTool::getInstance()->asRelationship($table, $field['vocabulary']);
46  if ( $rel and !PEAR::isError($rel) ){
47  if ( !is_a($rel, 'Dataface_Relationship') ){
48  throw new Exception("The relationship object for the vocabulary ".$field['vocabulary']." could not be loaded.");
49 
50  }
51  if ( !$rel->getDomainTable() ){
52  throw new Exception("The relationship object for the vocabulary ".$field['vocabulary']." could not be loaded or the domain table could not be found");
53  }
54  $dtable = Dataface_Table::loadTable($rel->getDomainTable());
55  if ( $dtable and !PEAR::isError($dtable) ){
56  $perms = $dtable->getPermissions();
57  if ( @$perms['new'] ){
58  $fields =& $rel->fields();
59  if ( count($fields) > 1 ) {
60  $valfield = $fields[1];
61  $keyfield = $fields[0];
62  }
63  else {
64  $valfield = $fields[0];
65  $keyfield = $fields[0];
66  }
67  if ( strpos($valfield, '.') !== false ) list($tmp, $valfield) = explode('.', $valfield);
68  if ( strpos($keyfield, '.') !== false ) list($tmp, $keyfield) = explode('.', $keyfield);
70  $jt->import('RecordDialog/RecordDialog.js');
71  //$suffix = '<script type="text/javascript" src="'.DATAFACE_URL.'/js/jquery-ui-1.7.2.custom.min.js"></script>';
72  //$suffix .= '<script type="text/javascript" src="'.DATAFACE_URL.'/js/RecordDialog/RecordDialog.js"></script>';
73  $suffix = '<a href="#" onclick="return false" id="'.htmlspecialchars($field['name']).'-other">Other..</a>';
74  $suffix .= '<script>
75  jQuery(document).ready(function($){
76  $("#'.$field['name'].'-other").each(function(){
77  var tablename = "'.addslashes($dtable->tablename).'";
78  var valfld = '.json_encode($valfield).';
79  var keyfld = '.json_encode($keyfield).';
80  var fieldname = '.json_encode($field['name']).';
81  var btn = this;
82  $(this).RecordDialog({
83  table: tablename,
84  callback: function(data){
85  var key = data[keyfld];
86  var val = data[valfld];
87  $("#"+fieldname).append(\'<option value="\'+key+\'">\'+val+\'</option>\');
88  $("#"+fieldname).val(key);
89 
90  }
91  });
92  });
93  });
94  </script>
95  ';
96  $widget['suffix'] = $suffix;
97  }
98  }
99 
100 
101  }
102  } catch (Exception $ex){
103  error_log($ex->getMessage());
104  }
105  }
106 
107  //$el->setFieldDef($field);
108  //return $el;
109  return $el;
110  }
111 
112  function pushValue(&$record, &$field, &$form, &$element, &$metaValues){
113  // quickform stores select fields as arrays, and the table schema will only accept
114  // array values if the 'repeat' flag is set.
115  $table =& $record->_table;
116  $formTool =& Dataface_FormTool::getInstance();
117  //$formFieldName =& $element->getName();
118 
119  if ( !$field['repeat'] ){
120 
121  $val = $element->getValue();
122 
123  if ( count($val)>0 ){
124  return $val[0];
125 
126  } else {
127  return null;
128 
129  }
130  } else {
131  return $element->getValue();
132  }
133 
134 
135 
136  }
137 
138 
139 }