1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.db |
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MidCOM level replacement for the Midgard Style record with framework support. |
11
|
|
|
* |
12
|
|
|
* The uplink is the owning Style. |
13
|
|
|
* |
14
|
|
|
* @property string $name Path name of the style |
15
|
|
|
* @property integer $up Style the style is under |
16
|
|
|
* @package midcom.db |
17
|
|
|
*/ |
18
|
|
|
class midcom_db_style extends midcom_core_dbaobject |
19
|
|
|
{ |
20
|
|
|
public string $__midcom_class_name__ = __CLASS__; |
21
|
|
|
public string $__mgdschema_class_name__ = 'midgard_style'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns the path of the style described by $id. |
25
|
|
|
*/ |
26
|
|
|
public static function path_from_id(int $id) : string |
27
|
|
|
{ |
28
|
|
|
static $path_cache = []; |
29
|
|
|
if (isset($path_cache[$id])) { |
30
|
|
|
return $path_cache[$id]; |
31
|
|
|
} |
32
|
|
|
// Construct the path |
33
|
|
|
$path_parts = []; |
34
|
|
|
$original_id = $id; |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
while (($style = new self($id))) { |
38
|
|
|
$path_parts[] = $style->name; |
39
|
|
|
$id = $style->up; |
40
|
|
|
|
41
|
|
|
if ($style->up == 0) { |
42
|
|
|
// Toplevel style |
43
|
|
|
break; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} catch (midcom_error) { |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$path_parts = array_reverse($path_parts); |
50
|
|
|
|
51
|
|
|
$path_cache[$original_id] = '/' . implode('/', $path_parts); |
52
|
|
|
|
53
|
|
|
return $path_cache[$original_id]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the id of the style described by $path. |
58
|
|
|
* |
59
|
|
|
* Note: $path already includes the element name, so $path looks like |
60
|
|
|
* "/rootstyle/style/style/element". |
61
|
|
|
* |
62
|
|
|
* @todo complete documentation |
63
|
|
|
* @param int $rootstyle ??? |
64
|
|
|
* @return int ID of the matching style or 0 |
65
|
|
|
*/ |
66
|
|
|
public static function id_from_path(string $path, $rootstyle = 0) : int |
67
|
|
|
{ |
68
|
|
|
if (str_starts_with($path, 'theme:')) { |
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
static $cached = []; |
72
|
|
|
|
73
|
|
|
$cache_key = $rootstyle . '::' . $path; |
74
|
|
|
|
75
|
|
|
if (array_key_exists($cache_key, $cached)) { |
76
|
|
|
return $cached[$cache_key]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$path = preg_replace("/^\/(.*)/", "$1", $path); // leading "/" |
80
|
|
|
$cached[$cache_key] = 0; |
81
|
|
|
$current_style = 0; |
82
|
|
|
|
83
|
|
|
$path_array = array_filter(explode('/', $path)); |
84
|
|
|
if (!empty($path_array)) { |
85
|
|
|
$current_style = $rootstyle; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($path_array as $path_item) { |
89
|
|
|
$mc = midgard_style::new_collector('up', $current_style); |
90
|
|
|
$mc->set_key_property('guid'); |
91
|
|
|
$mc->add_value_property('id'); |
92
|
|
|
$mc->add_constraint('name', '=', $path_item); |
93
|
|
|
$mc->execute(); |
94
|
|
|
|
95
|
|
|
if ($style_guid = key($mc->list_keys())) { |
96
|
|
|
$current_style = $mc->get_subkey($style_guid, 'id'); |
97
|
|
|
midcom::get()->cache->content->register($style_guid); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$cached[$cache_key] = $current_style; |
102
|
|
|
|
103
|
|
|
return $cached[$cache_key]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|