<?php

/* ========================================================================
 *
 * @license This source file is subject to version 3.01 of the PHP license,
 *              that is available at http://www.php.net/license/3_01.txt
 *              If you did not receive a copy of the PHP license and are unable 
 *              to obtain it through the world-wide-web, please send a note to 
 *              license@php.net so we can mail you a copy immediately.  
 *
 * @category   Application of MyNETS
 * @project    OpenPNE UsagiProject 2006-2007
 * @package    MyNETS
 * @author     UsagiProject <info@usagi.mynets.jp>
 * @copyright  2006-2007 UsagiProject <author member ad http://usagi.mynets.jp/member.html>
 * @version    MyNETS,v 1.0.0
 * @since      File available since Release 1.0.0 Nighty
 * @chengelog  [2007/02/17] Ver1.1.0Nighty package
 * ======================================================================== 
 */
 
function db_diary_count_c_diary_commentself4c_diary_id($c_dairy_id,$c_diary_comment_id)
{
    $sql =
    ' SELECT count(*)' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary_comment' .
    ' WHERE c_diary_comment_id < ' . $c_diary_comment_id .
    ' AND c_diary_id =' . $c_dairy_id;
    return db_get_one($sql);
}

function db_commu_count_c_topic_comment4c_topic_id($c_commu_topic_id)
{
    $sql =
    'SELECT count(*)' .
    ' FROM ' . MYNETS_PREFIX_NAME . "c_commu_topic_comment" .
    ' WHERE c_commu_topic_id = ' . $c_commu_topic_id;
    return db_get_one($sql);
}

function db_commu_count_c_topic_commentself4c_topic_id($c_commu_topic_id,$c_commu_topic_comment_id)
{
    $sql =
    'SELECT count(*)' .
    ' FROM ' . MYNETS_PREFIX_NAME . "c_commu_topic_comment" .
    ' WHERE c_commu_topic_comment_id < ' . $c_commu_topic_comment_id .
    ' AND c_commu_topic_id = ' . $c_commu_topic_id;
    return db_get_one($sql);
}

function p_h_gmaps_list_all_search_c_diary4c_diary($keyword, $page_size, $page)
{

    //and検索を実装
    //subject,body を検索
    if ($keyword) {
        //全角空白を半角に統一
        $keyword = str_replace('　', ' ', $keyword);

        $keyword_list = explode(' ', $keyword);
        foreach ($keyword_list as $word) {
            $word = check_search_word($word);
            $where_d .= ' AND (c_diary.subject LIKE "%'.$word.'%" OR c_diary.body LIKE "%'.$word.'%")';
            $where_dc .= ' AND (c_diary.subject LIKE "%'.$word.'%" OR c_diary_comment.body LIKE "%'.$word.'%")';
        }
    }

    $sql_d =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_diary.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_diary.subject, ' . MYNETS_PREFIX_NAME . 'c_diary.body, ' . MYNETS_PREFIX_NAME . 'c_diary.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_diary.image_filename_1, ' . MYNETS_PREFIX_NAME . 'c_diary.c_diary_id, ' . '"top" AS c_diary_comment_id, ' . '"top" AS comment_number' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_diary.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.public_flag = 'public'" .
    $where_d;

    $sql_dc =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_diary.subject, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.body, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.image_filename_1,' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_id, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_comment_id, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.comment_number' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary_comment,' . MYNETS_PREFIX_NAME . 'c_diary' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_diary_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    ' AND ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_id = ' . MYNETS_PREFIX_NAME . 'c_diary.c_diary_id' .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.public_flag = 'public'" .
    $where_dc;
    
    $list_d = db_get_all($sql_d);
    $list_dc = db_get_all($sql_dc);
    $list_all = array_merge_recursive($list_d,$list_dc);
    foreach($list_all as $key => $row) {
        $c_member_id[$key] = $row["c_member_id"];
        $subject[$key] = $row["subject"];
        $body[$key] = $row["body"];
        $r_datetime[$key] = $row["r_datetime"];
        $image_filename_1[$key] = $row["image_filename_1"];
        $c_diary_id[$key] = $row["c_diary_id"];
        $c_diary_comment_id[$key] = $row["c_diary_comment_id"];
        $comment_number[$key] = $number["comment_number"];
    }
    if (count($list_all) !== 0) {
        array_multisort($r_datetime, SORT_DESC, $list_all);
    }
    $list = array_slice($list_all,(intval($page) - 1) * intval($page_size),$page_size);

    $regexp = '<cmd\s+src="gmaps"(?:\s+args="([^,&<>]+(,[\w\-\+%]+)*)?")?\s*>';
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_common_c_member_with_profile($value['c_member_id']);
        preg_match("/".$regexp."/i",$value['body'],$match);
        $pt = explode(',', $match[1]);
        $list[$key]['lat'] =$pt[1].'.'.$pt[2];
        $list[$key]['lon'] =$pt[3].'.'.$pt[4];
        $list[$key]['zoom'] =$pt[0];
        $list[$key]['subject'] = str_replace("'","’",$value['subject']);
        $list[$key]['body'] = preg_replace("/".$regexp."/i","",str_replace("'","’",$value['body']));
        if($value['c_diary_comment_id'] != 'top') {
            $dnum = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
            $dnum_self = db_diary_count_c_diary_commentself4c_diary_id($value['c_diary_id'],$value['c_diary_comment_id']);
            $list[$key]['c_diary_link'] = floor(($dnum-$dnum_self-1)/20)+1 . '#' . $value['comment_number'];
        } else {
            $list[$key]['c_diary_link'] = '1';
        }            
    }
    return array($list,count($list_all));
}

function p_h_gmaps_list_all_search_c_topic4c_topic($keyword, $page_size, $page)
{

    //and検索を実装
    //subject,body を検索
    if ($keyword) {
        //全角空白を半角に統一
        $keyword = str_replace('　', ' ', $keyword);

        $keyword_list = explode(' ', $keyword);
        foreach ($keyword_list as $word) {
            $word = check_search_word($word);
            $where_d .= ' AND (c_commu.name LIKE "%'.$word.'%" OR c_commu_topic.name LIKE "%'.$word.'%" OR c_commu_topic_comment.body LIKE "%'.$word.'%")';
        }
    }

    $sql =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_commu.name AS communame, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.name AS commutopicname, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.body, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.image_filename1, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.c_commu_topic_id, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.c_commu_topic_comment_id, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.number, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.event_flag' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment, ' . MYNETS_PREFIX_NAME . 'c_commu_topic, ' . MYNETS_PREFIX_NAME . 'c_commu' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_id = " . MYNETS_PREFIX_NAME . "c_commu.c_commu_id" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . MYNETS_PREFIX_NAME . "c_commu_topic.c_commu_topic_id" .
    " AND (" . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'public' OR " . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'auth_sns')" .
    $where_d .
    " ORDER BY c_commu_topic_comment.r_datetime DESC";
    
    $list = db_get_all_page($sql, $page, $page_size);
    $regexp = '<cmd\s+src="gmaps"(?:\s+args="([^,&<>]+(,[\w\-\+%]+)*)?")?\s*>';
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_common_c_member_with_profile($value['c_member_id']);
        preg_match("/".$regexp."/i",$value['body'],$match);
        $pt = explode(',', $match[1]);
        $list[$key]['lat'] =$pt[1].'.'.$pt[2];
        $list[$key]['lon'] =$pt[3].'.'.$pt[4];
        $list[$key]['zoom'] =$pt[0];
        $list[$key]['communame'] = str_replace("'","’",$value['communame']);
        $list[$key]['commutopicname'] = str_replace("'","’",$value['commutopicname']);
        $list[$key]['body'] = preg_replace("/".$regexp."/i","",str_replace("'","’",$value['body']));
        $cnum = db_commu_count_c_topic_comment4c_topic_id($value['c_commu_topic_id']);
        $cnum_self = db_commu_count_c_topic_commentself4c_topic_id($value['c_commu_topic_id'],$value['c_commu_topic_comment_id']);
        if($cnum_self != 0) {
            $list[$key]['c_topic_link'] = floor(($cnum-$cnum_self-1)/10)+1 . '#' . $value['number'];
        } else {
            $list[$key]['c_topic_link'] = '1';
        }
    }

    $sql =
    'SELECT COUNT(*)' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment, ' . MYNETS_PREFIX_NAME . 'c_commu_topic, ' . MYNETS_PREFIX_NAME . 'c_commu' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_id = " . MYNETS_PREFIX_NAME . "c_commu.c_commu_id" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . MYNETS_PREFIX_NAME . "c_commu_topic.c_commu_topic_id" .
    " AND (" . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'public' OR " . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'auth_sns')" .
    $where_d;
    $total_num = db_get_one($sql);
    return array($list,$total_num);
}

function p_h_gmaps_list_all_search_c_diary4c_diary_area($area, $page_size, $page)
{

    if ($area) {
        $areas = explode('_', $area);
        $n = $areas[0];
        $s = $areas[1];
        $e = $areas[2];
        $w = $areas[3];
    }

    $sql_d =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_diary.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_diary.subject, ' . MYNETS_PREFIX_NAME . 'c_diary.body, ' . MYNETS_PREFIX_NAME . 'c_diary.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_diary.image_filename_1, ' . MYNETS_PREFIX_NAME . 'c_diary.c_diary_id, ' . '"top" AS c_diary_comment_id, ' . '"top" AS comment_number' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_diary.body LIKE '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.public_flag = 'public'";

    $sql_dc =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_diary.subject, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.body, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.image_filename_1,' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_id, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_comment_id, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.comment_number' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary_comment,' . MYNETS_PREFIX_NAME . 'c_diary' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_diary_comment.body LIKE '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    ' AND ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_id = ' . MYNETS_PREFIX_NAME . 'c_diary.c_diary_id' .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.public_flag = 'public'";
    
    $list_d = db_get_all($sql_d);
    $list_dc = db_get_all($sql_dc);
    $list_all = array_merge_recursive($list_d,$list_dc);
    
    $regexp = '<cmd\s+src="gmaps"(?:\s+args="([^,&<>]+(,[\w\-\+%]+)*)?")?\s*>';
    $list_area = array();
    foreach ($list_all as $key => $value) {
        preg_match("/".$regexp."/i",$value['body'],$match);
        $pt = explode(',', $match[1]);
        $ns =$pt[1].'.'.$pt[2];
        $ew =$pt[3].'.'.$pt[4];
        if($ns > $s && $ns < $n) {
            if($e > $w) {
                if($ew > $w && $ew < $e) {
                    $list_all[$key]['lat'] =$ns;
                    $list_all[$key]['lon'] =$ew;
                    $list_all[$key]['zoom'] =$pt[0];
                    array_push($list_area,$list_all[$key]);
                }
            } else {
                if($ew > $w || $ew < $e) {
                    $list_all[$key]['lat'] =$ns;
                    $list_all[$key]['lon'] =$ew;
                    $list_all[$key]['zoom'] =$pt[0];
                    array_push($list_area,$list_all[$key]);
                }
            }
        }
    }

    foreach($list_area as $key => $row) {
        $c_member_id[$key] = $row["c_member_id"];
        $lat[$key] = $row["lat"];
        $lon[$key] = $row["lon"];
        $zoom[$key] = $row["zoom"];
        $subject[$key] = $row["subject"];
        $body[$key] = $row["body"];
        $r_datetime[$key] = $row["r_datetime"];
        $image_filename_1[$key] = $row["image_filename_1"];
        $c_diary_id[$key] = $row["c_diary_id"];
        $c_diary_comment_id[$key] = $row["c_diary_comment_id"];
        $comment_number[$key] = $row["comment_number"];
    }
    if (count($list_area) !== 0) {
        array_multisort($r_datetime, SORT_DESC, $list_area);
    }
    $list = array_slice($list_area,(intval($page) - 1) * intval($page_size),$page_size);
    
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_common_c_member_with_profile($value['c_member_id']);
        $list[$key]['subject'] = str_replace("'","’",$value['subject']);
        $list[$key]['body'] = preg_replace("/".$regexp."/i","",str_replace("'","’",$value['body']));
        if($value['c_diary_comment_id'] != 'top') {
            $dnum = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
            $dnum_self = db_diary_count_c_diary_commentself4c_diary_id($value['c_diary_id'],$value['c_diary_comment_id']);
            $list[$key]['c_diary_link'] = floor(($dnum-$dnum_self-1)/20)+1 . '#' . $value['comment_number'];
        } else {
            $list[$key]['c_diary_link'] = '1';
        }            
    }
    return array($list,count($list_area));
}

function p_h_gmaps_list_all_search_c_topic4c_topic_area($area, $page_size, $page)
{

    if ($area) {
        $areas = explode('_', $area);
        $n = $areas[0];
        $s = $areas[1];
        $e = $areas[2];
        $w = $areas[3];
    }

    $sql =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_commu.name AS communame, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.name AS commutopicname, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.body, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.image_filename1, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.c_commu_topic_id, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.c_commu_topic_comment_id, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.number, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.event_flag' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment, ' . MYNETS_PREFIX_NAME . 'c_commu_topic, ' . MYNETS_PREFIX_NAME . 'c_commu' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_id = " . MYNETS_PREFIX_NAME . "c_commu.c_commu_id" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . MYNETS_PREFIX_NAME . "c_commu_topic.c_commu_topic_id" .
    " AND (" . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'public' OR " . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'auth_sns')" .
    " ORDER BY c_commu_topic_comment.r_datetime DESC";

    $list_c = db_get_all($sql);
    
    $regexp = '<cmd\s+src="gmaps"(?:\s+args="([^,&<>]+(,[\w\-\+%]+)*)?")?\s*>';
    $list_area = array();
    foreach ($list_c as $key => $value) {
        preg_match("/".$regexp."/i",$value['body'],$match);
        $pt = explode(',', $match[1]);
        $ns =$pt[1].'.'.$pt[2];
        $ew =$pt[3].'.'.$pt[4];
        if($ns > $s && $ns < $n) {
            if($e > $w) {
                if($ew > $w && $ew < $e) {
                    $list_c[$key]['lat'] =$ns;
                    $list_c[$key]['lon'] =$ew;
                    $list_c[$key]['zoom'] =$pt[0];
                    array_push($list_area,$list_c[$key]);
                }
            } else {
                if($ew > $w || $ew < $e) {
                    $list_c[$key]['lat'] =$ns;
                    $list_c[$key]['lon'] =$ew;
                    $list_c[$key]['zoom'] =$pt[0];
                    array_push($list_area,$list_c[$key]);
                }
            }
        }
    }

    $list = array_slice($list_area,(intval($page) - 1) * intval($page_size),$page_size);
    
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_common_c_member_with_profile($value['c_member_id']);
        $list[$key]['communame'] = str_replace("'","’",$value['communame']);
        $list[$key]['commutopicname'] = str_replace("'","’",$value['commutopicname']);
        $list[$key]['body'] = preg_replace("/".$regexp."/i","",str_replace("'","’",$value['body']));
        $cnum = db_commu_count_c_topic_comment4c_topic_id($value['c_commu_topic_id']);
        $cnum_self = db_commu_count_c_topic_commentself4c_topic_id($value['c_commu_topic_id'],$value['c_commu_topic_comment_id']);
        if($cnum_self != 0) {
            $list[$key]['c_topic_link'] = floor(($cnum-$cnum_self-1)/10)+1 . '#' . $value['number'];
        } else {
            $list[$key]['c_topic_link'] = '1';
        }
    }

    return array($list,count($list_area));
}

function p_h_gmaps_list_all_search_c_diary4c_diary_id($id, $page_size, $page)
{

    $sql_d =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_diary.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_diary.subject, ' . MYNETS_PREFIX_NAME . 'c_diary.body, ' . MYNETS_PREFIX_NAME . 'c_diary.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_diary.image_filename_1, ' . MYNETS_PREFIX_NAME . 'c_diary.c_diary_id, ' . '"top" AS c_diary_comment_id, ' . '"top" AS comment_number' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_diary.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.public_flag = 'public'" .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.c_diary_id = " . $id;

    $sql_dc =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_diary.subject, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.body, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.image_filename_1,' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_id, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_comment_id, ' . MYNETS_PREFIX_NAME . 'c_diary_comment.comment_number' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_diary_comment,' . MYNETS_PREFIX_NAME . 'c_diary' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_diary_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    ' AND ' . MYNETS_PREFIX_NAME . 'c_diary_comment.c_diary_id = ' . MYNETS_PREFIX_NAME . 'c_diary.c_diary_id' .
    " AND " . MYNETS_PREFIX_NAME . "c_diary.public_flag = 'public'" .
    " AND " . MYNETS_PREFIX_NAME . "c_diary_comment.c_diary_id = " . $id;
    
    $list_d = db_get_all($sql_d);
    $list_dc = db_get_all($sql_dc);
    $list_all = array_merge_recursive($list_d,$list_dc);
    
    foreach($list_all as $key => $row) {
        $c_member_id[$key] = $row["c_member_id"];
        $subject[$key] = $row["subject"];
        $body[$key] = $row["body"];
        $r_datetime[$key] = $row["r_datetime"];
        $image_filename_1[$key] = $row["image_filename_1"];
        $c_diary_id[$key] = $row["c_diary_id"];
        $c_diary_comment_id[$key] = $row["c_diary_comment_id"];
        $comment_number[$key] = $row["comment_number"];
    }
    if (count($list_all) !== 0) {
        array_multisort($r_datetime, SORT_DESC, $list_all);
    }
    $list = array_slice($list_all,(intval($page) - 1) * intval($page_size),$page_size);

    $regexp = '<cmd\s+src="gmaps"(?:\s+args="([^,&<>]+(,[\w\-\+%]+)*)?")?\s*>';
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_common_c_member_with_profile($value['c_member_id']);
        preg_match("/".$regexp."/i",$value['body'],$match);
        $pt = explode(',', $match[1]);
        $list[$key]['lat'] =$pt[1].'.'.$pt[2];
        $list[$key]['lon'] =$pt[3].'.'.$pt[4];
        $list[$key]['zoom'] =$pt[0];
        $list[$key]['subject'] = str_replace("'","’",$value['subject']);
        $list[$key]['body'] = preg_replace("/".$regexp."/i","",str_replace("'","’",$value['body']));
        if($value['c_diary_comment_id'] != 'top') {
            $dnum = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
            $dnum_self = db_diary_count_c_diary_commentself4c_diary_id($value['c_diary_id'],$value['c_diary_comment_id']);
            $list[$key]['c_diary_link'] = floor(($dnum-$dnum_self-1)/20)+1 . '#' . $value['comment_number'];
        } else {
            $list[$key]['c_diary_link'] = '1';
        }            
    }
    return array($list,count($list_all));
}

function p_h_gmaps_list_all_search_c_topic4c_topic_id($id, $page_size, $page)
{

    $sql =
    ' SELECT ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.c_member_id, ' . MYNETS_PREFIX_NAME . 'c_commu.name AS communame, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.name AS commutopicname, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.body, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.image_filename1, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.r_datetime, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.c_commu_topic_id, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.c_commu_topic_comment_id, ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment.number, ' . MYNETS_PREFIX_NAME . 'c_commu_topic.event_flag' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment, ' . MYNETS_PREFIX_NAME . 'c_commu_topic, ' . MYNETS_PREFIX_NAME . 'c_commu' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_id = " . MYNETS_PREFIX_NAME . "c_commu.c_commu_id" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . MYNETS_PREFIX_NAME . "c_commu_topic.c_commu_topic_id" .
    " AND (" . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'public' OR " . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'auth_sns')" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . $id .
    " ORDER BY c_commu_topic_comment.r_datetime DESC";
    
    $list = db_get_all_page($sql, $page, $page_size);
    $regexp = '<cmd\s+src="gmaps"(?:\s+args="([^,&<>]+(,[\w\-\+%]+)*)?")?\s*>';
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_common_c_member_with_profile($value['c_member_id']);
        preg_match("/".$regexp."/i",$value['body'],$match);
        $pt = explode(',', $match[1]);
        $list[$key]['lat'] =$pt[1].'.'.$pt[2];
        $list[$key]['lon'] =$pt[3].'.'.$pt[4];
        $list[$key]['zoom'] =$pt[0];
        $list[$key]['communame'] = str_replace("'","’",$value['communame']);
        $list[$key]['commutopicname'] = str_replace("'","’",$value['commutopicname']);
        $list[$key]['body'] = preg_replace("/".$regexp."/i","",str_replace("'","’",$value['body']));
        $cnum = db_commu_count_c_topic_comment4c_topic_id($value['c_commu_topic_id']);
        $cnum_self = db_commu_count_c_topic_commentself4c_topic_id($value['c_commu_topic_id'],$value['c_commu_topic_comment_id']);
        if($cnum_self != 0) {
            $list[$key]['c_topic_link'] = floor(($cnum-$cnum_self-1)/10)+1 . '#' . $value['number'];
        } else {
            $list[$key]['c_topic_link'] = '1';
        }
    }

    $sql =
    'SELECT COUNT(*)' .
    ' FROM ' . MYNETS_PREFIX_NAME . 'c_commu_topic_comment, ' . MYNETS_PREFIX_NAME . 'c_commu_topic, ' . MYNETS_PREFIX_NAME . 'c_commu' .
    " WHERE " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.body like '%<cmd src=\"gmaps\" args=\"%,%,%,%,%\">%'" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_id = " . MYNETS_PREFIX_NAME . "c_commu.c_commu_id" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . MYNETS_PREFIX_NAME . "c_commu_topic.c_commu_topic_id" .
    " AND (" . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'public' OR " . MYNETS_PREFIX_NAME . "c_commu.public_flag = 'auth_sns')" .
    " AND " . MYNETS_PREFIX_NAME . "c_commu_topic_comment.c_commu_topic_id = " . $id;
    $total_num = db_get_one($sql);
    return array($list,$total_num);
}
?>