00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBSortColumn implements IDBQueryModifier {
00009 const TYPE_TEXT = 'text';
00010 const TYPE_CURRENCY = 'currency';
00011 const TYPE_NUMERIC = 'numeric';
00012 const TYPE_DATE = 'date';
00013 const TYPE_MATCH = 'match';
00014
00015 const ORDER_FORWARD = 'forward';
00016 const ORDER_BACKWARD = 'backward';
00017
00018
00019
00020
00021 private $column;
00022
00023
00024
00025 private $title;
00026
00027
00028
00029 private $type;
00030
00031
00032
00033
00034
00035 private $single_direction = false;
00036
00037
00038
00039 private $direction = self::ORDER_FORWARD;
00040
00041
00042
00043
00044
00045
00046
00047
00048 public function __construct($column, $title, $type, $direction = self::ORDER_FORWARD, $single_direction = false) {
00049 $this->column = $column;
00050 $this->title = $title;
00051 $this->type = $type;
00052 $this->direction = $direction;
00053 $this->single_direction = $single_direction;
00054 }
00055
00056 public function apply($query) {
00057 $query->sort($this->get_db_column(), $this->get_sort_order($this->direction));
00058 }
00059
00060
00061
00062
00063
00064
00065 protected function get_db_column() {
00066 return $this->get_column();
00067 }
00068
00069 public function get_column() {
00070 return $this->column;
00071 }
00072
00073 public function get_title() {
00074 return $this->title;
00075 }
00076
00077
00078
00079
00080
00081
00082 public function get_is_single_direction() {
00083 return $this->single_direction;
00084 }
00085
00086 public function set_direction($direction) {
00087 if (!$this->get_is_single_direction()) {
00088 $this->direction = $direction;
00089 }
00090 }
00091
00092 public function get_direction() {
00093 return $this->direction;
00094 }
00095
00096
00097
00098
00099 public function get_order_title($direction) {
00100 if ($direction == self::ORDER_FORWARD) {
00101 switch ($this->type) {
00102 case self::TYPE_CURRENCY:
00103 return tr('Ascending (cheaper first)', 'core');
00104 break;
00105 case self::TYPE_DATE:
00106 return tr('Ascending (newer first)', 'core');
00107 break;
00108 case self::TYPE_TEXT:
00109 return tr('Ascending (A-Z)', 'core');
00110 break;
00111 case self::TYPE_MATCH:
00112 return tr('Ascending (Most important first)', 'core');
00113 break;
00114 case self::TYPE_NUMERIC:
00115 default:
00116 return tr('Ascending (smaller first)', 'core');
00117 break;
00118 }
00119 }
00120 else {
00121 switch ($this->type) {
00122 case self::TYPE_CURRENCY:
00123 return tr('Descending (expensive first)', 'core');
00124 break;
00125 case self::TYPE_DATE:
00126 return tr('Descending (older first)', 'core');
00127 break;
00128 case self::TYPE_TEXT:
00129 return tr('Descending (Z-A)', 'core');
00130 break;
00131 case self::TYPE_MATCH:
00132 return tr('Descending (Less important first)', 'core');
00133 break;
00134 case self::TYPE_NUMERIC:
00135 default:
00136 return tr('Descending (greater first)', 'core');
00137 break;
00138 }
00139 }
00140 }
00141
00142
00143
00144
00145
00146
00147 public function get_sort_order($direction) {
00148 switch ($this->type) {
00149 case self::TYPE_DATE:
00150 case self::TYPE_MATCH:
00151 return ($direction == self::ORDER_BACKWARD) ? ISearchAdapter::ASC : ISearchAdapter::DESC;
00152 break;
00153 default:
00154 return ($direction == self::ORDER_BACKWARD) ? ISearchAdapter::DESC : ISearchAdapter::ASC;
00155 break;
00156 }
00157 }
00158
00159
00160
00161
00162 public function get_opposite_order($direction) {
00163 return ($direction == self::ORDER_BACKWARD) ? self::ORDER_FORWARD : self::ORDER_BACKWARD;
00164 }
00165 }