PageRenderTime 47ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/common/includes/toplist/class.base.php

https://code.google.com/
PHP | 495 lines | 351 code | 54 blank | 90 comment | 89 complexity | 4063c6ad67f7a6b0380329a975c3a5cd MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package EDK
  4. */
  5. // Create a box to display the top pilots at something. Subclasses of TopList
  6. // define the something.
  7. class TopList_Base
  8. {
  9. protected $exc_vic_scl = array();
  10. protected $inc_vic_scl = array();
  11. protected $exc_vic_shp = array();
  12. protected $inc_vic_shp = array();
  13. protected $inv_all = array();
  14. protected $inv_crp = array();
  15. protected $inv_plt = array();
  16. protected $vic_all = array();
  17. protected $vic_crp = array();
  18. protected $vic_plt = array();
  19. protected $mixedvictims = false;
  20. protected $mixedinvolved = false;
  21. protected $regions_ = array();
  22. protected $systems_ = array();
  23. protected $qry = null;
  24. protected $weekno_ = 0;
  25. protected $yearno_ = 0;
  26. protected $monthno_ = 0;
  27. protected $startweekno_ = 0;
  28. protected $startDate_ = 0;
  29. protected $endDate_ = 0;
  30. protected $limit = 10;
  31. protected $finalStartDate = null;
  32. protected $finalEndDate = null;
  33. /**
  34. * Set the maximum number of results to show in the TopList.
  35. *
  36. * @param integer $limit Maximum number of kills to show.
  37. *
  38. * @return integer value TopList was set to.
  39. */
  40. function setLimit($limit = 10)
  41. {
  42. $this->limit = intval($limit);
  43. return $this->limit;
  44. }
  45. /**
  46. * Include or exclude pods/noob ships/shuttles.
  47. *
  48. * @param boolean $flag true to show P/N/S, false to remove.
  49. */
  50. function setPodsNoobShips($flag)
  51. {
  52. if (!$flag)
  53. {
  54. $this->excludeVictimShipClass(2);
  55. $this->excludeVictimShipClass(3);
  56. $this->excludeVictimShipClass(11);
  57. }
  58. }
  59. /**
  60. * Remove structures.
  61. *
  62. * Note that these class types are hard coded so will need modification
  63. * if the classes are changed in future.
  64. */
  65. function setNoStructures()
  66. {
  67. $this->excludeVictimShipClass(35);
  68. $this->excludeVictimShipClass(36);
  69. $this->excludeVictimShipClass(37);
  70. $this->excludeVictimShipClass(38);
  71. $this->excludeVictimShipClass(41);
  72. $this->excludeVictimShipClass(42);
  73. }
  74. function setSQLTop($sql)
  75. {
  76. $this->sqltop_ = $sql;
  77. }
  78. function setSQLBottom($sql)
  79. {
  80. $this->sqlbottom_ = $sql;
  81. }
  82. function addInvolvedPilot($pilot)
  83. {
  84. involved::add($this->inv_plt,$pilot);
  85. }
  86. function addInvolvedCorp($corp)
  87. {
  88. involved::add($this->inv_crp,$corp);
  89. }
  90. function addInvolvedAlliance($alliance)
  91. {
  92. involved::add($this->inv_all,$alliance);
  93. }
  94. function addVictimPilot($pilot)
  95. {
  96. involved::add($this->vic_plt,$pilot);
  97. }
  98. function addVictimCorp($corp)
  99. {
  100. involved::add($this->vic_crp,$corp);
  101. }
  102. function addVictimAlliance($alliance)
  103. {
  104. involved::add($this->vic_all,$alliance);
  105. }
  106. /**
  107. * Set a victim ship class to include.
  108. *
  109. * If this is set then only ship classes set will be in the output.
  110. *
  111. * @param integer $shipclass ID of a ship class.
  112. */
  113. function addVictimShipClass($shipclass)
  114. {
  115. if(!is_numeric($shipclass)) $scl_id = $shipclass->getID();
  116. else $scl_id = intval($shipclass);
  117. $this->inc_vic_scl[$scl_id] = $scl_id;
  118. unset ($this->exc_vic_scl[$scl_id]);
  119. }
  120. /**
  121. * Set a victim ship class to exclude.
  122. *
  123. * If this is set then only ship classes not set will be in the output.
  124. *
  125. * @param integer $shipclass ID of a ship class
  126. */
  127. function excludeVictimShipClass($shipclass)
  128. {
  129. if(!is_numeric($shipclass)) $scl_id = $shipclass->getID();
  130. else $scl_id = intval($shipclass);
  131. $this->exc_vic_scl[$scl_id] = $scl_id;
  132. unset ($this->inc_vic_scl[$scl_id]);
  133. }
  134. /**
  135. * Set a victim ship type to include.
  136. *
  137. * If this is set then only ship types set will be in the output.
  138. *
  139. * @param integer $ship ID of a shiptype
  140. */
  141. function addVictimShip($ship)
  142. {
  143. $ship = intval($ship);
  144. $this->inc_vic_shp[$ship] = $ship;
  145. unset ($this->exc_vic_shp[$ship]);
  146. }
  147. /**
  148. * Set a victim ship type to exclude.
  149. *
  150. * If this is set then only ship types not set will be in the output.
  151. *
  152. * @param integer $ship ID of a shiptype
  153. */
  154. function excludeVictimShip($ship)
  155. {
  156. $ship = intval($ship);
  157. $this->exc_vic_shp[$ship] = $ship;
  158. unset ($this->inc_vic_shp[$ship]);
  159. }
  160. /**
  161. * @param integer|Region $region
  162. */
  163. function addRegion($region)
  164. {
  165. if(is_numeric($region)) array_push($this->regions_, $region);
  166. else array_push($this->regions_, $region->getID());
  167. }
  168. /**
  169. * @param integer|SolarSystem $system
  170. */
  171. function addSystem($system)
  172. {
  173. if(is_numeric($system)) array_push($this->systems_, $system);
  174. else array_push($this->systems_, $system->getID());
  175. }
  176. function addGroupBy($groupby)
  177. {
  178. array_push($this->groupby_, $groupby);
  179. }
  180. /**
  181. * @param PageSplitter $pagesplitter
  182. */
  183. function setPageSplitter($pagesplitter)
  184. {
  185. if (isset($_GET['page'])) $page = $_GET['page'];
  186. else $page = 1;
  187. $this->plimit_ = $pagesplitter->getSplit();
  188. $this->poffset_ = ($page * $this->plimit_) - $this->plimit_;
  189. }
  190. /**
  191. * @param integer $weekno
  192. */
  193. function setWeek($weekno)
  194. {
  195. $weekno=intval($weekno);
  196. if($weekno <1) $this->weekno_ = 1;
  197. if($weekno >53) $this->weekno_ = 53;
  198. else $this->weekno_ = $weekno;
  199. }
  200. /**
  201. * @param integer $monthno
  202. */
  203. function setMonth($monthno)
  204. {
  205. $monthno = intval($monthno);
  206. if($monthno < 1) $this->monthno_ = 1;
  207. if($monthno > 12) $this->monthno_ = 12;
  208. else $this->monthno_ = $monthno;
  209. }
  210. /**
  211. * @param integer $yearno
  212. */
  213. function setYear($yearno)
  214. {
  215. // 1970-2038 is the allowable range for the timestamp code used
  216. // Needs to be revisited in the next 30 years
  217. $yearno = intval($yearno);
  218. if($yearno < 1970) $this->yearno_ = 1970;
  219. if($yearno > 2038) $this->yearno_ = 2038;
  220. else $this->yearno_ = $yearno;
  221. }
  222. /**
  223. * @param integer $weekno
  224. */
  225. function setStartWeek($weekno)
  226. {
  227. $weekno=intval($weekno);
  228. if($weekno <1) $this->startweekno_ = 1;
  229. if($weekno >53) $this->startweekno_ = 53;
  230. else $this->startweekno_ = $weekno;
  231. }
  232. /**
  233. * @param string $timestamp
  234. */
  235. function setStartDate($timestamp)
  236. {
  237. // Check timestamp is valid before adding
  238. if(strtotime($timestamp)) $this->startDate_ = $timestamp;
  239. }
  240. /**
  241. * @param string $timestamp
  242. */
  243. function setEndDate($timestamp)
  244. {
  245. // Check timestamp is valid before adding
  246. if(strtotime($timestamp)) $this->endDate_ = $timestamp;
  247. }
  248. // Convert given date ranges to SQL date range.
  249. function getDateFilter($field = "kll.kll_timestamp")
  250. {
  251. $sql = "";
  252. if(is_null($this->finalStartDate))
  253. {
  254. $this->finalStartDate = makeStartDate($this->weekno_, $this->yearno_, $this->monthno_, $this->startweekno_, $this->startDate_);
  255. $this->finalEndDate = makeEndDate($this->weekno_, $this->yearno_, $this->monthno_, $this->endDate_);
  256. }
  257. if($this->finalStartDate || $this->finalEndDate)
  258. {
  259. if($this->finalStartDate) $sql .= " $field >= '".gmdate('Y-m-d H:i',$this->finalStartDate)."' ";
  260. if($this->finalStartDate && $this->finalEndDate) $sql .= " AND ";
  261. if($this->finalEndDate) $sql .= " $field <= '".gmdate('Y-m-d H:i',$this->finalEndDate)."' ";
  262. }
  263. return $sql;
  264. }
  265. function setGroupBy($groupby)
  266. {
  267. $this->groupby_ = $groupby;
  268. }
  269. function execQuery()
  270. {
  271. if ($this->inv_plt && $this->inv_crp || $this->inv_plt && $this->inv_all
  272. || $this->inv_crp && $this->inv_all) $this->mixedinvolved = true;
  273. if ($this->vic_plt && $this->vic_crp || $this->vic_plt && $this->vic_all
  274. || $this->vic_crp && $this->vic_all) $this->mixedvictims = true;
  275. $this->sql_ .= $this->sqltop_;
  276. // involved
  277. if(!$this->mixedinvolved)
  278. {
  279. if ($this->inv_crp)
  280. {
  281. $this->sql_ .= "\n\tINNER JOIN(SELECT inc_kll_id as kll_id
  282. FROM kb3_inv_crp
  283. WHERE ";
  284. if($this->getDateFilter())
  285. $this->sql_ .= $this->getDateFilter("inc_timestamp")." AND ";
  286. $this->sql_ .= "inc_crp_id IN (".implode(",", $this->inv_crp).")".
  287. " GROUP BY kll_id) inv ON (inv.kll_id = ind.ind_kll_id)";
  288. }
  289. else if ($this->inv_all)
  290. {
  291. $this->sql_ .= "\n\tINNER JOIN (SELECT ina_kll_id as kll_id
  292. FROM kb3_inv_all
  293. WHERE ";
  294. if($this->getDateFilter())
  295. $this->sql_ .= $this->getDateFilter("ina_timestamp")." AND ";
  296. $this->sql_ .= "ina_all_id IN (".implode(",", $this->inv_all).")".
  297. " GROUP BY kll_id) inv ON (inv.kll_id = ind.ind_kll_id)";
  298. }
  299. }
  300. else
  301. {
  302. $mixedinv = array();
  303. // All this to avoid plt OR crp OR all over kb3_inv_details
  304. // which makes mysql unhappy - sometimes
  305. if ($this->inv_plt)
  306. {
  307. $misql = "SELECT ind_kll_id as kll_id
  308. FROM kb3_inv_detail
  309. WHERE ";
  310. if($this->getDateFilter())
  311. $misql .= $this->getDateFilter("ind_timestamp")." AND ";
  312. $misql .= "ind_plt_id IN (".implode(",", $this->inv_plt).")";
  313. $mixedinv[] = $misql;
  314. }
  315. if ($this->inv_crp)
  316. {
  317. $misql = "SELECT inc_kll_id as kll_id
  318. FROM kb3_inv_crp
  319. WHERE ";
  320. if($this->getDateFilter())
  321. $misql .= $this->getDateFilter("inc_timestamp")." AND ";
  322. $misql .= "inc_crp_id IN (".implode(",", $this->inv_crp).")";
  323. $mixedinv[] = $misql;
  324. }
  325. if ($this->inv_all)
  326. {
  327. $misql = "SELECT ina_kll_id as kll_id
  328. FROM kb3_inv_all
  329. WHERE ";
  330. if($this->getDateFilter())
  331. $misql .= $this->getDateFilter("ina_timestamp")." AND ";
  332. $misql .= "ina_all_id IN (".implode(",", $this->inv_all).")";
  333. $mixedinv[] = $misql;
  334. }
  335. $this->sql_ .= "\n\tINNER JOIN (".implode("\nUNION\n", $mixedinv).") inv ON (inv.kll_id = ind.ind_kll_id)";
  336. }
  337. if (count($this->inc_vic_scl) || count($this->exc_vic_scl))
  338. {
  339. $this->sql_ .= "\n\tINNER JOIN kb3_ships shp
  340. ON ( shp.shp_id = kll.kll_ship_id )";
  341. }
  342. if (count($this->regions_))
  343. {
  344. $this->sql_ .= "\n\tINNER JOIN kb3_systems sys
  345. on ( sys.sys_id = kll.kll_system_id )
  346. INNER JOIN kb3_constellations con
  347. on ( con.con_id = sys.sys_con_id and
  348. con.con_reg_id in ( ".implode($this->regions_, ",")." ) )";
  349. }
  350. $op = "\nWHERE ";
  351. // victim filter
  352. if ($this->vic_plt || $this->vic_crp || $this->vic_all)
  353. {
  354. $vicP = array();
  355. if ($this->vic_plt)
  356. $vicP[] = "kll.kll_victim_id IN ( ".implode(",", $this->vic_plt)." )";
  357. if ($this->vic_crp)
  358. $vicP[] = "kll.kll_crp_id IN ( ".implode(",", $this->vic_crp)." )";
  359. if ($this->vic_all)
  360. $vicP[] = "kll.kll_all_id IN ( ".implode(",", $this->vic_all)." )";
  361. $this->sql_ .= $op."( ".implode(" OR ", $vicP).")";
  362. $op = " AND ";
  363. }
  364. if (count($this->exc_vic_scl))
  365. {
  366. $this->sql_ .= $op." shp.shp_class not IN ( ".implode(",", $this->exc_vic_scl)." ) ";
  367. $op = " AND ";
  368. }
  369. if (count($this->inc_vic_scl))
  370. {
  371. $this->sql_ .= $op." shp.shp_class IN ( ".implode(",", $this->inc_vic_scl)." ) ";
  372. $op = " AND ";
  373. }
  374. if (count($this->exc_vic_shp))
  375. {
  376. $this->sql_ .= $op." kll.kll_ship_id not IN ( ".implode(",", $this->exc_vic_shp)." ) ";
  377. $op = " AND ";
  378. }
  379. if (count($this->inc_vic_shp))
  380. {
  381. $this->sql_ .= $op." kll.kll_ship_id IN ( ".implode(",", $this->inc_vic_shp)." ) ";
  382. $op = " AND ";
  383. }
  384. $invP = array();
  385. if ($this->inv_plt)
  386. $invP[] = "ind.ind_plt_id IN (".implode(",", $this->inv_plt).")";
  387. if ($this->inv_crp)
  388. $invP[] = "ind.ind_crp_id IN (".implode(",", $this->inv_crp).")";
  389. if ($this->inv_all)
  390. $invP[] = "ind.ind_all_id IN (".implode(",", $this->inv_all).")";
  391. if($invP)
  392. {
  393. $this->sql_ .= $op." ( ".implode(' OR ', $invP)." ) ";
  394. $op = " AND ";
  395. }
  396. if (count($this->systems_))
  397. {
  398. $this->sql_ .= $op." kll.kll_system_id IN ( ".implode($this->systems_, ",").") ";
  399. $op = " AND ";
  400. }
  401. // Add dates
  402. if ($this->vic_plt || $this->vic_crp || $this->vic_all
  403. || !($this->inv_plt || $this->inv_crp || $this->inv_all))
  404. {
  405. if($this->getDateFilter())
  406. {
  407. $this->sql_ .= $op.$this->getDateFilter();
  408. $op = " AND ";
  409. }
  410. }
  411. if($this->getDateFilter())
  412. {
  413. $filter = "";
  414. if($this->mixedinvolved) $filter = "ind.ind_timestamp";
  415. else if($this->inv_plt) $filter = "ind.ind_timestamp";
  416. if($filter)
  417. {
  418. $this->sql_ .= $op.$this->getDateFilter($filter)." ";
  419. $op = " AND ";
  420. }
  421. }
  422. // This is a little ugly but is needed since the bottom can start with
  423. // AND or GROUP BY.
  424. if($op == " WHERE ") $this->sql_ .= $op." 1=1 ";
  425. $this->sql_ .= " ".$this->sqlbottom_;
  426. $this->sql_ .= " /* ".get_class($this)." */";
  427. $this->qry = DBFactory::getDBQuery();
  428. $this->qry->execute($this->sql_);
  429. }
  430. function getRow()
  431. {
  432. if (is_null($this->qry))
  433. $this->execQuery();
  434. $row = $this->qry->getRow();
  435. return $row;
  436. }
  437. function getTimeFrameSQL()
  438. {
  439. return $this->getDateFilter();
  440. }
  441. }