<?php
    include_once '../../account/session.php';
    include_once '../../../scripts/bd.php';
    
    if(isset($_GET['action'])){
        $action=$_GET['action'];
        if($action=='add')add();
        if($action=='show')show();
        if($action=='get')get();
        if($action=='set')set();
        if($action=='set_active_on')set_active_on();
        if($action=='set_active_off')set_active_off();
        if($action=='get_news')get_news();
        if($action=='get_data_tables_not_linked')get_data_tables_not_linked();
        if($action=='get_data_tables')get_data_tables();
   }
   
   function show(){
      refresh_count();
      $list=array();
      if($_GET["id_news"]==0){
          $data= bd::query("SELECT id,name,data_table,count,active FROM data_list WHERE id_news='{$_SESSION['id-news']}'");
      }else{
          $data= bd::query("SELECT id,name,count,active FROM data_list WHERE id_news='{$_SESSION['id-news']}'");
      }
      while ($row = mysql_fetch_object($data)) {
          $list[]=$row;
      }
      echo json_encode($list);
   }
   
   function get_news(){
      $data=array();
      $query = bd::query("SELECT id,name FROM data_news WHERE id='{$_SESSION['id-news']}'");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function refresh_count(){
       $query= bd::query("SELECT id,data_table FROM data_list WHERE id_news='{$_SESSION['id-news']}'");
       while ($row = mysql_fetch_array($query)) {
           $queryx=bd::query("SELECT COUNT(*) FROM {$row['data_table']}");
           if(mysql_num_rows($queryx)>0){
               $rowx=mysql_fetch_row($queryx);
               bd::query("UPDATE data_list SET count='$rowx[0]' WHERE id='{$row['id']}'");
           }else{
               bd::query("UPDATE data_list SET count='-1' WHERE id='{$row['id']}'");
           }
           
       }
   }
 
   

