1 (edited by XuMiX 2004-12-15 09:46)

Topic: New message mod++ :)

So, after my unsuccessful try to resurrect improved new message mod, I have rewritten phorum.org "new marking" system.
Its not very pretty, but it works smile There may be some mistakes, so be careful smile

go to file lang/en_common.php
find line:

    'Mark all as read'        =>    'Mark all forums as read',

replace with:

    'Mark all as read'        =>    'Mark forum as read',

go to file  include/functions.php
add:

     function explode_haveread($var){
    global $haveread;
    $haveread[$var]=true;
  }

go to file idex.php
find :

 if (!$cookie['is_guest'] &&($cur_forum['last_post'] > $cur_user['last_post']))
 {
                if ($cur_user['show_img'] == '1')
                        $icon = '<img src="img/'.$cur_user['style'].'_new.png" w
                else
                        $icon = '<span class="puntext"><b>?</b></span>';
        }
        else
                $icon = ' ';

delete all this, and add then:

$f_id = $cur_forum['fid'];
$forum_id = $f_id;

$new_cookie="pun-new-$forum_id";
$haveread_cookie="pun-haveread-$forum_id";

$res = $db->query('Select last_post_id as f_maxid FROM '.$db->prefix.'forums WHERE id='.$f_id.'');
$f_maxid = $db->result($res,0);
if(!IsSet($$new_cookie)){
    $$new_cookie='0';
}
$use_haveread=false;
if(IsSet($$haveread_cookie)) {
    $arr=explode(".", $$haveread_cookie);
    $old_message=reset($arr);
    array_walk($arr, "explode_haveread");
    $use_haveread=true;
}
else{
    $old_message=$$new_cookie;
}
$isnew=false;
if($use_haveread){
    if ($old_message<$f_maxid) {
        if(!IsSet($haveread[$f_maxid])) {
            $isnew=true;
        }
    }
}
elseif($old_message<$f_maxid){
    $isnew=true;
}
if($isnew)
{
    if (!$cookie['is_guest'])
 {
                if ($cur_user['show_img'] == '1')
                        $icon = '<img src="img/'.$cur_user['style'].'_new.png" w
                else
                        $icon = '<span class="puntext"><b>?</b></span>';
        }
        else
                $icon = '<img src="img/'.$pun_config['o_default_style'].'_new.png" width="16" height="16" alt="">';
}
else
$icon = ' ';

go to file  misc.php
find line:

 elseif ($action == 'markread') 
{
....................
}

and replace whole elsif expression with:

    elseif ($action == 'markread')
{
    $forum_id = intval($_GET['fid']);

    $new_cookie="pun-new-$forum_id";
    $haveread_cookie="pun-haveread-$forum_id";

    $res = $db->query('Select last_post_id as max_id FROM '.$db->prefix.'forums WHERE id='.$forum_id.'');
    //$aryRow=$q->getrow();
    $aryRow= $db->fetch_array($res);
    if(isset($aryRow['max_id'])){
        $max_id=$aryRow['max_id'];
        $$new_cookie=$max_id;
        SetCookie($new_cookie,$$new_cookie,time()+ 31536000);
        SetCookie($haveread_cookie,$$new_cookie); //destroy session cookie
        unset($$haveread_cookie);
    }

    redirect($_SERVER['HTTP_REFERER'], $lang_misc['Mark read redirect']);
}

go to file viewforum.php

find line:

<td class="punright" style="width: 19%; white-space: nowrap"><b><?php echo $post_link ?></b></td>

after it add:

<td><?php echo '  <b><a href="misc.php?action=markread&fid='.$id.'">'.$lang_common['Mark forum read'].'</b></a>' ?></td>

   
find:

            if (!$cookie['is_guest'] && $cur_topic['last_post'] > $cur_user['last_visit'] && $cur_topic['moved_to'] == null)
        {
            if ($cur_user['show_img'] == '1')
                $icon = '<img src="img/'.$cur_user['style'].'_new.png" width="16" height="16" alt="">';
            else
                $icon = '<span class="puntext"><b>?</b></span>';

            $subject = '<b>'.$subject.'</b>';
            //New posts in the topic
            $subject_new_posts = '[ <a href="viewtopic.php?id='.$cur_topic['id'].'&action=new" title="'.$lang_common['New posts info'].'">'.$lang_common['New posts'].'</a> ]';
        }
        else
        {
            $icon = ' ';
            $subject_new_posts = null;
        }

replace whole this block with:

$forum_id = $id;
$new_cookie="pun-new-$forum_id";
$haveread_cookie="pun-haveread-$forum_id";
$t_id = $cur_topic['id'];

$res = $db->query('Select last_post_id as t_maxid FROM '.$db->prefix.'topics WHERE id='.$cur_topic['id'].'');
$t_maxid = $db->result($res,0);
if(!IsSet($$new_cookie)){
    $$new_cookie='0';
}

$use_haveread=false;
if(IsSet($$haveread_cookie)) {
    $arr=explode(".", $$haveread_cookie);
    $old_message=reset($arr);
    array_walk($arr, "explode_haveread");
    $use_haveread=true;
}
else{
    $old_message=$$new_cookie;
}

$isnew=false;

if($use_haveread){
    if ($old_message<$t_maxid) {
        if(!IsSet($haveread[$t_maxid])) {
            $isnew=true;
        }
    }
}
elseif($old_message<$t_maxid){
    $isnew=true;
}

if($isnew){
    //New posts in the topic
    $subject_new_posts = '[ <a href="viewtopic.php?id='.$cur_topic['id'].'&action=new" title="'.$lang_common['New posts info'].'"><font color=orange>New</font></a> ]';
}
else{
    $subject_new_posts = null;}
            

go to file viewtopic.php

find:

 else if (isset($_GET['action']) && $_GET['action'] == 'new' && !$cookie['is_guest'])
{
      $result = $db->query('SELECT MIN(id) FROM '.$db->prefix.'posts WHERE topic_id='.$id.' AND posted >'.$cur_user['last_visitonline']) or error('Unable to fetch post info', __FILE__, __LINE__, $db->error());
  $first_new_post_id = $db->result($result, 0);

replace whole block with:

else if (isset($_GET['action']) && $_GET['action'] == 'new')
{
    $res = $db->query('Select id from '.$db->prefix.'posts where topic_id='.$id.' ORDER BY id');
    $rec = $db->fetch_row($res);
    While($rec){
        $msgarr[]=$rec[0];
        $rec = $db->fetch_row($res);
    }
    $res = $db->query('Select forum_id from '.$db->prefix.'topics where topic_id='.$id.'');
    $forum_id = $db->result($res,0);

    $new_cookie="pun-new-$forum_id";
    $haveread_cookie="pun-haveread-$forum_id";

    $arr=explode(".", $$haveread_cookie);

    function newmsg($a, $b) {
        $al = sizeof($a);
        $bl = sizeof($b);
        sort($a);

        if ($b[$bl-1]<$a[0]) return $b[$bl-1];

        $i = 0; $j = 0;
        while(1) {
            if ($a[$i] < $b[$j]) {
                $i++;
                if ($i == $al) return $b[$j];
            } else if($a[$i] == $b[$j]) {
                $j++;
                if ($j == $bl) return $b[$bl-1];
            } else {
                return $b[$j];
            }
        }
    }

    $first_new_post_id = newmsg($arr, $msgarr);

   
find line:

$footer_style = 'topic';

before add:

$new_cookie="pun-new-$forum_id"; 
$haveread_cookie="pun-haveread-$forum_id";


 if(!IsSet($$new_cookie)){
    $$new_cookie='0';
}

$use_haveread=false;
if(IsSet($$haveread_cookie)) {
    $arr=explode(".", $$haveread_cookie);
    $old_message=@reset($arr);
    array_walk($arr, "explode_haveread");
    $use_haveread=true;
}
else{
    $old_message=$$new_cookie;
}


$res = $db->query('Select id from '.$db->prefix.'posts where topic_id='.$id.' ORDER BY id');
$rec = $db->fetch_row($res);

While($rec){
    $headers[]=$rec;
    $rec = $db->fetch_row($res);
}
$madechange=false;
@reset($headers);
$row=@current($headers);
while(!empty($row[0])){

    if(empty($haveread[$row[0]]) && $row[0] > $old_message){
        $madechange=true;
        if(empty($$haveread_cookie)){
            $haveread[$$new_cookie] = true;
            $$haveread_cookie=$$new_cookie;
        }
        $$haveread_cookie.=".";
        $$haveread_cookie.=$row[0];
    }
    $haveread[$row[0]] = true;
    $max_id=$row[0];
    $row=next($headers);
}
if ($madechange) {
    SetCookie($haveread_cookie,$$haveread_cookie,0);
}




if($$new_cookie<$max_id){
        $$new_cookie=$max_id;
        SetCookie($new_cookie,$$new_cookie,time()+ 31536000);
}

That's all ! smile

Re: New message mod++ :)

Cool. I haven't tried it, but there are some problems.

First of all, you can't have made those instructions based on a vanilla 1.1.5 install. E.g. there is no row "if (!$cookie['is_guest'] &&($cur_forum['last_post'] > $cur_user['last_post']))" in index.php. There's no index last_visitonline in $cur_user etc. I think a lot of people will have difficulties installing this.

Then, the more serious problem. You're running one query per forum on index.php and one query per topic in viewforum.php. That's a lot of queries.

Last, but not least. Why do you people insist on never indenting your code properly? It's impossible to see which } closes which { and where a certain part of the code belongs. I don't care if if you place your { on the same row as the control statement, but for gods sake, indent your code with tabs! Pardon my rant here, but I'm kind of in a bad mood tonight. Don't take it too hard :)

"Programming is like sex: one mistake and you have to support it for the rest of your life."

3

Re: New message mod++ :)

thx, im not very good coder... an it was written in simple text editor smile i can change all this. BTW, about viewtopic & viewforum, how can i optimise my queries?

Re: New message mod++ :)

Well, for both index.php and viewforum.php, you're selecting last_post_id and I believe that is already done in the query that displays the forums/topics. Just use that value instead.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

5

Re: New message mod++ :)

Could you please tell me how to install this mod and/or correct the instruction you gave?
I'd really like to have a new message notifying system better than the default one, but i can't install this one, and Jansson's one have a broken link sad

Thx

6

Re: New message mod++ :)

ok, i'll try to make this more readable.

7

Re: New message mod++ :)

thx i'll be looking for it wink

8

Re: New message mod++ :)

done

9

Re: New message mod++ :)

but that doesn't correct what Rickard pointed out does it?

10

Re: New message mod++ :)

i've made _some_ corrections
BTW you can do them yourself, as for me, i have no time for this optimising now, and as far as i remember, i placed this code  outside main cycle not just  for fun,  there were some issues with it...