diff --git a/timesheet/inc/class.export_openoffice.inc.php b/timesheet/inc/class.export_openoffice.inc.php deleted file mode 100644 index 84204696a3..0000000000 --- a/timesheet/inc/class.export_openoffice.inc.php +++ /dev/null @@ -1,512 +0,0 @@ - - * @copyright Knut Moeller - * @version $Id: $ - */ - -require_once(EGW_INCLUDE_ROOT. '/timesheet/inc/class.spreadsheet.inc.php'); - -/** - * class export_cmslite - * - * special export class (metaways openoffice calc table) - * adapter to spreadsheet class - * - */ -class export_openoffice implements importexport_iface_export_record -{ - - /** - * array with field mapping in form egw_field_name => exported_field_name - * @var array - */ - protected $mapping = array(); - - /** - * array with conversions to be done in form: egw_field_name => conversion_string - * @var array - */ - protected $conversion = array(); - - /** - * array holding the current record - * @access protected - */ - protected $record = array(); - - /** - * holds (charset) translation object - * @var object - */ - protected $translation; - - /** - * holds number of exported records - * @var unknown_type - */ - protected $num_of_records = 0; - - /** - * stream resource of csv file - * @var resource - */ - protected $handle; - - protected $document; - protected $table; - - protected $summarytable; // link to first table - protected $summary; // array, project -> user -> time - protected $summaryweekend; // same for weekend - - private $tablecount; - private $columncount; - private $rowcount; - - - - /** - * constructor - * - * @param object _handle resource where records are exported to. - * @param string _charset charset the records are exported to. - * @param array _options options for specific backends - * @return bool - * @access public - */ - public function __construct( $_handle, array $_options ) { - $this->handle = $_handle; - } - - /** - * sets field mapping - * - * @param array $_mapping egw_field_name => csv_field_name - */ - public function set_mapping( array $_mapping) { - - } - - /** - * Sets conversion. - * See import_export_helper_functions::conversion. - * - * @param array $_conversion - */ - public function set_conversion( array $_conversion) { - - } - - /** - * exports a record into resource of handle - * - * @param iface_egw_record record - * @return bool - * @access public - */ - public function export_record( importexport_iface_egw_record $_record ) { - - } - - public function create_summarytable($_tablename='Gesamtzeiten') { - $this->summarytable = new SpreadSheetTable($this->document, $_tablename); - $this->tablecount++; - $this->summary = array(); - $this->summaryweekend = array(); - } - - - /** - * @return weekdate array (1-7) for given week - */ - private function week_dates($_timestamp) { - $week_dates = array(); - $day = (int) date("w", $_timestamp); - - if ($day != 1) { - $diff = $day - 1; - if ($day==0) $diff = 6; - $monday = strtotime("-".$diff." days" , $_timestamp); - } - else { - $monday = $_timestamp; - } - - for ($i=0; $i < 7; $i++) { - $week_dates[$i+1] = strtotime("+".$i." days", $monday); - } - return($week_dates); - } - - - /** - * - * - */ - public function fill_summarytable($_tstamp_min, $_tstamp_max) { - - // prepare data -------------------------- - - uksort($this->summary, "strnatcasecmp"); - uksort($this->summaryweekend, "strnatcasecmp"); - - // get user-array - $users = array(); - foreach ($this->summary as $project => $user_time) { - foreach($user_time as $user => $time) { - if (!in_array($user, $users)) $users[] = $user; - } - } - asort($users); - $usersweekend = array(); - foreach ($this->summaryweekend as $project => $user_time) { - foreach($user_time as $user => $time) { - if (!in_array($user, $usersweekend)) $usersweekend[] = $user; - } - } - asort($usersweekend); - - - // get project-array (sites) - $projects = array(); - foreach ($this->summary as $project => $user_time) { - if (!in_array($project ,$projects)) $projects[] = $project; - } - foreach ($this->summaryweekend as $project => $user_time) { - if (!in_array($project ,$projects)) $projects[] = $project; - } - asort($projects); - - $this->summarytable->addColumn('co20'); - for ($i=1; $i <= count($users) + count($usersweekend) + 2; $i++) { - $this->summarytable->addColumn(); - } - - - // populate table -------------------------- - - - // create table-headlines - $row = $this->summarytable->addRow(); - $this->summarytable->addCell($row, 'string', 'CMS Lite Support / Montag - Freitag / 08.00 Uhr - 18.00 Uhr', array('bold')); - - for ($i=0; $isummarytable->addCell($row, 'string', ''); - } - $this->summarytable->addCell($row, 'string', 'CMS Lite Support / Wochenende', array('bold')); - - - // headline, row 1 - $row = $this->summarytable->addRow(); - $this->summarytable->addCell($row, 'string', 'Mitarbeiter:', array('bold')); - - foreach ($users as $user) { - $this->summarytable->addCell($row, 'string', $user, array('bold')); - } - $this->summarytable->addCell($row, 'string', 'Mitarbeiter:', array('bold')); - foreach ($usersweekend as $user) { - $this->summarytable->addCell($row, 'string', $user, array('bold')); - } - - // fixed date rows, row 2 - $row = $this->summarytable->addRow(); - $this->summarytable->addCell($row, 'string', 'Zeitraum:', array('bold')); - $kw_min = strftime("%V", $_tstamp_min); - $kw_max = strftime("%V", $_tstamp_max); - $kw = ($kw_min == $kw_max) ? "KW $kw_min" : "KW $kw_min - KW $kw_max"; - for ($i=0; $i < count($users); $i++) { - $this->summarytable->addCell($row, 'string', $kw, array('bold')); - } - $this->summarytable->addCell($row, 'string', 'Zeitraum:', array('bold')); - for ($i=0; $i < count ($usersweekend); $i++) { - $this->summarytable->addCell($row, 'string', $kw, array('bold')); - } - - - // weekdays, row 3 left - $row = $this->summarytable->addRow(); - $this->summarytable->addCell($row, 'string', '', array('bold')); - - $week_dates = $this->week_dates($_tstamp_min); - - if ($kw_min != $kw_max) { - $days = strftime("%d.%m.%y", $_tstamp_min).' - '.strftime("%d.%m.%y", $_tstamp_max); - } - else { - // monday-friday - $days = strftime("%d.%m.%y", $week_dates[1]).' - '.strftime("%d.%m.%y", $week_dates[5]); - } - for ($i=0; $i < count($users); $i++) { - $this->summarytable->addCell($row, 'string', $days, array('bold')); - } - - // weekend, row 3 right - $this->summarytable->addCell($row, 'string', '', array('bold')); - if ($kw_min != $kw_max) { - $days = strftime("%d.%m.%y", $_tstamp_min).' - '.strftime("%d.%m.%y", $_tstamp_max); - } - else { - $days = strftime("%d.%m.%y", $week_dates[6]).' - '.strftime("%d.%m.%y", $week_dates[7]); - } - for ($i=0; $i < count($usersweekend); $i++) { - $this->summarytable->addCell($row, 'string', $days, array('bold')); - } - - $this->rowcount = 4; - - - - // project lines (sitenames) - foreach ($projects as $project) { - - // 1.Cell: projectname - $row = $this->summarytable->addRow(); - $this->rowcount++; - $this->summarytable->addCell($row, 'string', $project); - - // iterate all users for each line - foreach ($users as $user) { - - if (array_key_exists($project, $this->summary) && array_key_exists($user, $this->summary[$project])) { - $this->summarytable->addCell($row, 'float', (float) ($this->summary[$project][$user] / 60) ); - } - else { // add empty cell if no user-entry - $this->summarytable->addCell($row, 'string', ''); - } - } - $this->summarytable->addCell($row, 'string', ''); - foreach ($usersweekend as $user) { - - // weekend - if (array_key_exists($project, $this->summaryweekend) && array_key_exists($user, $this->summaryweekend[$project])) { - $this->summarytable->addCell($row, 'float', (float) ($this->summaryweekend[$project][$user] / 60) ); - } - else { // add empty cell if no user-entry - $this->summarytable->addCell($row, 'string', ''); - } - } - } - - - - // summary line 1 - $row = $this->summarytable->addRow(); - $this->rowcount++; - $row = $this->summarytable->addRow(); - $this->rowcount++; - $this->summarytable->addCell($row, 'string', 'Summe:'); - for ($i=1; $i <= count($users); $i++) { - $this->table->addCell($row, 'formula', 'SUM([.'.chr(65+$i).'5:.'.chr(65 + $i).($this->rowcount - 1).'])'); - } - $this->summarytable->addCell($row, 'string', 'Summe:'); - for ($i= count($users)+2; $i <= count($usersweekend)+count($users)+1; $i++) { - $this->table->addCell($row, 'formula', 'SUM([.'.chr(65+$i).'5:.'.chr(65 + $i).($this->rowcount - 1).'])'); - } - - - // summary line 2 - $row = $this->summarytable->addRow(); - $this->rowcount++; - $row = $this->summarytable->addRow(); - $this->rowcount++; - $this->summarytable->addCell($row, 'string', 'Gesamt:'); - $this->table->addCell($row, 'formula', 'SUM([.B'. ($this->rowcount - 2) . ':.' . chr(65 + count($users)) . ($this->rowcount - 2) . '])'); - for ($i=1; $i <= count($users)-1; $i++) { - $this->table->addCell($row, 'string', ''); - } - - $this->summarytable->addCell($row, 'string', 'Gesamt:'); - $this->table->addCell($row, 'formula', 'SUM([.'.chr(65 + count($users) + 2) . ($this->rowcount - 2). - ':.'.chr(65 + count($users) + 2 + count($usersweekend) - 1) . ($this->rowcount - 2).'])'); - } - - - public function create_usertable($_tablename, $_username, $_tstamp_min, $_tstamp_max) { - $this->table = new SpreadSheetTable($this->document, $_tablename); - for ($i=10; $i < 19; $i++) { - $this->table->addColumn('co'.$i); - } - - $row = $this->table->addRow(); - $this->table->addCell($row, 'string', 'Monat:' . strftime("%m/%Y", $_tstamp_min), array('bold')); - $this->table->addCell($row, 'string', 'KW ' . strftime("%V", $_tstamp_min), array('bold')); - - $row = $this->table->addRow(); - $this->table->addCell($row, 'string', 'Mitarbeiter:', array('bold') ); - $this->table->addCell($row, 'string', $_username, array('bold')); - - // create table-headlines - $headlines = array( - 'Datum', - 'Site', - 'Ansprechpartner', - 'Projekttyp', - 'Ticket#', - 'Std.', - 'Newsletter', -// 'SOW-Nr.', - 'Bemerkungen' - ); - $row = $this->table->addRow('double'); - $this->table->addCells($row, 'string', $headlines, array('bold', 'border')); - - $this->tablecount++; - $this->rowcount = 3; - $this->columncount = count($headlines); - } - - - - /** - * exports a record into resource of handle - * - * @param iface_egw_record record - * @return bool - * @access public - */ - public function add_record( $_record, $_extras ) { - - if (is_array($_record)) { - $row = $this->table->addRow(); - $this->rowcount++; - - $this->table->addCell($row, 'date', strftime("%d.%m.%Y", $_record['ts_start'])); - $this->table->addCell($row, 'string', $_record['ts_project']); - $this->table->addCell($row, 'string', $_extras['asp']); - $this->table->addCell($row, 'string', $_record['cat_name']);// $_extras['typ']); - $this->table->addCell($row, 'string', $_extras['ticket']); - $this->table->addCell($row, 'float', (float) ($_record['ts_duration'] / 60) ); - $this->table->addCell($row, 'string', $_extras['newsletter']); -// $this->table->addCell($row, 'string', $_extras['sow']); - $this->table->addCell($row, 'string', $_record['ts_description']); - - - // collect statistics - - // username - $res = array(); -// $nameRegex = '/\s(.*)\s(.*)$/'; // z.B. "[admin] Richard Blume" - $nameRegex = '/(.*),\s(.*)$/'; // z.B. "Blume, Richard" - preg_match($nameRegex, $GLOBALS['egw']->common->grab_owner_name($_record['ts_owner']), $res); -// $user = $res[1]; - $user = $res[0]; // full name - - $site = $_record['ts_project']; - $time = $_record['ts_duration']; - $weekday = (int) strftime("%w", $_record['ts_start']); - - if ( $weekday == 0 || $weekday == 6 ) { - - // weekend - if (!array_key_exists($site, $this->summaryweekend)) { - $this->summaryweekend[$site] = array(); - $this->summaryweekend[$site][$user] = $time; - } - elseif (!array_key_exists($user, $this->summaryweekend[$site])) { - $this->summaryweekend[$site][$user] = $time; - } - else { - $this->summaryweekend[$site][$user] += $time; - } - } - else { - // site -> user -> sum - if (!array_key_exists($site, $this->summary)) { - $this->summary[$site] = array(); - $this->summary[$site][$user] = $time; - } - elseif (!array_key_exists($user, $this->summary[$site])) { - $this->summary[$site][$user] = $time; - } - else { - $this->summary[$site][$user] += $time; - } - } - - $this->num_of_records++; - $this->record = array(); - - } - } - - - /** - * - * - */ - public function summarize() { - if ($this->rowcount > 1) { - $row = $this->table->addRow(); - $this->rowcount++; - $row = $this->table->addRow(); - $this->rowcount++; - - $this->table->addCell($row, 'string', ''); - $this->table->addCell($row, 'string', ''); - $this->table->addCell($row, 'string', ''); - $this->table->addCell($row, 'string', ''); - $this->table->addCell($row, 'string', ''); - $this->table->addCell($row, 'formula', 'SUM([.F2:.F'.($this->rowcount-2).'])'); - } - } - - - - public function init() { - $this->document = new SpreadSheetDocument($this->handle); - - // global usertable styles - $columnwidth = array( - '2.767cm', - '5.067cm', - '5.067cm', - '3.267cm', - '2.267cm', - '2.267cm', - '2.267cm', -// '2.267cm', - '6.267cm' - ); - - for ($i=0; $i < count($columnwidth); $i++) { - $this->document->addColumnStyle('co'.($i + 10), $columnwidth[$i]); - } - - // first column, summary-table - $this->document->addColumnStyle('co20', '5.2cm'); - } - - public function finalize() { - $this->document->finalize(); - } - - /** - * Returns total number of exported records. - * - * @return int - * @access public - */ - public function get_num_of_records() { - return $this->num_of_records; - } - - /** - * destructor - * - * @return - * @access public - */ - public function __destruct() { - - } -} - - - -?> diff --git a/timesheet/inc/class.spreadsheet.inc.php b/timesheet/inc/class.spreadsheet.inc.php deleted file mode 100644 index aede801166..0000000000 --- a/timesheet/inc/class.spreadsheet.inc.php +++ /dev/null @@ -1,653 +0,0 @@ - - * @copyright Knut Moeller - * @version $Id:$ - */ - - -/** - * - * Spreadsheet main class - * - * - */ -class SpreadSheetDocument { - - // TODO generalize... - // TODO internalize counts - - const TEMPDIR = '/tmp'; // system temppath TODO: better solution? - const ZIP = 'zip'; // system zip command TODO: zip via php possible? no shellexec ? - - private $debug = 0; - - private $temppath; // temppath (to assemble zip file) - private $tempzip; // temp zip file - - private $handle; // filehandle OO export file for importexport - - // xml nodes - private $doc; - private $styles; - private $body; - private $spreadsheet; // contains tables .... - - - - // File handling, INIT - - function __construct($_handle) { - $this->temppath = $this->mk_tempdir(self::TEMPDIR, 'cmsliteexport'); - $this->tempzip = tempnam('/tmp','cmslite_export_outfile'); - $this->handle = $_handle; - $this->initSpreadsheet(); - } - - - function __destruct() { - @unlink($this->tempzip); - @unlink($this->tempzip . '.zip'); - $this->remove_directory($this->temppath.'/'); - } - - - - /** - * assemble the zipped OpenOffice Document - * - */ - public function finalize() { - - @mkdir($this->temppath."/META-INF"); - @mkdir($this->temppath."/Thumbnails"); - @mkdir($this->temppath."/Configurations2"); - @mkdir($this->temppath."/Configurations2/statusbar"); - @mkdir($this->temppath."/Configurations2/accelerator"); - @touch($this->temppath."/Configurations2/accelerator/current.xml"); - @mkdir($this->temppath."/Configurations2/floater"); - @mkdir($this->temppath."/Configurations2/popupmenu"); - @mkdir($this->temppath."/Configurations2/progressbar"); - @mkdir($this->temppath."/Configurations2/menubar"); - @mkdir($this->temppath."/Configurations2/toolbar"); - @mkdir($this->temppath."/Configurations2/images"); - @mkdir($this->temppath."/Configurations2/images/Bitmaps"); - - // main content - $this->write( $this->doc->saveXML(), "content.xml"); - if ($this->debug > 0) error_log(print_r($this->doc->saveXML(),true)); - - $this->createMimetype(); - $this->createManifest(); - $this->createMeta(); - - // create zip - shell_exec("cd $this->temppath; ".self::ZIP." -r $this->tempzip.zip * "); - - // copy to filehandle - if ($this->handle) { - $fh = fopen($this->tempzip.'.zip', "rb"); - if($fh) { - while (!feof($fh)) { - fwrite($this->handle, fread($fh, 8192)); - } - } - fclose($fh); - } - else error_log("output file error"); - } - - - - protected function createMeta() { - - //TODO generate - $content = ' - -OpenOffice.org/2.0$Linux OpenOffice.org_project/680m5$Build-9011 -2005-01-18T12:05:292006-10-25T10:35:552006-01-17T10:57:23 -de-DE218P1DT9H3M54S - -'; - $this->write($content,"meta.xml"); - } - - protected function createMimetype() { - $content = 'application/vnd.oasis.opendocument.spreadsheet'; - $this->write($content,"mimetype"); - - } - - protected function createManifest() { - //TODO generate -$content = ' - - - - - - - - - - - - - - - - -'; - - $this->write($content,"META-INF/manifest.xml"); - } - - - protected function initSpreadsheet() { - - $this->doc = new DOMDocument ('1.0', 'UTF-8'); - $this->doc->formatOutput = true; - - $root = $this->doc->createElementNS('urn:oasis:names:tc:opendocument:xmlns:office:1.0', - 'office:document-content'); - $this->doc->appendChild($root); - - // define Namespaces.... - - $namespaces = array( - 'style' => 'urn:oasis:names:tc:opendocument:xmlns:style:1.0', - 'text' => 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', - 'table' => 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', - 'draw' => 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0', - 'fo' => 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0', - 'xlink' => 'http://www.w3.org/1999/xlink', - 'dc' => 'http://purl.org/dc/elements/1.1/', - 'meta' => 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0', - 'number' => 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0', - 'svg' => 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0', - 'chart' => 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0', - 'dr3d' => 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0', - 'math' => 'http://www.w3.org/1998/Math/MathML', - 'form' => 'urn:oasis:names:tc:opendocument:xmlns:form:1.0', - 'script' => 'urn:oasis:names:tc:opendocument:xmlns:script:1.0', - 'ooo' => 'http://openoffice.org/2004/office', - 'ooow' => 'http://openoffice.org/2004/writer', - 'oooc' => 'http://openoffice.org/2004/calc', - 'dom' => 'http://www.w3.org/2001/xml-events', - 'xforms' => 'http://www.w3.org/2002/xforms', - 'xsd' => 'http://www.w3.org/2001/XMLSchema', - 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance'); - - foreach ($namespaces as $name => $url) { - $root->setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:$name", $url); - } - $root->setAttribute('office:version', '1.0'); - - - // main parts - - $node = $root->appendChild( $this->doc->createElement('office:scripts') ); - $node = $root->appendChild( $this->doc->createElement('office:font-face-decls') ); - $this->styles = $root->appendChild( $this->doc->createElement('office:automatic-styles') ); - $this->body = $root->appendChild( $this->doc->createElement('office:body') ); - $this->spreadsheet = $this->body->appendChild( $this->doc->createElement('office:spreadsheet') ); - - // styles - - $this->initTableStyles(); - $this->initColumnStyles(); - $this->initRowStyles(); - $this->initCellStyles(); - $this->initNumberStyles(); - } - - - /** - * define the table styles - */ - protected function initTableStyles() { - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ta1'); - $style->setAttribute('style:family', 'table'); - $style->setAttribute('style:master-page-name', 'Default'); - $property = $style->appendChild($this->doc->createElement('style:table-properties')); - $property->setAttribute('table:display', 'true'); - $property->setAttribute('style:writing-mode', 'lr-tb'); - } - - /** - * define the column styles - */ - protected function initColumnStyles() { - $this->addColumnStyle('co1'); - } - - public function addColumnStyle($_name, $_width='2.267cm') { - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', $_name); - $style->setAttribute('style:family', 'table-column'); - $property = $style->appendChild($this->doc->createElement('style:table-column-properties')); - $property->setAttribute('fo:break-before', 'auto'); - $property->setAttribute('style:column-width', $_width); - } - - - /** - * define the row styles - */ - protected function initRowStyles() { - - // standard - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ro1'); - $style->setAttribute('style:family', 'table-row'); - $property = $style->appendChild($this->doc->createElement('style:table-row-properties')); - $property->setAttribute('style:row-height', '0.48cm'); - $property->setAttribute('fo:break-before', 'auto'); - $property->setAttribute('style:use-optimal-row-height', 'true'); - - // double height - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ro2'); - $style->setAttribute('style:family', 'table-row'); - $property = $style->appendChild($this->doc->createElement('style:table-row-properties')); - $property->setAttribute('style:row-height', '1.053cm'); - $property->setAttribute('fo:break-before', 'auto'); - $property->setAttribute('style:use-optimal-row-height', 'false'); - - } - - - /** - * define the cell styles - */ - protected function initCellStyles() { - - // default - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce1'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', 'none'); - $this->appendTextAttributes($style); - - // default bold - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce2'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', 'none'); - $this->appendTextAttributes($style, true); - - // default bold + border - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce3'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', '0.002cm solid #000000'); - $this->appendTextAttributes($style, true); - - - - // N2 decimal - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce101'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - $style->setAttribute('style:data-style-name', 'N2'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', 'none'); - $this->appendTextAttributes($style); - - - // N0 decimal - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce100'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - $style->setAttribute('style:data-style-name', 'N0'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', 'none'); - $this->appendTextAttributes($style); - - - // N3 decimal Sum - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce102'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - $style->setAttribute('style:data-style-name', 'N2'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', 'none'); - $this->appendTextAttributes($style, true); - - - // N4 date - $style = $this->styles->appendChild($this->doc->createElement('style:style')); - $style->setAttribute('style:name', 'ce103'); - $style->setAttribute('style:family', 'table-cell'); - $style->setAttribute('style:parent-style-name', 'Default'); - $style->setAttribute('style:data-style-name', 'N4'); - - $property = $style->appendChild($this->doc->createElement('style:table-cell-properties')); - $property->setAttribute('fo:border', 'none'); - $this->appendTextAttributes($style); - - } - - - protected function appendTextAttributes($_style, $_bold=false) { - $property = $_style->appendChild($this->doc->createElement('style:text-properties')); - $property->setAttribute('style:use-window-font-color', 'true' ); - $property->setAttribute('style:text-outline', 'false' ); - $property->setAttribute('style:text-line-through-style', 'none' ); - $property->setAttribute('style:font-name', 'Arial1' ); - $property->setAttribute('style:text-underline-style', 'none' ); - $property->setAttribute('style:font-size-asian', '10pt' ); - $property->setAttribute('style:font-style-asian', 'normal' ); - $property->setAttribute('style:font-weight-asian', 'bold' ); - $property->setAttribute('style:font-size-complex', '10pt' ); - $property->setAttribute('style:font-style-complex', 'normal' ); - $property->setAttribute('style:font-weight-complex', 'bold' ); - $property->setAttribute('fo:font-size', '10pt' ); - $property->setAttribute('fo:font-style', 'normal' ); - $property->setAttribute('fo:text-shadow', 'none' ); - $property->setAttribute('fo:font-weight', ($_bold)?'bold':'normal' ); - } - - - /** - * define the number styles - */ - protected function initNumberStyles() { - - // N2 : decimal precision 2 - $style = $this->styles->appendChild($this->doc->createElement('number:number-style')); - $style->setAttribute('style:name', 'N2'); - $property = $style->appendChild($this->doc->createElement('number:number')); - $property->setAttribute('number:decimal-places', '2'); - $property->setAttribute('number:min-integer-digits', '1'); - $property->setAttribute('number:grouping', 'false'); - - // N0 : plain integer N0 - $style = $this->styles->appendChild($this->doc->createElement('number:number-style')); - $style->setAttribute('style:name', 'N0'); - $property = $style->appendChild($this->doc->createElement('number:number')); - $property->setAttribute('number:decimal-places', '0'); - $property->setAttribute('number:min-integer-digits', '0'); - $property->setAttribute('number:grouping', 'false'); - - // N4 : date - $style = $this->styles->appendChild($this->doc->createElement('number:date-style')); - $style->setAttribute('style:name', 'N4'); - $style->setAttribute('number:automatic-order', 'true'); - - $property = $style->appendChild($this->doc->createElement('number:day')); - $property->setAttribute('number:style', 'long'); - $property = $style->appendChild($this->doc->createElement('number:text', '.')); - - $property = $style->appendChild($this->doc->createElement('number:month')); - $property->setAttribute('number:style', 'long'); - $property = $style->appendChild($this->doc->createElement('number:text', '.')); - - $property = $style->appendChild($this->doc->createElement('number:year')); - } - - - - - /* GETTER,SETTER FUNCTIONS */ - - public function getDoc() { - return $this->doc; - } - - public function get() { - return $this->spreadsheet; - } - - public function toString() { - return (is_object($this->doc)) ? $this->doc->saveXML() : 'empty' ; - } - - - - /* HELPER FUNCTIONS */ - - private function mk_tempdir($dir, $prefix='', $mode=0700) { - if (substr($dir, -1) != '/') $dir .= '/'; - - do { - $path = $dir.$prefix.mt_rand(0, 9999999); - } while (!mkdir($path, $mode)); - - return $path; - } - - private function write($content, $file) { - $fh = fopen ($this->temppath.'/'.$file, "w") or die("can't open tempfile ". $this->temppath.'/'.$file); - fwrite($fh, $content) or die ("cannot write to tempfile ". $this->temppath.'/'.$file); - fclose($fh); - } - - private function remove_directory($dir) { - $dir_contents = scandir($dir); - foreach ($dir_contents as $item) { - if (is_dir($dir.$item) && $item != '.' && $item != '..') { - $this->remove_directory($dir.$item.'/'); - } - elseif (file_exists($dir.$item) && $item != '.' && $item != '..') { - unlink($dir.$item); - } - } - rmdir($dir); - } - -} // class - - - - -/** - * - * - * - */ -class SpreadSheetTable { - - private $doc; - private $table; - private $columns; - - - function __construct($_spreadsheet, $_name) { - $this->doc = $_spreadsheet->getDoc(); - $this->spreadsheet = $_spreadsheet->get(); - - if (is_object($this->doc)) { - $this->table = $this->spreadsheet->appendChild( $this->doc->createElement('table:table') ); - $this->table->setAttribute('table:name', $_name); - $this->table->setAttribute('table:style-name', 'ta1'); - $this->table->setAttribute('table:print','false'); - } - else return false; - } - - /** - * - */ - public function get() { - return $this->table; - } - - - /** - * add column - * @return colElement - */ - public function addColumn($_name='co1', $_cellstylename='ce1', $_repeat=1) { - $col = $this->table->appendChild( $this->doc->createElement('table:table-column') ); - $col->setAttribute('table:style-name', $_name); - $col->setAttribute('table:number-columns-repeated',$_repeat); - $col->setAttribute('table:default-cell-style-name', $_cellstylename); - - // table:number-columns-repeated="2" - return $col; - } - - - /** - * add row - * @return rowElement - * @param type: normal, double (height) - */ - public function addRow($_type='normal') { - $row = $this->table->appendChild( $this->doc->createElement('table:table-row') ); - $row->setAttribute('table:style-name', ($_type=='normal') ? 'ro1' : 'ro2'); - return $row; - } - - - /** - * requires row element, adds one cell to it - * @return cellElement - */ - public function addCell( $row, $type, $value, $attributes=array() ) { - $cell = $row->appendChild( $this->doc->createElement('table:table-cell') ); - cellType($this->doc, $cell, $type, $value, $attributes); - return $cell; - } - - - /** - * requires row element, adds multiple cells - */ - public function addCells( $row, $type, $values, $attributes=array() ) { - if (!is_array($values)) { - echo "array required"; - return false; - } - foreach ($values as $value) { - $this->addCell($row, $type, $value, $attributes); - } - } - -} // class - - - - -/** - * - * type factory - * - */ -function cellType($_doc, $_cell, $_type, $_value, $_attributes=array()) { - - //TODO think about attribute dispatching <-> xml cell definition - - switch ($_type) { - case 'float' : return new SpreadSheetCellTypeFloat ($_doc, $_cell, $_value, $_attributes); - case 'int' : return new SpreadSheetCellTypeInt ($_doc, $_cell, $_value, $_attributes); - case 'formula' : return new SpreadSheetCellTypeFormula($_doc, $_cell, $_value, $_attributes); - case 'date' : return new SpreadSheetCellTypeDate ($_doc, $_cell, $_value, $_attributes); - default : return new SpreadSheetCellTypeString ($_doc, $_cell, $_value, $_attributes); - } -} - - -/** - * string type cell - */ -class SpreadSheetCellTypeString { - - function __construct($doc, $cell, $value, $_attributes) { - $cell->setAttribute('office:value-type', 'string'); - - $name = 'ce1'; - if (array_search('bold', $_attributes) !== FALSE ) { - $name = 'ce2'; - if (array_search('border', $_attributes) !== FALSE ) { - $name = 'ce3'; - } - } - $cell->setAttribute('table:style-name', $name); - - $cell->appendChild( $doc->createElement('text:p', $value) ); - } -} - - -/** - * float type cell - */ -class SpreadSheetCellTypeFloat { - function __construct($doc, $cell, $value, $_attributes) { - $cell->setAttribute('office:value-type', 'float'); - $cell->setAttribute('office:value', number_format($value, 2, '.', '') ); - $cell->setAttribute('table:style-name', 'ce101'); - $cell->appendChild( $doc->createElement('text:p', number_format($value, 2, ',', '')) ); - } -} - - -/** - * int type cell - */ -class SpreadSheetCellTypeInt { - function __construct($doc, $cell, $value, $_attributes) { - $cell->setAttribute('office:value-type', 'float'); - $cell->setAttribute('office:value', number_format($value, 0, ',', '')); - $cell->setAttribute('table:style-name', 'ce100'); - $cell->appendChild( $doc->createElement('text:p', number_format($value, 0, ',', '')) ); - } -} - - -/** - * date type cell - */ -class SpreadSheetCellTypeDate { - function __construct($doc, $cell, $value, $_attributes) { - if (($timestamp = strtotime($value)) === false) { - echo "Wrong date ($value)"; - } - else { - $cell->setAttribute('office:value-type', 'date'); - $cell->setAttribute('office:date-value', strftime("%Y-%m-%d", $timestamp) ); - $cell->setAttribute('table:style-name', 'ce103'); - $cell->appendChild( $doc->createElement('text:p', strftime("%d.%m.%Y", $timestamp) ) ); - } - } -} - - -/** - * formula type cell - */ -class SpreadSheetCellTypeFormula { - - function __construct($doc, $cell, $value, $_attributes) { - $cell->setAttribute('table:formula', 'oooc:='.$value); - $cell->setAttribute('office:value-type', 'float'); - $cell->setAttribute('office:value', '0'); - $cell->setAttribute('table:style-name', 'ce102'); - $cell->appendChild( $doc->createElement('text:p', '0,00' )); - } -} - - -?>