Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.43 KB | None | 0 0
  1. <?php
  2. /*The business.php contains the business class and methods
  3. * which will simplify the operations that are
  4. * associated with business.
  5. *
  6. * definelabs.com
  7. *
  8. */
  9. require_once (APP_ROOT."include/session.php");
  10. require_once (APP_ROOT."include/category.php");
  11. require_once (APP_ROOT."push/pushAPI.php");
  12. require_once (APP_ROOT."time.php");
  13. require_once (APP_ROOT."timezone.php");
  14.  
  15.  
  16. class Business {
  17. public $info = array();
  18. public $totaldeals;
  19. public $deals = array();
  20. public $id;
  21. public $timezoneoffset;
  22.  
  23. function __Construct() {
  24. global $session, $database, $form;
  25. $q = sprintf("SELECT * FROM business WHERE userid= '%s'", mysql_real_escape_string($session -> userid));
  26. $result = $database -> query($q) or die(mysql_error());
  27. $info = mysql_fetch_assoc($result);
  28. $this -> info = $info;
  29. $this -> id = $info['businessid'];
  30. $timezone = $info['timezone'];
  31. if (isset($timezone) && strlen($timezone) !== 0) {
  32. $this -> timezoneoffset = getTimeZoneOffset($timezone);
  33. } else {
  34. $this -> timezoneoffset = "36000";
  35. }
  36. }
  37.  
  38. //function to add new business if the advertiser have not added any.
  39.  
  40. function addNewBusiness($bname, $street, $suburb, $city, $state, $zipcode, $desc, $lattitude, $longitude, $website, $phone, $timezone, $catid, $selectedcategory) {
  41.  
  42. global $session, $form, $database;
  43. //Validation for business name
  44. $field = "bname";
  45. if (!$bname || strlen($bname = trim($bname)) == 0) {
  46. $form -> setError($field, " * Business name is required");
  47. }
  48.  
  49. $field = "suburb";
  50. if (!($suburb) || strlen($suburb = trim($suburb)) == 0) {
  51. $form -> setError($field, "* Suburb is not mentioned");
  52. }
  53.  
  54. $field = "street";
  55. if (!$street || strlen($street = trim($street)) == 0) {
  56. $form -> setError($field, "* Street is not mentioned");
  57. }
  58.  
  59. //validation for city
  60. $field = "city";
  61. if (!$city || strlen($city = trim($city)) == 0) {
  62. $form -> setError($field, "* city is not mentioned");
  63. }
  64.  
  65. //validation for state
  66. $field = "state";
  67. if (!$state || strlen($state = trim($state)) == 0) {
  68. $form -> setError($field, "* state is not mentioned");
  69. }
  70.  
  71. //validation for postal code
  72. $field = "zipcode";
  73. if (!$zipcode || strlen($zipcode = trim($zipcode)) == 0) {
  74. $form -> setError($field, "* Please enter correct zipcode");
  75. }
  76.  
  77. if (strlen($zipcode = trim($zipcode)) !== 4) {
  78. $form -> setError($field, "* Please enter correct zipcode");
  79. } else if (!preg_match("/^[0-9]+$/", $zipcode)) {
  80. $form -> setError($field, "* Zip code can only be Numbers");
  81. }
  82.  
  83. //validation for lattitude
  84. $field = "lattitude";
  85. if (!$lattitude || strlen($lattitude = trim($lattitude)) == 0) {
  86. $form -> setError($field, "* Please enter Latitude");
  87. } else if (!preg_match("/^[\-0-9]+[\.][0-9]+$/", $lattitude)) {
  88. $form -> setError($field, "* Please enter correct lattitude");
  89. }
  90.  
  91. $field = "phone";
  92. if (!$phone || strlen($phone = trim($phone)) == 0) {
  93. $form -> setError($field, "* Please enter your business phone number");
  94. }
  95. else if (!preg_match("/^[0-9a-z A-Z _ \- \+ \( \)]+$/", $phone)) {
  96. $form -> setError($field, "* Please enter valid phone number.");
  97. }
  98.  
  99. //validation for longitude
  100. $field = "longitude";
  101. if (!$longitude || strlen($longitude = trim($longitude)) == 0) {
  102. $form -> setError($field, "* Please enter Longitude");
  103. } else if (!preg_match("/^[\-0-9]+[\.][0-9]+$/", $longitude)) {
  104. $form -> setError($field, "* Please enter correct Longitude");
  105. }
  106.  
  107. //validation for website
  108. $field = "website";
  109. if (!$website || strlen($website = trim($website)) == 0) {
  110. $form -> setError($field, "* Please enter your business website");
  111. }
  112.  
  113. if (!isValidURL($website)) {
  114. $form -> setError($field, "* Website is invalid!");
  115. }
  116.  
  117.  
  118. $field = "category";
  119. if (!$catid || strlen($catid = trim($catid)) == 0) {
  120. $form -> setError($field, "* Select a deal category" . $catid);
  121. }
  122.  
  123. //Validation for the business description... So users can get a little idea about the business.
  124.  
  125. $field = "description";
  126. if (!$desc || strlen($desc = trim($desc)) == 0) {
  127. $form -> setError($field, "* You have to tell people a little bit about your business");
  128. }
  129.  
  130. if ($form -> num_errors > 0) {
  131. return 2;
  132. }
  133.  
  134. // $userid=$session->userid;
  135.  
  136. $connection = $database -> connection;
  137.  
  138. //business id generation.
  139. $businessid = $session -> generateRandID();
  140.  
  141. unset($_SESSION['business']);
  142.  
  143. $_SESSION['business'] = array("businessid" => $businessid, "name" => $bname, "street" => $street, "suburb" => $suburb, "city" => $city, "state" => $state, "zipcode" => $zipcode, "description" => $desc, "latitude" => $lattitude, "longitude" => $longitude, "website" => $website, "phone" => $phone, "timezone" => $timezone, "catid" => $catid, "selectedcategory" => $selectedcategory);
  144.  
  145. return $businessid;
  146. }
  147.  
  148. //This function will add new deal for an advertiser.
  149. function addNewDeal($dealname, $dealtime, $expirytime, $about, $sendto) {
  150.  
  151. global $session, $form, $database, $mailer;
  152.  
  153. //validation for deal name
  154. $field = "dealname";
  155. if (!$dealname || strlen($dealname = trim($dealname)) == 0) {
  156. $form -> setError($field, " * Deal name is not entered");
  157. }
  158. // else{
  159. // $field="dealname";
  160. // if(!preg_match("/^([0-9 a-z A-Z % \.\$])+$/", $dealname)){
  161. // $form->setError($field, "* Deal name can only be alphanumeric");
  162. // }
  163. // }
  164. //
  165. //validation for deal's date
  166. // $field = "dealdate";
  167. // if(!$dealdate || strlen($dealdate = trim($dealdate)) == 0 ){
  168. // $form->setError($field, " * Deal date is required");
  169. // }
  170. // else{
  171. // $field="dealdate";
  172. // if(!preg_match("/^([0-9\-\/])+$/", $dealdate)){
  173. // $form->setError($field, "* Deal date can only be Numbers");
  174. // }
  175. // }
  176.  
  177. // //validation for deal's time
  178. // $field = "dealtime";
  179. // if(!$dealtime || strlen($dealtime = trim($dealtime)) == 0 ){
  180. // $form->setError($field, " * Deal time is required");
  181. // }
  182. // else{
  183. // $field="dealtime";
  184. // if(!preg_match("/^([0-9:])+$/", $dealtime)){
  185. // $form->setError($field, "* Deal time can only be Numbers");
  186. // }
  187. // }
  188. //
  189. // //validation for expiry date
  190. // $field="expirydate";
  191. // if(!preg_match("/^([0-9\-\/])+$/", $expirydate)){
  192. // $form->setError($field, "* Expiry date can only be Numbers");
  193. // }
  194. //
  195.  
  196. // //validation for expiry time
  197. // $field = "expirytime";
  198. // if(!$expirytime || strlen($expirytime = trim($expirytime)) == 0 ){
  199. // $form->setError($field, " * Expiry time is required");
  200. // }
  201. // else{
  202. // $field="exprirytime";
  203. // if(!preg_match("/^([0-9:])+$/", $expirytime)){
  204. // $form->setError($field, "* Expiry time can only be Numbers");
  205. // }
  206. // }
  207.  
  208. //validation for the deal description
  209. $field = "about";
  210.  
  211. if (!$about || strlen($about = trim($about)) == 0) {
  212. $form -> setError($field, " * Deal Description is required");
  213. }
  214. // else{
  215. // $field="about";
  216. // if(!preg_match("/^([a-z A-Z 0-9 \% \.])+$/", $about)){
  217. // $form->setError($field, "* Deal description can only be Numbers and letters");
  218. // }
  219. // }
  220. //
  221. $field = "submittedcat";
  222.  
  223. //$dealid = $session->generateRandID();
  224. $businessid = $this -> info['businessid'];
  225. error_log("business id ".$businessid);
  226.  
  227. if ($form -> num_errors > 0) {
  228. return 2;
  229. }
  230. // Convert deal name to uppercse
  231. $dealname = strtoupper($dealname);
  232. if ($database -> addNewDeal($businessid, $about, $dealtime, $expirytime, $dealname, $sendto)) {
  233. $dealid = mysql_insert_id();
  234.  
  235. //automaticaly send push if deal is created
  236. $info = $this -> getBusinessInfo($businessid);
  237. $dealOwnerUserId=$info['userid'];
  238. error_log("asurabh".$dealOwnerUserId);
  239. error_log($dealtime);
  240. $userinfo = getUserInfoById($dealOwnerUserId);
  241. //$mail=$mailer->sendApproveDeal($userinfo['email'], $info['bname'],$_POST['dealname'],$userinfo['firstname']);
  242. $resultPush = pushBusiness($businessid, $dealname, $dealid, $dealtime, $sendto);
  243.  
  244. return $dealid;
  245. } else {
  246. return 1;
  247. }
  248. }
  249.  
  250.  
  251. function addNewDealSales($dealname, $dealtime, $expirytime, $about,$businessid) {
  252.  
  253. global $session, $form, $database,$mailer;
  254.  
  255. //validation for deal name
  256. $field = "dealname";
  257. if (!$dealname || strlen($dealname = trim($dealname)) == 0) {
  258. $form -> setError($field, " * Deal name is not entered");
  259. }
  260. // else{
  261. // $field="dealname";
  262. // if(!preg_match("/^([0-9 a-z A-Z % \.\$])+$/", $dealname)){
  263. // $form->setError($field, "* Deal name can only be alphanumeric");
  264. // }
  265. // }
  266. //
  267. //validation for deal's date
  268. // $field = "dealdate";
  269. // if(!$dealdate || strlen($dealdate = trim($dealdate)) == 0 ){
  270. // $form->setError($field, " * Deal date is required");
  271. // }
  272. // else{
  273. // $field="dealdate";
  274. // if(!preg_match("/^([0-9\-\/])+$/", $dealdate)){
  275. // $form->setError($field, "* Deal date can only be Numbers");
  276. // }
  277. // }
  278.  
  279. // //validation for deal's time
  280. // $field = "dealtime";
  281. // if(!$dealtime || strlen($dealtime = trim($dealtime)) == 0 ){
  282. // $form->setError($field, " * Deal time is required");
  283. // }
  284. // else{
  285. // $field="dealtime";
  286. // if(!preg_match("/^([0-9:])+$/", $dealtime)){
  287. // $form->setError($field, "* Deal time can only be Numbers");
  288. // }
  289. // }
  290. //
  291. // //validation for expiry date
  292. // $field="expirydate";
  293. // if(!preg_match("/^([0-9\-\/])+$/", $expirydate)){
  294. // $form->setError($field, "* Expiry date can only be Numbers");
  295. // }
  296. //
  297.  
  298. // //validation for expiry time
  299. // $field = "expirytime";
  300. // if(!$expirytime || strlen($expirytime = trim($expirytime)) == 0 ){
  301. // $form->setError($field, " * Expiry time is required");
  302. // }
  303. // else{
  304. // $field="exprirytime";
  305. // if(!preg_match("/^([0-9:])+$/", $expirytime)){
  306. // $form->setError($field, "* Expiry time can only be Numbers");
  307. // }
  308. // }
  309.  
  310. //validation for the deal description
  311. $field = "about";
  312.  
  313. if (!$about || strlen($about = trim($about)) == 0) {
  314. $form -> setError($field, " * Deal Description is required");
  315. }
  316. // else{
  317. // $field="about";
  318. // if(!preg_match("/^([a-z A-Z 0-9 \% \.])+$/", $about)){
  319. // $form->setError($field, "* Deal description can only be Numbers and letters");
  320. // }
  321. // }
  322. //
  323. $field = "submittedcat";
  324.  
  325. //$dealid = $session->generateRandID();
  326.  
  327.  
  328. if ($form -> num_errors > 0) {
  329. return 2;
  330. }
  331. // Convert deal name to uppercse
  332. $dealname = strtoupper($dealname);
  333. if ($database -> addNewDeal($businessid, $about, $dealtime, $expirytime, $dealname)) {
  334. $dealid = mysql_insert_id();
  335.  
  336. //if created by moderator or admin
  337. if($session->userlevel == 9 || $session->userlevel == 7 ){
  338. $businessinfo = getBusinessDetail($businessid);
  339. $dealOwnerUserId = $businessinfo['userid'];
  340. error_log("asurabh".$dealOwnerUserId);
  341. error_log($dealtime);
  342. $userinfo = getUserInfoById($dealOwnerUserId);
  343. //$mail=$mailer->sendApproveDeal($userinfo['email'], $businessinfo['bname'],$dealname,$userinfo['firstname']);
  344. $resultPush = pushBusiness($businessid, $dealname, $dealid, $dealtime);
  345. //error_log($resultPush);
  346. }
  347.  
  348. return $dealid;
  349. } else {
  350. return 1;
  351. }
  352. }
  353.  
  354.  
  355. // function addNewCat will add a new Category whenever applicable
  356. function addNewCat($newcategory) {
  357. global $database;
  358.  
  359. $q = sprintf("insert into category values('','%s')", mysql_real_escape_string($newcategory));
  360.  
  361. return $database -> query($q) or die(mysql_error());
  362.  
  363. }
  364.  
  365. function addCat($dealid, $catid) {
  366. global $database;
  367. $q = sprintf("insert into dealcat values('%s','$catid')", mysql_real_escape_string($dealid));
  368.  
  369. return $database -> query($q) or die(mysql_error());
  370. }
  371.  
  372. function updateCat($dealid, $catid, $oldcatid) {
  373. global $database;
  374. $q = sprintf("update dealcat set dealcat.catid = '%s' where dealid = '%s')", mysql_real_escape_string($catid), mysql_real_escape_string($dealid));
  375. $database -> query($q) or die(mysql_error());
  376.  
  377. $q = sprintf("update catsubcat set catsubcat.catid='%s' where catid='%s'", mysql_real_escape_string($catid), mysql_real_escape_string($oldcatid));
  378. $database -> query($q) or die(mysql_error());
  379.  
  380. }
  381.  
  382. function addNewSubCat($newsubcategory) {
  383.  
  384. global $database;
  385. $q = sprintf("insert into subcategory values('','%s')", mysql_real_escape_string($newsubcategory));
  386.  
  387. return $database -> query($q) or die(mysql_error());
  388.  
  389. }
  390.  
  391. function addSubCat($catid, $subcatid, $dealid) {
  392. global $database;
  393.  
  394. $q = sprintf("insert into catsubcat values('%s','%s')", mysql_real_escape_string($catid), mysql_real_escape_string($subcatid));
  395. $database -> query($q) or die(mysql_error());
  396.  
  397. $q = sprintf("insert into dealsubcat values('%s','$subcatid')", mysql_real_escape_string($dealid));
  398. $database -> query($q) or die(mysql_error());
  399. }
  400.  
  401. function updateSubCat($catid, $subcatid, $dealid, $oldsubcatid) {
  402. global $database;
  403. $q = sprintf("update catsubcat set catsubcat.subcatid='%s' where subcatid='%s'", mysql_real_escape_string($subcatid), mysql_real_escape_string($oldsubcatid));
  404. $database -> query($q) or die(mysql_error());
  405.  
  406. $q = sprintf("update dealsubcat set dealsubcat.subcatid ='%s' where dealid='%s'", mysql_real_escape_string($subcatid), mysql_real_escape_string($dealid));
  407. $database -> query($q) or die(mysql_error());
  408.  
  409. }
  410.  
  411. function getCatId($category) {
  412. global $database;
  413. $q = sprintf("select catid from category where categoryName = '%s'", mysql_real_escape_string($category));
  414.  
  415. $array = $database -> query($q);
  416. $result = mysql_fetch_array($array);
  417. $catid = $result['catid'];
  418.  
  419. return $catid;
  420.  
  421. }
  422.  
  423. function getSubCatId($subcategory) {
  424. global $database;
  425. $q = sprintf("select subcatid from subcategory where subcat = '%s'", mysql_real_escape_string($subcategory));
  426.  
  427. $array = $database -> query($q) or die(mysql_error());
  428. $result = mysql_fetch_array($array);
  429. $subcatid = $result['subcatid'];
  430.  
  431. return $subcatid;
  432. }
  433.  
  434. function getCat($catid) {
  435. global $database;
  436.  
  437. $q = sprintf("select * from category where catid = '%d'", $catid);
  438.  
  439. $array = $database -> query($q) or die(mysql_error());
  440. $result = mysql_fetch_assoc($array);
  441. $cat = $result['categoryName'];
  442.  
  443. return $cat;
  444. }
  445.  
  446. function getSubCat($subcatid) {
  447. global $database;
  448.  
  449. $q = sprintf("select subcat from subcategory where subcatid = '%s'", mysql_real_escape_string($subcatid));
  450.  
  451. $array = $database -> query($q) or die(mysql_error());
  452. $result = mysql_fetch_array($array);
  453. $subcat = $result['subcat'];
  454.  
  455. return $subcat;
  456. }
  457.  
  458. function getDealInfo($dealid) {
  459. global $database;
  460.  
  461. $q = sprintf("SELECT deals.*, business.*
  462. FROM deals LEFT JOIN business ON deals.businessid = business.businessid
  463. WHERE dealid = '%d'", $dealid);
  464. $array = $database -> query($q) or die(mysql_error());
  465. $result = mysql_fetch_assoc($array);
  466. $result['dealname'] = stripslashes($result['dealname']);
  467. $result['desc'] = stripslashes($result['desc']);
  468.  
  469. return $result;
  470. }
  471.  
  472. //To obtain a businessid from a corresponding deal.
  473. function getBusinessId($dealid) {
  474. global $database;
  475. $q = sprintf("select businessid from deals where dealid = '%s'", mysql_real_escape_string($dealid));
  476. $array = $database -> query($q) or die(mysql_error());
  477. $result = mysql_fetch_array($array);
  478. return $result['businessid'];
  479.  
  480. }
  481.  
  482. //This function is required for the iphone app. when the iphone app wants to get deal info. It will get the business info by quering the
  483. //the business table using the businessid
  484. //from the deals table.
  485.  
  486. function getBusinessInfo($businessid) {
  487. global $database;
  488. $q = sprintf("select * from business LEFT JOIN business_timing ON business.businessid = business_timing.business_id where business.businessid= '%d'", mysql_real_escape_string($businessid));
  489. $array = $database -> query($q) or die(mysql_error());
  490. $result = mysql_fetch_array($array, MYSQL_ASSOC);
  491. return $result;
  492. }
  493.  
  494. function updateDeal($dealname, $desc, $dealtime, $expirytime, $dealoffer, $dealid) {
  495. global $database;
  496.  
  497. $q = sprintf("UPDATE deals SET deals.dealname='%s', deals.desc='%s', deals.dealtimestamp='%s',deals.expirytimestamp='%s',
  498. deals.dealoffer='$dealoffer' WHERE deals.dealid='%s'", mysql_real_escape_string($dealname), mysql_real_escape_string($desc), mysql_real_escape_string($dealtime), mysql_real_escape_string($expirytime), mysql_real_escape_string($dealid));
  499.  
  500. $result = $database -> query($q) or die(mysql_error());
  501. return $result;
  502.  
  503. }
  504.  
  505. function addNewProduct($productname, $productprice, $productdetail) {
  506. global $database;
  507.  
  508. $q = sprintf("INSERT into PRODUCTS VALUES('','%s','%s','$productprice')", mysql_real_escape_string($productname), mysql_real_escape_string($productdetail));
  509.  
  510. $result = $database -> query($q) or die(mysql_error());
  511. return $result;
  512. }
  513.  
  514. function isSubscribed() {
  515. global $database;
  516.  
  517. $q = sprintf("select * from subscription where businessid = '%s'", mysql_real_escape_string($this -> id));
  518. $array = $database -> query($q) or die(mysql_error());
  519. if (mysql_num_rows($array) < 1) {
  520. return false;
  521. } else {
  522. $data = mysql_fetch_assoc($array);
  523. $rebillID = $data['rebill_customer_id'];
  524. return $rebillID;
  525. }
  526. }
  527.  
  528. function getProductInfo($productid) {
  529. global $database;
  530.  
  531. $q = "select * from products where product_id='$productid'";
  532. $array = $database -> query($q) or die(mysql_error());
  533. $result = mysql_fetch_array($array);
  534. return $result;
  535. }
  536.  
  537. function getDealImage($dealid) {
  538. global $database;
  539.  
  540. $q = sprintf("select * from dealimage where dealid = '%s'", mysql_real_escape_string($dealid));
  541. $result = $database -> query($q) or die(mysql_error);
  542.  
  543. if (mysql_num_rows($result) < 1) {
  544. $location = "../images/deals/default.png";
  545. return $location;
  546. } else {
  547. $name = mysql_fetch_array($result);
  548. $location = $name['directory'] . $name['imagename'];
  549. return $location;
  550.  
  551. }
  552. }
  553.  
  554. function getBusinessImage($businessid) {
  555. global $database;
  556.  
  557. $q = sprintf("select * from businessimage where businessid = '%s'", mysql_real_escape_string($businessid));
  558. $result = $database -> query($q) or die(mysql_error);
  559.  
  560. if (mysql_num_rows($result) < 1) {
  561. $location = "../images/business/default.jpg";
  562. return $location;
  563. } else {
  564. $name = mysql_fetch_array($result);
  565. $location = $name['directory'].$name['imagename'];
  566. return $location;
  567.  
  568. }
  569.  
  570. }
  571.  
  572. /*-----To get all info regarding the saved business------*/
  573.  
  574. function isRevision($dealid, $token, $userid) {
  575. global $database;
  576.  
  577. $q = sprintf("SELECT * from pushed WHERE dealid = '%d' and device_token = '%s' and userid='%s'", $dealid, mysql_real_escape_string($token), mysql_real_escape_string($userid));
  578.  
  579. $result = mysql_num_rows($database -> query($q));
  580.  
  581. if ($result > 0) {
  582. return true;
  583. } else {
  584. return false;
  585. }
  586. }
  587.  
  588. function getSavedBusiness($userid) {
  589. global $database;
  590.  
  591. $q = sprintf("SELECT savedbusiness.*, business.*, businessimage.directory, businessimage.imagename FROM
  592. (savedbusiness LEFT JOIN business ON savedbusiness.businessid = business.businessid) LEFT JOIN businessimage
  593. ON savedbusiness.businessid = businessimage.businessid WHERE savedbusiness.userid = '%s'", mysql_real_escape_string($userid));
  594.  
  595. $result = $database -> query($q) or die(mysql_error());
  596. while ($info = mysql_fetch_assoc($result)) {
  597. $infoFinal[] = $info;
  598. }
  599. return $infoFinal;
  600. }
  601.  
  602. //---Return all the catids of the saved categories as well as the sibling categories of the category---//
  603. function getSavedCategories($userid) {
  604. global $database;
  605.  
  606. $q = sprintf("SELECT * from user_category WHERE userid = '%s'", mysql_real_escape_string($userid));
  607. $result = $database -> query($q) or die(mysql_error());
  608.  
  609. $catIds = array();
  610. $ids = array();
  611. $id = array();
  612.  
  613. while ($info = mysql_fetch_assoc($result)) {
  614. $catIds[] = $info['catid'];
  615.  
  616. }
  617.  
  618. if (!empty($catIds)) {
  619. foreach ($catIds as $catid) {
  620. $ids[] = getTree($catid);
  621. }
  622. } else {
  623. $ids[] = getTree(0);
  624. }
  625.  
  626. foreach ($ids as $array) {
  627. foreach ($array as $key => $value) {
  628. $id[] = $value;
  629. }
  630. }
  631. $filtArray = array_unique($id);
  632. return $filtArray;
  633. //Returned the unique array of catids.
  634. }
  635.  
  636. function getDealsOfSavedCategories($catids, $token, $userid) {
  637. echo "In getDealsOfSavedCategories Function<br />";
  638. global $database, $dealids;
  639. foreach ($catids as $catid) {
  640. $query = "SELECT businessid FROM business WHERE catid = '$catid'";
  641. $resultid = $database -> query($query) or die(mysql_error());
  642.  
  643. while ($ids = mysql_fetch_assoc($resultid)) {
  644. $businessids[] = $ids['businessid'];
  645. }
  646.  
  647. }
  648.  
  649. foreach ($businessids as $businessid) {
  650. $time = gmdate("Y-m-d H:i:s");
  651. $q = sprintf("SELECT dealid FROM deals WHERE businessid = '%s' AND deals.expirytimestamp >= '$time' AND UTC_TIMESTAMP() >= deals.dealtimestamp AND deals.status='1' ", mysql_real_escape_string($businessid));
  652. $result = $database -> query($q);
  653. while ($ids2 = mysql_fetch_assoc($result)) {
  654. var_dump($ids2);
  655. if (!$this -> isRevision($ids2['dealid'], $token, $userid)) {
  656. $dealids[] = $ids2['dealid'];
  657. }
  658. }
  659.  
  660. }
  661. return $dealids;
  662. }
  663.  
  664. function getDealsUnderRadius($dealids, $userLat, $userLong) {
  665. echo "<br /> In Radius Function<br />";
  666. global $database;
  667. foreach ($dealids as $dealid) {
  668. $q = sprintf("SELECT deals.businessid, business.lattitude, business.longitude, business.radius FROM
  669. deals LEFT JOIN business ON deals.businessid = business.businessid WHERE deals.dealid = '%d'", $dealid);
  670. $result = $database -> query($q) or die(mysql_error());
  671. $info = mysql_fetch_assoc($result);
  672. $lat = $info['lattitude'];
  673. $long = $info['longitude'];
  674. $radius = $info['radius'];
  675. $distance = getDistanceBetweenPoints($userLat, $userLong, $lat, $long, $unit = 'Km');
  676. if ($distance <= $radius) {
  677. if ($distance < 1) {
  678. $distance = $distance * 1000;
  679. $distance = $distance . " m";
  680. } else {
  681. $distance = $distance . " Km";
  682. }
  683. $pushdealIds[] = array("dealid" => $dealid, "distance" => $distance);
  684. }
  685.  
  686. }
  687. return $pushdealIds;
  688. }
  689.  
  690. function getDealsBySavedCategory($userid, $userLat, $userLong, $token) {
  691. echo "<br /> In getDealsBySavedCategory Function<br />";
  692. global $database;
  693.  
  694.  
  695. $catids = $this -> getSavedCategories($userid);
  696.  
  697. if (count($catids) != 0) {
  698. $dealids = $this->getDealsOfSavedCategories($catids, $token, $userid);
  699. }
  700.  
  701. if (count($dealids) > 0) {
  702. $deals = $this->getDealsUnderRadius($dealids, $userLat, $userLong);
  703. }
  704. return $deals;
  705. }
  706.  
  707. function getDealsUpdatePush($userid, $userLat, $userLong, $token) {
  708. global $database;
  709.  
  710. $catids = $this -> getSavedCategories($userid);
  711. if (count($catids) != 0) {
  712. $dealids = $this -> getDealsOfSavedCategories($catids, $token);
  713. }
  714.  
  715. return $dealids;
  716. }
  717.  
  718. /*Function to check if the user has and active deal.
  719. * If so find out the time when the deal ends and notify the user
  720. * */
  721. function checkActiveDeal() {
  722. global $database, $session, $business;
  723. $q = sprintf("SELECT dealname, dealtimestamp, status, expirytimestamp FROM deals WHERE businessid = '%s' ORDER BY dealid DESC limit 1", mysql_real_escape_string($this -> id));
  724. $status = checkBusiness($session -> userid);
  725. //$data = $database -> query($q) or die(mysql_error());
  726.  
  727. // $deal = mysql_fetch_assoc($data);
  728.  
  729. // $end_time = getRealTime($deal['expirytimestamp'], $business -> timezoneoffset);
  730. // $nextday_time = date("l F d, Y h:i:s A", mktime(0, 0, 0, date("m", $end_time), date("d", $end_time) + 1, date("Y", $end_time)));
  731.  
  732. // $usernow = gmdate("Y-m-d H:i:s");
  733. // $usernow = getRealTime($usernow, $business -> timezoneoffset);
  734.  
  735. // $deal_midnight = strtotime($nextday_time);
  736. // $difference = $deal_midnight - $usernow;
  737.  
  738. // if ($difference <= 0 && $status == 0) {
  739. // return false;
  740. // } else {
  741.  
  742. if ($status == 2) {
  743. if($session->userlevel==9)
  744. {
  745. $status=0;
  746. }else
  747. {
  748. return "<h1>You don't appear to have any registered business.</h1>";
  749. }
  750. } elseif ($status == 1) {
  751. return "<h1>Your business is not approved yet. Check back soon.</h1>";
  752.  
  753. } elseif ($status == 3) {
  754. return "<h1>Your business has been banned. Please contact administrator.</h1>";
  755. }
  756. // else {
  757.  
  758. // $status = $deal['status'] == 0 ? "a Pending " : "an Active ";
  759.  
  760. // $endDate = date("l F d, Y h:i:s A", $end_time);
  761.  
  762. // return "You have {$status} deal called <b><i>" . $deal['dealname'] . "</i></b> which ends on " . $endDate . " (Business's local time)<br /> You can add a new deal from " . $nextday_time . "
  763. // <p> Your new deal is " . $hours = round($difference / 3600) . " hours " . date("i", $difference) . " minutes " . date("s", $difference) . " seconds away </p>";
  764. // }
  765. // }
  766.  
  767. }
  768.  
  769. function checkActiveDeal_location() {
  770. global $database, $session, $business;
  771. $q = sprintf("SELECT dealname, dealtimestamp, status, expirytimestamp FROM deals WHERE businessid = '%s' AND sendto='location' ORDER BY dealid DESC limit 1", mysql_real_escape_string($this -> id));
  772. $status = checkBusiness($session -> userid);
  773. $data = $database -> query($q) or die(mysql_error());
  774.  
  775. $deal = mysql_fetch_assoc($data);
  776.  
  777. $end_time = getRealTime($deal['expirytimestamp'], $business -> timezoneoffset);
  778. $nextday_time = date("l F d, Y h:i:s A", mktime(0, 0, 0, date("m", $end_time), date("d", $end_time) + 1, date("Y", $end_time)));
  779.  
  780. $usernow = gmdate("Y-m-d H:i:s");
  781. $usernow = getRealTime($usernow, $business -> timezoneoffset);
  782.  
  783. $deal_midnight = strtotime($nextday_time);
  784. $difference = $deal_midnight - $usernow;
  785.  
  786. if ($difference <= 0 && $status == 0) {
  787. return false;
  788. } else {
  789.  
  790. if ($status == 2) {
  791. if($session->userlevel==9)
  792. {
  793. $status=0;
  794. }else
  795. {
  796. return "<h1>You don't appear to have any registered business.</h1>";
  797. }
  798. } elseif ($status == 1) {
  799. return "<h1>Your business is not approved yet. Check back soon.</h1>";
  800.  
  801. } elseif ($status == 3) {
  802. return "<h1>Your business has been banned. Please contact administrator.</h1>";
  803. } else {
  804.  
  805. $status = $deal['status'] == 0 ? "a Pending " : "an Active ";
  806.  
  807. $endDate = date("l F d, Y h:i:s A", $end_time);
  808.  
  809. return "You have {$status} 'location' deal called <b><i>" . $deal['dealname'] . "</i></b> which ends on " . $endDate . " (Business's local time)<br /> You can add a new deal from " . $nextday_time . "
  810. <p> Your new deal is " . $hours = round($difference / 3600) . " hours " . date("i", $difference) . " minutes " . date("s", $difference) . " seconds away </p>";
  811. }
  812. }
  813.  
  814. }
  815.  
  816. function checkActiveDeal_all() {
  817. global $database, $session, $business;
  818. $q = sprintf("SELECT dealname, dealtimestamp, status, expirytimestamp FROM deals WHERE businessid = '%s' AND sendto='all' ORDER BY dealid DESC limit 1", mysql_real_escape_string($this -> id));
  819. $status = checkBusiness($session -> userid);
  820. $data = $database -> query($q) or die(mysql_error());
  821.  
  822. $deal = mysql_fetch_assoc($data);
  823.  
  824. $end_time = getRealTime($deal['expirytimestamp'], $business -> timezoneoffset);
  825. $nextday_time = date("l F d, Y h:i:s A", mktime(0, 0, 0, date("m", $end_time), date("d", $end_time) + 1, date("Y", $end_time)));
  826.  
  827. $usernow = gmdate("Y-m-d H:i:s");
  828. $usernow = getRealTime($usernow, $business -> timezoneoffset);
  829.  
  830. $deal_midnight = strtotime($nextday_time);
  831. $difference = $deal_midnight - $usernow;
  832.  
  833. if ($difference <= 0 && $status == 0) {
  834. return false;
  835. } else {
  836.  
  837. if ($status == 2) {
  838. if($session->userlevel==9)
  839. {
  840. $status=0;
  841. }else
  842. {
  843. return "<h1>You don't appear to have any registered business.</h1>";
  844. }
  845. } elseif ($status == 1) {
  846. return "<h1>Your business is not approved yet. Check back soon.</h1>";
  847.  
  848. } elseif ($status == 3) {
  849. return "<h1>Your business has been banned. Please contact administrator.</h1>";
  850. } else {
  851.  
  852. $status = $deal['status'] == 0 ? "a Pending " : "an Active ";
  853.  
  854. $endDate = date("l F d, Y h:i:s A", $end_time);
  855.  
  856. return "You have {$status} 'all' deal called <b><i>" . $deal['dealname'] . "</i></b> which ends on " . $endDate . " (Business's local time)<br /> You can add a new deal from " . $nextday_time . "
  857. <p> Your new deal is " . $hours = round($difference / 3600) . " hours " . date("i", $difference) . " minutes " . date("s", $difference) . " seconds away </p>";
  858. }
  859. }
  860.  
  861. }
  862.  
  863. function checkActiveDealSales($businessid)
  864. {
  865. global $database, $session, $business;
  866. $q = sprintf("SELECT dealname, dealtimestamp, status, expirytimestamp FROM deals WHERE businessid = '%s' ORDER BY dealid DESC limit 1", $businessid);
  867. $status = checkBusinessSales($businessid);
  868. $data = $database -> query($q) or die(mysql_error());
  869. $deal = mysql_fetch_assoc($data);
  870. $end_time = getRealTime($deal['expirytimestamp'], $business -> timezoneoffset);
  871. $nextday_time = date("l F d, Y h:i:s A", mktime(0, 0, 0, date("m", $end_time), date("d", $end_time) + 1, date("Y", $end_time)));
  872.  
  873. $usernow = gmdate("Y-m-d H:i:s");
  874. $usernow = getRealTime($usernow, $business -> timezoneoffset);
  875.  
  876. $deal_midnight = strtotime($nextday_time);
  877. $difference = $deal_midnight - $usernow;
  878. if ($difference <= 0 && $status == 0) {
  879.  
  880. return false;
  881. }
  882. else {
  883.  
  884. if ($status == 2) {
  885. if($session->userlevel==9)
  886. {
  887. $status=0;
  888. }else
  889. {
  890. return "<h1>You don't appear to have any registered business.</h1>";
  891. }
  892. } elseif ($status == 1) {
  893. return "<h1>Your business is not approved yet. Check back soon.</h1>";
  894.  
  895. } elseif ($status == 3) {
  896. return "<h1>Your business has been banned. Please contact administrator.</h1>";
  897. } //else {
  898.  
  899. // $status = $deal['status'] == 0 ? "a Pending " : "an Active ";
  900.  
  901. // $endDate = date("l F d, Y h:i:s A", $end_time);
  902.  
  903. // return "Business has {$status} deal called <b><i>" . $deal['dealname'] . "</i></b> which ends on " . $endDate . " (Business's local time)<br /> You can add a new deal from " . $nextday_time . "
  904. // <p> New business deal is " . $hours = round($difference / 3600) . " hours " . date("i", $difference) . " minutes " . date("s", $difference) . " seconds away </p>";
  905. // }
  906. }
  907. }
  908.  
  909. function checkActiveDealSales_all($businessid)
  910. {
  911. global $database, $session, $business;
  912. $q = sprintf("SELECT dealname, dealtimestamp, status, expirytimestamp FROM deals WHERE businessid = '%s' and sendto='all' ORDER BY dealid DESC limit 1", $businessid);
  913. $status = checkBusinessSales($businessid);
  914. $data = $database -> query($q) or die(mysql_error());
  915. $deal = mysql_fetch_assoc($data);
  916. $end_time = getRealTime($deal['expirytimestamp'], $business -> timezoneoffset);
  917. $nextday_time = date("l F d, Y h:i:s A", mktime(0, 0, 0, date("m", $end_time), date("d", $end_time) + 1, date("Y", $end_time)));
  918.  
  919. $usernow = gmdate("Y-m-d H:i:s");
  920. $usernow = getRealTime($usernow, $business -> timezoneoffset);
  921.  
  922. $deal_midnight = strtotime($nextday_time);
  923. $difference = $deal_midnight - $usernow;
  924. if ($difference <= 0 && $status == 0) {
  925.  
  926. return false;
  927. }
  928. else {
  929.  
  930. if ($status == 2) {
  931. if($session->userlevel==9)
  932. {
  933. $status=0;
  934. }else
  935. {
  936. return "<h1>You don't appear to have any registered business.</h1>";
  937. }
  938. } elseif ($status == 1) {
  939. return "<h1>Your business is not approved yet. Check back soon.</h1>";
  940.  
  941. } elseif ($status == 3) {
  942. return "<h1>Your business has been banned. Please contact administrator.</h1>";
  943. } else {
  944.  
  945. $status = $deal['status'] == 0 ? "a Pending " : "an Active ";
  946.  
  947. $endDate = date("l F d, Y h:i:s A", $end_time);
  948.  
  949. return "Business has {$status} 'all' deal called <b><i>" . $deal['dealname'] . "</i></b> which ends on " . $endDate . " (Business's local time)<br /> You can add a new deal from " . $nextday_time . "
  950. <p> New business deal is " . $hours = round($difference / 3600) . " hours " . date("i", $difference) . " minutes " . date("s", $difference) . " seconds away </p>";
  951. }
  952. }
  953. }
  954.  
  955. function checkActiveDealSales_location($businessid)
  956. {
  957. global $database, $session, $business;
  958. $q = sprintf("SELECT dealname, dealtimestamp, status, expirytimestamp FROM deals WHERE businessid = '%s' and sendto='all' ORDER BY dealid DESC limit 1", $businessid);
  959. $status = checkBusinessSales($businessid);
  960. $data = $database -> query($q) or die(mysql_error());
  961. $deal = mysql_fetch_assoc($data);
  962. $end_time = getRealTime($deal['expirytimestamp'], $business -> timezoneoffset);
  963. $nextday_time = date("l F d, Y h:i:s A", mktime(0, 0, 0, date("m", $end_time), date("d", $end_time) + 1, date("Y", $end_time)));
  964.  
  965. $usernow = gmdate("Y-m-d H:i:s");
  966. $usernow = getRealTime($usernow, $business -> timezoneoffset);
  967.  
  968. $deal_midnight = strtotime($nextday_time);
  969. $difference = $deal_midnight - $usernow;
  970. if ($difference <= 0 && $status == 0) {
  971.  
  972. return false;
  973. }
  974. else {
  975.  
  976. if ($status == 2) {
  977. if($session->userlevel==9)
  978. {
  979. $status=0;
  980. }else
  981. {
  982. return "<h1>You don't appear to have any registered business.</h1>";
  983. }
  984. } elseif ($status == 1) {
  985. return "<h1>Your business is not approved yet. Check back soon.</h1>";
  986.  
  987. } elseif ($status == 3) {
  988. return "<h1>Your business has been banned. Please contact administrator.</h1>";
  989. } else {
  990.  
  991. $status = $deal['status'] == 0 ? "a Pending " : "an Active ";
  992.  
  993. $endDate = date("l F d, Y h:i:s A", $end_time);
  994.  
  995. return "Business has {$status} 'location' deal called <b><i>" . $deal['dealname'] . "</i></b> which ends on " . $endDate . " (Business's local time)<br /> You can add a new deal from " . $nextday_time . "
  996. <p> New business deal is " . $hours = round($difference / 3600) . " hours " . date("i", $difference) . " minutes " . date("s", $difference) . " seconds away </p>";
  997. }
  998. }
  999. }
  1000. };
  1001.  
  1002. $business = new Business;
  1003.  
  1004. //------FUNCTION TO CHECK STATUS OF A BUSINESS---------//
  1005. function checkBusiness($userid) {
  1006. global $database;
  1007.  
  1008. $q = sprintf("SELECT status from business WHERE userid = '%s'", mysql_real_escape_string($userid));
  1009. $result = $database -> query($q) or die(mysql_error());
  1010.  
  1011. $numrow = mysql_num_rows($result);
  1012. if ($numrow < 1) {
  1013. return 2;
  1014. //Business has not been created.
  1015. }
  1016.  
  1017. $status = mysql_fetch_assoc($result);
  1018.  
  1019. if ($status['status'] == 0) {
  1020. return 1;
  1021. //Business has been created but not approved by admin
  1022. } elseif ($status['status'] == 1) {
  1023. return 0;
  1024. //Business has been created and approve.
  1025. } elseif ($status['status'] == 2) {
  1026. return 3;
  1027. //Business has been banned.
  1028. }
  1029. }
  1030.  
  1031. function checkBusinessSales($businessid) {
  1032. global $database;
  1033.  
  1034. $q = sprintf("SELECT status from business WHERE businessid = '%s'",$businessid);
  1035. $result = $database -> query($q) or die(mysql_error());
  1036.  
  1037. $numrow = mysql_num_rows($result);
  1038. if ($numrow < 1) {
  1039. return 2;
  1040. //Business has not been created.
  1041. }
  1042.  
  1043. $status = mysql_fetch_assoc($result);
  1044.  
  1045. if ($status['status'] == 0) {
  1046. return 1;
  1047. //Business has been created but not approved by admin
  1048. } elseif ($status['status'] == 1) {
  1049. return 0;
  1050. //Business has been created and approve.
  1051. } elseif ($status['status'] == 2) {
  1052. return 3;
  1053. //Business has been banned.
  1054. }
  1055. }
  1056.  
  1057. function checkNotification($userid) {
  1058. global $database;
  1059.  
  1060. $q = sprintf("SELECT notification FROM users WHERE userid = '%s'", mysql_real_escape_string($userid));
  1061.  
  1062. $data = $database -> query($q) or die(mysql_error());
  1063. $row = mysql_fetch_assoc($data);
  1064. $status = $row['notification'];
  1065.  
  1066. if ($status == 1) {
  1067. return true;
  1068. } else {
  1069. return false;
  1070. }
  1071. }
  1072.  
  1073. function notificationsCountDeliveredForBusiness($businessId) {
  1074. global $database;
  1075. $date = gmdate('Y-m-d H:i:s');
  1076. $alreadyStartedDealsArray = array();
  1077. $q = sprintf("select dealid from deals where businessid='%s' and dealtimestamp < '%s'", mysql_real_escape_string($businessId),mysql_real_escape_string($date));
  1078. $alreadyStartedDeals = $database->query($q) or die(mysql_error());
  1079. while($row = mysql_fetch_assoc($alreadyStartedDeals)){
  1080. $alreadyStartedDealsArray[] = $row['dealid'];
  1081. }
  1082. if(count($alreadyStartedDealsArray) == 0)
  1083. return 0;
  1084. $alreadyStartedDealsCSV = implode(",", $alreadyStartedDealsArray);
  1085. $q1 = sprintf("select count(dealid) as deal_count from pushed where dealid in (%s)", mysql_real_escape_string($alreadyStartedDealsCSV));
  1086. $result_alert1 = $database->query($q1) or die(mysql_error());
  1087. $data3 = mysql_fetch_assoc($result_alert1);
  1088. return $data3['deal_count'];
  1089. }
  1090.  
  1091. function isExpired($dealid) {
  1092. global $database;
  1093.  
  1094. $q = sprintf("SELECT expirytimestamp FROM deals WHERE dealid = '%s'", $dealid);
  1095.  
  1096. $data = $database -> query($q) or die(mysql_error());
  1097. $row = mysql_fetch_assoc($data);
  1098. $status = $row['notification'];
  1099.  
  1100. if ($status == 1) {
  1101. return true;
  1102. } else {
  1103. return false;
  1104. }
  1105. }
  1106.  
  1107. function getCat($catid) {
  1108. global $database;
  1109.  
  1110. $q = sprintf("select * from category where catid = '%d'", $catid);
  1111.  
  1112. $array = $database -> query($q) or die(mysql_error());
  1113. $result = mysql_fetch_array($array);
  1114. $cat = $result['categoryName'];
  1115.  
  1116. return $cat;
  1117. }
  1118.  
  1119. function isValidURL($url) {
  1120. return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
  1121. }
  1122.  
  1123. function getBusinessDetail($businessid) {
  1124. global $database;
  1125. $q = sprintf("select * from business LEFT JOIN business_timing ON business.businessid = business_timing.business_id where business.businessid= '%s'", mysql_real_escape_string($businessid));
  1126. $array = $database -> query($q) or die(mysql_error());
  1127. $result = mysql_fetch_array($array, MYSQL_ASSOC);
  1128. return $result;
  1129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement