Subversion-Projekte lars-tiefland.laravel_shop

Revision

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