Thinkphp implements left and right value addition, modification, and deletion operations

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn, write basic functions, unwritten sorting, and other related operations,
Database:

Class Library file:
Category. class. php /**
Unlimited left/right value Classification of applications based on thinkphp
**/
Class Category
{
// Input the real object [M ('table name ')]
Private $ objCategory;
// Basic node ID
Public $ intCurrentId;
// Set the tab Style
Private $ arrTabsStyle = array (
'Indent '=> '',
'Process' => 'process ',
'End' => 'stopped'
);
// Constructor Initialization
Public function _ construct ($ objCategory)
{
$ This-> objCategory = $ objCategory;
}
// Verify the input ID (number greater than 0]
Private function checkFun ($ intId)
{
// $ IntId priority verification
If (isset ($ intId ))
{
$ This-> intCurrentId = $ intId;
Return true;
}
// If $ this-> intCurrentId has been set, verify
Else
{
If (isset ($ this-> intCurrentId ))
{
Return true;
}
Else
{
Return false;
}
}
}
// Obtain the left and right values of the current node based on the ID number
Private function setCurrentData ($ intId)
{
If (false = $ this-> checkFun ($ intId ))
{
Return false;
}
$ Map ['id'] = $ this-> intCurrentId;
Return $ this-> objCategory-> field (array ('lft ', 'rgt')-> where ($ map)-> find ();
}
/*
* Function:
Set the tab style of output List Data
* Parameters:
$ Key: KEY of arrTabsStyle
$ Value: arrTabsStyle value
*/
Public function setTabStyle ($ key, $ value = '')
{
If (isset ($ this-> arrTabsStyle [$ key])
{
$ This-> arrTabsStyle [$ key] = $ value;
}
}
/*
* Function:
Obtains the data of the current node based on the ID number.
* Parameters:
$ IntId: configurable. You need to read the node ID.
*/
Public function getCurrentData ($ intId)
{
If (false = $ this-> checkFun ($ intId ))
{
Return false;
}
$ Map ['id'] = $ this-> intCurrentId;
Return $ this-> objCategory-> field (array ('id', 'title')-> where ($ map)-> find ();
}
/*
* Function:
Obtains the data of the parent node of the current node.
* Parameters:
$ IntId: ID of the node to be read
*/
Public function getParentCategoryData ($ intId)
{
$ ArrRoot = $ this-> setCurrentData ($ intId );
If ($ arrRoot)
{
$ Map ['lft '] = array ('lt', $ arrRoot ['lft ']);
$ Map ['rgt '] = array ('gt', $ arrRoot ['rgt ']);
Return $ this-> objCategory-> where ($ map)-> find ();
}
Else
{
Return false;
}
}
/*
* Function:
Obtain the node list under the ID
* Parameters:
$ IntId: parent ID of the node to be read
$ IntLevel: the default directory level is 100.
*/
Public function getCategoryList ($ intId = 1, $ intLevel = 100)
{
// Obtain the left and right values of the selected node and obtain the value range
$ ArrRoot = $ this-> setCurrentData ($ intId );
If ($ arrRoot)
{
// Read the database's Qualified Data
$ Map ['lft '] = array ('between', array ($ arrRoot ['lft'], $ arrRoot ['rgt ']);
$ ArrChildList = $ this-> objCategory-> where ($ map)-> order ('lft ')-> select ();
// Return $ arrChildList;
// Format the Retrieved Data
$ ArrRight = array ();
Foreach ($ arrChildList as $ v)
{

If (count ($ arrRight ))
{
While ($ arrRight [count ($ arrRight)-1] <$ v ['rgt '])
{
Array_pop ($ arrRight );
}
}
// Set the read directory level
If ($ intLevel> count ($ arrRight ))
{
$ Title = $ v ['title'];
// Set the output style
If (count ($ arrRight ))
{
$ Title = $ this-> arrTabsStyle ['process']. $ title;
}
$ Title = str_repeat ($ this-> arrTabsStyle ['indent '], count ($ arrRight). $ title;
$ ReturnCategoryList [] = array ('id' => $ v ['id'], 'title' => $ title, 'lft '=> $ v ['lft'], 'rgt '=> $ v ['rgt']);
$ ArrRight [] = $ v ['rgt '];
}
}
Return $ returnCategoryList;
}
Return false;
}
/*
* Function:
Obtains the number of subnodes of a node.
* Parameters:
$ IntId: parent ID of the node to be read
*/
Public function getCategoryCount ($ intId)
{
$ ArrRoot = $ this-> setCurrentData ($ intId );
Return ($ arrRoot ['rgt ']-$ arrRoot ['lft']-1)/2;
}
/*
* Function:
Add Node
* Parameters:
$ BolType: true: add the node to the front, and false to the end of the node.
$ IntId: parent node added
*/
Public function insertCategory ($ bolType = false, $ intPid)
{
$ Data = I ('Param .');
If (! Isset ($ intPid ))
{
$ IntPid = $ data ['pid'];
}
$ ArrRoot = $ this-> setCurrentData ($ intPid );
If ($ arrRoot)
{
If ($ bolType)
// Add true to the node
{
$ This-> objCategory-> where ('rgt> '. $ arrRoot ['lft'])-> setInc ('rgt ', 2 );
$ This-> objCategory-> where ('lft> '. $ arrRoot ['lft'])-> setInc ('lft ', 2 );
// Set the left and right values of the current node
$ Data ['lft '] = $ arrRoot ['lft'] + 1;
$ Data ['rgt '] = $ arrRoot ['lft'] + 2;
}
Else
// Add false to the end of the node
{
$ This-> objCategory-> where ('rgt> = '. $ arrRoot ['rgt'])-> setInc ('rgt ', 2 );
$ This-> objCategory-> where ('lft> '. $ arrRoot ['rgt'])-> setInc ('lft ', 2 );
$ Data ['lft '] = $ arrRoot ['rgt'];
$ Data ['rgt '] = $ arrRoot ['rgt'] + 1;
}
Return $ this-> objCategory-> add ($ data );
}
Else
{
Return false;
}
}
/*
* Function:
Delete a node
* Parameters:
$ IntId: ID of the deleted Node
*/
Public function deleteCategory ($ intId)
{
$ ArrRoot = $ this-> setCurrentData ($ intId );
If ($ arrRoot)
{
$ Ints = $ arrRoot ['rgt ']-$ arrRoot ['lft'] + 1;
$ Map ['lft '] = array ('between', array ($ arrRoot ['lft'], $ arrRoot ['rgt ']);
$ This-> objCategory-> where ($ map)-> delete ();
$ This-> objCategory-> where ('lft> '. $ arrRoot ['rgt'])-> setDec ('lft ', $ ints );
$ This-> objCategory-> where ('rgt> '. $ arrRoot ['rgt'])-> setDec ('rgt ', $ ints );
Return true;
}
Else
{
Return false;
}
}
/*
* Function:
Update a node
* Parameters:
$ IntId: ID of the deleted Node
*/
Public function updateCategory ()
{
// Read POST Data and store it in an array
$ Data = I ('Param .');
// The parent ID is equal to the child ID and jumps out directly.
If ($ data ['pid '] = $ data ['id']) {return false ;}
// If the value of post. pid is equal to that of the current parent post. old, the directory is not changed and the left and right values are not updated.
If ($ data ['pid']! = $ Data ['old'])
{
/*********************************** [Read related value ]********************************/
// Obtain the data of the new parent node
$ ArrParent = $ this-> setCurrentData ($ data ['pid']);
// Retrieve the data of the current node
$ ArrCurrent = $ this-> setCurrentData ($ data ['id']);
/* Task: delete a node */
/*********************************** [A-1: isolate data ]************************************/
// Add the value of the left and right of the position to + 100000
$ Map ['lft '] = array (
Array ('egt', $ arrCurrent ['lft ']),
Array ('elt', $ arrCurrent ['rgt '])
);
$ This-> objCategory-> where ($ map)-> setInc ('lft, 100000 );
// Because the left value has been updated, the condition changes by + 100000.
$ Map ['lft '] = array (
Array ('egt', $ arrCurrent ['lft '] + 100000 ),
Array ('elt', $ arrCurrent ['rgt '] + 100000)
);
$ This-> objCategory-> where ($ map)-> setInc ('rgt ', 100000 );
Unset ($ map );
/*********************************** [A-2: update normal node values ]******************************/
// Obtain the step value updated later on the isolated node
$ IntStep = $ arrCurrent ['rgt ']-$ arrCurrent ['lft'] + 1;
// Update the Left and Right node values
$ Map ['lft '] = array (
Array ('gt ', $ arrCurrent ['rgt']),
Array ('lt ', 100000)
);
$ This-> objCategory-> where ($ map)-> setDec ('lft ', $ intStep );
Unset ($ map );
$ Map ['rgt '] = array (
Array ('gt ', $ arrCurrent ['rgt']),
Array ('lt ', 100000)
);
$ This-> objCategory-> where ($ map)-> setDec ('rgt ', $ intStep );
Unset ($ map );
/* Complete: Delete the node */
/* Task: Update node */
/*********************************** [B-1: the new parent program provides space for lower-level nodes ]****************/
$ Map ['lft '] = array (
Array ('gt ', $ arrParent ['lft']),
Array ('lt ', 100000)
);
$ This-> objCategory-> where ($ map)-> setInc ('lft ', $ intStep );
Unset ($ map );
$ Map ['rgt '] = array (
Array ('gt ', $ arrParent ['lft']),
Array ('lt ', 100000)
);
$ This-> objCategory-> where ($ map)-> setInc ('rgt ', $ intStep );
Unset ($ map );
/*********************************** [B-2: place the node in the space of the specified lower level ]********************/
// Obtain the step value updated later on the isolated node
$ IntStep = 100000 + ($ arrCurrent ['lft ']-($ arrParent ['lft'] + 1 ));
// Update the left and right values of a node greater than the left value of the parent node
$ Map ['lft '] = array (
Array ('egt', $ arrCurrent ['lft '] + 100000 ),
Array ('elt', $ arrCurrent ['rgt '] + 100000)
);
$ This-> objCategory-> where ($ map)-> setDec ('lft ', $ intStep );
Unset ($ map );
$ Map ['rgt '] = array (
Array ('egt', $ arrCurrent ['lft '] + 100000 ),
Array ('elt', $ arrCurrent ['rgt '] + 100000)
);
$ This-> objCategory-> where ($ map)-> setDec ('rgt ', $ intStep );
}
Return $ this-> objCategory-> where ('Id = '. $ data ['id'])-> setField ('title', $ data ['title']);
}
}
?>
Usage: Class CategoryAction extends CommonAction {
Private $ objCG;
Public function _ initialize (){
// Import the category Library
Import ('@. ORG. Util. Category ');
$ This-> objCG = new Category (M ('commoncategory '));
}
// Directory list
Public function index ($ id = 1 ){
$ This-> cateorylist = $ this-> objCG-> getCategoryList ($ id );
$ This-> display ();
}
// Add a directory
Public function add ($ id = 1 ){
$ This-> cateorylist = $ this-> objCG-> getCategoryList ($ id );
$ This-> display ();
}
// Edit the Directory
Public function edit ($ id ){
If (! Empty ($ id )){
// Output all nodes
$ This-> cateorylist = $ this-> objCG-> getCategoryList (1 );
// Read the data of the current node
$ Vo = $ this-> objCG-> getCurrentData ($ id );
If ($ vo ){
// Read the data of the parent node of the current node
$ ArrParent = $ this-> objCG-> getParentCategoryData ($ id );
$ Vo ['pid '] = $ arrParent ['id'];
$ This-> assign ('vo', $ vo );
$ This-> display ();
} Else {
$ This-> error ('data does not exist! ');
}
} Else {
$ This-> error ('data does not exist! ');
}
}
// Add a directory: Operation
Public function insert (){
$ List = $ this-> objCG-> insertCategory ();
If ($ list! = False)
{
$ This-> success ('data is saved successfully! ');
}
Else
{
$ This-> error ('data writing error! ');
}
}
Public function delete ($ id ){
If (! Empty ($ id ))
{
$ Result = $ this-> objCG-> deleteCategory ($ id );
If ($ result)
{
$ This-> success ('deleted successfully! ');
}
Else
{
$ This-> error ('deletion error! ');
}
}
Else
{
$ This-> error ('Id error! ');
}
}
Public function update (){
$ List = $ this-> objCG-> updateCategory ();
If ($ list! = False)
{
$ This-> success ('Update successful! ');
}
Else
{
$ This-> error ("operation failed! ");
}
}
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.