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

/controllers/server/tools.php

https://bitbucket.org/ivanfr26/alquilolibre-production
PHP | 563 lines | 328 code | 140 blank | 95 comment | 48 complexity | ae585a360d2fe8dedace2059ae6aff8e MD5 | raw file
  1. <?php
  2. function getPriceLabel($item_priceType, $item_price){
  3. include $_SERVER['DOCUMENT_ROOT'] . "/" . 'languages/es.php';
  4. if($item_price == 0){
  5. return 'Consultar precio';
  6. }
  7. $priceLabel;
  8. switch ($item_priceType) {
  9. case 1:
  10. $priceLabel = $lang_item_day;
  11. break;
  12. case 2:
  13. $priceLabel = $lang_item_week;
  14. break;
  15. case 3:
  16. $priceLabel = $lang_item_month;
  17. break;
  18. default:
  19. $priceLabel = $lang_item_hour;
  20. break;
  21. }
  22. return $priceLabel;
  23. }
  24. /**
  25. * Returns the value from a variable in the URL address (GET method)
  26. * @param unknown $name
  27. * @param unknown $default
  28. * @return unknown
  29. */
  30. function getUrlVariableValue($name, $default){
  31. $req = @$_REQUEST[$name];
  32. if(strlen($req) > 0){
  33. return $req;
  34. }
  35. return $default;
  36. }
  37. /**
  38. *
  39. * @param unknown $avoid
  40. * @return string
  41. */
  42. function getCurrentUrl($avoid){
  43. $current = "";
  44. foreach ($_REQUEST as $key => $value) {
  45. if(strstr($key, $avoid) == false){
  46. $current .= "$key=$value&";
  47. }
  48. }
  49. return $current;
  50. }
  51. /**
  52. * Returns string "<input type='hidden' name='$key' value='$value'>"; with all the URL variables.
  53. * Usefull for all 'html forms'.
  54. * @param string $avoid variables to avoid
  55. * @return string
  56. */
  57. function getCurrentUrlVars($avoid){
  58. $currentFilters = "";
  59. foreach ($_REQUEST as $key => $value) {
  60. if(@strstr($key, $avoid) == false){
  61. $filter = "<input type='hidden' name='$key' value='$value'>";
  62. $currentFilters .= $filter;
  63. }
  64. }
  65. return $currentFilters;
  66. }
  67. function printFilter($filterId, $filterValue){
  68. $id = @$_REQUEST[$filterId];
  69. $view = '';
  70. if(strlen($id) > 0){
  71. $value = $_REQUEST[$filterValue];
  72. $view = "<div class='filter_section_filterBy'>
  73. <div class=\"filter_section_filterBy_name\">$value
  74. <img class=\"filter_section_filterBy_cross\" onclick=\"removeFilter('$filterId', '$filterValue')\" src='/lib/images/cross.png'>
  75. </div>
  76. </div>";
  77. }else{
  78. return false;
  79. }
  80. return $view;
  81. }
  82. /**
  83. * Returns if a user is logged In
  84. * @return boolean
  85. */
  86. function isRentalLogued(){
  87. if(!isset($_SESSION)){
  88. session_start();
  89. }
  90. if(@$_SESSION['rental_id'] != 0 && @$_SESSION['rental_confirmed'] == 1){
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Returns if a user is logged In
  97. * @return boolean
  98. */
  99. function isUserLogued(){
  100. if(!isset($_SESSION)){
  101. session_start();
  102. }
  103. if(isset($_SESSION['user_id'])){
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * If the user is not logged is, redirects to login page
  110. * @param unknown $redirect
  111. */
  112. function controlRentalLogguedIn(){
  113. if(isUserLogued() == false){
  114. redirect("Debes crear una cuenta para continuar", 'Ok');
  115. return;
  116. }
  117. if(isRentalLogued() == false){
  118. ffUserLogguedIn("/pages/upgrade/");
  119. return;
  120. }
  121. }
  122. /**
  123. * If the user is not logged is, redirects to login page
  124. * @param unknown $redirect
  125. */
  126. function controlUserLogguedIn(){
  127. if(isUserLogued() == false){
  128. include $_SERVER['DOCUMENT_ROOT'] . "/" . 'languages/es.php';
  129. redirect($lang_logInToContinue, 'Ok');
  130. }
  131. }
  132. function gotUrl($url, $notice, $nType){
  133. header("location: $url&notice=$notice&nType=$nType");
  134. die();
  135. }
  136. function getBackUrl(){
  137. return $_SERVER['HTTP_REFERER'] . "?";
  138. }
  139. function redirect($notice, $nType){
  140. header("location: /pages/login/?notice=$notice&nType=$nType");
  141. die();
  142. }
  143. /**
  144. * If the user is ogged is, redirects to page
  145. * @param unknown $redirect
  146. */
  147. function ffUserLogguedIn($redirect){
  148. if(isUserLogued() == true){
  149. header("location: $redirect");
  150. die();
  151. }
  152. }
  153. function printNotice(){
  154. if(isset($_REQUEST['notice'])){
  155. $message = $_REQUEST['notice'];
  156. $notice = "<div id='all_notice' class='notice_".$_REQUEST['nType']."' onclick=\"fadeOut(this.id)\">
  157. <div class='all_notice_cross'>x</div>
  158. $message
  159. </div>";
  160. echo $notice;
  161. }
  162. }
  163. function showRentalCategories($rental_id, $link){
  164. $view = '';
  165. $result = getRentalCategories($rental_id, $link);
  166. while(($row = mysqli_fetch_assoc($result)) != false){
  167. $id = $row['category_id'];
  168. if($id != CATEGORY_ID_GENERAL_TOP){
  169. $name = $row['category_name'];
  170. $view .= "<div class='item_categoty_level' onclick=\"goSearchCategory('$id', '$name')\">$name</div> <pipe> | </pipe> ";
  171. }
  172. }
  173. return $view;
  174. }
  175. function cleanStringForDb($string){
  176. $i = 0;
  177. $inp = Array();
  178. $out = Array();
  179. $inp[$i] = "'";
  180. $out[$i] = "&#39;";
  181. $i++;
  182. $inp[$i] = '"';
  183. $out[$i] = "&#34;";
  184. $i++;
  185. $string = str_replace($inp, $out, $string);
  186. $string = str_replace("\n", '<br />', $string);
  187. $string = preg_replace('/\s+/', ' ', trim($string));
  188. return $string;
  189. }
  190. function cleanStringJs($string) {
  191. $i = 0;
  192. $inp = Array();
  193. $out = Array();
  194. $inp[$i] = "&#39;";
  195. $out[$i] = "'";
  196. $i++;
  197. $inp[$i] = "&#34;";
  198. $out[$i] = '\"';
  199. $i++;
  200. $string = str_replace($inp, $out, $string);
  201. $string = str_replace('<br />', '\n', $string);
  202. return $string;
  203. }
  204. /**
  205. * Makes a call to the DB, and returns an Array of ItemImgs()
  206. *
  207. * @param int $item_id
  208. * @param int $owner_id
  209. *
  210. * @return multitype:
  211. */
  212. function getUserImages($user_id){
  213. include_once $_SERVER['DOCUMENT_ROOT'] . '/controllers/database/models/image.model.php';
  214. include_once $_SERVER['DOCUMENT_ROOT'] . '/controllers/database/connectionManager.php';
  215. $link = new ConnectionManager();
  216. $query = "call sp_get_user_images('$user_id')";
  217. $result = mysqli_query($link->getConnection(), $query);
  218. $images = Array();
  219. $img = new Image();
  220. while($img->getModelFromResult($img, $result) != false){
  221. $img->image_fullpath = S3_IMAGES_PATH . "/upload/$user_id/profile/imgs/$img->image_name";
  222. array_push($images, $img);
  223. $img = new Image();
  224. }
  225. @mysqli_free_result($result);
  226. return $images;
  227. }
  228. /**
  229. * Makes a call to the DB, and returns an Array of ItemImgs()
  230. *
  231. * @param int $item_id
  232. * @param int $owner_id
  233. *
  234. * @return multitype:
  235. */
  236. function getItemImages($item_id, $owner_id){
  237. include_once $_SERVER['DOCUMENT_ROOT'] . '/controllers/database/models/itemImg.model.php';
  238. include_once $_SERVER['DOCUMENT_ROOT'] . '/controllers/database/connectionManager.php';
  239. $link = new ConnectionManager();
  240. $query = "call sp_get_item_images('$item_id')";
  241. $result = mysqli_query($link->getConnection(), $query);
  242. $images = Array();
  243. $img = new ItemImg();
  244. while($img->getModelFromResult($img, $result) != false){
  245. $img->image_fullpath = S3_IMAGES_PATH . "/upload/$owner_id/items/$item_id/$img->image_name";
  246. array_push($images, $img);
  247. $img = new ItemImg();
  248. }
  249. @mysqli_free_result($result);
  250. return $images;
  251. }
  252. /**
  253. * If an error ocurred, prints and dies.
  254. * @param unknown $result
  255. * @param unknown $link
  256. */
  257. function controlDbResul($result, $query, $link){
  258. if($result == false){
  259. echo "<br><b>$query</b><br> Error: " . mysqli_error($link);
  260. die();
  261. }
  262. }
  263. function getRentalCategories($rental_id, $link){
  264. $query = "call sp_get_rental_categories('$rental_id')";
  265. return mysqli_query($link, $query);
  266. controlDbResul($result, $query, $link);
  267. }
  268. /**
  269. * Prints the Imgs of an item
  270. * @param Item $item
  271. */
  272. function showImgsEdit($images, $type){
  273. $view = '';
  274. $i = 0;
  275. foreach ($images as $img) {
  276. $deleteButton = "<div class='windows_button'>
  277. <div class='updateItem_imgs_delMe' id='updateItem_imgs_delMe_$i'
  278. onclick=\"deleteImage('$img->image_fullpath', '$img->image_id', 'updateItem_imgs_thumb_$i', '$type')\">
  279. x
  280. </div>
  281. </div>";
  282. $view .= "<div class='updateItem_imgs_thumbBlock'>
  283. $deleteButton
  284. <img class='updateItem_imgs_thumb' id='updateItem_imgs_thumb_$i' src='$img->image_fullpath'>
  285. </div>";
  286. $i++;
  287. }
  288. //Complete with empty images until the limit
  289. for ($extra = $i; $extra < MAX_IMAGES_PER_USER; $extra++) {
  290. $view .= "<div class='updateItem_imgs_thumbBlock'>
  291. <img class='updateItem_imgs_thumb' id='updateItem_imgs_thumb_$extra' src='/lib/images/noimg.png'>
  292. </div>";
  293. }
  294. echo $view;
  295. }
  296. /**
  297. * Creates a random string
  298. * @param unknown $length
  299. * @return string
  300. */
  301. function rand_string( $length ) {
  302. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  303. $str = '';
  304. $size = strlen( $chars );
  305. for( $i = 0; $i < $length; $i++ ) {
  306. $str .= $chars[ rand( 0, $size - 1 ) ];
  307. }
  308. return $str;
  309. }
  310. function getUserIdFromRental($rental_id, $link){
  311. $query = "SELECT user_id FROM rental WHERE rental_id=$rental_id";
  312. $result = mysqli_query($link, $query);
  313. $aux = mysqli_fetch_assoc($result);
  314. mysqli_free_result($result);
  315. mysqli_close($link);
  316. return $aux['user_id'];
  317. }
  318. /**
  319. * Prints all categories inside a select
  320. * @param unknown $selected
  321. */
  322. function printCats($selected, $link){
  323. $query = "select * from `category` ORDER BY `category`.`category_l` ASC";
  324. $result = mysqli_query($link, $query);
  325. printOptions($result, 'category_name', 'category_id', 'Otros', $selected);
  326. }
  327. /**
  328. * Prints the results inside a select
  329. * @param unknown $result
  330. * @param unknown $name
  331. * @param unknown $id
  332. * @param unknown $ignore
  333. * @param unknown $selected
  334. */
  335. function printOptions($result, $name, $id, $ignore, $selected){
  336. while(($row = mysqli_fetch_assoc($result)) != false){
  337. $isSelect = "";
  338. if($row[$name] == $selected){
  339. $isSelect = 'selected="selected"';
  340. }else{
  341. $isSelect = '';
  342. }
  343. if($row[$name] != $ignore){
  344. echo "<option $isSelect value='".$row[$id]."'>".$row[$name]."</option>";
  345. }
  346. }
  347. }
  348. function getItemName($raw){
  349. $name = substr($raw, stripos($raw, "/") + 1);
  350. return str_replace('-', " ", $name);
  351. }
  352. function getItemLink($id, $name){
  353. $linkName = str_replace(' ', '-', $name);
  354. $link = "/product/?$id/$linkName";
  355. return $link;
  356. }
  357. function getRentalLink($id, $name){
  358. $linkName = str_replace(' ', '-', $name);
  359. $link = "/store/?$id/$linkName";
  360. return $link;
  361. }
  362. function getUserLink($id, $name){
  363. $linkName = str_replace(' ', '-', $name);
  364. $link = "/pages/user/?$id/$linkName";
  365. return $link;
  366. }
  367. /**
  368. * Counts the comments not responded done to the User
  369. * @param unknown $user_id
  370. * @param unknown $link
  371. */
  372. function getReservationsCount($user_id, $link){
  373. $query = "SELECT reservation_status FROM reservation WHERE owner_id=$user_id OR user_id=user_id";
  374. $result = mysqli_query($link, $query);
  375. $count = "";
  376. while(($row = mysqli_fetch_assoc($result)) != false){
  377. $status = $row['reservation_status'];
  378. if($status == 0){
  379. $count++;
  380. }
  381. }
  382. return $count;
  383. }
  384. /**
  385. * Counts the comments not responded done to the User
  386. * @param unknown $user_id
  387. * @param unknown $link
  388. */
  389. function getUnrespondedCommentsPerItems($user_id, $link){
  390. $query = "select itemComment_reply from itemComment where user_id=$user_id";
  391. $result = mysqli_query($link, $query);
  392. $count = "";
  393. while(($comment = mysqli_fetch_assoc($result)) != false){
  394. $comment_reply = $comment['itemComment_reply'];
  395. if(strlen($comment_reply) == 0){
  396. $count++;
  397. }
  398. }
  399. return $count;
  400. }
  401. /**
  402. * Counts the comments not responded done to the User
  403. * @param unknown $user_id
  404. * @param unknown $link
  405. */
  406. function getUnrespondedCommentsPerUser($user_id, $link){
  407. $query = "call sp_get_user_comments('$user_id')";
  408. $result = mysqli_query($link, $query);
  409. $count = "";
  410. while(($comment = mysqli_fetch_assoc($result)) != false){
  411. $comment_reply = $comment['userComment_reply'];
  412. if(strlen($comment_reply) == 0){
  413. $count++;
  414. }
  415. }
  416. return $count;
  417. }
  418. ?>