summaryrefslogtreecommitdiff
path: root/plugins/Timetrackingeditor/Html.php
blob: eb3e1fa5bd9c6b5c9df7b97b6d76d4f99f19671a (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php

namespace Kanboard\Plugin\Timetrackingeditor;

use SplFileObject;

/**
 * HTML Writer
 *
 * Allows exporting Data as HTML file.
 * In contrast to CSV this allows clean interpration of HTML Tags by Excel
 *
 * @author  Thomas Stinner
 */
class Html
{
    /**
     * CSV/SQL columns
     *
     * @access private
     * @var array
     */
    private $columns = array();

    /**
     * Constructor
     *
     * @access public
     */
    public function __construct()
    {
    }

    /**
     * Check boolean field value
     *
     * @static
     * @access public
     * @param  mixed $value
     * @return int
     */
    public static function getBooleanValue($value)
    {
        if (! empty($value)) {
            $value = trim(strtolower($value));
            return $value === '1' || $value{0} === 't' || $value{0} === 'y' ? 1 : 0;
        }

        return 0;
    }

    /**
     * Output CSV file to standard output
     *
     * @static
     * @access public
     * @param  array  $rows
     */
    public static function output(array $rows)
    {
        $html= new static;
        $html->write('php://output', $rows);
    }

    /**
     * Define column mapping between CSV and SQL columns
     *
     * @access public
     * @param  array $columns
     * @return Csv
     */
    public function setColumnMapping(array $columns)
    {
        $this->columns = $columns;
        return $this;
    }

    /**
     * Write HTML file
     *
     * @access public
     * @param  string    $filename
     * @param  array     $rows
     * @return Html
     */
    public function write($filename, array $rows)
    {

        $fp = fopen($filename, 'w');

        if (is_resource($fp)) {
            $types = array_shift($rows);

            $this->writeHeader($fp);
            foreach ($rows as $row) {
                $this->writeRow($fp, $types, $row);
            }
            $this->writeFooter($fp);
            fclose($fp);
        }

        return $this;
    }

  /**
   *  write a HTML header
   *
   * @param $fp filepointer
   */
    private function writeHeader($fp)
    {
      fwrite($fp,"<HTML><HEAD><STYLE>\n");
      fwrite($fp,"b,p {mso-data-placement: same-cell; }\n");
      fwrite($fp,".num { mso-number-format:General; }\n");
      fwrite($fp,".dec { mso-number-format: 0,00; }\n");
      fwrite($fp,".text { mso-number-format:\"\\@\"; }\n");
      fwrite($fp,".date { mso-number-format:\"Short Date\"; }\n");
      fwrite($fp,"</STYLE></HEAD>\n");
      fwrite($fp,"<BODY><TABLE>");

    }

  /**
   * write a single row
   *
   * @param fp filepointer
   * @param $row row
   */
    private function writeRow($fp, array $types, array $row)
    {
        fwrite($fp,"<tr>");
        foreach ($row as $key => $value) {
          fwrite($fp,"<td class='".$types[$key]."'>".$value."</td>");
        }
        fwrite($fp,"</tr>\n");
    }

  /**
   * write a HTML footer
   */
   private function writeFooter($fp)
   {
     fwrite($fp,"</TABLE></BODY></HTML>");
   }

    /**
     * Associate columns header with row values
     *
     * @access private
     * @param  array $row
     * @return array
     */
    private function associateColumns(array $row)
    {
        $line = array();
        $index = 0;

        foreach ($this->columns as $sql_name => $csv_name) {
            if (isset($row[$index])) {
                $line[$sql_name] = $row[$index];
            } else {
                $line[$sql_name] = '';
            }

            $index++;
        }

        return $line;
    }

    /**
     * Filter empty rows
     *
     * @access private
     * @param  array $row
     * @return array
     */
    private function filterRow(array $row)
    {
        return array_filter($row);
    }
}