PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/_lib/dao/class.UserMySQLDAO.php

https://github.com/SimonCoopey/ThinkUp
PHP | 233 lines | 177 code | 15 blank | 41 comment | 29 complexity | c0752f6fcc396fc50da188a1d946caf0 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.UserMySQLDAO.php
  5. *
  6. * Copyright (c) 2009-2016 Gina Trapani
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkup.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * User Data Access Object MySQL Implementation
  25. *
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2016 Gina Trapani
  28. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  29. *
  30. */
  31. class UserMySQLDAO extends PDODAO implements UserDAO {
  32. /**
  33. * Get the SQL to generate average_tweets_per_day number
  34. * @TODO rename "tweets" "posts"
  35. * @return str SQL calcuation
  36. */
  37. private function getAverageTweetCount() {
  38. return "round(post_count/(datediff(curdate(), joined)), 2) as avg_tweets_per_day";
  39. }
  40. public function isUserInDB($user_id, $network) {
  41. $q = "SELECT user_id ";
  42. $q .= "FROM #prefix#users ";
  43. $q .= "WHERE user_id = :user_id AND network = :network;";
  44. $vars = array(
  45. ':user_id'=>(string)$user_id,
  46. ':network'=>$network
  47. );
  48. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  49. $ps = $this->execute($q, $vars);
  50. return $this->getDataIsReturned($ps);
  51. }
  52. public function isUserInDBByName($username, $network) {
  53. $q = "SELECT user_id ";
  54. $q .= "FROM #prefix#users ";
  55. $q .= "WHERE user_name = :username AND network = :network";
  56. $vars = array(
  57. ':username'=>$username,
  58. ':network'=>$network
  59. );
  60. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  61. $ps = $this->execute($q, $vars);
  62. return $this->getDataIsReturned($ps);
  63. }
  64. public function updateUsers($users_to_update) {
  65. $count = 0;
  66. $status_message = "";
  67. if (sizeof($users_to_update) > 0) {
  68. $status_message .= count($users_to_update)." users queued for insert or update; ";
  69. foreach ($users_to_update as $user) {
  70. $count += $this->updateUser($user);
  71. }
  72. $status_message .= "$count users affected.";
  73. }
  74. $this->logger->logInfo($status_message, __METHOD__.','.__LINE__);
  75. $status_message = "";
  76. return $count;
  77. }
  78. public function updateUser($user) {
  79. if (!isset($user->username)) {
  80. return 0;
  81. }
  82. $status_message = "";
  83. $has_friend_count = $user->friend_count != '' ? true : false;
  84. $has_favorites_count = $user->favorites_count != '' ? true : false;
  85. $has_last_post = $user->last_post != '' ? true : false;
  86. $has_last_post_id = $user->last_post_id != '' ? true : false;
  87. $network = $user->network != '' ? $user->network : 'twitter';
  88. $user->follower_count = $user->follower_count != '' ? $user->follower_count : 0;
  89. $user->post_count = $user->post_count != '' ? $user->post_count : 0;
  90. $vars = array(
  91. ':user_id'=>(string)$user->user_id,
  92. ':username'=>$user->username,
  93. ':full_name'=>$user->full_name,
  94. ':avatar'=>$user->avatar,
  95. ':gender'=>$user->gender,
  96. ':birthday'=>$user->birthday,
  97. ':location'=>$user->location,
  98. ':description'=>$user->description,
  99. ':url'=>$user->url,
  100. ':is_verified'=>$this->convertBoolToDB($user->is_verified),
  101. ':is_protected'=>$this->convertBoolToDB($user->is_protected),
  102. ':follower_count'=>$user->follower_count,
  103. ':post_count'=>$user->post_count,
  104. ':found_in'=>$user->found_in,
  105. ':joined'=>$user->joined,
  106. ':network'=>$user->network
  107. );
  108. $user_in_storage = $this->getDetails($user->user_id, $user->network);
  109. if (!isset($user_in_storage)) {
  110. //Insert new user
  111. $q = "INSERT INTO #prefix#users (user_id, user_name, full_name, avatar, gender, birthday,";
  112. $q .= "location, description, url, is_verified, is_protected, follower_count, post_count, ".
  113. ($has_friend_count ? "friend_count, " : "")." ".
  114. ($has_favorites_count ? "favorites_count, " : "")." ".
  115. ($has_last_post ? "last_post, " : "")." found_in, joined, network ".
  116. ($has_last_post_id ? ", last_post_id" : "").") ";
  117. $q .= "VALUES ( :user_id, :username, :full_name, :avatar, :gender, :birthday, :location, :description, ";
  118. $q .= ":url, :is_verified, :is_protected, :follower_count, :post_count, ".
  119. ($has_friend_count ? ":friend_count, " : "")." ".
  120. ($has_favorites_count ? ":favorites_count, " : "")." ".
  121. ($has_last_post ? ":last_post, " : "")." :found_in, :joined, :network ".
  122. ($has_last_post_id ? ", :last_post_id " : "")." )";
  123. } else {
  124. //Update existing user
  125. $q = "UPDATE #prefix#users SET full_name = :full_name, avatar = :avatar, location = :location, ";
  126. $q .= "user_name = :username, description = :description, url = :url, is_verified = :is_verified, ";
  127. $q .= "gender=:gender, birthday=:birthday, ";
  128. $q .= "is_protected = :is_protected, follower_count = :follower_count, post_count = :post_count, ".
  129. ($has_friend_count ? "friend_count= :friend_count, " : "")." ".
  130. ($has_favorites_count ? "favorites_count= :favorites_count, " : "")." ".
  131. ($has_last_post ? "last_post= :last_post, " : "")." last_updated = NOW(), found_in = :found_in, ";
  132. $q .= "joined = :joined, network = :network ".
  133. ($has_last_post_id ? ", last_post_id = :last_post_id" : "")." ";
  134. $q .= "WHERE user_id = :user_id AND network = :network;";
  135. //Capture description version
  136. //If stored description doesn't match the current one, store the new version
  137. if (Utils::stripURLsOutOfText($user_in_storage->description)
  138. != Utils::stripURLsOutOfText($user->description)) {
  139. $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
  140. $user_versions_dao->addVersionOfField($user_in_storage->id, 'description', $user->description);
  141. }
  142. //Capture avatar version
  143. //If stored avatar doesn't match the current one, store the new version
  144. if ($user_in_storage->avatar != $user->avatar) {
  145. if (!isset($user_versions_dao)) {
  146. $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
  147. }
  148. $user_versions_dao->addVersionOfField($user_in_storage->id, 'avatar', $user->avatar);
  149. }
  150. }
  151. if ($has_friend_count) {
  152. $vars[':friend_count'] = $user->friend_count;
  153. }
  154. if ($has_favorites_count) {
  155. $vars[':favorites_count'] = $user->favorites_count;
  156. }
  157. if ($has_last_post) {
  158. $vars[':last_post'] = $user->last_post;
  159. }
  160. if ($has_last_post_id) {
  161. $vars[':last_post_id'] = $user->last_post_id;
  162. }
  163. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  164. $ps = $this->execute($q, $vars);
  165. //If a new user got inserted, add the base bio and avatar into user_versions
  166. if (!isset($user_in_storage)) {
  167. $new_user_id = $this->getInsertId($ps);
  168. $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
  169. $user_versions_dao->addVersionOfField($new_user_id, 'description', $user->description);
  170. $user_versions_dao->addVersionOfField($new_user_id, 'avatar', $user->avatar);
  171. }
  172. $results = $this->getUpdateCount($ps);
  173. return $results;
  174. }
  175. public function getDetails($user_id, $network) {
  176. $q = "SELECT * , ".$this->getAverageTweetCount()." ";
  177. $q .= "FROM #prefix#users u ";
  178. $q .= "WHERE u.user_id = :user_id AND u.network = :network;";
  179. $vars = array(
  180. ':user_id'=>(string)$user_id,
  181. ':network'=>$network
  182. );
  183. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  184. $ps = $this->execute($q, $vars);
  185. return $this->getDataRowAsObject($ps, "User");
  186. }
  187. public function getDetailsByUserKey($user_key) {
  188. $q = "SELECT * , ".$this->getAverageTweetCount()." ";
  189. $q .= "FROM #prefix#users u ";
  190. $q .= "WHERE u.id = :user_key ";
  191. $vars = array( ':user_key'=>(int)$user_key);
  192. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  193. $ps = $this->execute($q, $vars);
  194. return $this->getDataRowAsObject($ps, "User");
  195. }
  196. public function getUserByName($user_name, $network) {
  197. $q = "SELECT * , ".$this->getAverageTweetCount()." ";
  198. $q .= "FROM #prefix#users u ";
  199. $q .= "WHERE u.user_name = :user_name AND u.network = :network";
  200. $vars = array(
  201. ':user_name'=>$user_name,
  202. ':network'=>$network
  203. );
  204. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  205. $ps = $this->execute($q, $vars);
  206. return $this->getDataRowAsObject($ps, "User");
  207. }
  208. public function deleteUsersByHashtagId($hashtag_id){
  209. $q = "DELETE u.* ";
  210. $q .= "FROM #prefix#users u INNER JOIN #prefix#posts t ON u.user_id=t.author_user_id ";
  211. $q .= "INNER JOIN #prefix#hashtags_posts hp ON t.post_id = hp.post_id ";
  212. $q .= "WHERE hp.hashtag_id= :hashtag_id;";
  213. $vars = array(':hashtag_id'=>$hashtag_id);
  214. if ($this->profiler_enabled) { Profiler::setDAOMethod(__METHOD__); }
  215. $ps = $this->execute($q, $vars);
  216. return $this->getDeleteCount($ps);
  217. }
  218. }