Project

General

Profile

1 4 ryan
<?php
2 1487 DarkViper
/**
3
 * @category        WebsiteBaker
4
 * @package         WebsiteBaker_core
5 1529 Luisehahne
 * @author          Ryan Djurovich, WebsiteBaker Project, Werner v.d.Decken
6
 * @copyright       2009-2011, Website Baker Org. e.V.
7 1487 DarkViper
 * @link            http://websitebaker2.org
8
 * @license         http://www.gnu.org/licenses/gpl.html
9
 * @version         $Id$
10
 * @filesource		$HeadURL$
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 1496 DarkViper
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20 1499 DarkViper
if(!defined('WB_PATH')) {
21
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
22
	throw new IllegalFileException();
23
}
24 1496 DarkViper
/* -------------------------------------------------------- */
25 1487 DarkViper
	define('ORDERING_CLASS_LOADED', true);
26
// Load the other required class files if they are not already loaded
27 1684 Luisehahne
//	require_once(WB_PATH."/framework/class.database.php");
28 4 ryan
29 1487 DarkViper
class order {
30 4 ryan
31 1487 DarkViper
	const MOVE_UP   = 0;
32
	const MOVE_DOWN = 1;
33 4 ryan
34 1487 DarkViper
	private $_Table      = '';
35
	private $_FieldOrder = '';
36
	private $_FieldId    = '';
37
	private $_FieldGroup = '';
38
	private $_DB         = null;
39 4 ryan
40 1487 DarkViper
	/**
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 4 ryan
	}
55 1487 DarkViper
	/**
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 1502 DarkViper
		// get Position and Group for Element to move
67 1487 DarkViper
		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 1502 DarkViper
					// 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 1487 DarkViper
					$sql .=     'AND `'.$this->_FieldOrder.'`>\''.$rec1['order'].'\' ';
79
					$sql .= 'ORDER BY `'.$this->_FieldOrder.'` ASC';
80
				}
81 1502 DarkViper
				// get Id and Position of the Element to change with
82 1487 DarkViper
				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 1502 DarkViper
						// update Position number of target
88 1487 DarkViper
						if($this->_DB->query($sql)) {
89
							$sql  = 'UPDATE `'.$this->_Table.'` ';
90
							$sql .= 'SET `'.$this->_FieldOrder.'`=\''.$rec2['order'].'\' ';
91
							$sql .= 'WHERE `'.$this->_FieldId.'`=\''.$id.'\'';
92 1502 DarkViper
							// update Position number source
93 1487 DarkViper
							$retval = $this->_DB->query($sql);
94
						}
95
					}
96
				}
97 4 ryan
			}
98
		}
99 1487 DarkViper
		return $retval;
100 4 ryan
	}
101 1487 DarkViper
102
	/**
103
	 * Move a row up
104
	 * @param string|int $id
105
	 * @return bool
106
	 */
107
	public function move_up($id) {
108 4 ryan
		// Get current order
109 1487 DarkViper
		return $this->move($id, self::MOVE_UP);
110 4 ryan
	}
111 1487 DarkViper
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 1684 Luisehahne
122 1487 DarkViper
	/**
123
	 * Get next free number for order
124
	 * @param string|int $group
125
	 * @return integer
126
	 */
127
	public function get_new($group) {
128 4 ryan
		// Get last order
129 1487 DarkViper
		$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 4 ryan
	}
134 1684 Luisehahne
135 1487 DarkViper
	/**
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 4 ryan
		// Loop through all records and give new order
142 1505 Luisehahne
		$sql  = 'SET @c:=0';
143 1504 DarkViper
		$this->_DB->query($sql);
144 1505 Luisehahne
		$sql  = 'UPDATE `'.$this->_Table.'` SET `'.$this->_FieldOrder.'`=(SELECT @c:=@c+1) ';
145
		$sql .= 'WHERE `'.$this->_FieldGroup.'`=\''.$group.'\' ';
146 1487 DarkViper
		$sql .= 'ORDER BY `'.$this->_FieldOrder.'` ASC;';
147
		return $this->_DB->query($sql);
148 4 ryan
	}
149 1505 Luisehahne
150 1487 DarkViper
} // end of class