PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/class/util/RegisteredCharacter.php

https://code.google.com/p/yapeal/
PHP | 308 lines | 160 code | 0 blank | 148 comment | 29 complexity | 3d6433e5cd752263aa26690b8e1cee5c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains RegisteredCharacter 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. };
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. };
  50. /**
  51. * Wrapper class for utilRegisteredCharacter table.
  52. *
  53. * @package Yapeal
  54. * @subpackage Wrappers
  55. */
  56. class RegisteredCharacter extends ALimitedObject implements IGetBy {
  57. /**
  58. * Hold an instance of the AccessMask class.
  59. * @var object
  60. */
  61. protected $am;
  62. /**
  63. * Holds an instance of the DB connection.
  64. * @var object
  65. */
  66. protected $con;
  67. /**
  68. * Holds the table name of the query is being built.
  69. * @var string
  70. */
  71. protected $tableName;
  72. /**
  73. * Holds query builder object.
  74. * @var object
  75. */
  76. protected $qb;
  77. /**
  78. * Set to TRUE if a database record exists.
  79. * @var bool
  80. */
  81. private $recordExists;
  82. /**
  83. * Constructor
  84. *
  85. * @param mixed $id Id of Character wanted.
  86. * @param bool $create When $create is set to FALSE will throw DomainException
  87. * if $id doesn't exist in database.
  88. *
  89. * @throws InvalidArgumentException If $id isn't a number or string throws an
  90. * InvalidArgumentException.
  91. * @throws DomainException If $create is FALSE and a database record for $id
  92. * doesn't exist a DomainException will be thrown.
  93. */
  94. public function __construct($id = NULL, $create = TRUE) {
  95. $this->tableName = YAPEAL_TABLE_PREFIX . 'util' . __CLASS__;
  96. try {
  97. // Get a database connection.
  98. $this->con = YapealDBConnection::connect(YAPEAL_DSN);
  99. }
  100. catch (ADODB_Exception $e) {
  101. $mess = 'Failed to get database connection in ' . __CLASS__;
  102. throw new RuntimeException($mess);
  103. }
  104. // Get a new access mask object.
  105. $this->am = new AccessMask();
  106. // Get a new query builder object.
  107. $this->qb = new YapealQueryBuilder($this->tableName, YAPEAL_DSN);
  108. // Get a list of column names and their ADOdb generic types.
  109. $this->colTypes = $this->qb->getColumnTypes();
  110. // Was $id set?
  111. if (!empty($id)) {
  112. // If $id has any characters other than 0-9 it's not a characterID.
  113. if (0 == strlen(str_replace(range(0, 9), '', $id))) {
  114. if (FALSE === $this->getItemById($id)) {
  115. if (TRUE == $create) {
  116. // If $id is a number and doesn't exist yet set characterID with it.
  117. $this->properties['characterID'] = $id;
  118. } else {
  119. $mess = 'Unknown character ' . $id;
  120. throw new DomainException($mess);
  121. };// else ...
  122. };
  123. // else if it's a string ...
  124. } else if (is_string($id)) {
  125. if (FALSE === $this->getItemByName($id)) {
  126. if (TRUE == $create) {
  127. // If $id is a string and doesn't exist yet set name with it.
  128. $this->properties['characterName'] = $id;
  129. } else {
  130. $mess = 'Unknown character ' . $id;
  131. throw new DomainException($mess);
  132. };// else ...
  133. };
  134. } else {
  135. $mess = 'Parameter $id must be an integer or a string';
  136. throw new InvalidArgumentException($mess);
  137. };// else ...
  138. };// if !empty $id ...
  139. }// function __construct
  140. /**
  141. * Destructor used to make sure to release ADOdb resource correctly more for
  142. * peace of mind than actual need.
  143. */
  144. public function __destruct() {
  145. $this->con = NULL;
  146. }// function __destruct
  147. /**
  148. * Used to add an API to the list in activeAPIMask.
  149. *
  150. * @param string $name Name of the API to add without 'char' part i.e.
  151. * 'charAccountBalance' would just be 'AccountBalance'
  152. *
  153. * @return bool Returns TRUE if $name already exists else FALSE.
  154. *
  155. * @throws DomainException Throws DomainException if $name could not be found.
  156. */
  157. public function addActiveAPI($name) {
  158. $mask = $this->am->apisToMask($name, 'char');
  159. if (($this->properties['activeAPIMask'] & $mask) > 0) {
  160. $ret = TRUE;
  161. } else {
  162. $this->properties['activeAPIMask'] |= $mask;
  163. $ret = FALSE;
  164. };// if $this->properties['activeAPIMask'] ...
  165. return $ret;
  166. }// function addActiveAPI
  167. /**
  168. * Used to delete an API from the list in activeAPIMask.
  169. *
  170. * @param string $name Name of the API to delete without 'char' part i.e.
  171. * 'charAccountBalance' would just be 'AccountBalance'
  172. *
  173. * @return bool Returns TRUE if $name was already set else FALSE.
  174. *
  175. * @throws DomainException Throws DomainException if $name could not be found.
  176. */
  177. public function deleteActiveAPI($name) {
  178. $mask = $this->am->apisToMask($name, 'char');
  179. if (($this->properties['activeAPIMask'] & $mask) > 0) {
  180. $this->properties['activeAPIMask'] ^= $mask;
  181. $ret = TRUE;
  182. } else {
  183. $ret = FALSE;
  184. };// if $this->properties['activeAPIMask'] ...
  185. return $ret;
  186. }// function deleteActiveAPI
  187. /**
  188. * Used to get user from utilRegisteredCharacter table by char ID.
  189. *
  190. * @param $id Id of Character wanted.
  191. *
  192. * @return bool TRUE if char was retrieved.
  193. */
  194. public function getItemById($id) {
  195. $sql = 'select `' . implode('`,`', array_keys($this->colTypes)) . '`';
  196. $sql .= ' from `' . $this->tableName . '`';
  197. $sql .= ' where `characterID`=' . $id;
  198. try {
  199. $result = $this->con->GetRow($sql);
  200. if (!empty($result)) {
  201. $this->properties = $result;
  202. $this->recordExists = TRUE;
  203. } else {
  204. $this->recordExists = FALSE;
  205. // Get new mask from section.
  206. $sql = 'select `activeAPIMask`';
  207. $sql .= ' from `' . YAPEAL_TABLE_PREFIX . 'utilSections`';
  208. $sql .= ' where `section` = "char"';
  209. $result = $this->con->GetOne($sql);
  210. $this->properties['activeAPIMask'] = (string)$result;
  211. // Get characterName from accountCharacter if available.
  212. $sql = 'select `characterName`';
  213. $sql .= ' from `' . YAPEAL_TABLE_PREFIX . 'accountCharacters`';
  214. $sql .= ' where `characterID`=' . $id;
  215. $result = $this->con->GetOne($sql);
  216. if (!empty($result)) {
  217. $this->properties['characterName'] = (string)$result;
  218. };
  219. };
  220. }
  221. catch (ADODB_Exception $e) {
  222. Logger::getLogger('yapeal')->warn($e);
  223. $this->recordExists = FALSE;
  224. }
  225. return $this->recordExists();
  226. }// function getItemById
  227. /**
  228. * Used to get item from table by name.
  229. *
  230. * @param $name Name of record wanted.
  231. *
  232. * @return bool TRUE if item was retrieved else FALSE.
  233. */
  234. public function getItemByName($name) {
  235. $sql = 'select `' . implode('`,`', array_keys($this->colTypes)) . '`';
  236. $sql .= ' from `' . $this->tableName . '`';
  237. try {
  238. $sql .= ' where `characterName`=' . $this->con->qstr($name);
  239. $result = $this->con->GetRow($sql);
  240. if (!empty($result)) {
  241. $this->properties = $result;
  242. $this->recordExists = TRUE;
  243. } else {
  244. $this->recordExists = FALSE;
  245. // Get new mask from section.
  246. $sql = 'select `activeAPIMask`';
  247. $sql .= ' from `' . YAPEAL_TABLE_PREFIX . 'utilSections`';
  248. $sql .= ' where `section` = "char"';
  249. $result = $this->con->GetOne($sql);
  250. $this->properties['activeAPIMask'] = (string)$result;
  251. // Get characterID from accountCharacter if available.
  252. $sql = 'select `characterID`';
  253. $sql .= ' from `' . YAPEAL_TABLE_PREFIX . 'accountCharacters`';
  254. $sql .= ' where `characterName`=' . $name;
  255. $result = $this->con->GetOne($sql);
  256. if (!empty($result)) {
  257. $this->properties['characterID'] = (string)$result;
  258. };
  259. };
  260. }
  261. catch (ADODB_Exception $e) {
  262. Logger::getLogger('yapeal')->warn($e);
  263. $this->recordExists = FALSE;
  264. }
  265. return $this->recordExists();
  266. }// function getItemByName
  267. /**
  268. * Function used to check if database record already existed.
  269. *
  270. * @return bool Returns TRUE if the the database record already existed.
  271. */
  272. public function recordExists() {
  273. return $this->recordExists;
  274. }// function recordExists
  275. /**
  276. * Used to set default for column.
  277. *
  278. * @param string $name Name of the column.
  279. * @param mixed $value Value to be used as default for column.
  280. *
  281. * @return bool Returns TRUE if column exists in table and default was set.
  282. */
  283. public function setDefault($name, $value) {
  284. return $this->qb->setDefault($name, $value);
  285. }// function setDefault
  286. /**
  287. * Used to set defaults for multiple columns.
  288. *
  289. * @param array $defaults List of column names and new default values.
  290. *
  291. * @return bool Returns TRUE if all column defaults could be set, else FALSE.
  292. */
  293. public function setDefaults(array $defaults) {
  294. return $this->qb->setDefaults($defaults);
  295. }// function setDefaults
  296. /**
  297. * Used to store data into table.
  298. *
  299. * @return bool Return TRUE if store was successful.
  300. */
  301. public function store() {
  302. if (FALSE === $this->qb->addRow($this->properties)) {
  303. return FALSE;
  304. };// if FALSE === ...
  305. return $this->qb->store();
  306. }// function store
  307. }
  308. ?>