Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
checkbox.php
Go to the documentation of this file.
1 <?php
6  function &buildWidget(&$record, &$field, &$form, $formFieldName, $new=false){
7  $table =& $record->_table;
8  $widget =& $field['widget'];
9 
10  if ( !@$widget['separator'] ) $widget['separator'] = '<br />';
11  $factory =& Dataface_FormTool::factory();
12  if ( (isset( $field['repeat']) and $field['repeat'] and isset($field['vocabulary']) and $field['vocabulary']) or
13  (isset($field['transient']) and isset($field['relationship']) )){
14  $boxes = array();
15  $options = array();
16  if ( @$field['vocabulary'] ){
17  $options =& Dataface_FormTool::getVocabulary($record, $field);
18  $options__classes = Dataface_FormTool::getVocabularyClasses($record, $field);
19  } else if ( isset($field['relationship']) ){
20  $relationship =& $record->_table->getRelationship($field['relationship']);
21  $options = $relationship->getAddableValues($record);
22  $options__classes = array();
23 
24  // Now let's add the ability to add an option that isn't already there
25  // but only if the user has permission
26  if ( !@$widget['suffix'] ) $widget['suffix'] = '';
27  $dtable = & Dataface_Table::loadTable($relationship->getDomainTable());
28  if ( !PEAR::isError($dtable) and $record->checkPermission('add new related record', array('relationship'=>$relationship->getName()) )){
29 
30 
31  $suffix = '<script type="text/javascript" src="'.DATAFACE_URL.'/js/jquery-ui-1.7.2.custom.min.js"></script>';
32  $suffix .= '<script type="text/javascript" src="'.DATAFACE_URL.'/js/RecordDialog/RecordDialog.js"></script>';
33  $suffix .= '<a href="#" onclick="return false" id="'.htmlspecialchars($field['name']).'-other">Other..</a>';
34  $suffix .= '<script>
35  $(\'head\').append(\'<link rel="stylesheet" type="text/css" href="\'+DATAFACE_URL+\'/css/smoothness/jquery-ui-1.7.2.custom.css"/>\');
36  jQuery(document).ready(function($){
37  $("#'.$field['name'].'-other").each(function(){
38  var tablename = "'.addslashes($dtable->tablename).'";
39  var fldname = "'.addslashes(htmlspecialchars($field['name'])).'";
40  var keys = '.json_encode(array_keys($dtable->keys())).';
41  var btn = this;
42  $(this).RecordDialog({
43  table: tablename,
44  callback: function(data){
45  var val = [];
46  for ( var i=0; i<keys.length; i++){
47  val.push(encodeURIComponent(keys[i])+\'=\'+encodeURIComponent(data[keys[i]]));
48  }
49  val = val.join(\'&\');
50  fldname = tablename+\'[\'+val+\']\';
51 
52 
53  $(btn).before(\'<input type="checkbox" name="\'+fldname+\'" value="\'+val+\'" checked="1"/>\'+data["__title__"]+\'<br/>\');
54  }
55  });
56  });
57  });
58  </script>
59  ';
60  $widget['suffix'] = $suffix;
61  }
62  }
63 
64 
65  if ( $record and $record->val($field['name']) ){
66  $vals = $record->val($field['name']);
67  if ( is_array($vals) ){
68  foreach ( $vals as $thisval){
69  if ( !isset($options[$thisval]) ){
70  $options[$thisval] = $thisval;
71  }
72  }
73  }
74 
75  }
76  $dummyForm = new HTML_QuickForm();
77  foreach ($options as $opt_val=>$opt_text){
78  if ( !$opt_val ) continue;
79  $boxes[] =& $dummyForm->createElement('checkbox',$opt_val , null, $opt_text, array('class'=>'checkbox-of-'.$field['name'].' '.@$options__classes[$opt_val]));
80  //$boxes[count($boxes)-1]->setValue($opt_val);
81 
82  }
83  $el =& $factory->addGroup($boxes, $field['name'], $widget['label']);
84 
85  } else {
86 
87 
88 
89  $el =& $factory->addElement('advcheckbox', $formFieldName, $widget['label']);
90  if ( $field['vocabulary'] ){
91  $yes = '';
92  $no = '';
93  if ( $table->isYesNoValuelist($field['vocabulary'], $yes, $no) ){
94  $el->setValues(array($no,$yes));
95  }
96  }
97  }
98  return $el;
99  }
100 
101  function &pushValue(&$record, &$field, &$form, &$element, &$metaValues){
102  $table =& $record->_table;
103  $formTool =& Dataface_FormTool::getInstance();
104  $formFieldName = $element->getName();
105 
106  $val = $element->getValue();
107  if ( $field['repeat'] ){
108 
109  //print_r(array_keys($val));
110  // eg value array('value1'=>1, 'value2'=>1, ..., 'valueN'=>1)
111  if ( is_array($val) ){
112  $out = array_keys($val);
113  } else {
114  $out = array();
115  }
116  //$res =& $s->setValue($fieldname, array_keys($val));
117  } else {
118  if ( preg_match('/int/', @$field['Type']) ){
119  $out = intval($val);
120  } else {
121  $out = $val;
122  }
123  //$res =& $s->setValue($fieldname, $val);
124  }
125  if (PEAR::isError($val) ){
126  $val->addUserInfo(
127  df_translate(
128  'scripts.Dataface.QuickForm.pushValue.ERROR_PUSHING_VALUE',
129  "Error pushing value for field '$field[name]' in QuickForm::pushWidget() on line ".__LINE__." of file ".__FILE__,
130  array('name'=>$field['name'],'file'=>__FILE__,'line'=>__LINE__)
131  )
132  );
133  return $val;
134  }
135  return $out;
136  }
137 
138  function pullValue(&$record, &$field, &$form, &$element, $new=false){
139 
140  /*
141  *
142  * Checkbox widgets store values as associative array $a where
143  * $a[$x] == 1 <=> element named $x is checked.
144  * Note: See _buildWidget() for information about how the checkbox widget is
145  * created. It is created differently for repeat fields than it is for individual
146  * fields. For starters, individual fields are advcheckbox widgets, whereas
147  * repeat fields are just normal checkbox widgets.
148  *
149  */
150  $formFieldName = $element->getName();
151  $raw =& $record->getValue($field['name']);
152  if ( $field['repeat'] and is_array($raw)){
153  // If the field is a repeat field $raw will be an array of
154  // values.
155  $v = array();
156  foreach ($raw as $key=>$value){
157  $v[$value] = 1;
158  }
159  /*
160  *
161  * In this case we set this checkbox to the array of values that are currently checked.
162  *
163  */
164  $val = $v;
165  } else {
166  /*
167  *
168  * If the field is not a repeat, then it is only one value
169  *
170  */
171  $val = $record->getValueAsString($field['name']);
172  }
173 
174 
175  return $val;
176  }
177 }