These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file contains only a single class. |
||
4 | * |
||
5 | * @file |
||
6 | * @package Tabulate |
||
7 | */ |
||
8 | |||
9 | namespace WordPress\Tabulate\DB; |
||
10 | |||
11 | /** |
||
12 | * This class handles everything to do with Reports. |
||
13 | */ |
||
14 | class Reports { |
||
15 | |||
16 | /** |
||
17 | * The ID of the primary report. |
||
18 | */ |
||
19 | const DEFAULT_REPORT_ID = 1; |
||
20 | |||
21 | /** |
||
22 | * The database. |
||
23 | * |
||
24 | * @var \WordPress\Tabulate\DB\Database |
||
25 | */ |
||
26 | protected $db; |
||
27 | |||
28 | /** |
||
29 | * Create a Reports object based on a given Database. |
||
30 | * |
||
31 | * @param \WordPress\Tabulate\DB\Database $db The database object. |
||
32 | */ |
||
33 | public function __construct( Database $db ) { |
||
34 | $this->db = $db; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get the name of the 'reports' database table. |
||
39 | * |
||
40 | * @global \wpdb $wpdb |
||
41 | * @return string |
||
42 | */ |
||
43 | public static function reports_table_name() { |
||
44 | global $wpdb; |
||
45 | return $wpdb->prefix . TABULATE_SLUG . '_reports'; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the name of the 'report_sources' database table. |
||
50 | * |
||
51 | * @global \wpdb $wpdb |
||
52 | * @return string |
||
53 | */ |
||
54 | public static function report_sources_table_name() { |
||
55 | global $wpdb; |
||
56 | return $wpdb->prefix . TABULATE_SLUG . '_report_sources'; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get a Template instance based on a given report's template string and |
||
61 | * populated with all of the report's source queries. |
||
62 | * |
||
63 | * @param int $report_id The report ID. |
||
64 | * @return \WordPress\Tabulate\Template |
||
65 | * @throws Exception If the requested report could not be found. |
||
66 | */ |
||
67 | public function get_template( $report_id ) { |
||
68 | // Find the report. |
||
69 | $reports = $this->db->get_table( self::reports_table_name() ); |
||
70 | if ( false === $reports ) { |
||
71 | $msg = "Reports table not found. Please re-activate Tabulate to create it."; |
||
72 | throw new Exception( $msg ); |
||
73 | } |
||
74 | $report = $reports->get_record( $report_id ); |
||
75 | if ( false === $report ) { |
||
76 | throw new Exception( "Report $report_id not found." ); |
||
77 | } |
||
78 | $template = new \WordPress\Tabulate\Template( false, $report->template() ); |
||
0 ignored issues
–
show
|
|||
79 | $template->title = $report->title(); |
||
0 ignored issues
–
show
The method
title does not exist on object<WordPress\Tabulate\DB\Record> ? Since you implemented __call , maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
Loading history...
|
|||
80 | $template->file_extension = $report->file_extension(); |
||
0 ignored issues
–
show
The method
file_extension does not exist on object<WordPress\Tabulate\DB\Record> ? Since you implemented __call , maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
Loading history...
|
|||
81 | $template->mime_type = $report->mime_type(); |
||
0 ignored issues
–
show
The method
mime_type does not exist on object<WordPress\Tabulate\DB\Record> ? Since you implemented __call , maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
Loading history...
|
|||
82 | |||
83 | // Populate with source data. |
||
84 | $sql = "SELECT * FROM `" . self::report_sources_table_name() . "` WHERE report = " . $report_id; |
||
85 | $sources = $this->db->get_wpdb()->get_results( $sql ); |
||
86 | foreach ( $sources as $source ) { |
||
87 | $data = $this->db->get_wpdb()->get_results( $source->query ); |
||
88 | $template->{$source->name} = $data; |
||
89 | } |
||
90 | |||
91 | // Return the template. |
||
92 | return $template; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * On plugin activation, create the required database tables. |
||
97 | * |
||
98 | * @param \wpdb $wpdb The global database object. |
||
99 | */ |
||
100 | public static function activate( \wpdb $wpdb ) { |
||
101 | $db = new Database( $wpdb ); |
||
102 | |||
103 | View Code Duplication | if ( ! $db->get_table( self::reports_table_name() ) ) { |
|
104 | $sql = "CREATE TABLE IF NOT EXISTS `" . self::reports_table_name() . "` ( |
||
105 | `id` INT(4) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, |
||
106 | `title` varchar(200) NOT NULL UNIQUE, |
||
107 | `description` text NOT NULL, |
||
108 | `mime_type` varchar(50) NOT NULL DEFAULT 'text/html', |
||
109 | `file_extension` varchar(10) DEFAULT NULL COMMENT 'If defined, this report will be downloaded.', |
||
110 | `template` text NOT NULL COMMENT 'The Twig template used to display this report.' |
||
111 | ) ENGINE=InnoDB;"; |
||
112 | $wpdb->query( $sql ); |
||
113 | } |
||
114 | |||
115 | View Code Duplication | if ( ! $db->get_table( self::report_sources_table_name() ) ) { |
|
116 | $sql = "CREATE TABLE IF NOT EXISTS `" . self::report_sources_table_name() . "` ( |
||
117 | `id` INT(5) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, |
||
118 | `report` INT(4) unsigned NOT NULL, |
||
119 | FOREIGN KEY (`report`) REFERENCES `" . self::reports_table_name() . "` (`id`), |
||
120 | `name` varchar(50) NOT NULL, |
||
121 | `query` text NOT NULL |
||
122 | ) ENGINE=InnoDB;"; |
||
123 | $wpdb->query( $sql ); |
||
124 | } |
||
125 | |||
126 | if ( '0' === $wpdb->get_var( "SELECT COUNT(*) FROM `" . self::reports_table_name() . "`" ) ) { |
||
127 | // Create the default report, to list all reports. |
||
128 | $template_string = "<dl>\n" |
||
129 | . "{% for report in reports %}\n" |
||
130 | . " <dt><a href='{{ admin_url('admin.php?page=tabulate&controller=reports&id='~report.id) }}'>{{report.title}}</a></dt>\n" |
||
131 | . " <dd>{{report.description}}</dd>\n" |
||
132 | . "{% endfor %}\n" |
||
133 | . "</dl>"; |
||
134 | $sql1 = "INSERT INTO `" . self::reports_table_name() . "` SET" |
||
135 | . " id = " . self::DEFAULT_REPORT_ID . ", " |
||
136 | . " title = 'Reports', " |
||
137 | . " description = 'List of all Reports.'," |
||
138 | . " template = %s;"; |
||
139 | $wpdb->query( $wpdb->prepare( $sql1, array( $template_string ) ) ); |
||
140 | // And the query for the above report. |
||
141 | $query = "SELECT * FROM " . self::reports_table_name(); |
||
142 | $sql2 = "INSERT INTO `" . self::report_sources_table_name() . "` SET " |
||
143 | . " report = " . self::DEFAULT_REPORT_ID . "," |
||
144 | . " name = 'reports'," |
||
145 | . " query = %s;"; |
||
146 | $wpdb->query( $wpdb->prepare( $sql2, array( $query ) ) ); |
||
147 | } |
||
148 | |||
149 | } |
||
150 | } |
||
151 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: