flack /
openpsa
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @package midcom.templating |
||||
| 4 | * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
||||
| 5 | * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
||||
| 6 | * @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | namespace midcom\templating; |
||||
| 10 | |||||
| 11 | use midcom_services_cache_module_content; |
||||
| 12 | use midcom_core_context; |
||||
| 13 | use midgard_style; |
||||
| 14 | use midgard_element; |
||||
| 15 | use midcom_db_style; |
||||
| 16 | use midcom_error_forbidden; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Loader class for db-based templates |
||||
| 20 | * |
||||
| 21 | * @package midcom.templating |
||||
| 22 | */ |
||||
| 23 | class dbloader extends loader |
||||
| 24 | { |
||||
| 25 | private int $dbstyle; |
||||
| 26 | |||||
| 27 | private midcom_services_cache_module_content $content_cache; |
||||
| 28 | |||||
| 29 | public function __construct(string $theme_root, midcom_services_cache_module_content $content_cache) |
||||
| 30 | { |
||||
| 31 | parent::__construct($theme_root); |
||||
| 32 | $this->content_cache = $content_cache; |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | public function get_element(string $name, bool $scope_from_path) : ?string |
||||
| 36 | { |
||||
| 37 | $scope = 0; |
||||
| 38 | if ($scope_from_path) { |
||||
| 39 | // we have full qualified path to element |
||||
| 40 | $matches = []; |
||||
| 41 | if (preg_match("|(.*)/(.*)|", $name, $matches)) { |
||||
| 42 | $styleid = midcom_db_style::id_from_path($matches[1]); |
||||
| 43 | $name = $matches[2]; |
||||
| 44 | } |
||||
| 45 | $scope = $styleid ?? $this->dbstyle; |
||||
| 46 | } else { |
||||
| 47 | try { |
||||
| 48 | $root_topic = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC); |
||||
| 49 | if ($root_topic->style) { |
||||
| 50 | $scope = midcom_db_style::id_from_path($root_topic->style); |
||||
| 51 | } |
||||
| 52 | } catch (midcom_error_forbidden $e) { |
||||
| 53 | $e->log(); |
||||
| 54 | } |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | if ($scope && $content = $this->get_element_in_styletree($scope, $name)) { |
||||
| 58 | return $content; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | return parent::get_element($name, $scope_from_path); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | /** |
||||
| 65 | * Returns a style element that matches $name and is in style $id. |
||||
| 66 | * It also returns an element if it is not in the given style, |
||||
| 67 | * but in one of its parent styles. |
||||
| 68 | */ |
||||
| 69 | private function get_element_in_styletree(int $id, string $name) : ?string |
||||
| 70 | { |
||||
| 71 | $cache_key = $id . '::' . $name; |
||||
| 72 | if (array_key_exists($cache_key, $this->cache)) { |
||||
| 73 | return $this->cache[$cache_key]; |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | $element_mc = midgard_element::new_collector('style', $id); |
||||
| 77 | $element_mc->set_key_property('guid'); |
||||
| 78 | $element_mc->add_value_property('value'); |
||||
| 79 | $element_mc->add_constraint('name', '=', $name); |
||||
| 80 | $element_mc->execute(); |
||||
| 81 | |||||
| 82 | if ($element_guid = key($element_mc->list_keys())) { |
||||
| 83 | $this->content_cache->register($element_guid); |
||||
| 84 | return $this->add_to_cache($cache_key, $element_mc->get_subkey($element_guid, 'value')); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 85 | } |
||||
| 86 | |||||
| 87 | // No such element on this level, check parents |
||||
| 88 | $style_mc = midgard_style::new_collector('id', $id); |
||||
| 89 | $style_mc->set_key_property('guid'); |
||||
| 90 | $style_mc->add_value_property('up'); |
||||
| 91 | $style_mc->add_constraint('up', '>', 0); |
||||
| 92 | $style_mc->execute(); |
||||
| 93 | |||||
| 94 | if ($style_guid = key($style_mc->list_keys())) { |
||||
| 95 | $this->content_cache->register($style_guid); |
||||
| 96 | $up = $style_mc->get_subkey($style_guid, 'up'); |
||||
| 97 | return $this->get_element_in_styletree($up, $name); |
||||
|
0 ignored issues
–
show
It seems like
$up can also be of type false; however, parameter $id of midcom\templating\dbload..._element_in_styletree() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 98 | } |
||||
| 99 | |||||
| 100 | $this->cache[$cache_key] = null; |
||||
| 101 | return $this->cache[$cache_key]; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Initializes style sources |
||||
| 106 | */ |
||||
| 107 | public function initialize(midcom_core_context $context, ?string $style) |
||||
| 108 | { |
||||
| 109 | $this->dbstyle = 0; |
||||
| 110 | // get user defined style for component |
||||
| 111 | // style inheritance |
||||
| 112 | // should this be cached somehow? |
||||
| 113 | |||||
| 114 | if ($style && $this->dbstyle = midcom_db_style::id_from_path($style)) { |
||||
| 115 | if ($substyle = $context->get_key(MIDCOM_CONTEXT_SUBSTYLE)) { |
||||
| 116 | $chain = explode('/', $substyle); |
||||
| 117 | foreach ($chain as $stylename) { |
||||
| 118 | if ($_subst_id = midcom_db_style::id_from_path($stylename, $this->dbstyle)) { |
||||
| 119 | $this->dbstyle = $_subst_id; |
||||
| 120 | } |
||||
| 121 | } |
||||
| 122 | } |
||||
| 123 | } |
||||
| 124 | parent::initialize($context, $style); |
||||
| 125 | } |
||||
| 126 | } |
||||
| 127 |