Example #1
0
 /**
  * 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;
 }