api.eventview.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * Logs viewing and searching basic class
  4. */
  5. class EventView {
  6. /**
  7. * System messages object placeholder
  8. *
  9. * @var object
  10. */
  11. protected $messages = '';
  12. /**
  13. * Default events limit to display
  14. *
  15. * @var int
  16. */
  17. protected $eventLimit = 100;
  18. /**
  19. * Contains current instance administrator login filter
  20. *
  21. * @var string
  22. */
  23. protected $filterAdmin = '';
  24. /**
  25. * Contains current instance event text filter
  26. *
  27. * @var string
  28. */
  29. protected $filterEventText = '';
  30. /**
  31. * Contains current instance date filter
  32. *
  33. * @var string
  34. */
  35. protected $filterDate = '';
  36. /**
  37. * weblogs table database abstraction layer
  38. *
  39. * @var object
  40. */
  41. protected $weblogsDb = '';
  42. /**
  43. * Contains available render limits presets
  44. *
  45. * @var array
  46. */
  47. protected $renderLimits = array();
  48. /**
  49. * Predefined tables,routes, URLs, etc...
  50. */
  51. const TABLE_DATASOURCE = 'weblogs';
  52. const URL_ME = '?module=eventview';
  53. const ROUTE_LIMIT = 'onpage';
  54. const ROUTE_ZEN = 'zenmode';
  55. const PROUTE_FILTERADMIN = 'eventadmin';
  56. const PROUTE_FILTEREVENTTEXT = 'eventsearch';
  57. const PROUTE_FILTERDATE = 'eventdate';
  58. const PROUTE_PROFILELINKS = 'profilelinks';
  59. // .-.
  60. // |_:_|
  61. // /(_Y_)\
  62. //. ( \/M\/ )
  63. // '. _.'-/'-'\-'._
  64. // ': _/.--'[[[[]'--.\_
  65. // ': /_' : |::"| : '.\
  66. // ': // ./ |oUU| \.' :\
  67. // ': _:'..' \_|___|_/ : :|
  68. // ':. .' |_[___]_| :.':\
  69. // [::\ | : | | : ; : \
  70. // '-' \/'.| |.' \ .;.' |
  71. // |\_ \ '-' : |
  72. // | \ \ .: : | |
  73. // | \ | '. : \ |
  74. // / \ :. .; |
  75. // / | | :__/ : \\
  76. // | | | \: | \ | ||
  77. // / \ : : |: / |__| /|
  78. // | : : :_/_| /'._\ '--|_\
  79. // /___.-/_|-' \ \
  80. // '-'
  81. /**
  82. * Creates new EventView instance
  83. */
  84. public function __construct() {
  85. $this->initMessages();
  86. $this->setRenderLimits();
  87. $this->setLimit();
  88. $this->setFilterDate();
  89. $this->setFilterAdmin();
  90. $this->setFilterEventText();
  91. $this->initDatabase();
  92. }
  93. /**
  94. * Inits system messages helper
  95. *
  96. * @return void
  97. */
  98. protected function initMessages() {
  99. $this->messages = new UbillingMessageHelper();
  100. }
  101. /**
  102. * Sets events render limit if required
  103. *
  104. * @return void
  105. */
  106. protected function setLimit() {
  107. $eventLimitRaw = ubRouting::get(self::ROUTE_LIMIT, 'int') ? ubRouting::get(self::ROUTE_LIMIT, 'int') : 100;
  108. //prevent memory overusage
  109. if (isset($this->renderLimits[$eventLimitRaw])) {
  110. $this->eventLimit = $eventLimitRaw;
  111. }
  112. }
  113. /**
  114. * Sets current instance administrator filter if required
  115. *
  116. * @return void
  117. */
  118. protected function setFilterAdmin() {
  119. $this->filterAdmin = ubRouting::post(self::PROUTE_FILTERADMIN, 'mres') ? ubRouting::post(self::PROUTE_FILTERADMIN, 'mres') : '';
  120. }
  121. /**
  122. * Sets current instance event text filter if required
  123. *
  124. * @return void
  125. */
  126. protected function setFilterEventText() {
  127. $this->filterEventText = ubRouting::post(self::PROUTE_FILTEREVENTTEXT, 'mres') ? ubRouting::post(self::PROUTE_FILTEREVENTTEXT, 'mres') : '';
  128. }
  129. /**
  130. * Sets current instance date filter if required
  131. *
  132. * @return void
  133. */
  134. protected function setFilterDate() {
  135. $rawDate = ubRouting::post(self::PROUTE_FILTERDATE, 'mres') ? ubRouting::post(self::PROUTE_FILTERDATE, 'mres') : '';
  136. if (!empty($rawDate)) {
  137. if (zb_checkDate($rawDate)) {
  138. $this->filterDate = $rawDate;
  139. }
  140. }
  141. }
  142. /**
  143. * Sets possible render limits values
  144. *
  145. * @return void
  146. */
  147. protected function setRenderLimits() {
  148. $this->renderLimits = array(
  149. 50 => 50,
  150. 100 => 100,
  151. 200 => 200,
  152. 500 => 500,
  153. 800 => 800,
  154. 1000 => 1000);
  155. }
  156. /**
  157. * Inits weblogs database abstraction layer
  158. *
  159. * @return void
  160. */
  161. protected function initDatabase() {
  162. $this->weblogsDb = new NyanORM(self::TABLE_DATASOURCE);
  163. }
  164. /**
  165. * Renders available event limits switching controls
  166. *
  167. * @return string
  168. */
  169. protected function renderEventLimits() {
  170. $result = '';
  171. if (!empty($this->renderLimits)) {
  172. $result .= __('On page') . ': ';
  173. foreach ($this->renderLimits as $io => $each) {
  174. $hs = '';
  175. $he = '';
  176. if ($each == $this->eventLimit) {
  177. $hs = wf_tag('b');
  178. $he = wf_tag('b', true);
  179. }
  180. $result .= $hs . wf_Link(self::URL_ME . '&' . self::ROUTE_LIMIT . '=' . $each, $each, false) . $he . ' ';
  181. }
  182. }
  183. return($result);
  184. }
  185. /**
  186. * Preloads all events from database, applying all of required filters
  187. *
  188. * @return array
  189. */
  190. protected function getAllEventsFiltered() {
  191. $result = array();
  192. $this->weblogsDb->orderBy('id', 'DESC'); //from newest to oldest
  193. //
  194. //date filters ignores default render limits
  195. if (!empty($this->filterDate)) {
  196. $this->eventLimit = 0; //show all of events by selected date
  197. $this->weblogsDb->where('date', 'LIKE', $this->filterDate . '%');
  198. }
  199. //apply administrator filter
  200. if (!empty($this->filterAdmin)) {
  201. $this->weblogsDb->where('admin', '=', $this->filterAdmin);
  202. }
  203. //apply event-text filter
  204. if (!empty($this->filterEventText)) {
  205. $this->weblogsDb->where('event', 'LIKE', '%' . $this->filterEventText . '%');
  206. }
  207. //setting query limits
  208. if (!empty($this->eventLimit)) {
  209. $this->weblogsDb->limit($this->eventLimit);
  210. }
  211. //getting events from database
  212. $result = $this->weblogsDb->getAll();
  213. return($result);
  214. }
  215. /**
  216. * Returns selector of administrator logins
  217. *
  218. * @return string
  219. */
  220. protected function adminSelector() {
  221. $all = rcms_scandir(USERS_PATH);
  222. $allLogins = array('' => '-');
  223. if (!empty($all)) {
  224. foreach ($all as $each) {
  225. $allLogins[$each] = $each;
  226. }
  227. }
  228. $allLogins['external'] = 'external';
  229. $allLogins['guest'] = 'guest';
  230. $result = wf_Selector(self::PROUTE_FILTERADMIN, $allLogins, __('Administrator'), $this->filterAdmin, false);
  231. return ($result);
  232. }
  233. /**
  234. * Renders form for setting event filters
  235. *
  236. * @return string
  237. */
  238. protected function renderSearchForm() {
  239. $result = '';
  240. $inputs = __('By date') . ': ';
  241. $inputs .= wf_DatePickerPreset(self::PROUTE_FILTERDATE, $this->filterDate, true) . ' '; //date filter
  242. $inputs .= $this->adminSelector() . ' '; //administrator filter
  243. $inputs .= wf_TextInput(self::PROUTE_FILTEREVENTTEXT, __('Event'), $this->filterEventText, false, 30) . ' '; //event text mask
  244. $inputs .= wf_Submit(__('Search'));
  245. $result = wf_Form('', 'POST', $inputs, 'glamour');
  246. return($result);
  247. }
  248. /**
  249. * Renders weblogs search results
  250. *
  251. * @return string
  252. */
  253. public function renderEventsReport() {
  254. $result = '';
  255. $zenMode = ubRouting::checkGet(self::ROUTE_ZEN) ? true : false;
  256. if (!$zenMode) {
  257. $result .= $this->renderEventLimits();
  258. $result .= wf_delimiter(0);
  259. $result .= $this->renderSearchForm();
  260. } else {
  261. $this->eventLimit = 50;
  262. }
  263. $allEvents = $this->getAllEventsFiltered();
  264. if (!empty($allEvents)) {
  265. $tablecells = wf_TableCell(__('ID'));
  266. $tablecells .= wf_TableCell(__('Date'));
  267. $tablecells .= wf_TableCell(__('Admin'));
  268. $tablecells .= wf_TableCell(__('IP'));
  269. $tablecells .= wf_TableCell(__('Event'));
  270. $tablerows = wf_TableRow($tablecells, 'row1');
  271. foreach ($allEvents as $io => $eachevent) {
  272. $event = htmlspecialchars($eachevent['event']);
  273. $tablecells = wf_TableCell($eachevent['id']);
  274. $tablecells .= wf_TableCell($eachevent['date']);
  275. $tablecells .= wf_TableCell($eachevent['admin']);
  276. $tablecells .= wf_TableCell($eachevent['ip']);
  277. $tablecells .= wf_TableCell($event);
  278. $tablerows .= wf_TableRow($tablecells, 'row5');
  279. }
  280. $result .= wf_TableBody($tablerows, '100%', 0, 'sortable resp-table');
  281. } else {
  282. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'info');
  283. }
  284. return($result);
  285. }
  286. /**
  287. * Renders module controls panel
  288. *
  289. * @return string
  290. */
  291. public function renderControls() {
  292. $result = '';
  293. $result .= wf_Link(self::URL_ME, wf_img('skins/log_icon_small.png', __('Events')) . ' ' . __('Events'), false, 'ubButton') . ' ';
  294. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_ZEN . '=true', wf_img('skins/zen.png', __('Zen')) . ' ' . __('Zen'), false, 'ubButton') . ' ';
  295. return($result);
  296. }
  297. }