Subversion-Projekte lars-tiefland.webanos.marine-sales.de

Revision

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

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