PHP code example of gtcesar / recursive-db-tree

1. Go to this page and download the library: Download gtcesar/recursive-db-tree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gtcesar / recursive-db-tree example snippets



/**
 * Segment
 *
 * Represents a segment entity in the database.
 * Extends TRecord, the base class for database records in Adianti Framework.
 */
class Segment extends TRecord
{
    const TABLENAME = 'segment'; // Name of the database table
    const PRIMARYKEY = 'id'; // Primary key field name
    const IDPOLICY = 'serial'; // ID generation policy (max, serial)

    /**
     * Constructor method
     *
     * @param int|null $id The ID of the segment (optional)
     * @param bool $callObjectLoad Whether to call the parent's load method (default: TRUE)
     */
    public function __construct($id = NULL, $callObjectLoad = TRUE)
    {
        // Call the parent constructor to initialize the object
        parent::__construct($id, $callObjectLoad);
        
        // Add attributes to the record
        parent::addAttribute('parent_segment_id'); // Parent segment ID
        parent::addAttribute('description'); // Segment description
    }
}



use Gtcesar\RecursiveDBTree\RecursiveDBTree;

/**
 * SegmentTree
 *
 * This class represents a segment tree page.
 * It displays a hierarchical structure of segments using RecursiveDBTree component.
 * Users can interact with the segments by selecting, editing, deleting, or viewing them.
 */
class SegmentTree extends TPage
{
    /**
     * Class constructor
     * Creates the page and initializes its components
     */
    function __construct()
    {
        parent::__construct();
        
        // Create a panel
        $panel = new TPanelGroup('Segment');
       
        // Create a RecursiveDBTree instance to display segments
        $segment = new RecursiveDBTree('segment', 'app', 'Segment', 'id', 'parent_segment_id', 'description', 'id asc');
        $segment->collapse();
        
        // Set an action when selecting an item
        $segment->setItemAction(new TAction(array($this, 'onSelect')));
        
        // Add options to the context menu
        $segment->addContextMenuOption('Edit', new TAction([$this, 'onEdit']));
        $segment->addContextMenuOption('Delete', new TAction([$this, 'onDelete']));
        $segment->addContextMenuOption('View', new TAction([$this, 'onView']));
        
        $panel->add($segment);
        
        // Wrap the page content using a vertical box
        $vbox = new TVBox;
        $vbox->style = 'width: 100%';
        $vbox->add($panel);

        parent::add($vbox);
    }
    
    /**
     * Handle item selection
     * Displays information about the selected item
     */
    public function onSelect($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }    
    
    /**
     * Handle item editing
     * Displays information about the item being edited
     */
    public function onEdit($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }    

    /**
     * Handle item deletion
     * Displays information about the item being deleted
     */
    public function onDelete($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }    

    /**
     * Handle item viewing
     * Displays information about the item being viewed
     */
    public function onView($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }  
}