require_once dirname(__FILE__) . '/helper.php';
$document =& JFactory::getDocument();
$document->addStyleSheet(JURI::base(true) . '/modules/mod_ninjaboard_random_posts/css/mod_ninjaboard_random_posts.css', 'text/css');
$document->addScript(JURI::base(true) . '/media/com_ninja/js/jquery/jquery.min.js', 'text/javascript');
$document->addScript(JURI::base(true) . '/media/com_ninja/js/jquery/jquery.tools.min.js', 'text/javascript');
//Load module parameters
$layout = $params->get('layout', 'default');
$height = str_replace('px', '', $params->get('height', 200));
$width = str_replace('px', '', $params->get('width', 200));
$num_cols = $params->get('num_cols', 1);
$num_posts = $params->get('num_posts', 1);
//safety measures for bad user input
//set $num_cols to at least one so we don't recieve div by zero error
if ($num_cols <= 0) {
    $num_cols = 1;
}
//set $num_posts to at least one so we don't return empty results
if ($num_posts <= 0) {
    $num_posts = 1;
}
//set $num_cols equal to $num_posts so we don't recieve results less than one
if ($num_cols > $num_posts) {
    $num_cols = $num_posts;
}
$module_id = $module->id;
//get list items
$items = modNinjaBoardRandomHelper::getNinjaBoardPosts($params);
//if no results return
//if (!count($items)) return;
//get template
require JModuleHelper::getLayoutPath('mod_ninjaboard_random_posts', $layout);
Example #2
0
 function getNinjaBoardPosts($params)
 {
     if (!file_exists(JPATH_SITE . '/components/com_ninjaboard/ninjaboard.php')) {
         return;
     }
     //connect to database
     $db =& JFactory::getDBO();
     //get current user
     $user =& JFactory::getUser();
     //load module parameters & set defaults
     $num_posts = $params->get('num_posts', 1);
     //if $num_posts isn't set or set to zero, then set it to one
     if ($num_posts <= 0) {
         $num_posts = 1;
     }
     /*        $show_public = $params->get('show_public', 1);
             $show_regualar = $params->get('show_regular', 0);
             $show_special = $params->get('show_special', 0);*/
     $only_forums = $params->get('only_forums', 0);
     $not_forums = $params->get('not_forums', 0);
     $child_forums = $params->get('child_forums', 1);
     $which_posts = $params->get('which_posts', 0);
     $order_by = $params->get('order_by', 0);
     $show_recent = $params->get('show_recent', '720');
     $show_unread = $params->get('show_unread', 1);
     $unread_image = $params->get('unread_image', 1);
     $subject_max = $params->get('subject_max', 50);
     $message_max = $params->get('message_max', 200);
     $name_link = $params->get('name_link', 1);
     $item_format = $params->get('item_format', '%R %S<br /><small>%N %D</small>');
     $tooltip_format = $params->get('tooltip_format', '%F > %L');
     $date_format = $params->get('date_format', 'j.n.Y G:i');
     $avatar_w_h = $params->get('avatar_w_h', 'height');
     $avatar_size = $params->get('avatar_size', '50');
     $name_link = $params->get('name_link', 'nb');
     $url_view = JRequest::getVar('view');
     $url_option = JRequest::getVar('option');
     //get current forum id
     if ($url_option == 'com_ninjaboard') {
         $url_id = JRequest::getInt('id', '');
     }
     //get menu id
     $itemId = modNinjaBoardRandomHelper::getItemId();
     //get com menu id
     $comItemid = modNinjaBoardRandomHelper::getComItemid($name_link);
     //how recent should shown posts be
     //create new post indicator
     //if user is logged in
     if ($user->id != 0) {
         //get last session from sessions table by user id
         $db->setQuery("SELECT time FROM #__session WHERE userid = {$user->id} ORDER BY time ASC LIMIT 1");
         $session = $db->loadResult();
         //if user has visited in last year
         if ($session > time() - 1314000) {
             $lasttime = $session;
             //limit to 1 year, if user hasn't visited in over a year
         } else {
             $lasttime = time() - 1314000;
         }
         //if user isn't logged or is guest
     } else {
         $lasttime = 0;
     }
     if ($lasttime > 0) {
         //logged in user
         $sql_new = ", (unix_timestamp(p.created_time) > " . $lasttime . ") AS show_unread";
     } else {
         //user isn't logged or is guest
         $sql_new = ", 1 AS show_unread";
     }
     //how recent should shown posts be
     $sql_time = '';
     if ($show_recent > 0) {
         $recent_time = time() - $show_recent * 3600;
         $sql_time = 'AND unix_timestamp(p.created_time) >= ' . $recent_time;
     }
     // access levels
     /*		$i = $show_public + 2 * $show_regualar + 4 * $show_special;
         switch ($i)
         {
             case 0:
                 $sql_access = '';
                 break;
             case 1:
                 $sql_access = 'AND u.gid in (0,1)';
                 break;
             case 2:
                 $sql_access = 'AND u.gid in (-1,18)';
                 break;
             case 3:
                 $sql_access = 'AND u.gid in (0,1,-1,18)';
                 break;
             case 4:
                 $sql_access = 'AND u.gid in (19,20,21)';
                 break;
             case 5:
                 $sql_access = 'AND u.gid in (0,1,19,20,21)';
                 break;
             case 6:
                 $sql_access = 'AND u.gid in (-1,18,19,20,21)';
                 break;
             default:
                 $sql_access = '';
         }*/
     //get posts in selected categories
     $sql_forum = '';
     if ($only_forums) {
         $forums = $only_forums;
         foreach ($forums as $i => $forum) {
             $forums[$i] = (int) $forum;
             if ($forum == 0) {
                 unset($forums[$i]);
             }
         }
         $only_forums = implode(',', $forums);
         if ($only_forums) {
             if ($forums) {
                 $sql_forum = " AND (f.ninjaboard_forum_id IN (" . $only_forums . ") OR f.parent_id IN (" . $only_forums . "))";
             } else {
                 $sql_forum = " AND f.ninjaboard_forum_id IN (" . $only_forums . ")";
             }
         }
     }
     //get posts not in selected categories
     if ($not_forums) {
         $forums = $not_forums;
         foreach ($forums as $i => $forum) {
             $forums[$i] = (int) $forum;
             if ($forum == 0) {
                 unset($forums[$i]);
             }
         }
         $not_forums = implode(',', $forums);
         if ($not_forums) {
             if ($forums) {
                 $sql_forum .= " AND (f.ninjaboard_forum_id NOT IN (" . $not_forums . ") AND f.parent_id NOT IN (" . $not_forums . "))";
             } else {
                 $sql_forum .= " AND f.ninjaboard_forum_id NOT IN (" . $not_forums . ")";
             }
         }
     }
     //ASC or DESC
     $sql_order_by = "DESC";
     if ($order_by == 1) {
         $sql_order_by = "ASC";
     }
     //post type
     $post_type = "Left Join #__ninjaboard_topics AS t ON p.ninjaboard_topic_id = t.ninjaboard_topic_id";
     if ($which_posts = 1) {
         $post_type = "Left Join #__ninjaboard_topics AS t ON p.ninjaboard_post_id = t.first_post_id";
     }
     if ($which_posts = 2) {
         $post_type = "Left Join #__ninjaboard_topics AS t ON p.ninjaboard_post_id = t.last_post_id";
     }
     switch ($name_link) {
         //get avatar from cbe profiler
         case 'cbe':
             $select = 'avt.`avatar` as custom_avatar, avt.`user_id`, ';
             $join = 'LEFT JOIN #__cbe avt ON p.`created_user_id`=avt.`user_id` ';
             $and = 'AND avt.`avatarapproved`=1 ';
             break;
             //get avatar from cb profiler
         //get avatar from cb profiler
         case 'cb':
             $select = 'avt.`avatar` as custom_avatar, avt.`user_id`, ';
             $join = 'LEFT JOIN #__comprofiler avt ON p.`created_user_id`=avt.`user_id` ';
             $and = 'AND avt.`avatarapproved`=1 ';
             break;
             //get avatar from jomsocial
         //get avatar from jomsocial
         case 'js':
             $select = 'avt.`avatar` as custom_avatar, avt.`userid`, ';
             $join = 'LEFT JOIN #__community_users avt ON p.`created_user_id`=avt.`userid` ';
             $and = '';
             break;
             //get avatar from ninjaboard
         //get avatar from ninjaboard
         case 'nb':
             $select = 'avt.`avatar` as custom_avatar, avt.`ninjaboard_person_id`, ';
             $join = 'LEFT JOIN #__ninjaboard_people avt ON p.`created_user_id`=avt.`ninjaboard_person_id` ';
             $and = '';
             break;
             //default
         //default
         default:
             $select = '';
             $join = '';
             $and = '';
             break;
     }
     $query = "\n        SELECT\n        " . $select . "\n\t\tu.username as author_name,  \n\t\tu.id,\n        p.locked,\n        p.`text` as message,\n        p.subject as subject,\n\t\tp.created_time as created,\n\t\tp.modified as modified,\n        p.ninjaboard_post_id as post_id,\n        p.ninjaboard_topic_id,\n        p.created_user_id as userid,\n        p.guest_name as name,\n\t\tt.hits as topic_hits,\n        t.ninjaboard_topic_id as topic_id,\n        t.forum_id,\n        f.title as forumname,\n        f.ninjaboard_forum_id,\n        f.enabled,\n        f.parent_id as parent_id\n        " . $sql_new . "\n        FROM \n        #__ninjaboard_posts AS p\n        " . $post_type . "\n        Left Join #__ninjaboard_forums AS f ON t.forum_id = f.ninjaboard_forum_id\n\t\tLeft Join #__users AS u ON p.created_user_id = u.id\n        " . $join . "\n        WHERE\n        p.enabled = '1' AND\n        f.enabled = '1'\n        " . $sql_time . "\t\t\n\t\t" . $sql_forum . "\n\t\t" . $and . "\n\t\tORDER BY RAND()\n        LIMIT 0," . $num_posts;
     $db->setQuery($query);
     $rows = $db->loadObjectList();
     //$countid = count($rows);
     if (!count($rows)) {
         return;
     }
     $items = array();
     foreach ($rows as $row) {
         $item = '';
         //lets build the avatar
         //set which avatar file retrieved from the db to use
         $avatar_file = $name_link == 'default' ? $row->avatar_file : $row->custom_avatar;
         //set $avatar_file for tmpl use
         $item->avatar_file = $avatar_file;
         //get paths and default files for avatars
         switch ($name_link) {
             //get avatar from cbe profiler
             case 'cbe':
                 //$avatar_file = (strpos($avatar_file, 'gallery/') === false) ? 'tn' . $avatar_file : $avatar_file;
                 if ($avatar_file) {
                     $avatar_path = JURI::base() . 'images/cbe/' . $avatar_file;
                 } else {
                     $avatar_path = JURI::base() . 'components/com_cbe/images/english/nophoto.png';
                 }
                 $user_link = JURI::base() . 'index.php?option=com_cbe&task=userProfile&user='******'<a href="' . $user_link . $row->userid . '&Itemid=' . $comItemid . '"><img name="' . $row->author_name . '" src="' . $avatar_path . '" ' . $avatar_w_h . '="' . str_replace('px', '', $avatar_size) . '" alt="" id="nb-posts-avatar" /></a>';
                 break;
                 //get avatar from cb profiler
             //get avatar from cb profiler
             case 'cb':
                 //$avatar_file = (strpos($avatar_file, 'gallery/') === false) ? 'tn' . $avatar_file : $avatar_file;
                 if ($avatar_file) {
                     $avatar_path = JURI::base() . 'images/comprofiler/' . $avatar_file;
                 } else {
                     $avatar_path = JURI::base() . 'components/com_comprofiler/plugin/templates/luna/images/avatar/tnnophoto_n.png';
                 }
                 $user_link = JURI::base() . 'index.php?option=com_comprofiler&task=userProfile&user='******'<a href="' . $user_link . $row->userid . '&Itemid=' . $comItemid . '"><img name="' . $row->author_name . '" src="' . $avatar_path . '" ' . $avatar_w_h . '="' . str_replace('px', '', $avatar_size) . '" alt="" id="nb-posts-avatar" /></a>';
                 break;
                 //get avatar from jomsocial
             //get avatar from jomsocial
             case 'js':
                 if ($avatar_file) {
                     $avatar_path = JURI::base() . $avatar_file;
                 } else {
                     $avatar_path = JURI::base() . 'components/com_community/assets/default_thumb.jpg';
                 }
                 $user_link = JURI::base() . 'index.php?option=com_community&view=profile&userid=';
                 $item->avatar = '<a href="' . $user_link . $row->userid . '&Itemid=' . $comItemid . '"><img name="' . $row->author_name . '" src="' . $avatar_path . '" ' . $avatar_w_h . '="' . str_replace('px', '', $avatar_size) . '" alt="" id="nb-posts-avatar" /></a>';
                 break;
                 //get avatar from ninjaboard
             //get avatar from ninjaboard
             case 'nb':
             case 'default':
                 if ($avatar_file) {
                     $avatar_path = JURI::base() . $avatar_file;
                 } else {
                     $avatar_path = JURI::base() . 'media/com_ninjaboard/images/avatar.png';
                 }
                 $user_link = JURI::base() . 'index.php?option=com_ninjaboard&view=person&id=' . $row->userid . '&Itemid=' . $comItemid;
                 $item->avatar = '<a href="' . $user_link . '"><img name="' . $row->name . '" src="' . $avatar_path . '" ' . $avatar_w_h . '="' . str_replace('px', '', $avatar_size) . '" alt="" id="nb-posts-avatar" /></a>';
                 break;
         }
         $item->subject = stripslashes($row->subject) ? stripslashes($row->subject) : '';
         $item->subject_max = JString::strlen($item->subject) > $subject_max ? JString::substr($item->subject, 0, $subject_max - 4) . '...' : $item->subject;
         $row->message = modNinjaBoardRandomHelper::parseBBcode($row->message);
         $item->message = stripslashes($row->message) ? stripslashes($row->message) : '';
         $item->message_max = JString::strlen($item->message) > $message_max ? JString::substr($item->message, 0, $message_max - 4) . '...' : $item->message;
         $item->postname = $row->userid > 0 ? "<a href='" . $user_link . "' title='" . $row->author_name . "'>" . $row->author_name . "</a>" : JText::_('GUEST');
         $item->name = $row->userid > 0 ? $row->author_name : JText::_('GUEST');
         $item->postlink_s = "<a href='" . JURI::base() . 'index.php?option=com_ninjaboard&view=topic&id=' . $row->topic_id . '&Itemid=' . $itemId . '#post' . $row->post_id . "' title='" . $item->subject_max . "'>" . $item->subject_max . "</a>";
         $item->postlink_l = "<a href='" . JURI::base() . 'index.php?option=com_ninjaboard&view=topic&id=' . $row->topic_id . '&Itemid=' . $itemId . '#post' . $row->post_id . "' title='" . $item->subject . "'>" . $item->subject . "</a>";
         $item->datetime = date($date_format, strtotime($row->created));
         $item->unreadimage = '<img src="' . JURI::base() . 'modules/mod_ninjaboard_related_posts/images/new.png" alt="new" />';
         $item->online_status = modNinjaBoardRandomHelper::getOnlineStatus($row->userid) ? '<span id="nb-posts-online">' . JText::_('ONLINE') . '</span>' : '<span id="nb-posts-offline">' . JText::_('OFFLINE') . '</span>';
         $s = $item_format;
         $s = str_replace('%S', $item->postlink_s, $s);
         $s = str_replace('%L', $item->postlink_l, $s);
         $s = str_replace('%M', $item->message_max, $s);
         $s = str_replace('%T', $item->message, $s);
         $s = str_replace('%N', $item->postname, $s);
         $s = str_replace('%D', $item->datetime, $s);
         $s = str_replace('%H', $row->topic_hits, $s);
         $s = str_replace('%F', $row->forumname, $s);
         $s = str_replace('%I', $item->unreadimage, $s);
         $s = strpos($s, '%A') === false ? $s : str_replace('%A', $item->avatar, $s);
         $s = strpos($s, '%O') === false ? $s : str_replace('%O', $item->online_status, $s);
         $s = str_replace('title=""', '', $s);
         $item->title = $s;
         $s = $tooltip_format;
         $s = str_replace('%S', $item->subject_max, $s);
         $s = str_replace('%L', $item->subject, $s);
         $s = str_replace('%M', $item->message_max, $s);
         $s = str_replace('%T', $item->message, $s);
         $s = str_replace('%N', $item->name, $s);
         $s = str_replace('%D', date($date_format, strtotime($row->created)), $s);
         $s = str_replace('%H', $row->topic_hits, $s);
         $s = str_replace('%F', $row->forumname, $s);
         $s = str_replace('%I', $item->unreadimage, $s);
         $s = strpos($s, '%O') === false ? $s : str_replace('%O', $item->online_status, $s);
         $item->tooltip = $s;
         $items[] = $item;
     }
     return $items;
 }