<?php
// возвращает список тем/разделов КЗ для jsfinder
class c_services_events_quiz_get_themes
{
  function show()
  { $event_id=$_GET['event_id'] ;
    $only_event_themes=($_GET['only_event_themes'])? 1:0 ;
  	if (!$event_id) return(json_encode([])) ;

    $rec_event=EVENTS()->get_event_by_id($_GET['event_id']) ;
    $arr_themes_ids=(_sizeof($rec_event['_ext_info']['themes_ids']))? array_keys($rec_event['_ext_info']['themes_ids']):[];

	  //echo '$arr_themes_ids='.print_r($arr_themes_ids).'<br>' ;

	if (!isset($_GET['id'])) $id=1 ; else  $id=(int)$_GET['id'] ;
	if (!$id) $id=1 ;

  $show_checkbox=($only_event_themes)? 0:1 ;


	$recs_themes=execSQL('select pkey,obj_name,parent,level,
				  		  (select count(t2.pkey) from quiz_themes t2 where t2.parent=t1.pkey and t2.clss in (1,472)) as cnt_subsections
				  		  from quiz_themes t1 
				  		  where clss in (1,472) and parent='.$id) ;

  $recs_themes_parent=execSQL('select pkey as id,parent1,parent2,parent3,parent4,parent5,parent6,parent7,parent8,parent9 from quiz_themes where pkey in ('.implode(',',array_keys($recs_themes)).')') ;
  /*if (_sizeof($recs_themes_parent)) foreach($recs_themes_parent as $i=>$id)
                {  foreach($recs as $rec) if ($id!=$rec['pkey'])
                   { if (!($id!=$rec['parent1'] and $id!=$rec['parent2'] and $id!=$rec['parent3']  and $id!=$rec['parent4']  and $id!=$rec['parent5']  and $id!=$rec['parent6']  and $id!=$rec['parent7']  and $id!=$rec['parent8']   and $id!=$rec['parent9'])) unset($arr_result[$i]) ;
                   }
                }
*/
    $res=array() ;
    if (_sizeof($recs_themes)) foreach($recs_themes as $rec)
    {   $checked=(_sizeof($arr_themes_ids) and in_array($rec['pkey'],$arr_themes_ids))? 1:0 ;
      $view_item=0 ;
      if (!$only_event_themes) $view_item=1 ;
      else
      {   // проверяем показ тема при прямом указании ID
          $view_item=(_sizeof($arr_themes_ids) and (in_array($rec['pkey'],$arr_themes_ids)))? 1:0 ;
          // проверяем показ темы при указании ID родительского раздела
          if (_sizeof($arr_themes_ids)) foreach($arr_themes_ids as $theme_id) if (in_array($theme_id,$recs_themes[$rec['pkey']])) $view_item=1 ;

      }
    	if ($view_item) $res[]=array('id'=>$rec['pkey'],'label'=>$rec['obj_name'],'type'=>'section','parent'=>$rec['parent'],'show_checkbox'=>$show_checkbox,'checked'=>$checked,'has_children'=>($rec['cnt_subsections'])? true:false) ;
    }

    echo json_encode($res) ;

  }
}
