Xataface 2.0
Xataface Application Framework
Dataface/SortControl.php
Go to the documentation of this file.
00001 <?php
00002 class Dataface_SortControl {
00003         var $current_sort;
00004         var $fields;
00005         var $prefix;
00006         var $table;
00007         function Dataface_SortControl($fields, $prefix=''){
00008                 
00009                 if ( is_string($fields) ){
00010                         $t =& Dataface_Table::loadTable($fields);
00011                         $fields = array_keys($t->fields(false,true));
00012                 } else {
00013                         $app =& Dataface_Application::getInstance();
00014                         $query =& $app->getQuery();
00015                         $t =& Dataface_Table::loadTable($query['-table']);
00016                 }
00017                 
00018                 $this->table =& $t;
00019                 $this->fields = array();
00020                 
00021                 if ( isset($t->_atts['__global__']) ) $globalProps = $t->_atts['__global__'];
00022                 else $globalProps = array();
00023                 foreach ( $fields as $field ){
00024                         $fieldDef =& $t->getField($field);
00025                         if ( isset($globalProps['sortable']) and !$globalProps['sortable'] and !@$fieldDef['sortable']){
00026                                 continue;
00027                         } else if ( isset($fieldDef['sortable']) and !@$fieldDef['sortable'] ){
00028                                 continue;
00029                         }
00030                         $this->fields[] = $field;
00031                 }
00032                 
00033                 $this->prefix = $prefix;
00034                 //$this->fields = $fields;
00035                 $app =& Dataface_Application::getInstance();
00036                 $query =& $app->getQuery();
00037                 if ( !isset($query['-'.$prefix.'sort']) ){
00038                         $sort = '';
00039                 } else {
00040                         $sort = $query['-'.$prefix.'sort'];
00041                 }
00042                 
00043                 $sort = array_map('trim',explode(',', $sort));
00044                 
00045                 $sort2 = array();
00046                 foreach ( $sort as $col ){
00047                         if ( !trim($col) ) continue;
00048                         $col = explode(' ',$col);
00049 
00050                         if ( count($col) <= 1 ){
00051                                 $col[1] = 'asc';
00052                         }
00053                         $sort2[$col[0]] = $col[1];
00054                 }
00055                 
00056                 // Now sort2 looks like array('col1'=>'asc', 'col2'=>'desc', etc...)
00057                 $this->current_sort=&$sort2;
00058                 $this->fields = array_diff($this->fields, array_keys($this->current_sort));
00059                 
00060         }
00061         
00062         function toHtml(){
00063                 $id = rand(10,100000);
00064                 $app =& Dataface_Application::getInstance();
00065                 $p = $this->prefix;
00066                 if ( @$app->prefs['default_collapse_sort_control'] ){
00067                         $out = '<a href="#" onclick="document.getElementById(\'Dataface_SortControl-'.$id.'\').style.display=\'\'; this.style.display=\'none\'; return false">Sort Results</a>';
00068                         $style = 'display:none';
00069                 } else {
00070                         $style = '';
00071                 }       
00072                 $out .= '<div style="'.$style.'" id="Dataface_SortControl-'.$id.'" class="Dataface_SortControl"><fieldset><legend>Sorted on:</legend><ul class="Dataface_SortControl_current_sort-list">
00073                                 ';
00074                 foreach ($this->current_sort as $fieldname=>$dir){
00075                         $fieldDef = $this->table->getField($fieldname);
00076                         $out .= '<li>
00077                         
00078                         <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>
00079                         <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>
00080                         </li>';
00081                 }
00082                 $out .= '</ul>';
00083                 
00084                 $out .= '<select onchange="window.location=this.options[this.selectedIndex].value">
00085                         <option value="">Add Columns</th>';
00086                 foreach ($this->fields as $fieldname){
00087                         $fieldDef = $this->table->getField($fieldname);
00088                         $out .= '<option value="'.$app->url('-'.$p.'sort='.urlencode($this->addParameter($fieldname))).'">'.$fieldDef['widget']['label'].'</option>';
00089                 }
00090                 $out .= '</select><div style="clear:both"></div></fieldset></div>';
00091                 return $out;
00092         
00093         }
00094         
00095         function reverseSortOn($fieldname){
00096                 $params = $this->current_sort;
00097                 $curr = strtolower($params[$fieldname]);
00098                 $params[$fieldname] = ( $curr == 'asc' ? 'desc' : 'asc');
00099                 return $this->sortString($params);
00100         }
00101         
00102         function addParameter($fieldname,$dir='asc'){
00103                 $params = $this->current_sort;
00104                 $params[$fieldname] = $dir;
00105                 return $this->sortString($params);
00106         }
00107         
00108         function removeParameter($fieldname){
00109                 $params = $this->current_sort;
00110                 unset($params[$fieldname]);
00111                 return $this->sortString($params);
00112         }
00113         
00114         function sortString($params){
00115                 $out = array();
00116                 foreach ( $params as $fieldname=>$dir){
00117                         $out[] = $fieldname.' '.$dir;
00118                 }
00119                 return implode(',',$out);
00120         }
00121 }
 All Data Structures Namespaces Files Functions Variables Enumerations