PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://bitbucket.org/pavolve/masterskayaludmila
PHP | 349 lines | 342 code | 6 blank | 1 comment | 53 complexity | 2e6b11919236d097dc25a26359400db2 MD5 | raw file
  1. <?php
  2. require_once("../libs/Smarty.class.php");
  3. require_once("controllers/is_logged.php");
  4. $smarty = new Smarty();
  5. $is_logged = is_logged();
  6. if ( $err = $_SESSION["err"] ) {
  7. unset($_SESSION["err"]);
  8. $smarty -> assign("err", $err);
  9. }
  10. if ( $values = $_SESSION["values"] ) {
  11. unset($_SESSION["values"]);
  12. $smarty -> assign("values", $values);
  13. }
  14. $smarty -> assign("is_logged", $is_logged);
  15. // Получаем URI для проверки на слеши
  16. $uri = preg_replace("/\?.*/i",'', $_SERVER['REQUEST_URI']);
  17. if (strlen($uri)>1) {// если не главная страница...
  18. if ( isset($_GET["p"]) ){
  19. if (rtrim($uri,'/')!=$uri) {
  20. header("HTTP/1.1 301 Moved Permanently");
  21. header('Location: http://'.$_SERVER['SERVER_NAME'].str_replace($uri, rtrim($uri,'/'), $_SERVER['REQUEST_URI']));
  22. exit();
  23. }
  24. }
  25. else{
  26. if (rtrim($uri,'/')."/"!=$uri) {
  27. header("HTTP/1.1 301 Moved Permanently");
  28. header('Location: http://'.$_SERVER['SERVER_NAME'].str_replace($uri, $uri.'/', $_SERVER['REQUEST_URI']));
  29. exit();
  30. }
  31. }
  32. }
  33. if (isset($_GET["page"])){
  34. switch ($_GET["page"]) {
  35. case "admin":
  36. if( $is_logged )
  37. if ( $is_logged == "admin" ){
  38. $smarty -> assign("page_title", "Админка");
  39. $smarty->assign("name", $_SESSION["first_name"]);
  40. $smarty -> display("includes/adm_header.tpl");
  41. require_once ("config.php");
  42. $new_orders = mysql_query("SELECT COUNT(*) FROM orders WHERE status=0");
  43. $order_count = mysql_fetch_array($new_orders);
  44. $smarty -> assign("order_count", $order_count[0]);
  45. $smarty -> display("includes/adm_menu.tpl");
  46. if ( isset($_GET["action"]) ){
  47. switch ( $_GET["action"] ){
  48. case "new_set":
  49. $smarty -> assign("title", "Добавление нового набора");
  50. $smarty -> display("includes/new_set.tpl");
  51. break;
  52. case "new_product":
  53. $smarty -> assign("title", "Добавление нового изделия");
  54. include "config.php";
  55. $result = mysql_query("SELECT id, article FROM collections ORDER BY id");
  56. while ($data = mysql_fetch_assoc($result))
  57. $collections[$data["id"]] = $data["article"];
  58. $smarty -> assign("collections", $collections);
  59. $smarty -> assign("selected_collection_id", $values["collections_id"]);
  60. $type = array( "1" => "Кольцо",
  61. "2" => "Серьги",
  62. "3" => "Браслет",
  63. "4" => "Колье" );
  64. $smarty -> assign("type", $type);
  65. $smarty -> assign("selected_type_id", $values["type_id"]);
  66. $smarty -> display("includes/new_product.tpl");
  67. break;
  68. case "edit_set":
  69. $smarty -> assign("title", "Редактирование набора");
  70. if ( isset($_GET['id']) ){
  71. if ( !$values ){
  72. include "config.php";
  73. $result = mysql_query("SELECT
  74. name,
  75. article,
  76. description
  77. FROM collections
  78. WHERE id='".$_GET['id']."'");
  79. $values = mysql_fetch_assoc($result);
  80. }
  81. $smarty -> assign("values", $values);
  82. $smarty -> assign("red", "1");
  83. $smarty -> display("includes/new_set.tpl");
  84. }
  85. break;
  86. case "edit_product":
  87. $smarty -> assign("title", "Редактирование изделия");
  88. if ( isset($_GET['id']) ){
  89. if ( !$values ){
  90. include "config.php";
  91. $result = mysql_query("SELECT id, article FROM collections ORDER BY article");
  92. while ($data = mysql_fetch_assoc($result))
  93. $collections[$data["id"]] = $data["article"];
  94. $result2 = mysql_query("SELECT article, name, description, price_silver, price_rodiy, price_gold, price_silver2, price_rodiy2, price_gold2, is_size, is_insert_color, type_id, collections_id, status
  95. FROM products
  96. WHERE id='".$_GET['id']."'");
  97. $values = mysql_fetch_assoc($result2);
  98. if ( $values["is_size"] == 1 )
  99. $values["is_size"] = "checked";
  100. if ( $values["is_insert_color"] == 1 )
  101. $values["is_insert_color"] = "checked";
  102. if ( $values["status"] == 0 )
  103. $values["status"] = "checked";
  104. }
  105. $smarty -> assign("values", $values);
  106. $smarty -> assign("collections", $collections);
  107. $smarty -> assign("selected_collection_id", $values["collections_id"]);
  108. $type = array( "1" => "Кольцо",
  109. "2" => "Серьги",
  110. "3" => "Браслет",
  111. "4" => "Колье" );
  112. $smarty -> assign("type", $type);
  113. $smarty -> assign("red", "1");
  114. $smarty -> assign("selected_type_id", $values["type_id"]);
  115. $smarty -> display("includes/new_product.tpl");
  116. }
  117. break;
  118. case "list":
  119. require_once ("config.php");
  120. $type = (int) $_GET["type"];
  121. $num = 30;
  122. $result_to_nav = mysql_query("SELECT COUNT(*) FROM products");
  123. require_once ("controllers/nav.php");
  124. $result = mysql_query("SELECT id, article, name, description, collections_id, type_id, image, is_insert_color, price_silver, price_rodiy, price_gold, status
  125. FROM products
  126. ORDER BY id DESC LIMIT ".$start.", ".$num);
  127. while ( $data = mysql_fetch_assoc($result) ){
  128. $result2 = mysql_query("SELECT article FROM collections WHERE id = '".$data['collections_id']."'");
  129. $data2 = mysql_fetch_assoc($result2);
  130. switch ($data['type_id']){
  131. case '1': $data['type'] = "Кольцо"; break;
  132. case '2': $data['type'] = "Серьги"; break;
  133. case '3': $data['type'] = "Браслет"; break;
  134. case '4': $data['type'] = "Колье"; break;
  135. }
  136. $data['collections'] = $data2['article'];
  137. $products[$data["id"]] = $data;
  138. }
  139. $smarty -> assign("products", $products);
  140. $smarty -> display("includes/list.tpl");
  141. break;
  142. case "sets":
  143. require_once ("config.php");
  144. $num = 50;
  145. $result_to_nav = mysql_query("SELECT COUNT(*) FROM collections");
  146. require_once ("controllers/nav.php");
  147. $result = mysql_query("SELECT id, name, article, description
  148. FROM collections
  149. ORDER BY id DESC LIMIT ".$start.", ".$num);
  150. while ( $data = mysql_fetch_assoc($result) )
  151. $collections[$data["id"]] = $data;
  152. $smarty -> assign("collections", $collections);
  153. $smarty -> display("includes/sets.tpl");
  154. break;
  155. case "insert_colors":
  156. $smarty -> assign("title", "Цвета вставок");
  157. include "config.php";
  158. $result = mysql_query("SELECT id, name, image, mark_up FROM insert_colors");
  159. while ( $data = mysql_fetch_assoc($result) )
  160. $insert_colors[$data["id"]] = $data;
  161. $smarty -> assign("insert_colors", $insert_colors);
  162. $smarty -> display("includes/insert_colors.tpl");
  163. break;
  164. case "orders":
  165. require_once ("config.php");
  166. $type = (int) $_GET["type"];
  167. $num = 10;
  168. $result_to_nav = mysql_query("SELECT COUNT(*) FROM orders");
  169. require_once ("controllers/nav.php");
  170. $result = mysql_query("SELECT * FROM orders ORDER BY id DESC LIMIT ".$start.", ".$num);
  171. while ( $data = mysql_fetch_assoc($result) ){
  172. $result2 = mysql_query("SELECT * FROM users WHERE id = '".$data['users_id']."'");
  173. $data2 = mysql_fetch_assoc($result2);
  174. $result3 = mysql_query("SELECT * FROM order_items WHERE orders_id = '".$data['id']."'");
  175. while ( $data3 = mysql_fetch_assoc($result3) ){
  176. if(empty($data3['size']))$data3['size'] = "Нет";
  177. switch ($data3['type']){
  178. case '1': $data3['type'] = "Кольцо"; break;
  179. case '2': $data3['type'] = "Серьги"; break;
  180. case '3': $data3['type'] = "Браслет"; break;
  181. case '4': $data3['type'] = "Колье"; break;
  182. }
  183. $result4 = mysql_query("SELECT * FROM insert_colors WHERE id = '".$data3['insert_color']."'");
  184. if($data4 = mysql_fetch_assoc($result4))$data3['insert_color'] = $data4['name'];
  185. else $data3['insert_color'] = "Нет";
  186. $data['order_items'][$data3['id']] = $data3;
  187. }
  188. $data['user'] = $data2;
  189. $data['date'] = date("Y-m-d H:i:s", $data['date']);
  190. $pageData[$data['id']] = $data;
  191. }
  192. $smarty -> assign("data", $pageData);
  193. $smarty -> display("includes/orders.tpl");
  194. break;
  195. }
  196. } else {
  197. $smarty -> display("includes/admin_index.tpl");
  198. }
  199. $smarty -> display("includes/adm_footer.tpl");
  200. } else echo "Панель администратора. Нет доступа, авторизуйтесь как \"Админ\" <a href='/logout'>Выйти</a>";
  201. else {
  202. $smarty -> display("header.tpl");
  203. $smarty -> display("includes/login.tpl");
  204. $smarty -> display("footer.tpl");
  205. }
  206. break;
  207. case "order":
  208. if( $is_logged && isset($values['order_message'])){
  209. $smarty -> display("header.tpl");
  210. $smarty -> display("order.tpl");
  211. $smarty -> display("footer.tpl");
  212. }
  213. else{
  214. header("Location: http://".$_SERVER['HTTP_HOST']); exit();
  215. }
  216. break;
  217. case "products":
  218. require_once ("config.php");
  219. $type = (int) $_GET["type"];
  220. $num = 12;
  221. $result_to_nav = mysql_query("SELECT COUNT(*) FROM products WHERE type_id='".$type."'");
  222. switch ($type){
  223. case '1': $title = "Кольца"; break;
  224. case '2': $title = "Серьги"; break;
  225. case '3': $title = "Браслеты"; break;
  226. case '4': $title = "Колье"; break;
  227. }
  228. require_once ("controllers/nav.php");
  229. $result = mysql_query("SELECT id, article, name, description, image, price_silver, price_rodiy, price_gold, is_size, is_insert_color, type_id
  230. FROM products
  231. WHERE type_id='".$type."'
  232. ORDER BY id LIMIT ".$start.", ".$num);
  233. while ( $data = mysql_fetch_assoc($result) )
  234. $products[$data["id"]] = $data;
  235. $smarty -> assign("products", $products);
  236. $smarty -> assign("title", $title);
  237. $smarty -> display("header.tpl");
  238. $smarty -> display("products.tpl");
  239. $smarty -> display("footer.tpl");
  240. break;
  241. case "sets":
  242. require_once ("config.php");
  243. $num = 12;
  244. $result_to_nav = mysql_query("SELECT COUNT(*) FROM collections");
  245. require_once ("controllers/nav.php");
  246. $result = mysql_query("SELECT id FROM collections ORDER BY id LIMIT ".$start.", ".$num);
  247. while ( $data = mysql_fetch_assoc($result) ){
  248. $result2 = mysql_query("SELECT id, article, name, description, image, price_silver, price_rodiy, price_gold, is_size, is_insert_color, type_id
  249. FROM products
  250. WHERE collections_id='".$data["id"]."'
  251. ORDER BY id");
  252. while ( $data2 = mysql_fetch_assoc($result2) ){
  253. $products[$data["id"]][$data2["id"]] = $data2;
  254. $set_prices[$data["id"]]['silver'] += $data2['price_silver'];
  255. $set_prices[$data["id"]]['gold'] += $data2['price_gold'];
  256. $set_prices[$data["id"]]['rodiy'] += $data2['price_rodiy'];
  257. }
  258. $collections[$data["id"]] = $data;
  259. }
  260. $smarty -> assign("set_prices", $set_prices);
  261. $smarty -> assign("products", $products);
  262. $smarty -> display("header.tpl");
  263. $smarty -> display("sets.tpl");
  264. $smarty -> display("footer.tpl");
  265. break;
  266. case "password_recovery":
  267. if ($is_logged) {header("Location: http://".$_SERVER['HTTP_HOST']."/auth/"); exit();}
  268. if(isset($_GET['hash']) && $_GET['hash'] == $_SESSION["hash"]){
  269. $smarty -> assign("stage", "new_pass");
  270. $smarty -> assign("hash", $_GET['hash']);
  271. }
  272. else if (isset($_GET['hash'])){
  273. $values["recovery_message"] = "Не правильная ссылка.";
  274. }
  275. $smarty -> assign("values", $values);
  276. $smarty -> display("header.tpl");
  277. $smarty -> display("password_recovery.tpl");
  278. $smarty -> display("footer.tpl");
  279. break;
  280. case "basket":
  281. $smarty -> display("header.tpl");
  282. $smarty -> display("basket.tpl");
  283. $smarty -> display("footer.tpl");
  284. break;
  285. case "about":
  286. $smarty -> display("header.tpl");
  287. $smarty -> display("about.tpl");
  288. $smarty -> display("footer.tpl");
  289. break;
  290. case "contacts":
  291. $smarty -> display("header.tpl");
  292. $smarty -> display("contacts.tpl");
  293. $smarty -> display("footer.tpl");
  294. break;
  295. case "pay-and-delivery":
  296. $smarty -> display("header.tpl");
  297. $smarty -> display("pay-and-delivery.tpl");
  298. $smarty -> display("footer.tpl");
  299. break;
  300. case "how-to-buy":
  301. $smarty -> display("header.tpl");
  302. $smarty -> display("how-to-buy.tpl");
  303. $smarty -> display("footer.tpl");
  304. break;
  305. case "auth":
  306. $smarty -> display("header.tpl");
  307. $smarty -> display("index.tpl");
  308. $smarty -> display("footer.tpl");
  309. break;
  310. case "diamonds":
  311. require_once ("config.php");
  312. $result = mysql_query("SELECT id, name, image FROM insert_colors");
  313. while( $data = mysql_fetch_assoc($result) )
  314. $diamonds[$data['id']] = $data;
  315. $smarty -> assign("diamonds", $diamonds);
  316. $smarty -> display("header.tpl");
  317. $smarty -> display("diamonds.tpl");
  318. $smarty -> display("footer.tpl");
  319. break;
  320. case "metals":
  321. $smarty -> display("header.tpl");
  322. $smarty -> display("metals.tpl");
  323. $smarty -> display("footer.tpl");
  324. break;
  325. default:
  326. $smarty -> display("errors/404.tpl");
  327. }
  328. }
  329. else if ( isset($_GET["error"]) ){
  330. switch ($_GET["error"]) {
  331. case "404": $smarty -> display("errors/404.tpl"); break;
  332. case "403": $smarty -> display("errors/403.tpl"); break;
  333. case "401": $smarty -> display("errors/401.tpl"); break;
  334. case "500": $smarty -> display("errors/500.tpl"); break;
  335. default: $smarty -> display("errors/404.tpl");
  336. }
  337. }
  338. else{
  339. $smarty -> display("header.tpl");
  340. $smarty -> display("index.tpl");
  341. $smarty -> display("footer.tpl");
  342. }
  343. ?>