OpenPNE2 /webapp/lib/db/diary.php

Language PHP Lines 1338
MD5 Hash 99c87f6e5b5df597b31bbd0f03ad09bf
Repository https://github.com/openpne/OpenPNE2.git View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
<?php
/**
 * @copyright 2005-2008 OpenPNE Project
 * @license   http://www.php.net/license/3_01.txt PHP License 3.01
 */

/**
 * 指定メンバーの日記カテゴリリストを取得する
 *
 * @param int $c_member_id
 * @return array
 */
function db_diary_category_list4c_member_id($c_member_id)
{
    $sql = 'SELECT c_diary_category_id, category_name FROM c_diary_category '
         . 'WHERE c_member_id = ? ORDER BY c_diary_category_id';
    $result = db_get_all($sql, array(intval($c_member_id)), 'main');

    return $result;
}

/**
 * カテゴリIDからカテゴリ名を得る
 */
function db_diary_get_category_name4category_id($category_id)
{
    $sql = 'SELECT category_name FROM c_diary_category ' .
        'WHERE c_diary_category_id = ?';
    return db_get_one($sql, array($category_id));
}

/**
 * 日記IDからカテゴリリストを得る
 *
 * @param int $c_diary_id
 * @return array
 */
function db_diary_category_list4c_diary_id($c_diary_id)
{
    $sql = 'SELECT c_diary_category_id FROM c_diary_category_diary WHERE c_diary_id = ?';
    $category_list = db_get_col($sql, array(intval($c_diary_id)));
    if (!$category_list) {
        return array();
    }
    $ids = implode(',', $category_list);

    $sql = 'SELECT c_diary_category_id, category_name FROM c_diary_category'
         . ' WHERE c_diary_category_id IN ('.$ids.') ORDER BY c_diary_category_id';
    return db_get_all($sql);
}

/**
 * カテゴリIDから日記を得る
 *
 * @param int $c_member_id
 * @param int $c_diary_category_id
 * @param int $u
 * @param int $page_size
 * @param int $page
 * @return array
 */
function db_diary_list4c_diary_category_id($c_member_id, $c_diary_category_id, $u = null, $page_size = 20, $page = 0)
{
    $sql = 'SELECT c_diary_id FROM c_diary_category_diary WHERE c_diary_category_id = ?';
    $diary_list = db_get_col($sql, array(intval($c_diary_category_id)));
    if (!$diary_list) {
        return array(array(), false, false, 0);
    }
    $ids = implode(',', $diary_list);

    $pf_cond = db_diary_public_flag_condition($c_member_id, $u);
    $where = ' WHERE c_diary_id IN ('.$ids.') AND c_member_id = ? ' . $pf_cond . ' ORDER BY r_datetime DESC';
    $sql = 'SELECT * FROM c_diary' . $where;
    $params = array($c_member_id);
    $list = db_get_all_limit($sql, $page_size * ($page - 1), $page_size, $params);

    foreach ($list as $key => $c_diary) {
        $list[$key]['num_comment'] = db_diary_count_c_diary_comment4c_diary_id($c_diary['c_diary_id']);
    }

    $sql = 'SELECT COUNT(*) FROM c_diary' . $where;
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }
        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }

    return array($list, $prev, $next, $total_num);
}

/**
 * カテゴリを追加する
 *
 * @param int $c_member_id
 * @param int $category_name
 */
function db_diary_category_insert_category($c_member_id, $category_name)
{
    $data = array(
        'c_member_id' => intval($c_member_id),
        'category_name' => $category_name,
        'r_datetime' => db_now(),
    );
    return db_insert('c_diary_category', $data);
}

/**
 * カテゴリを削除する
 *
 * @param int $c_diary_category_id
 */
function db_diary_category_delete_category($c_diary_category_id)
{
    $sql = 'DELETE FROM c_diary_category WHERE c_diary_category_id = ?';
    db_query($sql, array($c_diary_category_id));
}
/**
 * カテゴリと日記を関連づける
 *
 * @param int $c_diary_id
 * @param int $c_diary_category_id
 */
function db_diary_category_insert_c_diary_category_diary($c_diary_id, $c_diary_category_id)
{
    $data = array(
        'c_diary_id' => intval($c_diary_id),
        'c_diary_category_id' => intval($c_diary_category_id),
    );
    db_insert('c_diary_category_diary', $data);
}

/**
 * カテゴリと日記の関連づけを削除する
 */
function db_diary_category_delete_c_diary_category_diary($c_diary_id)
{
    $diary_category_list = db_diary_category_list4c_diary_id($c_diary_id);

    $sql = 'DELETE FROM c_diary_category_diary WHERE c_diary_id = ?';
    db_query($sql, array($c_diary_id));

    foreach($diary_category_list as $value) {
        $sql = 'SELECT COUNT(*) FROM c_diary_category_diary'.
            ' WHERE c_diary_category_id = ?';
        $c_diary_category_id = $value['c_diary_category_id'];
        $is_diary = (bool)db_get_one($sql, array(intval($c_diary_category_id)), 'main');
        if (!$is_diary) {  //カテゴリに関連付いた日記が存在しない
            db_diary_category_delete_category($c_diary_category_id);
        }
    }
}

/**
 * db_diary_public_flag_condition
 *
 * @param int $c_member_id target c_member_id
 * @param int $u viewer's c_member_id
 * @param string $force
 * @return string
 */
function db_diary_public_flag_condition($c_member_id, $u = null, $force = null)
{
    $pf_cond = '';
    if ($force) {
        switch ($force) {
        case 'friend':
            $pf_cond = " AND public_flag <> 'private'";
            break;
        case 'private':
            $pf_cond = " AND public_flag = 'public'";
            break;
        }
    } else {
        if (!is_null($u) && $c_member_id != $u) {
            $is_friend = db_friend_is_friend($c_member_id, $u);
            if ($is_friend) {
                $pf_cond = " AND public_flag <> 'private'";
            } else {
                $pf_cond = " AND public_flag = 'public'";
            }
        }
    }
    return $pf_cond;
}

//// c_diary

/**
 * 日記IDから日記を取得
 *
 * @param   int   $c_diary_id
 * @return  array
 */
function db_diary_get_c_diary4id($c_diary_id)
{
    $sql = 'SELECT * FROM c_diary WHERE c_diary_id = ?';
    $c_diary_list = db_get_row($sql, array(intval($c_diary_id)));
    return $c_diary_list;
}

/**
 * メンバーIDから日記リストを最新順で取得
 *
 * @param int $c_member_id
 * @return array 日記リスト
 */
function db_diary_get_c_diary_list4c_member_id($target_c_member_id, $count = 10, $u = null, $force = null)
{
    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ

    if (!$is_recurred) {  //function cacheのために再帰処理を行う
        $is_recurred = true;
        $funcargs = func_get_args();
        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_FAST, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $pf_cond = db_diary_public_flag_condition($target_c_member_id, $u, $force);
    $sql = 'SELECT * FROM c_diary WHERE c_member_id = ?' . $pf_cond .
           ' ORDER BY r_datetime DESC';
    $params = array(intval($target_c_member_id));
    $arr = db_get_all_limit($sql, 0, $count, $params);
    foreach ($arr as $key => $value) {
        $arr[$key]['comment_count'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }
    return $arr;
}

function p_common_is_active_c_diary_id($c_diary_id)
{
    $sql = 'SELECT c_diary_id FROM c_diary WHERE c_diary_id = ?';
    return (bool)db_get_one($sql, array(intval($c_diary_id)));
}

/**
 * 指定した日記の前の日記IDを取得する
 *
 * @param int $c_member_id
 * @param int $c_diary_id
 */
function db_diary_c_diary_id_prev4c_diary_id($c_member_id, $c_diary_id, $u = null)
{
    $public_flag_condition = db_diary_public_flag_condition($c_member_id, $u);
    $sql = 'SELECT c_diary_id FROM c_diary WHERE c_member_id = ? AND c_diary_id < ?'
         . $public_flag_condition . ' ORDER BY c_diary_id DESC';
    return db_get_one($sql, array(intval($c_member_id), intval($c_diary_id)));
}

/**
 * 指定した日記の次の日記IDを取得する
 *
 * @param int $c_member_id
 * @param int $c_diary_id
 */
function db_diary_c_diary_id_next4c_diary_id($c_member_id, $c_diary_id, $u = null)
{
    $public_flag_condition = db_diary_public_flag_condition($c_member_id, $u);
    $sql = 'SELECT c_diary_id FROM c_diary WHERE c_member_id = ? AND c_diary_id > ?'
         . $public_flag_condition . ' ORDER BY c_diary_id ASC';
    return db_get_one($sql, array(intval($c_member_id), intval($c_diary_id)));
}

//// c_diary_comment

/**
 * 日記のコメントリストを得る
 *
 * @param   int $c_diary_id
 * @param   int $limit
 * @return  array 日記コメント+ニックネーム
 */
function db_diary_get_c_diary_comment_list4c_diary_id($c_diary_id)
{
    $sql = 'SELECT c_member.nickname, c_diary_comment.*' .
        ' FROM c_diary_comment LEFT JOIN c_member USING (c_member_id)' .
        ' WHERE c_diary_id = ?' .
        ' ORDER BY c_diary_comment.r_datetime';
    return db_get_all($sql, array(intval($c_diary_id)));
}

/**
 * 日記のコメントリスト(好きなID(複数))を取得
 *
 * @param   array $id_list
 * @param   int   $page
 * @param   int   $page_size
 * @param   bool  $desc  並び順を日時の新しい順にするかどうか
 * @return  日記コメント(+ニックネーム)リスト
 */
function db_diary_get_c_diary_comment_list4id_list($id_list, $page = 1, $page_size= -1, $desc = false)
{
    if (!count($id_list)) return array();
    if ($page_size < 0) {
        $page_size = count($id_list);
    }
    $e_id_list = implode(',', array_map('intval', (array)$id_list));

    $sql = 'SELECT * FROM c_diary_comment' .
            ' WHERE c_diary_comment_id IN ('.$e_id_list.')';
    if ($desc) {
        $sql .= ' ORDER BY r_datetime DESC';
    } else {
        $sql .= ' ORDER BY r_datetime';
    }
    $c_diary_comment_list = db_get_all_page($sql, $page, $page_size);
    foreach ($c_diary_comment_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $c_diary_comment_list[$key]['nickname'] = $c_member['nickname'];
    }

    $sql = 'SELECT COUNT(*) FROM c_diary_comment' .
        ' WHERE c_diary_comment_id IN ('.$e_id_list.')';
    $total_num = db_get_one($sql);

    if ($total_num != 0) {
        $total_page_num = ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }

        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }

    return array($c_diary_comment_list, $prev , $next, $total_num);
}

/**
 * 日記のコメント数を取得
 *
 * @param int $c_diary_id
 * @return int コメント数
 */
function db_diary_count_c_diary_comment4c_diary_id($c_diary_id)
{
    $sql = 'SELECT COUNT(*) FROM c_diary_comment WHERE c_diary_id = ?';
    return db_get_one($sql, array(intval($c_diary_id)));
}

////

/**
 * あるメンバーの日記リストを取得
 *
 * @param int $c_member_id target c_member_id
 * @param int $page_size
 * @param int $page
 * @param int $u viewer's c_member_id
 * @return array 日記リスト
 */
function p_fh_diary_list_diary_list4c_member_id($c_member_id, $page_size, $page, $u = null)
{
    $pf_cond = db_diary_public_flag_condition($c_member_id, $u);
    $sql = 'SELECT * FROM c_diary WHERE c_member_id = ?' . $pf_cond .
           ' ORDER BY r_datetime DESC';
    $params = array(intval($c_member_id));
    $list = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($list as $key => $c_diary) {
        $list[$key]['num_comment'] = db_diary_count_c_diary_comment4c_diary_id($c_diary['c_diary_id']);
    }

    $sql = 'SELECT COUNT(*) FROM c_diary WHERE c_member_id = ?' . $pf_cond;
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }

        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }
    return array($list, $prev, $next, $total_num);
}

/**
 * フレンドの最新日記リスト
 */
function p_h_diary_list_friend_h_diary_list_friend4c_member_id($c_member_id, $page_size, $page)
{
    $friends = db_friend_c_member_id_list($c_member_id, true);
    if (!$friends) {
        return array(array(), false, false, 0);
    }
    $ids = implode(',', array_map('intval', $friends));

    $hint = db_mysql_hint('USE INDEX (r_datetime_c_member_id, r_datetime)');
    $sql = 'SELECT * FROM c_diary' . $hint .
            ' WHERE c_member_id IN (' . $ids . ')' .
            ' AND public_flag <> \'private\'' .
            ' ORDER BY c_diary.r_datetime DESC';

    $lst = db_get_all_page($sql, $page, $page_size);

    foreach ($lst as $key=>$value) {
        $lst[$key]['count_comments'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
        $lst[$key]['c_member'] = db_member_c_member4c_member_id($value['c_member_id']);
    }

    $sql = 'SELECT count(*) FROM c_diary' .
           ' WHERE c_member_id IN (' . $ids . ')' .
           ' AND public_flag <> \'private\'';
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }

        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }

    return array($lst, $prev, $next, $total_num);
}

/**
 * 未読コメントのある日記の数を数える
 *
 * @param int $c_member_id
 * @return int 未読日記数
 */
function p_h_diary_count_c_diary_not_is_read4c_member_id($c_member_id)
{
    $sql = 'SELECT COUNT(*) FROM c_diary WHERE c_member_id = ? AND is_checked = 0';
    $params = array(intval($c_member_id));
    return db_get_one($sql, $params);
}

/**
 * 未読コメントのある日記のうちもっとも古い日記IDを返す
 *
 * @param int $c_member_id
 * @return int c_diary_id
 */
function p_h_diary_c_diary_first_diary_read4c_member_id($c_member_id)
{
    $sql = 'SELECT c_diary_id FROM c_diary WHERE c_member_id = ? AND is_checked = 0 ORDER BY r_datetime';
    $params = array(intval($c_member_id));
    return db_get_one($sql, $params);
}

/**
 * フレンド最新日記リスト取得
 * 日記公開範囲を考慮
 *
 * @param   int $c_member_id
 * @param   int $limit
 * @param   int $use_is_display_friend_home
 * @return  array_of_array  (c_diary.*, nickname)
 */
function p_h_home_c_diary_friend_list4c_member_id($c_member_id, $limit, $use_is_display_friend_home = false)
{
    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ

    if (!$is_recurred) {  //function cacheのために再帰処理を行う
        $is_recurred = true;
        $funcargs = func_get_args();
        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_FAST, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $friends = db_friend_c_member_id_list($c_member_id, true);
    if (!$friends) {
        return array();
    }
    // 対象の最新書き込みのみを表示する場合
    if ($use_is_display_friend_home) {
        // 表示対象一覧を取得し、取得済みフレンド一覧とぶつける
        $friends = array_intersect($friends, db_friend_is_display_friend_home_list($c_member_id, $is_display_friend_home=1));
    }
    $ids = implode(',', array_map('intval', $friends));

    $hint = db_mysql_hint('USE INDEX (r_datetime_c_member_id, r_datetime)');
    $sql = 'SELECT * FROM c_diary' . $hint .
            ' WHERE c_member_id IN (' . $ids . ')' .
            ' AND public_flag <> \'private\'' .
            ' ORDER BY c_diary.r_datetime DESC';
    $c_diary_friend_list = db_get_all_limit($sql, 0, $limit);

    foreach ($c_diary_friend_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $c_diary_friend_list[$key]['nickname'] = $c_member['nickname'];
        $c_diary_friend_list[$key]['count_comments'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }
    return $c_diary_friend_list;
}

/**
 * 日記コメント記入履歴取得
 *
 * @param   int $c_member_id
 * @param   int $limit
 * @return  array_of_array  (c_diary.*, nickname)
 */
function p_h_home_c_diary_my_comment_list4c_member_id($c_member_id, $limit)
{
    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ

    if (!$is_recurred) {  //function cacheのために再帰処理を行う
        $is_recurred = true;
        $funcargs = func_get_args();
        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_FAST, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $sql = 'SELECT cdcl.c_diary_id, cdcl.r_datetime AS maxdate, cd.*'
         . ' FROM c_diary_comment_log AS cdcl INNER JOIN c_diary AS cd USING (c_diary_id) '
         . ' WHERE cdcl.c_member_id = ?'
         . ' AND (cd.public_flag = \'public\'';

    $friends = db_friend_c_member_id_list($c_member_id, true);
    $friend_ids = implode(',', array_map('intval', $friends));
    if ($friend_ids) {
         $sql .= ' OR (cd.public_flag = \'friend\' AND cd.c_member_id IN (' . $friend_ids . '))';
    }

    $sql .= ') ORDER BY maxdate DESC';
    $params = array(intval($c_member_id));
    $list = db_get_all_limit($sql, 0, $limit, $params);

    foreach ($list as $key => $value) {
        $list[$key] += db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $list[$key]['r_datetime'] = $value['maxdate'];
        $list[$key]['num_comment'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }

    return $list;
}

function p_h_diary_comment_list_c_diary_my_comment_list4c_member_id($c_member_id, $page, $page_size)
{
    $select = 'SELECT cdcl.c_diary_id, cdcl.r_datetime AS maxdate, cd.*';
    $from = ' FROM c_diary_comment_log AS cdcl INNER JOIN c_diary AS cd USING (c_diary_id) ';
    $where = ' WHERE cdcl.c_member_id = ? AND (cd.public_flag = \'public\'';


    $friends = db_friend_c_member_id_list($c_member_id, true);
    $friend_ids = implode(',', array_map('intval', $friends));
    if ($friend_ids) {
         $where .= ' OR (cd.public_flag = \'friend\' AND cd.c_member_id IN (' . $friend_ids . '))';
    }
    $where .= ')';

    $sql = $select . $from . $where . ' ORDER BY maxdate DESC';
    $params = array(intval($c_member_id));
    $list = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($list as $key => $value) {
        $list[$key] += db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $list[$key]['r_datetime'] = $value['maxdate'];
        $list[$key]['num_comment'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }

    $sql = 'SELECT COUNT(cdcl.c_diary_id)' . $from . $where;
    $params = array(intval($c_member_id));
    $total_num = db_get_one($sql, $params);

    $is_prev = false;
    $is_next = false;
    if ($total_num) {
        $is_prev = (bool)($page > 1);
        $is_next = (bool)($page < ceil($total_num / $page_size));
    }
    return array($list, $is_prev, $is_next, $total_num);
}

/**
 * あるメンバーの指定された年月日の日記のリストを得る
 */
function p_fh_diary_list_diary_list_date4c_member_id($c_member_id, $page_size, $page, $year, $month, $day=0 ,$u = null)
{
    if ($day) {
        $s_date = date('Y-m-d H:i:s', mktime(0, 0, 0, $month, $day, $year));
        $e_date = date('Y-m-d H:i:s', mktime(0, 0, 0, $month, $day+1, $year));
    } else {
        $s_date = date('Y-m-d H:i:s', mktime(0, 0, 0, $month, 1, $year));
        $e_date = date('Y-m-d H:i:s', mktime(0, 0, 0, $month+1, 1, $year));
    }

    $pf_cond = db_diary_public_flag_condition($c_member_id, $u);
    $where = ' WHERE c_member_id = ? AND r_datetime >= ? AND r_datetime < ?';
    $sql = 'SELECT * FROM c_diary' .
            $where . $pf_cond .
            ' ORDER BY r_datetime DESC';
    $params = array(intval($c_member_id), $s_date, $e_date);
    $list = db_get_all_limit($sql, $page_size * ($page - 1), $page_size, $params);

    foreach ($list as $key => $c_diary) {
        $list[$key]['num_comment'] = db_diary_count_c_diary_comment4c_diary_id($c_diary['c_diary_id']);
    }

    $sql = 'SELECT COUNT(*) FROM c_diary' . $where;
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }
        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }
    return array($list , $prev , $next, $total_num);
}

/**
 * 日記ページの「各月の日記」用
 *
 * 日記を最初に書いた月からスタートしてみる
 */
function p_fh_diary_list_date_list4c_member_id($c_member_id)
{
    $sql = "SELECT r_datetime FROM c_diary" .
        " WHERE c_member_id = ?" .
        " ORDER BY r_datetime";
    $params = array(intval($c_member_id));
    if (!$first_datetime = db_get_one($sql, $params)) {
        return array();
    }

    $start_date = getdate(strtotime($first_datetime));
    $end_date =  getdate();

    $date = array();
    $year = $start_date['year'];
    $month = $start_date['mon'];
    while (1) {
        $date[] =  array(
            'year' => $year,
            'month' => $month,
        );

        if ($end_date['year'] <= $year
            && $end_date['mon'] <= $month) {
            break;
        }

        $month++;
        if ($month > 12) {
            $month = 1;
            $year++;
        }
    }
    return array_reverse($date);
}

//c_member_id から自分の日記についてるコメントID(複数)を取得
function p_fh_diary_c_diary_comment_id_list4c_member_id($c_member_id)
{
    $sql = "SELECT cdc.c_diary_comment_id FROM c_diary as cd,c_diary_comment as cdc" .
        " WHERE cd.c_member_id = ?".
        " AND cd.c_diary_id = cdc.c_diary_id";
    $params = array(intval($c_member_id));
    return db_get_col($sql, $params);
}

/**
 * 年月 から日記のある日(複数)を取得
 */
function p_fh_diary_list_calendar_list4c_member_id($year, $month, $c_member_id)
{
    $sql = "SELECT cdc.c_diary_comment_id FROM c_diary as cd,c_diary_comment as cdc" .
        " WHERE cd.c_member_id = ?".
        " AND cd.c_diary_id = cdc.c_diary_id";
    $params = array(intval($c_member_id));
    return db_get_col($sql, $params);
}

/**
 * 新着日記検索
 * 検索ポイントはタイトル、本文
 * 空白(全角半角問わない)でand検索可
 */
function p_h_diary_list_all_search_c_diary4c_diary($keyword, $page_size, $page, $c_member_id = '')
{
    $params = array();

    $select = 'SELECT *';
    $from = ' FROM c_diary';

    //自分の日記だけを対象にする事もできる
    if ($c_member_id) {
        $where = ' WHERE c_member_id = ?';
        $params[] = intval($c_member_id);
    } else {
        $where = " WHERE public_flag = 'public'";
    }

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

        $keyword_list = explode(' ', $keyword);
        foreach ($keyword_list as $word) {
            $word = check_search_word($word);

            $where .= ' AND (subject LIKE ? OR body LIKE ?)';
            $params[] = '%'.$word.'%';
            $params[] = '%'.$word.'%';
        }
    }
    $order = " ORDER BY r_datetime DESC";

    $sql = $select . $from . $where . $order;

    $list = db_get_all_page($sql, $page, $page_size, $params);
    foreach ($list as $key => $value) {
        $list[$key]['c_member'] = db_member_c_member_with_profile($value['c_member_id']);
        $list[$key]['num_comment'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }

    $sql = 'SELECT COUNT(*)' . $from . $where;
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }
        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }
    return array($list , $prev , $next, $total_num);
}

/**
 * 指定された年月に日記を書いている日のリストを返す
 */
function p_h_diary_is_diary_written_list4date($year, $month, $c_member_id, $u = null)
{
    include_once 'Date/Calc.php';

    $pf_cond = db_diary_public_flag_condition($c_member_id, $u);

    if ($GLOBALS['_OPENPNE_DSN_LIST']['main']['dsn']['phptype'] == 'pgsql') {
        $sql = "SELECT DISTINCT date_part('day', r_datetime) FROM c_diary" .
               " WHERE c_member_id = ? AND r_datetime >= ? AND r_datetime < ?" . $pf_cond;
    } else {
        $sql = 'SELECT DISTINCT DAYOFMONTH(r_datetime) FROM c_diary' .
               ' WHERE c_member_id = ? AND r_datetime >= ? AND r_datetime < ?' . $pf_cond;
    }

    $date_format = '%Y-%m-%d 00:00:00';
    $thismonth = Date_Calc::beginOfMonth($month, $year, $date_format);
    $nextmonth = Date_Calc::beginOfNextMonth(0, $month, $year, $date_format);

    $params = array(intval($c_member_id), $thismonth, $nextmonth);

    return db_get_col($sql, $params);
}

/**
 * 日記コメント情報をIDから取得
 *
 * @param   int $c_diary_comment_id
 * @return array
 *              c_diary_comemnt.*
 *              c_diary.c_member_id AS c_member_id_author
 */
function _do_c_diary_comment4c_diary_comment_id($c_diary_comment_id)
{
    $sql = "SELECT dc.*, d.c_member_id AS c_member_id_author";
    $sql .= " FROM c_diary_comment AS dc, c_diary AS d";
    $sql .= " WHERE dc.c_diary_comment_id = ?";
    $sql .= " AND dc.c_diary_id = d.c_diary_id";
    $params = array(intval($c_diary_comment_id));
    return db_get_row($sql, $params);
}

/**
 * メンバーの最新日記のリストを返す
 */
function k_p_f_home_c_diary_list4c_member_id($c_member_id, $limit)
{
    //日記リスト
    $sql = "SELECT c_diary_id, r_date, subject ,image_filename_1";
    $sql .= " FROM c_diary";
    $sql .= " WHERE c_member_id = ?";
    $sql .= " ORDER BY r_date DESC";
    $params = array(intval($c_member_id));
    $c_diary_list = db_get_all_limit($sql, 0, $limit, $params);

    //コメント数
    foreach ($c_diary_list as $key => $value) {
        $c_diary_id = $value['c_diary_id'];

        $sql = "SELECT COUNT(*) ";
        $sql .= "FROM c_diary_comment ";
        $sql .= "WHERE c_diary_id = ?";
        $params = array(intval($c_diary_id));
        $c_diary_list[$key]['count_comment'] = db_get_one($sql, $params);
    }
    return $c_diary_list;
}

/**
 * 最新日記リストを取得。
 * 取得範囲を指定できる。
 */
function k_p_fh_diary_list_c_diary_list4c_member_id($c_member_id, $page_size, $page)
{
    $sql = 'SELECT * FROM c_diary WHERE c_member_id = ? ORDER BY r_datetime DESC';
    $params = array(intval($c_member_id));
    $c_diary_list = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($c_diary_list as $key => $value) {
        $c_diary_list[$key]['count_comment'] =
            db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }

    $sql = 'SELECT COUNT(*) FROM c_diary WHERE c_member_id = ?';
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }
        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }
    return array($c_diary_list , $prev , $next);
}

/**
 * 日記へのコメントリストを取得
 */
function k_p_fh_diary_c_diary_comment_list4c_diary_id($c_diary_id, $page_size, $page, $desc = true)
{
    $sql = 'SELECT * FROM c_diary_comment WHERE c_diary_id = ? ORDER BY r_datetime';
    if ($desc) {
        $sql .= ' DESC';
    }
    $params = array(intval($c_diary_id));
    $c_diary_comment_list = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($c_diary_comment_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $c_diary_comment_list[$key]['nickname'] = $c_member['nickname'];
    }

    $sql = 'SELECT COUNT(c_diary_comment_id) FROM c_diary_comment WHERE c_diary_id = ?';
    $params = array(intval($c_diary_id));
    $total_num = db_get_one($sql, $params);

    if ($total_num > 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }
        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }
    return array($c_diary_comment_list, $prev, $next, $total_num, $total_page_num);
}

/**
 * 日記IDからその日記を書いたメンバーIDとニックネームと日記公開範囲を得る
 */
function k_p_fh_diary_c_member4c_diary_id($c_diary_id)
{
    $sql = "SELECT cm.c_member_id, cm.nickname,cm. public_flag_diary ";
    $sql .= " FROM c_member AS cm, c_diary AS cd ";
    $sql .= " WHERE cd.c_diary_id = ?";
    $sql .= " AND cm.c_member_id=cd.c_member_id";
    $params = array(intval($c_diary_id));
    return db_get_row($sql, $params);
}

/**
 * フレンドの最新日記リスト
 */
function k_p_h_diary_list_friend_h_diary_list_friend4c_member_id($c_member_id, $page_size, $page)
{
    $from = "c_diary, c_friend";
    $where = "c_friend.c_member_id_from = ?" .
            " AND c_diary.c_member_id = c_friend.c_member_id_to";

    $sql = "SELECT c_diary.* FROM {$from} WHERE {$where}" .
            " ORDER BY c_diary.r_datetime DESC";
    $params = array(intval($c_member_id));
    $lst = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($lst as $key=>$value) {
        $lst[$key]['count_comments'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
        $lst[$key]['c_member'] = db_member_c_member4c_member_id($value['c_member_id']);
    }

    $sql = "SELECT count(*) FROM {$from} WHERE {$where}";
    $total_num = db_get_one($sql, $params);

    if ($total_num != 0) {
        $total_page_num =  ceil($total_num / $page_size);
        if ($page >= $total_page_num) {
            $next = false;
        } else {
            $next = true;
        }

        if ($page <= 1) {
            $prev = false;
        } else {
            $prev = true;
        }
    }

    return array($lst, $prev, $next, $total_num);
}

//--- diary

/**
 * 日記を追加
 */
function db_diary_insert_c_diary($c_member_id, $subject, $body, $public_flag, $is_comment_input = 1)
{
    //function cacheの削除
    pne_cache_drop('db_diary_get_c_diary_list4c_member_id', $c_member_id, 5);

    // タイトルと本文中に書いてあるURLがSNS内でありセッションパラメータを含んでいた場合は削除
    $subject = db_ktai_delete_url_session_parameter($subject);
    $body = db_ktai_delete_url_session_parameter($body);

    $data = array(
        'c_member_id' => intval($c_member_id),
        'subject' => $subject,
        'body' => $body,
        'public_flag' => util_cast_public_flag_diary($public_flag),
        'r_datetime' => db_now(),
        'r_date' => db_now(),
        'u_datetime' => db_now(),
        'is_checked' => 1,
        'image_filename_1' => '',
        'image_filename_2' => '',
        'image_filename_3' => '',
        'is_comment_input' => (bool)$is_comment_input,
    );
    return db_insert('c_diary', $data);
}

/**
 * 日記を編集
 */
function db_diary_update_c_diary($c_diary_id, $subject, $body, $public_flag, $is_comment_input, $image_filename_1 = '', $image_filename_2 = '', $image_filename_3 = '')
{
    //function cacheの削除
    $c_diary = db_diary_get_c_diary4id($c_diary_id);
    pne_cache_drop('db_diary_get_c_diary_list4c_member_id', $c_diary['c_member_id'], 5);

    // タイトルと本文中に書いてあるURLがSNS内でありセッションパラメータを含んでいた場合は削除
    $subject = db_ktai_delete_url_session_parameter($subject);
    $body = db_ktai_delete_url_session_parameter($body);

    $data = array(
        'subject' => $subject,
        'body' => $body,
        'public_flag' => util_cast_public_flag_diary($public_flag),
        'u_datetime' => db_now(),
        'is_comment_input' => (bool)$is_comment_input,
    );
    if ($image_filename_1) $data['image_filename_1'] = $image_filename_1;
    if ($image_filename_2) $data['image_filename_2'] = $image_filename_2;
    if ($image_filename_3) $data['image_filename_3'] = $image_filename_3;

    $where = array(
        'c_diary_id' => intval($c_diary_id),
    );
    return db_update('c_diary', $data, $where);
}

/**
 * 日記削除
 * コメント、画像も削除
 *
 * @param int $c_diary_id
 */
function db_diary_delete_c_diary($c_diary_id)
{
     //function cacheの削除
    $c_diary = db_diary_get_c_diary4id($c_diary_id);
    pne_cache_drop('db_diary_get_c_diary_list4c_member_id', $c_diary['c_member_id'], 5);

   // 画像
    db_image_data_delete($c_diary['image_filename_1'], $c_diary['c_member_id']);
    db_image_data_delete($c_diary['image_filename_2'], $c_diary['c_member_id']);
    db_image_data_delete($c_diary['image_filename_3'], $c_diary['c_member_id']);

    // コメント
    $sql = 'SELECT image_filename_1, image_filename_2, image_filename_3, c_member_id FROM c_diary_comment WHERE c_diary_id =?';
    $params = array(intval($c_diary_id));
    $comment_images = db_get_all($sql, $params);

    //カテゴリ
    db_diary_category_delete_c_diary_category_diary($c_diary_id);

    foreach ($comment_images as $value) {
        db_image_data_delete($value['image_filename_1'], $value['c_member_id']);
        db_image_data_delete($value['image_filename_2'], $value['c_member_id']);
        db_image_data_delete($value['image_filename_3'], $value['c_member_id']);
    }

    $sql = 'DELETE FROM c_diary_comment_log WHERE c_diary_id = ?';
    db_query($sql, $params);

    $sql = 'DELETE FROM c_diary_comment WHERE c_diary_id = ?';
    db_query($sql, $params);

    // 日記
    $sql = 'DELETE FROM c_diary WHERE c_diary_id = ?';
    db_query($sql, $params);
}

/**
 * 指定した番号の日記画像を削除
 */
function db_diary_delete_c_diary_image($c_diary_id, $image_num)
{
    $data = array(
        'image_filename_'.intval($image_num) => '',
    );
    $where = 'c_diary_id = '.intval($c_diary_id);
    return db_update('c_diary', $data, $where);
}

/**
 * 日記に画像を追加する
 */
function db_diary_update_c_diary_image_filename($c_diary_id, $image_filename, $image_num)
{
    $data = array(
        'image_filename_'.intval($image_num) => $image_filename,
    );
    $where = 'c_diary_id = '.intval($c_diary_id);
    return db_update('c_diary', $data, $where);
}

/**
 * 日記の未読/既読を変更
 * c_diary.is_checkedを更新
 */
function db_diary_update_c_diary_is_checked($c_diary_id, $value)
{
    $data = array(
        'is_checked' => (bool)$value,
    );
    $where = 'c_diary_id = '.intval($c_diary_id);
    return db_update('c_diary', $data, $where);
}

//--- diary_comment

/**
 * 日記コメント追加
 *
 * @param  int    $c_member_id
 * @param  int    $c_diary_id
 * @param  string $body
 * @return int    insert_id
 */
function db_diary_insert_c_diary_comment($c_member_id, $c_diary_id, $body)
{
    //function cache削除
    $c_diary =  db_diary_get_c_diary4id($c_diary_id);
    cache_drop_c_diary($c_member_id, $c_diary['c_member_id']);
    pne_cache_drop('p_h_home_c_diary_my_comment_list4c_member_id', $c_member_id, 5);

    // 本文中に書いてあるURLがSNS内でありセッションパラメータを含んでいた場合は削除
    $body = db_ktai_delete_url_session_parameter($body);

    $data = array(
        'c_member_id' => intval($c_member_id),
        'c_diary_id' => intval($c_diary_id),
        'body' => $body,
        'r_datetime' => db_now(),
        'image_filename_1' => '',
        'image_filename_2' => '',
        'image_filename_3' => '',
        'number' => db_diary_get_max_c_diary_comment_number4diary($c_diary_id)+1,
    );
    return db_insert('c_diary_comment', $data);
}

/**
 * 日記コメント用画像追加
 */
function db_diary_insert_c_diary_comment_images($c_diary_comment_id, $image_filename_1 = '', $image_filename_2 = '', $image_filename_3 = '')
{
    $data = array();
    if ($image_filename_1) $data['image_filename_1'] = $image_filename_1;
    if ($image_filename_2) $data['image_filename_2'] = $image_filename_2;
    if ($image_filename_3) $data['image_filename_3'] = $image_filename_3;

    $where = array(
        'c_diary_comment_id' => intval($c_diary_comment_id),
    );
    return db_update('c_diary_comment', $data, $where);
}

/**
 * 日記コメント削除
 *
 * @param   int $c_diary_comment_id
 * @param   int $u  : 削除しようとしている人の c_member_id
 */
function db_diary_delete_c_diary_comment($c_diary_comment_id, $u)
{
    $dc = _do_c_diary_comment4c_diary_comment_id($c_diary_comment_id);
    if ($dc['c_member_id'] != $u && $dc['c_member_id_author'] != $u) {
        return false;
    }

    db_image_data_delete($dc['image_filename_1'], $dc['c_member_id']);
    db_image_data_delete($dc['image_filename_2'], $dc['c_member_id']);
    db_image_data_delete($dc['image_filename_3'], $dc['c_member_id']);

    $sql = 'DELETE FROM c_diary_comment WHERE c_diary_comment_id = ?';
    $params = array(intval($c_diary_comment_id));
    return db_query($sql, $params);
}

// 日記公開範囲の一括設定をする
function db_diary_update_public_flag_diary_all($c_member_id, $public_flag)
{
    $data = array(
        'public_flag' => util_cast_public_flag_diary($public_flag),
    );

    $where = array(
        'c_member_id' => intval($c_member_id),
    );
    return db_update('c_diary', $data, $where);
}

/**
 * 現在の日記へのコメントのナンバーの最大値を返す
 */
function db_diary_get_max_c_diary_comment_number4diary($c_diary_id)
{
    $sql = 'SELECT MAX(number) FROM c_diary_comment WHERE c_diary_id = ?';
    $params = array(intval($c_diary_id));
    return db_get_one($sql, $params, 'main');
}

/**
 * SNS全体の最新日記リスト取得
 * 日記公開範囲を考慮
 *
 * @param   int $limit
 * @return  array_of_array  (c_diary.*, nickname)
 */
function p_h_home_c_diary_all_list($limit)
{
    $sql = 'SELECT * FROM c_diary WHERE public_flag = \'public\''
         . ' ORDER BY c_diary.r_datetime DESC';

    $c_diary_list_all = db_get_all_limit($sql, 0, $limit);

    foreach ($c_diary_list_all as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $c_diary_list_all[$key]['nickname'] = $c_member['nickname'];
        $c_diary_list_all[$key]['count_comments'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
    }
    return $c_diary_list_all;
}


/**
 * 日記コメント記入履歴の追加
 *
 * @param  int    $c_member_id
 * @param  int    $c_diary_id
 */
function db_diary_insert_c_diary_comment_log($c_member_id, $c_diary_id)
{
    $sql = 'SELECT c_diary_id FROM c_diary_comment_log'
         . ' WHERE c_member_id = ? AND c_diary_id = ?';
    $params = array(intval($c_member_id), intval($c_diary_id));
    $log_id = db_get_one($sql, $params, 'main');
    if (!$log_id) {
        $data = array(
            'c_member_id' => intval($c_member_id),
            'c_diary_id' => intval($c_diary_id),
            'r_datetime' => db_now(),
        );
        return db_insert('c_diary_comment_log', $data);
    }
    return false;
}

/**
 * 日記コメント記入履歴の更新
 *
 * @param  int    $c_diary_id
 */
function db_diary_update_c_diary_comment_log($c_diary_id)
{
    $data = array(
        'r_datetime' => db_now(),
    );
    $where = array('c_diary_id' => intval($c_diary_id));
    return db_update('c_diary_comment_log', $data, $where);
}

/**
 * 日記コメント記入履歴の削除
 *
 * @param  int    $c_member_id
 * @param  int    $c_diary_id
 * 該当c_diary_idへのコメント数が0なら、日記コメント記入履歴も削除する。
 *
 */
function db_diary_delete_c_diary_comment_log($c_member_id, $c_diary_id)
{
    // 投稿したコメントの有無
    $sql = 'SELECT COUNT(c_diary_comment_id) FROM c_diary_comment'
         . ' WHERE c_member_id = ? AND c_diary_id = ?';
    $params = array(intval($c_member_id), intval($c_diary_id));
    $count = db_get_one($sql, $params, 'main');

    // コメントが無ければ履歴削除
    if (!$count) {
        $sql = 'DELETE FROM c_diary_comment_log'
             . ' WHERE c_member_id = ? AND c_diary_id = ?';
        $params = array(intval($c_member_id), intval($c_diary_id));
        db_query($sql, $params);
    }
    $sql = 'SELECT * FROM c_diary_comment WHERE c_diary_id = ?'
         . ' ORDER BY c_diary_comment_id DESC';
    $params = array(intval($c_diary_id));
    $comment = db_get_row($sql, $params, 'main');

    // 最新コメントの日付で履歴更新
    $data = array(
        'r_datetime' => $comment['r_datetime'],
    );
    $where = array('c_diary_id' => intval($c_diary_id));
    return db_update('c_diary_comment_log', $data, $where);
}

function db_diary_is_writable_comment4c_diary_id($c_diary_id)
{
    $max_c_diary_comment_number = db_diary_get_max_c_diary_comment_number4diary($c_diary_id);
    if ($max_c_diary_comment_number >= 1000) {
        return false;
    }

    return true;
}

/**
 * メンバーの日記とコメントを削除
 *
 * @param  int    $c_member_id
 */
function db_diary_delete4c_member_id($c_member_id)
{
    $sql = 'SELECT * FROM c_diary WHERE c_member_id = ?';
    $single = array(intval($c_member_id));
    $c_diary_list = db_get_all($sql, $single, 'main');
    foreach ($c_diary_list as $c_diary) {
        db_image_data_delete($c_diary['image_filename_1']);
        db_image_data_delete($c_diary['image_filename_2']);
        db_image_data_delete($c_diary['image_filename_3']);

        // c_diary_comment
        $sql = 'SELECT * FROM c_diary_comment WHERE c_diary_id = ?';
        $params = array(intval($c_diary['c_diary_id']));
        $c_diary_comment_list = db_get_all($sql, $params, 'main');
        foreach ($c_diary_comment_list as $c_diary_comment) {
            db_image_data_delete($c_diary_comment['image_filename_1']);
            db_image_data_delete($c_diary_comment['image_filename_2']);
            db_image_data_delete($c_diary_comment['image_filename_3']);
        }

        $sql = 'DELETE FROM c_diary_comment WHERE c_diary_id = ?';
        db_query($sql, $params);
        db_diary_delete_c_diary_comment_log($c_member_id, $c_diary['c_diary_id']);
    }
    $sql = 'DELETE FROM c_diary WHERE c_member_id = ?';
    db_query($sql, $single);
}

?>
Back to Top