Advertisement
Guest User

Untitled

a guest
Feb 7th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1. <?php
  2. if(!defined('INITIALIZED'))
  3. exit;
  4.  
  5. class Guild extends ObjectData
  6. {
  7. const LOADTYPE_ID = 'id';
  8. const LOADTYPE_NAME = 'name';
  9.  
  10. const LEVEL_NOT_IN_GUILD = 0;
  11. const LEVEL_MEMBER = 1;
  12. const LEVEL_VICE = 2;
  13. const LEVEL_LEADER = 3;
  14. const LEVEL_OWNER = 4;
  15. public static $table = 'guilds';
  16. public $data = array('world_id' => null, 'name' => null, 'ownerid' => null, 'creationdata' => null, 'motd' => null, 'description' => null, 'create_ip' => null, 'guild_logo' => null);
  17. public static $fields = array('id', 'world_id', 'name', 'ownerid', 'creationdata', 'motd', 'description', 'create_ip', 'guild_logo');
  18. public $invitedPlayers;
  19. public $ranks;
  20. public $owner;
  21.  
  22. public function __construct($search_text = null, $search_by = self::LOADTYPE_ID)
  23. {
  24. if($search_text != null)
  25. $this->load($search_text, $search_by);
  26. }
  27.  
  28. public function load($search_text, $search_by = self::LOADTYPE_ID)
  29. {
  30. if(in_array($search_by, self::$fields))
  31. $search_string = $this->getDatabaseHandler()->fieldName($search_by) . ' = ' . $this->getDatabaseHandler()->quote($search_text);
  32. else
  33. new Error_Critic('', 'Wrong guild search_by type.');
  34. $fieldsArray = array();
  35. foreach(self::$fields as $fieldName)
  36. $fieldsArray[] = $this->getDatabaseHandler()->fieldName($fieldName);
  37.  
  38. $this->data = $this->getDatabaseHandler()->query('SELECT ' . implode(', ', $fieldsArray) . ' FROM ' . $this->getDatabaseHandler()->tableName(self::$table) . ' WHERE ' . $search_string)->fetch();
  39. }
  40.  
  41. public function loadById($id)
  42. {
  43. return $this->load($id, self::LOADTYPE_ID);
  44. }
  45.  
  46. public function loadByName($name)
  47. {
  48. return $this->load($name, self::LOADTYPE_NAME);
  49. }
  50.  
  51. public function save($forceInsert = false)
  52. {
  53. if(!isset($this->data['id']) || $forceInsert)
  54. {
  55. $keys = array();
  56. $values = array();
  57. foreach(self::$fields as $key)
  58. if($key != 'id')
  59. {
  60. $keys[] = $this->getDatabaseHandler()->fieldName($key);
  61. $values[] = $this->getDatabaseHandler()->quote($this->data[$key]);
  62. }
  63. $this->getDatabaseHandler()->query('INSERT INTO ' . $this->getDatabaseHandler()->tableName(self::$table) . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $values) . ')');
  64. $this->setID($this->getDatabaseHandler()->lastInsertId());
  65. }
  66. else
  67. {
  68. $updates = array();
  69. foreach(self::$fields as $key)
  70. $updates[] = $this->getDatabaseHandler()->fieldName($key) . ' = ' . $this->getDatabaseHandler()->quote($this->data[$key]);
  71. $this->getDatabaseHandler()->query('UPDATE ' . $this->getDatabaseHandler()->tableName(self::$table) . ' SET ' . implode(', ', $updates) . ' WHERE ' . $this->getDatabaseHandler()->fieldName('id') . ' = ' . $this->getDatabaseHandler()->quote($this->data['id']));
  72. }
  73. }
  74.  
  75. public function delete()
  76. {
  77. if($this->isLoaded())
  78. {
  79. foreach($this->getRanks(true) as $rank)
  80. {
  81. $rank->delete();
  82. }
  83. $this->getDatabaseHandler()->query('DELETE FROM ' . $this->getDatabaseHandler()->tableName(self::$table) . ' WHERE ' . $this->getDatabaseHandler()->fieldName('id') . ' = ' . $this->getDatabaseHandler()->quote($this->data['id']));
  84. $_tmp = new self();
  85. $this->data = $_tmp->data;
  86. $this->invitedPlayers = $_tmp->invitedPlayers;
  87. $this->ranks = $_tmp->ranks;
  88. $this->owner = $_tmp->owner;
  89. unset($_tmp);
  90. }
  91. else
  92. new Error_Critic('', __METHOD__ . '() - cannot delete, guild not loaded');
  93. }
  94.  
  95. public function kickPlayer($playerId)
  96. {
  97. if($playerId == $this->getOwnerID())
  98. $this->delete();
  99. else
  100. {
  101. $player = new Player($playerId);
  102. if($player->isLoaded())
  103. {
  104. $player->setRankID(0);
  105. $player->save();
  106. }
  107. }
  108. }
  109.  
  110. public function getInvitedPlayers($forceReload = false)
  111. {
  112. return $this->getInvitations($forceReload);
  113. }
  114.  
  115. public function getInvitations($forceReload = false)
  116. {
  117. if(!isset($this->invitedPlayers) || $forceReload)
  118. {
  119. $invitedPlayers = new DatabaseList('Player');
  120. $filterGuild = new SQL_Filter(new SQL_Field('guild_id', 'guild_invites'), SQL_Filter::EQUAL, $this->getID());
  121. $filterPlayer = new SQL_Filter(new SQL_Field('id', 'players'), SQL_Filter::EQUAL, new SQL_Field('player_id', 'guild_invites'));
  122. $invitedPlayers->setFilter(new SQL_Filter($filterGuild, SQL_Filter::CRITERIUM_AND, $filterPlayer));
  123. $invitedPlayers->addOrder(new SQL_Order(new SQL_Field('name', 'players')));
  124. $this->invitedPlayers = $invitedPlayers;
  125. }
  126. return $this->invitedPlayers;
  127. }
  128.  
  129. public function addInvitation($playerId, $reloadInvites = false)
  130. {
  131. $this->getDatabaseHandler()->query('INSERT INTO ' . $this->getDatabaseHandler()->tableName('guild_invites') . ' (' . $this->getDatabaseHandler()->fieldName('player_id') . ', ' . $this->getDatabaseHandler()->fieldName('guild_id') . ') VALUES (' . $this->getDatabaseHandler()->quote($playerId) . ', ' . $this->getDatabaseHandler()->quote($this->getID()) . ')');
  132. if($reloadInvites)
  133. $this->getInvitations(true);
  134. }
  135.  
  136. public function removeInvitation($playerId)
  137. {
  138. $this->getDatabaseHandler()->query('DELETE FROM ' . $this->getDatabaseHandler()->tableName('guild_invites') . ' WHERE ' . $this->getDatabaseHandler()->fieldName('player_id') . ' = ' . $this->getDatabaseHandler()->quote($playerId) . ' AND ' . $this->getDatabaseHandler()->fieldName('guild_id') . ' = ' . $this->getDatabaseHandler()->quote($this->getID()));
  139. }
  140.  
  141. public function isInvited($playerId, $forceReload = false)
  142. {
  143. foreach($this->getInvitations($forceReload) as $invitedPlayer)
  144. if($invitedPlayer->getID() == $playerId)
  145. return true;
  146.  
  147. return false;
  148. }
  149.  
  150. public function getRanks($forceReload = false)
  151. {
  152. if(!isset($this->ranks) || $forceReload)
  153. {
  154. $ranks = new DatabaseList('GuildRank');
  155. $ranks->setFilter(new SQL_Filter(new SQL_Field('guild_id'), SQL_Filter::EQUAL, $this->getID()));
  156. $ranks->addOrder(new SQL_Order(new SQL_Field('level'), SQL_Order::DESC));
  157. $this->ranks = $ranks;
  158. }
  159.  
  160. return $this->ranks;
  161. }
  162.  
  163. public function getOwner($forceReload = false)
  164. {
  165. if(!isset($this->owner) || $forceReload)
  166. $this->owner = new Player($this->getOwnerID());
  167.  
  168. return $this->owner;
  169. }
  170.  
  171. public function setOwner($owner)
  172. {
  173. $this->owner = $owner;
  174. $this->setOwnerID($owner->getID());
  175. }
  176.  
  177. public function getGuildLogoLink()
  178. {
  179. return 'guild_image.php?id=' . $this->getID();
  180. }
  181.  
  182. public function getID(){return $this->data['id'];}
  183. public function setID($value){$this->data['id'] = $value;}
  184. public function getWorldID(){return $this->data['world_id'];}
  185. public function setWorldID($value){$this->data['world_id'] = $value;}
  186. public function getName(){return $this->data['name'];}
  187. public function setName($value){$this->data['name'] = $value;}
  188. public function getOwnerID(){return $this->data['ownerid'];}
  189. public function setOwnerID($value){$this->data['ownerid'] = $value;}
  190. public function getCreationData(){return $this->data['creationdata'];}
  191. public function setCreationData($value){$this->data['creationdata'] = $value;}
  192. public function getMOTD(){return $this->data['motd'];}
  193. public function setMOTD($value){$this->data['motd'] = $value;}
  194. /*
  195. * Custom AAC fields
  196. * create_ip , INT, default 0
  197. * description , TEXT, default ''
  198. * guild_logo, MEDIUMBLOB, default NULL
  199. */
  200. public function setCreateIP($value){$this->data['create_ip'] = $value;}
  201. public function getCreateIP(){return $this->data['create_ip'];}
  202. public function getDescription(){return $this->data['description'];}
  203. public function setDescription($value){$this->data['description'] = $value;}
  204. public function getGuildLogo()
  205. {
  206. return $this->data['guild_logo'];
  207. }
  208.  
  209. public function setGuildLogo($mimeType, $fileData)
  210. {
  211. $this->data['guild_logo'] = time() . ';data:' . $mimeType . ';base64,' . base64_encode($fileData);
  212. }
  213. /*
  214. * for compability with old scripts
  215. */
  216. public function setCreateDate($value){$this->data['creationdata'] = $value;}
  217. public function getCreateDate(){return $this->data['creationdata'];}
  218. public function getGuildRanksList(){return $this->getRanks();}
  219. public function getGuildRanks(){return $this->getRanks();}
  220. public function listInvites(){return $this->getInvitations();}
  221. public function invite($player){$this->addInvitation($player->getID());}
  222. public function deleteInvite($player){$this->removeInvitation($player->getID());}
  223. public function acceptInvite($player)
  224. {
  225. $ranks = new DatabaseList('GuildRank');
  226. $ranks->setFilter(new SQL_Filter(new SQL_Field('guild_id'), SQL_Filter::EQUAL, $this->getID()));
  227. $ranks->addOrder(new SQL_Order(new SQL_Field('level'), SQL_Order::ASC));
  228. // load rank with lowest access level
  229. if($rank = $ranks->getResult(0))
  230. {
  231. $player->setRank($rank);
  232. $player->save();
  233. $player->removeGuildInvitations();
  234. }
  235. else
  236. new Error_Critic('', 'There is no rank in guild <b>' . htmlspecialchars($guild->getName()) . '</b>, cannot add player <b>' . htmlspecialchars($player->getName()) . '</b> to guild.');
  237. }
  238. public function find($name){$this->loadByName($name);}
  239. public function getWorld(){return $this->data['world_id'];}
  240. public function setWorld($value){$this->data['world_id'] = $value;}
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement