Source of file SystemJSTreeBuilder.php

Size: 4,434 Bytes - Last Modified: 2016-05-18T03:08:27+02:00

buildproject/core/module_system/system/SystemJSTreeBuilder.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
<?php
/*"******************************************************************************************************
*   (c) 2004-2006 by MulchProductions, www.mulchprod.de                                                 *
*   (c) 2007-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$                                *
********************************************************************************************************/


namespace Kajona\System\System;

/**
 * Base class for the tree nodes based on a class of type InterfaceJStreeNodeLoader.
 * This class actually retrieves the nodes for a tree based on the given JsTreeNodeLoader
 *
 * @package module_system
 * @author stefan.meyer1@yahoo.de
 *
 * @module system
 * @moduleId _system_modul_id_
 *
 */
class SystemJSTreeBuilder
{
    const STR_PARAM_INITIALTOGGLING = "jstree_initialtoggling";
    const STR_PARAM_LOADALLCHILDNOES = "jstree_loadallchildnodes";


    private $objNodeLoader = null;

    /**
     * SystemJSTreeBuilder constructor.
     *
     * @param InterfaceJStreeNodeLoader $objNodeGetter
     */
    public function __construct(InterfaceJStreeNodeLoader $objNodeGetter)
    {
        $this->objNodeLoader = $objNodeGetter;
    }

    /**
     * Retrieves nodes for a tree by the given path.
     *
     * @param array $arrSystemIdPath - array of system id's, id's contained in this array will be loaded
     *
     * @return SystemJSTreeNode[]
     */
    public function getNodesByPath(array $arrSystemIdPath)
    {

        if(empty($arrSystemIdPath)) {
            return true;
        }

        $strSystemId = array_shift($arrSystemIdPath);
        $arrChildren = $this->objNodeLoader->getChildNodes($strSystemId);

        $strSubId = array_key_exists(0, $arrSystemIdPath) ? $arrSystemIdPath[0] : null;
        foreach($arrChildren as $objChildNode) {

            if($strSubId !== null && $objChildNode->getStrId() == $strSubId) {
                $objChildNode->addStateAttr(SystemJSTreeNode::STR_NODE_STATE_OPENED, true);

                $arrSubchildNodes = $this->getNodesByPath($arrSystemIdPath);
                $objChildNode->setArrChildren($arrSubchildNodes);
            }
        }

        return $arrChildren;
    }


    /**
     * Method to get all child nodes for a given system id
     *
     * @param $strSystemId
     *
     * @return SystemJSTreeNode[]
     */
    public function getChildAllNodes($strSystemId)
    {
        $arrChildren = $this->objNodeLoader->getChildNodes($strSystemId);

        foreach($arrChildren as $objChildNode) {
            $objChildNode->addStateAttr(SystemJSTreeNode::STR_NODE_STATE_OPENED, true);

            $arrSubchildNodes = $this->getChildAllNodes($objChildNode->getStrId());
            $objChildNode->setArrChildren($arrSubchildNodes);
        }

        return $arrChildren;
    }


    /**
     * Returns the JSON reprensentation for the JSTree based on the retrieved nodes
     *
     * @param $arrSystemIdPath - array of system id's, id's contained in this array will be loaded
     * @param bool $bitInitialLoading
     *
     * @return string
     */
    public function getJson($arrSystemIdPath, $bitInitialLoading = false, $bitLoadAllSubnodes = false)
    {
        $arrNodes = $this->getNodesByPath($arrSystemIdPath);

        if($bitInitialLoading) {
            //root node is always first node in the array
            $objNode = $this->objNodeLoader->getNode($arrSystemIdPath[0]);
            $objNode->addStateAttr(SystemJSTreeNode::STR_NODE_STATE_OPENED, true);

            $objNode->setArrChildren($arrNodes);
            $arrNodes = $objNode;
        }
        else {
            foreach($arrNodes as $objSingleNode) {
                if($objSingleNode->getArrChildren() === true) {
                    if($bitLoadAllSubnodes) {
                        $objSingleNode->setArrChildren($this->getChildAllNodes($objSingleNode->getStrId()));
                    }
                    else {
                        $objSingleNode->setArrChildren($this->getNodesByPath(array($objSingleNode->getStrId())));
                    }
                }
            }
        }

        return json_encode($arrNodes);
    }
}