예제 #1
0
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~';
}