|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\App as MainApp; |
|
6
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
|
7
|
|
|
use Ffcms\Core\Cache\MemoryObject; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Any; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
10
|
|
|
use Ffcms\Core\Traits\SearchableTrait; |
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ContentCategory. Active record model for content category nesting |
|
15
|
|
|
* @package Apps\ActiveRecord |
|
16
|
|
|
* @property int $id |
|
17
|
|
|
* @property string $path |
|
18
|
|
|
* @property string $title |
|
19
|
|
|
* @property string $description |
|
20
|
|
|
* @property array $configs |
|
21
|
|
|
* @property string $created_at |
|
22
|
|
|
* @property string $updated_at |
|
23
|
|
|
*/ |
|
24
|
|
|
class ContentCategory extends ActiveModel |
|
25
|
|
|
{ |
|
26
|
|
|
use SearchableTrait; |
|
27
|
|
|
|
|
28
|
|
|
protected $casts = [ |
|
29
|
|
|
'id' => 'integer', |
|
30
|
|
|
'path' => 'string', |
|
31
|
|
|
'title' => 'serialize', |
|
32
|
|
|
'description' => 'serialize', |
|
33
|
|
|
'configs' => 'serialize' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
protected $searchable = [ |
|
37
|
|
|
'columns' => [ |
|
38
|
|
|
'path' => 8, |
|
39
|
|
|
'title' => 4, |
|
40
|
|
|
'description' => 2 |
|
41
|
|
|
] |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get all table rows as object |
|
46
|
|
|
* @param array $columns |
|
47
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[] |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function all($columns = ['*']) |
|
50
|
|
|
{ |
|
51
|
|
|
$cacheName = 'activerecord.contentcategory.all.' . implode('.', $columns); |
|
52
|
|
|
$records = MemoryObject::instance()->get($cacheName); |
|
53
|
|
|
if (!$records) { |
|
54
|
|
|
$records = parent::all($columns); |
|
55
|
|
|
MemoryObject::instance()->set($cacheName, $records); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $records; |
|
59
|
|
|
} |
|
60
|
|
|
/** |
|
61
|
|
|
* Get record via category path address |
|
62
|
|
|
* @param string $path |
|
63
|
|
|
* @return self|ActiveModel|Collection |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function getByPath($path = '') |
|
66
|
|
|
{ |
|
67
|
|
|
if (MainApp::$Memory->get('cache.content.category.path.' . $path) !== null) { |
|
68
|
|
|
return MainApp::$Memory->get('cache.content.category.path.' . $path); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$record = self::where('path', $path)->first(); |
|
72
|
|
|
MainApp::$Memory->set('cache.content.category.path.' . $path, $record); |
|
73
|
|
|
return $record; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Find category by id |
|
78
|
|
|
* @param int $id |
|
79
|
|
|
* @return self|object|null |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function getById($id) |
|
82
|
|
|
{ |
|
83
|
|
|
if (MainApp::$Memory->get('cache.content.category.id.' . $id) !== null) { |
|
84
|
|
|
return MainApp::$Memory->get('cache.content.category.id.' . $id); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$record = self::find($id); |
|
88
|
|
|
MainApp::$Memory->set('cache.content.category.id.' . $id, $record); |
|
89
|
|
|
return $record; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Build id-title array of sorted by nesting level categories |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function getSortedCategories(): array |
|
97
|
|
|
{ |
|
98
|
|
|
$response = []; |
|
99
|
|
|
$tmpData = self::getSortedAll(); |
|
100
|
|
|
foreach ($tmpData as $path => $data) { |
|
101
|
|
|
$title = null; |
|
102
|
|
|
if (Str::likeEmpty($path)) { |
|
103
|
|
|
$title .= '--'; |
|
104
|
|
|
} else { |
|
105
|
|
|
// set level marker based on slashes count in pathway |
|
106
|
|
|
$slashCount = Str::entryCount($path, '/'); |
|
107
|
|
|
for ($i=-1; $i <= $slashCount; $i++) { |
|
108
|
|
|
$title .= '--'; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
// add canonical title from db |
|
112
|
|
|
$title .= ' ' . $data->getLocaled('title'); |
|
113
|
|
|
// set response as array [id => title, ... ] |
|
114
|
|
|
$response[$data->id] = $title; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $response; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get all categories sorted by pathway |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function getSortedAll(): array |
|
125
|
|
|
{ |
|
126
|
|
|
$list = self::all(); |
|
127
|
|
|
$response = []; |
|
128
|
|
|
foreach ($list as $row) { |
|
129
|
|
|
$response[$row->path] = $row; |
|
130
|
|
|
} |
|
131
|
|
|
ksort($response); |
|
132
|
|
|
return $response; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get property by key of current category |
|
137
|
|
|
* @param string $key |
|
138
|
|
|
* @return bool|string|null |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getProperty($key) |
|
141
|
|
|
{ |
|
142
|
|
|
$properties = $this->configs; |
|
143
|
|
|
// check if properties is defined |
|
144
|
|
|
if (!Any::isArray($properties) || !array_key_exists($key, $properties)) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $properties[$key]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get parent category object |
|
153
|
|
|
* @return ContentCategory|null|object |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getParent() |
|
156
|
|
|
{ |
|
157
|
|
|
$path = $this->path; |
|
158
|
|
|
if (!Str::contains('/', $path)) { |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$arr = explode('/', $path); |
|
163
|
|
|
array_pop($arr); |
|
164
|
|
|
$parentPath = trim(implode('/', $arr), '/'); |
|
165
|
|
|
return self::getByPath($parentPath); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|