function _install() { if ($this->canInstall()) { global $user; $sheetlib = TikiLib::lib('sheet'); require_once 'lib/sheet/grid.php'; //here we convert the array to that of what is acceptable to the sheet lib $parentSheetId = 0; $sheets = array(); $nbsheets = count($this->data); for ($sheetI = 0; $sheetI < $nbsheets; $sheetI++) { $sheets[$sheetI] = new stdClass(); $sheets[$sheetI]->rows = array(); $sheets[$sheetI]->metadata = new stdClass(); $sheets[$sheetI]->metadata->widths = array(); $title = $this->data[$sheetI]['title']; $title = $title ? $title : "Untitled - From Profile Import"; $nbdatasheetI = count($this->data[$sheetI]); if (in_array('title', array_keys($this->data[$sheetI]))) { $nbdatasheetI--; // do not take the title into account on the number of rows } for ($r = 0; $r < $nbdatasheetI; $r++) { $nbdatasheetIr = count($this->data[$sheetI][$r]); $sheets[$sheetI]->rows[$r]->columns = array(); for ($c = 0; $c < $nbdatasheetIr; $c++) { $sheets[$sheetI]->rows[$r]->columns[$c] = new stdClass(); $value = ""; $formula = ""; $rawValue = $this->data[$sheetI][$r][$c]; if (substr($rawValue, 0, 1) == "=") { $formula = $rawValue; } else { $value = $rawValue; } $sheets[$sheetI]->rows[$r]->columns[$c]->formula = $formula; $sheets[$sheetI]->rows[$r]->columns[$c]->value = $value; $sheets[$sheetI]->rows[$r]->columns[$c]->width = 1; $sheets[$sheetI]->rows[$r]->columns[$c]->height = 1; } } $sheets[$sheetI]->metadata->widths[] = $nbdatasheetIr; $sheets[$sheetI]->metadata->rows = $nbdatasheetI; $sheets[$sheetI]->metadata->columns = count($this->data[$sheetI][0]); $id = $sheetlib->replace_sheet(0, $title, "", $user, $parentSheetId); $parentSheetId = $parentSheetId ? $parentSheetId : $id; $grid = new TikiSheet($id); $handler = new TikiSheetHTMLTableHandler($sheets[$sheetI]); $res = $grid->import($handler); $handler = new TikiSheetDatabaseHandler($id); $grid->export($handler); } return $parentSheetId; } }
function getDocument($objectId, Search_Type_Factory_Interface $typeFactory) { $sheetlib = TikiLib::lib('sheet'); $info = $sheetlib->get_sheet_info($objectId); $values = $this->db->table('tiki_sheet_values'); $contributors = $values->fetchColumn($values->expr('DISTINCT `user`'), array('sheetId' => $objectId)); $lastModif = $values->fetchOne($values->max('begin'), array('sheetId' => $objectId)); $loader = new TikiSheetDatabaseHandler($objectId); $writer = new TikiSheetCSVHandler('php://output'); $grid = new TikiSheet(); $grid->import($loader); $grid->export($writer); $text = $writer->output; $data = array('title' => $typeFactory->sortable($info['title']), 'description' => $typeFactory->sortable($info['description']), 'modification_date' => $typeFactory->timestamp($lastModif), 'contributors' => $typeFactory->multivalue($contributors), 'sheet_content' => $typeFactory->plaintext($text), 'view_permission' => $typeFactory->identifier('tiki_p_view_sheet')); return $data; }
function _install() { if ($this->canInstall()) { global $user, $sheetlib; require_once 'lib/sheet/grid.php'; //here we convert the array to that of what is acceptable to the sheet lib $parentSheetId = null; $sheets = array(); $nbsheets = count($this->data); for ($sheetI = 0; $sheetI < $nbsheets; $sheetI++) { $title = $this->data[$sheetI]['title']; $title = $title ? $title : "Untitled - From Profile Import"; $nbdatasheetI = count($this->data[$sheetI]); for ($r = 0; $r < $nbdatasheetI; $r++) { $nbdatasheetIr = count($this->data[$sheetI][$r]); for ($c = 0; $c < $nbdatasheetIr; $c++) { $value = ""; $formula = ""; $rawValue = $this->data[$sheetI][$r][$c]; if (substr($rawValue, 0, 1) == "=") { $formula = $rawValue; } else { $value = $rawValue; } $ri = 'r' . $r; $ci = 'c' . $c; $sheets[$sheetI]->data->{$ri}->{$ci}->formula = $formula; $sheets[$sheetI]->data->{$ri}->{$ci}->value = $value; $sheets[$sheetI]->data->{$ri}->{$ci}->width = 1; $sheets[$sheetI]->data->{$ri}->{$ci}->height = 1; } } $sheets[$sheetI]->metadata->rows = count($this->data[$sheetI]); $sheets[$sheetI]->metadata->columns = count($this->data[$sheetI][0]); $id = $sheetlib->replace_sheet(0, $title, "", $user, $parentSheetId); $parentSheetId = $parentSheetId ? $parentSheetId : $id; $grid = new TikiSheet($id); $handler = new TikiSheetHTMLTableHandler($sheets[$sheetI]); $res = $grid->import($handler); $handler = new TikiSheetDatabaseHandler($id); $grid->export($handler); } return $parentSheetId; } }
function wikiplugin_sheet($data, $params) { global $dbTiki, $tiki_p_edit_sheet, $tiki_p_edit, $tiki_p_admin_sheet, $tiki_p_admin, $prefs, $user, $sheetlib, $page, $tikilib, $smarty; extract($params, EXTR_SKIP); $style = isset($height) ? "height: {$height} !important;" : ''; $style .= isset($width) ? "width: {$width};" : ''; // $urlHeight = (isset($height)) ? "&height=$height" : ''; // $urlHeight .= (isset($width)) ? "&width=$width" : ''; $urlHeight = isset($height) ? "&height=100" : ''; // not setting any height or width in the sheet params created for me the literal '...&height=100%&...' or '...&width=100%&...' in the url with a 400 error (bad request). Hardcoding to 100 (instead of 100%) to avoid this error until a better fix is found $urlHeight .= isset($width) ? "&width=100" : ''; // not setting any height or width in the sheet params created for me the literal '...&height=100%&...' or '...&width=100%&...' in the url with a 400 error (bad request). Hardcoding to 100 (instead of 100%) to avoid this error until a better fix is found $editable = isset($editable) && $editable == 'n' ? false : true; $subsheets = isset($subsheets) && $subsheets == 'n' ? false : true; $class = isset($class) ? " {$class}" : ''; $sheetlib = TikiLib::lib("sheet"); static $index = 0; ++$index; if (empty($id) && empty($url)) { if ($tiki_p_edit_sheet != 'y' || $tiki_p_edit != 'y') { return "<b>missing id parameter for plugin</b><br />"; } else { if (isset($_POST['create_sheet'], $_POST['index']) && $index == $_POST['index']) { // Create a new sheet and rewrite page $sheetId = $sheetlib->replace_sheet(null, tra('New sheet in page: ') . $page, '', $user); $page = htmlentities($page); $content = htmlentities($data); $formId = "form{$index}"; return <<<EOF \t\t\t\t~np~ \t\t\t\t<form id="{$formId}" method="post" action="tiki-wikiplugin_edit.php"> \t\t\t\t<div> \t\t\t\t\t<input type="hidden" name="page" value="{$page}"/> \t\t\t\t\t<input type="hidden" name="content" value="{$data}"/> \t\t\t\t\t<input type="hidden" name="index" value="{$index}"/> \t\t\t\t\t<input type="hidden" name="type" value="sheet"/> \t\t\t\t\t<input type="hidden" name="params[id]" value="{$sheetId}"/> \t\t\t\t</div> \t\t\t\t</form> \t\t\t\t<script type="text/javascript"> \t\t\t\tdocument.getElementById('{$formId}').submit(); \t\t\t\t</script> \t\t\t\t~/np~ EOF; } else { $label = tra('Create New Sheet'); return <<<EOF ~np~ <form method="post" action=""> \t<p> \t\t<input type="submit" name="create_sheet" value="{$label}"/> \t\t<input type="hidden" name="index" value="{$index}"/> \t</p> </form> ~/np~ EOF; } } } $sheet = new TikiSheet(); if (empty($url)) { $info; if (!empty($id)) { $info = $sheetlib->get_sheet_info($id); } if (empty($info)) { return tra("Error loading spreadsheet"); } $objectperms = Perms::get('sheet', $id); if (!$objectperms->view_sheet && !($user && $info['author'] == $user)) { return tra('Permission denied'); } // Build required objects $db = new TikiSheetDatabaseHandler($id); //$out = new TikiSheetOutputHandler($data); // Fetch sheet from database $sheet->import($db); } else { if (!isset($simple)) { $simple = 'y'; } } $calcOff = ''; if (!empty($range)) { $sheet->setRange($range); $calcOff = ',calcOff: true'; } // Grab sheet output if (isset($url)) { $file = file_get_contents($url); $pathInfo = pathinfo($url); if ($pathInfo['extension'] == 'csv') { $handler = new TikiSheetCSVHandler($url); $grid = new TikiSheet(); $grid->import($handler); $ret = $grid->getTableHtml(true, null, false); } else { $ret = file_get_contents($url); } } else { $ret = $sheet->getTableHtml($subsheets); } if (strpos($ret, '<table ') === false) { return '~np~' . $ret . '~/np~'; // return a single cell raw } if (!isset($simple) || $simple != 'y') { global $headerlib; $sheetlib->setup_jquery_sheet(); $headerlib->add_jq_onready('$("div.tiki_sheet").each(function() { $(this).sheet($.extend($.sheet.tikiOptions,{ editable:false' . $calcOff . '})); });'); } $ret = '<div id="tiki_sheet' . $sheet->instance . '" class="tiki_sheet' . $class . '" style="overflow:hidden;' . $style . '">' . $ret . '</div>'; if ($editable && ($objectperms->edit_sheet || $objectperms->admin_sheet || $tiki_p_admin == 'y')) { $smarty->loadPlugin('smarty_function_button'); //If you've given the sheet a url, you can't edit it, disable if not possible if (!isset($url)) { $button_params = array('_text' => tra("Edit Sheet"), '_script' => "tiki-view_sheets.php?sheetId={$id}&parse=edit{$urlHeight}&page={$page}"); } $ret .= smarty_function_button($button_params, $smarty); } return '~np~' . $ret . '~/np~'; }
$series[] = 'y0'; $series[] = 'y1'; $series[] = 'y2'; $series[] = 'y3'; $series[] = 'y4'; } else { $series[] = $s; } } $smarty->assign('mode', 'param'); $smarty->assign('series', $series); $smarty->assign('graph', $graph); $smarty->assign('renderer', $_GET['renderer']); $handler = new TikiSheetDatabaseHandler($sheetId); $grid = new TikiSheet($_REQUEST['sheetId']); $grid->import($handler); $dataGrid = $grid->getTableHtml(true); require_once 'lib/sheet/grid.php'; $sheetlib->setup_jquery_sheet(); $headerlib->add_jq_onready('$("div.tiki_sheet").sheet($.extend($.sheet.tikiOptions, {editable: false}));'); $smarty->assign('dataGrid', $dataGrid); if (function_exists('pdf_new')) { $smarty->assign('format', $_GET['format']); $smarty->assign('orientation', $_GET['orientation']); } if (function_exists('imagepng')) { $smarty->assign('im_width', $_GET['width']); $smarty->assign('im_height', $_GET['height']); } if (is_a($g, 'GridBasedGraphic')) { $smarty->assign('showgridparam', true);
function _save(TikiSheet &$sheet) { global $user, $prefs; $tikilib = TikiLib::lib('tiki'); // Load the current database state {{{3 $current = new TikiSheet(); $handler = new TikiSheetDatabaseHandler($this->id, null, $this->metadata); $current->import($handler); // Find differences {{{3 $mods = array(); for ($row = 0; $sheet->getRowCount() > $row; $row++) { for ($col = 0; $sheet->getColumnCount() > $col; $col++) { if (!$sheet->equals($current, $row, $col)) { $mods[] = array("row" => $row, "col" => $col); } } } $stamp = time(); $inserts = array(); $updates = array(); $updates[] = $stamp; $updates[] = $this->id; // Update the database {{{3 if (is_array($mods)) { foreach ($mods as $coord) { extract($coord); $value = $sheet->dataGrid[$row][$col]['value']; $calc = $sheet->calcGrid[$row][$col]['calculation']; $width = $sheet->cellInfo[$row][$col]['width']; $height = $sheet->cellInfo[$row][$col]['height']; $format = $sheet->cellInfo[$row][$col]['format']; $style = $sheet->cellInfo[$row][$col]['style']; $class = $sheet->cellInfo[$row][$col]['class']; $updates[] = $row; $updates[] = $col; //Now that sheets have styles, many things can change and the cell not have a value. //if ( !$sheet->isEmpty( $row, $col ) ) $inserts[] = array((int) $this->id, $stamp, $row, $col, $value, $calc, $width, $height, $format, $style, $class, $user); } } $updates[] = $sheet->getRowCount(); $updates[] = $sheet->getColumnCount(); $conditions = str_repeat("( rowIndex = ? AND columnIndex = ? ) OR ", (count($updates) - 4) / 2); if ($prefs['feature_actionlog'] == 'y') { // must keep the previous value to do the difference $query = "SELECT `rowIndex`, `columnIndex`, `value`, `style`, `class` FROM `tiki_sheet_values` WHERE `sheetId` = ? AND `end` IS NULL"; $result = $tikilib->query($query, array($this->id)); $old = array(); while ($row = $result->fetchRow()) { $old[$row['rowIndex'] . '-' . $row['columnIndex']] = $row['value']; $old[$row['rowIndex'] . '-' . $row['columnIndex']]['style'] = $row['style']; $old[$row['rowIndex'] . '-' . $row['columnIndex']]['class'] = $row['class']; } $tikilib->query("UPDATE `tiki_sheet_layout` SET `metadata` = ? WHERE `sheetId` = ?", array($handler->metadata, $handler->id)); } $tikilib->query("UPDATE `tiki_sheet_values` SET `end` = ? WHERE `sheetId` = ? AND `end` IS NULL AND ( {$conditions}`rowIndex` >= ? OR `columnIndex` >= ? )", $updates); if (count($inserts) > 0) { foreach ($inserts as $values) { $tikilib->query("INSERT INTO `tiki_sheet_values` (`sheetId`, `begin`, `rowIndex`, `columnIndex`, `value`, `calculation`, `width`, `height`, `format`, `style`, `class`, `user` ) VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )", $values); } } if ($prefs['feature_actionlog'] == 'y') { $logslib = TikiLib::lib('logs'); $add = 0; $del = 0; foreach ($inserts as $values) { $add += strlen($values[4]); if (!empty($old[$values[2] . '-' . $values[3]])) { $del += strlen($old[$values[2] . '-' . $values[3]]); } } if ($prefs['feature_contribution'] == 'y' && isset($_REQUEST['contributions'])) { $contributionlib = TikiLib::lib('contribution'); $contributionlib->assign_contributions($_REQUEST['contributions'], $this->id, 'sheet', '', '', ''); } if (isset($_REQUEST['contributions'])) { $logslib->add_action('Updated', $this->id, 'sheet', "add={$add}&del={$del}&sheetId=" . $this->id, '', '', '', '', $_REQUEST['contributions']); } else { $logslib->add_action('Updated', $this->id, 'sheet', "add={$add}&del={$del}&sheetId=" . $this->id); } } // }}}3 return true; }
function outputSheet($name = "") { $sheetlib = TikiLib::lib("sheet"); if (empty($name)) { $name = $this->type; } $handler = new TikiSheetSimpleArrayHandler(array("values" => $this->outputArray(), "name" => $name)); $grid = new TikiSheet(); $grid->import($handler); return $grid->getTableHtml(); }
// Instanciate the handler switch ($handler) { case 'TikiSheetWikiTableHandler': // Well known, special handlers $handler = new $handler($_POST['page']); break; default: // All file based handlers registered if (!in_array($handler, TikiSheet::getHandlerList())) { $smarty->assign('msg', "Handler is not allowed."); $smarty->display("error.tpl"); die; } $handler = new $handler($_FILES['file']['tmp_name'], $encoding, 'UTF-8'); } if (!$grid->import($handler)) { $smarty->assign('msg', "Impossible to import the file."); $smarty->display("error.tpl"); die; } $handler = new TikiSheetDatabaseHandler($sheetId); $grid->export($handler); ob_start(); $handler = new TikiSheetOutputHandler(); $grid->export($handler); $smarty->assign("grid_content", ob_get_contents()); ob_end_clean(); } else { $list = array(); $encoding = new Encoding(); $charsetList = $encoding->get_input_supported_encodings();
function join_with_sub_grids($id, $date) { global $prefs, $sheetlib; $result1 = ""; $result2 = ""; $handler = new TikiSheetDatabaseHandler($id, $date); $handler->setReadDate($date); $grid = new TikiSheet(); $grid->import($handler); $childSheetIds = $sheetlib->get_related_sheet_ids($grid->id); $i = 0; $grids = array($grid); foreach ($childSheetIds as $childSheetId) { $handler = new TikiSheetDatabaseHandler($childSheetId, $date); $handler->setReadDate($date); $childSheet = new TikiSheet(); $childSheet->import($handler); array_push($grids, $childSheet); $i++; } return $grids; }
function wikiplugin_sheet($data, $params) { global $dbTiki, $tiki_p_edit_sheet, $tiki_p_edit, $tiki_p_admin_sheet, $tiki_p_admin, $prefs, $user, $sheetlib, $page, $tikilib; extract($params, EXTR_SKIP); if (!class_exists('TikiSheet')) { require "lib/sheet/grid.php"; } static $index = 0; ++$index; if (!isset($id)) { if ($tiki_p_edit_sheet != 'y' || $tiki_p_edit != 'y') { return "<b>missing id parameter for plugin</b><br />"; } else { if (isset($_POST['create_sheet'], $_POST['index']) && $index == $_POST['index']) { // Create a new sheet and rewrite page $sheetId = $sheetlib->replace_sheet(null, tra('New sheet in page: ') . $page, '', $user); $page = htmlentities($page); $content = htmlentities($data); $formId = "form{$index}"; return <<<EOF ~np~ <form id="{$formId}" method="post" action="tiki-wikiplugin_edit.php"> <div> \t<input type="hidden" name="page" value="{$page}"/> \t<input type="hidden" name="content" value="{$data}"/> \t<input type="hidden" name="index" value="{$index}"/> \t<input type="hidden" name="type" value="sheet"/> \t<input type="hidden" name="params[id]" value="{$sheetId}"/> </div> </form> <script type="text/javascript"> document.getElementById('{$formId}').submit(); </script> ~/np~ EOF; } else { $intro = tra('Incomplete call to plugin: No target sheet.'); $label = tra('Create new sheet'); return <<<EOF ~np~ <form method="post" action=""> \t<p>{$intro}</p> \t<p> \t\t<input type="submit" name="create_sheet" value="{$label}"/> \t\t<input type="hidden" name="index" value="{$index}"/> \t</p> </form> ~/np~ EOF; } } } // Build required objects $sheet = new TikiSheet(); $db = new TikiSheetDatabaseHandler($id); $out = new TikiSheetOutputHandler($data); // Fetch sheet from database $sheet->import($db); // Grab sheet output ob_start(); $sheet->export($out); $ret = ob_get_contents(); ob_end_clean(); if ($tiki_p_edit_sheet == 'y' || $tiki_p_admin_sheet == 'y' || $tiki_p_admin == 'y') { $ret .= "<a href='tiki-view_sheets.php?sheetId={$id}&readdate=" . time() . "&mode=edit' class='linkbut'>" . tra("Edit Sheet") . "</a>"; } return $ret; }
function diff_sheets_as_html($id, $dates = null) { global $prefs; $count_longest = function ($array1, $array2) { return max(count($array1), count($array2)); }; $join_with_sub_grids = function ($id, $date) { global $prefs; $handler = new TikiSheetDatabaseHandler($id, $date); $handler->setReadDate($date); $grid = new TikiSheet(); $grid->import($handler); $childSheetIds = $this->get_related_sheet_ids($grid->id); $i = 0; $grids = array($grid); foreach ($childSheetIds as $childSheetId) { $handler = new TikiSheetDatabaseHandler($childSheetId, $date); $handler->setReadDate($date); $childSheet = new TikiSheet(); $childSheet->import($handler); array_push($grids, $childSheet); $i++; } return $grids; }; $sanitize_for_diff = function ($val) { $val = str_replace("<br/>", "<br>", $val); $val = str_replace("<br />", "<br>", $val); $val = str_replace("<br />", "<br>", $val); $val = str_replace("<BR/>", "<br>", $val); $val = str_replace("<BR />", "<br>", $val); $val = str_replace("<BR />", "<br>", $val); return explode("<br>", $val); }; $diff_to_html = function ($changes) use($count_longest) { $result = array("", ""); for ($i = 0; $i < $count_longest($changes->orig, $changes->final); $i++) { $class = array("", ""); $char = array("", ""); $vals = array(trim($changes->orig[$i]), trim($changes->final[$i])); if ($vals[0] && $vals[1]) { if ($vals[0] != $vals[1]) { $class[1] .= "diffadded"; } } else { if ($vals[0]) { $class[0] .= "diffadded"; $class[1] .= "diffdeleted"; $vals[1] = $vals[0]; $char[1] = "-"; } else { if ($vals[1]) { $class[0] .= "diffdeleted"; $class[1] .= "diffadded"; $char[1] = "+"; } } } if ($vals[0]) { $result[0] .= "<td class='{$class['0']}'>" . $char[0] . $vals[0] . "</td>"; } if ($vals[1]) { $result[1] .= "<td class='{$class['1']}'>" . $char[1] . $vals[1] . "</td>"; } } return $result; }; $grids1 = $join_with_sub_grids($id, $dates[0]); $grids2 = $join_with_sub_grids($id, $dates[1]); $result1 = ''; $result2 = ''; for ($i = 0; $i < $count_longest($grids1, $grids2); $i++) { //cycle through the sheets within a spreadsheet $result1 .= "<table title='" . $grids1[$i]->name() . "'>"; $result2 .= "<table title='" . $grids2[$i]->name() . "'>"; for ($row = 0; $row < $count_longest($grids1[$i]->dataGrid, $grids2[$i]->dataGrid); $row++) { //cycle through rows $result1 .= "<tr>"; $result2 .= "<tr>"; for ($col = 0; $col < $count_longest($grids1[$i]->dataGrid[$row], $grids2[$i]->dataGrid[$row]); $col++) { //cycle through columns $diff = new Text_Diff($sanitize_for_diff(html_entity_decode($grids1[$i]->dataGrid[$row][$col])), $sanitize_for_diff(html_entity_decode($grids2[$i]->dataGrid[$row][$col]))); $changes = $diff->getDiff(); //I left this diff switch, but it really isn't being used as of now, in the future we may though. switch (get_class($changes[0])) { case 'Text_Diff_Op_copy': $values = $diff_to_html($changes[0]); break; case 'Text_Diff_Op_change': $values = $diff_to_html($changes[0]); break; case 'Text_Diff_Op_delete': $values = $diff_to_html($changes[0]); break; case 'Text_Diff_Op_add': $values = $diff_to_html($changes[0]); break; default: $values = $diff_to_html($changes[0]); } $result1 .= empty($values[0]) ? '<td></td>' : $values[0]; $result2 .= empty($values[1]) ? '<td></td>' : $values[1]; } $result1 .= "</tr>"; $result2 .= "</tr>"; } $result1 .= "</table>"; $result2 .= "</table>"; } return array($result1, $result2); }