PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/lib/db/friend.php

https://github.com/openpne/OpenPNE2
PHP | 903 lines | 608 code | 119 blank | 176 comment | 43 complexity | 8bcdb0dbd1f2f16a39b23156fa9943d7 MD5 | raw file
  1<?php
  2/**
  3 * @copyright 2005-2008 OpenPNE Project
  4 * @license   http://www.php.net/license/3_01.txt PHP License 3.01
  5 */
  6
  7/**
  8 * あるメンバーの友達数を取得
  9 *
 10 * @param int $c_member_id
 11 * @return int 友達数
 12 */
 13function db_friend_count_friends($c_member_id)
 14{
 15    $sql = 'SELECT COUNT(*) FROM c_friend WHERE c_member_id_from = ?';
 16    $params = array(intval($c_member_id));
 17    return db_get_one($sql, $params);
 18}
 19
 20/**
 21 * 友達かどうか判定
 22 *
 23 * @param  int $c_member_id1
 24 * @param  int $c_member_id2
 25 * @return bool
 26 */
 27function db_friend_is_friend($c_member_id1, $c_member_id2)
 28{
 29    $sql = 'SELECT c_friend_id FROM c_friend' .
 30            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
 31    $params = array(intval($c_member_id1), intval($c_member_id2));
 32    return (bool)db_get_one($sql, $params, 'main');
 33}
 34
 35/**
 36 * 友達のメンバーIDリスト取得
 37 *
 38 * @param  int $c_member_id
 39 * @param  bool $except_blocked アクセスブロックされているメンバーを除外するか
 40 * @return array  友達のメンバーID配列
 41 */
 42function db_friend_c_member_id_list($c_member_id, $except_blocked = false)
 43{
 44    $sql = 'SELECT c_member_id_to FROM c_friend WHERE c_member_id_from = ? ORDER BY c_friend_id';
 45    $params = array(intval($c_member_id));
 46    $friends = db_get_col($sql, $params);
 47
 48    if ($except_blocked) {
 49        $blocked = db_member_access_block_list4c_member_id_to($c_member_id);
 50        $friends = array_diff($friends, $blocked);
 51    }
 52    return $friends;
 53}
 54
 55/**
 56 * 紹介文を取得
 57 */
 58function db_friend_c_friend_intro($c_member_id_from, $c_member_id_to)
 59{
 60    $sql = 'SELECT intro FROM c_friend WHERE c_member_id_from = ? AND c_member_id_to = ?';
 61    $params = array(intval($c_member_id_from), intval($c_member_id_to));
 62    return db_get_one($sql, $params);
 63}
 64
 65/**
 66 * メンバーと自分の中間のフレンドを得る
 67 */
 68function db_friend_friend_path4c_member_ids($my_id, $target_id)
 69{
 70    if (db_friend_is_friend($my_id, $target_id)) {
 71        return null;
 72    }
 73
 74    //自分の友達リスト
 75    $my_list = db_friend_c_member_id_list($my_id);
 76
 77    //相手の友達リスト
 78    $target_list = db_friend_c_member_id_list($target_id);
 79
 80    //共通の友達リスト
 81    $result_list = array_intersect($my_list, $target_list);
 82    if (!$result_list) {
 83        return null;
 84    }
 85
 86    $result_key = array_rand($result_list);
 87    $result_id  = $result_list[$result_key];
 88
 89    return db_member_c_member4c_member_id($result_id);
 90}
 91
 92/**
 93 * 自分のフレンドのうち、対象のメンバーと
 94 * フレンドリンクしていないフレンドのリストを取得
 95 */
 96function db_friend_invite_list4c_member_ids($target_c_member_id, $u)
 97{
 98    // 自分の友達リスト
 99    $my_list = db_friend_c_member_id_list($u);
100
101    // ターゲットの友達リスト
102    $target_list = db_friend_c_member_id_list($target_c_member_id);
103    $target_list[] = $target_c_member_id;
104
105    // 自分の友達リストからターゲットの友達を引く
106    $result = array_diff($my_list, $target_list);
107
108    $list = array();
109    foreach ($result as $value) {
110        $list[] = db_member_c_member4c_member_id_LIGHT($value);
111    }
112    return $list;
113}
114
115//---
116
117function db_friend_friend_list4c_member_id2($c_member_id, $page_size, $page, $orderby)
118{
119    switch ($orderby) {
120    case "ld":
121        $orderby = "m.access_date DESC";
122        break;
123    case "la":
124        $orderby = "m.access_date";
125        break;
126    case "rd":
127        $orderby = "f.r_datetime DESC";
128        break;
129    case "ra":
130        $orderby = "f.r_datetime";
131        break;
132    default:
133        $orderby = "f.r_datetime DESC";
134    }
135
136    $sql = "SELECT" .
137          " m.c_member_id, m.access_date, m.nickname, m.image_filename" .
138          ", f.intro, f.r_datetime" .
139        " FROM c_member AS m, c_friend AS f" .
140        " WHERE f.c_member_id_from = ?" .
141          " AND f.c_member_id_to = m.c_member_id" .
142        " ORDER BY ". $orderby;
143    $params = array(intval($c_member_id));
144    $friend_list = db_get_all_page($sql, $page, $page_size, $params);
145
146    foreach ($friend_list as $key => $value) {
147        $friend_list[$key]['friend_count'] = db_friend_count_friends($value['c_member_id']);
148    }
149
150    $total_num = db_friend_count_friends($c_member_id);
151    $total_page_num =  ceil($total_num / $page_size);
152
153    $next = ($page < $total_page_num);
154    $prev = ($page > 1);
155
156    return array($friend_list , $prev , $next, $total_page_num);
157}
158
159function db_friend_intro_list4c_member_id($c_member_id)
160{
161    $sql = "SELECT c_member_id_from AS c_member_id, intro FROM c_friend" .
162            " WHERE c_member_id_to = ? AND intro <> ''" .
163            " ORDER BY r_datetime_intro DESC";
164    $params = array(intval($c_member_id));
165
166    $list = db_get_all($sql, $params);
167    foreach ($list as $key => $value) {
168        $list[$key] += db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
169    }
170    return $list;
171}
172
173function db_friend_intro_list_with_my_intro4c_member_id($c_member_id)
174{
175    $list = db_friend_intro_list4c_member_id($c_member_id);
176
177    $sql = 'SELECT intro FROM c_friend WHERE c_member_id_from = ? AND c_member_id_to = ?';
178    foreach ($list as $key => $value) {
179        $params = array(intval($c_member_id), intval($value['c_member_id']));
180        $list[$key]['has_intro_by_me'] = (db_get_one($sql, $params) !== '');
181    }
182    return $list;
183}
184
185/**
186 * フレンドからの紹介文リスト
187 */
188function db_friend_c_friend_comment4c_member_id($c_member_id, $limit = 5)
189{
190    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ
191
192    if (!$is_recurred) {  //function cacheのために再帰処理を行う
193        $is_recurred = true;
194        $funcargs = func_get_args();
195        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_LONG, __FUNCTION__, $funcargs);
196        $is_recurred = false;
197        return $result;
198    }
199
200    $sql = "SELECT c_member_id_from AS c_member_id, intro FROM c_friend" .
201            " WHERE c_member_id_to = ? AND intro <> ''" .
202            " ORDER BY r_datetime_intro DESC";
203    $params = array(intval($c_member_id));
204
205    $list = db_get_all_limit($sql, 0, $limit, $params);
206    foreach ($list as $key => $value) {
207        $list[$key] += db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
208    }
209    return $list;
210}
211
212/**
213 * c_friend取得
214 */
215function db_friend_c_friend4c_member_id_from4c_member_id_to($c_member_id_from,$c_member_id_to)
216{
217    $sql =  "SELECT c_friend_id,".
218                "c_member_id_from,".
219                "c_member_id_to,".
220                "intro,".
221                "r_datetime".
222            " FROM c_friend".
223            " WHERE c_friend.c_member_id_from = ?" .
224                " AND c_friend.c_member_id_to = ?" .
225                " AND intro <> ''";
226    $params = array(intval($c_member_id_from), intval($c_member_id_to));
227    return db_get_row($sql, $params);
228}
229
230/**
231 * フレンドリスト
232 */
233function db_friend_c_friend_list4c_member_id($c_member_id, $limit = 0)
234{
235    $result = db_friend_c_friend_id_list4c_member_id($c_member_id, $limit);
236
237    foreach ($result as $key => $value) {
238        $result[$key] = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
239        $result[$key]['friend_count'] = db_friend_count_friends($value['c_member_id']);
240    }
241    return $result;
242}
243
244/**
245 * フレンドリスト用IDリストを取得する関数
246 */
247function db_friend_c_friend_id_list4c_member_id($c_member_id, $limit)
248{
249    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ
250
251    if (!$is_recurred) {  //function cacheのために再帰処理を行う
252        $is_recurred = true;
253        $funcargs = func_get_args();
254        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_LONG, __FUNCTION__, $funcargs);
255        $is_recurred = false;
256        return $result;
257    }
258
259    $sql = 'SELECT c_member_id_to AS c_member_id FROM c_friend' .
260            ' WHERE c_member_id_from = ?' . db_order_by_rand();
261    $params = array(intval($c_member_id));
262    if ($limit) {
263        $result = db_get_all_limit($sql, 0, $limit, $params);
264    } else {
265        $result = db_get_all($sql, $params);
266    }
267
268    return $result;
269}
270
271function db_friend_intro_body4c_member_id($u, $target_c_member_id)
272{
273    $sql = "SELECT intro FROM c_friend" .
274            " WHERE c_member_id_to = ? AND c_member_id_from = ?";
275    $params = array(intval($target_c_member_id), intval($u));
276    return db_get_one($sql, $params);
277}
278
279/**
280 * あなたにフレンドリンクを要請しているメンバー
281 *
282 * @param   int $c_member_id_to : 要請されている方(あなた)
283 * @return  array_of_array
284 *              c_friend_confirm.*
285 *              nickname        : 要請している人の名前
286 *              image_filename  : 要請している人の画像
287 * @see     h_confirm_list.php
288 */
289function db_friend_anatani_c_friend_confirm_list4c_member_id($c_member_id_to)
290{
291    $sql = 'SELECT * FROM c_friend_confirm WHERE c_member_id_to = ?' .
292            ' ORDER BY r_datetime DESC';
293    $params = array(intval($c_member_id_to));
294    $c_friend_confirm_list = db_get_all($sql, $params);
295
296    foreach ($c_friend_confirm_list as $key => $value) {
297        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_from']);
298        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
299        $c_friend_confirm_list[$key]['image_filename'] = $c_member['image_filename'];
300    }
301    return $c_friend_confirm_list;
302}
303
304/**
305 * あなたがフレンドリンクを要請しているメンバー
306 *
307 * @param   int $c_member_id_from   : 要請している方(あなた)
308 * @return  array_of_array
309 *              c_friend_confirm.*
310 *              nickname        : 要請されている人の名前
311 *              image_filename  : 要請されている人の画像
312 * @see     h_confirm_list.php
313 */
314function db_friend_anataga_c_friend_confirm_list4c_member_id($c_member_id_from)
315{
316    $sql = 'SELECT * FROM c_friend_confirm WHERE c_member_id_from = ?' .
317            ' ORDER BY r_datetime DESC';
318    $params = array(intval($c_member_id_from));
319    $c_friend_confirm_list = db_get_all($sql, $params);
320
321    foreach ($c_friend_confirm_list as $key => $value) {
322        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_to']);
323        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
324        $c_friend_confirm_list[$key]['image_filename'] = $c_member['image_filename'];
325    }
326    return $c_friend_confirm_list;
327}
328
329function db_friend_c_friend_intro_list4c_member_id($c_member_id, $limit)
330{
331    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ
332
333    if (!$is_recurred) {  //function cacheのために再帰処理を行う
334        $is_recurred = true;
335        $funcargs = func_get_args();
336        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_LONG, __FUNCTION__, $funcargs);
337        $is_recurred = false;
338        return $result;
339    }
340
341    $sql = 'SELECT * FROM c_friend' .
342            ' WHERE c_member_id_to = ? AND intro <> \'\'' . db_order_by_rand();
343    $params = array(intval($c_member_id));
344    $list = db_get_all_limit($sql, 0, $limit, $params);
345
346    foreach ($list as $key => $value) {
347        $list[$key] += db_member_c_member4c_member_id_LIGHT($value['c_member_id_from']);
348    }
349    return $list;
350}
351
352function db_friend_c_friend_list_disp4c_member_id($c_member_id, $page = 1, $size = 20)
353{
354    $sql =  "SELECT c_member_id,".
355                "image_filename,".
356                "nickname,".
357                "intro,".
358                "c_member_id_from,".
359                "c_member_id_to,".
360                "is_display_friend_home".
361            " FROM c_member,".
362                "c_friend".
363            " WHERE c_member.c_member_id = c_friend.c_member_id_to".
364                " AND c_friend.c_member_id_from = ?".
365            " ORDER BY c_friend.r_datetime DESC";
366    $params = array(intval($c_member_id));
367    $c_friend_list = db_get_all_page($sql, $page, $size, $params);
368
369    foreach ($c_friend_list as $key => $value) {
370        $c_friend = db_friend_c_friend4c_member_id_from4c_member_id_to($value['c_member_id_from'], $value['c_member_id_to']);
371        $c_friend_list[$key]['intro'] = $c_friend['intro'];
372
373        $c_friend_list[$key]['friend_count'] = db_friend_count_friends($value['c_member_id_to']);
374    }
375
376    $total_num = db_friend_count_friends($c_member_id);
377    $prev = 0;
378    $next = 0;
379    if ($total_num) {
380        $total_page_num = ceil($total_num / $size);
381        if ($page < $total_page_num) {
382            $next = $page + 1;
383        }
384
385        if ($page > 1) {
386            $prev = $page - 1;
387        }
388    }
389
390    return array($c_friend_list, $prev, $next, $total_num);
391}
392
393function db_friend_status($u, $target_c_member_id)
394{
395    $ret = array(
396        'c_member'      => db_member_c_member4c_member_id($target_c_member_id),
397        'is_friend'     => false,
398        'is_friend_confirm' => false,
399    );
400
401    $ret['is_friend'] = db_friend_is_friend($u, $target_c_member_id);
402    $ret['is_friend_confirm'] = db_friend_is_friend_link_wait($u, $target_c_member_id);
403
404    return $ret;
405}
406
407/**
408 * 友達リンク要請情報をIDから取得
409 *
410 * @param   int $c_friend_confirm_id
411 * @return  array c_friend_confirm.*
412 */
413function db_friend_c_friend_confirm4c_friend_confirm_id($c_friend_confirm_id)
414{
415    $sql = 'SELECT * FROM c_friend_confirm WHERE c_friend_confirm_id = ?';
416    $params = array(intval($c_friend_confirm_id));
417    return db_get_row($sql, $params);
418}
419
420function db_friend_is_friend_link_wait($c_member_id_from, $c_member_id_to)
421{
422    //from→to
423    $sql = "SELECT count(*)";
424    $sql .= " FROM  c_friend_confirm";
425    $sql .= " WHERE c_member_id_from = ?";
426    $sql .= " AND c_member_id_to = ?";
427    $params = array(intval($c_member_id_from), intval($c_member_id_to));
428    $countA = db_get_one($sql, $params);
429
430    //to→from
431    $sql = "SELECT count(*)";
432    $sql .= " FROM  c_friend_confirm";
433    $sql .= " WHERE c_member_id_from = ?";
434    $sql .= " AND c_member_id_to = ?";
435    $params = array(intval($c_member_id_to), intval($c_member_id_from));
436    $countB = db_get_one($sql, $params);
437
438    if ($countA || $countB) {
439        return true;
440    } else {
441        return false;
442    }
443}
444
445function db_friend_c_friend_list4c_member_id2($c_member_id)
446{
447    $sql = "SELECT c_member.* FROM c_friend, c_member" .
448        " WHERE c_friend.c_member_id_from = ?".
449        " AND c_member.c_member_id=c_friend.c_member_id_to".
450        " ORDER BY c_friend.r_datetime DESC";
451    $params = array(intval($c_member_id));
452    return db_get_all($sql, $params);
453}
454
455/**
456 * フレンドのランダムなリストを得る
457 */
458function db_friend_c_friend_list_random4c_member_id($c_member_id, $limit)
459{
460    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ
461
462    if (!$is_recurred) {  //function cacheのために再帰処理を行う
463        $is_recurred = true;
464        $funcargs = func_get_args();
465        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_LONG, __FUNCTION__, $funcargs);
466        $is_recurred = false;
467        return $result;
468    }
469
470    $sql = "SELECT cf.* , cm.nickname";
471    $sql .= " FROM c_friend AS cf, c_member AS cm";
472    $sql .= " WHERE cf.c_member_id_from = ?" .
473            " AND cm.c_member_id=cf.c_member_id_to";
474    $sql .= db_order_by_rand();
475    $params = array(intval($c_member_id));
476    $c_friend_list = db_get_all_limit($sql, 0, $limit, $params);
477
478    foreach ($c_friend_list as $key => $value) {
479        $c_friend_list[$key]['count_friend'] = db_friend_count_friends($value['c_member_id_to']);
480    }
481
482    return $c_friend_list;
483}
484
485/**
486 * 2つのメンバーIDからその2人の関係を返す。
487 */
488function db_friend_relationship4two_members($c_member_id, $target_c_member_id)
489{
490    $relation = array(
491        'friend' => false,
492        'wait'   => false
493    );
494
495    // リンク承認待ちかどうかをチェック
496    $sql = 'SELECT c_friend_confirm_id FROM c_friend_confirm' .
497            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
498    $params = array(intval($c_member_id), intval($target_c_member_id));
499
500    if (db_get_one($sql, $params)) {
501        $relation['wait'] = true;
502        return $relation;
503    }
504
505    // 友達かどうかチェック
506    $sql = 'SELECT c_friend_id FROM c_friend' .
507            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
508
509    if (db_get_one($sql, $params)) {
510        $relation['friend'] = true;
511        return $relation;
512    }
513
514    //他人
515    return $relation;
516}
517
518function db_friend_friend_list4c_member_id($c_member_id,$page_size,$page)
519{
520    $sql = "SELECT ".
521            "c_friend_id,".
522            "c_member_id_from,".
523            "c_member_id_to,".
524            "r_datetime,".
525            "is_display_friend_home".
526        " FROM ".
527            "c_friend".
528        " WHERE c_member_id_from = ?".
529        " ORDER BY r_datetime DESC";
530    $params = array(intval($c_member_id));
531    $c_friend_list = db_get_all_page($sql, $page, $page_size, $params);
532
533    foreach ($c_friend_list as $key => $value) {
534        $c_member = db_member_c_member4c_member_id($value['c_member_id_to']);
535
536        $c_friend_list[$key]['image_filename'] = $c_member['image_filename'];
537        $c_friend_list[$key]['c_member_id'] = $c_member['c_member_id'];
538        $c_friend_list[$key]['nickname'] = $c_member['nickname'];
539        $c_friend_list[$key]['access_date'] = $c_member['access_date'];
540
541        $c_friend_list[$key]['friend_count'] = db_friend_count_friends($c_member['c_member_id']);
542    }
543
544    $total_num = db_friend_count_friends($c_member_id);
545
546    if ($total_num != 0) {
547        $total_page_num =  ceil($total_num / $page_size);
548        if ($page >= $total_page_num) {
549            $next = false;
550        } else {
551            $next = true;
552        }
553
554        if ($page <= 1) {
555            $prev = false;
556        } else {
557            $prev = true;
558        }
559    }
560    return array($c_friend_list, $prev, $next, $total_num);
561}
562
563/**
564 * あなたにフレンドリンクを要請しているメンバー数を取得(リンク承認待ち)
565 *
566 * @param int $c_member_id
567 * @return int 要請しているメンバー数
568 */
569function db_friend_count_c_anatani_friend_confirm($c_member_id)
570{
571    $sql = 'SELECT COUNT(*) FROM c_friend_confirm WHERE c_member_id_to = ?';
572    $params = array(intval($c_member_id));
573    return db_get_one($sql, $params);
574}
575
576/**
577 * あなたにフレンドリンクを要請しているメンバー(リンク承認待ち)
578 *
579 * @param   int $c_member_id_to : 要請されている方(あなた)
580 * @return  array_of_array
581 *              c_friend_confirm.*
582 *              nickname        : 要請している人の名前
583 *              message_disp    : 表示用メッセージ
584 */
585function db_friend_ktai_anatani_c_friend_confirm_list4c_member_id($c_member_id_to)
586{
587    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_to = ?";
588    $sql .= " ORDER BY r_datetime DESC";
589    $params = array(intval($c_member_id_to));
590    $c_friend_confirm_list = db_get_all($sql, $params);
591
592    foreach ($c_friend_confirm_list as $key => $value) {
593        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_from']);
594        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
595    }
596    return $c_friend_confirm_list;
597}
598
599function db_friend_ktai_anatani_c_friend_confirm_list4c_member_id_with_pager($c_member_id_to, $page_size, $page)
600{
601    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_to = ?";
602    $sql .= " ORDER BY r_datetime ASC";
603    $params = array(intval($c_member_id_to));
604    $c_friend_confirm_list = db_get_all_page($sql, $page, $page_size, $params);
605
606    foreach ($c_friend_confirm_list as $key => $value) {
607        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_from']);
608        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
609    }
610
611    $total_num = db_friend_count_c_anatani_friend_confirm($c_member_id_to);
612
613    if ($total_num != 0) {
614        $total_page_num = ceil($total_num / $page_size);
615        if ($page >= $total_page_num) {
616            $next = false;
617        } else {
618            $next = true;
619        }
620
621        if ($page <= 1) {
622            $prev = false;
623        } else {
624            $prev = true;
625        }
626    }
627    return array($c_friend_confirm_list, $prev, $next, $total_num);
628}
629
630/**
631 * あなたがフレンドリンクを要請しているメンバー数を取得(リンク承認待ち)
632 *
633 * @param int $c_member_id
634 * @return int 要請しているメンバー数
635 */
636function db_friend_count_c_anataga_friend_confirm($c_member_id)
637{
638    $sql = 'SELECT COUNT(*) FROM c_friend_confirm WHERE c_member_id_from = ?';
639    $params = array(intval($c_member_id));
640    return db_get_one($sql, $params);
641}
642
643/**
644 * あなたがフレンドリンクを要請しているメンバー(リンク要請中)
645 *
646 * @param   int $c_member_id_from   : 要請している方(あなた)
647 * @return  array_of_array
648 *              c_friend_confirm.*
649 *              nickname        : 要請されている人の名前
650 *              message_disp    : 表示用メッセージ
651 */
652function db_friend_ktai_anataga_c_friend_confirm_list4c_member_id($c_member_id_from)
653{
654    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_from = ?";
655    $sql .= " ORDER BY r_datetime DESC";
656    $params = array(intval($c_member_id_from));
657    $c_friend_confirm_list = db_get_all($sql, $params);
658
659    foreach ($c_friend_confirm_list as $key => $value) {
660        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_to']);
661        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
662    }
663    return $c_friend_confirm_list;
664}
665
666function db_friend_ktai_anataga_c_friend_confirm_list4c_member_id_with_pager($c_member_id_from, $page_size, $page)
667{
668    $sql = "SELECT * FROM c_friend_confirm WHERE c_member_id_from = ?";
669    $sql .= " ORDER BY r_datetime ASC";
670    $params = array(intval($c_member_id_from));
671    $c_friend_confirm_list = db_get_all_page($sql, $page, $page_size, $params);
672
673    foreach ($c_friend_confirm_list as $key => $value) {
674        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id_to']);
675        $c_friend_confirm_list[$key]['nickname'] = $c_member['nickname'];
676    }
677
678    $total_num = db_friend_count_c_anataga_friend_confirm($c_member_id_from);
679
680    if ($total_num != 0) {
681        $total_page_num = ceil($total_num / $page_size);
682        if ($page >= $total_page_num) {
683            $next = false;
684        } else {
685            $next = true;
686        }
687
688        if ($page <= 1) {
689            $prev = false;
690        } else {
691            $prev = true;
692        }
693    }
694    return array($c_friend_confirm_list, $prev, $next, $total_num);
695}
696
697/**
698 * 友達ランダム50人
699 */
700function db_friend_ktai_c_friend_list_random4c_member_id($c_member_id)
701{
702    $sql = "SELECT cf.c_member_id_to, cm.nickname";
703    $sql .= " FROM c_friend AS cf, c_member AS cm";
704    $sql .= " WHERE cf.c_member_id_from = ?".
705            " AND cf.c_member_id_to = cm.c_member_id";
706    $sql .= db_order_by_rand();
707    $params = array(intval($c_member_id));
708    return db_get_all_limit($sql, 0, 50, $params);
709}
710
711function db_friend_intro_list4c_friend_comment_list($c_friend_comment_list, $page, $page_size)
712{
713    $lst = array_slice($c_friend_comment_list, ($page - 1) * $page_size, $page_size);
714    $total_num = count($c_friend_comment_list);
715
716    if ($total_num === 0) {
717        $prev = $next = false;
718    } else {
719        $total_page_num =  ceil($total_num / $page_size);
720        $next = ($page < $total_page_num) ? true : false;
721        $prev = ($page > 1) ? true : false;
722    }
723    return array($lst, $prev, $next, $total_num);
724}
725
726/*** write ***/
727
728//--- c_friend
729
730/**
731 * フレンドリンクを追加
732 */
733function db_friend_insert_c_friend($c_member_id_from, $c_member_id_to)
734{
735    //function cache削除
736    cache_drop_c_member($c_member_id_from);
737    cache_drop_c_member($c_member_id_to);
738
739    if (($c_member_id_from < 1) || ($c_member_id_to < 1))
740        return false;
741
742    if (db_friend_is_friend($c_member_id_from, $c_member_id_to))
743        return false;
744
745    $data = array(
746        'c_member_id_from' => intval($c_member_id_from),
747        'c_member_id_to' => intval($c_member_id_to),
748        'r_datetime' => db_now(),
749        'intro' => '',
750    );
751    db_insert('c_friend', $data);
752
753    $data = array(
754        'c_member_id_from' => intval($c_member_id_to),
755        'c_member_id_to' => intval($c_member_id_from),
756        'r_datetime' => db_now(),
757        'intro' => '',
758    );
759    db_insert('c_friend', $data);
760}
761
762/**
763 * リンク申請からフレンドリンクを追加
764 *
765 * @param   int $c_friend_confirm_id
766 * @param   int $u  自分のc_member_id
767 */
768function db_friend_insert_c_friend4confirm($c_friend_confirm_id, $u)
769{
770    $confirm = db_friend_c_friend_confirm4c_friend_confirm_id($c_friend_confirm_id);
771    if ($confirm['c_member_id_to'] != $u) {
772        return false;
773    }
774    $c_member_id_from = $confirm['c_member_id_from'];
775    $c_member_id_to   = $confirm['c_member_id_to'];
776
777    // フレンドリンクを追加
778    db_friend_insert_c_friend($c_member_id_from, $c_member_id_to);
779
780    // フレンド申請を削除
781    db_friend_delete_c_friend_confirm($c_friend_confirm_id, $u);
782    return true;
783}
784
785/**
786 * フレンドリンクを削除
787 */
788function db_friend_delete_c_friend($c_member_id_from, $c_member_id_to)
789{
790    //function cache削除
791    cache_drop_c_member($c_member_id_from);
792    cache_drop_c_member($c_member_id_to);
793
794    $sql = 'DELETE FROM c_friend' .
795            ' WHERE (c_member_id_from = ? AND c_member_id_to = ?)' .
796               ' OR (c_member_id_to = ? AND c_member_id_from = ?)';
797    $params = array(
798        intval($c_member_id_from), intval($c_member_id_to),
799        intval($c_member_id_from), intval($c_member_id_to));
800    db_query($sql, $params);
801}
802
803/**
804 * フレンド紹介文入力・編集
805 */
806function db_friend_update_c_friend_intro($c_member_id_from, $c_member_id_to, $intro)
807{
808    //function cacheを削除
809    cache_drop_c_friend_intro($c_member_id_to);
810
811    // 紹介文中に書いてあるURLがSNS内でありセッションパラメータを含んでいた場合は削除
812    $intro = db_ktai_delete_url_session_parameter($intro);
813
814    $data = array(
815        'intro' => $intro,
816        'r_datetime_intro' => db_now(),
817    );
818    $where = array(
819        'c_member_id_from' => intval($c_member_id_from),
820        'c_member_id_to' => intval($c_member_id_to),
821    );
822    db_update('c_friend', $data, $where);
823}
824
825//--- c_friend_confirm
826
827/**
828 * フレンド申請を追加
829 *
830 * @param  int    $c_member_id_from
831 * @param  int    $c_member_id_to
832 * @param  string $message
833 * @return int insert_id
834 */
835function db_friend_insert_c_friend_confirm($c_member_id_from, $c_member_id_to, $message)
836{
837    // タイトルと本文中に書いてあるURLがSNS内でありセッションパラメータを含んでいた場合は削除
838    $message = db_ktai_delete_url_session_parameter($message);
839
840    $data = array(
841        'c_member_id_from' => intval($c_member_id_from),
842        'c_member_id_to'   => intval($c_member_id_to),
843        'message'          => $message,
844        'r_datetime'       => db_now(),
845    );
846    return db_insert('c_friend_confirm', $data);
847}
848
849/**
850 * フレンド申請を削除
851 *
852 * @param  int  $c_friend_confirm_id
853 * @param  int  $u 自分のc_member_id
854 */
855function db_friend_delete_c_friend_confirm($c_friend_confirm_id, $u)
856{
857    $sql = 'DELETE FROM c_friend_confirm WHERE c_friend_confirm_id = ?' .
858            ' AND (c_member_id_from = ? OR c_member_id_to = ?)';
859    $params = array(intval($c_friend_confirm_id), intval($u), intval($u));
860    db_query($sql, $params);
861}
862
863/**
864 * フレンド最新書き込みのホーム表示設定
865 *
866 * @param int $c_member_id_from (自分)
867 * @param int $c_member_id_to   (対象となるマイフレンド)
868 * @param int $is_display_friend_home (表示するか否か)
869 */
870function db_friend_update_is_display_friend_home($c_member_id_from, $c_member_id_to, $is_display_friend_home)
871{
872    pne_cache_drop('p_h_home_c_diary_friend_list4c_member_id', (int)$c_member_id_from, 5, true);
873    pne_cache_drop('p_h_home_c_diary_friend_list4c_member_id', (string)$c_member_id_from, 5, true);
874    $data = array(
875        'is_display_friend_home' => (bool)$is_display_friend_home,
876    );
877    $where = array(
878        'c_member_id_from'  => intval($c_member_id_from),
879        'c_member_id_to'    => intval($c_member_id_to),
880    );
881    return db_update('c_friend', $data, $where);
882}
883/**
884 * 友達のメンバーで、新着表示可否のIDリスト取得
885 *
886 * @param  int $c_member_id
887 * @param  bool $is_display_friend_home = 1:新着表示対象 / 0:新着表示対象外
888 * @return array  友達のメンバーID配列
889 */
890function db_friend_is_display_friend_home_list($c_member_id, $is_display_friend_home = 1)
891{
892    $sql  = 'SELECT c_member_id_to ';
893    $sql .= 'FROM c_friend ';
894    $sql .= 'WHERE c_member_id_from = ? ';
895    $sql .= 'AND is_display_friend_home = ? ';
896    $sql .= 'ORDER BY c_friend_id';
897    $params = array(intval($c_member_id), intval($is_display_friend_home));
898    $friends = db_get_col($sql, $params);
899
900    return $friends;
901}
902
903?>