private function get_table($table_name) { $db = new Database($this->wpdb); $table = $db->get_table($table_name); if (!$table) { throw new \Exception("Table '{$table}' not found."); } return $table; }
/** * Substitute the Shortcode with the relevant formatted output. * @param array $atts * @return string */ public function run($atts) { $defaults = array('format' => 'table', 'table' => null); $attrs = shortcode_atts($defaults, $atts); if (!isset($attrs['table'])) { return "<div class='tabulate error'>The 'table' attribute must be set.</div>"; } $table = $this->db->get_table($attrs['table']); if (!$table) { // Show no error for not-found tables? return ''; } $format_method = $attrs['format'] . '_format'; if (is_callable(array($this, $format_method))) { return $this->{$format_method}($table, $attrs); } else { return "Format '{$attrs['format']}' not available."; } }
/** * Get a list of a table's records' IDs and titles, filtered by * `$_GET['term']`, for foreign-key fields. Only used when there are more * than N records in a foreign table (otherwise the options are presented in * a select list). * * @return array */ public function foreign_key_values($table_name) { $db = new Database($this->wpdb); $table = $db->get_table($table_name); $table->add_filter($table->get_title_column(), 'like', '%' . $_GET['term'] . '%'); $out = array(); foreach ($table->get_records() as $record) { $out[] = array('value' => $record->get_primary_key(), 'label' => $record->get_title()); } return $out; }
/** * Substitute the Shortcode with the relevant formatted output. * @param array $atts * @return string */ public function run($atts) { $defaults = array('format' => 'table', 'table' => null); $attrs = shortcode_atts($defaults, $atts); if (!isset($attrs['table'])) { return "<div class='tabulate error'>The 'table' attribute must be set.</div>"; } $table = $this->db->get_table($attrs['table']); if (!$table) { if (!is_user_logged_in()) { return "<div class='tabulate error'>You are not logged in. " . wp_loginout(get_the_permalink(), false) . "</div>"; } return ''; } $format_method = $attrs['format'] . '_format'; if (is_callable(array($this, $format_method))) { return $this->{$format_method}($table, $attrs); } else { return "Format '{$attrs['format']}' not available."; } }
/** * Substitute the Shortcode with the relevant formatted output. * @param string[] $atts * @return string */ public function run($atts) { $defaults = array('format' => 'table', 'table' => null); $attrs = shortcode_atts($defaults, $atts); if (!isset($attrs['table'])) { return $this->error("The 'table' attribute must be set."); } $table = $this->db->get_table($attrs['table']); if (!$table) { if (!is_user_logged_in()) { return $this->error("You are not logged in. " . wp_loginout(get_the_permalink(), false)); } return $this->error(); } $format_method = $attrs['format'] . '_format'; if (is_callable(array($this, $format_method))) { wp_enqueue_script('tabulate-scripts'); return $this->{$format_method}($table, $attrs, $_GET); } else { return $this->error("Format '{$attrs['format']}' not available."); } }
/** * Get a Template instance based on a given report's template string and * populated with all of the report's source queries. * @param int $report_id * @return \WordPress\Tabulate\Template */ public function get_template($report_id) { // Find the report. $reports = $this->db->get_table(self::reports_table_name()); $report = $reports->get_record($report_id); if (!$report) { throw new \Exception("Report {$report_id} not found."); } $template = new \WordPress\Tabulate\Template(false, $report->template()); $template->title = $report->title(); $template->file_extension = $report->file_extension(); $template->mime_type = $report->mime_type(); // Populate with source data. $sql = "SELECT * FROM `" . self::report_sources_table_name() . "` WHERE report = " . $report_id; $sources = $this->db->get_wpdb()->get_results($sql); foreach ($sources as $source) { $data = $this->db->get_wpdb()->get_results($source->query); $template->{$source->name} = $data; } // Return the template. return $template; }
private function get_table($table_name) { $db = new Database($this->wpdb); $table = $db->get_table($table_name); if (!$table) { add_action('admin_notices', function ($table_name) use($table_name) { echo "<div class='error'><p>Table '" . $table_name . "' not found.</p></div>"; }); $home = new HomeController($this->wpdb); return $home->index(); } return $table; }
/** * Get a list of a table's records' IDs and titles, filtered by * `$_GET['term']`, for foreign-key fields. Only used when there are more * than N records in a foreign table (otherwise the options are presented in * a select list). * @param \WP_REST_Request $request The request, with a 'table_name' parameter. * @return array */ public function foreign_key_values(\WP_REST_Request $request) { if (!isset($this->get['term'])) { return array(); } $db = new Database($this->wpdb); $table = $db->get_table($request->get_param('table_name')); if (!$table instanceof \WordPress\Tabulate\DB\Table) { return array(); } // First get any exact matches. $out = $this->foreign_key_values_build($table, '=', $this->get['term']); // Then get any 'contains' matches. $out += $this->foreign_key_values_build($table, 'like', '%' . $this->get['term'] . '%'); return $out; }
protected function set_up($args) { $db = new Database($this->wpdb); $this->table = $db->get_table($args['table']); // Check that a point column exists. $points = $this->table->get_columns('point'); if (empty($points)) { // @TODO Show error. return; } $point_col = array_shift($points); $this->point_col_name = $point_col->get_name(); // Apply filters. $filter_param = isset($args['filter']) ? $args['filter'] : array(); $this->table->add_filters($filter_param); $this->table->add_filter($this->point_col_name, 'not empty', ''); }
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->get_table($args['schema']); $new_name = $args['schema']; if ($schema instanceof Table && !empty($args['new_name'])) { $schema->rename($args['new_name']); $new_name = $schema->get_name(); } $template = new Template('schema.html'); $template->add_notice('updated', 'Schema updated.'); $url = admin_url('admin.php?page=tabulate_schema&schema=' . $new_name); wp_redirect($url); }
public function kml($args) { $db = new Database($this->wpdb); $table = $db->get_table($args['table']); // Check that a point column exists. $points = $table->get_columns('point'); if (empty($points)) { // @TODO Show error. return; } $point_col = array_shift($points); $point_col_name = $point_col->get_name(); // Apply filters. $filter_param = isset($args['filter']) ? $args['filter'] : array(); $table->add_filters($filter_param); $table->add_filter($point_col_name, 'not empty', ''); // Create KML. $kml = new \SimpleXMLElement('<kml />'); $kml->addAttribute('xmlns', 'http://www.opengis.net/kml/2.2'); $kml_doc = $kml->addChild('Document'); foreach ($table->get_records(false) as $record) { $placemark = $kml_doc->addChild('Placemark'); $placemark->addChild('name', $record->get_title()); $placemark->addChild('description', htmlentities('<a href="' . $record->get_url() . '">View record.</a>')); $point = $placemark->addChild('Point'); $geom = \geoPHP::load($record->{$point_col_name}()); $point->addChild('coordinates', $geom->getX() . ',' . $geom->getY()); } // Send to browser. $download_name = date('Y-m-d') . '_' . $table->get_name() . '.kml'; header('Content-Encoding: UTF-8'); header('Content-type: application/vnd.google-earth.kml+xml; charset=UTF-8'); header('Content-Disposition: attachment; filename="' . $download_name . '"'); echo $kml->asXML(); exit; }
public static function activate() { global $wpdb; $db = new Database($wpdb); if (!$db->get_table(self::changesets_name())) { $sql = "CREATE TABLE IF NOT EXISTS `" . self::changesets_name() . "` (\n\t\t\t`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\t\t\t`date_and_time` DATETIME NOT NULL,\n\t\t\t`user_id` BIGINT(20) UNSIGNED NOT NULL,\n\t\t\tFOREIGN KEY (`user_id`) REFERENCES `{$wpdb->prefix}users` (`ID`),\n\t\t\t`comment` TEXT NULL DEFAULT NULL\n\t\t\t);"; $wpdb->query($sql); } if (!$db->get_table(self::changes_name())) { $sql = "CREATE TABLE IF NOT EXISTS `" . self::changes_name() . "` (\n\t\t\t`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,\n\t\t\t`changeset_id` INT(10) UNSIGNED NOT NULL,\n\t\t\tFOREIGN KEY (`changeset_id`) REFERENCES `" . self::changesets_name() . "` (`id`),\n\t\t\t`change_type` ENUM('field', 'file', 'foreign_key') NOT NULL DEFAULT 'field',\n\t\t\t`table_name` TEXT(65) NOT NULL,\n\t\t\t`record_ident` TEXT(65) NOT NULL,\n\t\t\t`column_name` TEXT(65) NOT NULL,\n\t\t\t`old_value` LONGTEXT NULL DEFAULT NULL,\n\t\t\t`new_value` LONGTEXT NULL DEFAULT NULL\n\t\t\t);"; $wpdb->query($sql); } }