Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 197 | Revision 201 | 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;
101
        dump($nav);
181 lars 102
        $GLOBALS["zeit"][__function__ . "_" . $root] += $diff;
176 lars 103
        return $nav;
104
    }
105
 
106
    /**
107
     * Wenn in der Config der Wert 'showNavigation' in der Kategorie 'Navigation' gleich "1" ist,
108
     * dann zeige die Navigation an.
109
     */
110
 
197 lars 111
    public function nav_menu($father = -1)
176 lars 112
    {
113
        $navigation = null;
181 lars 114
        if (!isset($GLOBALS["count"][__function__ . "_" . $father])) {
115
            $GLOBALS["count"][__function__ . "_" . $father] = 0;
116
            $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
176 lars 117
        }
181 lars 118
        $GLOBALS["count"][__function__ . "_" . $father]++;
119
        $time = microtime(true);
120
        if ($GLOBALS['INI']['navigation']['cache_type'] == "2") {
176 lars 121
            $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu_serialized.txt";
181 lars 122
            if (isset($GLOBALS["INI"]["treeRootMainNav"]) && $father == $GLOBALS["INI"]["treeRootMainNav"]) {
176 lars 123
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/mainnav_serialized.txt";
124
            }
181 lars 125
            if (isset($GLOBALS["INI"]["treeRooSecondNav"]) && $father == $GLOBALS["INI"]["treeRootSecondNav"]) {
176 lars 126
                $fName = $_SERVER["DOCUMENT_ROOT"] . "/secondnav_serialized.txt";
127
            }
128
 
181 lars 129
            if (file_exists($fName)) {
130
                $content = file_get_contents($fName);
131
                $navigation = unserialize($content);
176 lars 132
            }
181 lars 133
        } elseif ($GLOBALS["INI"]["navigation"]["cache_type"] == "1") {
176 lars 134
            $fName = $_SERVER["DOCUMENT_ROOT"] . "/menu.xml";
135
 
181 lars 136
            if (file_exists($fName)) {
176 lars 137
                $confArr = array(
181 lars 138
                    "root" => "menu",
176 lars 139
                    "encoding" => "UTF-8",
140
                );
141
                $nav = new Config();
181 lars 142
                $nav = $nav->parseConfig($fName, "XML", $confArr);
176 lars 143
                $nav = $nav->toArray();
144
                $navigation = $nav["root"];
145
            }
146
        }
147
 
181 lars 148
        if (!$navigation) {
149
            $navigation = $this->getMenuFromDB($father);
176 lars 150
        }
151
 
181 lars 152
        $time2 = microtime(true);
176 lars 153
        $diff = $time2 - $time;
181 lars 154
        $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
176 lars 155
        return $navigation;
156
    }
157
 
197 lars 158
    private function getMenuFromDB($father): array
176 lars 159
    {
181 lars 160
        if (!isset($GLOBALS["count"][__function__ . "_" . $father])) {
161
            $GLOBALS["count"][__function__ . "_" . $father] = 0;
162
            $GLOBALS["zeit"][__function__ . "_" . $father] = 0;
176 lars 163
        }
181 lars 164
        $GLOBALS["count"][__function__ . "_" . $father]++;
176 lars 165
        $navigation = array();
181 lars 166
        $time = microtime(true);
189 lars 167
        $order_by = array("kennung", "name");
178 lars 168
        $sql_erw = "AND artikelstamm = 0";
181 lars 169
        if (isset($GLOBALS['INI']['navigation']['order_by'])) {
176 lars 170
            $order_by = $GLOBALS['INI']['navigation']['order_by'];
171
        }
172
 
173
        //Sollen Artikelstämme in der Navigation rauskommen, dann hole auch Artikelstämme
181 lars 174
        if (isset($GLOBALS["INI"]["Navigation"]["showArtikelstamm"])) {
176 lars 175
            $sql_erw = "";
176
        }
181 lars 177
        if (isset($GLOBALS["INI"]["navigation"]["sqlErweiterung"])) {
176 lars 178
            $sql_erw .= " " . $GLOBALS["INI"]["navigation"]["sqlErweiterung"];
179
        }
180
 
190 lars 181
        $dirs = self::with('directory')->whereIn("status", [0, 2, 99])->where("directory_id", "=", $father)->get();
182 lars 182
        foreach ($dirs as $cnt => $dir) {
187 lars 183
            $dirLink = "/directory/" . $dir["id"] . "-" . $dir["slug"] . "/";
185 lars 184
            if ($dir["url"]) {
185
                $dirLink = $dir["url"];
186
            }
187
            $navigation[$cnt]["top"][] = array("id" => $dir["id"], "name" => $dir["name"], "dirLink" => $dirLink, "beschreibung" => $dir["description"],);
188 lars 188
            $nav = $this->navigation($dir["id"], false, true, $cnt);
194 lars 189
            if (isset($nav["sub"])) {
190
                $navigation[$cnt]["sub"] = $nav["sub"];
191
            }
181 lars 192
        }
193
        $time2 = microtime(true);
176 lars 194
        $diff = $time2 - $time;
181 lars 195
        $GLOBALS["zeit"][__function__ . "_" . $father] += $diff;
176 lars 196
        return $navigation;
197
    }
181 lars 198
 
197 lars 199
    public function directory(): BelongsTo
180 lars 200
    {
181 lars 201
        return $this->belongsTo(Directory::class);
180 lars 202
    }
2 lars 203
}