PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/class/api/char/charCharacterSheet.php

https://code.google.com/p/yapeal/
PHP | 386 lines | 280 code | 0 blank | 106 comment | 48 complexity | 6205d7b393920cd754db79474e7e736f MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains CharacterSheet class.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: This file is part of Yet Another Php Eve Api library also know
  8. * as Yapeal which will be used to refer to it in the rest of this license.
  9. *
  10. * Yapeal is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Yapeal is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with Yapeal. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @author Michael Cummings <mgcummings@yahoo.com>
  24. * @copyright Copyright (c) 2008-2012, Michael Cummings
  25. * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
  26. * @package Yapeal
  27. * @link http://code.google.com/p/yapeal/
  28. * @link http://www.eveonline.com/
  29. */
  30. /**
  31. * @internal Allow viewing of the source code in web browser.
  32. */
  33. if (isset($_REQUEST['viewSource'])) {
  34. highlight_file(__FILE__);
  35. exit();
  36. };
  37. /**
  38. * @internal Only let this code be included.
  39. */
  40. if (count(get_included_files()) < 2) {
  41. $mess = basename(__FILE__)
  42. . ' must be included it can not be ran directly.' . PHP_EOL;
  43. if (PHP_SAPI != 'cli') {
  44. header('HTTP/1.0 403 Forbidden', TRUE, 403);
  45. die($mess);
  46. } else {
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. }
  50. };
  51. /**
  52. * Class used to fetch and store CharacterSheet API.
  53. *
  54. * @package Yapeal
  55. * @subpackage Api_char
  56. */
  57. class charCharacterSheet extends AChar {
  58. /**
  59. * Constructor
  60. *
  61. * @param array $params Holds the required parameters like keyID, vCode, etc
  62. * used in HTML POST parameters to API servers which varies depending on API
  63. * 'section' being requested.
  64. *
  65. * @throws LengthException for any missing required $params.
  66. */
  67. public function __construct(array $params) {
  68. // Cut off 'A' and lower case abstract class name to make section name.
  69. $this->section = strtolower(substr(get_parent_class($this), 1));
  70. $this->api = str_replace($this->section, '', __CLASS__);
  71. parent::__construct($params);
  72. }// function __construct
  73. /**
  74. * Per API parser for XML.
  75. *
  76. * @return bool Returns TRUE if XML was parsed correctly, FALSE if not.
  77. */
  78. protected function parserAPI() {
  79. $tableName = YAPEAL_TABLE_PREFIX . $this->section . $this->api;
  80. // Get a new query instance.
  81. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  82. $qb->setDefault('allianceName', '');
  83. $row = array();
  84. try {
  85. while ($this->xr->read()) {
  86. switch ($this->xr->nodeType) {
  87. case XMLReader::ELEMENT:
  88. switch ($this->xr->localName) {
  89. case 'allianceID':
  90. case 'allianceName':
  91. case 'ancestry':
  92. case 'balance':
  93. case 'bloodLine':
  94. case 'characterID':
  95. case 'cloneName':
  96. case 'cloneSkillPoints':
  97. case 'corporationID':
  98. case 'corporationName':
  99. case 'DoB':
  100. case 'gender':
  101. case 'name':
  102. case 'race':
  103. // Grab node name.
  104. $name = $this->xr->localName;
  105. if ($name == 'allianceName' && $this->xr->isEmptyElement == TRUE) {
  106. $row[$name] = '';
  107. } else {
  108. // Move to text node.
  109. $this->xr->read();
  110. $row[$name] = $this->xr->value;
  111. };
  112. break;
  113. case 'attributes':
  114. case 'attributeEnhancers':
  115. // Check if empty.
  116. if ($this->xr->isEmptyElement == TRUE) {
  117. break;
  118. };// if $this->xr->isEmptyElement ...
  119. // Grab node name.
  120. $subTable = $this->xr->localName;
  121. // Check for method with same name as node.
  122. if (!is_callable(array($this, $subTable))) {
  123. $mess = 'Unknown what-to-be rowset ' . $subTable;
  124. $mess .= ' found in ' . $this->api;
  125. Logger::getLogger('yapeal')->warn($mess);
  126. return FALSE;
  127. };
  128. $this->$subTable();
  129. break;
  130. case 'rowset':
  131. // Check if empty.
  132. if ($this->xr->isEmptyElement == TRUE) {
  133. break;
  134. };// if $this->xr->isEmptyElement ...
  135. // Grab rowset name.
  136. $subTable = $this->xr->getAttribute('name');
  137. if (empty($subTable)) {
  138. $mess = 'Name of rowset is missing in ' . $this->api;
  139. Logger::getLogger('yapeal')->warn($mess);
  140. return FALSE;
  141. };
  142. if ($subTable == 'skills') {
  143. $this->$subTable();
  144. } else {
  145. $this->rowset($subTable);
  146. };// else $subTable ...
  147. break;
  148. default:// Nothing to do here.
  149. };// $this->xr->localName ...
  150. break;
  151. case XMLReader::END_ELEMENT:
  152. if ($this->xr->localName == 'result') {
  153. $qb->addRow($row);
  154. if (count($qb) > 0) {
  155. $qb->store();
  156. };// if count $rows ...
  157. $qb = NULL;
  158. return TRUE;
  159. };// if $this->xr->localName == 'row' ...
  160. break;
  161. default:// Nothing to do.
  162. };// switch $this->xr->nodeType ...
  163. };// while $this->xr->read() ...
  164. }
  165. catch (ADODB_Exception $e) {
  166. Logger::getLogger('yapeal')->error($e);
  167. return FALSE;
  168. }
  169. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  170. Logger::getLogger('yapeal')->warn($mess);
  171. return FALSE;
  172. }// function parserAPI
  173. /**
  174. * Handles attributes table.
  175. *
  176. * @return bool Returns TRUE if data stored to database table.
  177. */
  178. protected function attributes() {
  179. $tableName = YAPEAL_TABLE_PREFIX . $this->section . ucfirst(__FUNCTION__);
  180. // Get a new query instance.
  181. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  182. // Save some overhead for tables that are truncated or in some way emptied.
  183. $qb->useUpsert(FALSE);
  184. $row = array('ownerID' => $this->ownerID);
  185. while ($this->xr->read()) {
  186. switch ($this->xr->nodeType) {
  187. case XMLReader::ELEMENT:
  188. switch ($this->xr->localName) {
  189. case 'charisma':
  190. case 'intelligence':
  191. case 'memory':
  192. case 'perception':
  193. case 'willpower':
  194. $name = $this->xr->localName;
  195. $this->xr->read();
  196. $row[$name] = $this->xr->value;
  197. break;
  198. };// switch $xr->localName ...
  199. break;
  200. case XMLReader::END_ELEMENT:
  201. if ($this->xr->localName == 'attributes') {
  202. $qb->addRow($row);
  203. return $qb->store();
  204. };// if $this->xr->localName ...
  205. break;
  206. default:// Nothing to do here.
  207. };// switch $this->xr->nodeType ...
  208. };// while $xr->read() ...
  209. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  210. Logger::getLogger('yapeal')->warn($mess);
  211. return FALSE;
  212. }// function attributes
  213. /**
  214. * Used to store XML to CharacterSheet's attributeEnhancers table.
  215. *
  216. * @return Bool Return TRUE if store was successful.
  217. */
  218. protected function attributeEnhancers() {
  219. $tableName = YAPEAL_TABLE_PREFIX . $this->section . ucfirst(__FUNCTION__);
  220. // Get a new query instance.
  221. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  222. // Save some overhead for tables that are truncated or in some way emptied.
  223. $qb->useUpsert(FALSE);
  224. $row = array();
  225. while ($this->xr->read()) {
  226. switch ($this->xr->nodeType) {
  227. case XMLReader::ELEMENT:
  228. switch ($this->xr->localName) {
  229. case 'charismaBonus':
  230. case 'intelligenceBonus':
  231. case 'memoryBonus':
  232. case 'perceptionBonus':
  233. case 'willpowerBonus':
  234. $row = array('ownerID' => $this->ownerID);
  235. $row['bonusName'] = $this->xr->localName;
  236. break;
  237. case 'augmentatorName':
  238. case 'augmentatorValue':
  239. $name = $this->xr->localName;
  240. $this->xr->read();
  241. $row[$name] = $this->xr->value;
  242. break;
  243. default:// Nothing to do here.
  244. };// switch $xr->localName ...
  245. break;
  246. case XMLReader::END_ELEMENT:
  247. switch ($this->xr->localName) {
  248. case 'charismaBonus':
  249. case 'intelligenceBonus':
  250. case 'memoryBonus':
  251. case 'perceptionBonus':
  252. case 'willpowerBonus':
  253. $qb->addRow($row);
  254. break;
  255. case 'attributeEnhancers':
  256. return $qb->store();
  257. default:// Nothing to do here.
  258. };// switch $xr->localName ...
  259. break;
  260. default:// Nothing to do here.
  261. };// switch $this->xr->nodeType ...
  262. };// while $xr->read() ...
  263. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  264. Logger::getLogger('yapeal')->warn($mess);
  265. return FALSE;
  266. }// function attributeEnhancers
  267. /**
  268. * Used to store XML to rowset tables.
  269. *
  270. * @param string $table Name of the table for this rowset.
  271. *
  272. * @return Bool Return TRUE if store was successful.
  273. */
  274. protected function rowset($table) {
  275. $tableName = YAPEAL_TABLE_PREFIX . $this->section . ucfirst($table);
  276. // Get a new query instance.
  277. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  278. // Save some overhead for tables that are truncated or in some way emptied.
  279. $qb->useUpsert(FALSE);
  280. $qb->setDefault('ownerID', $this->ownerID);
  281. $row = array();
  282. while ($this->xr->read()) {
  283. switch ($this->xr->nodeType) {
  284. case XMLReader::ELEMENT:
  285. switch ($this->xr->localName) {
  286. case 'row':
  287. // Walk through attributes and add them to row.
  288. while ($this->xr->moveToNextAttribute()) {
  289. $row[$this->xr->name] = $this->xr->value;
  290. };// while $this->xr->moveToNextAttribute() ...
  291. $qb->addRow($row);
  292. break;
  293. };// switch $this->xr->localName ...
  294. break;
  295. case XMLReader::END_ELEMENT:
  296. if ($this->xr->localName == 'rowset') {
  297. // Insert any leftovers.
  298. if (count($qb) > 0) {
  299. $qb->store();
  300. };// if count $rows ...
  301. $qb = NULL;
  302. return TRUE;
  303. };// if $this->xr->localName == 'row' ...
  304. break;
  305. };// switch $this->xr->nodeType
  306. };// while $this->xr->read() ...
  307. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  308. Logger::getLogger('yapeal')->warn($mess);
  309. return FALSE;
  310. }// function rowset
  311. /**
  312. * Used to store XML to CharacterSheet's skills table.
  313. *
  314. * @return Bool Return TRUE if store was successful.
  315. */
  316. protected function skills() {
  317. $tableName = YAPEAL_TABLE_PREFIX . $this->section . ucfirst(__FUNCTION__);
  318. // Get a new query instance.
  319. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  320. // Save some overhead for tables that are truncated or in some way emptied.
  321. $qb->useUpsert(FALSE);
  322. $defaults = array('level' => 0, 'ownerID' => $this->ownerID,
  323. 'published' => 1
  324. );
  325. $qb->setDefaults($defaults);
  326. $row = array();
  327. while ($this->xr->read()) {
  328. switch ($this->xr->nodeType) {
  329. case XMLReader::ELEMENT:
  330. switch ($this->xr->localName) {
  331. case 'row':
  332. // Walk through attributes and add them to row.
  333. while ($this->xr->moveToNextAttribute()) {
  334. $row[$this->xr->name] = $this->xr->value;
  335. };// while $this->xr->moveToNextAttribute() ...
  336. $qb->addRow($row);
  337. break;
  338. };// switch $this->xr->localName ...
  339. break;
  340. case XMLReader::END_ELEMENT:
  341. if ($this->xr->localName == 'rowset') {
  342. // Insert any leftovers.
  343. if (count($qb) > 0) {
  344. $qb->store();
  345. };// if count $rows ...
  346. $qb = NULL;
  347. return TRUE;
  348. };// if $this->xr->localName == 'row' ...
  349. break;
  350. };// switch $this->xr->nodeType
  351. };// while $this->xr->read() ...
  352. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  353. Logger::getLogger('yapeal')->warn($mess);
  354. return FALSE;
  355. }// function skills
  356. /**
  357. * Method used to prepare database table(s) before parsing API XML data.
  358. *
  359. * If there is any need to delete records or empty tables before parsing XML
  360. * and adding the new data this method should be used to do so.
  361. *
  362. * @return bool Will return TRUE if table(s) were prepared correctly.
  363. */
  364. protected function prepareTables() {
  365. $tables = array('Attributes', 'AttributeEnhancers', 'Certificates',
  366. 'CorporationRoles', 'CorporationRolesAtBase', 'CorporationRolesAtHQ',
  367. 'CorporationRolesAtOther', 'CorporationTitles', 'Skills'
  368. );
  369. foreach ($tables as $table) {
  370. try {
  371. $con = YapealDBConnection::connect(YAPEAL_DSN);
  372. // Empty out old data then upsert (insert) new.
  373. $sql = 'delete from `';
  374. $sql .= YAPEAL_TABLE_PREFIX . $this->section . $table . '`';
  375. $sql .= ' where `ownerID`=' . $this->ownerID;
  376. $con->Execute($sql);
  377. }
  378. catch (ADODB_Exception $e) {
  379. Logger::getLogger('yapeal')->warn($e);
  380. return FALSE;
  381. }
  382. };// foreach $tables ...
  383. return TRUE;
  384. }// function prepareTables
  385. }
  386. ?>