diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-01-29 20:34:08 +0100 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-01-29 20:34:08 +0100 |
commit | b28f1a4a516f93044c632b254670dd21f52e625c (patch) | |
tree | aa2661016383e51fe26d564d208ac302f6e48926 /app/class/Colors.php | |
parent | a30e115d62f10183acb7ff946821aaf066d4a61e (diff) | |
download | wcms-b28f1a4a516f93044c632b254670dd21f52e625c.tar.gz wcms-b28f1a4a516f93044c632b254670dd21f52e625c.zip |
new feature : colored tags
Diffstat (limited to 'app/class/Colors.php')
-rw-r--r-- | app/class/Colors.php | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/app/class/Colors.php b/app/class/Colors.php new file mode 100644 index 0000000..ce37d62 --- /dev/null +++ b/app/class/Colors.php @@ -0,0 +1,96 @@ +<?php + +namespace Wcms; + +class Colors +{ + + protected $file = MODEL::CSS_DIR . 'tagcolors.css'; + + + protected $rawcss = ""; + protected $tagcolor = []; + + + + public function __construct(array $taglist = []) + { + if ($this->readcssfile()) { + $this->parsetagcss(); + } + if (!empty($taglist)) { + $this->removeaddtags($taglist); + $this->tocss(); + $this->writecssfile(); + } + } + + public function readcssfile(): bool + { + if (MODEL::dircheck(MODEL::CSS_DIR) && file_exists($this->file)) { + $this->rawcss = file_get_contents($this->file); + return true; + } else { + return false; + } + } + + public function removeaddtags(array $taglist = []) + { + $tagcolor = []; + foreach ($taglist as $tag => $tagcount) { + if (key_exists($tag, $this->tagcolor)) { + $tagcolor[$tag] = $this->tagcolor[$tag]; + } else { + $tagcolor[$tag] = '#' . dechex(rand(100, 255)) . dechex(rand(100, 255)) . dechex(rand(100, 255)); + } + } + $this->tagcolor = $tagcolor; + } + + + + /** + * Transform a CSS string in a array of `tag => background-color` + * + * @return array Ouput array using TAG as key and Hex Color as value + */ + public function parsetagcss() + { + $pattern = '%.tag\_([a-z0-9\-\_]*)\s*\{\s*background-color:\s*(#[A-Fa-f0-9]{6})\;\s*\}%'; + preg_match_all($pattern, $this->rawcss, $matches); + $tagcolor = array_combine($matches[1], $matches[2]); + if ($tagcolor !== false) { + $this->tagcolor = $tagcolor; + return true; + } else { + return false; + } + } + + public function tocss() + { + $css = ""; + foreach ($this->tagcolor as $tag => $color) { + $css .= PHP_EOL . '.tag_' . $tag . ' { background-color: ' . $color . '; }'; + } + $this->rawcss = $css; + } + + public function writecssfile() + { + if (MODEL::dircheck(MODEL::CSS_DIR)) { + return file_put_contents($this->file, $this->rawcss); + } + } + + public function htmlcolorpicker(array $csstagcolor): string + { + $html = '<ul>'; + foreach ($csstagcolor as $tag => $color) { + $html .= PHP_EOL . '<li><input type="color" name="colors[' . $tag . ']" value="' . $color . '"></li>'; + } + $html .= PHP_EOL . '</ul>'; + return $html; + } +} |