PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/content/content.logs.php

https://github.com/nick-ryall/redirection_manager
PHP | 386 lines | 286 code | 85 blank | 15 comment | 47 complexity | 1056d42a790b4c0887ba7b24b297b681 MD5 | raw file
  1. <?php
  2. require_once(TOOLKIT . '/class.administrationpage.php');
  3. class contentExtensionRedirection_ManagerLogs extends AdministrationPage {
  4. protected $_driver = null;
  5. protected $_column = 'request_time';
  6. protected $_columns = array();
  7. protected $_direction = 'desc';
  8. protected $_logs = array();
  9. protected $_log = array();
  10. protected $_pagination = null;
  11. protected $_uri = null;
  12. public function __construct(&$parent){
  13. parent::__construct($parent);
  14. $this->_uri = URL . '/symphony/extension/redirection_manager';
  15. $this->_driver = $this->_Parent->ExtensionManager->create('redirection_manager');
  16. }
  17. public function build($context) {
  18. if (@$context[0] == 'show') {
  19. $this->_log = (object)$this->_driver->getLog($context[1]);
  20. } else {
  21. $this->__prepareIndex();
  22. }
  23. parent::build($context);
  24. }
  25. /*-------------------------------------------------------------------------
  26. Show
  27. -------------------------------------------------------------------------*/
  28. public function __viewShow() {
  29. $this->setPageType('form');
  30. $title = DateTimeObj::get(__SYM_DATETIME_FORMAT__, $this->_log->request_time);
  31. $this->setTitle("Redirection Manager &ndash; {$title}");
  32. $this->appendSubheading("<a href=\"{$this->_uri}/logs/\">Redirection Logs</a> &mdash; {$title}");
  33. $values = unserialize($this->_log->request_args);
  34. foreach ($values as $type => $array) {
  35. if (!empty($array)) {
  36. $type = strtoupper($type);
  37. $fieldset = new XMLElement('fieldset');
  38. $fieldset->setAttribute('class', 'settings');
  39. $fieldset->appendChild(new XMLElement('legend', "{$type} Values"));
  40. $pre = new XMLElement('pre');
  41. $code = new XMLElement('code');
  42. ob_start();
  43. print_r($array);
  44. $code->setValue(General::sanitize(ob_get_clean()));
  45. $pre->appendChild($code);
  46. $fieldset->appendChild($pre);
  47. $this->Form->appendChild($fieldset);
  48. }
  49. }
  50. }
  51. /*-------------------------------------------------------------------------
  52. Index
  53. -------------------------------------------------------------------------*/
  54. public function __prepareIndex() {
  55. $this->_columns = array(
  56. 'type' => array('Type', true),
  57. 'request_uri' => array('Requested URL', true),
  58. 'target_uri' => array('Redirected to:', true),
  59. 'hits' => array('Hits', true),
  60. 'request_time' => array('Time', true),
  61. 'request_method' => array('Method', true),
  62. 'remote_addr' => array('Remote IP', true),
  63. );
  64. if (@$_GET['sort'] and $this->_columns[$_GET['sort']][1]) {
  65. $this->_column = $_GET['sort'];
  66. }
  67. if (@$_GET['order'] == 'asc') {
  68. $this->_direction = 'asc';
  69. }
  70. $this->_pagination = (object)array(
  71. 'page' => (@(integer)$_GET['pg'] > 1 ? (integer)$_GET['pg'] : 1),
  72. 'length' => $this->_Parent->Configuration->get('pagination_maximum_rows', 'symphony')
  73. );
  74. $this->_logs = $this->_driver->getLogs(
  75. $this->_column,
  76. $this->_direction,
  77. $this->_pagination->page,
  78. $this->_pagination->length
  79. );
  80. // Calculate pagination:
  81. $this->_pagination->start = max(1, (($page - 1) * 17));
  82. $this->_pagination->end = (
  83. $this->_pagination->start == 1
  84. ? $this->_pagination->length
  85. : $start + count($this->_rules)
  86. );
  87. $this->_pagination->total = $this->_driver->countLogs();
  88. $this->_pagination->pages = ceil(
  89. $this->_pagination->total / $this->_pagination->length
  90. );
  91. }
  92. public function generateLink($values) {
  93. $values = array_merge(array(
  94. 'pg' => $this->_pagination->page,
  95. 'sort' => $this->_column,
  96. 'order' => $this->_direction
  97. ), $values);
  98. $count = 0;
  99. $link = $this->_Parent->getCurrentPageURL();
  100. foreach ($values as $key => $value) {
  101. if ($count++ == 0) {
  102. $link .= '?';
  103. } else {
  104. $link .= '&amp;';
  105. }
  106. $link .= "{$key}={$value}";
  107. }
  108. return $link;
  109. }
  110. public function __actionIndex() {
  111. $checked = @array_keys($_POST['items']);
  112. if (is_array($checked) and !empty($checked)) {
  113. switch ($_POST['with-selected']) {
  114. case 'delete':
  115. foreach ($checked as $log_id) {
  116. $this->_Parent->Database->query("
  117. DELETE FROM
  118. `tbl_redirectionmanager_logs`
  119. WHERE
  120. `id` = {$log_id}
  121. ");
  122. }
  123. redirect($this->_uri . '/logs/');
  124. break;
  125. }
  126. }
  127. }
  128. public function __viewIndex() {
  129. $this->setPageType('table');
  130. $this->setTitle('Symphony Redirection Manager &ndash; Logs');
  131. $this->appendSubheading('Redirection Logs');
  132. $tableHead = array();
  133. $tableBody = array();
  134. // Columns, with sorting:
  135. foreach ($this->_columns as $column => $values) {
  136. if ($values[1]) {
  137. if ($column == $this->_column) {
  138. if ($this->_direction == 'desc') {
  139. $direction = 'asc';
  140. $label = 'ascending';
  141. } else {
  142. $direction = 'desc';
  143. $label = 'descending';
  144. }
  145. } else {
  146. $direction = 'asc';
  147. $label = 'ascending';
  148. }
  149. $link = $this->generateLink(array(
  150. 'sort' => $column,
  151. 'order' => $direction
  152. ));
  153. $anchor = Widget::Anchor($values[0], $link, "Sort by {$label} " . strtolower($values[0]));
  154. if ($column == $this->_column) {
  155. $anchor->setAttribute('class', 'active');
  156. }
  157. $tableHead[] = array($anchor, 'col');
  158. } else {
  159. $tableHead[] = array($values[0], 'col');
  160. }
  161. }
  162. if (!is_array($this->_logs) or empty($this->_logs)) {
  163. $tableBody = array(
  164. Widget::TableRow(array(Widget::TableData(__('None Found.'), 'inactive', null, count($tableHead))))
  165. );
  166. } else {
  167. foreach ($this->_logs as $log) {
  168. $log = (object)$log;
  169. if (!empty($log->related_rule)) {
  170. $col_type = Widget::TableData(
  171. Widget::Anchor(
  172. $log->type,
  173. $this->_uri . "/rules/edit/".$log->related_rule
  174. )
  175. );
  176. }
  177. else if (!empty($log->request_uri)) {
  178. $value = $this->_driver->truncateValue($log->request_uri);
  179. $col_type = Widget::TableData(
  180. Widget::Anchor(
  181. $log->type,
  182. $this->_uri . "/rules/new/?source=".$log->request_uri
  183. )
  184. );
  185. }
  186. $col_type->appendChild(Widget::Input("items[{$log->id}]", null, 'checkbox'));
  187. if (!empty($log->request_uri)) {
  188. $col_request_uri = Widget::TableData(
  189. $log->request_uri
  190. );
  191. } else {
  192. $col_request_uri = Widget::TableData('None', 'inactive');
  193. }
  194. if (!empty($log->target_uri)) {
  195. $col_target_uri = Widget::TableData(
  196. $log->target_uri
  197. );
  198. } else {
  199. $col_target_uri = Widget::TableData('None', 'inactive');
  200. }
  201. if (!empty($log->hits)) {
  202. $col_hits = Widget::TableData($log->hits);
  203. } else {
  204. $col_hits = Widget::TableData('None', 'inactive');
  205. }
  206. $col_time = Widget::TableData(
  207. DateTimeObj::get(__SYM_DATETIME_FORMAT__, $log->request_time)
  208. );
  209. //$col_time->appendChild(Widget::Input("items[{$log->id}]", null, 'checkbox'));
  210. if (!empty($log->request_method)) {
  211. $col_method = Widget::TableData(ucwords($log->request_method));
  212. } else {
  213. $col_method = Widget::TableData('None', 'inactive');
  214. }
  215. if (!empty($log->remote_addr)) {
  216. $value = $this->_driver->truncateValue($log->remote_addr);
  217. $col_remote_addr = Widget::TableData($value);
  218. } else {
  219. $col_remote_addr = Widget::TableData('None', 'inactive');
  220. }
  221. $tableBody[] = Widget::TableRow(array(
  222. $col_type, $col_request_uri, $col_target_uri, $col_hits, $col_time, $col_method, $col_remote_addr
  223. ));
  224. }
  225. }
  226. $table = Widget::Table(
  227. Widget::TableHead($tableHead), null,
  228. Widget::TableBody($tableBody)
  229. );
  230. $table->setAttribute('class', 'selectable');
  231. $this->Form->appendChild($table);
  232. $actions = new XMLElement('div');
  233. $actions->setAttribute('class', 'actions');
  234. $options = array(
  235. array(null, false, 'With Selected...'),
  236. array('delete', false, 'Delete')
  237. );
  238. $actions->appendChild(Widget::Select('with-selected', $options));
  239. $actions->appendChild(Widget::Input('action[apply]', 'Apply', 'submit'));
  240. $this->Form->appendChild($actions);
  241. // Pagination:
  242. if ($this->_pagination->pages > 1) {
  243. $ul = new XMLElement('ul');
  244. $ul->setAttribute('class', 'page');
  245. ## First
  246. $li = new XMLElement('li');
  247. if ($this->_pagination->page > 1) {
  248. $li->appendChild(
  249. Widget::Anchor('First', $this->generateLink(array(
  250. 'pg' => 1
  251. )))
  252. );
  253. } else {
  254. $li->setValue('First');
  255. }
  256. $ul->appendChild($li);
  257. ## Previous
  258. $li = new XMLElement('li');
  259. if ($this->_pagination->page > 1) {
  260. $li->appendChild(
  261. Widget::Anchor('&larr; Previous', $this->generateLink(array(
  262. 'pg' => $this->_pagination->page - 1
  263. )))
  264. );
  265. } else {
  266. $li->setValue('&larr; Previous');
  267. }
  268. $ul->appendChild($li);
  269. ## Summary
  270. $li = new XMLElement('li', 'Page ' . $this->_pagination->page . ' of ' . max($this->_pagination->page, $this->_pagination->pages));
  271. $li->setAttribute('title', 'Viewing ' . $this->_pagination->start . ' - ' . $this->_pagination->end . ' of ' . $this->_pagination->total . ' entries');
  272. $ul->appendChild($li);
  273. ## Next
  274. $li = new XMLElement('li');
  275. if ($this->_pagination->page < $this->_pagination->pages) {
  276. $li->appendChild(
  277. Widget::Anchor('Next &rarr;', $this->generateLink(array(
  278. 'pg' => $this->_pagination->page + 1
  279. )))
  280. );
  281. } else {
  282. $li->setValue('Next &rarr;');
  283. }
  284. $ul->appendChild($li);
  285. ## Last
  286. $li = new XMLElement('li');
  287. if ($this->_pagination->page < $this->_pagination->pages) {
  288. $li->appendChild(
  289. Widget::Anchor('Last', $this->generateLink(array(
  290. 'pg' => $this->_pagination->pages
  291. )))
  292. );
  293. } else {
  294. $li->setValue('Last');
  295. }
  296. $ul->appendChild($li);
  297. $this->Form->appendChild($ul);
  298. }
  299. }
  300. }
  301. ?>