Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. <?php
  2. class db_connect {
  3.  
  4. // ---------------------------------------------------------------
  5. // Public functions
  6. // ---------------------------------------------------------------
  7. public function updateDatabase($entry_id, $facebook_id, $table_prefix) {
  8.  
  9. $return['status'] = "";
  10. $return['error_response'] = "";
  11. $return['vote_count'] = "";
  12. $return['date_last_voted'] = "";
  13.  
  14. //echo json_encode($entry_id);
  15.  
  16. /* Connect to mysql */
  17. $servername = "servername";
  18. $username = "username";
  19. $password = "password";
  20. $db_name = "db_name";
  21.  
  22. // Create connection
  23. $conn = new mysqli($servername, $username, $password, $db_name);
  24.  
  25. // Check connection
  26. if ($conn->connect_error) {
  27. die("Connection failed: " . $conn->connect_error);
  28. die("Error : ( $mysqli->connect_errno ) " . $mysqli->connect_error);
  29. }
  30.  
  31. // Main codes here
  32. date_default_timezone_set('Asia/Manila');
  33.  
  34. // Get last vote date: fetch_date_voted returns none if user voted for first time
  35. $current_date = date('Y-m-d');
  36. $last_vote_date = $this->fetch_date_voted($entry_id, $facebook_id, $table_prefix, $conn);
  37. $return['date_last_voted'] = $last_vote_date;
  38.  
  39. if( $last_vote_date && ($last_vote_date == "none" || $last_vote_date < $current_date) ) {
  40. $table_name = $table_prefix . '_voter';
  41. $query = $conn->query("
  42. INSERT INTO $table_name
  43. (facebook_id, entry_id, date_voted)
  44. VALUES
  45. ($facebook_id, $entry_id, '$current_date')
  46. ");
  47.  
  48. // After inserting, the variable $query will return a true status
  49. if ( $query ) {
  50.  
  51.  
  52. //If the data fetched is right, it will update
  53. $update = $this->update_count($entry_id, $table_prefix, $conn);
  54.  
  55. //if condition is successful, it will update the vote_count
  56. if($update) {
  57.  
  58. $fetched_data = $this->fetch_current_count($entry_id, $table_prefix, $conn);
  59.  
  60. if ($fetched_data['status']) {
  61. $return['status'] = true;
  62. $return['vote_count'] = $fetched_data['vote_count'];
  63.  
  64. } else {
  65. $return['status'] = false;
  66. }
  67. } else {
  68. $return['status'] = false;
  69. }
  70.  
  71. } else {
  72.  
  73. die('Error : ('. $conn->errno .') '. $conn->error);
  74. $return['status'] = false;
  75. $return['error_response'] = $conn->error;
  76.  
  77. }
  78. } else {
  79. $return['status'] = false;
  80. $return['error_response'] = 'date_invalid';
  81.  
  82. }
  83.  
  84.  
  85. echo json_encode($return);
  86.  
  87. //$query->close();
  88. $conn->close();
  89. }
  90.  
  91. public function fetchDatabase($entry_id, $table_prefix) {
  92. $return['status'] = "";
  93. $return['error_response'] = "";
  94. $return['vote_count'] = "";
  95.  
  96. /* Connect to mysql */
  97. $servername = "servername";
  98. $username = "username";
  99. $password = "password";
  100. $db_name = "db_name";
  101.  
  102. // Create connection
  103. $conn = new mysqli($servername, $username, $password, $db_name);
  104.  
  105. // Check connection
  106. if ($conn->connect_error) {
  107. die("Connection failed: " . $conn->connect_error);
  108. die("Error : ( $mysqli->connect_errno ) " . $mysqli->connect_error);
  109. }
  110.  
  111. $fetched_data = $this->fetch_current_count($entry_id, $table_prefix, $conn);
  112.  
  113. $return['status'] = true;
  114.  
  115. if ($fetched_data['status']) {
  116. $return['vote_count'] = $fetched_data['vote_count'];
  117. } else {
  118. $return['vote_count'] = 0;
  119. }
  120.  
  121. echo json_encode($return);
  122.  
  123. //$query->close();
  124. $conn->close();
  125. }
  126.  
  127. // ---------------------------------------------------------------
  128. // Private functions
  129. // ---------------------------------------------------------------
  130. function fetch_current_count($entry_id, $table_prefix, $conn){
  131. $return['status'] = "";
  132. $return['vote_count'] = "";
  133.  
  134. $table_name = $table_prefix . '_entry';
  135. $select = $conn->query("SELECT * FROM $table_name WHERE entry_id = $entry_id");
  136.  
  137. if ($select->num_rows > 0) {
  138.  
  139. $return['status'] = true;
  140.  
  141. while($row = $select->fetch_assoc()) {
  142.  
  143. $return['vote_count'] = $row["vote_count"];
  144.  
  145. }
  146.  
  147. } else {
  148.  
  149. $return['status'] = false;
  150.  
  151. }
  152.  
  153. return $return;
  154. }
  155.  
  156. function update_count($entry_id, $table_prefix, $conn ){
  157. $table_name = $table_prefix . '_entry';
  158. $update = $conn->query("
  159. INSERT INTO $table_name( entry_id, vote_count )
  160. VALUES ( $entry_id, 1 )
  161. ON DUPLICATE KEY UPDATE vote_count = vote_count + 1
  162. ");
  163.  
  164. if ($update) {
  165. return true;
  166. } else {
  167. return false;
  168. }
  169.  
  170. }
  171.  
  172. function fetch_date_voted($entry_id, $facebook_id, $table_prefix, $conn) {
  173. $table_name = $table_prefix . '_voter';
  174. $query = $conn->query("
  175. SELECT max(date_voted) FROM $table_name
  176. WHERE facebook_id=$facebook_id AND entry_id=$entry_id
  177. ");
  178.  
  179. if ( $query ) {
  180. $row = $query->fetch_array(MYSQLI_NUM);
  181.  
  182. if ( $row[0] ) {
  183. // do something
  184. return $row['date_voted'];
  185. }
  186. else {
  187. // do something else
  188. return "none";
  189. }
  190. } else {
  191. die('Error : ('. $conn->errno .') '. $conn->error);
  192. }
  193.  
  194. return "none";
  195. }
  196.  
  197. }
  198.  
  199. // This will check if there is a POST data value
  200. // If there's a value, it will initialize then call the'updateDatabase'
  201. // then will pass the data of 'ENTRY ID' and FACEBOOK ID
  202. if( isset( $_POST['mode'] ) ) {
  203. switch( $_POST['mode'] ) {
  204. case 'update':
  205. if(isset( $_POST['entry_id'] ) && isset( $_POST['facebook_id'] ) && isset( $_POST['table_prefix'] ) ) {
  206.  
  207. $updateClass = new db_connect();
  208. $entry_id = $_POST['entry_id'];
  209. $facebook_id = $_POST['facebook_id'];
  210. $table_prefix = $_POST['table_prefix'];
  211.  
  212. $updateClass->updateDatabase($entry_id, $facebook_id, $table_prefix);
  213.  
  214. } else {
  215. echo json_encode("Incomplete Parameters");
  216. }
  217. break;
  218.  
  219. case 'fetch':
  220. if(isset( $_POST['entry_id'] ) && isset( $_POST['table_prefix'] ) ) {
  221. $retrieveClass = new db_connect();
  222. $entry_id = $_POST['entry_id'];
  223. $table_prefix = $_POST['table_prefix'];
  224. } else {
  225. echo json_encode("Incomplete Parameters");
  226. }
  227.  
  228. $retrieveClass->fetchDatabase($entry_id, $table_prefix);
  229. break;
  230. }
  231.  
  232.  
  233.  
  234. } else {
  235. echo json_encode("Please provide mode ( update or fetch )");
  236. }
  237.  
  238.  
  239. ?>
  240.  
  241. $(function() {
  242. initOverlay();
  243.  
  244. $('a.share-button').click(function (event){
  245. postToFeed();
  246. });
  247.  
  248. // Get current votes
  249. var entry_id = $('.vote-button').attr('rel');
  250. console.log('------------------ '+entry_id);
  251.  
  252. $.ajax({
  253. method: "POST",
  254. url: "https://url",
  255. data: {
  256. mode: 'fetch',
  257. table_prefix: 'photo',
  258. entry_id: entry_id
  259. },
  260.  
  261. success: function(data) {
  262. //console.log(data[0]['status']);
  263. console.log( data );
  264.  
  265. var val = JSON.parse(data);
  266. // console.log(val.status);
  267. if (val.status) {
  268. $('#vote-count').html( val.vote_count );
  269. // console.log("VOTE COUNT: " + val.vote_count);
  270. }
  271.  
  272. },
  273.  
  274. error: function(e){
  275. //console.log(e);
  276. }
  277. });
  278.  
  279.  
  280. // Here's the click function that will POST data using AJAX
  281. $('.vote-button').click(function(){
  282. //These 2 variables will get the entry id at the voter's facebook id
  283. var facebook_id = $("#facebook_id").val();
  284.  
  285. // It will pass the variables on AJAX Call
  286. $.ajax({
  287. method: "POST",
  288. url: "https://url",
  289. data: {
  290. mode: 'update',
  291. table_prefix: 'photo',
  292. entry_id: entry_id,
  293. facebook_id: facebook_id
  294. },
  295.  
  296. success: function(data) {
  297. //console.log(data[0]['status']);
  298. console.log( data );
  299.  
  300. var val = JSON.parse(data);
  301. //console.log(val.status);
  302. if (val.status) {
  303. $('#vote-count').html( val.vote_count );
  304.  
  305. setActiveLightbox(0);
  306. showLightBox(true);
  307. // console.log("VOTE COUNT: " + val.vote_count);
  308. } else if ( val.error_response == 'date_invalid' ) {
  309. setActiveLightbox(1);
  310. showLightBox(true);
  311. }
  312.  
  313. },
  314.  
  315. error: function(e){
  316. setActiveLightbox(2);
  317. showLightBox(true);
  318. }
  319. });
  320. })
  321. });
  322.  
  323. strotime($last_vote_date) < strotime($current_date)
  324.  
  325. (strtotime($last_vote_date) + (60*60*24)) < strtotime($current_date)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement