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\Serialize; |
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ContentCategory. Active record model for content category nesting |
13
|
|
|
* @package Apps\ActiveRecord |
14
|
|
|
* @property int $id |
15
|
|
|
* @property string $path |
16
|
|
|
* @property string $title |
17
|
|
|
* @property string $description |
18
|
|
|
* @property string $configs |
19
|
|
|
* @property string $created_at |
20
|
|
|
* @property string $updated_at |
21
|
|
|
*/ |
22
|
|
|
class ContentCategory extends ActiveModel |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get all table rows as object |
27
|
|
|
* @param array $columns |
28
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[] |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public static function all($columns = ['*']) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$cacheName = 'activerecord.contentcategory.all.' . implode('.', $columns); |
33
|
|
|
$records = MemoryObject::instance()->get($cacheName); |
34
|
|
|
if ($records === null) { |
35
|
|
|
$records = parent::all($columns); |
36
|
|
|
MemoryObject::instance()->set($cacheName, $records); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $records; |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* Get record via category path address |
43
|
|
|
* @param string $path |
44
|
|
|
* @return self|object|null |
45
|
|
|
*/ |
46
|
|
|
public static function getByPath($path = '') |
47
|
|
|
{ |
48
|
|
|
if (MainApp::$Memory->get('cache.content.category.path.' . $path) !== null) { |
49
|
|
|
return MainApp::$Memory->get('cache.content.category.path.' . $path); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$record = self::where('path', '=', $path)->first(); |
53
|
|
|
MainApp::$Memory->set('cache.content.category.path.' . $path, $record); |
54
|
|
|
return $record; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Find category by id |
59
|
|
|
* @param int $id |
60
|
|
|
* @return self|object|null |
61
|
|
|
*/ |
62
|
|
|
public static function getById($id) |
63
|
|
|
{ |
64
|
|
|
if (MainApp::$Memory->get('cache.content.category.id.' . $id) !== null) { |
65
|
|
|
return MainApp::$Memory->get('cache.content.category.id.' . $id); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$record = self::find($id); |
69
|
|
|
MainApp::$Memory->set('cache.content.category.id.' . $id, $record); |
70
|
|
|
return $record; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @deprecated |
75
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[] |
76
|
|
|
*/ |
77
|
|
|
public static function getAll() |
78
|
|
|
{ |
79
|
|
|
return self::all(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Build id-title array of sorted by nesting level categories |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public static function getSortedCategories() |
87
|
|
|
{ |
88
|
|
|
$response = []; |
89
|
|
|
$tmpData = self::getSortedAll(); |
90
|
|
|
foreach ($tmpData as $path => $data) { |
91
|
|
|
$title = null; |
92
|
|
|
if (Str::likeEmpty($path)) { |
93
|
|
|
$title .= '--'; |
94
|
|
|
} else { |
95
|
|
|
// set level marker based on slashes count in pathway |
96
|
|
|
$slashCount = Str::entryCount($path, '/'); |
97
|
|
|
for ($i=-1; $i <= $slashCount; $i++) { |
98
|
|
|
$title .= '--'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
// add canonical title from db |
102
|
|
|
$title .= ' ' . Serialize::getDecodeLocale($data->title); |
103
|
|
|
// set response as array [id => title, ... ] |
104
|
|
|
$response[$data->id] = $title; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $response; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get all categories sorted by pathway |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public static function getSortedAll() |
115
|
|
|
{ |
116
|
|
|
$list = self::all(); |
117
|
|
|
$response = []; |
118
|
|
|
foreach ($list as $row) { |
119
|
|
|
$response[$row->path] = $row; |
120
|
|
|
} |
121
|
|
|
ksort($response); |
122
|
|
|
return $response; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get property by key of current category |
127
|
|
|
* @param string $key |
128
|
|
|
* @return bool|string|null |
129
|
|
|
*/ |
130
|
|
|
public function getProperty($key) |
131
|
|
|
{ |
132
|
|
|
$properties = $this->configs; |
133
|
|
|
// check if properties is defined |
134
|
|
|
if (Str::likeEmpty($properties)) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$properties = Serialize::decode($properties); |
139
|
|
|
return $properties[$key]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.