Subversion-Projekte lars-tiefland.laravel_shop

Revision

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