api.friendship.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Referal like bonus program for users
  4. */
  5. class FriendshipIsMagic {
  6. /**
  7. * All of available user relations as friend=>parent
  8. *
  9. * @var array
  10. */
  11. protected $allFriends = array();
  12. /**
  13. * System alter.ini config stored as array key=>value
  14. *
  15. * @var array
  16. */
  17. protected $altCfg = array();
  18. /**
  19. * Contains all available users as login=>data
  20. *
  21. * @var array
  22. */
  23. protected $allUsers = array();
  24. /**
  25. * Contains raw payments array
  26. *
  27. * @var array
  28. */
  29. protected $rawPayments = array();
  30. /**
  31. * Friendship payment percent
  32. *
  33. * @var int
  34. */
  35. protected $percent = 0;
  36. /**
  37. * Payment type for friendship payments
  38. *
  39. * @var int
  40. */
  41. protected $payid = 1;
  42. public function __construct() {
  43. $this->loadAltCfg();
  44. $this->setPercent();
  45. $this->setPayid();
  46. $this->loadUsers();
  47. $this->loadFriends();
  48. }
  49. /**
  50. * Loads system alter config
  51. *
  52. * @global object $ubillingConfig
  53. *
  54. * @return void
  55. */
  56. protected function loadAltCfg() {
  57. global $ubillingConfig;
  58. $this->altCfg = $ubillingConfig->getAlter();
  59. }
  60. /**
  61. * Sets payments percent for further usage
  62. *
  63. * @return void
  64. */
  65. protected function setPercent() {
  66. $this->percent = $this->altCfg['FRIENDSHIP_PERCENT'];
  67. }
  68. /**
  69. * Sets payments cashtype
  70. *
  71. * @return void
  72. */
  73. protected function setPayid() {
  74. $this->payid = $this->altCfg['FRIENDSHIP_CASHTYPEID'];
  75. }
  76. /**
  77. * Loads all of existing friends relations from database to protected property
  78. *
  79. * @return void
  80. */
  81. protected function loadFriends() {
  82. $query = "SELECT * from `friendship`";
  83. $all = simple_queryall($query);
  84. if (!empty($all)) {
  85. foreach ($all as $io => $each) {
  86. $this->allFriends[$each['friend']] = $each['parent'];
  87. }
  88. }
  89. }
  90. /**
  91. * Loads all of existing users from database to protected property
  92. *
  93. * @return void
  94. */
  95. protected function loadUsers() {
  96. $all = zb_UserGetAllStargazerData();
  97. if (!empty($all)) {
  98. foreach ($all as $io => $each) {
  99. $this->allUsers[$each['login']] = $each;
  100. }
  101. }
  102. }
  103. /**
  104. * Checks is user allowed to set as someone friend
  105. *
  106. * @param string $login
  107. *
  108. * @return bool
  109. */
  110. protected function isFriendable($login) {
  111. $result = true;
  112. if (isset($this->allFriends[$login])) {
  113. $result = false;
  114. }
  115. return ($result);
  116. }
  117. /**
  118. * Creates friend-parent relationship as database record
  119. *
  120. * @param string $login
  121. * @param string $parentLogin
  122. *
  123. * @return void
  124. */
  125. public function createFriend($login, $parentLogin) {
  126. $loginF = mysql_real_escape_string($login);
  127. $parentLoginF = mysql_real_escape_string($parentLogin);
  128. if ($this->isFriendable($login)) {
  129. if (isset($this->allUsers[$login])) {
  130. if (isset($this->allUsers[$parentLogin])) {
  131. $query = "INSERT INTO `friendship` (`id`, `friend`, `parent`) VALUES (NULL, '" . $loginF . "', '" . $parentLoginF . "'); ";
  132. nr_query($query);
  133. log_register('FRIENDSHIP CREATE `' . $login . '` PARENT `' . $parentLogin . '`');
  134. } else {
  135. log_register('FRIENDSHIP CREATE FAIL `' . $login . '` NOT_EXISTS_PARENT');
  136. }
  137. } else {
  138. log_register('FRIENDSHIP CREATE FAIL `' . $login . '` NOT_EXISTS');
  139. }
  140. } else {
  141. log_register('FRIENDSHIP CREATE FAIL `' . $login . '` BUSY');
  142. }
  143. }
  144. /**
  145. * Removes login from friendship relations
  146. *
  147. * @param string $login
  148. *
  149. * @return void
  150. */
  151. public function deleteFriend($login) {
  152. $loginF = mysql_real_escape_string($login);
  153. if (isset($this->allFriends[$login])) {
  154. $query = "DELETE from `friendship` WHERE `friend`='" . $loginF . "';";
  155. nr_query($query);
  156. log_register('FRIENDSHIP DELETE `' . $login . '`');
  157. } else {
  158. log_register('FRIENDSHIP DELETE FAIL `' . $login . '` NOT_EXISTS');
  159. }
  160. }
  161. /**
  162. * Renders friendship creation form
  163. *
  164. * @param string $parentLogin
  165. * @return string
  166. */
  167. public function renderCreateForm($parentLogin) {
  168. $inputs = wf_HiddenInput('newparentlogin', $parentLogin);
  169. $inputs.= wf_TextInput('newfriendlogin', __('Login'), '', false, '15');
  170. $inputs.= wf_Submit(__('Create'));
  171. $result = wf_Form('', 'POST', $inputs, 'glamour');
  172. return ($result);
  173. }
  174. /**
  175. * Renders list of associated friend users for some parent login
  176. *
  177. * @param string $parentLogin
  178. *
  179. * @return string
  180. */
  181. public function renderFriendsList($parentLogin) {
  182. $result = '';
  183. $allRealnames = zb_UserGetAllRealnames();
  184. $allAddress = zb_AddressGetFulladdresslistCached();
  185. $messages = new UbillingMessageHelper();
  186. $friendCount = 0;
  187. if (!empty($this->allFriends)) {
  188. $cells = wf_TableCell(__('Login'));
  189. $cells.= wf_TableCell(__('Real Name'));
  190. $cells.= wf_TableCell(__('Full address'));
  191. $cells.= wf_TableCell(__('Actions'));
  192. $rows = wf_TableRow($cells, 'row1');
  193. foreach ($this->allFriends as $friendLogin => $parent) {
  194. if ($parent == $parentLogin) {
  195. $cells = wf_TableCell(wf_Link('?module=userprofile&username=' . $friendLogin, web_profile_icon() . ' ' . $friendLogin));
  196. $cells.= wf_TableCell(@$allRealnames[$friendLogin]);
  197. $cells.= wf_TableCell(@$allAddress[$friendLogin]);
  198. $actLinks = wf_JSAlert('?module=pl_friendship&username=' . $parentLogin . '&deletefriend=' . $friendLogin, web_delete_icon(), $messages->getDeleteAlert());
  199. $cells.= wf_TableCell($actLinks);
  200. $rows.= wf_TableRow($cells, 'row2');
  201. $friendCount++;
  202. }
  203. }
  204. if ($friendCount > 0) {
  205. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  206. $result.= __('Total').': '.$friendCount;
  207. } else {
  208. $result = $messages->getStyledMessage(__('Nothing found'), 'info');
  209. }
  210. } else {
  211. $result = $messages->getStyledMessage(__('Nothing found'), 'info');
  212. }
  213. return ($result);
  214. }
  215. /**
  216. Подумай кто твои друзья в этом мире
  217. С кем судьба свела тебя не зря, и мир стал шире
  218. С кем за горизонт готов идти ради света
  219. Кто поможет вовремя найти все ответы
  220. */
  221. /**
  222. * Loads yesterday payments
  223. *
  224. * @return void
  225. */
  226. protected function loadDailyPayments() {
  227. $curdate = curdate();
  228. $query = "SELECT * from `payments` WHERE DATE(`date`) = (CURDATE() - INTERVAL 1 DAY)";
  229. $all = simple_queryall($query);
  230. if (!empty($all)) {
  231. foreach ($all as $io => $each) {
  232. $this->rawPayments[$each['id']] = $each;
  233. }
  234. }
  235. }
  236. /**
  237. * Performs friends yesterday payments processing
  238. *
  239. * @return void
  240. */
  241. public function friendsDailyProcessing() {
  242. $this->loadDailyPayments();
  243. if (!empty($this->rawPayments)) {
  244. foreach ($this->rawPayments as $paymentId => $eachPayment) {
  245. if (isset($this->allFriends[$eachPayment['login']])) {
  246. $friendLogin = $eachPayment['login'];
  247. $parentLogin = $this->allFriends[$eachPayment['login']];
  248. $originalSum = $eachPayment['summ'];
  249. $percent = zb_Percent($originalSum, $this->percent);
  250. zb_CashAdd($parentLogin, $percent, 'add', $this->payid, 'FRIENDSHIP:' . $eachPayment['id']);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. ?>