wb-2_10_x / branches / main / framework / class.order.php @ 10
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* @category WebsiteBaker
|
4 |
* @package WebsiteBaker_core
|
5 |
* @author Ryan Djurovich, WebsiteBaker Project, Werner v.d.Decken
|
6 |
* @copyright 2009-2011, Website Baker Org. e.V.
|
7 |
* @link http://websitebaker2.org
|
8 |
* @license http://www.gnu.org/licenses/gpl.html
|
9 |
* @version $Id: class.order.php 2 2017-07-02 15:14:29Z Manuela $
|
10 |
* @filesource $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/framework/class.order.php $
|
11 |
* Ordering class
|
12 |
* This class will be used to change the order of an item in a table
|
13 |
* which contains a special order field (type must be integer)
|
14 |
*/
|
15 |
/*******************************************************************************
|
16 |
* abstract factory for application
|
17 |
*/
|
18 |
/* -------------------------------------------------------- */
|
19 |
// Must include code to stop this file being accessed directly
|
20 |
if(!defined('WB_PATH')) { |
21 |
require_once(dirname(__FILE__).'/globalExceptionHandler.php'); |
22 |
throw new IllegalFileException(); |
23 |
} |
24 |
/* -------------------------------------------------------- */
|
25 |
define('ORDERING_CLASS_LOADED', true); |
26 |
// Load the other required class files if they are not already loaded
|
27 |
require_once(WB_PATH."/framework/class.database.php"); |
28 |
|
29 |
class order { |
30 |
|
31 |
const MOVE_UP = 0; |
32 |
const MOVE_DOWN = 1; |
33 |
|
34 |
private $_Table = ''; |
35 |
private $_FieldOrder = ''; |
36 |
private $_FieldId = ''; |
37 |
private $_FieldGroup = ''; |
38 |
private $_DB = null; |
39 |
|
40 |
/**
|
41 |
* Constructor
|
42 |
* @param string $Table
|
43 |
* @param string $FieldOrder
|
44 |
* @param string $FieldId
|
45 |
* @param string $FieldGroup
|
46 |
* use $GLOBALS['database']
|
47 |
*/
|
48 |
public function __construct($Table, $FieldOrder, $FieldId, $FieldGroup) { |
49 |
$this->_DB = $GLOBALS['database']; |
50 |
$this->_Table = $Table; |
51 |
$this->_FieldOrder = $FieldOrder; |
52 |
$this->_FieldId = $FieldId; |
53 |
$this->_FieldGroup = $FieldGroup; |
54 |
} |
55 |
/**
|
56 |
*
|
57 |
* @param string|int $id
|
58 |
* @param int $direction
|
59 |
* @return bool
|
60 |
*/
|
61 |
public function move($id, $direction = self::MOVE_UP) |
62 |
{ |
63 |
$retval = false; |
64 |
$sql = 'SELECT `'.$this->_FieldOrder.'` `order`, `'.$this->_FieldGroup.'` `group` '; |
65 |
$sql .= 'FROM `'.$this->_Table.'` WHERE `'.$this->_FieldId.'`=\''.$id.'\''; |
66 |
// get Position and Group for Element to move
|
67 |
if(($res1 = $this->_DB->query($sql))) { |
68 |
if(($rec1 = $res1->fetchRow())) { |
69 |
$sql = 'SELECT `'.$this->_FieldId.'` `id`, `'.$this->_FieldOrder.'` `order` '; |
70 |
$sql .= 'FROM `'.$this->_Table.'` '; |
71 |
$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$rec1['group'].'\' '; |
72 |
if($direction == self::MOVE_UP) { |
73 |
// search for Element with next lower Position
|
74 |
$sql .= 'AND `'.$this->_FieldOrder.'`<\''.$rec1['order'].'\' '; |
75 |
$sql .= 'ORDER BY `'.$this->_FieldOrder.'` DESC'; |
76 |
}else {
|
77 |
// search for Element with next higher Position
|
78 |
$sql .= 'AND `'.$this->_FieldOrder.'`>\''.$rec1['order'].'\' '; |
79 |
$sql .= 'ORDER BY `'.$this->_FieldOrder.'` ASC'; |
80 |
} |
81 |
// get Id and Position of the Element to change with
|
82 |
if(($res2 = $this->_DB->query($sql))) { |
83 |
if(($rec2 = $res2->fetchRow())) { |
84 |
$sql = 'UPDATE `'.$this->_Table.'` '; |
85 |
$sql .= 'SET `'.$this->_FieldOrder.'`=\''.$rec1['order'].'\' '; |
86 |
$sql .= 'WHERE `'.$this->_FieldId.'`=\''.$rec2['id'].'\''; |
87 |
// update Position number of target
|
88 |
if($this->_DB->query($sql)) { |
89 |
$sql = 'UPDATE `'.$this->_Table.'` '; |
90 |
$sql .= 'SET `'.$this->_FieldOrder.'`=\''.$rec2['order'].'\' '; |
91 |
$sql .= 'WHERE `'.$this->_FieldId.'`=\''.$id.'\''; |
92 |
// update Position number source
|
93 |
$retval = $this->_DB->query($sql); |
94 |
} |
95 |
} |
96 |
} |
97 |
} |
98 |
} |
99 |
return $retval; |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Move a row up
|
104 |
* @param string|int $id
|
105 |
* @return bool
|
106 |
*/
|
107 |
public function move_up($id) { |
108 |
// Get current order
|
109 |
return $this->move($id, self::MOVE_UP); |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Move a row down
|
114 |
* @param string|int $id
|
115 |
* @return bool
|
116 |
*/
|
117 |
public function move_down($id) { |
118 |
// Get current order
|
119 |
return $this->move($id, self::MOVE_DOWN); |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Get next free number for order
|
124 |
* @param string|int $group
|
125 |
* @return integer
|
126 |
*/
|
127 |
public function get_new($group) { |
128 |
// Get last order
|
129 |
$sql = 'SELECT MAX(`'.$this->_FieldOrder.'`) FROM `'.$this->_Table.'` '; |
130 |
$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$group.'\' '; |
131 |
$max = intval($this->_DB->get_one($sql)) + 1; |
132 |
return $max; |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Renumbering a group from 1 to n (should be called if a row in the middle has been deleted)
|
137 |
* @param string|int $group
|
138 |
* @return bool
|
139 |
*/
|
140 |
public function clean($group) { |
141 |
// Loop through all records and give new order
|
142 |
$sql = 'SET @c:=0'; |
143 |
$this->_DB->query($sql); |
144 |
$sql = 'UPDATE `'.$this->_Table.'` SET `'.$this->_FieldOrder.'`=(SELECT @c:=@c+1) '; |
145 |
$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$group.'\' '; |
146 |
$sql .= 'ORDER BY `'.$this->_FieldOrder.'` ASC;'; |
147 |
return $this->_DB->query($sql); |
148 |
} |
149 |
|
150 |
} // end of class
|