示例#1
0
文件: grid.php 项目: linuxwhy/tiki-1
 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;
 }
示例#2
0
 function save_sheet($sheets, $sheetId, $type = 'db')
 {
     global $user, $sheetlib;
     $sheets = json_decode($sheets);
     $rc = '';
     if (!empty($sheetId)) {
         $grid = new TikiSheet();
         if (is_array($sheets)) {
             foreach ($sheets as $sheet) {
                 $handler = new TikiSheetHTMLTableHandler($sheet);
                 $res = $grid->import($handler);
                 // Save the changes
                 $rc .= strlen($rc) === 0 ? '' : ', ';
                 if ($sheet->metadata->sheetId != $sheetId) {
                     $sheetIds[] = $sheet->metadata->sheetId;
                 }
                 if ($res) {
                     if (!$sheet->metadata->sheetId) {
                         if (!empty($sheet->metadata->title)) {
                             $title = $sheet->metadata->title;
                         } else {
                             $title = $info['title'] . ' subsheet';
                         }
                         $newId = $sheetlib->replace_sheet(0, $title, '', $user, $sheetId);
                         $rc .= tra('new') . " (sheetId={$newId}) ";
                         $sheet->metadata->sheetId = $newId;
                         $handler = new TikiSheetHTMLTableHandler($sheet);
                         $res = $grid->import($handler);
                     }
                     if ($sheetId && $res) {
                         $handler = new TikiSheetDatabaseHandler($sheet->metadata->sheetId);
                         $grid->export($handler);
                         $rc .= $grid->getColumnCount() . ' x ' . $grid->getRowCount() . ' ' . tra('sheet') . " (sheetId=" . $sheet->metadata->sheetId . ")";
                     }
                     if (!empty($sheet->metadata->title)) {
                         $sheetlib->set_sheet_title($sheet->metadata->sheetId, $sheet->metadata->title);
                     }
                 }
             }
         }
     }
     return $res ? tra('Saved') . ': ' . $rc : tra('Save failed');
 }