'休息', 'LUNCH'=>'午餐','DINNER'=>'晚餐', 'QA'=>'問答時間', 'HAPPY'=>'歡樂交流', 'FREE'=>'自由演練'); } function _week_course_text2ary($s){ $a = array(); $valid_field_ary = array( 'C.S', #new sub-course 'PS','P','C', 'C.L', #new speaker(lecturers) 'VA','MERGE' ,'BREAK','LUNCH','DINNER','QA','HAPPY','FREE'); $hard_code_field = _week_course_hard_code_fields(); foreach(explode('`', $s) as$v){ $v = trim($v); foreach($valid_field_ary as$field){ if(strpos($v,$field)===0){ if(strpos($v,'MERGE')===0){ preg_match('/[0-9]+/', $v, $m); $a['MERGE'] = $m ? $m[0] : 2;#default is 2 }else{ $is_hard_codetext = false; foreach($hard_code_field as$code=>$codetext){ if(strpos($v,$code)===0){ $a[$code] = $codetext; $is_hard_codetext=true; break; } } if(!$is_hard_codetext){ $key = substr($v, 0, strpos($v, ' ')); $a[$key] = substr($v, strpos($v,' ')); } } } } } return$a; } function _week_course_text($s, $ary_speakers, $previous_texts = array()){ # $a = array(); if($previous_texts!==FALSE and is_array($previous_texts)){ foreach($previous_texts as $text){ $previous = _week_course_text2ary($text, FALSE); # DO NOT up-inherit unset($previous['VA']); unset($previous['MERGE']); unset($previous['BREAK']); unset($previous['LUNCH']); unset($previous['DINNER']); unset($previous['QA']); unset($previous['HAPPY']);unset($previous['FREE']); $a = array_merge($a, $previous); } } $original_C = isset($a['C']) ? $a['C'] : false; $current_a = _week_course_text2ary($s); $IS_NEED_CLEAN_C_ATTR = false; if($original_C and isset($current_a['C']) and $current_a['C'] and $current_a['C']!=$original_C){ // Let's do refresh/clean C.S and C.L value. $IS_NEED_CLEAN_C_ATTR = true; } if($IS_NEED_CLEAN_C_ATTR){ unset($a['C.S']); unset($a['C.L']); } $a = array_merge($a, $current_a); // print_r($previous_texts);echo'---'; // print_r($a);echo'
'."\n\n\n\n"; // return $s; $ret = array('conf'=>$a); // 20180119 $a_keys = array_keys($a); foreach(_week_course_hard_code_fields() as$code=>$codetext){ if(in_array($code, $a_keys)){ $ret = array_merge($ret, array('text'=>$codetext)); return $ret; } } $C = isset($a['C'])&&$a['C'] ? $a['C'] : ''; # if(isset($_GET['e'])){print_r($a);echo'----';var_dump(strpos($C,' #'));} $C = trim($C); if($C and strpos($C, ' #')>0){ $_ = explode(' #',$C); $href = trim($_[1]); $text = trim($_[0]); $C = ''.$text.''; } $C = $C ? '
' . $C . '
' : ''; # $SC = isset($a['C.S'])&&$a['C.S'] ? $a['C.S'] : ''; $SC = trim($SC); if($SC and strpos($SC, ' #')>0){ $_ = explode(' #',$SC); $href = trim($_[1]); $text = trim($_[0]); $SC = ''.$text.''; } $SC = $SC ? '
'.$SC.'
' : ''; # $S = isset($a['C.L'])&&$a['C.L'] ? $a['C.L'] : ''; $S = trim($S); if($S){ $S1=''; $S2=''; if(strpos($S, '&')!==false){ $_ = explode('&', $S); $S1 = trim($_[0]); $S2 = trim($_[1]); }else{ $S1 = $S; } if($S1 and isset($ary_speakers[$S1]) and $ary_speakers[$S1]){ $S1 = ''.$S1.''; } if($S2 and isset($ary_speakers[$S2]) and $ary_speakers[$S2]){ $S2 = ''.$S2.''; } $S=$S1; if($S2){$S.=' & '.$S2;} } // move to wkplace $S = $S ? '
'.$S.'
' : ''; # $P = isset($a['P'])&&$a['P'] ? $a['P'] : ''; $P = trim($P); if($P and strpos($P, ' #')>0){ $_ = explode(' #',$P); $href = trim($_[1]); $text = trim($_[0]); $P = ''.$text.''; } $P = $P ? '
'.$S.($S ? ' @ ' : '').$P.'
' : ''; $VA = isset($a['VA'])&&$a['VA'] ? $a['VA'] : ''; $VA = $VA ? '
'.$VA.'
' : ''; $ret = array_merge($ret, array('text'=>$C.$SC.$P.$VA)); return $ret; } function _week_course_date($date){ $date0 = str_replace('/', '-', $date); $w = date('w', strtotime($date0)); $w = get_chinese_number($w); return substr($date,5) . ' ('.$w.')'; } function week_course_func($atts=array()){ $d = isset($atts['d']) ? $atts['d'] : ''; $gid=isset($atts['gid']) ? $atts['gid'].'' : '0'; $speaker_d = isset($atts['speaker_d']) ? $atts['speaker_d'] : '1DXuJE2n9Is34NeAlQgcQ9-IvfRBX0n7dwfYnXxb6WUM'; $speaker_gid = isset($atts['speaker_gid']) ? $atts['speaker_gid'] : '0'; if(!$d or strlen($gid)<=0){ return''; } $user = wp_get_current_user(); $google_link = ''; if(isset($_GET['sheet']) || ($user && isset($user->data) && isset($user->data->user_login) && in_array($user->data->user_login, array('swc','kevin','jim','cheryl','kate','rusi','sean','vansindata'))) ){ $google_link='https://docs.google.com/spreadsheets/d/'.$d.'/edit#gid='.$gid; } $speaker = '';$ary_speakers = array(); if($speaker_d and strlen($speaker_gid)>0){ $speaker = getHtml('https://docs.google.com/spreadsheets/d/'.$speaker_d.'/export?format=tsv&gid='.$speaker_gid); } if($speaker){ foreach(preg_split('/\n|\r\n?/', $speaker) as $v){ $_ = explode("\t",$v); if(!isset($_[1]) or empty($_[1])){continue;} $speaker_href = $_[1]; $speaker_href = trim($speaker_href); if(strpos($speaker_href,'http')===0){ $speaker_name = str_replace(array("\r","\n","\t"),'',$_[0]); $ary_speakers[$speaker_name] = $speaker_href; } } } #$bgcolor = isset($atts['bgcolor']) ? explode('-',$atts['bgcolor']) : array('','','#DAA2A2','#ddd','#EFD0A7','#ddd','#B5C8F4','#ddd','#DAE3BC','#DAE3BC'); $bgcolor = isset($atts['bgcolor']) ? explode(',',$atts['bgcolor']) : array('','','#adf','','#adf','','#adf','','#adf','#adf','#adf','#adf'); $s = getHtml('https://docs.google.com/spreadsheets/d/'.$d.'/export?format=csv&gid='.$gid); $s = preg_replace('/\n|\r\n?/', '`', $s); $s = preg_replace('/`WEEKDAY/i', "\nWEEKDAY",$s); $s = preg_replace('/`([0-2][0-9])/', "\n$1", $s); $s = preg_replace('/`,/', "\n,", $s); $ary = array(); $START_POSSIBLE_HAS_MERGE_ROWIDX = 1; $MERGE_DEFAULT_VALUE = 2; foreach(preg_split('/\n|\r\n?/',$s)as$k=>$v){ # $k is row-index if($k==1){continue;}/*pass weekday row*/ $a = str_getcsv($v); $ary[] = $a; } // echo'
';print_r($ary);exit();
  $s = '';
  foreach($ary as$rowidx=>$a){
    if(!$a[0]){break;}
    $style = isset($bgcolor[$rowidx])&&$bgcolor[$rowidx] ? ' style="background:'.$bgcolor[$rowidx].'"' : '';
    if($rowidx===0){
      $s.='';
$s.=''.($a[0]=='DATE' ? '日期' : '').'';
$s.=''._week_course_date($a[1]).'';
if(isset($a[2]) && $a[2]){$s.=''._week_course_date($a[2]).'';}
if(isset($a[3]) && $a[3]){$s.=''._week_course_date($a[3]).'';}
if(isset($a[4]) && $a[4]){$s.=''._week_course_date($a[4]).'';}
if(isset($a[5]) && $a[5]){$s.=''._week_course_date($a[5]).'';}
$s.='';
      continue;
    }

    $a1_prevconfigs = array(); # _prevconfigs SHOULD RENAME to _prevtexts
    $a2_prevconfigs = array();
    $a3_prevconfigs = array();
    $a4_prevconfigs = array();
    $a5_prevconfigs = array();
    for($i=1;$i<$rowidx;$i++){
      $a1_prevconfigs[$i] = $ary[$i][1];
      $a2_prevconfigs[$i] = $ary[$i][2];
      $a3_prevconfigs[$i] = $ary[$i][3];
      $a4_prevconfigs[$i] = $ary[$i][4];
      $a5_prevconfigs[$i] = $ary[$i][5];
    }
    $td0 = $a[0];
    foreach(range(1,5)as$tdnum){
      ${'td'.$tdnum} = $a[$tdnum];
      if($td0!='DATE' && $td0!='WEEKDAY'){
        ${'a'.$tdnum.'_includeprevconfigs'} = _week_course_text($a[$tdnum], $ary_speakers, ${'a'.$tdnum.'_prevconfigs'});
        $tmp = ${'a'.$tdnum.'_includeprevconfigs'};
        if(isset($tmp['text'])){
          ${'td'.$tdnum} = $tmp['text'];
        }
        #
        ${'td'.$tdnum.'attr'} = array();#default
        if(isset(${'a'.$tdnum.'_includeprevconfigs'}['conf']['MERGE'])){
          $merge = ${'a'.$tdnum.'_includeprevconfigs'}['conf']['MERGE'];
          if($merge>=$MERGE_DEFAULT_VALUE){
            ${'td'.$tdnum.'attr'}[] = 'rowspan='.$merge;
          }
        }
        ${'td'.$tdnum.'attr'} = ${'td'.$tdnum.'attr'} ? ' '.implode(' ', ${'td'.$tdnum.'attr'}) : '';
      }
      #
      ${'td'.$tdnum.'class'} = strpos(${'td'.$tdnum}, 'wkplace')!==false ? ' class=haswkplace' : '';
    }

    $NEED_CHECK_MERGE = $rowidx>=($START_POSSIBLE_HAS_MERGE_ROWIDX+1);
    $s.=''.$td0.'';
    if(!$NEED_CHECK_MERGE){
      $s.=''.$td1.'';
      if(isset($a[2]) && $a[2]){
      $s.=''.$td2.'';
      }
      if(isset($a[3]) && $a[3]){
      $s.=''.$td3.'';
      }
      if(isset($a[4]) && $a[4]){
      $s.=''.$td4.'';
      }
      if(isset($a[5]) && $a[5]){
      $s.=''.$td5.'';
      }
    }else{
      foreach(range(1,5)as$n){
        if(!isset($a[$n]) || !$a[$n]){continue;}
        $bool_output = true;
        for(
          //$text_key=count(${'a'.$n.'_prevconfigs'})-1; # because we start index from 1 INSTEAD OF 0
          $text_key=count(${'a'.$n.'_prevconfigs'});
          $text_key>=$START_POSSIBLE_HAS_MERGE_ROWIDX;$text_key--){
          $text = ${'a'.$n.'_prevconfigs'}[$text_key];
          preg_match('/MERGE/m', $text, $m1);
          preg_match('/MERGE ([0-9]+)/m', $text, $m2);
          $MERGE = 0;
          if($m1 && !$m2){
            $MERGE = $MERGE_DEFAULT_VALUE;
          }elseif($m1 && $m2){
            $MERGE = $m2[1];
          }
          if(($text_key  +  $MERGE-1) >= $rowidx){
            $bool_output=false;
          }
          if($MERGE>=$MERGE_DEFAULT_VALUE){break;}
        }
        if($bool_output){
          $s.=''.${'td'.$n}
            //.($bool_output ? 'TRUE' : 'FALSE')
            // .'--'.${'td'.$n.'attr'}
            // .implode('---', ${'a'.$n.'_prevconfigs'})
            .'';
        }
      } // end range(1,5)as$n
    }
    $s.='';
  }

  $s.='';

  // if(isset($atts['show_place_mapping']) && $atts['show_place_mapping']){
  //   $place_mapping = '
  • R0: 人文館國際會議廳
  • A0: 活動中心一樓演講廳
  • A1: 活動中心二樓第一會議室
  • HQ: 跨領域大樓七樓
'; // }else{ $place_mapping = ''; // } $google_link = $google_link ? ''.$google_link.'' : ''; return '
'.$google_link.$place_mapping.''.$s.'
'; } add_shortcode('week_course', 'week_course_func');