Subversion-Projekte lars-tiefland.webanos.faltradxxs.de

Revision

Revision 2 | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\Relations\BelongsTo;
    use Illuminate\Database\Eloquent\Relations\HasMany;
    use Illuminate\Support\Collection;
    use Spatie\Sluggable\HasSlug;
    use Spatie\Sluggable\SlugOptions;

    class Directory extends Model
    {
        use HasFactory, HasSlug;

        /**
         * Get the options for generating the slug.
         */
        public function getSlugOptions(): SlugOptions
        {
            return SlugOptions::create()
                ->generateSlugsFrom( 'name' )
                ->saveSlugsTo( 'slug' )
                ->usingLanguage( 'de' )
                ->doNotGenerateSlugsOnUpdate()
                ->skipGenerateWhen( fn() => $this->id < 0 );
        }

        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 = $this->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 = $this->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 = self::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 directory(): BelongsTo
        {
            return $this->belongsTo( Directory::class );
        }

        function medium(): HasMany
        {
            return $this->hasMany( DirectoryMedium::class )->orderBy( "rank" );
        }

        function template(): HasMany
        {
            return $this->hasMany( DirectoryTemplate::class );
        }

        public function manufacturer(): BelongsTo
        {
            return $this->belongsTo( Manufacturer::class );
        }

        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;
        }

    }