ajax
3 years ago
databases
3 years ago
migrations
3 years ago
models
3 years ago
queries
3 years ago
utils
3 years ago
chart.php
3 years ago
chart_geo.php
3 years ago
dashboard_widget.php
3 years ago
db.php
3 years ago
filters.php
3 years ago
freemius.php
3 years ago
independent_analytics.php
3 years ago
known_referrers.php
3 years ago
quick_stats.php
3 years ago
rest_api.php
3 years ago
settings.php
3 years ago
table.php
3 years ago
table_geo.php
3 years ago
table_referrers.php
3 years ago
table_views.php
3 years ago
track_resource_changes.php
3 years ago
view_counter.php
3 years ago
table.php
225 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | abstract class Table |
| 6 | { |
| 7 | abstract protected function columns(); |
| 8 | |
| 9 | abstract protected function default_columns(); |
| 10 | |
| 11 | abstract protected function rows(array $dates); |
| 12 | |
| 13 | abstract protected function table_name(); |
| 14 | |
| 15 | private $filters; |
| 16 | |
| 17 | public function __construct() |
| 18 | { |
| 19 | $this->filters = new Filters(); |
| 20 | } |
| 21 | |
| 22 | public function get_table_markup($dates = null) |
| 23 | { |
| 24 | if (is_null($dates)) { |
| 25 | $dates = IAWP()->default_date_range(); |
| 26 | } |
| 27 | |
| 28 | $rows = $this->rows($dates); |
| 29 | $columns = $this->default_columns(); |
| 30 | |
| 31 | return $this->get_rendered_template($rows, $columns); |
| 32 | } |
| 33 | |
| 34 | public function get_row_markup($rows, $columns) |
| 35 | { |
| 36 | return $this->get_rendered_template($rows, $columns, true); |
| 37 | } |
| 38 | |
| 39 | private function get_rendered_template($rows, $columns, $just_rows = false) |
| 40 | { |
| 41 | $templates = IAWP()->templates(); |
| 42 | |
| 43 | $templates->registerFunction('row_data_attributes', [$this, 'get_row_data_attributes']); |
| 44 | $templates->registerFunction('cell_content', [$this, 'get_cell_content']); |
| 45 | |
| 46 | return $templates->render('table/index', [ |
| 47 | 'just_rows' => $just_rows, |
| 48 | 'table_name' => $this->table_name(), |
| 49 | 'columns' => $columns, |
| 50 | 'all_columns' => $this->columns(), |
| 51 | 'row_count' => count($rows), |
| 52 | 'rows' => $rows, |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | public function get_row_data_attributes($row) |
| 57 | { |
| 58 | $html = ''; |
| 59 | |
| 60 | foreach ($this->columns() as $key => $value) { |
| 61 | $data_val = $row->$key(); |
| 62 | // Need to make an exception for labels so that sorting matches what people see e.g. attachment -> Media |
| 63 | if ($key == 'type') { |
| 64 | $data_val = $row->type_label(); |
| 65 | } |
| 66 | |
| 67 | $html .= ' data-' . esc_attr($key) . '="' . esc_attr($data_val) . '"'; |
| 68 | } |
| 69 | |
| 70 | return $html; |
| 71 | } |
| 72 | |
| 73 | public function get_cell_content($row, $column) |
| 74 | { |
| 75 | if (is_null($row->$column())) { |
| 76 | return null; |
| 77 | } |
| 78 | if ($column == 'title' && $row->is_deleted()) { |
| 79 | return Security::string($row->$column()) . ' <span class="deleted-label">' . esc_html__('(deleted)', 'iawp') . '</span>'; |
| 80 | } elseif ($column == 'views') { |
| 81 | return number_format($row->views(), 0); |
| 82 | } elseif ($column == 'visitors') { |
| 83 | return number_format($row->visitors(), 0); |
| 84 | } elseif ($column == 'url') { |
| 85 | if ($row->is_deleted()) { |
| 86 | return esc_url($row->path()); |
| 87 | } else { |
| 88 | return '<a href="' . esc_url($row->url()) . '" target="_blank" class="external-link">' . esc_url($row->path()) . '<span class="dashicons dashicons-external"></span></a>'; |
| 89 | } |
| 90 | } elseif ($column == 'author') { |
| 91 | return Security::html($row->avatar()) . ' ' . Security::string($row->author()); |
| 92 | } elseif ($column == 'date') { |
| 93 | return Security::string(date(Date_Format::php(), $row->date())); |
| 94 | } elseif ($column == 'type' && method_exists($row, 'icon')) { |
| 95 | return Security::html($row->icon(0)) . ' ' . Security::string($row->type_label()); |
| 96 | } elseif ($column == 'referrer' && !$row->is_direct()) { |
| 97 | return '<a href="' . esc_url($row->referrer()) . '" target="_blank" class="external-link">' . Security::string($row->referrer_label()) . '<span class="dashicons dashicons-external"></span></a>'; |
| 98 | } elseif ($column === 'country') { |
| 99 | return '<img class="flag" alt="Country flag" src="' . $row->flag() . '"/>' . $row->country(); |
| 100 | } else { |
| 101 | return $row->$column(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private function filters() |
| 106 | { |
| 107 | return $this->filters; |
| 108 | } |
| 109 | |
| 110 | public function output_toolbar(array $columns) |
| 111 | { |
| 112 | list($start, $end) = IAWP()->default_date_range(); ?> |
| 113 | <div class="toolbar"> |
| 114 | <div class="buttons"> |
| 115 | <div class="modal-parent" |
| 116 | data-controller="dates" |
| 117 | data-dates-relative-range-id-value="LAST_THIRTY" |
| 118 | data-dates-exact-start-value="" |
| 119 | data-dates-exact-end-value="" |
| 120 | data-dates-first-day-of-week-value="<?php echo absint(IAWP()->get_option('iawp_dow', 0)) ?>" |
| 121 | data-dates-css-url-value="<?php echo esc_url(url_to('dist/styles/easepick/datepicker.css')) ?>" |
| 122 | data-dates-format-value="<?php echo esc_attr(Date_Format::js()) ?>" |
| 123 | > |
| 124 | <button id="dates-button" |
| 125 | class="iawp-button ghost-white" |
| 126 | data-action="dates#toggleModal" |
| 127 | > |
| 128 | <span class="dashicons dashicons-calendar-alt"></span> |
| 129 | <span><?php esc_html_e('Last 30 Days', 'iawp'); ?></span> |
| 130 | </button> |
| 131 | <div id="modal-dates" |
| 132 | class="modal large flex" |
| 133 | data-dates-target="modal" |
| 134 | > |
| 135 | <div class="modal-inner"> |
| 136 | <div id="easepick-picker" |
| 137 | data-dates-target="easepick" |
| 138 | style="display: none;" |
| 139 | > |
| 140 | </div> |
| 141 | <div class="relative-dates"> |
| 142 | <?php foreach (Relative_Range::ranges() as $range): ?> |
| 143 | <button class="iawp-button ghost-purple" |
| 144 | data-dates-target="relativeRange" |
| 145 | data-action="dates#relativeRangeSelected" |
| 146 | data-relative-range-id="<?php echo $range->id ?>" |
| 147 | data-relative-range-label="<?php echo $range->label ?>" |
| 148 | data-relative-range-start="<?php echo $range->start->format('Y-m-d') ?>" |
| 149 | data-relative-range-end="<?php echo $range->end->format('Y-m-d') ?>" |
| 150 | > |
| 151 | <?php echo $range->label ?> |
| 152 | </button> |
| 153 | <?php endforeach ?> |
| 154 | </div> |
| 155 | <div> |
| 156 | <hr/> |
| 157 | </div> |
| 158 | <div> |
| 159 | <button class="iawp-button purple" |
| 160 | data-dates-target="apply" |
| 161 | data-action="dates#apply" |
| 162 | > |
| 163 | Apply |
| 164 | </button> |
| 165 | <button class="iawp-button ghost-purple" |
| 166 | data-action="dates#closeModal" |
| 167 | > |
| 168 | Cancel |
| 169 | </button> |
| 170 | </div> |
| 171 | </div> |
| 172 | </div> |
| 173 | </div> |
| 174 | <?php // Todo - This needs to be able to render a set of stored filters (such as those in a saved report) |
| 175 | ?> |
| 176 | <?php $this->filters()->output_filters($columns) ?> |
| 177 | <?php $this->column_picker() ?> |
| 178 | </div> |
| 179 | </div><?php |
| 180 | } |
| 181 | |
| 182 | private function column_picker() |
| 183 | { |
| 184 | $default_columns = $this->default_columns(); ?> |
| 185 | |
| 186 | <div class="customize-columns modal-parent small" |
| 187 | data-controller="columns" |
| 188 | > |
| 189 | <button id="customize-columns" |
| 190 | class="iawp-button ghost-white" |
| 191 | data-action="columns#toggleModal" |
| 192 | > |
| 193 | <span class="dashicons dashicons-columns"></span> |
| 194 | <span><?php _e('Edit Columns', 'iawp'); ?></span> |
| 195 | </button> |
| 196 | <div id="modal-columns" |
| 197 | class="modal small" |
| 198 | data-columns-target="modal" |
| 199 | > |
| 200 | <div class="modal-inner"> |
| 201 | <div class="title-small"> |
| 202 | <?php _e('Columns', 'iawp'); ?> |
| 203 | </div> |
| 204 | <?php foreach ($this->columns() as $key => $label): ?> |
| 205 | <?php $selected = in_array($key, $default_columns); ?> |
| 206 | <label class="column-label" |
| 207 | for="iawp-column-<?php echo esc_attr($key); ?>" |
| 208 | > |
| 209 | <input id="iawp-column-<?php echo esc_attr($key); ?>" |
| 210 | <?php checked(true, $selected, true); ?> |
| 211 | type="checkbox" |
| 212 | name="<?php echo esc_attr($key) ?>" |
| 213 | data-columns-target="checkbox" |
| 214 | data-action="columns#check" |
| 215 | > |
| 216 | <span><?php echo esc_html($label); ?></span> |
| 217 | </label> |
| 218 | <?php endforeach ?> |
| 219 | </div> |
| 220 | </div> |
| 221 | </div> |
| 222 | <?php |
| 223 | } |
| 224 | } |
| 225 |