Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 1696 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php

namespace App\Http\Controllers;

use App\Models\Directory;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class DirectoryController extends Controller
{
    //
    public function index($id)
    {
        $_SESSION["navigation"]["position"] = $id;
        $subdir = Directory::with( 'medium.medium', 'template.template', 'manufacturer' )->where( "id", $id )->firstOrFail();
        $dirs = Directory::with( 'medium.medium','manufacturer' )->where( "directory_id", $id )->whereIn( 'status', [
            0,
            99
        ] )->orderBy( 'kennung' )->orderBy( 'name' )->get();
        $items = Item::with( 'price', 'medium.medium', 'manufacturer' )->where( "directory_id", $id )->where( 'status', '=', 0 )->get();
        $subdir["subdirs"] = $dirs->reject( function ( $value )
        {
            return $value->articlemaster == 1;
        } );
        $subdir["items"] = $items;
        $template = 'subdir';
        if ( $subdir->articlemaster == 1 )
        {
            $template = 'artstamm';
        }
        $artstaemmeItems = array();
        if ( is_countable( $subdir->template ) && isset( $subdir->template[0] ) )
        {
            $template = substr( $subdir->template[0]->template->template_value, strpos( $subdir->template[0]->template->template_value, "=" ) + 1 );
        }
        foreach ( $dirs as $dir )
        {
            if ( $dir->articlemaster == 1 )
            {
                $preise = array();
                $items = Item::with( 'price' )->where( "directory_id", $dir->id )->whereIn( "status", [ 0, 2 ] )->get();
                $dir->anz = count( $items );
                foreach ( $items as $item )
                {
                    $preise[] = $item->price[0]->preis;
                }
                if ( $preise )
                {
                    $startPrice = min( $preise );
                    $dir->mehrerePreise = count( array_unique( $preise ) ) > 1;
                    $dir->startpreis_tpl = $startPrice;
                }
                $subdir["items"] = $dirs->filter( function ( $value )
                {
                    return $value->articlemaster == 1 && $value->anz > 0;
                } );
                /*$artstaemmeItems[$dir->id] = $dir;
                $artstaemmeItems[$dir->id]["items"] = Item::with( 'medium.medium', 'price', 'manufacturer' )->where( 'directory_id', '=', $dir->id )->where( 'status', '=', 0 )->get();*/
            }
        }
        return view( $template, [
            "subdir"          => $subdir,
            "artstaemmeItems" => $artstaemmeItems,
        ] );
    }

    public function navigation( $root, $override = false, $rootInfo = false, $cnt = 0 ): array
    {
        if ( !isset( $GLOBALS["count"][__function__ . "_" . $root] ) )
        {
            $GLOBALS["count"][__function__ . "_" . $root] = 0;
            $GLOBALS["zeit"][__function__ . "_" . $root] = 0;
        }
        $nav = array();
        $GLOBALS["count"][__function__ . "_" . $root]++;
        $time = microtime( true );
        $showArtikelstamm = false;

        if ( isset( $GLOBALS["INI"]["Navigation"]["showArtikelstamm"] ) )
        {
            $showArtikelstamm = true;
        }

        $treebuffer = Directory::with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $root )->where( 'articlemaster', '=', 0 )->get();


        /*if (is_array($treebuffer) && is_array($linkDirs)) {
            $treebuffer = array_merge($treebuffer, $linkDirs);
        }*/

        $treebufferCount = 0;
        if ( is_countable( $treebuffer ) )
        {
            $treebufferCount = count( $treebuffer ) - 1;
        }

        $i = 0;
        if ( is_countable( $treebuffer ) )
        {
            foreach ( $treebuffer as $counter => $zeile )
            {
                $k = $i % 2;
                $i++;

                /* ######################################################################## */ /* Link entsprechend der Alias-Definition generieren.                        */
                /* ######################################################################## */
                // Pfad ermitteln
                $buffer = array();

                $dirLink = "/directory/" . $zeile['id'] . "-" . $zeile["slug"] . "/";


                // dirLink
                //$zeile['dirLink']=$GLOBALS["INI"]["absoluteURL"]."/".implode("/",$buffer)."/";
                $zeile['dirLink'] = $dirLink;

                /* ######################################################################## */ /* Ausgabe der Unterverzeichnisse                                            */ /* ######################################################################## */
                // Container-Kopf
                $_SESSION['navigation']['nav-zebra'] = $k;

                $subdirC = Directory::with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $zeile["id"] )->where( 'articlemaster', '=', 0 )->get();
                $subdirs = array();
                foreach ( $subdirC as $subdir )
                {
                    $subdirs[] = array(
                        "id"   => $subdir["id"],
                        "link" => "/directory/" . $subdir["id"] . "-" . $subdir['slug'] . "/",
                        "name" => $subdir["name"],
                    );
                }
                $nav["sub"][] = array(
                    "id"   => $zeile["id"],
                    "link" => $zeile['dirLink'],
                    "name" => $zeile["name"],
                    "sub2" => $subdirs,

                );

            }
        }
        $anzahl_spalten = 3;

        if ( isset( $GLOBALS["INI"]["navigation"]["anzahl_spalten"] ) && is_numeric( $GLOBALS["INI"]["navigation"]["anzahl_spalten"] ) )
        {
            $anzahl_spalten = $GLOBALS["INI"]["navigation"]["anzahl_spalten"];
        }

        if ( isset( $nav["sub"] ) && is_array( $nav["sub"] ) )
        {
            $nav["sub"]['elemsPerRow'] = floor( count( $nav["sub"] ) / $anzahl_spalten );
            $nav["sub"]['elemsPerRowRest'] = count( $treebuffer ) - ( $nav["sub"]['elemsPerRow'] * $anzahl_spalten );
        }

        $time2 = microtime( true );
        $diff = $time2 - $time;
        $GLOBALS["zeit"][__function__ . "_" . $root] += $diff;
        return $nav;
    }

    /**
     * Wenn in der Config der Wert 'showNavigation' in der Kategorie 'Navigation' gleich "1" ist,
     * dann zeige die Navigation an.
     */

    public function nav_menu( $father = -1 )
    {
        $navigation = null;
        if ( !isset( $GLOBALS["count"][__function__ . "_" . $father] ) )
        {
            $GLOBALS["count"][__function__ . "_" . $father] = 0;
            $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
        }
        $GLOBALS["count"][__function__ . "_" . $father]++;
        $time = microtime( true );
        if ( $GLOBALS['INI']['navigation']['cache_type'] == "2" )
        {
            $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu_serialized.txt";
            if ( isset( $GLOBALS["INI"]["treeRootMainNav"] ) && $father == $GLOBALS["INI"]["treeRootMainNav"] )
            {
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/mainnav_serialized.txt";
            }
            if ( isset( $GLOBALS["INI"]["treeRooSecondNav"] ) && $father == $GLOBALS["INI"]["treeRootSecondNav"] )
            {
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/secondnav_serialized.txt";
            }

            if ( file_exists( $fName ) )
            {
                $content = file_get_contents( $fName );
                $navigation = unserialize( $content );
            }
        }
        elseif ( $GLOBALS["INI"]["navigation"]["cache_type"] == "1" )
        {
            $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu.xml";

            if ( file_exists( $fName ) )
            {
                $confArr = array(
                    "root"     => "menu",
                    "encoding" => "UTF-8",
                );
                $nav = new Config();
                $nav = $nav->parseConfig( $fName, "XML", $confArr );
                $nav = $nav->toArray();
                $navigation = $nav["root"];
            }
        }

        if ( !$navigation )
        {
            $navigation = $this->getMenuFromDB( $father );
        }

        $time2 = microtime( true );
        $diff = $time2 - $time;
        $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
        return $navigation;
    }

    private function getMenuFromDB( $father ): array
    {
        if ( !isset( $GLOBALS["count"][__function__ . "_" . $father] ) )
        {
            $GLOBALS["count"][__function__ . "_" . $father] = 0;
            $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
        }
        $GLOBALS["count"][__function__ . "_" . $father]++;
        $navigation = array();
        $time = microtime( true );
        $order_by = array( "kennung", "name" );
        $sql_erw = "AND artikelstamm = 0";
        if ( isset( $GLOBALS['INI']['navigation']['order_by'] ) )
        {
            $order_by = $GLOBALS['INI']['navigation']['order_by'];
        }

        //Sollen Artikelstämme in der Navigation rauskommen, dann hole auch Artikelstämme
        if ( isset( $GLOBALS["INI"]["Navigation"]["showArtikelstamm"] ) )
        {
            $sql_erw = "";
        }
        if ( isset( $GLOBALS["INI"]["navigation"]["sqlErweiterung"] ) )
        {
            $sql_erw .= " " . $GLOBALS["INI"]["navigation"]["sqlErweiterung"];
        }

        $dirs = Directory::with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $father )->where( 'articlemaster', '=', 0 )->get();
        foreach ( $dirs as $cnt => $dir )
        {
            $dirLink = "/directory/" . $dir["id"] . "-" . $dir["slug"] . "/";
            if ( $dir["url"] )
            {
                $dirLink = $dir["url"];
            }
            $navigation[$cnt]["top"][] = array( "id" => $dir["id"], "name" => $dir["name"], "dirLink" => $dirLink, "beschreibung" => $dir["description"], );
            $nav = $this->navigation( $dir["id"], false, true, $cnt );
            if ( isset( $nav["sub"] ) )
            {
                $navigation[$cnt]["sub"] = $nav["sub"];
            }
        }
        $time2 = microtime( true );
        $diff = $time2 - $time;
        $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
        return $navigation;
    }

    public function shopPosition(): Collection
    {
        $pos = collect(); //$pos = "Sie sind hier: <a href=\"/\">Startseite</a> <span class='breadcrumb-trenner'> &raquo; </span>";
        $path = getPath( $_SESSION['navigation']['position'] )->reverse();
        foreach ( $path as $dir )
        {
            $pos[] = $dir;
        }

        return $pos;
    }
}