Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 1583 | Revision 1585 | 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
 
1584 lars 251
        public function shopPosition( ?Item $item): 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
            {
257
                //Wenn man auf der Artikeldetailseite ist und der Artikel in einem Artikelstamm liegt dann darf nicht zusätzlich der Artikelstamm angezeigt werden
1583 lars 258
                if ( $item && $dir->articlemaster == 1 )
1582 lars 259
                {
260
 
261
                    $isItemImArtikelstamm = true;
262
                }
263
                else
264
                {
265
                    $buffer[] = $dir['ID'] . "-" . str2url( $dir['Name'] );
266
                    $isItemImArtikelstamm = false;
267
                }
268
 
269
                if ( !$isItemImArtikelstamm )
270
                {
271
                    if ( $GLOBALS["INI"]['alias'] == 0 )
272
                    {
273
                        $link = "?navigation=" . $dir['ID'];
274
                    }
275
                    else
276
                    {
277
                        $link = "/" . implode( "/", $buffer ) . "/";
278
                    }
279
 
280
                    /*if ( $dir['Artikelstamm'] == 0 )
281
                    * {
282
                    * $pos .= "<a href=\"$link\">" . $dir['Name'] . "</a>";
283
                    * }
284
                    * else
285
                    * {*/ //$pos .= "<a href=\"$link\">" . $dir['Name'] . "</a>";
286
                    //}
287
 
288
                    $pos[] = array(
289
                        "link"  => $link,
290
                        "name"  => $dir["Name"],
291
                        "dirID" => ( $dir["ID"] ),
292
                    );
293
                }
294
 
295
            }
296
 
297
            if ( is_array( $item ) )
298
            {
299
                $pos[] = array(
300
                    "link" => $item['itemLink'],
301
                    "name" => $item['kurzbezeichnung'],
302
                );
303
            }
304
            return $pos;
305
        }
306
 
307
        private function getPath_new( $root, $return_locked_folders = false ): array
308
        {
309
            $conn = $GLOBALS["shopDB"];
310
            if ( isset( $GLOBALS["remoteDB"] ) && is_object( $GLOBALS["remoteDB"] ) )
311
            {
312
                $conn = $GLOBALS["remoteDB"];
313
            }
314
            $ini = array();
315
            if ( !isset( $_SESSION["INI"] ) || !is_array( $_SESSION["INI"] ) )
316
            {
317
                if ( isset( $GLOBALS["INI"] ) )
318
                {
319
                    $ini = $GLOBALS["INI"];
320
                }
321
            }
322
            else
323
            {
324
                $ini = $_SESSION["INI"];
325
            }
326
            $treeRoot = -1;
327
            if ( $ini["treeRoot"] )
328
            {
329
                $treeRoot = $ini["treeRoot"];
330
            }
331
 
332
            $path = array();
333
            $father = $root;
334
            while ( $father != -1 && !is_null( $father ) )
335
            {
336
                $sql = "
337
                SELECT
338
                    *
339
                FROM
340
                    directory
341
                WHERE
342
                    ID=" . $father . "
343
                AND
344
                    language='" . $ini["language"] . "'
345
            ";
346
                $status_sql = "
347
                AND
348
                    status IN (0,2)
349
            ";
350
                if ( $return_locked_folders === false )
351
                {
352
                    $sql .= $status_sql;
353
                }
354
                $res = $conn->query( $sql );
355
                if ( !$res )
356
                {
357
                    return array();
358
                }
359
                $row = $res->fetch_assoc();
360
                if ( !is_array( $row ) )
361
                {
362
                    return array();
363
                }
364
                $father = $row["Father"];
365
                foreach ( array(
366
                    "",
367
                    "2",
368
                    "3",
369
                ) as $i )
370
                {
371
                    if ( $row["bild_url" . $i] )
372
                    {
373
                        $row["bild_url" . $i . "_beurer"] = $ini['itemContainerKorrektur']["imagePath" . $i] . $row["bild_url" . $i];
374
                        $row["bild_url" . $i] = "<img src=" . $ini['itemContainerKorrektur']["imagePath" . $i] . $row["bild_url" . $i] . " >";
375
                    }
376
                    else
377
                    {
378
                        $row["bild_url" . $i] = "";
379
                    }
380
                }
381
                $path[] = $row;
382
            }
383
            $path = array_reverse( $path );
384
            if ( $path[0]["Father"] != $treeRoot )
385
            {
386
                unset( $path[0] );
387
            }
388
            if ( !is_array( $path ) )
389
            {
390
                $path = array();
391
            }
392
            return $path;
393
        }
394
 
176 lars 395
    }