aboutsummaryrefslogtreecommitdiff
path: root/app/class/Colors.php
blob: 0ad5ccad50be10ce67e9156d43c9f624b1212e4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

namespace Wcms;

class Colors extends Item
{

    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(50, 255)) . dechex(rand(50, 255)) . dechex(rand(50, 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(): string
    {
        $html = '<ul>';
        foreach ($this->tagcolor as $tag => $color) {
            $html .= PHP_EOL . '<li><input type="color" name="tagcolor[' . $tag . ']" value="' . $color . '" id="color_' . $tag . '"><label for="color_' . $tag . '" >' . $tag . '</label></li>';
        }
        $html .= PHP_EOL . '</ul>';
        return $html;
    }


    // ______________________ G E T _________________________

    public function rawcss()
    {
        return $this->rawcss;
    }

    public function tagcolor()
    {
        return $this->tagcolor;
    }

    // _______________________ S E T _________________________

    public function setrawcss($rawcss)
    {
        if(is_string($rawcss)) {
            $this->rawcss = $rawcss;
        }
    }

    public function settagcolor($tagcolor)
    {
        if(is_array($tagcolor)) {
            $this->tagcolor = $tagcolor;
        }
    }
}