Project

General

Profile

1
<?php
2
/**
3
 * @category        WebsiteBaker
4
 * @package         WebsiteBaker_core
5
 * @author          Werner v.d.Decken
6
 * @copyright       WebsiteBaker.org e.V.
7
 * @link            http://websitebaker2.org
8
 * @license         http://www.gnu.org/licenses/gpl.html
9
 * @version         $Id: class.order.php 1487 2011-08-10 13:20:15Z DarkViper $
10
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/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
// Must include code to stop this file being access directly
19
	if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
20
	define('ORDERING_CLASS_LOADED', true);
21
// Load the other required class files if they are not already loaded
22
	require_once(WB_PATH."/framework/class.database.php");
23

    
24
class order {
25

    
26
	const MOVE_UP   = 0;
27
	const MOVE_DOWN = 1;
28

    
29
	private $_Table      = '';
30
	private $_FieldOrder = '';
31
	private $_FieldId    = '';
32
	private $_FieldGroup = '';
33
	private $_DB         = null;
34

    
35
	/**
36
	 * Constructor
37
	 * @param string $Table
38
	 * @param string $FieldOrder
39
	 * @param string $FieldId
40
	 * @param string $FieldGroup
41
	 * use $GLOBALS['database']
42
	 */
43
	public function __construct($Table, $FieldOrder, $FieldId, $FieldGroup) {
44
		$this->_DB         = $GLOBALS['database'];
45
		$this->_Table      = $Table;
46
		$this->_FieldOrder = $FieldOrder;
47
		$this->_FieldId    = $FieldId;
48
		$this->_FieldGroup = $FieldGroup;
49
	}
50
	/**
51
	 *
52
	 * @param string|int $id
53
	 * @param int $direction
54
	 * @return bool
55
	 */
56
	public function move($id, $direction = self::MOVE_UP)
57
	{
58
		$retval = false;
59
		$sql  = 'SELECT `'.$this->_FieldOrder.'` `order`, `'.$this->_FieldGroup.'` `group` ';
60
		$sql .= 'FROM `'.$this->_Table.'` WHERE `'.$this->_FieldId.'`=\''.$id.'\'';
61
		if(($res1 = $this->_DB->query($sql))) {
62
			if(($rec1 = $res1->fetchRow())) {
63
				$sql  = 'SELECT `'.$this->_FieldId.'` `id`, `'.$this->_FieldOrder.'` `order` ';
64
				$sql .= 'FROM `'.$this->_Table.'` ';
65
				$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$rec1['group'].'\' ';
66
				if($direction == self::MOVE_UP) {
67
					$sql .=     'AND `'.$this->_FieldOrder.'`>\''.$rec1['order'].'\' ';
68
					$sql .= 'ORDER BY `'.$this->_FieldOrder.'` ASC';
69
				}else {
70
					$sql .=     'AND `'.$this->_FieldOrder.'`<\''.$rec1['order'].'\' ';
71
					$sql .= 'ORDER BY `'.$this->_FieldOrder.'` DESC';
72
				}
73
				if(($res2 = $this->_DB->query($sql))) {
74
					if(($rec2 = $res2->fetchRow())) {
75
						$sql  = 'UPDATE `'.$this->_Table.'` ';
76
						$sql .= 'SET `'.$this->_FieldOrder.'`=\''.$rec1['order'].'\' ';
77
						$sql .= 'WHERE `'.$this->_FieldId.'`=\''.$rec2['id'].'\'';
78
						if($this->_DB->query($sql)) {
79
							$sql  = 'UPDATE `'.$this->_Table.'` ';
80
							$sql .= 'SET `'.$this->_FieldOrder.'`=\''.$rec2['order'].'\' ';
81
							$sql .= 'WHERE `'.$this->_FieldId.'`=\''.$id.'\'';
82
							$retval = $this->_DB->query($sql);
83
						}
84
					}
85
				}
86
			}
87
		}
88
		return $retval;
89
	}
90

    
91
	/**
92
	 * Move a row up
93
	 * @param string|int $id
94
	 * @return bool
95
	 */
96
	public function move_up($id) {
97
		// Get current order
98
		return $this->move($id, self::MOVE_UP);
99
	}
100

    
101
	/**
102
	 * Move a row down
103
	 * @param string|int $id
104
	 * @return bool
105
	 */
106
	public function move_down($id) {
107
		// Get current order
108
		return $this->move($id, self::MOVE_DOWN);
109
	}
110
	
111
	/**
112
	 * Get next free number for order
113
	 * @param string|int $group
114
	 * @return integer
115
	 */
116
	public function get_new($group) {
117
		// Get last order
118
		$sql  = 'SELECT MAX(`'.$this->_FieldOrder.'`) FROM `'.$this->_Table.'` ';
119
		$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$group.'\' ';
120
		$max = intval($this->_DB->get_one($sql)) + 1;
121
		return $max;
122
	}
123
	
124
	/**
125
	 * Renumbering a group from 1 to n (should be called if a row in the middle has been deleted)
126
	 * @param string|int $group
127
	 * @return bool
128
	 */
129
	public function clean($group) {
130
		// Loop through all records and give new order
131
		$sql  = 'SET @c := 0; ';
132
		$sql .= 'UPDATE `'.$this->_Table.'` SET `'.$this->_FieldOrder.'`=( SELECT @c := @c + 1 ) ';
133
		$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$group.'\' ';
134
		$sql .= 'ORDER BY `'.$this->_FieldOrder.'` ASC;';
135
		return $this->_DB->query($sql);
136
	}
137
	
138
} // end of class
(10-10/19)