PageRenderTime 204ms CodeModel.GetById 141ms app.highlight 11ms RepoModel.GetById 49ms app.codeStats 1ms

/webapp/lib/db/bookmark.php

https://github.com/openpne/OpenPNE2
PHP | 229 lines | 163 code | 32 blank | 34 comment | 16 complexity | ab18ca131e635eab2faa710fc3ec9308 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
  7function db_bookmark_list($c_member_id, $page, $page_size)
  8{
  9    $sql = 'SELECT c_member_id, nickname, image_filename, access_date' .
 10            ' FROM c_bookmark INNER JOIN c_member' .
 11            ' ON c_bookmark.c_member_id_to = c_member.c_member_id' .
 12            ' WHERE c_bookmark.c_member_id_from = ?' .
 13            ' ORDER BY c_bookmark.r_datetime DESC';
 14    $params = array(intval($c_member_id));
 15    $lst = db_get_all_page($sql, $page, $page_size, $params);
 16
 17    foreach ($lst as $key => $value) {
 18        $lst[$key]['profile'] = db_member_c_member_profile_list4c_member_id($value['c_member_id']);
 19        $lst[$key]['last_login'] = p_f_home_last_login4access_date($value['access_date']);
 20    }
 21
 22    $sql = 'SELECT COUNT(*) FROM c_bookmark INNER JOIN c_member' .
 23            ' ON c_bookmark.c_member_id_to = c_member.c_member_id' .
 24            ' WHERE c_bookmark.c_member_id_from = ?';
 25    $total_num = db_get_one($sql, $params);
 26
 27    if ($total_num === 0) {
 28        $prev = $next = false;
 29    } else {
 30        $total_page_num =  ceil($total_num / $page_size);
 31        $next = ($page < $total_page_num) ? true : false;
 32        $prev = ($page > 1) ? true : false;
 33    }
 34
 35    return array($lst, $prev, $next, $total_num);
 36}
 37
 38function db_bookmark_is_bookmark($c_member_id_from, $c_member_id_to)
 39{
 40    $sql = 'SELECT c_bookmark_id FROM c_bookmark' .
 41            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
 42    $params = array(intval($c_member_id_from), intval($c_member_id_to));
 43    return (bool)db_get_one($sql, $params);
 44}
 45
 46/**
 47 * お気に入りのメンバーIDリスト取得
 48 *
 49 * @param  int $c_member_id
 50 * @param  bool $block  アクセスブロックしているメンバーを除外するかどうか
 51 * @return array  お気に入りのメンバーID配列
 52 */
 53function db_bookmark_c_member_id_list($c_member_id_from, $block = false)
 54{
 55    $sql = 'SELECT c_member_id_to FROM c_bookmark WHERE c_member_id_from = ? ORDER BY c_bookmark_id';
 56    $params = array(intval($c_member_id_from));
 57    $list = db_get_col($sql, $params);
 58    if ($block) {
 59        $block_list = db_member_access_block_list4c_member_id_to($c_member_id_from);
 60        $list = array_diff($list, $block_list);
 61    }
 62    return $list;
 63}
 64
 65/**
 66 * お気に入りメンバーの日記リスト
 67 */
 68function db_bookmark_diary_list($c_member_id, $limit)
 69{
 70    $bookmarks = db_bookmark_c_member_id_list($c_member_id, true);
 71    if (!$bookmarks) {
 72        return array();
 73    }
 74    $ids = implode(',', array_map('intval', $bookmarks));
 75
 76    $sql = 'SELECT c_diary.* FROM c_diary,c_member WHERE c_diary.c_member_id IN ('.$ids.') AND c_diary.public_flag = \'public\' AND c_diary.c_member_id=c_member.c_member_id ORDER BY r_datetime DESC';
 77
 78    $diary_list = db_get_all_limit($sql, 0, intval($limit));
 79
 80    foreach ($diary_list as $key => $value) {
 81        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
 82        $diary_list[$key]['nickname'] = $c_member['nickname'];
 83        $diary_list[$key]['count_comments'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
 84    }
 85    return $diary_list;
 86}
 87
 88/**
 89 * お気に入りメンバーのBlogリスト
 90 */
 91function db_bookmark_blog_list($c_member_id, $limit)
 92{
 93    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ
 94
 95    if (!$is_recurred) {  //function cacheのために再帰処理を行う
 96        $is_recurred = true;
 97        $funcargs = func_get_args();
 98        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_FAST, __FUNCTION__, $funcargs);
 99        $is_recurred = false;
100        return $result;
101    }
102
103    $bookmarks = db_bookmark_c_member_id_list($c_member_id, true);
104    if (!$bookmarks) {
105        return array();
106    }
107    $ids = implode(',', array_map('intval', $bookmarks));
108
109    $sql = 'SELECT * FROM c_rss_cache WHERE c_member_id IN (' . $ids . ') ORDER BY r_datetime DESC';
110    $blog_list = db_get_all_limit($sql, 0, intval($limit));
111
112    foreach ($blog_list as $key => $value) {
113        $c_member = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
114        $blog_list[$key]['nickname'] = $c_member['nickname'];
115    }
116    return $blog_list;
117}
118
119/**
120 * お気に入りメンバーの日記リスト(ページャ付き)
121 */
122function db_bookmark_diary_list_with_pager($c_member_id, $page_size, $page)
123{
124    $bookmarks = db_bookmark_c_member_id_list($c_member_id, true);
125    if (!$bookmarks) {
126        return array(array(), false, false, 0);
127    }
128    $ids = implode(',', array_map('intval', $bookmarks));
129
130    $sql = 'SELECT c_diary.* FROM c_diary, c_member WHERE c_diary.c_member_id IN ('.$ids.') AND c_diary.public_flag = \'public\' AND c_diary.c_member_id=c_member.c_member_id ORDER BY r_datetime DESC';
131
132    $diary_list = db_get_all_page($sql, intval($page), intval($page_size));
133    foreach ($diary_list as $key => $value) {
134        $diary_list[$key]['c_member'] = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
135        $diary_list[$key]['count_comments'] = db_diary_count_c_diary_comment4c_diary_id($value['c_diary_id']);
136    }
137
138    $sql = 'SELECT count(*) FROM c_diary WHERE c_member_id IN (' . $ids . ')';
139    $total_num = db_get_one($sql);
140
141    if ($total_num != 0) {
142        $total_page_num =  ceil($total_num / $page_size);
143        if ($page >= $total_page_num) {
144            $next = false;
145        } else {
146            $next = true;
147        }
148        if ($page <= 1) {
149            $prev = false;
150        } else {
151            $prev = true;
152        }
153    }
154    return array($diary_list, $prev, $next, $total_num);
155}
156
157/**
158 * お気に入りメンバーリスト
159 */
160function db_bookmark_member_list($c_member_id, $limit = 0)
161{
162    static $is_recurred = false;  //再帰処理中かどうかの判定フラグ
163
164    if (!$is_recurred) {  //function cacheのために再帰処理を行う
165        $is_recurred = true;
166        $funcargs = func_get_args();
167        $result = pne_cache_recursive_call(OPENPNE_FUNCTION_CACHE_LIFETIME_LONG, __FUNCTION__, $funcargs);
168        $is_recurred = false;
169        return $result;
170    }
171
172    $sql = 'SELECT c_member_id_to AS c_member_id FROM c_bookmark' .
173            ' WHERE c_member_id_from = ?' . db_order_by_rand();
174    $params = array(intval($c_member_id));
175
176    if ($limit) {
177        $result = db_get_all_limit($sql, 0, intval($limit), $params);
178    } else {
179        $result = db_get_all($sql, $params);
180    }
181
182    foreach ($result as $key => $value) {
183        $result[$key] = db_member_c_member4c_member_id_LIGHT($value['c_member_id']);
184        $result[$key]['friend_count'] = db_friend_count_friends($value['c_member_id']);
185    }
186    return $result;
187}
188
189/**
190 * お気に入りのメンバー数
191 */
192function db_bookmark_count($c_member_id)
193{
194    $sql = 'SELECT COUNT(*) FROM c_bookmark WHERE c_member_id_from = ?';
195    $params = array(intval($c_member_id));
196    return db_get_one($sql, $params);
197}
198
199/**
200 * お気に入り追加
201 */
202function db_bookmark_insert_c_bookmark($c_member_id_from, $c_member_id_to)
203{
204    //function cacheの削除
205    cache_drop_c_bookmark($c_member_id_from);
206
207    $data = array(
208        'c_member_id_from' => intval($c_member_id_from),
209        'c_member_id_to' => intval($c_member_id_to),
210        'r_datetime' => db_now(),
211    );
212    return db_insert('c_bookmark', $data);
213}
214
215/**
216 * お気に入り削除
217 */
218function db_bookmark_delete_c_bookmark($c_member_id_from, $c_member_id_to)
219{
220    //function cacheの削除
221    cache_drop_c_bookmark($c_member_id_from);
222
223    $sql = 'DELETE FROM c_bookmark' .
224            ' WHERE c_member_id_from = ? AND c_member_id_to = ?';
225    $params = array(intval($c_member_id_from), intval($c_member_id_to));
226    db_query($sql, $params);
227}
228
229?>