Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 1657 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
2 lars 1
<?php
2
 
411 lars 3
    namespace App\Models;
2 lars 4
 
411 lars 5
    use Illuminate\Database\Eloquent\Factories\HasFactory;
6
    use Illuminate\Database\Eloquent\Model;
7
    use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
    use Illuminate\Database\Eloquent\Relations\HasMany;
1590 lars 9
    use Illuminate\Support\Collection;
411 lars 10
    use Spatie\Sluggable\HasSlug;
11
    use Spatie\Sluggable\SlugOptions;
181 lars 12
 
411 lars 13
    class Directory extends Model
169 lars 14
    {
411 lars 15
        use HasFactory, HasSlug;
181 lars 16
 
411 lars 17
        /**
18
         * Get the options for generating the slug.
19
         */
20
        public function getSlugOptions(): SlugOptions
21
        {
22
            return SlugOptions::create()
23
                ->generateSlugsFrom( 'name' )
24
                ->saveSlugsTo( 'slug' )
25
                ->usingLanguage( 'de' )
26
                ->doNotGenerateSlugsOnUpdate()
27
                ->skipGenerateWhen( fn() => $this->id < 0 );
176 lars 28
        }
29
 
411 lars 30
        public function navigation( $root, $override = false, $rootInfo = false, $cnt = 0 ): array
31
        {
32
            if ( !isset( $GLOBALS["count"][__function__ . "_" . $root] ) )
33
            {
34
                $GLOBALS["count"][__function__ . "_" . $root] = 0;
35
                $GLOBALS["zeit"][__function__ . "_" . $root] = 0;
36
            }
37
            $nav = array();
38
            $GLOBALS["count"][__function__ . "_" . $root]++;
39
            $time = microtime( true );
40
            $showArtikelstamm = false;
176 lars 41
 
411 lars 42
            if ( isset( $GLOBALS["INI"]["Navigation"]["showArtikelstamm"] ) )
43
            {
44
                $showArtikelstamm = true;
45
            }
176 lars 46
 
1582 lars 47
            $treebuffer = $this->with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $root )->where( 'articlemaster', '=', 0 )->get();
176 lars 48
 
49
 
411 lars 50
            /*if (is_array($treebuffer) && is_array($linkDirs)) {
51
                $treebuffer = array_merge($treebuffer, $linkDirs);
52
            }*/
176 lars 53
 
411 lars 54
            $treebufferCount = 0;
55
            if ( is_countable( $treebuffer ) )
56
            {
57
                $treebufferCount = count( $treebuffer ) - 1;
58
            }
176 lars 59
 
411 lars 60
            $i = 0;
61
            if ( is_countable( $treebuffer ) )
62
            {
63
                foreach ( $treebuffer as $counter => $zeile )
64
                {
65
                    $k = $i % 2;
66
                    $i++;
176 lars 67
 
411 lars 68
                    /* ######################################################################## */ /* Link entsprechend der Alias-Definition generieren.                        */
69
                    /* ######################################################################## */
70
                    // Pfad ermitteln
71
                    $buffer = array();
176 lars 72
 
1568 lars 73
                    $dirLink = "/directory/" . $zeile['id'] . "-" . $zeile["slug"] . "/";
176 lars 74
 
75
 
411 lars 76
                    // dirLink
77
                    //$zeile['dirLink']=$GLOBALS["INI"]["absoluteURL"]."/".implode("/",$buffer)."/";
78
                    $zeile['dirLink'] = $dirLink;
176 lars 79
 
411 lars 80
                    /* ######################################################################## */ /* Ausgabe der Unterverzeichnisse                                            */ /* ######################################################################## */
81
                    // Container-Kopf
82
                    $_SESSION['navigation']['nav-zebra'] = $k;
176 lars 83
 
432 lars 84
                    $subdirC = $this->with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $zeile["id"] )->where( 'articlemaster', '=', 0 )->get();
411 lars 85
                    $subdirs = array();
86
                    foreach ( $subdirC as $subdir )
87
                    {
88
                        $subdirs[] = array(
89
                            "id"   => $subdir["id"],
1568 lars 90
                            "link" => "/directory/" . $subdir["id"] . "-" . $subdir['slug'] . "/",
411 lars 91
                            "name" => $subdir["name"],
92
                        );
93
                    }
94
                    $nav["sub"][] = array(
95
                        "id"   => $zeile["id"],
96
                        "link" => $zeile['dirLink'],
97
                        "name" => $zeile["name"],
98
                        "sub2" => $subdirs,
176 lars 99
 
411 lars 100
                    );
101
 
102
                }
195 lars 103
            }
411 lars 104
            $anzahl_spalten = 3;
176 lars 105
 
411 lars 106
            if ( isset( $GLOBALS["INI"]["navigation"]["anzahl_spalten"] ) && is_numeric( $GLOBALS["INI"]["navigation"]["anzahl_spalten"] ) )
107
            {
108
                $anzahl_spalten = $GLOBALS["INI"]["navigation"]["anzahl_spalten"];
109
            }
176 lars 110
 
411 lars 111
            if ( isset( $nav["sub"] ) && is_array( $nav["sub"] ) )
112
            {
113
                $nav["sub"]['elemsPerRow'] = floor( count( $nav["sub"] ) / $anzahl_spalten );
114
                $nav["sub"]['elemsPerRowRest'] = count( $treebuffer ) - ( $nav["sub"]['elemsPerRow'] * $anzahl_spalten );
115
            }
176 lars 116
 
411 lars 117
            $time2 = microtime( true );
118
            $diff = $time2 - $time;
119
            $GLOBALS["zeit"][__function__ . "_" . $root] += $diff;
120
            return $nav;
121
        }
176 lars 122
 
411 lars 123
        /**
124
         * Wenn in der Config der Wert 'showNavigation' in der Kategorie 'Navigation' gleich "1" ist,
125
         * dann zeige die Navigation an.
126
         */
176 lars 127
 
411 lars 128
        public function nav_menu( $father = -1 )
129
        {
130
            $navigation = null;
131
            if ( !isset( $GLOBALS["count"][__function__ . "_" . $father] ) )
132
            {
133
                $GLOBALS["count"][__function__ . "_" . $father] = 0;
134
                $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
176 lars 135
            }
411 lars 136
            $GLOBALS["count"][__function__ . "_" . $father]++;
137
            $time = microtime( true );
138
            if ( $GLOBALS['INI']['navigation']['cache_type'] == "2" )
139
            {
140
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu_serialized.txt";
141
                if ( isset( $GLOBALS["INI"]["treeRootMainNav"] ) && $father == $GLOBALS["INI"]["treeRootMainNav"] )
142
                {
143
                    $fName = $_SERVER["DOCUMENT_ROOT"] . "/mainnav_serialized.txt";
144
                }
145
                if ( isset( $GLOBALS["INI"]["treeRooSecondNav"] ) && $father == $GLOBALS["INI"]["treeRootSecondNav"] )
146
                {
147
                    $fName = $_SERVER["DOCUMENT_ROOT"] . "/secondnav_serialized.txt";
148
                }
149
 
150
                if ( file_exists( $fName ) )
151
                {
152
                    $content = file_get_contents( $fName );
153
                    $navigation = unserialize( $content );
154
                }
176 lars 155
            }
411 lars 156
            elseif ( $GLOBALS["INI"]["navigation"]["cache_type"] == "1" )
157
            {
158
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu.xml";
176 lars 159
 
411 lars 160
                if ( file_exists( $fName ) )
161
                {
162
                    $confArr = array(
163
                        "root"     => "menu",
164
                        "encoding" => "UTF-8",
165
                    );
166
                    $nav = new Config();
167
                    $nav = $nav->parseConfig( $fName, "XML", $confArr );
168
                    $nav = $nav->toArray();
169
                    $navigation = $nav["root"];
170
                }
176 lars 171
            }
172
 
411 lars 173
            if ( !$navigation )
174
            {
175
                $navigation = $this->getMenuFromDB( $father );
176 lars 176
            }
177
 
411 lars 178
            $time2 = microtime( true );
179
            $diff = $time2 - $time;
180
            $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
181
            return $navigation;
176 lars 182
        }
183
 
411 lars 184
        private function getMenuFromDB( $father ): array
185
        {
186
            if ( !isset( $GLOBALS["count"][__function__ . "_" . $father] ) )
187
            {
188
                $GLOBALS["count"][__function__ . "_" . $father] = 0;
189
                $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
190
            }
191
            $GLOBALS["count"][__function__ . "_" . $father]++;
192
            $navigation = array();
193
            $time = microtime( true );
194
            $order_by = array( "kennung", "name" );
195
            $sql_erw = "AND artikelstamm = 0";
196
            if ( isset( $GLOBALS['INI']['navigation']['order_by'] ) )
197
            {
198
                $order_by = $GLOBALS['INI']['navigation']['order_by'];
199
            }
176 lars 200
 
411 lars 201
            //Sollen Artikelstämme in der Navigation rauskommen, dann hole auch Artikelstämme
202
            if ( isset( $GLOBALS["INI"]["Navigation"]["showArtikelstamm"] ) )
203
            {
204
                $sql_erw = "";
205
            }
206
            if ( isset( $GLOBALS["INI"]["navigation"]["sqlErweiterung"] ) )
207
            {
208
                $sql_erw .= " " . $GLOBALS["INI"]["navigation"]["sqlErweiterung"];
209
            }
210
 
432 lars 211
            $dirs = self::with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $father )->where( 'articlemaster', '=', 0 )->get();
411 lars 212
            foreach ( $dirs as $cnt => $dir )
213
            {
1568 lars 214
                $dirLink = "/directory/" . $dir["id"] . "-" . $dir["slug"] . "/";
411 lars 215
                if ( $dir["url"] )
216
                {
217
                    $dirLink = $dir["url"];
218
                }
219
                $navigation[$cnt]["top"][] = array( "id" => $dir["id"], "name" => $dir["name"], "dirLink" => $dirLink, "beschreibung" => $dir["description"], );
220
                $nav = $this->navigation( $dir["id"], false, true, $cnt );
221
                if ( isset( $nav["sub"] ) )
222
                {
223
                    $navigation[$cnt]["sub"] = $nav["sub"];
224
                }
225
            }
226
            $time2 = microtime( true );
227
            $diff = $time2 - $time;
228
            $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
229
            return $navigation;
176 lars 230
        }
231
 
411 lars 232
        public function directory(): BelongsTo
233
        {
234
            return $this->belongsTo( Directory::class );
176 lars 235
        }
236
 
411 lars 237
        function medium(): HasMany
238
        {
239
            return $this->hasMany( DirectoryMedium::class )->orderBy( "rank" );
181 lars 240
        }
1582 lars 241
 
622 lars 242
        function template(): HasMany
243
        {
626 lars 244
            return $this->hasMany( DirectoryTemplate::class );
622 lars 245
        }
1582 lars 246
 
1581 lars 247
        public function manufacturer(): BelongsTo
248
        {
249
            return $this->belongsTo( Manufacturer::class );
250
        }
251
 
1597 lars 252
        public function shopPosition(): Collection
1582 lars 253
        {
1597 lars 254
            $pos = collect(); //$pos = "Sie sind hier: <a href=\"/\">Startseite</a> <span class='breadcrumb-trenner'> &raquo; </span>";
1659 lars 255
            $path = getPath( $_SESSION['navigation']['position'] )->reverse();
1582 lars 256
            foreach ( $path as $dir )
257
            {
1598 lars 258
                $pos[] = $dir;
1582 lars 259
            }
1585 lars 260
 
1582 lars 261
            return $pos;
262
        }
263
 
176 lars 264
    }