Subversion-Projekte lars-tiefland.laravel_shop

Revision

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