| 1 | <?php |
|---|
| 2 | /******************************************************************************/ |
|---|
| 3 | // // |
|---|
| 4 | // InstantCMS v1.8 // |
|---|
| 5 | // http://www.instantcms.ru/ // |
|---|
| 6 | // // |
|---|
| 7 | // written by InstantCMS Team, 2007-2010 // |
|---|
| 8 | // produced by InstantSoft, (www.instantsoft.ru) // |
|---|
| 9 | // // |
|---|
| 10 | // LICENSED BY GNU/GPL v2 // |
|---|
| 11 | // // |
|---|
| 12 | /******************************************************************************/ |
|---|
| 13 | |
|---|
| 14 | if(!defined('VALID_CMS')) { die('ACCESS DENIED'); } |
|---|
| 15 | |
|---|
| 16 | if (!defined('USER_UPDATER')) { define('USER_UPDATER', -1); } |
|---|
| 17 | if (!defined('USER_MASSMAIL')) { define('USER_MASSMAIL', -2); } |
|---|
| 18 | |
|---|
| 19 | function setClubsRating($club_id){ |
|---|
| 20 | |
|---|
| 21 | $inDB = cmsDatabase::getInstance(); |
|---|
| 22 | |
|---|
| 23 | if (!is_int($club_id)){ return; } |
|---|
| 24 | |
|---|
| 25 | $sql = "SELECT SUM( u.rating ) AS rating |
|---|
| 26 | FROM cms_user_clubs c |
|---|
| 27 | LEFT JOIN cms_users u ON u.id = c.user_id |
|---|
| 28 | WHERE c.club_id = '$club_id'"; |
|---|
| 29 | |
|---|
| 30 | $rs = $inDB->query($sql); |
|---|
| 31 | |
|---|
| 32 | if ($inDB->num_rows($rs)){ |
|---|
| 33 | $data = $inDB->fetch_assoc($rs); |
|---|
| 34 | $rating = $data['rating'] * 5; |
|---|
| 35 | } else { |
|---|
| 36 | $rating = 0; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | $sql = "UPDATE cms_clubs SET rating = '$rating' WHERE id = '$club_id'"; |
|---|
| 40 | $inDB->query($sql); |
|---|
| 41 | |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | function setUsersRating(){ |
|---|
| 45 | |
|---|
| 46 | $inCore = cmsCore::getInstance(); |
|---|
| 47 | $inDB = cmsDatabase::getInstance(); |
|---|
| 48 | |
|---|
| 49 | $target = $inCore->request('target', 'str', ''); |
|---|
| 50 | $item_id = $inCore->request('item_id', 'int', 0); |
|---|
| 51 | $opt = $inCore->request('opt', 'str', 'plus'); |
|---|
| 52 | |
|---|
| 53 | $comment_id = $inCore->request('comment_id', 'int', 0); |
|---|
| 54 | $comment_vote = $inCore->request('vote', 'int', 1); |
|---|
| 55 | |
|---|
| 56 | if ($comment_id) { $target = 'comment'; $item_id = $comment_id; } |
|---|
| 57 | |
|---|
| 58 | $table = ''; |
|---|
| 59 | |
|---|
| 60 | switch($target){ |
|---|
| 61 | case 'blogpost': $table = 'cms_blog_posts'; break; |
|---|
| 62 | case 'content': $table = 'cms_content'; break; |
|---|
| 63 | case 'comment': $table = 'cms_comments'; break; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | if (!$table) { return false; } |
|---|
| 67 | |
|---|
| 68 | $author_sql = "SELECT u.id as id |
|---|
| 69 | FROM cms_users u, {$table} t |
|---|
| 70 | WHERE t.id = {$item_id} AND t.user_id = u.id |
|---|
| 71 | LIMIT 1"; |
|---|
| 72 | |
|---|
| 73 | $author_res = $inDB->query($author_sql); |
|---|
| 74 | |
|---|
| 75 | if (!$inDB->num_rows($author_res)) { return false; } |
|---|
| 76 | |
|---|
| 77 | $author = $inDB->fetch_assoc($author_res); |
|---|
| 78 | |
|---|
| 79 | if ($comment_id){ |
|---|
| 80 | $inc = ($comment_vote>0 ? ('+'.$comment_vote*2) : ($comment_vote*2)); |
|---|
| 81 | } else { |
|---|
| 82 | $inc = ($opt=='plus' ? '+ 5' : '- 5'); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | $inDB->query("UPDATE cms_users SET rating = rating {$inc} WHERE id = {$author['id']}"); |
|---|
| 86 | |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | function cmsClearKarma($target, $item_id){ |
|---|
| 90 | $inDB = cmsDatabase::getInstance(); |
|---|
| 91 | $inDB->query("DELETE FROM cms_ratings WHERE target='$target' AND item_id = $item_id"); |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | function cmsKarma($target, $item_id){ //returns array with total votes and total points of karma |
|---|
| 96 | |
|---|
| 97 | if (!preg_match('/^([a-zA-Z0-9\_]+)$/i', $target)) { return; } |
|---|
| 98 | |
|---|
| 99 | $item_id = intval($item_id); |
|---|
| 100 | |
|---|
| 101 | $inDB = cmsDatabase::getInstance(); |
|---|
| 102 | |
|---|
| 103 | $item = $inDB->get_fields('cms_ratings_total', "item_id = '$item_id' AND target='$target'", 'total_rating, total_votes'); |
|---|
| 104 | |
|---|
| 105 | if (!$item){ return array('points'=>0, 'votes'=>0); } |
|---|
| 106 | |
|---|
| 107 | return array('points'=>$item['total_rating'], 'votes'=>$item['total_votes']); |
|---|
| 108 | |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | function cmsAlreadyKarmed($target, $item_id, $user_id){ |
|---|
| 112 | $inDB = cmsDatabase::getInstance(); |
|---|
| 113 | return $inDB->rows_count('cms_ratings', "target='$target' AND item_id = $item_id AND user_id = '$user_id'"); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | function cmsAlreadyKarmedIP($target, $item_id, $ip){ |
|---|
| 117 | $inDB = cmsDatabase::getInstance(); |
|---|
| 118 | return $inDB->rows_count('cms_ratings', "target='$target' AND item_id = $item_id AND ip = '$ip'"); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | function cmsAlreadyKarmedAny($target, $user_id){ |
|---|
| 122 | $inDB = cmsDatabase::getInstance(); |
|---|
| 123 | return $inDB->rows_count('cms_ratings', "target='$target' AND user_id = '$user_id'"); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | function cmsAlreadyKarmedAnyIP($target, $ip){ |
|---|
| 127 | $inDB = cmsDatabase::getInstance(); |
|---|
| 128 | return $inDB->rows_count('cms_ratings', "target='$target' AND ip = '$ip'"); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | function cmsSubmitKarma($target, $item_id, $points){ |
|---|
| 132 | |
|---|
| 133 | $inUser = cmsUser::getInstance(); |
|---|
| 134 | $inDB = cmsDatabase::getInstance(); |
|---|
| 135 | $id = $inUser->id; |
|---|
| 136 | $ip = $_SERVER['REMOTE_ADDR']; |
|---|
| 137 | |
|---|
| 138 | if(cmsAlreadyKarmed($target, $item_id, $id)){ return false; } |
|---|
| 139 | |
|---|
| 140 | //âñòàâëÿåì íîâûé ãîëîñ |
|---|
| 141 | $sql = "INSERT INTO cms_ratings (item_id, points, ip, target, user_id, pubdate) |
|---|
| 142 | VALUES ('$item_id', '$points', '$ip', '$target', '$id', NOW())"; |
|---|
| 143 | $inDB->query($sql); |
|---|
| 144 | |
|---|
| 145 | //ïðîâåðÿåì áûëà ëè ñäåëàíà àãðåãàöèÿ äëÿ ýòîé öåëè ðàíåå |
|---|
| 146 | $is_agr = $inDB->rows_count('cms_ratings_total', "target='$target' AND item_id = '$item_id'", 1); |
|---|
| 147 | |
|---|
| 148 | //åñëè áûëà, òî îáíîâëÿåì |
|---|
| 149 | if ($is_agr) { $agr_sql = "UPDATE cms_ratings_total |
|---|
| 150 | SET total_rating = total_rating + ({$points}), |
|---|
| 151 | total_votes = total_votes + 1 |
|---|
| 152 | WHERE target='$target' AND item_id = '$item_id'"; } |
|---|
| 153 | |
|---|
| 154 | //åñëè íå áûëî, òî âñòàâëÿåì |
|---|
| 155 | if (!$is_agr) { $agr_sql = "INSERT INTO cms_ratings_total (target, item_id, total_rating, total_votes) |
|---|
| 156 | VALUES ('{$target}', '{$item_id}', '{$points}', '1')"; } |
|---|
| 157 | |
|---|
| 158 | $inDB->query($agr_sql); |
|---|
| 159 | |
|---|
| 160 | //ïîëó÷àåì èíôîðìàöèþ î öåëè |
|---|
| 161 | $info = $inDB->get_fields('cms_rating_targets', "target='{$target}'", '*'); |
|---|
| 162 | |
|---|
| 163 | //åñëè íóæíî, èçìåíÿåì ðåéòèíã àâòîðà öåëè |
|---|
| 164 | if ($info['is_user_affect'] && $info['user_weight'] && $info['target_table']){ |
|---|
| 165 | |
|---|
| 166 | $user_sql = "UPDATE cms_users u, |
|---|
| 167 | {$info['target_table']} t |
|---|
| 168 | SET u.rating = u.rating + ({$points}*{$info['user_weight']}) |
|---|
| 169 | WHERE t.user_id = u.id AND t.id = '$item_id'"; |
|---|
| 170 | |
|---|
| 171 | $inDB->query($user_sql); |
|---|
| 172 | |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | //ïðîâåðÿåì íàëè÷èå ìåòîäà updateRatingHook(target, item_id, points) â ìîäåëè |
|---|
| 176 | //êîìïîíåíòà, îòâåòñòâåííîãî çà öåëü |
|---|
| 177 | if ($info['component']){ |
|---|
| 178 | $inCore = cmsCore::getInstance(); |
|---|
| 179 | $inCore->loadModel($info['component']); |
|---|
| 180 | if (class_exists('cms_model_'.$info['component'])){ |
|---|
| 181 | $model_class = 'cms_model_'.$info['component']; |
|---|
| 182 | $model = new $model_class(); |
|---|
| 183 | if (method_exists($model, 'updateRatingHook')){ |
|---|
| 184 | $model->updateRatingHook($target, $item_id, $points); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | return true; |
|---|
| 190 | |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | function cmsKarmaFormat($points){ |
|---|
| 194 | if ($points==0) { |
|---|
| 195 | $html = '<span style="color:silver;">0</span>'; |
|---|
| 196 | } elseif ($points>0){ |
|---|
| 197 | $html = '<span style="color:green;">+'.$points.'<span style="color:silver">↑</span></span>'; |
|---|
| 198 | } else { |
|---|
| 199 | $html = '<span style="color:red;">'.$points.'<span style="color:silver">↓</span></span>'; |
|---|
| 200 | } |
|---|
| 201 | return $html; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | function cmsKarmaFormatSmall($points){ |
|---|
| 205 | if ($points==0) { |
|---|
| 206 | $html = '<span style="color:gray;">0</span>'; |
|---|
| 207 | } elseif ($points>0){ |
|---|
| 208 | $html = '<span style="color:green">+'.$points.'</span>'; |
|---|
| 209 | } else { |
|---|
| 210 | $html = '<span style="color:red">'.$points.'</span>'; |
|---|
| 211 | } |
|---|
| 212 | return $html; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | function cmsKarmaForm($target, $target_id, $points = 0, $is_author = false){ |
|---|
| 216 | |
|---|
| 217 | $inUser = cmsUser::getInstance(); |
|---|
| 218 | $inPage = cmsPage::getInstance(); |
|---|
| 219 | $html = ''; |
|---|
| 220 | |
|---|
| 221 | global $_LANG; |
|---|
| 222 | |
|---|
| 223 | if (!$points) { |
|---|
| 224 | $postkarma = cmsKarma($target, $target_id); |
|---|
| 225 | $points = cmsKarmaFormat($postkarma['points']); |
|---|
| 226 | } else { |
|---|
| 227 | $points = $points; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | $control = ''; |
|---|
| 231 | |
|---|
| 232 | //PREPARE RATING FORM |
|---|
| 233 | if ($inUser->id && !$is_author){ |
|---|
| 234 | if(!cmsAlreadyKarmed($target, $target_id, $inUser->id)){ |
|---|
| 235 | $inPage->addHeadJS('core/js/karma.js'); |
|---|
| 236 | $control .= '<div style="text-align:center;margin-top:10px;">'; |
|---|
| 237 | $control .= '<a href="javascript:void(0);" onclick="plusKarma(\''.$target.'\', \''.$target_id.'\')" title="'.$_LANG['LIKE'].'"><img src="/components/users/images/karma_up.png" border="0" alt="Êàðìà+"/></a> '; |
|---|
| 238 | $control .= '<a href="javascript:void(0);" onclick="minusKarma(\''.$target.'\', \''.$target_id.'\')" title="'.$_LANG['UNLIKE'].'"><img src="/components/users/images/karma_down.png" border="0" alt="Êàðìà-"/></a>'; |
|---|
| 239 | $control .= '</div>'; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | $html .= '<div class="karma_form">'; |
|---|
| 243 | $html .= '<div id="karmapoints" style="font-size:24px">'.$points.'</div>'; |
|---|
| 244 | $html .= '<div id="karmavotes">Ãîëîñîâ: '.$postkarma['votes'].'</div>'; |
|---|
| 245 | $html .= '<div id="karmactrl">'.$control.'</div>'; |
|---|
| 246 | $html .= '</div>'; |
|---|
| 247 | return $html; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | function cmsKarmaButtons($target, $target_id, $points = 0, $is_author = false){ |
|---|
| 251 | |
|---|
| 252 | $inUser = cmsUser::getInstance(); |
|---|
| 253 | $inPage = cmsPage::getInstance(); |
|---|
| 254 | $html = ''; |
|---|
| 255 | $control = ''; |
|---|
| 256 | global $_LANG; |
|---|
| 257 | |
|---|
| 258 | if (!$points) { |
|---|
| 259 | $postkarma = cmsKarma($target, $target_id); |
|---|
| 260 | $points = cmsKarmaFormat($postkarma['points']); |
|---|
| 261 | } else { |
|---|
| 262 | $points = $points; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | //PREPARE RATING FORM |
|---|
| 266 | if ($inUser->id && !$is_author){ |
|---|
| 267 | if(!cmsAlreadyKarmed($target, $target_id, $inUser->id)){ |
|---|
| 268 | $inPage->addHeadJS('core/js/karma.js'); |
|---|
| 269 | |
|---|
| 270 | $control .= '<div style="text-align:center">'; |
|---|
| 271 | $control .= '<a href="javascript:void(0);" onclick="plusKarma(\''.$target.'\', '.$target_id.');" title="'.$_LANG['LIKE'].'"><img src="/components/users/images/karma_up.png" border="0" alt="Êàðìà+"/></a> '; |
|---|
| 272 | $control .= '<a href="javascript:void(0);" onclick="minusKarma(\''.$target.'\', '.$target_id.');" title="'.$_LANG['UNLIKE'].'"><img src="/components/users/images/karma_down.png" border="0" alt="Êàðìà-"/></a>'; |
|---|
| 273 | $control .= '</div>'; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | if ($control){ |
|---|
| 278 | $html .= '<div class="karma_buttons">'; |
|---|
| 279 | $html .= '<div id="karmactrl">'.$control.'</div>'; |
|---|
| 280 | $html .= '</div>'; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | return $html; |
|---|
| 284 | |
|---|
| 285 | } |
|---|
| 286 | function cmsKarmaButtonsText($target, $target_id, $points = 0, $is_author = false){ |
|---|
| 287 | |
|---|
| 288 | $inUser = cmsUser::getInstance(); |
|---|
| 289 | $inPage = cmsPage::getInstance(); |
|---|
| 290 | $html = ''; |
|---|
| 291 | |
|---|
| 292 | if (!$points) { |
|---|
| 293 | $postkarma = cmsKarma($target, $target_id); |
|---|
| 294 | $points = cmsKarmaFormat($postkarma['points']); |
|---|
| 295 | } else { |
|---|
| 296 | $points = $points; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | $control = ''; |
|---|
| 300 | //PREPARE RATING FORM |
|---|
| 301 | if ($inUser->id && !$is_author){ |
|---|
| 302 | if(!cmsAlreadyKarmed($target, $target_id, $inUser->id)){ |
|---|
| 303 | $inPage->addHeadJS('core/js/karma.js'); |
|---|
| 304 | $control .= '<span>'; |
|---|
| 305 | $control .= '<a href="javascript:void(0);" onclick="plusKarma(\''.$target.'\', '.$target_id.');" style="color:green">Íðàâèòñÿ</a> ↑ '; |
|---|
| 306 | $control .= '<a href="javascript:void(0);" onclick="minusKarma(\''.$target.'\', '.$target_id.');" style="color:red">Íå íðàâèòñÿ</a> ↓'; |
|---|
| 307 | $control .= '</span>'; |
|---|
| 308 | $html .= '<span class="karma_buttons">'; |
|---|
| 309 | $html .= '<span id="karmactrl">'.$control.'</span>'; |
|---|
| 310 | $html .= '</span>'; |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | return $html; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | ?> |
|---|