<?php 
session_start();
foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 

// if($edit==1)

$cong_codigo=$_SESSION['cong_codigo'];
if(!empty($cong)){$cong_codigo=$cong;}
$_SESSION['cong_codigo']=$cong_codigo;

include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];
$user0=data_maes('user',$user_codigo);

$cong0=data_maes('cong',$cong_codigo);
$lang='es';

// for($idx=-40; $idx<=16; $idx++)
for($idx=0; $idx<=16; $idx++)
{
     $fecha=date('Y-m-d',strtotime ('last Monday +'.($idx*7).' days'));
     $panio=substr($fecha,0,4);	
     $pmes=substr($fecha,5,2);	
     $pdia=substr($fecha,8,2);	

     $tdb1='';
     $tdb2='';
     $nvc1='';
     $nvc2='';
     $smm0='';
     $smm1='';
     $smm2='';
     $smm3='';
     $smm4='';
     $lib1='';
     $ata1='';
     
     // https://wol.jw.org/en/wol/meetings/r1/lp-e/2023/38     
     $jworg='http://wol.jw.org';
     $jworg1=$jworg.'/es/wol/dt/r4/lp-s/'.$panio.'/'.$pmes.'/'.$pdia;

     $page=file_get_contents($jworg1);
     $html=str_get_html($page);

     echo '<br>==>'.$fecha;  
     echo '<br>==>'.$jworg1;  

//                $html2=preg_replace('#<div class="gen-field">(.*?)</h1>#', '', $html2);
//                     $html3 = preg_replace('#<div id="desc">(.*?)</div>#', '', $html);                    
     
     
     if($fecha<="2023-12-31")
     foreach($html->find('div') as $element)
     {
          if($element->id=='section2')
          {
//                echo '<br>'.$element;
               $xli=0;
               $element=preg_replace('#<div class="gen-field"(.*?)</div>#', '', $element);
               $html2=str_get_html($element);
               foreach($html2->find('li') as $element2)
               {
                    $xli++;
                    $value=$element2->innertext;
                    
                    
//                     $value = str_replace('href="','target="_blank" tabindex="-1" href="'.$jworg,$value);
                    $value = str_replace('href="','target="_blank" tabindex="-1" href="',$value);
                    $value = str_replace('href="/','target="_blank" tabindex="-1" href="'.$jworg.'/',$value);
                    $value = str_replace('<div','<div tabindex="-1" ',$value);
                    $value = str_replace('):','):<br>',$value);
                    $value=str_replace("'","\'",$value);
                    $value=str_replace('"','\"',$value);
                    

                    if($xli==1){$tdb1=str_get_html($value);}
                    if($xli==2){$tdb2=str_get_html($value);}
//                     if($xli==3)
                    {$smm0=str_get_html($value);}
               }
               
          }
          
          if($element->id=='section3')
          {
//                echo '<br>'.$element;
               $xli=0;
               $html2=str_get_html($element);
               foreach($html2->find('li') as $element2)
               {
                    $xli++;
                    
                    $value=$element2->innertext;
//                     $value = str_replace('href="','target="_blank" tabindex="-1" href="'.$jworg,$value);
                    $value = str_replace('href="','target="_blank" tabindex="-1" href="',$value);
                    $value = str_replace('href="/','target="_blank" tabindex="-1" href="'.$jworg.'/',$value);
                    $value = str_replace('<div','<div tabindex="-1" ',$value);
                    $value = str_replace('):','):<br>',$value);
                    $value=str_replace("'","\'",$value);
                    $value=str_replace('"','\"',$value);
                    
                    if($xli==1){$smm1=str_get_html($value);}
                    if($xli==2){$smm2=str_get_html($value);}
                    if($xli==3){$smm3=str_get_html($value);}
               }
          }
          
          if($element->id=='section4')
          {
//                echo '<br>'.$element;
               $xli=0;
               $html2=str_get_html($element);
               foreach($html2->find('li') as $element2)
               {
                    $xli++;
                    $value=$element2->innertext;
//                     $value = str_replace('href="','target="_blank" tabindex="-1" href="'.$jworg,$value);
                    $value = str_replace('href="','target="_blank" tabindex="-1" href="',$value);
                    $value = str_replace('href="/','target="_blank" tabindex="-1" href="'.$jworg.'/',$value);
                    $value = str_replace('<div','<div tabindex="-1" ',$value);
                    $value = str_replace('):','):<br>',$value);
                    $value=str_replace("'","\'",$value);
                    $value=str_replace('"','\"',$value);
                    $value=str_replace('¿','<br>¿',$value);
                    
                    
                    $html3=str_get_html($value);
                    $value='';
                    foreach($html3->find('p') as $element3)
                    {
                         $value.=$element3->innertext;
                    }
                    
                    
                    if($xli==2){$nvc1=str_get_html($value);}
                    if($xli==3){$nvc2=str_get_html($value);}
                    
                    
                    if(mb_stristr($nvc2,'Estudio bíblico de la congregación')){$nvc2='';}
                    
//                     echo '<br>'.$xli.' : '.$value;
               }
          }
          
          if($element->class=='groupTOC')
          {
               $value=$element->innertext;
//                     $value = str_replace('href="','target="_blank" tabindex="-1" href="'.$jworg,$value);
                    $value = str_replace('href="','target="_blank" tabindex="-1" href="',$value);
                    $value = str_replace('href="/','target="_blank" tabindex="-1" href="'.$jworg.'/',$value);
               $value = str_replace('<div','<div tabindex="-1" ',$value);
               $value=str_replace("'","\'",$value);
               $value=str_replace('"','\"',$value);
               $ata1=$value;
          }
     }
     
     $nume=0;
     $a_mins=array();
     foreach($html->find('p') as $element)
     {
//           if(mb_stristr($element,'mins.)')){$a_mins[$nume++]=$element;}
          if(mb_stristr($element,'mins.)') or mb_stristr($element,'1 min.)')){$a_mins[$nume++]=$element;  }
     }
     
     
     $xseccion=0;
     
     $lib1_ok=0;
     
     foreach($html->find('h3') as $element)
     {
          
          if(mb_stristr($element,'color--teal')){$xseccion='1';} // tesoros     
          if(mb_stristr($element,'color--gold')){$xseccion='2';} // maestros
          if(mb_stristr($element,'color--maroon')){$xseccion='3';} // vida     
             
          foreach($element->find('strong') as $element2)
          {
               $nume=substr($element2->innertext,0,2);
               $nume=(int)$nume;
               $text=trim(substr($element2->innertext,0));
//                echo '<br>=== '.$element2->innertext;
//                echo '<br>=== '.$text;
               
               if($nume!=0)
               {
                    $value=$element;
                    $value=$element.$a_mins[$nume-1];
                    
                    if(mb_stristr($value,'Estudio bíblico de la congregación'))
                    {$value=$a_mins[$nume-1]; $lib1_ok=1;}

                    
                
                    
                    $value = str_replace('<em>','',$value);
                    $value = str_replace('</em>','',$value);

                    
                    
//                     $jworg1 = str_replace('<em>','',$jworg1);
//                     $jworg1 = str_replace('</em>','',$jworg1);
//                     $jworg1 = str_replace('"','',$jworg1);
                    
                    $text = str_replace('<em>','',$text);
                    $text = str_replace('</em>','',$text);
                    $text = str_replace('"','',$text);

                    if($xseccion==3){$text=$nume.'.';}
                    
//                     if($xseccion==2)
                    {
                         $value=str_replace('<strong>','<strong>'.'<a href="'.$jworg1.'#:~:text='.$text.'">',$value);
                         $value=str_replace('</strong>','</a></strong>',$value);
                    }
                    
                    {
                         $value=str_replace('<strong','<span',$value);
                         $value=str_replace('</strong>','</span>',$value);
                    }
                    
                    
                    $value = str_replace('href="','target="_blank" tabindex="-1" href="',$value);
                    $value = str_replace('href="/','href="'.$jworg.'/',$value);
                    $value = str_replace('<h3','<div',$value);
                    $value = str_replace('h3>','div>',$value);
                    
                    
//                     $value=str_replace('">','" >',$value);
                    
//                     echo '<br>===='.$value;
                    $value=str_replace("'","\'",$value);
                    $value=str_replace('"','\"',$value);
//                     echo '<br>===='.$value;
//                     $value='<div>'.$value.'</div>';
                    
                    
                    if($nume==1){$tdb1=$value;}
                    if($nume==2){$tdb2=$value;}
                    if($nume==3){$smm0=$value;}

                    if($xseccion==2)
                    {
                         if($nume==4){$smm1=$value;}
                         if($nume==5){$smm2=$value;}
                         if($nume==6){$smm3=$value;}
                         if($nume==7){$smm4=$value;}
                    }
                    
                    if($xseccion==3)
                    {
                         if($nume==6){$nvc1=$value;}
                         if($lib1_ok==0)
                         {
                              if($nume==7 and empty($nvc1)){$nvc1=$value;}
                              if($nume==7 and !empty($nvc1)){$nvc2=$value;}

                              if($nume==8 and empty($nvc1)){$nvc1=$value;}
                              if($nume==8 and !empty($nvc1)){$nvc2=$value;}

                              if($nume==9 and empty($nvc1)){$nvc1=$value;}
                              if($nume==9 and !empty($nvc1)){$nvc2=$value;}
                         }
                         if($nvc1==$nvc2){$nvc2='';}
                         
//                          if(mb_stristr($nvc2,'Estudio bíblico de la congregación')){$nvc2='';}
//                          if($lib1_ok==1){$nvc2='';}
                         
                    }
                    
//                     if(mb_stristr($value,'Estudio bíblico de la congregación'))
//                     {$lib1=$value;}
                    
                    if($lib1_ok==1){$lib1=$value;}
                    
               }
          }
          
     }
     
     if($fecha>="2024-01-01")
     {
          foreach($html->find('div') as $element)
          {
               if($element->class=='groupTOC')
               {
                    $value=$element->innertext;
                    $value = str_replace('href="','target="_blank" tabindex="-1" href="',$value);
                    $value = str_replace('href="/',' href="'.$jworg.'/',$value);
                    $value = str_replace('<div','<div tabindex="-1" ',$value);
                    $value = str_replace('<h3','<strong',$value);
                    $value = str_replace('h3>','strong>',$value);

                    {
                         $value=str_replace('<strong','<span',$value);
                         $value=str_replace('</strong>','</span>',$value);
                    }
                    
                    
                    $delal=$value;
//                     $delal=substr($delal,strpos($delal,'(del'));
                    $delal=substr($delal,strpos($delal,'Artículo de estudio'));
                    $delal=substr($delal,0,strpos($delal,')')+1);
                    $value=str_replace($delal,'',$value);
                    
                    $value=str_replace("'","\'",$value);
                    $value=str_replace('"','\"',$value);
                    $ata1=$value;
               }
          }
     }
     
     
     $jwib_sql='select * from jwol0 where jwol_fecha="'.$fecha.'"';
     $jwib_res=msql_query($jwib_sql,$link_data);
     if(msql_num_rows($jwib_res)==0)
     {
          $jwib_sql='insert into jwol0(jwol_codigo,jwol_fecha,jwol_idate)value("'.$fecha.'","'.$fecha.'",now())';
          $jwib_res=msql_query($jwib_sql,$link_data);
     }
     
//      if(!empty($tdb1))
     {
          $jwib_sql='update jwol0 set ';
          $jwib_sql.='jwol_tdb1="'.$tdb1.'",';
          $jwib_sql.='jwol_tdb2="'.$tdb2.'",';
          $jwib_sql.='jwol_nvc1="'.$nvc1.'",';
          $jwib_sql.='jwol_nvc2="'.$nvc2.'",';
          $jwib_sql.='jwol_smm0="'.$smm0.'",';
          $jwib_sql.='jwol_smm1="'.$smm1.'",';
          $jwib_sql.='jwol_smm2="'.$smm2.'",';
          $jwib_sql.='jwol_smm3="'.$smm3.'",';
          $jwib_sql.='jwol_smm4="'.$smm4.'",';
          $jwib_sql.='jwol_lib1="'.$lib1.'",';
          $jwib_sql.='jwol_ata1="'.$ata1.'",';
          $jwib_sql.='jwol_status="-" ';
          $jwib_sql.='where jwol_fecha="'.$fecha.'" ';
          $jwib_res=msql_query($jwib_sql,$link_data);
     }
     
     $smm1='';
     $smm2='';
     $smm3='';
     $smm4='';
     $nvc1='';
     $nvc2='';
     
//      echo '<br>=='.$jwib_sql;
     
     
     
//      	echo '<br>'.$fecha;
//           echo '<br>'.'TDB';
//           echo '<br>'.$tdb1;
//           echo '<br>'.$tdb2;
//           echo '<br>'.'SMM';
//           echo '<br>'.$smm0;
//           echo '<br>'.$smm1;
//           echo '<br>'.$smm2;
//           echo '<br>'.$smm3;
//           echo '<br>'.'NVC';
//           echo '<br>'.$nvc1;
//           echo '<br>'.$nvc2;
//           echo '<br>'.$lib1;
//           echo '<br>'.$ata1;
     
     
//      echo '<br>'.$fecha;  
//      echo '<br>'.$jworg1;  
//      echo '<br>'.$html;  
     
     
     
     
}


?>


