aboutsummaryrefslogtreecommitdiff
path: root/app/class/Colors.php
blob: bdea98ec88e2d0573658d3d478dc5784f1902184 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

namespace Wcms;

class Colors extends Item
{

    protected $file = Model::CSS_DIR . 'tagcolors.css';


    protected $rawcss = "";
    protected $tagcolor = [];


    public function __construct(string $file = 'tagcolors.css', array $taglist = [])
    {
        $this->setfile($file);
        if (file_exists($this->file)) {
            $this->rawcss = $this->readcssfile();
            $this->tagcolor = $this->parsetagcss($this->rawcss);
        }
        
        if (!empty($taglist)) {
            $this->tagcolor = $this->removeaddtags($taglist);
            $this->rawcss = $this->tocss($this->tagcolor);
            $this->writecssfile($this->file, $this->rawcss);
        }
    }

    /**
     * Read file containing css
     * @return string raw css or empty string
     */
    public function readcssfile(): string
    {
        $rawcss = file_get_contents($this->file);
        if (is_string($rawcss)) {
            return $rawcss;
        }
        return '';
    }

    /**
     * Check if new tags have been created and generate them a background color
     * @param array $taglist associative array using tag as key
     * @return array associative array of `tag => background-color`
     */
    public function removeaddtags(array $taglist = []): array
    {
        $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));
            }
        }
        return $tagcolor;
    }



    /**
     * Transform a CSS string in a array of datas
     * @param string $rawcss CSS string to parse
     * @return array associative array of `tag => background-color`
     */
    public function parsetagcss(string $rawcss): array
    {
        $pattern = '%.tag\_([a-z0-9\-\_]*)\s*\{\s*background-color:\s*(#[A-Fa-f0-9]{6})\;\s*\}%';
        preg_match_all($pattern, $rawcss, $matches);
        $tagcolor = array_combine($matches[1], $matches[2]);
        if ($tagcolor !== false) {
            return $tagcolor;
        } else {
            return [];
        }
    }

    /**
     * Generate CSS string from datas
     * @param array $tagcolor associative array of `tag => background-color`
     * @return string CSS
     */
    public function tocss(array $tagcolor): string
    {
        $css = "";
        foreach ($tagcolor as $tag => $color) {
            $css .= PHP_EOL  . '.tag_' . $tag . ' { background-color: ' . $color . '; }';
        }
        return $css;
    }

    /**
     * Write css in the file
     * @param string $rawcss
     * @throws \InvalidArgumentException If cant create
     */
    public function writecssfile(string $file, string $rawcss)
    {
        accessfile($file, true);
        if (!file_put_contents($file, $rawcss)) {
            throw new \InvalidArgumentException("cant create file : $this->file", 1);
        }
    }

    /**
     * Update tagcolors based on datas
     * @param array $tagcolor associative array of `tag => background-color`
     */
    public function update(array $tagcolor)
    {
        $this->settagcolor($tagcolor);
        $this->rawcss = $this->tocss($this->tagcolor);
        $this->writecssfile($this->file, $this->rawcss);
    }

    public function htmlcolorpicker(): string
    {
        $html = '<ul>';
        foreach ($this->tagcolor as $tag => $color) {
            $i = '<input type="color" name="tagcolor[' . $tag . ']" value="' . $color . '" id="color_' . $tag . '">';
            $l = '<label for="color_' . $tag . '" >' . $tag . '</label>';
            $html .= "\n<li>$i$l</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 _________________________

    /**
     * @throws \InvalidArgumentException If cant access file
     */
    public function setfile(string $path)
    {
        accessfile($path);
        $this->file = $path;
    }

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

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