OpenPNE2 /webapp/lib/db/friend.php

Language PHP Lines 904
MD5 Hash 8bcdb0dbd1f2f16a39b23156fa9943d7
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
<?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 int 友達数
 */
function db_friend_count_friends($c_member_id)
{
    $sql = 'SELECT COUNT(*) FROM c_friend WHERE c_member_id_from = ?';
    $params = array(intval($c_member_id));
    return db_get_one($sql, $params);
}

/**
 * 友達かどうか判定
 *
 * @param  int $c_member_id1
 * @param  int $c_member_id2
 * @return bool
 */
function db_friend_is_friend($c_member_id1, $c_member_id2)
{
    $sql = 'SELECT c_friend_id FROM c_friend' .
            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
    $params = array(intval($c_member_id1), intval($c_member_id2));
    return (bool)db_get_one($sql, $params, 'main');
}

/**
 * 友達のメンバーIDリスト取得
 *
 * @param  int $c_member_id
 * @param  bool $except_blocked アクセスブロックされているメンバーを除外するか
 * @return array  友達のメンバーID配列
 */
function db_friend_c_member_id_list($c_member_id, $except_blocked = false)
{
    $sql = 'SELECT c_member_id_to FROM c_friend WHERE c_member_id_from = ? ORDER BY c_friend_id';
    $params = array(intval($c_member_id));
    $friends = db_get_col($sql, $params);

    if ($except_blocked) {
        $blocked = db_member_access_block_list4c_member_id_to($c_member_id);
        $friends = array_diff($friends, $blocked);
    }
    return $friends;
}

/**
 * 紹介文を取得
 */
function db_friend_c_friend_intro($c_member_id_from, $c_member_id_to)
{
    $sql = 'SELECT intro FROM c_friend WHERE c_member_id_from = ? AND c_member_id_to = ?';
    $params = array(intval($c_member_id_from), intval($c_member_id_to));
    return db_get_one($sql, $params);
}

/**
 * メンバーと自分の中間のフレンドを得る
 */
function db_friend_friend_path4c_member_ids($my_id, $target_id)
{
    if (db_friend_is_friend($my_id, $target_id)) {
        return null;
    }

    //自分の友達リスト
    $my_list = db_friend_c_member_id_list($my_id);

    //相手の友達リスト
    $target_list = db_friend_c_member_id_list($target_id);

    //共通の友達リスト
    $result_list = array_intersect($my_list, $target_list);
    if (!$result_list) {
        return null;
    }

    $result_key = array_rand($result_list);
    $result_id  = $result_list[$result_key];

    return db_member_c_member4c_member_id($result_id);
}

/**
 * 自分のフレンドのうち、対象のメンバーと
 * フレンドリンクしていないフレンドのリストを取得
 */
function db_friend_invite_list4c_member_ids($target_c_member_id, $u)
{
    // 自分の友達リスト
    $my_list = db_friend_c_member_id_list($u);

    // ターゲットの友達リスト
    $target_list = db_friend_c_member_id_list($target_c_member_id);
    $target_list[] = $target_c_member_id;

    // 自分の友達リストからターゲットの友達を引く
    $result = array_diff($my_list, $target_list);

    $list = array();
    foreach ($result as $value) {
        $list[] = db_member_c_member4c_member_id_LIGHT($value);
    }
    return $list;
}

//---

function db_friend_friend_list4c_member_id2($c_member_id, $page_size, $page, $orderby)
{
    switch ($orderby) {
    case "ld":
        $orderby = "m.access_date DESC";
        break;
    case "la":
        $orderby = "m.access_date";
        break;
    case "rd":
        $orderby = "f.r_datetime DESC";
        break;
    case "ra":
        $orderby = "f.r_datetime";
        break;
    default:
        $orderby = "f.r_datetime DESC";
    }

    $sql = "SELECT" .
          " m.c_member_id, m.access_date, m.nickname, m.image_filename" .
          ", f.intro, f.r_datetime" .
        " FROM c_member AS m, c_friend AS f" .
        " WHERE f.c_member_id_from = ?" .
          " AND f.c_member_id_to = m.c_member_id" .
        " ORDER BY ". $orderby;
    $params = array(intval($c_member_id));
    $friend_list = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($friend_list as $key => $value) {
        $friend_list[$key]['friend_count'] = db_friend_count_friends($value['c_member_id']);
    }

    $total_num = db_friend_count_friends($c_member_id);
    $total_page_num =  ceil($total_num / $page_size);

    $next = ($page < $total_page_num);
    $prev = ($page > 1);

    return array($friend_list , $prev , $next, $total_page_num);
}

function db_friend_intro_list4c_member_id($c_member_id)
{
    $sql = "SELECT c_member_id_from AS c_member_id, intro FROM c_friend" .
            " WHERE c_member_id_to = ? AND intro <> ''" .
            " ORDER BY r_datetime_intro DESC";
    $params = array(intval($c_member_id));

    $list = db_get_all($sql, $params);
    foreach ($list as $key => $value) {
        $list[$key] += db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
    }
    return $list;
}

function db_friend_intro_list_with_my_intro4c_member_id($c_member_id)
{
    $list = db_friend_intro_list4c_member_id($c_member_id);

    $sql = 'SELECT intro FROM c_friend WHERE c_member_id_from = ? AND c_member_id_to = ?';
    foreach ($list as $key => $value) {
        $params = array(intval($c_member_id), intval($value['c_member_id']));
        $list[$key]['has_intro_by_me'] = (db_get_one($sql, $params) !== '');
    }
    return $list;
}

/**
 * フレンドからの紹介文リスト
 */
function db_friend_c_friend_comment4c_member_id($c_member_id, $limit = 5)
{
    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_LONG, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $sql = "SELECT c_member_id_from AS c_member_id, intro FROM c_friend" .
            " WHERE c_member_id_to = ? AND intro <> ''" .
            " ORDER BY r_datetime_intro 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']);
    }
    return $list;
}

/**
 * c_friend取得
 */
function db_friend_c_friend4c_member_id_from4c_member_id_to($c_member_id_from,$c_member_id_to)
{
    $sql =  "SELECT c_friend_id,".
                "c_member_id_from,".
                "c_member_id_to,".
                "intro,".
                "r_datetime".
            " FROM c_friend".
            " WHERE c_friend.c_member_id_from = ?" .
                " AND c_friend.c_member_id_to = ?" .
                " AND intro <> ''";
    $params = array(intval($c_member_id_from), intval($c_member_id_to));
    return db_get_row($sql, $params);
}

/**
 * フレンドリスト
 */
function db_friend_c_friend_list4c_member_id($c_member_id, $limit = 0)
{
    $result = db_friend_c_friend_id_list4c_member_id($c_member_id, $limit);

    foreach ($result as $key => $value) {
        $result[$key] = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
        $result[$key]['friend_count'] = db_friend_count_friends($value['c_member_id']);
    }
    return $result;
}

/**
 * フレンドリスト用IDリストを取得する関数
 */
function db_friend_c_friend_id_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_LONG, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $sql = 'SELECT c_member_id_to AS c_member_id FROM c_friend' .
            ' WHERE c_member_id_from = ?' . db_order_by_rand();
    $params = array(intval($c_member_id));
    if ($limit) {
        $result = db_get_all_limit($sql, 0, $limit, $params);
    } else {
        $result = db_get_all($sql, $params);
    }

    return $result;
}

function db_friend_intro_body4c_member_id($u, $target_c_member_id)
{
    $sql = "SELECT intro FROM c_friend" .
            " WHERE c_member_id_to = ? AND c_member_id_from = ?";
    $params = array(intval($target_c_member_id), intval($u));
    return db_get_one($sql, $params);
}

/**
 * あなたにフレンドリンクを要請しているメンバー
 *
 * @param   int $c_member_id_to : 要請されている方(あなた)
 * @return  array_of_array
 *              c_friend_confirm.*
 *              nickname        : 要請している人の名前
 *              image_filename  : 要請している人の画像
 * @see     h_confirm_list.php
 */
function db_friend_anatani_c_friend_confirm_list4c_member_id($c_member_id_to)
{
    $sql = 'SELECT * FROM c_friend_confirm WHERE c_member_id_to = ?' .
            ' ORDER BY r_datetime DESC';
    $params = array(intval($c_member_id_to));
    $c_friend_confirm_list = db_get_all($sql, $params);

    foreach ($c_friend_confirm_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_from']);
        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
        $c_friend_confirm_list[$key]['image_filename'] = $c_member['image_filename'];
    }
    return $c_friend_confirm_list;
}

/**
 * あなたがフレンドリンクを要請しているメンバー
 *
 * @param   int $c_member_id_from   : 要請している方(あなた)
 * @return  array_of_array
 *              c_friend_confirm.*
 *              nickname        : 要請されている人の名前
 *              image_filename  : 要請されている人の画像
 * @see     h_confirm_list.php
 */
function db_friend_anataga_c_friend_confirm_list4c_member_id($c_member_id_from)
{
    $sql = 'SELECT * FROM c_friend_confirm WHERE c_member_id_from = ?' .
            ' ORDER BY r_datetime DESC';
    $params = array(intval($c_member_id_from));
    $c_friend_confirm_list = db_get_all($sql, $params);

    foreach ($c_friend_confirm_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_to']);
        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
        $c_friend_confirm_list[$key]['image_filename'] = $c_member['image_filename'];
    }
    return $c_friend_confirm_list;
}

function db_friend_c_friend_intro_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_LONG, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $sql = 'SELECT * FROM c_friend' .
            ' WHERE c_member_id_to = ? AND intro <> \'\'' . db_order_by_rand();
    $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_from']);
    }
    return $list;
}

function db_friend_c_friend_list_disp4c_member_id($c_member_id, $page = 1, $size = 20)
{
    $sql =  "SELECT c_member_id,".
                "image_filename,".
                "nickname,".
                "intro,".
                "c_member_id_from,".
                "c_member_id_to,".
                "is_display_friend_home".
            " FROM c_member,".
                "c_friend".
            " WHERE c_member.c_member_id = c_friend.c_member_id_to".
                " AND c_friend.c_member_id_from = ?".
            " ORDER BY c_friend.r_datetime DESC";
    $params = array(intval($c_member_id));
    $c_friend_list = db_get_all_page($sql, $page, $size, $params);

    foreach ($c_friend_list as $key => $value) {
        $c_friend = db_friend_c_friend4c_member_id_from4c_member_id_to($value['c_member_id_from'], $value['c_member_id_to']);
        $c_friend_list[$key]['intro'] = $c_friend['intro'];

        $c_friend_list[$key]['friend_count'] = db_friend_count_friends($value['c_member_id_to']);
    }

    $total_num = db_friend_count_friends($c_member_id);
    $prev = 0;
    $next = 0;
    if ($total_num) {
        $total_page_num = ceil($total_num / $size);
        if ($page < $total_page_num) {
            $next = $page + 1;
        }

        if ($page > 1) {
            $prev = $page - 1;
        }
    }

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

function db_friend_status($u, $target_c_member_id)
{
    $ret = array(
        'c_member'      => db_member_c_member4c_member_id($target_c_member_id),
        'is_friend'     => false,
        'is_friend_confirm' => false,
    );

    $ret['is_friend'] = db_friend_is_friend($u, $target_c_member_id);
    $ret['is_friend_confirm'] = db_friend_is_friend_link_wait($u, $target_c_member_id);

    return $ret;
}

/**
 * 友達リンク要請情報をIDから取得
 *
 * @param   int $c_friend_confirm_id
 * @return  array c_friend_confirm.*
 */
function db_friend_c_friend_confirm4c_friend_confirm_id($c_friend_confirm_id)
{
    $sql = 'SELECT * FROM c_friend_confirm WHERE c_friend_confirm_id = ?';
    $params = array(intval($c_friend_confirm_id));
    return db_get_row($sql, $params);
}

function db_friend_is_friend_link_wait($c_member_id_from, $c_member_id_to)
{
    //from→to
    $sql = "SELECT count(*)";
    $sql .= " FROM  c_friend_confirm";
    $sql .= " WHERE c_member_id_from = ?";
    $sql .= " AND c_member_id_to = ?";
    $params = array(intval($c_member_id_from), intval($c_member_id_to));
    $countA = db_get_one($sql, $params);

    //to→from
    $sql = "SELECT count(*)";
    $sql .= " FROM  c_friend_confirm";
    $sql .= " WHERE c_member_id_from = ?";
    $sql .= " AND c_member_id_to = ?";
    $params = array(intval($c_member_id_to), intval($c_member_id_from));
    $countB = db_get_one($sql, $params);

    if ($countA || $countB) {
        return true;
    } else {
        return false;
    }
}

function db_friend_c_friend_list4c_member_id2($c_member_id)
{
    $sql = "SELECT c_member.* FROM c_friend, c_member" .
        " WHERE c_friend.c_member_id_from = ?".
        " AND c_member.c_member_id=c_friend.c_member_id_to".
        " ORDER BY c_friend.r_datetime DESC";
    $params = array(intval($c_member_id));
    return db_get_all($sql, $params);
}

/**
 * フレンドのランダムなリストを得る
 */
function db_friend_c_friend_list_random4c_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_LONG, __FUNCTION__, $funcargs);
        $is_recurred = false;
        return $result;
    }

    $sql = "SELECT cf.* , cm.nickname";
    $sql .= " FROM c_friend AS cf, c_member AS cm";
    $sql .= " WHERE cf.c_member_id_from = ?" .
            " AND cm.c_member_id=cf.c_member_id_to";
    $sql .= db_order_by_rand();
    $params = array(intval($c_member_id));
    $c_friend_list = db_get_all_limit($sql, 0, $limit, $params);

    foreach ($c_friend_list as $key => $value) {
        $c_friend_list[$key]['count_friend'] = db_friend_count_friends($value['c_member_id_to']);
    }

    return $c_friend_list;
}

/**
 * 2つのメンバーIDからその2人の関係を返す。
 */
function db_friend_relationship4two_members($c_member_id, $target_c_member_id)
{
    $relation = array(
        'friend' => false,
        'wait'   => false
    );

    // リンク承認待ちかどうかをチェック
    $sql = 'SELECT c_friend_confirm_id FROM c_friend_confirm' .
            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
    $params = array(intval($c_member_id), intval($target_c_member_id));

    if (db_get_one($sql, $params)) {
        $relation['wait'] = true;
        return $relation;
    }

    // 友達かどうかチェック
    $sql = 'SELECT c_friend_id FROM c_friend' .
            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';

    if (db_get_one($sql, $params)) {
        $relation['friend'] = true;
        return $relation;
    }

    //他人
    return $relation;
}

function db_friend_friend_list4c_member_id($c_member_id,$page_size,$page)
{
    $sql = "SELECT ".
            "c_friend_id,".
            "c_member_id_from,".
            "c_member_id_to,".
            "r_datetime,".
            "is_display_friend_home".
        " FROM ".
            "c_friend".
        " WHERE c_member_id_from = ?".
        " ORDER BY r_datetime DESC";
    $params = array(intval($c_member_id));
    $c_friend_list = db_get_all_page($sql, $page, $page_size, $params);

    foreach ($c_friend_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id($value['c_member_id_to']);

        $c_friend_list[$key]['image_filename'] = $c_member['image_filename'];
        $c_friend_list[$key]['c_member_id'] = $c_member['c_member_id'];
        $c_friend_list[$key]['nickname'] = $c_member['nickname'];
        $c_friend_list[$key]['access_date'] = $c_member['access_date'];

        $c_friend_list[$key]['friend_count'] = db_friend_count_friends($c_member['c_member_id']);
    }

    $total_num = db_friend_count_friends($c_member_id);

    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_friend_list, $prev, $next, $total_num);
}

/**
 * あなたにフレンドリンクを要請しているメンバー数を取得(リンク承認待ち)
 *
 * @param int $c_member_id
 * @return int 要請しているメンバー数
 */
function db_friend_count_c_anatani_friend_confirm($c_member_id)
{
    $sql = 'SELECT COUNT(*) FROM c_friend_confirm WHERE c_member_id_to = ?';
    $params = array(intval($c_member_id));
    return db_get_one($sql, $params);
}

/**
 * あなたにフレンドリンクを要請しているメンバー(リンク承認待ち)
 *
 * @param   int $c_member_id_to : 要請されている方(あなた)
 * @return  array_of_array
 *              c_friend_confirm.*
 *              nickname        : 要請している人の名前
 *              message_disp    : 表示用メッセージ
 */
function db_friend_ktai_anatani_c_friend_confirm_list4c_member_id($c_member_id_to)
{
    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_to = ?";
    $sql .= " ORDER BY r_datetime DESC";
    $params = array(intval($c_member_id_to));
    $c_friend_confirm_list = db_get_all($sql, $params);

    foreach ($c_friend_confirm_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_from']);
        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
    }
    return $c_friend_confirm_list;
}

function db_friend_ktai_anatani_c_friend_confirm_list4c_member_id_with_pager($c_member_id_to, $page_size, $page)
{
    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_to = ?";
    $sql .= " ORDER BY r_datetime ASC";
    $params = array(intval($c_member_id_to));
    $c_friend_confirm_list = db_get_all_page($sql, $page, $page_size, $params);

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

    $total_num = db_friend_count_c_anatani_friend_confirm($c_member_id_to);

    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_friend_confirm_list, $prev, $next, $total_num);
}

/**
 * あなたがフレンドリンクを要請しているメンバー数を取得(リンク承認待ち)
 *
 * @param int $c_member_id
 * @return int 要請しているメンバー数
 */
function db_friend_count_c_anataga_friend_confirm($c_member_id)
{
    $sql = 'SELECT COUNT(*) FROM c_friend_confirm WHERE c_member_id_from = ?';
    $params = array(intval($c_member_id));
    return db_get_one($sql, $params);
}

/**
 * あなたがフレンドリンクを要請しているメンバー(リンク要請中)
 *
 * @param   int $c_member_id_from   : 要請している方(あなた)
 * @return  array_of_array
 *              c_friend_confirm.*
 *              nickname        : 要請されている人の名前
 *              message_disp    : 表示用メッセージ
 */
function db_friend_ktai_anataga_c_friend_confirm_list4c_member_id($c_member_id_from)
{
    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_from = ?";
    $sql .= " ORDER BY r_datetime DESC";
    $params = array(intval($c_member_id_from));
    $c_friend_confirm_list = db_get_all($sql, $params);

    foreach ($c_friend_confirm_list as $key => $value) {
        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_to']);
        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
    }
    return $c_friend_confirm_list;
}

function db_friend_ktai_anataga_c_friend_confirm_list4c_member_id_with_pager($c_member_id_from, $page_size, $page)
{
    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_from = ?";
    $sql .= " ORDER BY r_datetime ASC";
    $params = array(intval($c_member_id_from));
    $c_friend_confirm_list = db_get_all_page($sql, $page, $page_size, $params);

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

    $total_num = db_friend_count_c_anataga_friend_confirm($c_member_id_from);

    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_friend_confirm_list, $prev, $next, $total_num);
}

/**
 * 友達ランダム50人
 */
function db_friend_ktai_c_friend_list_random4c_member_id($c_member_id)
{
    $sql = "SELECT cf.c_member_id_to, cm.nickname";
    $sql .= " FROM c_friend AS cf, c_member AS cm";
    $sql .= " WHERE cf.c_member_id_from = ?".
            " AND cf.c_member_id_to = cm.c_member_id";
    $sql .= db_order_by_rand();
    $params = array(intval($c_member_id));
    return db_get_all_limit($sql, 0, 50, $params);
}

function db_friend_intro_list4c_friend_comment_list($c_friend_comment_list, $page, $page_size)
{
    $lst = array_slice($c_friend_comment_list, ($page - 1) * $page_size, $page_size);
    $total_num = count($c_friend_comment_list);

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

/*** write ***/

//--- c_friend

/**
 * フレンドリンクを追加
 */
function db_friend_insert_c_friend($c_member_id_from, $c_member_id_to)
{
    //function cache削除
    cache_drop_c_member($c_member_id_from);
    cache_drop_c_member($c_member_id_to);

    if (($c_member_id_from < 1) || ($c_member_id_to < 1))
        return false;

    if (db_friend_is_friend($c_member_id_from, $c_member_id_to))
        return false;

    $data = array(
        'c_member_id_from' => intval($c_member_id_from),
        'c_member_id_to' => intval($c_member_id_to),
        'r_datetime' => db_now(),
        'intro' => '',
    );
    db_insert('c_friend', $data);

    $data = array(
        'c_member_id_from' => intval($c_member_id_to),
        'c_member_id_to' => intval($c_member_id_from),
        'r_datetime' => db_now(),
        'intro' => '',
    );
    db_insert('c_friend', $data);
}

/**
 * リンク申請からフレンドリンクを追加
 *
 * @param   int $c_friend_confirm_id
 * @param   int $u  自分のc_member_id
 */
function db_friend_insert_c_friend4confirm($c_friend_confirm_id, $u)
{
    $confirm = db_friend_c_friend_confirm4c_friend_confirm_id($c_friend_confirm_id);
    if ($confirm['c_member_id_to'] != $u) {
        return false;
    }
    $c_member_id_from = $confirm['c_member_id_from'];
    $c_member_id_to   = $confirm['c_member_id_to'];

    // フレンドリンクを追加
    db_friend_insert_c_friend($c_member_id_from, $c_member_id_to);

    // フレンド申請を削除
    db_friend_delete_c_friend_confirm($c_friend_confirm_id, $u);
    return true;
}

/**
 * フレンドリンクを削除
 */
function db_friend_delete_c_friend($c_member_id_from, $c_member_id_to)
{
    //function cache削除
    cache_drop_c_member($c_member_id_from);
    cache_drop_c_member($c_member_id_to);

    $sql = 'DELETE FROM c_friend' .
            ' WHERE (c_member_id_from = ? AND c_member_id_to = ?)' .
               ' OR (c_member_id_to = ? AND c_member_id_from = ?)';
    $params = array(
        intval($c_member_id_from), intval($c_member_id_to),
        intval($c_member_id_from), intval($c_member_id_to));
    db_query($sql, $params);
}

/**
 * フレンド紹介文入力・編集
 */
function db_friend_update_c_friend_intro($c_member_id_from, $c_member_id_to, $intro)
{
    //function cacheを削除
    cache_drop_c_friend_intro($c_member_id_to);

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

    $data = array(
        'intro' => $intro,
        'r_datetime_intro' => db_now(),
    );
    $where = array(
        'c_member_id_from' => intval($c_member_id_from),
        'c_member_id_to' => intval($c_member_id_to),
    );
    db_update('c_friend', $data, $where);
}

//--- c_friend_confirm

/**
 * フレンド申請を追加
 *
 * @param  int    $c_member_id_from
 * @param  int    $c_member_id_to
 * @param  string $message
 * @return int insert_id
 */
function db_friend_insert_c_friend_confirm($c_member_id_from, $c_member_id_to, $message)
{
    // タイトルと本文中に書いてあるURLがSNS内でありセッションパラメータを含んでいた場合は削除
    $message = db_ktai_delete_url_session_parameter($message);

    $data = array(
        'c_member_id_from' => intval($c_member_id_from),
        'c_member_id_to'   => intval($c_member_id_to),
        'message'          => $message,
        'r_datetime'       => db_now(),
    );
    return db_insert('c_friend_confirm', $data);
}

/**
 * フレンド申請を削除
 *
 * @param  int  $c_friend_confirm_id
 * @param  int  $u 自分のc_member_id
 */
function db_friend_delete_c_friend_confirm($c_friend_confirm_id, $u)
{
    $sql = 'DELETE FROM c_friend_confirm WHERE c_friend_confirm_id = ?' .
            ' AND (c_member_id_from = ? OR c_member_id_to = ?)';
    $params = array(intval($c_friend_confirm_id), intval($u), intval($u));
    db_query($sql, $params);
}

/**
 * フレンド最新書き込みのホーム表示設定
 *
 * @param int $c_member_id_from (自分)
 * @param int $c_member_id_to   (対象となるマイフレンド)
 * @param int $is_display_friend_home (表示するか否か)
 */
function db_friend_update_is_display_friend_home($c_member_id_from, $c_member_id_to, $is_display_friend_home)
{
    pne_cache_drop('p_h_home_c_diary_friend_list4c_member_id', (int)$c_member_id_from, 5, true);
    pne_cache_drop('p_h_home_c_diary_friend_list4c_member_id', (string)$c_member_id_from, 5, true);
    $data = array(
        'is_display_friend_home' => (bool)$is_display_friend_home,
    );
    $where = array(
        'c_member_id_from'  => intval($c_member_id_from),
        'c_member_id_to'    => intval($c_member_id_to),
    );
    return db_update('c_friend', $data, $where);
}
/**
 * 友達のメンバーで、新着表示可否のIDリスト取得
 *
 * @param  int $c_member_id
 * @param  bool $is_display_friend_home = 1:新着表示対象 / 0:新着表示対象外
 * @return array  友達のメンバーID配列
 */
function db_friend_is_display_friend_home_list($c_member_id, $is_display_friend_home = 1)
{
    $sql  = 'SELECT c_member_id_to ';
    $sql .= 'FROM c_friend ';
    $sql .= 'WHERE c_member_id_from = ? ';
    $sql .= 'AND is_display_friend_home = ? ';
    $sql .= 'ORDER BY c_friend_id';
    $params = array(intval($c_member_id), intval($is_display_friend_home));
    $friends = db_get_col($sql, $params);

    return $friends;
}

?>
Back to Top