diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-02-10 20:18:37 +0100 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-02-10 20:18:37 +0100 |
commit | e8b1632d64205d1294f9bb2783bba6be353a548d (patch) | |
tree | a5e75e7659cbafc79fa18888260ec76a99718d9b /app | |
parent | 84cb333456f50fcc3cc64ec6cd0d82fb7c826987 (diff) | |
download | wcms-e8b1632d64205d1294f9bb2783bba6be353a548d.tar.gz wcms-e8b1632d64205d1294f9bb2783bba6be353a548d.zip |
Refactoring element rendering close #60
Diffstat (limited to 'app')
-rw-r--r-- | app/class/Element.php | 62 | ||||
-rw-r--r-- | app/class/Modelrender.php | 35 |
2 files changed, 22 insertions, 75 deletions
diff --git a/app/class/Element.php b/app/class/Element.php index a08e219..d94c76f 100644 --- a/app/class/Element.php +++ b/app/class/Element.php @@ -10,14 +10,10 @@ class Element extends Item protected $type; protected $options; protected $sources = []; - protected $params = []; protected $autolink = 0; protected $markdown = 1; protected $content = ''; - const OPTIONS = ['autolink', 'markdown']; - - // __________________________________________________ F U N ____________________________________________________________ @@ -27,58 +23,21 @@ class Element extends Item { $this->hydrate($datas); $this->analyse($pageid); - } + } private function analyse(string $pageid) { if(!empty($this->options)) { - - // Replace "!" by the real page name - $this->options = str_replace('!', $pageid, $this->options); - - preg_match('~(:([a-z0-9-_+!]+))?(\/([a-z0-9-,_+=]+))?~', $this->options, $matches); - if(isset($matches[2]) && !empty($matches[2])) { - $this->sources = explode('+', $matches[2]); + $this->options = str_replace('*', $pageid, $this->options); + parse_str($this->options, $datas); + if (isset($datas['id'])) { + $this->sources = explode(' ', $datas['id']); } else { - $this->sources[] = $pageid; + $this->sources = [$pageid]; } - if(isset($matches[4])) { - $this->params = explode(',', $matches[4]); - } - - $this->readoptions(); - + $this->hydrate($datas); } else { - $this->sources[] = $pageid; - } - } - - private function readoptions() - { - if(!empty($this->params)) { - foreach ($this->params as $param ) { - preg_match('~([a-z0-9-_]+)(=(-?[0-9]+))?~', $param, $optionmatch); - if(isset($optionmatch[1])) { - $key = $optionmatch[1]; - } - if(isset($optionmatch[3])) { - $value = $optionmatch[3]; - } else { - $read = '<h2>Rendering error :</h2><p>Paramaters must have a value like : <strong><code>/' . $key . '=__value__</code></strong> for parameter : <strong><code>' . $key . '</code></strong></p>'; - //throw new Exception($read); - } - $method = 'set' . $key; - if (in_array($key, self::OPTIONS)) { - if (!$this->$method($value)) { - $read = '<h2>Rendering error :</h2><p>Invalid value input : <strong><code>' . $value . '</code></strong> for parameter : <strong><code>' . $key . '</code></strong></p>'; - //throw new Exception($read); - - } - } else { - $read = '<h2>Rendering error :</h2><p>Parameter name : <strong><code>' . $optionmatch[1] . '</code></strong> does not exist<p>'; - //throw new Exception($read); - } - } + $this->sources = [$pageid]; } } @@ -109,11 +68,6 @@ class Element extends Item return $this->options; } - public function params() - { - return $this->params; - } - public function sources() { return $this->sources; diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 6fc8932..3acee3c 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -128,14 +128,14 @@ class Modelrender extends Modelpage $types = ['HEADER', 'NAV', 'MAIN', 'ASIDE', 'FOOTER']; // First level regex - $regex = '~\%(' . implode("|", $types) . ')(\S*)\%~'; + $regex = '~\%(' . implode("|", $types) . ')(\?([\S]+))?\%~'; // Match the first level regex preg_match_all($regex, $body, $out); // Create a list of all the elements that passed through the first level regex foreach ($out[0] as $key => $match) { - $matches[$key] = ['fullmatch' => $match, 'type' => $out[1][$key], 'options' => $out[2][$key]]; + $matches[$key] = ['fullmatch' => $match, 'type' => $out[1][$key], 'options' => $out[3][$key]]; } @@ -143,7 +143,7 @@ class Modelrender extends Modelpage if(isset($matches)) { foreach ($matches as $key => $match) { $element = new Element($match, $this->page->id()); - $element->setcontent($this->getelementcontent($element)); + $element->setcontent($this->getelementcontent($element->sources(), $element->type())); $element->setcontent($this->elementparser($element)); $element->addtags(); $body = str_replace($element->fullmatch(), $element->content(), $body); @@ -157,25 +157,30 @@ class Modelrender extends Modelpage } - public function getelementcontent(Element $element) + /** + * Foreach $sources (pages), this will get the corresponding $type element content + * + * @param array $sources Array of pages ID + * @param string $type Type of element + */ + public function getelementcontent(array $sources, string $type) { $content = ''; $subseparator = PHP_EOL . PHP_EOL; - foreach($element->sources() as $source) + foreach($sources as $source) { if($source !== $this->page->id()) { - $subcontent = $this->getpageelement($source, $element->type()); + $subcontent = $this->getpageelement($source, $type); if($subcontent !== false) { if(empty($subcontent && self::RENDER_VERBOSE > 0)) { - $subcontent = PHP_EOL . '<!-- The ' . strtoupper($element->type()) . ' from page "' . $source . '" is currently empty ! -->' . PHP_EOL; + $subcontent = PHP_EOL . '<!-- The ' . strtoupper($type) . ' from page "' . $source . '" is currently empty ! -->' . PHP_EOL; } } else { - $read = '<h2>Rendering error :</h2><p>The page <strong><code>' . $source . '</code></strong>, called in <strong><code>'. $element->fullmatch() . '</code></strong>, does not exist yet.</p>'; + $read = '<h2>Rendering error :</h2><p>The page <strong><code>' . $source . '</code></strong>, does not exist yet.</p>'; //throw new Exception($read); } } else { - $type = $element->type(); $subcontent = $this->page->$type(); } $content .= $subseparator . $subcontent; @@ -191,10 +196,7 @@ class Modelrender extends Modelpage $content = $this->date($content); $content = $this->thumbnail($content); if($element->autolink()) { - $content = str_replace('%LINK%', '' ,$content); $content = $this->everylink($content, $element->autolink()); - } else { - $content = $this->taglink($content); } if($element->markdown()) { $content = $this->markdown($content); @@ -576,15 +578,6 @@ class Modelrender extends Modelpage return $text; } - public function taglink($text) - { - $rend = $this; - $text = preg_replace_callback('/\%LINK\%(.*)\%LINK\%/msU', function ($matches) use ($rend) { - return $rend->everylink($matches[1], 1); - }, $text); - return $text; - } - /** * Autolink Function : transform every word of more than $limit characters in internal link * |