ultimix
ordering.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
38  var $Database = false;
39  var $Security = false;
40 
51  var $Table;
52 
63  function set_table_name( $NewTableName )
64  {
65  $this->Table = $NewTableName;
66  }
67 
82  function __construct()
83  {
84  try
85  {
86  $this->Database = get_package( 'database' , 'last' , __FILE__ );
87  $this->Security = get_package( 'security' , 'last' , __FILE__ );
88  }
89  catch( Exception $e )
90  {
91  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
92  }
93  }
94 
106  function get_item( $id , $Condition = '1 = 1' )
107  {
108  try
109  {
110  $id = $this->Security->get( $id , 'integer' );
111 
112  $Items = $this->Database->select( '*' , $this->Table , "id = $id AND $Condition" );
113 
114  if( count( $Items ) == 1 )
115  {
116  return( $Items[ 0 ] );
117  }
118 
119  throw( new Exception( "An error occured while getting item".mysql_error() ) );
120  }
121  catch( Exception $e )
122  {
123  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
124  }
125  }
126 
138  function get_next_order_item( $Order , $Condition = '1 = 1' )
139  {
140  try
141  {
142  $Order = $this->Security->get( $Order , 'string' );
143 
144  $NextItem = $this->Database->select(
145  '*' , $this->Table ,
146  "`order` >= $Order AND $Condition ORDER BY `order` ASC LIMIT 0 , 2"
147  );
148 
149  if( isset( $NextItem[ 1 ] ) )
150  {
151  return( $NextItem[ 1 ] );
152  }
153 
154  return( false );
155  }
156  catch( Exception $e )
157  {
158  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
159  }
160  }
161 
173  function get_prev_order_item( $Order , $Condition = '1 = 1' )
174  {
175  try
176  {
177  $Order = $this->Security->get( $Order , 'string' );
178 
179  $PrevItem = $this->Database->select(
180  '*' , $this->Table ,
181  "`order` <= $Order AND $Condition ORDER BY `order` DESC LIMIT 0 , 2"
182  );
183 
184  if( isset( $PrevItem[ 1 ] ) )
185  {
186  return( $PrevItem[ 1 ] );
187  }
188 
189  return( false );
190  }
191  catch( Exception $e )
192  {
193  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
194  }
195  }
196 
208  function swap_items( $Item1 , $Item2 , $Condition = '1 = 1' )
209  {
210  try
211  {
212  $this->Database->update(
213  $this->Table , array( '`order`' ) ,
214  get_field( $Item2 , 'order' ) ,
215  "id = ".get_field( $Item1 , 'id' )
216  );
217 
218  $this->Database->update(
219  $this->Table , array( '`order`' ) ,
220  get_field( $Item1 , 'order' ) ,
221  "id = ".get_field( $Item2 , 'id' )
222  );
223  }
224  catch( Exception $e )
225  {
226  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
227  }
228  }
229 
239  function shift_next( $id , $Condition = '1 = 1' )
240  {
241  try
242  {
243  $id = $this->Security->get( $id , 'integer' );
244 
245  $Item = $this->get_item( $id , $Condition );
246  $NextItem = $this->get_next_order_item( $Item[ 'order' ] , $Condition );
247 
248  if( $NextItem === false )
249  {
250  return;
251  }
252 
253  $this->swap_items( $Item , $NextItem , $Condition );
254  }
255  catch( Exception $e )
256  {
257  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
258  }
259  }
260 
270  function shift_prev( $id , $Condition = '1 = 1' )
271  {
272  try
273  {
274  $id = $this->Security->get( $id , 'integer' );
275 
276  $Item = $this->get_item( $id , $Condition );
277  $PrevItem = $this->get_prev_order_item( $Item[ 'order' ] , $Condition );
278 
279  if( $PrevItem === false )
280  {
281  return;
282  }
283 
284  $this->swap_items( $Item , $PrevItem , $Condition );
285  }
286  catch( Exception $e )
287  {
288  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
289  }
290  }
291  }
292 
293 ?>