PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/upload/awards.php

https://github.com/Smokki/PsychoStats-extended
PHP | 299 lines | 205 code | 37 blank | 57 comment | 41 complexity | eddba6f926864a67928a14fcf2fe0ce0 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PsychoStats.
  4. *
  5. * Written by Jason Morriss <stormtrooper@psychostats.com>
  6. * Copyright 2008 Jason Morriss
  7. *
  8. * PsychoStats is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * PsychoStats is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with PsychoStats. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Version $Id: awards.php 495 2008-06-18 18:41:37Z lifo $
  22. */
  23. define("PSYCHOSTATS_PAGE", true);
  24. include(dirname(__FILE__) . "/includes/common.php");
  25. include(PS_ROOTDIR . "/includes/class_calendar.php");
  26. $cms->init_theme($ps->conf['main']['theme'], $ps->conf['theme']);
  27. $ps->theme_setup($cms->theme);
  28. $cms->theme->page_title('PsychoStats - Player Awards');
  29. // collect url parameters ...
  30. $validfields = array('v','d','time','p');
  31. $cms->theme->assign_request_vars($validfields, true);
  32. // v = what view to display awards as (day,week,month);
  33. // d = date to view awards for (with the specified view)
  34. // p = limit awards matching this plrid
  35. if (!in_array($v, array('day','week','month'))) {
  36. if ($ps->conf['main']['awards']['daily']) {
  37. $v = 'day';
  38. } elseif ($ps->conf['main']['awards']['weekly']) {
  39. $v = 'week';
  40. } else {
  41. $v = 'month';
  42. }
  43. }
  44. if (!is_numeric($p)) $p = '';
  45. $_p = $ps->db->escape($p, true);
  46. $views = array(
  47. 'day' => $cms->trans("Day"),
  48. 'week' => $cms->trans("Week"),
  49. 'month' => $cms->trans("Month")
  50. );
  51. // get the min/max ranges for each award range (view).
  52. $range = array();
  53. $cmd = "SELECT awardrange,MAX(awarddate),MIN(awarddate) FROM $ps->t_awards ";
  54. if ($p) $cmd .= " WHERE topplrid=$_p ";
  55. $cmd .= "GROUP BY awardrange";
  56. $list = $ps->db->fetch_rows(0, $cmd);
  57. foreach ($list as $a) {
  58. $range[$a[0]]['max'] = $a[1];
  59. $range[$a[0]]['min'] = $a[2];
  60. }
  61. unset($list);
  62. // no date or the string is invalid?
  63. if (empty($d) or !preg_match('/\d\d\d\d-\d\d?-\d\d?/', $d)) {
  64. $d = date('Y-m-d');
  65. // $d = $range[$v]['max'];
  66. }
  67. // determine if this date exists
  68. /*
  69. list($valid_date) = $ps->db->fetch_list(sprintf("SELECT 1 FROM $ps->t_awards WHERE awarddate=%s AND awardrange=%s LIMIT 1",
  70. $ps->db->escape($d, true),
  71. $ps->db->escape($v, true)
  72. ));
  73. // if the selected date is not in the database then default to the newest date for the current view
  74. if (!$valid_date) {
  75. $d = $range[$v]['max'];
  76. }
  77. */
  78. // either the selected date or the next oldest date will be returned
  79. $cmd = "SELECT awarddate FROM $ps->t_awards WHERE awardrange = '$v' AND awarddate <= '$d' ";
  80. if ($p) $cmd .= "AND topplrid=$_p ";
  81. $cmd .= "ORDER BY awarddate DESC LIMIT 1";
  82. list($d) = $ps->db->fetch_list($cmd);
  83. // if date is still empty then we have no awards in the database (at least not for the selected view)
  84. if (empty($d)) {
  85. $cms->full_page_err('awards', array(
  86. 'message_title' => $cms->trans("No Awards Found"),
  87. 'message' => $cms->trans("There are currently no awards in the database to display.")
  88. ));
  89. exit();
  90. }
  91. $andplr = $p ? " AND topplrid=$_p" : "";
  92. // select them separately, since it's possible a date will not exist $nX should be empty in that case.
  93. list($p1) = $ps->db->fetch_list("SELECT awarddate FROM $ps->t_awards WHERE awardrange = 'day' AND awarddate < '$d' $andplr ORDER BY awarddate DESC LIMIT 1");
  94. list($p2) = $ps->db->fetch_list("SELECT awarddate FROM $ps->t_awards WHERE awardrange = 'week' AND awarddate < '$d' $andplr ORDER BY awarddate DESC LIMIT 1");
  95. list($p3) = $ps->db->fetch_list("SELECT awarddate FROM $ps->t_awards WHERE awardrange = 'month' AND awarddate < '$d' $andplr ORDER BY awarddate DESC LIMIT 1");
  96. list($n1) = $ps->db->fetch_list("SELECT awarddate FROM $ps->t_awards WHERE awardrange = 'day' AND awarddate > '$d' $andplr LIMIT 1");
  97. list($n2) = $ps->db->fetch_list("SELECT awarddate FROM $ps->t_awards WHERE awardrange = 'week' AND awarddate > '$d' $andplr LIMIT 1");
  98. list($n3) = $ps->db->fetch_list("SELECT awarddate FROM $ps->t_awards WHERE awardrange = 'month' AND awarddate > '$d' $andplr LIMIT 1");
  99. $prev = array( 'day' => $p1, 'week' => $p2, 'month' => $p3 );
  100. $next = array( 'day' => $n1, 'week' => $n2, 'month' => $n3 );
  101. // create column information based on view
  102. if ($v == 'month') {
  103. } elseif ($v == 'week') {
  104. } else { // day
  105. }
  106. // create a calendar
  107. $cal = new Calendar($d);
  108. $cal->startofweek( $ps->conf['main']['awards']['startofweek'] == 'monday' ? 1 : 0 ); // 0=sun, 1=mon
  109. $cal->set_conf(array(
  110. 'show_timeurl' => false,
  111. ));
  112. $first = $cal->first_date();
  113. $last = $cal->last_date();
  114. // populate the calendar with days that have awards
  115. $list = $ps->db->fetch_rows(1, "SELECT awarddate,awardrange FROM $ps->t_awards WHERE awardrange <> 'week' AND (awarddate BETWEEN '$first' AND '$last') $andplr GROUP BY awarddate");
  116. foreach ($list as $day) {
  117. $cal->day($day['awarddate'], array('link' => ps_url_wrapper(array( 'v' => $day['awardrange'], 'd' => $day['awarddate'], 'p' => $p ))));
  118. }
  119. $list = $ps->db->fetch_rows(1, "SELECT awarddate FROM $ps->t_awards WHERE awardrange='week' AND (awarddate BETWEEN '$first' AND '$last') $andplr GROUP BY awarddate");
  120. foreach ($list as $week) {
  121. $cal->week($week['awarddate'], array('link' => ps_url_wrapper(array( 'v' => 'week', 'd' => $week['awarddate'], 'p' => $p ))));
  122. }
  123. // select the week on the calendar so it highlights the week instead of the 1st day
  124. $cal->selected($v);
  125. $cms->theme->assign('calendar', $cal->draw());
  126. // load the awards for the date specified (for all players; not just the selected one if $p is selected)
  127. $list = $ps->db->fetch_rows(1,
  128. "SELECT a.*,ac.phrase,ac.negative,ac.format,ac.rankedonly,ac.description,p.*,pp.* ".
  129. "FROM $ps->t_awards a, $ps->t_config_awards ac, $ps->t_plr p, $ps->t_plr_profile pp " .
  130. "WHERE ac.id=a.awardid AND p.plrid=a.topplrid AND pp.uniqueid=p.uniqueid AND awardrange='$v' AND awarddate='$d' " .
  131. "ORDER BY idx,awardtype,awardname"
  132. );
  133. $awards = array();
  134. foreach ($list as $a) {
  135. if ($a['interpolate']) {
  136. $ary = unserialize($a['interpolate']);
  137. if (is_array($ary)) {
  138. $a = array_merge($a, $ary);
  139. }
  140. unset($ary);
  141. }
  142. $awards[ $a['awardtype'] ][] = $a;
  143. // if ($a['awardtype'] == 'player') {
  144. // $awards[ $a['awardtype'] ][] = $a;
  145. // } else {
  146. // // separate weapon awards from weapon class awards...
  147. //// $key = trim(str_replace('weapon class', '', $a['awardweapon']));
  148. // $awards[ $a['awardtype'] ][] = $a;
  149. // }
  150. }
  151. //print_r($awards);
  152. // assign variables to the theme
  153. $cms->theme->assign(array(
  154. 'page' => basename(__FILE__,'.php'),
  155. 'view_str' => $views[$v],
  156. 'view' => $v,
  157. 'date' => $d,
  158. 'next' => $next,
  159. 'next_str' => next_str($next),
  160. 'prev' => $prev,
  161. 'prev_str' => prev_str($prev),
  162. 'awards_for_str'=> curr_str($d,$v),
  163. 'awards' => $awards,
  164. 'plrid' => $p
  165. ));
  166. // display the output
  167. $basename = basename(__FILE__, '.php');
  168. $cms->theme->add_css('css/2column.css'); // this page has a left column
  169. $cms->theme->add_css('css/calendar.css');
  170. $cms->theme->add_js('js/calendar.js');
  171. $cms->full_page($basename, $basename, $basename.'_header', $basename.'_footer');
  172. function curr_str($d, $v) {
  173. global $cms, $p;
  174. $str = "";
  175. if ($v == 'day') {
  176. $str = $cms->trans("Daily awards for");
  177. list($y1,$m1,$d1) = explode('-', date("Y-m-d"));
  178. list($y2,$m2,$d2) = explode('-', $d);
  179. if ("$y2$m2" == "$y1$m1" and $d2 == $d1) {
  180. $str .= " " . $cms->trans("Today");
  181. } else {
  182. $str .= date(" M j Y", ymd2time($d));
  183. }
  184. } elseif ($v == 'week') {
  185. $first = ymd2time($d);
  186. $second = ymd2time($d) + 60*60*24*6;
  187. $str = $cms->trans("Weekly awards for week") . date(" W, M", $first);
  188. if (date("m",$first) == date("m",$second)) { // is it the same month?
  189. $str .= " " . date("j", $first) . "-" . date("j Y", $second);
  190. } else {
  191. $str .= " " . date(" d Y", $first) . " - " . date("M d Y", ymd2time($d)+60*60*24*6);
  192. }
  193. } else {
  194. $str = $cms->trans("Monthly awards for") . date(" F Y", ymd2time($d));
  195. }
  196. return $str;
  197. }
  198. function next_str($next) {
  199. global $cms, $v, $p;
  200. $str = "";
  201. if ($next['day']) {
  202. list($y1,$m1,$d1) = explode('-', date("Y-m-d"));
  203. list($y2,$m2,$d2) = explode('-', $next['day']);
  204. if ("$y2$m2" == "$y1$m1" and $d2+0 == $d1+1) {
  205. $str .= sprintf("<a href='%s' class='next'>%s</a>",
  206. ps_url_wrapper(array( 'v' => 'day', 'd' => $next['day'], 'p' => $p )),
  207. $cms->trans("Today")
  208. );
  209. } else {
  210. $str .= sprintf("<a href='%s' class='next'>%s</a>",
  211. ps_url_wrapper(array( 'v' => 'day', 'd' => $next['day'], 'p' => $p )),
  212. date("M d", ymd2time($next['day']))
  213. );
  214. }
  215. }
  216. if ($next['week']) {
  217. $str .= sprintf("<a href='%s' class='next'>%s</a>",
  218. ps_url_wrapper(array( 'v' => 'week', 'd' => $next['week'], 'p' => $p )),
  219. $cms->trans("Week") . date(" W; M d", ymd2time($next['week']))
  220. );
  221. }
  222. if ($next['month']) {
  223. $str .= sprintf("<a href='%s' class='next'><b>%s</b></a>",
  224. ps_url_wrapper(array( 'v' => 'month', 'd' => $next['month'], 'p' => $p )),
  225. date("M", ymd2time($next['month']))
  226. );
  227. }
  228. return "<div class='next'>$str</div>";
  229. }
  230. function prev_str($prev) {
  231. global $cms, $v, $p;
  232. $str = "";
  233. if ($prev['month']) {
  234. $str .= sprintf("<a href='%s' class='prev'>%s</a>",
  235. ps_url_wrapper(array( 'v' => 'month', 'd' => $prev['month'], 'p' => $p )),
  236. date("M", ymd2time($prev['month']))
  237. );
  238. }
  239. if ($prev['week']) {
  240. $str .= sprintf("<a href='%s' class='prev'>%s</a>",
  241. ps_url_wrapper(array( 'v' => 'week', 'd' => $prev['week'], 'p' => $p )),
  242. $cms->trans("Week") . date(" W; M d", ymd2time($prev['week']))
  243. );
  244. }
  245. if ($prev['day']) {
  246. list($y1,$m1,$d1) = explode('-', date("Y-m-d"));
  247. list($y2,$m2,$d2) = explode('-', $prev['day']);
  248. if ("$y2$m2" == "$y1$m1" and $d2+0 == $d1-1) {
  249. $str .= sprintf("<a href='%s' class='prev'>%s</a>",
  250. ps_url_wrapper(array( 'v' => 'day', 'd' => $prev['day'], 'p' => $p )),
  251. $cms->trans("Yesterday")
  252. );
  253. } else {
  254. $str .= sprintf("<a href='%s' class='prev'>%s</a>",
  255. ps_url_wrapper(array( 'v' => 'day', 'd' => $prev['day'], 'p' => $p )),
  256. date("M d", ymd2time($prev['day']))
  257. );
  258. }
  259. }
  260. return "<div class='prev'>$str</div>";
  261. }
  262. ?>