source: contrib/Timesheet/inc/class.export_openoffice.inc.php @ 3526

Revision 3526, 13.7 KB checked in by afernandes, 13 years ago (diff)

Ticket #1416 - Disponibilizado modulos Timesheet e DMS para a comunidade.

  • Property svn:executable set to *
Line 
1<?php
2/**
3 * eGroupWare importexport
4 *
5 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
6 * @package importexport
7 * @link http://www.egroupware.org
8 * @author Knut Moeller <k.moeller@metaways.de>
9 * @copyright Knut Moeller <k.moeller@metaways.de>
10 * @version $Id: $
11 */
12
13require_once(PHPGW_INCLUDE_ROOT. '/importexport/inc/class.iface_export_record.inc.php');
14require_once(PHPGW_INCLUDE_ROOT. '/importexport/inc/class.import_export_helper_functions.inc.php');
15require_once(PHPGW_INCLUDE_ROOT. '/importexport/inc/class.iface_phpgw_record.inc.php');
16require_once(PHPGW_INCLUDE_ROOT. '/timesheet/inc/class.spreadsheet.inc.php');
17
18/**
19 * class export_cmslite
20 *
21 * special export class (metaways openoffice calc table)
22 * adapter to spreadsheet class
23 *
24 */
25class export_openoffice implements iface_export_record
26{
27
28        /**
29         * array with field mapping in form phpgw_field_name => exported_field_name
30         * @var array
31         */
32        protected  $mapping = array();
33
34        /**
35         * array with conversions to be done in form: phpgw_field_name => conversion_string
36         * @var array
37         */
38        protected  $conversion = array();
39
40        /**
41         * array holding the current record
42         * @access protected
43         */
44        protected $record = array();
45
46        /**
47         * holds (charset) translation object
48         * @var object
49         */
50        protected $translation;
51
52        /**
53         * holds number of exported records
54         * @var unknown_type
55         */
56        protected $num_of_records = 0;
57
58        /**
59         * stream resource of csv file
60         * @var resource
61         */
62        protected  $handle;
63
64        protected  $document;
65        protected  $table;
66
67        protected  $summarytable;       // link to first table
68        protected  $summary;            // array, project -> user -> time
69        protected  $summaryweekend; // same for weekend
70
71        private $tablecount;
72        private $columncount;
73        private $rowcount;
74
75
76
77        /**
78         * constructor
79         *
80         * @param object _handle resource where records are exported to.
81         * @param string _charset charset the records are exported to.
82         * @param array _options options for specific backends
83         * @return bool
84         * @access public
85         */
86        public function __construct( $_handle,  $_charset, array $_options=array() ) {
87                $this->handle = $_handle;
88        }
89
90        /**
91         * sets field mapping
92         *
93         * @param array $_mapping phpgw_field_name => csv_field_name
94         */
95        public function set_mapping( array $_mapping) {
96
97        }
98
99        /**
100         * Sets conversion.
101         * See import_export_helper_functions::conversion.
102         *
103         * @param array $_conversion
104         */
105        public function set_conversion( array $_conversion) {
106
107        }
108
109        /**
110         * exports a record into resource of handle
111         *
112         * @param iface_phpgw_record record
113         * @return bool
114         * @access public
115         */
116        public function export_record( iface_phpgw_record $_record ) {
117
118        }
119
120        public function create_summarytable($_tablename='Gesamtzeiten') {
121                $this->summarytable = new SpreadSheetTable($this->document, $_tablename);
122                $this->tablecount++;
123                $this->summary = array();
124                $this->summaryweekend = array();
125        }
126
127
128        /**
129         * @return weekdate array (1-7) for given week
130         */
131        private function week_dates($_timestamp) {
132                $week_dates = array();
133                $day = (int) date("w", $_timestamp);
134
135                if ($day != 1) {
136                        $diff = $day - 1;
137                        if ($day==0) $diff = 6;
138                        $monday = strtotime("-".$diff." days" , $_timestamp);
139                }
140                else {
141                        $monday = $_timestamp;
142                }
143
144                for ($i=0; $i < 7; $i++) {
145                        $week_dates[$i+1] = strtotime("+".$i." days", $monday);
146                }
147           return($week_dates);
148        }
149
150
151        /**
152         *
153         *
154         */
155        public function fill_summarytable($_tstamp_min, $_tstamp_max) {
156
157                // prepare data --------------------------
158
159                uksort($this->summary, "strnatcasecmp");
160                uksort($this->summaryweekend, "strnatcasecmp");
161
162                        // get user-array
163                $users = array();
164                foreach ($this->summary as $project => $user_time) {
165                        foreach($user_time as $user => $time) {
166                                if (!in_array($user, $users)) $users[] = $user;
167                        }
168                }
169                asort($users);
170                $usersweekend = array();
171                foreach ($this->summaryweekend as $project => $user_time) {
172                        foreach($user_time as $user => $time) {
173                                if (!in_array($user, $usersweekend)) $usersweekend[] = $user;
174                        }
175                }
176                asort($usersweekend);
177
178
179                        // get project-array (sites)
180                $projects = array();
181                foreach ($this->summary as $project => $user_time) {
182                        if (!in_array($project ,$projects)) $projects[] = $project;
183                }
184                foreach ($this->summaryweekend as $project => $user_time) {
185                        if (!in_array($project ,$projects)) $projects[] = $project;
186                }
187                asort($projects);
188
189                $this->summarytable->addColumn('co20');
190                for ($i=1; $i <=  count($users) + count($usersweekend) + 2; $i++) {
191                        $this->summarytable->addColumn();
192                }
193
194
195                // populate table --------------------------
196
197
198                        // create table-headlines
199                $row = $this->summarytable->addRow();
200                $this->summarytable->addCell($row, 'string', 'CMS Lite Support / Montag - Freitag / 08.00 Uhr - 18.00 Uhr', array('bold'));
201
202                for ($i=0; $i<count($users); $i++) {
203                        $this->summarytable->addCell($row, 'string', '');
204                }
205                $this->summarytable->addCell($row, 'string', 'CMS Lite Support / Wochenende', array('bold'));
206
207
208                        // headline, row 1
209                $row = $this->summarytable->addRow();
210                $this->summarytable->addCell($row, 'string', 'Mitarbeiter:', array('bold'));
211
212                foreach ($users as $user) {
213                        $this->summarytable->addCell($row, 'string', $user, array('bold'));
214                }
215                $this->summarytable->addCell($row, 'string', 'Mitarbeiter:', array('bold'));
216                foreach ($usersweekend as $user) {
217                        $this->summarytable->addCell($row, 'string', $user, array('bold'));
218                }
219
220                        // fixed date rows, row 2
221                $row = $this->summarytable->addRow();
222                $this->summarytable->addCell($row, 'string', 'Zeitraum:', array('bold'));
223                $kw_min = strftime("%V", $_tstamp_min);
224                $kw_max = strftime("%V", $_tstamp_max);
225                $kw = ($kw_min == $kw_max) ? "KW $kw_min" : "KW $kw_min - KW $kw_max";
226                for ($i=0; $i < count($users); $i++) {
227                        $this->summarytable->addCell($row, 'string', $kw, array('bold'));
228                }
229                $this->summarytable->addCell($row, 'string', 'Zeitraum:', array('bold'));
230                for ($i=0; $i < count ($usersweekend); $i++) {
231                        $this->summarytable->addCell($row, 'string', $kw, array('bold'));
232                }
233
234
235                        // weekdays,  row 3 left
236                $row = $this->summarytable->addRow();
237                $this->summarytable->addCell($row, 'string', '', array('bold'));
238
239                $week_dates = $this->week_dates($_tstamp_min);
240
241                if ($kw_min != $kw_max) {
242                        $days = strftime("%d.%m.%y", $_tstamp_min).' - '.strftime("%d.%m.%y", $_tstamp_max);
243                }
244                else {
245                        // monday-friday
246                        $days = strftime("%d.%m.%y", $week_dates[1]).' - '.strftime("%d.%m.%y", $week_dates[5]);
247                }
248                for ($i=0; $i < count($users); $i++) {
249                        $this->summarytable->addCell($row, 'string', $days, array('bold'));
250                }
251
252                        // weekend,  row 3 right
253                $this->summarytable->addCell($row, 'string', '', array('bold'));
254                if ($kw_min != $kw_max) {
255                        $days = strftime("%d.%m.%y", $_tstamp_min).' - '.strftime("%d.%m.%y", $_tstamp_max);
256                }
257                else {
258                        $days = strftime("%d.%m.%y", $week_dates[6]).' - '.strftime("%d.%m.%y", $week_dates[7]);
259                }
260                for ($i=0; $i < count($usersweekend); $i++) {
261                        $this->summarytable->addCell($row, 'string', $days, array('bold'));
262                }
263
264                $this->rowcount = 4;
265
266
267
268                        // project lines (sitenames)
269                foreach ($projects as $project) {
270
271                                // 1.Cell: projectname
272                        $row = $this->summarytable->addRow();
273                        $this->rowcount++;
274                        $this->summarytable->addCell($row, 'string', $project);
275
276                                // iterate all users for each line
277                        foreach ($users as $user) {
278
279                                if   (array_key_exists($project, $this->summary) && array_key_exists($user, $this->summary[$project])) {
280                                        $this->summarytable->addCell($row, 'float', (float) ($this->summary[$project][$user] / 60) );
281                                }
282                                else { // add empty cell if no user-entry
283                                        $this->summarytable->addCell($row, 'string', '');
284                                }
285                        }
286                        $this->summarytable->addCell($row, 'string', '');
287                        foreach ($usersweekend as $user) {
288
289                                        // weekend
290                                if (array_key_exists($project, $this->summaryweekend) && array_key_exists($user, $this->summaryweekend[$project])) {
291                                                $this->summarytable->addCell($row, 'float', (float) ($this->summaryweekend[$project][$user] / 60) );
292                                }
293                                else {  // add empty cell if no user-entry
294                                        $this->summarytable->addCell($row, 'string', '');
295                                }
296                        }
297                }
298
299
300
301                        // summary line 1
302                $row = $this->summarytable->addRow();
303                $this->rowcount++;
304                $row = $this->summarytable->addRow();
305                $this->rowcount++;
306                $this->summarytable->addCell($row, 'string', 'Summe:');
307                for ($i=1; $i <= count($users); $i++) {
308                        $this->table->addCell($row, 'formula', 'SUM([.'.chr(65+$i).'5:.'.chr(65 + $i).($this->rowcount - 1).'])');
309                }
310                $this->summarytable->addCell($row, 'string', 'Summe:');
311                for ($i= count($users)+2; $i <= count($usersweekend)+count($users)+1; $i++) {
312                        $this->table->addCell($row, 'formula', 'SUM([.'.chr(65+$i).'5:.'.chr(65 + $i).($this->rowcount - 1).'])');
313                }
314
315
316                        // summary line 2
317                $row = $this->summarytable->addRow();
318                $this->rowcount++;
319                $row = $this->summarytable->addRow();
320                $this->rowcount++;
321                $this->summarytable->addCell($row, 'string', 'Gesamt:');
322                $this->table->addCell($row, 'formula', 'SUM([.B'. ($this->rowcount - 2) . ':.' . chr(65 + count($users)) . ($this->rowcount - 2) . '])');
323                for ($i=1; $i <= count($users)-1; $i++) {
324                        $this->table->addCell($row, 'string', '');
325                }
326
327                $this->summarytable->addCell($row, 'string', 'Gesamt:');
328                $this->table->addCell($row, 'formula', 'SUM([.'.chr(65 + count($users) + 2) . ($this->rowcount - 2).
329                                                                                                   ':.'.chr(65 + count($users) + 2 + count($usersweekend) - 1) . ($this->rowcount - 2).'])');
330        }
331
332
333        public function create_usertable($_tablename, $_username, $_tstamp_min, $_tstamp_max) {
334                $this->table = new SpreadSheetTable($this->document, $_tablename);
335                for ($i=10; $i < 19; $i++) {
336                        $this->table->addColumn('co'.$i);
337                }
338
339                $row = $this->table->addRow();
340                $this->table->addCell($row, 'string', 'Monat:' . strftime("%m/%Y", $_tstamp_min), array('bold'));
341                $this->table->addCell($row, 'string', 'KW ' . strftime("%V", $_tstamp_min), array('bold'));
342
343                $row = $this->table->addRow();
344                $this->table->addCell($row, 'string', 'Mitarbeiter:', array('bold') );
345                $this->table->addCell($row, 'string', $_username, array('bold'));
346
347                        // create table-headlines
348                $headlines = array(
349                        'Datum',
350                        'Site',
351                        'Ansprechpartner',
352                        'Projekttyp',
353                        'Ticket#',
354                        'Std.',
355                        'Newsletter',
356//                      'SOW-Nr.',
357                        'Bemerkungen'
358                        );
359                $row = $this->table->addRow('double');
360                $this->table->addCells($row, 'string', $headlines, array('bold', 'border'));
361
362                $this->tablecount++;
363                $this->rowcount = 3;
364                $this->columncount = count($headlines);
365        }
366
367
368
369        /**
370         * exports a record into resource of handle
371         *
372         * @param iface_phpgw_record record
373         * @return bool
374         * @access public
375         */
376        public function add_record( $_record, $_extras ) {
377
378                if (is_array($_record)) {
379                        $row = $this->table->addRow();
380                        $this->rowcount++;
381
382                        $this->table->addCell($row, 'date', strftime("%d.%m.%Y", $_record['ts_start']));
383                        $this->table->addCell($row, 'string', $_record['ts_project']);
384                        $this->table->addCell($row, 'string', $_extras['asp']);
385                        $this->table->addCell($row, 'string', $_record['cat_name']);// $_extras['typ']);
386                        $this->table->addCell($row, 'string', $_extras['ticket']);
387                        $this->table->addCell($row, 'float', (float) ($_record['ts_duration'] / 60) );
388                        $this->table->addCell($row, 'string', $_extras['newsletter']);
389//                      $this->table->addCell($row, 'string', $_extras['sow']);
390                        $this->table->addCell($row, 'string', $_record['ts_description']);
391
392
393                        // collect statistics
394
395                                // username
396                        $res = array();
397//                      $nameRegex = '/\s(.*)\s(.*)$/';  // z.B. "[admin] Richard Blume"
398                        $nameRegex = '/(.*),\s(.*)$/';  // z.B. "Blume, Richard"
399                        preg_match($nameRegex, $GLOBALS['phpgw']->common->grab_owner_name($_record['ts_owner']), $res);
400//                      $user = $res[1];
401                        $user = $res[0];  // full name
402
403                        $site = $_record['ts_project'];
404                        $time = $_record['ts_duration'];
405                        $weekday = (int) strftime("%w", $_record['ts_start']);
406
407                        if ( $weekday == 0 || $weekday == 6 ) {
408
409                                // weekend
410                                if (!array_key_exists($site, $this->summaryweekend)) {
411                                        $this->summaryweekend[$site] = array();
412                                        $this->summaryweekend[$site][$user] = $time;
413                                }
414                                elseif (!array_key_exists($user, $this->summaryweekend[$site])) {
415                                        $this->summaryweekend[$site][$user] = $time;
416                                }
417                                else {
418                                        $this->summaryweekend[$site][$user] += $time;
419                                }
420                        }
421                        else {
422                                // site -> user -> sum
423                                if (!array_key_exists($site, $this->summary)) {
424                                        $this->summary[$site] = array();
425                                        $this->summary[$site][$user] = $time;
426                                }
427                                elseif (!array_key_exists($user, $this->summary[$site])) {
428                                        $this->summary[$site][$user] = $time;
429                                }
430                                else {
431                                        $this->summary[$site][$user] += $time;
432                                }
433                        }
434
435                        $this->num_of_records++;
436                        $this->record = array();
437
438                }
439        }
440
441
442        /**
443         *
444         *
445         */
446        public function summarize() {
447                if ($this->rowcount > 1) {
448                        $row = $this->table->addRow();
449                        $this->rowcount++;
450                        $row = $this->table->addRow();
451                        $this->rowcount++;
452
453                        $this->table->addCell($row, 'string', '');
454                        $this->table->addCell($row, 'string', '');
455                        $this->table->addCell($row, 'string', '');
456                        $this->table->addCell($row, 'string', '');
457                        $this->table->addCell($row, 'string', '');
458                        $this->table->addCell($row, 'formula', 'SUM([.F2:.F'.($this->rowcount-2).'])');
459                }
460        }
461
462
463
464        public function init() {
465                $this->document = new SpreadSheetDocument($this->handle);
466
467                        // global usertable styles
468                $columnwidth = array(
469                        '2.767cm',
470                        '5.067cm',
471                        '5.067cm',
472                        '3.267cm',
473                        '2.267cm',
474                        '2.267cm',
475                        '2.267cm',
476//                      '2.267cm',
477                        '6.267cm'
478                        );
479
480                for ($i=0; $i < count($columnwidth); $i++) {
481                        $this->document->addColumnStyle('co'.($i + 10), $columnwidth[$i]);
482                }
483
484                        // first column, summary-table
485                $this->document->addColumnStyle('co20', '5.2cm');
486        }
487
488        public function finalize() {
489                $this->document->finalize();
490        }
491
492        /**
493         * Returns total number of exported records.
494         *
495         * @return int
496         * @access public
497         */
498        public function get_num_of_records() {
499                return $this->num_of_records;
500        }
501
502        /**
503         * destructor
504         *
505         * @return
506         * @access public
507         */
508        public function __destruct() {
509
510        }
511}
512
513
514
515?>
Note: See TracBrowser for help on using the repository browser.