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