<?php
include_once("c_events_media_card_db.php");
class c_page_cab_events_media_card_db_actions extends c_page_cab_events_media_card_db
{
  public $h1='Доведение контента - Операции' ;

  function block_main()
  { $this->page_title() ;
    ?><button class="button v1" cmd="check_personals_dublicate">Проверить дубликаты сотрудников</button><?
  }

  function CMD_check_personals_dublicate()
  {   $stats=[] ;
      $recs=execSQL_row('select personal_id,count(pkey) as cnt from obj_site_events_pass where event_id='.$this->rec_event['pkey'].' group by personal_id having cnt>1') ;
      if (_sizeof($recs)) foreach($recs as $personal_id=>$cnt) $stats[$cnt][]=$personal_id ;
      if (_sizeof($stats))
      { foreach($stats as $cnt=>$personal_ids) echo '<strong>'.$cnt.' дубл. </strong> - '._sizeof($personal_ids).' сотрудников:<br>'.implode(', ',$personal_ids).'<br><br>' ;
            ?><button class="button v1 left" cmd="remove_personals_dublicate">Удалить дубликаты сотрудников</button><div class="clear"></div><br><?

              SETUP()->create_temporary_table('temp_event_personals_dubl','select personal_id,count(pkey) as cnt from obj_site_events_pass where event_id='.$this->rec_event['pkey'].' group by personal_id having cnt>1') ;
              SETUP()->create_temporary_table('temp_event_pass_dubl','
                                 select t2.pkey,t2.status,t2.c_data,t2.personal_id
                                 from temp_event_personals_dubl t1
                                 left join obj_site_events_pass t2 on t2.personal_id=t1.personal_id 
                                 where t2.event_id='.$this->rec_event['pkey'].'
                                 group by t2.personal_id
                                 order by t2.personal_id,t2.status,t2.c_data') ;

              $recs=execSQL('select t2.pkey as pass_id,t2.personal_id,t2.status,t1.status as _status,t1.pkey as _pass_id,t2.c_data 
                             from temp_event_pass_dubl t1
                             left join obj_site_events_pass t2 on t2.personal_id=t1.personal_id and t2.event_id='.$this->rec_event['pkey'].' 
                             order by t1.personal_id,t2.status,t2.c_data',0,1) ;
              //print_2x_arr($recs);
              if (_sizeof($recs))
              {
                  ?><table class="basic fz_small auto">
                        <tr>
                            <th>%</th>
                            <th>pass_id</th>
                            <th>personal_id</th>
                            <th>status</th>
                            <th>Дата создания</th>
                            <th></th>
                        </tr>
                        <?foreach($recs as $i=>$rec)
                        {
                        ?>
                        <tr>
                            <td><?echo $i+1?></td>
                            <td><?echo $rec['pass_id']?></td>
                            <td><?echo $rec['personal_id']?></td>
                            <td><?echo $rec['status']?></td>
                            <td><?echo date('d.m.Y H:i',$rec['c_data'])?></td>
                            <td><?if ($rec['pass_id']==$rec['_pass_id']) echo '<div class="red">Дубликат будет удален</div>'?></td>
                        </tr><?}?>

                    </table><?
              }



      }
          else echo 'Дубликаты отсутствуют<br>' ;

  }

  function CMD_remove_personals_dublicate()
  {   $stats=[] ;
      SETUP()->create_temporary_table('temp_event_personals_dubl','select personal_id,count(pkey) as cnt from obj_site_events_pass where event_id='.$this->rec_event['pkey'].' group by personal_id having cnt>1') ;
      SETUP()->create_temporary_table('temp_event_pass_dubl','
               select t2.pkey,t2.status,t2.c_data,t2.personal_id
               from temp_event_personals_dubl t1
               left join obj_site_events_pass t2 on t2.personal_id=t1.personal_id 
               where t2.event_id='.$this->rec_event['pkey'].'
               group by t2.personal_id
               order by t2.personal_id,t2.status,t2.c_data') ;
      execSQL('select * from temp_event_pass_dubl',1) ;
      execSQL_update('delete from obj_site_events_pass where event_id='.$this->rec_event['pkey'].' and pkey in (select pkey from temp_event_pass_dubl)',array('debug'=>2)) ;
  }
}
?>
