Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 198 | Revision 202 | 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;
197 lars 7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
169 lars 8
use Spatie\Sluggable\HasSlug;
9
use Spatie\Sluggable\SlugOptions;
181 lars 10
 
2 lars 11
class Directory extends Model
12
{
169 lars 13
    use HasFactory, HasSlug;
14
 
15
    /**
16
     * Get the options for generating the slug.
17
     */
18
    public function getSlugOptions(): SlugOptions
19
    {
20
        return SlugOptions::create()
21
            ->generateSlugsFrom('name')
22
            ->saveSlugsTo('slug')
23
            ->usingLanguage('de')
24
            ->doNotGenerateSlugsOnUpdate()
181 lars 25
            ->skipGenerateWhen(fn() => $this->id < 0);
169 lars 26
    }
181 lars 27
 
197 lars 28
    public function navigation($root, $override = false, $rootInfo = false, $cnt = 0): array
176 lars 29
    {
181 lars 30
        if (!isset($GLOBALS["count"][__function__ . "_" . $root])) {
31
            $GLOBALS["count"][__function__ . "_" . $root] = 0;
32
            $GLOBALS["zeit"][__function__ . "_" . $root] = 0;
176 lars 33
        }
193 lars 34
        $nav = array();
181 lars 35
        $GLOBALS["count"][__function__ . "_" . $root]++;
36
        $time = microtime(true);
176 lars 37
        $showArtikelstamm = false;
38
 
181 lars 39
        if (isset($GLOBALS["INI"]["Navigation"]["showArtikelstamm"])) {
176 lars 40
            $showArtikelstamm = true;
41
        }
42
 
181 lars 43
        $treebuffer = $this->where("directory_id", "=", $root)->get();
176 lars 44
 
45
 
197 lars 46
        /*if (is_array($treebuffer) && is_array($linkDirs)) {
181 lars 47
            $treebuffer = array_merge($treebuffer, $linkDirs);
197 lars 48
        }*/
176 lars 49
 
50
        $treebufferCount = 0;
181 lars 51
        if (is_countable($treebuffer)) {
52
            $treebufferCount = count($treebuffer) - 1;
176 lars 53
        }
54
 
55
        $i = 0;
196 lars 56
        if (is_countable($treebuffer)) {
181 lars 57
            foreach ($treebuffer as $counter => $zeile) {
176 lars 58
                $k = $i % 2;
59
                $i++;
60
 
61
                /* ######################################################################## */ /* Link entsprechend der Alias-Definition generieren.                        */
62
                /* ######################################################################## */
188 lars 63
                // Pfad ermitteln
64
                $buffer = array();
176 lars 65
 
188 lars 66
                $dirLink = "/directory/" . $zeile['id'] . "-" . $zeile["slug"] . "/";
176 lars 67
 
68
 
195 lars 69
                // dirLink
70
                //$zeile['dirLink']=$GLOBALS["INI"]["absoluteURL"]."/".implode("/",$buffer)."/";
71
                $zeile['dirLink'] = $dirLink;
176 lars 72
 
195 lars 73
                /* ######################################################################## */ /* Ausgabe der Unterverzeichnisse                                            */ /* ######################################################################## */
74
                // Container-Kopf
75
                $_SESSION['navigation']['nav-zebra'] = $k;
176 lars 76
 
196 lars 77
                $subdirs = $this->where("directory_id", "=", $zeile["id"])->get();
195 lars 78
                $nav["sub"][] = array(
196 lars 79
                    "id" => $zeile["id"],
198 lars 80
                    "link" => $zeile['dirLink'],
196 lars 81
                    "name" => $zeile["name"],
195 lars 82
                    "sub2" => $subdirs,
176 lars 83
 
195 lars 84
                );
176 lars 85
 
195 lars 86
            }
176 lars 87
        }
88
        $anzahl_spalten = 3;
89
 
191 lars 90
        if (isset($GLOBALS["INI"]["navigation"]["anzahl_spalten"]) && is_numeric($GLOBALS["INI"]["navigation"]["anzahl_spalten"])) {
176 lars 91
            $anzahl_spalten = $GLOBALS["INI"]["navigation"]["anzahl_spalten"];
92
        }
93
 
192 lars 94
        if (isset($nav["sub"]) && is_array($nav["sub"])) {
181 lars 95
            $nav["sub"]['elemsPerRow'] = floor(count($nav["sub"]) / $anzahl_spalten);
96
            $nav["sub"]['elemsPerRowRest'] = count($treebuffer) - ($nav["sub"]['elemsPerRow'] * $anzahl_spalten);
176 lars 97
        }
98
 
181 lars 99
        $time2 = microtime(true);
197 lars 100
        $diff = $time2 - $time;
181 lars 101
        $GLOBALS["zeit"][__function__ . "_" . $root] += $diff;
176 lars 102
        return $nav;
103
    }
104
 
105
    /**
106
     * Wenn in der Config der Wert 'showNavigation' in der Kategorie 'Navigation' gleich "1" ist,
107
     * dann zeige die Navigation an.
108
     */
109
 
197 lars 110
    public function nav_menu($father = -1)
176 lars 111
    {
112
        $navigation = null;
181 lars 113
        if (!isset($GLOBALS["count"][__function__ . "_" . $father])) {
114
            $GLOBALS["count"][__function__ . "_" . $father] = 0;
115
            $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
176 lars 116
        }
181 lars 117
        $GLOBALS["count"][__function__ . "_" . $father]++;
118
        $time = microtime(true);
119
        if ($GLOBALS['INI']['navigation']['cache_type'] == "2") {
176 lars 120
            $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu_serialized.txt";
181 lars 121
            if (isset($GLOBALS["INI"]["treeRootMainNav"]) && $father == $GLOBALS["INI"]["treeRootMainNav"]) {
176 lars 122
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/mainnav_serialized.txt";
123
            }
181 lars 124
            if (isset($GLOBALS["INI"]["treeRooSecondNav"]) && $father == $GLOBALS["INI"]["treeRootSecondNav"]) {
176 lars 125
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/secondnav_serialized.txt";
126
            }
127
 
181 lars 128
            if (file_exists($fName)) {
129
                $content = file_get_contents($fName);
130
                $navigation = unserialize($content);
176 lars 131
            }
181 lars 132
        } elseif ($GLOBALS["INI"]["navigation"]["cache_type"] == "1") {
176 lars 133
            $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu.xml";
134
 
181 lars 135
            if (file_exists($fName)) {
176 lars 136
                $confArr = array(
181 lars 137
                    "root" => "menu",
176 lars 138
                    "encoding" => "UTF-8",
139
                );
140
                $nav = new Config();
181 lars 141
                $nav = $nav->parseConfig($fName, "XML", $confArr);
176 lars 142
                $nav = $nav->toArray();
143
                $navigation = $nav["root"];
144
            }
145
        }
146
 
181 lars 147
        if (!$navigation) {
148
            $navigation = $this->getMenuFromDB($father);
176 lars 149
        }
150
 
181 lars 151
        $time2 = microtime(true);
176 lars 152
        $diff = $time2 - $time;
181 lars 153
        $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
176 lars 154
        return $navigation;
155
    }
156
 
197 lars 157
    private function getMenuFromDB($father): array
176 lars 158
    {
181 lars 159
        if (!isset($GLOBALS["count"][__function__ . "_" . $father])) {
160
            $GLOBALS["count"][__function__ . "_" . $father] = 0;
161
            $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
176 lars 162
        }
181 lars 163
        $GLOBALS["count"][__function__ . "_" . $father]++;
176 lars 164
        $navigation = array();
181 lars 165
        $time = microtime(true);
189 lars 166
        $order_by = array("kennung", "name");
178 lars 167
        $sql_erw = "AND artikelstamm = 0";
181 lars 168
        if (isset($GLOBALS['INI']['navigation']['order_by'])) {
176 lars 169
            $order_by = $GLOBALS['INI']['navigation']['order_by'];
170
        }
171
 
172
        //Sollen Artikelstämme in der Navigation rauskommen, dann hole auch Artikelstämme
181 lars 173
        if (isset($GLOBALS["INI"]["Navigation"]["showArtikelstamm"])) {
176 lars 174
            $sql_erw = "";
175
        }
181 lars 176
        if (isset($GLOBALS["INI"]["navigation"]["sqlErweiterung"])) {
176 lars 177
            $sql_erw .= " " . $GLOBALS["INI"]["navigation"]["sqlErweiterung"];
178
        }
179
 
190 lars 180
        $dirs = self::with('directory')->whereIn("status", [0, 2, 99])->where("directory_id", "=", $father)->get();
182 lars 181
        foreach ($dirs as $cnt => $dir) {
187 lars 182
            $dirLink = "/directory/" . $dir["id"] . "-" . $dir["slug"] . "/";
185 lars 183
            if ($dir["url"]) {
184
                $dirLink = $dir["url"];
185
            }
186
            $navigation[$cnt]["top"][] = array("id" => $dir["id"], "name" => $dir["name"], "dirLink" => $dirLink, "beschreibung" => $dir["description"],);
188 lars 187
            $nav = $this->navigation($dir["id"], false, true, $cnt);
194 lars 188
            if (isset($nav["sub"])) {
189
                $navigation[$cnt]["sub"] = $nav["sub"];
190
            }
181 lars 191
        }
192
        $time2 = microtime(true);
176 lars 193
        $diff = $time2 - $time;
181 lars 194
        $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
176 lars 195
        return $navigation;
196
    }
181 lars 197
 
197 lars 198
    public function directory(): BelongsTo
180 lars 199
    {
181 lars 200
        return $this->belongsTo(Directory::class);
180 lars 201
    }
2 lars 202
}