/** * @return \Tabulate\Template */ private function getTemplate(\Tabulate\DB\Table $table) { $template = new Template('record/edit.twig'); $customTemplate = 'record/' . $table->getName() . '/edit.twig'; if ($template->getLoader()->exists($customTemplate)) { $template->setTemplateName($customTemplate); } $template->table = $table; $template->controller = 'record'; $template->title = $table->getTitle(); $template->tables = $table->getDatabase()->getTables(); return $template; }
public function save($args) { if (!isset($args['schema'])) { $url = admin_url('admin.php?page=tabulate_schema'); wp_redirect($url); } $db = new Database($this->wpdb); $schema = $db->getTable($args['schema']); $new_name = $args['schema']; if ($schema instanceof Table && !empty($args['new_name'])) { $schema->rename($args['new_name']); $new_name = $schema->getName(); } $template = new Template('schema.html'); $template->addNotice('updated', 'Schema updated.'); $url = admin_url('admin.php?page=tabulate_schema&schema=' . $new_name); wp_redirect($url); }
public function save() { $grants = new \Tabulate\DB\Grants(); // Validate the POSTed grants. $new_grants = array(); foreach ($_POST as $table => $table_grants) { if (in_array($table, $this->table_names)) { $new_grants[$table] = array(); foreach ($table_grants as $capability => $roles) { if (in_array($capability, $grants->get_capabilities())) { $new_grants[$table][$capability] = array_keys($roles); } } } } // Save the grants and return to the granting table. $grants->set($new_grants); $this->template->addNotice('updated', 'Grants saved.'); wp_redirect($this->get_url('index')); exit; }
public function remind(Request $request, Response $response, array $args) { $name = $request->get('name'); if ($request->get('login')) { return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name); } $config = new Config(); $user = new User($this->db); $user->loadByName($name); $template = new Template('remind_email.twig'); if (!empty($user->getEmail())) { $template->user = $user; $template->token = $user->getReminder(); $message = \Swift_Message::newInstance()->setSubject('Password reminder')->setFrom(array($config->siteEmail() => $config->siteTitle()))->setTo(array($user->getEmail() => $user->getName()))->setBody($template->render(), 'text/html'); $this->email($message); } else { // Pause for a moment, so it's not so obvious which users' names are resulting in mail being sent. sleep(5); } $template->alert('success', 'Please check your email', true); return new RedirectResponse($this->config->baseUrl() . '/remind?name=' . $user->getName()); }
public function loginForm() { $template = new \Tabulate\Template('users/login.twig'); $template->title = 'Log in'; echo $template->render(); }
/** * This action is for importing a single CSV file into a single database table. * It guides the user through the four stages of importing: * uploading, field matching, previewing, and doing the actual import. * All of the actual work is done in the CSV class. * * 1. In the first stage, a CSV file is **uploaded**, validated, and moved to a temporary directory. * The file is then accessed from this location in the subsequent stages of importing, * and only deleted upon either successful import or the user cancelling the process. * (The 'id' parameter of this action is the identifier for the uploaded file.) * 2. Once a valid CSV file has been uploaded, * its colums are presented to the user to be **matched** to those in the database table. * The columns from the database are presented first and the CSV columns are matched to these, * rather than vice versa, * because this way the user sees immediately what columns are available to be imported into. * 3. The column matches are then used to produce a **preview** of what will be added to and/or changed in the database. * All columns from the database are shown (regardless of whether they were in the import) and all rows of the import. * If a column is not present in the import the database will (obviously) use the default value if there is one; * this will be shown in the preview. * 4. When the user accepts the preview, the actual **import** of data is carried out. * Rows are saved to the database using the usual `Table::save()` method * and a message presented to the user to indicate successful completion. * * @return void */ public function import($args) { $template = new Template('import.html'); // Set up the progress bar. $template->stages = array('choose_file', 'match_fields', 'preview', 'complete_import'); $template->stage = 'choose_file'; // First make sure the user is allowed to import data into this table. $table = $this->getTable($args['table']); $template->record = $table->getDefaultRecord(); $template->action = 'import'; $template->table = $table; $template->maxsize = \Tabulate\File::maxUploadSize(); if (!$table->getDatabase()->checkGrant(Grants::IMPORT, $table->getName())) { $template->addNotice('error', 'You do not have permission to import data into this table.'); return $template->render(); } /* * Stage 1 of 4: Uploading. */ $template->form_action = $table->getUrl('import'); try { $hash = isset($_GET['hash']) ? $_GET['hash'] : false; $uploaded = isset($_FILES['file']) ? $_FILES['file'] : false; $csvFile = new \Tabulate\CSV($hash, $uploaded); } catch (\Exception $e) { $template->addNotice('error', $e->getMessage()); return $template->render(); } /* * Stage 2 of 4: Matching fields */ if ($csvFile->loaded()) { $template->file = $csvFile; $template->stage = $template->stages[1]; $template->form_action .= "&hash=" . $csvFile->hash; } /* * Stage 3 of 4: Previewing */ if ($csvFile->loaded() and isset($_POST['preview'])) { $template->stage = $template->stages[2]; $template->columns = serialize($_POST['columns']); $errors = array(); // Make sure all required columns are selected foreach ($table->getColumns() as $col) { // Handle missing columns separately; other column errors are // done in the CSV class. Missing columns don't matter if importing // existing records. $missing = empty($_POST['columns'][$col->getName()]); $pkPresent = isset($_POST['columns'][$table->getPkColumn()->getName()]); if (!$pkPresent && $col->isRequired() && $missing) { $errors[] = array('column_name' => '', 'column_number' => '', 'field_name' => $col->getName(), 'row_number' => 'N/A', 'messages' => array('Column required, but not found in CSV')); } } $template->errors = empty($errors) ? $csvFile->matchFields($table, wp_unslash($_POST['columns'])) : $errors; } /* * Stage 4 of 4: Import */ if ($csvFile->loaded() && isset($_POST['import'])) { $template->stage = $template->stages[3]; $this->wpdb->query('BEGIN'); $result = $csvFile->importData($table, unserialize($_POST['columns'])); $this->wpdb->query('COMMIT'); $template->addNotice('updated', 'Import complete; ' . $result . ' rows imported.'); } return $template->render(); }
public function timeline($args) { $table = $this->getTable($args['table']); $template = new \Tabulate\Template('timeline.html'); $template->action = 'timeline'; $template->table = $table; $start_date_arg = isset($args['start_date']) ? $args['start_date'] : date('Y-m-d'); $end_date_arg = isset($args['end_date']) ? $args['end_date'] : date('Y-m-d'); $start_date = new \DateTime($start_date_arg); $end_date = new \DateTime($end_date_arg); if ($start_date->diff($end_date, true)->d < 7) { // Add two weeks to the end date. $end_date->add(new \DateInterval('P14D')); } $date_period = new \DatePeriod($start_date, new \DateInterval('P1D'), $end_date); $template->start_date = $start_date->format('Y-m-d'); $template->end_date = $end_date->format('Y-m-d'); $template->date_period = $date_period; $data = array(); foreach ($table->getRecords(false) as $record) { if (!isset($data[$record->getTitle()])) { $data[$record->getTitle()] = array(); } $data[$record->getTitle()][] = $record; } $template->data = $data; return $template->render(); }
public function delete($args) { $db = new Database(); $table = $db->getTable($args['table']); $recordIdent = isset($args['ident']) ? $args['ident'] : false; if (!$recordIdent) { $this->redirect($table->getUrl()); } // Ask for confirmation. if (!isset($_POST['confirm_deletion'])) { $template = new \Tabulate\Template('record/delete.html'); $template->table = $table; $template->record = $table->getRecord($recordIdent); return $template->render(); } // Delete the record. try { $this->wpdb->query('BEGIN'); $table->deleteRecord($recordIdent); $this->wpdb->query('COMMIT'); } catch (\Exception $e) { $template = $this->getTemplate($table); $template->record = $table->getRecord($recordIdent); $template->addNotice('error', $e->getMessage()); return $template->render(); } wp_redirect($table->getUrl()); exit; }