public static function getLastComments($params) { try { $user = Lms_User::getUser(); $db = Lms_Db::get('main'); $wheres = array(); $wheres[] = "(ISNULL(ToUserID) OR ToUserID=0 {OR ToUserID=?d OR UserID=?d})"; if (!$user->isAllowed("film", "moderate")) { $wheres[] = " films.Hide=0 "; } $sql = "SELECT FilmID as film_id, films.Name as name, max(comments.ID) as comment_id " . "FROM comments LEFT JOIN films ON (films.ID = comments.FilmID) " . "WHERE " . implode(' AND ', $wheres) . " " . "GROUP BY comments.FilmID ORDER BY comment_id DESC LIMIT 0,20"; $films = $db->select($sql, $user->getId() ? $user->getId() : DBSIMPLE_SKIP, $user->getId() ? $user->getId() : DBSIMPLE_SKIP); if (count($films)) { $maxlength = 80; $commentsIds = array(); foreach ($films as $row) { $commentsIds[] = $row['comment_id']; } $sql = 'SELECT comments.ID AS ARRAY_KEY, users.Login as user_name, `Date` as `added_at`, `Text` as `text` ' . 'FROM comments LEFT JOIN users ON (users.ID = comments.UserID) ' . 'WHERE comments.ID IN(?a)'; $comments = $db->select($sql, $commentsIds); foreach ($films as &$row) { $commentId = $row['comment_id']; $comment = $comments[$commentId]; $row['text'] = Lms_Text::tinyString($comment['text'], 500, 1); $row['user_name'] = $comment["user_name"]; $row["added_at"] = $comment["added_at"]; } } $result['films'] = $films; return new Lms_Api_Response(200, null, $result); } catch (Exception $e) { return new Lms_Api_Response(500, $e->getMessage()); } }