<?php
/*
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/gpl.txt
#
*/
/******
*
* class Positions - Get and set to table db position's records
*
* @author Vladimir S. Bredihin
* @access public
* @version 1.0
* @package positions
* @link http://mycms.info
*
******/
class Positions{
var $table;
var $parent;
var $id;
var $field = array ();
var $_form = array ( 'RU' => array ( 'first' => 'First [Language = RU]',
'last' => 'Last [Language = RU]'),
'EN' => array ( 'first' => 'First',
'last' => 'Last'));
var $lang = 'EN';
function Positions($table=null, $parent=null, $id=null, $fieldName='name', $fieldParent='parent')
{
$this->table = $table;
$this->parent = $parent;
$this->id = $id;
$this->field['name' ] = $fieldName;
$this->field['parent' ] = $fieldParent;
$this->_constructor();
}
function _constructor()
{
if ($this->table)
if (!mysql_query ('SELECT position FROM '.$this->table.' LIMIT 0,1'))
{
mysql_query ('ALTER TABLE '.$this->table.' ADD COLUMN position int');
mysql_query ('UPDATE '.$this->table.' SET position=id');
}
}
/*
This function list records table
*/
function lst($table=null, $parent=null)
{
if (!$table) $table = $this->table;
if (!$parent) $parent = $this->parent;
$positions = array ('begin' => $this->_form [$this->lang]['first']);
$result = mysql_query ('SELECT id, '.$this->field['name'].' FROM '.$table."
WHERE {$this->field['parent']}='{$parent}' ORDER BY position ") or die (__FILE__.' - '.__LINE__.' :'.mysql_error());
while ($row = mysql_fetch_assoc ($result))
{
$positions [$row['id']] = $row [$this->field['name']];
}
$positions['end'] = $this->_form [$this->lang]['last'];
return $positions;
}
/*
This function set position to table
$set_position - is id record aftr which come set current record or constantes first|last
if result success full (and not error) then return true else return false
*/
function modify($set_position)
{
$table = $this->table;
$parent = $this->parent;
$id = $this->id;
$break = false;
if ($set_position && $set_position != $id)
{
if (is_numeric ($set_position))
{
$result = mysql_query ('SELECT position FROM '.$table." WHERE id='{$set_position}'");
if ($row = mysql_fetch_row ($result))
{
$position = $row[0]+1;
}
}
elseif ($set_position == 'begin')
{
$position = 1;
}
elseif ($set_position=='end')
{
$result = mysql_query ('SELECT position FROM '.$table." WHERE {$this->field['parent']}='{$parent}' ORDER BY position DESC LIMIT 0,1");
if ($row = mysql_fetch_row ($result))
$position = $row[0]+1;
}
else
{
//error
$break = true;
}
if (!$break)
{
mysql_query ('UPDATE '.$table." SET position='{$position}' WHERE id='{$id}'");
$result = mysql_query ('SELECT id FROM '.$table." WHERE {$this->field['parent']}='{$parent}' AND position>={$position} AND id!='{$id}' ORDER BY position") or die (mysql_error());
while ($row = mysql_fetch_row($result))
{
$position++;
mysql_query ('UPDATE '.$table." SET position='{$position}' WHERE id='{$row[0]}'");
}
}
}
return !$break;
}
}
?>