Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
SortControl.php
Go to the documentation of this file.
1 <?php
4  var $fields;
5  var $prefix;
6  var $table;
8 
9  if ( is_string($fields) ){
11  $fields = array_keys($t->fields(false,true));
12  } else {
14  $query =& $app->getQuery();
15  $t =& Dataface_Table::loadTable($query['-table']);
16  }
17 
18  $this->table =& $t;
19  $this->fields = array();
20 
21  if ( isset($t->_atts['__global__']) ) $globalProps = $t->_atts['__global__'];
22  else $globalProps = array();
23  foreach ( $fields as $field ){
24  $fieldDef =& $t->getField($field);
25  if ( isset($globalProps['sortable']) and !$globalProps['sortable'] and !@$fieldDef['sortable']){
26  continue;
27  } else if ( isset($fieldDef['sortable']) and !@$fieldDef['sortable'] ){
28  continue;
29  }
30  $this->fields[] = $field;
31  }
32 
33  $this->prefix = $prefix;
34  //$this->fields = $fields;
36  $query =& $app->getQuery();
37  if ( !isset($query['-'.$prefix.'sort']) ){
38  $sort = '';
39  } else {
40  $sort = $query['-'.$prefix.'sort'];
41  }
42 
43  $sort = array_map('trim',explode(',', $sort));
44 
45  $sort2 = array();
46  foreach ( $sort as $col ){
47  if ( !trim($col) ) continue;
48  $col = explode(' ',$col);
49 
50  if ( count($col) <= 1 ){
51  $col[1] = 'asc';
52  }
53  $sort2[$col[0]] = $col[1];
54  }
55 
56  // Now sort2 looks like array('col1'=>'asc', 'col2'=>'desc', etc...)
57  $this->current_sort=&$sort2;
58  $this->fields = array_diff($this->fields, array_keys($this->current_sort));
59 
60  }
61 
62  function toHtml(){
63  $id = rand(10,100000);
65  $p = $this->prefix;
66  if ( @$app->prefs['default_collapse_sort_control'] ){
67  $out = '<a href="#" onclick="document.getElementById(\'Dataface_SortControl-'.$id.'\').style.display=\'\'; this.style.display=\'none\'; return false">Sort Results</a>';
68  $style = 'display:none';
69  } else {
70  $style = '';
71  }
72  $out .= '<div style="'.$style.'" id="Dataface_SortControl-'.$id.'" class="Dataface_SortControl"><fieldset><legend>Sorted on:</legend><ul class="Dataface_SortControl_current_sort-list">
73  ';
74  foreach ($this->current_sort as $fieldname=>$dir){
75  $fieldDef = $this->table->getField($fieldname);
76  $out .= '<li>
77 
78  <a class="Dataface_SortControl-reverse-'.$dir.'" href="'.$app->url('-'.$p.'sort='.urlencode($this->reverseSortOn($fieldname))).'" title="Sort the results in reverse order on this column"><img src="'.DATAFACE_URL.'/images/'.($dir=='asc' ? 'arrowUp.gif' : 'arrowDown.gif').'"/>'.$fieldDef['widget']['label'].'</a>
79  <a href="'.$app->url('-'.$p.'sort='.urlencode($this->removeParameter($fieldname))).'" title="Remove this field from the sort parameters"><img src="'.DATAFACE_URL.'/images/delete.gif"/></a>
80  </li>';
81  }
82  $out .= '</ul>';
83 
84  $out .= '<select onchange="window.location=this.options[this.selectedIndex].value">
85  <option value="">Add Columns</th>';
86  foreach ($this->fields as $fieldname){
87  $fieldDef = $this->table->getField($fieldname);
88  $out .= '<option value="'.$app->url('-'.$p.'sort='.urlencode($this->addParameter($fieldname))).'">'.$fieldDef['widget']['label'].'</option>';
89  }
90  $out .= '</select><div style="clear:both"></div></fieldset></div>';
91  return $out;
92 
93  }
94 
96  $params = $this->current_sort;
97  $curr = strtolower($params[$fieldname]);
98  $params[$fieldname] = ( $curr == 'asc' ? 'desc' : 'asc');
99  return $this->sortString($params);
100  }
101 
102  function addParameter($fieldname,$dir='asc'){
103  $params = $this->current_sort;
104  $params[$fieldname] = $dir;
105  return $this->sortString($params);
106  }
107 
109  $params = $this->current_sort;
110  unset($params[$fieldname]);
111  return $this->sortString($params);
112  }
113 
114  function sortString($params){
115  $out = array();
116  foreach ( $params as $fieldname=>$dir){
117  $out[] = $fieldname.' '.$dir;
118  }
119  return implode(',',$out);
120  }
121 }