PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/cmd/main/sync_functions.php

https://bitbucket.org/kostyantyn_kyyashko/kino-teatr.ua
PHP | 343 lines | 281 code | 35 blank | 27 comment | 54 complexity | 5259f197bb1ba9c13ccf090f881d1251 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. <?php
  2. function get_cinemas(){
  3. $sql = "SELECT mc.id, mc.name AS name, mcl.title AS name_ukr FROM grifix_main_cinemas mc LEFT JOIN grifix_main_cinemas_lng mcl ON (mc.id = mcl.record_id AND mcl.lang_id = 3)";
  4. $res = mysql_query($sql);
  5. $arr = array();
  6. while($row = mysql_fetch_assoc($res)){
  7. $arr[$row['id']] = $row;
  8. }
  9. return $arr;
  10. }
  11. function get_cinemas_halls(){
  12. $sql = "SELECT mch.id AS hall_id, mch.cinema_id, mch.name, mchl.title AS name_ukr FROM grifix_main_cinemas_halls mch LEFT JOIN grifix_main_cinemas_halls_lng mchl ON (mch.id = mchl.record_id AND mchl.lang_id = 3)";
  13. $res = mysql_query($sql);
  14. $arr = array();
  15. while($row = mysql_fetch_assoc($res)){
  16. $arr[$row['hall_id']] = $row;
  17. }
  18. return $arr;
  19. }
  20. function get_cinema_hall_by_name($params,$halls){
  21. if(strpos($params['hallName'],'.'))
  22. list($cinema,$hall_name) = explode('.',$params['hallName']);
  23. else
  24. $hall_name = $params['hallName'];
  25. $hall_name = trim($hall_name);
  26. $hall_id = 0;
  27. $cinema_id = $params['cinema_id'];
  28. if(!$hall_name)
  29. return $hall_id;
  30. // echo 'cinema_id '.$cinema_id.' hall_name '.$hall_name.'<br>';
  31. foreach($halls as $key=>$value){
  32. if($value['cinema_id'] == $cinema_id){
  33. // echo 'name '.$value['name'].' name_ukr '.$value['name_ukr'].'<br>';
  34. if(mb_strtolower($value['name']) == mb_strtolower($hall_name) || mb_strtolower($value['name_ukr']) == mb_strtolower($hall_name)){
  35. $hall_id = $key;
  36. break;
  37. }
  38. if(replace_hall_names($hall_name,$value['name_ukr'])){
  39. $hall_id = $key;
  40. break;
  41. }
  42. }
  43. }
  44. return $hall_id;
  45. }
  46. function megakino_connect(){
  47. $author_url = 'http://server.megakino.com.ua/gate/login';
  48. $author_params = 'username=kino-teatr.ua&password=vdfv78464rwcs5g34';
  49. if( $curl = curl_init() ) {
  50. curl_setopt($curl, CURLOPT_URL, $author_url);
  51. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  52. curl_setopt($curl, CURLOPT_POST, true);
  53. curl_setopt($curl, CURLOPT_POSTFIELDS, $author_params);
  54. $out = curl_exec($curl);
  55. curl_close($curl);
  56. $session_id = trim($out);
  57. return $session_id;
  58. }
  59. else
  60. return '';
  61. }
  62. function replace_hall_names($hall_name,$enter_name){
  63. $hall_name = mb_strtolower($hall_name);
  64. $enter_name = mb_strtolower($enter_name);
  65. $hall_name = str_ireplace('синий зал','синя зала',$hall_name);
  66. $hall_name = str_ireplace('червоний зал','червона зала',$hall_name);
  67. $hall_name = str_ireplace('зелений зал','зелена зала',$hall_name);
  68. $hall_name = str_ireplace('малий зал','мала зала',$hall_name);
  69. if($hall_name == $enter_name)
  70. return true;
  71. return false;
  72. }
  73. function get_cinema_by_name($params,$cinemas){
  74. $name = str_replace('Кінотеатр','',$params['name']);
  75. $name = str_replace('Кинотеатр','',$name);
  76. if(strpos($name,'(')){
  77. list($name,$name2) = explode('(',$name);
  78. }
  79. $name = trim($name);
  80. $cinema_id = 0;
  81. foreach($cinemas as $key=>$value){
  82. if($value['name'] == $name || $value['name_ukr'] == $name){
  83. $cinema_id = $key;
  84. break;
  85. }
  86. }
  87. return $cinema_id;
  88. }
  89. function get_films(){
  90. $sql = "SELECT gmf.title_orig, gmf.name AS title, gmf.year, gmf.id, gmfl.title AS title_ukr, gmfr.title AS title_rus FROM grifix_main_films gmf LEFT JOIN grifix_main_films_lng gmfl ON (gmf.id = gmfl.record_id AND gmfl.lang_id = 3) LEFT JOIN grifix_main_films_lng gmfr ON (gmf.id = gmfr.record_id AND gmfr.lang_id = 1)";
  91. $res = mysql_query($sql);
  92. $arr = array();
  93. while($row = mysql_fetch_assoc($res)){
  94. $arr[$row['id']] = $row;
  95. }
  96. return $arr;
  97. }
  98. function get_film_by_name($params,$films){
  99. $orig_name = $params['originName'];
  100. $name = $params['name'];
  101. $premiere = $params['premiere'];
  102. list($year,$month,$day) = explode('-',$premiere);
  103. $film_id = 0;
  104. // echo 'origin_name '.$orig_name.'<br>';
  105. // echo 'name '.$name.'<br>';
  106. foreach($films as $key=>$value){
  107. if($value['title_orig'] == $orig_name || $value['title_ukr'] == $name || $value['title_rus'] == $name || $value['title'] == $orig_name || $value['title'] == $name){
  108. if($value['year'] != $year){
  109. }
  110. else{
  111. $film_id = $key;
  112. break;
  113. }
  114. }
  115. }
  116. return $film_id;
  117. }
  118. function check_url($response){
  119. echo 'response '.$response.'<br>';
  120. if(!$response || strpos($response,'ERROR 403')){
  121. echo 'Denied !! <br>';
  122. return false;
  123. }
  124. return true;
  125. }
  126. function set_events($session_id){
  127. if(!$session_id)
  128. return false;
  129. $sites_url = 'http://server.megakino.com.ua/gate/sites';
  130. mysql_query("TRUNCATE temp_auto_films_cmd");
  131. $sites_url .= '?sessionid='.$session_id;
  132. $films_url = 'http://server.megakino.com.ua/gate/shows';
  133. $buffer = file_get_contents($sites_url);
  134. $response = json_decode($buffer,true);
  135. if(!is_array($response))
  136. return false;
  137. foreach($response as $item){
  138. $site_id = $item['siteId'];
  139. $site_name = $item['name'];
  140. $site_index = $item['index'];
  141. $site_address = $item['address'];
  142. $filmes_buffer = file_get_contents($films_url.'?sessionid='.$session_id.'&siteId='.$site_id);
  143. // echo $films_url.'?sessionid='.$session_id.'&siteId='.$site_id.'<br>';
  144. $response_films = json_decode($filmes_buffer,true);
  145. foreach($response_films as $subitem){
  146. // var_dump($subitem);
  147. $id_film_export = $subitem['showId'];
  148. $date_premiere = $subitem['premiereLocal'];
  149. foreach($subitem['events'] as $event){
  150. $technology = $event['technology'];
  151. $hall_id = $event['hallId'];
  152. $event_id = $event['eventId'];
  153. // $map_url = 'http://server.megakino.com.ua/gate/eventmap?sessionid='.$session_id.'&amp;eventId='.$event_id.'&amp;siteId='.$site_id;
  154. $map_url = 'http://server.megakino.com.ua/gate/eventmap?sessionid='.$session_id.'&eventId='.$event_id.'&siteId='.$site_id;
  155. $map_response = file_get_contents($map_url);
  156. if(!check_url($map_response)){
  157. $buffer_session_id = megakino_connect();
  158. if($buffer_session_id){
  159. $session_id = $buffer_session_id;
  160. $map_url = 'http://server.megakino.com.ua/gate/eventmap?sessionid='.$session_id.'&eventId='.$event_id.'&siteId='.$site_id;
  161. $map_response = file_get_contents($map_url);
  162. }
  163. }
  164. $map_response = mysql_real_escape_string($map_response);
  165. // var_dump($map_response);
  166. // if($map_response && isset($buffer_map['code']) && $buffer_map['code'] == 0){
  167. // $map_response = mysql_real_escape_string($map_response);
  168. // $sql = "INSERT INTO `grifix_main_shows_eventmap`(`show_id`, `event_id`, `site_id`, `response`) VALUES ($show_id,$event_id,'$site_id','$map_response')";
  169. // mysql_query($sql);
  170. // }
  171. // var_dump($buffer);
  172. // echo 'origin '.$event['origin'].'<br>';
  173. // echo mb_substr($event['origin'],8,4).'<br>';
  174. $shows_time = mb_substr($event['origin'],8,4);
  175. $shows_time = mb_substr($shows_time,0,2).':'.mb_substr($shows_time,2,2).':00';
  176. $year = mb_substr($event['origin'],0,4);
  177. $month = mb_substr($event['origin'],4,2);
  178. $day = mb_substr($event['origin'],6,2);
  179. $date = "$year-$month-$day";
  180. $shows_min_price = $event['minPrice'];
  181. $shows_max_price = $event['maxPrice'];
  182. $prices = $shows_min_price.', '.$shows_max_price;
  183. // $date = date('Y-m-d H:i:s');
  184. // $sql = "INSERT INTO `temp_auto_films_cmd`(`id`, `film_id`, `site_id`, `site_index`, `event_id`, `hall_id`, `date`, `time`, `prices`, `technology`, `eventmap`) VALUES (null,$id_film_export,'$site_id',$site_index,$event_id,$hall_id,'$date_premiere','$shows_time','$prices','$technology', '$map_response')";
  185. $sql = "INSERT INTO `temp_auto_films_cmd`(`id`, `film_id`, `site_id`, `site_index`, `event_id`, `hall_id`, `date`, `time`, `prices`, `technology`, `eventmap`) VALUES (null,$id_film_export,'$site_id',$site_index,$event_id,$hall_id,'$date','$shows_time','$prices','$technology', '$map_response')";
  186. // echo $sql.'<br><br>';
  187. $result = mysql_query($sql);
  188. }
  189. }
  190. }
  191. }
  192. function get_eventmap($session_id,$site_id,$event_id){
  193. // $map_url = 'http://server.megakino.com.ua/gate/eventmap?sessionid='.$session_id.'&amp;siteId='.$site_id.'&amp;eventId='.$event_id;
  194. $site_id = trim($site_id);
  195. $event_id = trim($event_id);
  196. $map_url = 'http://server.megakino.com.ua/gate/eventmap?sessionid='.$session_id.'&eventId='.$event_id.'&siteId='.$site_id;
  197. echo 'map_url '.$map_url.'<br>';
  198. $buffer = file_get_contents($map_url);
  199. return $buffer;
  200. // $response = json_decode($buffer,true);
  201. }
  202. function set_shows(){
  203. mysql_query("TRUNCATE `grifix_main_shows2`");
  204. $sql = "SELECT id_film_export,id_film FROM grifix_main_auto_films WHERE id_export = 8 AND id_film != 0";
  205. $res = mysql_query($sql);
  206. if(mysql_num_rows($res)){
  207. while($arr = mysql_fetch_assoc($res)):
  208. $id_film_export = $arr['id_film_export'];
  209. $film_id = (int)$arr['id_film'];
  210. if(!$film_id)
  211. continue;
  212. $sql = "SELECT site_id, site_index, event_id, hall_id, date, time, prices, technology, eventmap FROM temp_auto_films_cmd WHERE film_id = $id_film_export";
  213. $res_shows = mysql_query($sql);
  214. if($res_shows && mysql_num_rows($res_shows)){
  215. while($arr = mysql_fetch_assoc($res_shows)){
  216. $site_id = trim($arr['site_id']);
  217. $site_index = $arr['site_index'];
  218. $event_id = trim($arr['event_id']);
  219. $hall_id = $arr['hall_id'];
  220. $date = $arr['date'];
  221. $time = $arr['time'];
  222. $prices = $arr['prices'];
  223. $technology = $arr['technology'];
  224. $sql_temp = "SELECT id_hall FROM grifix_main_auto_cinema WHERE id_hall_export = $hall_id AND id_cinema_export = '$site_id' AND id_export = 8 LIMIT 1";
  225. // echo $sql_temp.'<br>';
  226. $res_temp = mysql_query($sql_temp);
  227. $hall_id_self = (int)mysql_result($res_temp,0,'id_hall');
  228. $sql = "INSERT INTO `grifix_main_shows2`(`id`, `hall_id`, `film_id`, `partner_id`, `begin`, `end`,`event_id`) VALUES (null,$hall_id_self,$film_id,8,'$date','$date',$event_id)";
  229. mysql_query($sql);
  230. if($hall_id_self){
  231. // $sql_count = "SELECT COUNT(*) AS num FROM grifix_main_shows WHERE begin = '$date' AND hall_id = $hall_id AND film_id = $film_id";
  232. // $sql_count = "SELECT COUNT(*) AS num FROM grifix_main_shows WHERE hall_id = $hall_id_self AND film_id = $film_id AND partner_id = 8 AND begin = '$date' AND end = '$date'";
  233. $sql_count = "SELECT COUNT(*) AS num FROM grifix_main_shows WHERE event_id = $event_id";
  234. echo $sql_count.'<br>';
  235. $res_count = mysql_query($sql_count);
  236. if($res_count && mysql_result($res_count,0,'num')){
  237. }
  238. else{
  239. $sql = "INSERT INTO `grifix_main_shows`(`id`, `hall_id`, `film_id`, `partner_id`, `begin`, `end`, `event_id`) VALUES (null,$hall_id_self,$film_id,8,'$date','$date',$event_id)";
  240. mysql_query($sql);
  241. $show_id = mysql_insert_id();
  242. if($show_id){
  243. if($technology == '3D' || $technology == '3d')
  244. $_3d = 1;
  245. else
  246. $_3d = 0;
  247. $sql = "INSERT INTO `grifix_main_shows_times`(`id`, `show_id`, `time`, `prices`, `3D`, `may3D`, `sale_id`, `sale_status`) VALUES (null,$show_id,'$time','$prices',$_3d,$_3d,0,'')";
  248. mysql_query($sql);
  249. $eventmap = trim($arr['eventmap']);
  250. if($eventmap){
  251. $eventmap = mysql_real_escape_string($eventmap);
  252. $sql = "INSERT INTO `grifix_main_shows_eventmap`(`show_id`, `event_id`, `site_id`, `response`) VALUES ($show_id,$event_id,'$site_id','$eventmap')";
  253. echo $sql.'<br>';
  254. mysql_query($sql);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. endwhile;
  262. }
  263. else{
  264. return false;
  265. }
  266. }
  267. function get_show($film_id,$session_id){
  268. $sql = "SELECT id_film_export FROM grifix_main_auto_films WHERE id_film = $film_id";
  269. $res = mysql_query($sql);
  270. if(mysql_num_rows($res)){
  271. $id_film_export = mysql_result($res,0,'id_film_export');
  272. $sql = "SELECT site_id, event_id, hall_id, date, time, prices FROM temp_auto_films_cmd WHERE film_id = $id_film_export";
  273. // echo $sql.'<br>';
  274. $res_shows = mysql_query($sql);
  275. if(mysql_num_rows($res_shows)){
  276. while($arr = mysql_fetch_assoc($res_shows)){
  277. $site_id = $arr['site_id'];
  278. $event_id = $arr['event_id'];
  279. $hall_id = $arr['hall_id'];
  280. $date = $arr['date'];
  281. $time = $arr['time'];
  282. $prices = $arr['prices'];
  283. $res_temp = mysql_query("SELECT id_hall FROM grifix_main_auto_cinema WHERE id_hall_export = $hall_id AND id_cinema_export = '$site_id' LIMIT 1");
  284. $hall_id_self = mysql_result($res_temp,0,'id_hall');
  285. if($hall_id_self){
  286. $sql = "INSERT INTO `grifix_main_shows`(`id`, `hall_id`, `film_id`, `partner_id`, `begin`, `end`) VALUES (null,$hall_id_self,$film_id,8,'$date','$date')";
  287. // echo $sql.'<br>';
  288. mysql_query($sql);
  289. $show_id = mysql_insert_id();
  290. if($show_id){
  291. $sql = "INSERT INTO `grifix_main_shows_times`(`id`, `show_id`, `time`, `prices`, `3D`, `may3D`, `sale_id`, `sale_status`) VALUES (null,$show_id,'$time','$prices',0,0,0,'')";
  292. // echo $sql.'<br>';
  293. mysql_query($sql);
  294. $record_id = mysql_insert_id();
  295. if($record_id){
  296. mysql_query("INSERT INTO `grifix_main_shows_times_lng`(`record_id`, `lang_id`, `note`) VALUES ($record_id,1,'')");
  297. mysql_query("INSERT INTO `grifix_main_shows_times_lng`(`record_id`, `lang_id`, `note`) VALUES ($record_id,1,'')");
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. else{
  305. return false;
  306. }
  307. }
  308. ?>