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

Revision

Zur aktuellen Revision | Details | 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 Spatie\Sluggable\HasSlug;
10
    use Spatie\Sluggable\SlugOptions;
11
 
12
    class Directory extends Model
13
    {
14
        use HasFactory, HasSlug;
15
 
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 );
27
        }
28
 
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;
40
 
41
            if ( isset( $GLOBALS["INI"]["Navigation"]["showArtikelstamm"] ) )
42
            {
43
                $showArtikelstamm = true;
44
            }
45
 
46
            $treebuffer = $this->with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $root )->where( 'articlemaster', '=' , 0 )->get();
47
 
48
 
49
            /*if (is_array($treebuffer) && is_array($linkDirs)) {
50
                $treebuffer = array_merge($treebuffer, $linkDirs);
51
            }*/
52
 
53
            $treebufferCount = 0;
54
            if ( is_countable( $treebuffer ) )
55
            {
56
                $treebufferCount = count( $treebuffer ) - 1;
57
            }
58
 
59
            $i = 0;
60
            if ( is_countable( $treebuffer ) )
61
            {
62
                foreach ( $treebuffer as $counter => $zeile )
63
                {
64
                    $k = $i % 2;
65
                    $i++;
66
 
67
                    /* ######################################################################## */ /* Link entsprechend der Alias-Definition generieren.                        */
68
                    /* ######################################################################## */
69
                    // Pfad ermitteln
70
                    $buffer = array();
71
 
72
                    $dirLink = "/directory/" . $zeile['id'] . "-" . $zeile["slug"] . "/";
73
 
74
 
75
                    // dirLink
76
                    //$zeile['dirLink']=$GLOBALS["INI"]["absoluteURL"]."/".implode("/",$buffer)."/";
77
                    $zeile['dirLink'] = $dirLink;
78
 
79
                    /* ######################################################################## */ /* Ausgabe der Unterverzeichnisse                                            */ /* ######################################################################## */
80
                    // Container-Kopf
81
                    $_SESSION['navigation']['nav-zebra'] = $k;
82
 
83
                    $subdirC = $this->with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $zeile["id"] )->where( 'articlemaster', '=', 0 )->get();
84
                    $subdirs = array();
85
                    foreach ( $subdirC as $subdir )
86
                    {
87
                        $subdirs[] = array(
88
                            "id"   => $subdir["id"],
89
                            "link" => "/directory/" . $subdir["id"] . "-" . $subdir['slug'] . "/",
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,
98
 
99
                    );
100
 
101
                }
102
            }
103
            $anzahl_spalten = 3;
104
 
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
            }
109
 
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
            }
115
 
116
            $time2 = microtime( true );
117
            $diff = $time2 - $time;
118
            $GLOBALS["zeit"][__function__ . "_" . $root] += $diff;
119
            return $nav;
120
        }
121
 
122
        /**
123
         * Wenn in der Config der Wert 'showNavigation' in der Kategorie 'Navigation' gleich "1" ist,
124
         * dann zeige die Navigation an.
125
         */
126
 
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;
134
            }
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
                }
154
            }
155
            elseif ( $GLOBALS["INI"]["navigation"]["cache_type"] == "1" )
156
            {
157
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu.xml";
158
 
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
                }
170
            }
171
 
172
            if ( !$navigation )
173
            {
174
                $navigation = $this->getMenuFromDB( $father );
175
            }
176
 
177
            $time2 = microtime( true );
178
            $diff = $time2 - $time;
179
            $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
180
            return $navigation;
181
        }
182
 
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
            }
199
 
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
 
210
            $dirs = self::with( 'directory' )->orderBy( "kennung" )->orderBy( "name" )->whereIn( "status", [ 0, 99 ] )->where( "directory_id", "=", $father )->where( 'articlemaster', '=', 0 )->get();
211
            foreach ( $dirs as $cnt => $dir )
212
            {
213
                $dirLink = "/directory/" . $dir["id"] . "-" . $dir["slug"] . "/";
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;
229
        }
230
 
231
        public function directory(): BelongsTo
232
        {
233
            return $this->belongsTo( Directory::class );
234
        }
235
 
236
        function medium(): HasMany
237
        {
238
            return $this->hasMany( DirectoryMedium::class )->orderBy( "rank" );
239
        }
240
        function template(): HasMany
241
        {
242
            return $this->hasMany( DirectoryTemplate::class );
243
        }
244
    }