PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/user/inc/messaging.lib.php

http://wowroster.googlecode.com/
PHP | 362 lines | 227 code | 25 blank | 110 comment | 48 complexity | bd1ae3ed2aa297aa74317bbbd80bd2af MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Dev.PKComp.net user Addon
  4. *
  5. * LICENSE: Licensed under the Creative Commons
  6. * "Attribution-NonCommercial-ShareAlike 2.5" license
  7. *
  8. * @copyright 2005-2007 Pretty Kitty Development
  9. * @author mdeshane
  10. * @license http://creativecommons.org/licenses/by-nc-sa/2.5 Creative Commons "Attribution-NonCommercial-ShareAlike 2.5"
  11. * @link http://dev.pkcomp.net
  12. * @package user
  13. * @subpackage user Messaging Handler
  14. */
  15. if ( !defined('ROSTER_INSTALLED') )
  16. {
  17. exit('Detected invalid access to this file!');
  18. }
  19. /*
  20. Sample:
  21. $user->message = new userMessaging();
  22. $user->message->SendMessage('Promotion','Today we have a brand new promotion from the pack number5',10,30,3);//user 10 sends a message to user 30 and have a level of 3
  23. $array = $user->message->GetAllMessages(0,30);
  24. */
  25. class userMessaging
  26. {
  27. var $err_msg;
  28. /*
  29. * @desc Class constructor
  30. * @param String $tblName
  31. * @return userMessaging returns
  32. */
  33. function userMessaging()
  34. {
  35. global $roster, $addon, $user;
  36. return;
  37. }
  38. /**
  39. * @return Int 0 Message sended succesfully
  40. * @return Int 1 No title
  41. * @return Int 2 No body
  42. * @return Int 3 Invalid Sender
  43. * @return Int 4 Invalid Receiver
  44. * @return Int 5 Invalid Sender Level
  45. * @return Int 6 Error inputing in Database
  46. *
  47. * @param String $title
  48. * @param String $body
  49. * @param Int $sender
  50. * @param Int $receiver
  51. *
  52. * @desc Send message to $receiver
  53. */
  54. function SendMessage($title ,$body ,$sender ,$receiver ,$senderLevel)
  55. {
  56. global $roster, $addon, $user;
  57. if( strlen($title) == 0 )
  58. {
  59. $this->err_msg = 1;
  60. }
  61. if( strlen($body) == 0 )
  62. {
  63. $this->err_msg = 2;
  64. }
  65. if( strlen($sender) == 0 )
  66. {
  67. $this->err_msg = 3;
  68. }
  69. if( strlen($receiver) == 0 )
  70. {
  71. $this->err_msg = 4;
  72. }
  73. if( strlen($senderLevel) == 0)
  74. {
  75. $senderLevel = 0;
  76. }
  77. $result = $roster->db->query("INSERT INTO `".$roster->db->table('messaging',$addon['basename'])."` (title,body,sender,uid,senderLevel) VALUES ('$title','$body',$sender,$receiver,$senderLevel)");
  78. if($result)
  79. {
  80. $this->err_msg = 0;
  81. }
  82. else
  83. {
  84. return 6;
  85. }
  86. }
  87. /**
  88. * @return int 1 When msgId equal to 0
  89. * @return int 2 When no Message in database with that msgId
  90. * @return String Returns the title of the message
  91. * @param int $msgId
  92. * @desc This function is used to return the title of a especific message
  93. */
  94. function GetTitle($msgId)
  95. {
  96. global $roster, $addon, $user;
  97. if(strlen($msgId) == 0)
  98. {
  99. $this->err_msg = 1;
  100. }
  101. $result = $roster->db->query("SELECT `title` FROM `".$user->db['message']."` WHERE `msgid`=$msgId");
  102. if($roster->db->num_rows($result) == 0)
  103. {
  104. $this->err_msg = 2;
  105. }
  106. $row = $roster->db->fetch($result);
  107. return $row->title;
  108. }
  109. /**
  110. * @return int 1 When msgId equal to 0
  111. * @return int 2 When no Message in database with that msgId
  112. * @return String Returns the body of the message
  113. * @param int $msgId
  114. * @desc This function is used to return the body of a especific message
  115. */
  116. function GetBody($msgId)
  117. {
  118. global $roster, $addon, $user;
  119. if(strlen($msgId) == 0)
  120. {
  121. $this->err_msg = 1;
  122. }
  123. $result = $roster->db->query("SELECT `body` FROM `".$user->db['message']."` WHERE `msgid`=$msgId");
  124. if($roster->db->num_rows($result) == 0)
  125. {
  126. $this->err_msg = 2;
  127. }
  128. $row = $roster->db->fetch($result);
  129. return $row->body;
  130. }
  131. /**
  132. * @return int 1 When msgId equal to 0
  133. * @return int 2 When no Message in database with that msgId
  134. * @return int Other int Return the userId that sent this message
  135. * @param int $msgId
  136. * @desc This function is used to return the userId from the sender of a especific message
  137. */
  138. function GetSenderID($msgId)
  139. {
  140. global $roster, $addon, $user;
  141. if(strlen($msgId) == 0)
  142. {
  143. $this->err_msg = 1;
  144. }
  145. $result = $roster->db->query("SELECT `sender` FROM `".$user->db['message']."` WHERE `msgid`=$msgId");
  146. if($roster->db->num_rows($result) == 0)
  147. {
  148. $this->err_msg = 2;
  149. }
  150. $row = $roster->db->fetch($result);
  151. return $row->sender;
  152. }
  153. /**
  154. * @return int 1 When msgId equal to 0
  155. * @return int 2 When no Message in database with that msgId
  156. * @return int Other int Return the userId that is the receiver this message
  157. * @param int $msgId
  158. * @desc This function is used to return the userId from the receiver of a especific message
  159. */
  160. function GetReceiverID($msgId)
  161. {
  162. global $roster, $addon, $user;
  163. if(strlen($msgId) == 0)
  164. {
  165. $this->err_msg = 1;
  166. }
  167. $result = $roster->db->query("SELECT `uid` FROM `".$user->db['message']."` WHERE `msgid`=$msgId");
  168. if($roster->db->num_rows($result) == 0)
  169. {
  170. $this->err_msg = 2;
  171. }
  172. $row = $roster->db->fetch($result);
  173. return $row->receiver;
  174. }
  175. /**
  176. * @return int 1 When msgId equal to 0
  177. * @return int 2 When no Message in database with that msgId
  178. * @return int Other int Return the date when the message was sent
  179. * @param int $msgId
  180. * @desc This function is used to return the send date of a especific message
  181. */
  182. function GetSendDate( $msgId )
  183. {
  184. global $roster, $addon, $user;
  185. if(strlen($msgId) == 0)
  186. {
  187. $this->err_msg = 1;
  188. }
  189. $result = $roster->db->query("SELECT `date` FROM `".$user->db['message']."` WHERE `msgid`=$msgId");
  190. if($roster->db->num_rows($result) == 0)
  191. {
  192. $this->err_msg = 2;
  193. }
  194. $row = $roster->db->fetch($result);
  195. return $row->date;
  196. }
  197. /**
  198. * @return int 1 When msgId equal to 0
  199. * @return int 2 When no Message in database with that msgId
  200. * @return array Returns the an array with all the fields of the table were messages are stored
  201. * @param int $msgId
  202. * @desc This function is used to return the message, and all is specifications
  203. */
  204. function GetMessage($msgId)
  205. {
  206. global $roster, $addon, $user;
  207. $message = array();
  208. if(strlen($msgId) == 0)
  209. {
  210. $this->err_msg = 1;
  211. }
  212. $result = $roster->db->query("SELECT * FROM `".$user->db['message']."` WHERE `msgid`=$msgId");
  213. if($roster->db->num_rows($result) == 0)
  214. {
  215. $this->err_msg = 2;
  216. }
  217. $row = $roster->db->fetch($result);
  218. $message['receiver'] = $row['uid'];
  219. $message['sender'] = $row['sender'];
  220. $message['title'] = $row['title'];
  221. $message['body'] = $row['body'];
  222. $message['senderLevel'] = $row['senderLevel'];
  223. $message['read'] = $row['read'];
  224. $message['date'] = $row['date'];
  225. return $message;
  226. }
  227. /**
  228. * @return Int 0 Marked Readed succesfully
  229. * @return Int 1 When msgId equal to 0
  230. * @return Int 2 Error while updating database
  231. * @param unknown $msgId
  232. * @desc Marks the message has readed
  233. */
  234. function markAsRead($msgId)
  235. {
  236. global $roster, $addon, $user;
  237. if(strlen($msgId) == 0)
  238. {
  239. $this->err_msg = 1;
  240. }
  241. $result = $roster->db->query("UPDATE `".$roster->db->table('messaging','user')."` SET `read`='1' WHERE `msgid`='$msgId'");
  242. if($result)
  243. {
  244. return;
  245. }
  246. else
  247. {
  248. $this->err_msg = 2;
  249. }
  250. }
  251. /**
  252. * @return Int 1 Error while collectiing info on database
  253. * @return Int 2 No messages with this settings
  254. * @return Array Returns one array with all messages
  255. *
  256. * @param Int $order Can take values from 0 to 3(0-> Order By senderLevel Ascendent,
  257. * 1-> Order By senderLevel Descendent,
  258. * 2-> Order By readed message Ascendent,
  259. * 3-> Order By senderLevel Descendent)
  260. * @param Int $receiver
  261. * @param Int $sender
  262. *
  263. * @desc This function outputs all messages ordered by $order field and filtered by sender and/or receiver or none to display all messages
  264. */
  265. function GetAllMessages($order = 5, $receiver = '', $sender = '')
  266. {
  267. global $roster, $addon, $user;
  268. switch( $order )
  269. {
  270. case 0:
  271. $order = '`senderLevel` ASC';
  272. case 1:
  273. $order = '`senderLevel` DESC';
  274. case 2:
  275. $order = '`read` ASC';
  276. case 3:
  277. $order = '`read` DESC';
  278. case 4:
  279. $order = '`date` ASC';
  280. case 5:
  281. $order = '`date` DESC';
  282. }
  283. $where = '';
  284. if(strlen($receiver) > 0 && strlen($sender) > 0)
  285. {
  286. $where = ' AND ';
  287. }
  288. $where = ((strlen($receiver) > 0)?'`uid`=' . $receiver:'') . $where . ((strlen($sender) > 0)?'`sender`=' . $sender:'');
  289. $result = $roster->db->query("SELECT * FROM `".$user->db['message']."` WHERE $where ORDER BY $order");
  290. if( !$result )
  291. {
  292. return 1;
  293. }
  294. $num = $roster->db->num_rows($result);
  295. $message = '';
  296. for($i = 0 ; $i < $num ; $i++ )
  297. {
  298. $row = $roster->db->fetch($result);
  299. $message[$i]['id'] = $row['msgid'];
  300. $message[$i]['reciever'] = $row['uid'];
  301. $message[$i]['sender'] = $row['sender'];
  302. $message[$i]['title'] = $row['title'];
  303. $message[$i]['senderLevel'] = $row['senderLevel'];
  304. $message[$i]['read'] = $row['read'];
  305. $message[$i]['date'] = $row['date'];
  306. }
  307. if( !is_array($message) )
  308. {
  309. $this->err_msg = 2;
  310. }
  311. return $message;
  312. }
  313. /**
  314. * @return Int 0 Deleted succesfully
  315. * @return Int 1 When msgId equal to 0
  316. * @return Int 2 Error while deleting from database
  317. * @param Int $msgId
  318. * @desc Delete message
  319. */
  320. function DeleteMessage($msgId)
  321. {
  322. global $roster, $addon, $user;
  323. if(strlen($msgId) == 0)
  324. {
  325. $this->err_msg = 1;
  326. }
  327. $result = $roster->db->query("DELETE FROM `".$user->db['message']."` WHERE `idMsg`=$msgId");
  328. if($result)
  329. {
  330. $this->err_msg = 0;
  331. }
  332. else
  333. {
  334. $this->err_msg = 2;
  335. }
  336. }
  337. }