Jump to content

  • Chat
  •  
  •  

Welcome to Formiculture.com!

This is a website for anyone interested in Myrmecology and all aspects of finding, keeping, and studying ants. The site and forum are free to use. Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation points to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. If you already have an account, login here - otherwise create an account for free today!

Photo

Request for some help with a simple PHP modification


  • Please log in to reply
27 replies to this topic

#1 Offline dspdrew - Posted April 8 2017 - 12:28 AM

dspdrew
  • LocationSanta Ana, CA

I have very limited knowledge of PHP, but I'm usually able to figure it out enough to make simple modifications myself. This time I'm having some trouble. Below is the code that creates the "Newest Topics" section on the front page of the forum. What I need to do is exclude forum ID 45 and 48, along with any of their child forums. If anyone can help write that code for me, I would really appreciate it. So would anyone who doesn't want to see junk threads from the "sandbox" on there. :P
 

<?php

/**
 * Product Title:		(SOS34) Recent Topics
 * Product Version:		3.2.2
 * Author:				Adriano Faria
 * Website:				SOS Invision
 * Website URL:			http://forum.sosinvision.com.br/
 * Email:				administracao@sosinvision.com.br
 */

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class public_forums_ajax_recentTopics extends ipsAjaxCommand 
{
	public function doExecute( ipsRegistry $registry )
	{
		switch( $this->request['do'] )
		{
			default:
				return $this->recentTopics();
			break;
		}
	}
	
	public function recentTopics()
	{
		if ( $this->memberData['member_id'] AND !$this->memberData['recenttopics_onoff'] )
		{
			$this->returnJsonError( 'No permissions' );
		}
		
		if ( $this->settings['topicosrecentes_posts'] AND $this->memberData['posts'] < $this->settings['topicosrecentes_posts'] )
		{
			$this->returnJsonError( 'No permissions' );
		}

		$this->registry->class_localization->loadLanguageFile( array( 'public_forums', 'public_topic', 'forums' ) );
		$this->registry->class_localization->loadLanguageFile( array( 'public_boards', 'forums' ) );

		$topicos = array();

		$forumIDs = $this->registry->class_forums->forum_by_id;

		foreach( $forumIDs as $id => $data )
		{
			if ( $this->registry->permissions->check( 'read', $data ) === TRUE )
			{
				$forums[] = $data['id'];
			}
		}

		if ( is_array( $forums ) AND count( $forums ) )
		{
			$ids = implode( ',', $forums );
		}
		
		if ( !$ids )
		{
			$this->returnJsonError( 'No readable forums' );
		}

		/* Exclude forums (by Admin) */
		$exclude = $this->settings['topicosrecentes_forums'];
		$excludeforums       = "";

		if ( $exclude )
		{
			$excludeforums = " and t.forum_id NOT in({$exclude})";
		}

		/* Exclude forums (by user) */
		$excludeforumsuser	 = "";
		
		if ( $this->memberData['member_id'] AND $this->memberData['recenttopics_forums'] )
		{
			$excludeforumsuser = " and t.forum_id NOT in({$this->memberData['recenttopics_forums']})";
		}

		/* Exclude CLOSED topics */
		if ( $this->settings['topicosrecentes_fechados'] )
		{
			$closed = 'and t.state != "closed"';
		}

		if ( $this->memberData['member_id'] )
		{
			$qtd = $this->memberData['topicosrecentes_showdqtd'] ? $this->settings['topicosrecentes_nr'] : $this->memberData['recenttopics_qtd'];
		}
		else
		{
			$qtd = $this->settings['topicosrecentes_nr'];
		}
		
		$this->request['last_post_time'] = intval( $this->request['last_post_time'] );
		if ( ! $this->request['last_post_time'] )
		{
			$this->returnJsonError( 'Invalid timestamp' );
		}
		
		/* Ignore topics we already have in our list */
		$excludeTopics 	= "";
		$ourTids		= array();
		if ( $this->request['our_tids'] )
		{
			foreach( json_decode( $_POST['our_tids'], true ) as $tid )
			{
				$ourTids[ intval( $tid[0] ) ] = array( 'tid' => intval( $tid[0] ), 'timestamp' => intval( $tid[1] ) );
			}
		}
		
		/* Load tagging stuff */
		if ( ! $this->registry->isClassLoaded('tags') )
		{
			require_once( IPS_ROOT_PATH . 'sources/classes/tags/bootstrap.php' );/*noLibHook*/
			$this->registry->setClass( 'tags', classes_tags_bootstrap::run( 'forums', 'topics' )  );
		}
		
		$this->DB->build( array( 
							'select'   => 't.*',
							'from'     => array( 'topics' => 't' ),
							'add_join' => array(
												array(
														'select' => 'm1.members_display_name as nome1, m1.members_seo_name, m1.member_group_id as grupo1',
														'from' 	 => array( 'members' => 'm1' ),
														'where'  => 'm1.member_id=t.starter_id',
														'type'   => 'left',
												),
												$this->registry->tags->getCacheJoin( array( 'meta_id_field' => 't.tid' ) ),
										),
							'where'	   => "t.last_post > {$this->request['last_post_time']} AND t.approved = 1 AND t.forum_id in({$ids})".$excludeforums.$excludeforumsuser.$closed.$excludeTopics,
							'order'    => 't.last_post desc',
							'limit'	   => array( 0, $qtd*2 ),
		) );

		$this->DB->execute();
			
        while( $r = $this->DB->fetch() )
        {
            if ( $this->canViewTid( $r['forum_id'], $r['starter_id'] ) === TRUE )
            {
                $topic_array[ $r['tid'] ] = $r;
                $topic_ids[ $r['tid'] ]   = $r['tid'];
    
                if( $r['last_poster_id'] )
                {
                    $member_ids[ $r['last_poster_id'] ]    = $r['last_poster_id'];
                }

                $member_ids[ $r['last_poster_id'] ]    = $r['last_poster_id'];
            }
        }
		
		$_members	= IPSMember::load( $member_ids );
					
		$topic_array = $this->checkUserPosted( $topic_ids, 1, $topic_array );
		
		if ( !is_array( $topic_array ) AND !count( $topic_array ) )
		{
			$this->returnJsonError( 'Nothing new' );
		}

		$topic_array = array_slice( $topic_array, 0, $qtd );
		
		$classToLoad = IPSLib::loadActionOverloader( IPSLib::getAppDir( 'forums', 'forums' ) . '/forums.php' , 'public_forums_forums_forums' );
		$forumsClass = new $classToLoad;
		$forumsClass->makeRegistryShortcuts( $this->registry );
		
		foreach ( $topic_array as $r )
		{
			/* Topic title colored? */
			$r['style'] = "";
			
			if( isset( $r['ttc_fontcolor'] ) OR isset( $r['ttc_backgroundcolor'] ) OR isset( $r['ttc_bold'] ) OR isset( $r['ttc_italic'] ) )
			{
				$r['style'] .= " style='";
				$r['style'] .= "-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;padding-left:5px;padding-right:5px;padding-top:2px;padding-bottom:2px;";
				$r['style'] .= ( $r['ttc_fontcolor'] ) ? "color: {$r['ttc_fontcolor']}; " : '';
				$r['style'] .= ( $r['ttc_backgroundcolor'] ) ? "background-color: {$r['ttc_backgroundcolor']}; " : '';
				$r['style'] .= ( $r['ttc_italic'] ) ? "font-style: italic; " : '';
				$r['style'] .= ( $r['ttc_bold'] ) ? "font-weight: bold; " : '';
				$r['style'] .= "'";
			}

			/* Add member */
			if( $r['last_poster_id'] )
			{
				$r	= array_merge( IPSMember::buildDisplayData( $_members[ $r['last_poster_id'] ] ), $r );
			}
			else
			{
				$r	= array_merge( IPSMember::buildProfilePhoto( array() ), $r );
			}
			
			$r = $forumsClass->renderEntry( $r );
			
			
			$r['forumBreadcrumb'] = $this->registry->getClass('class_forums')->forumsBreadcrumbNav( $r['forum_id'] );
			
			if ( isset( $ourTids[ $r['tid'] ] ) )
			{
				$toBeUpdated[ $r['tid'] ] = array( 	'topic_data' 	=> $r, 
													'forum_data' 	=> $this->registry->class_forums->forum_by_id[ $r['forum_id'] ]
											);
			}
			else
			{
				$topicos[ $r['tid'] ] = array( 	'topic_data' 	=> $r, 
												'forum_data' 	=> $this->registry->class_forums->forum_by_id[ $r['forum_id'] ]
											);
			}
			
			$allTopics[] = $r;
		}
		
		$html = $this->registry->getClass('output')->getTemplate( 'boards' )->hooksos_topicosrecentesAjax_tema1( $topicos );


		$html = preg_replace( "#<!--hook\.([^\>]+?)-->#", '', $html );
		$html = $this->registry->getClass( 'output' )->replaceMacros( $html );
		
		if ( is_array( $toBeUpdated ) AND count( $toBeUpdated ) )
		{
			foreach( $toBeUpdated as $tid => $t )
			{
				$updatedTopics[] = array( 
											'tid' => $tid,
											'html' => $this->registry->getClass( 'output' )->replaceMacros( $this->registry->output->getTemplate( 'boards' )->hooksos_topicosrecentes_parseTopic( $t ) )
										);
			}
		}

		$lastTopic = reset( $allTopics );

		$this->returnJsonArray( array( 'html' => $html, 'updated' => $updatedTopics, 'last_tid' => $lastTopic['tid'], 'last_tid_timestamp' => $lastTopic['last_post'] ) );
	}

    function canViewTid( $fid, $mid )
    {
		if ( $this->registry->class_forums->allForums[ $fid ]['password'] != '' )
		{
			return false;
		}

		if( ( ! $this->registry->class_forums->allForums[ $fid ]['can_view_others'] AND ! $this->memberData['is_mod'] ) AND ( $mid != $this->memberData['member_id'] ) )
		{
			return false;
		}
		
		return true;
    }

    function checkUserPosted( $topic_ids=array(), $parse_dots=1, $topic_array=array() )
    {
       	if ( ( $this->memberData['member_id'] ) and ( count( $topic_ids ) ) and ( $parse_dots ) )
       	{
           		$this->DB->build(
               		array(
	               	    'select' => 'author_id, pid, topic_id',
	       	            'from'   => 'posts',
       	        	    'where'  => "author_id=".$this->memberData['member_id']." AND topic_id IN(".implode( ",", $topic_ids ).")",
               		)
           		);
            
			$this->DB->execute();

	        while ( $p = $this->DB->fetch() )
			{
        		if ( is_array( $topic_array[ $p['topic_id'] ] ) )
                {
	            	$topic_array[ $p['topic_id'] ]['author_id'] = $p['author_id'];
        	    }
            }
        }

        return $topic_array;
    }
}


#2 Offline Barristan - Posted April 8 2017 - 12:50 AM

Barristan

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • LocationBindlach, Bavaria, Germany

Change

$excludeforums       = "";

to

$excludeforums       = "45, 46, 48";

You have to list all forum ids separately (without changing the sql statement).



#3 Offline dspdrew - Posted April 8 2017 - 12:55 AM

dspdrew
  • LocationSanta Ana, CA

I tried that already, but I will try it again. I was half asleep last night when I was doing it. That should just start the variable with those ID's and then build on to it right?

 

Edit: Not working.



#4 Offline Barristan - Posted April 8 2017 - 12:59 AM

Barristan

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • LocationBindlach, Bavaria, Germany

ok I looked at it again:

$exclude = $this->settings['topicosrecentes_forums'];

to

$exclude = "45, 46, 48";

and don't touch $excludeforums

 

because in excludeforums the query string will be stored. However the script tries to get some forum ids from a settings variable. Maybe there is some kind of setting you can use to set these ids so you don't have to manually add them to the script.


Edited by Barristan, April 8 2017 - 12:59 AM.

  • MikeMc likes this

#5 Offline dspdrew - Posted April 8 2017 - 1:05 AM

dspdrew
  • LocationSanta Ana, CA

Maybe there is some kind of setting you can use to set these ids so you don't have to manually add them to the script.

 

That's exactly what I was thinking now that I am looking at that code. If there is, I never knew about them, and have no idea where they are. IPBoard's admin control panel has the worst organization, especially when it comes to hooks and add-on applications. They stick the settings for some of them in the strangest places.



#6 Offline Barristan - Posted April 8 2017 - 1:07 AM

Barristan

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • LocationBindlach, Bavaria, Germany

That's exactly what I was thinking now that I am looking at that code. If there is, I never knew about them, and have no idea where they are. IPBoard's admin control panel has the worst organization, especially when it comes to hooks and add-on applications. They stick the settings for some of them in the strangest places.

 

 

 

I've never used this forum as an admin ;)

 

What is the name of the db table in which forums are stored and what are the names of the colums which store id and parent id?


Edited by Barristan, April 8 2017 - 1:08 AM.


#7 Offline dspdrew - Posted April 8 2017 - 1:14 AM

dspdrew
  • LocationSanta Ana, CA

Geez, I'm not sure. I will tell you that even experienced PHP coders say this software some of the most confusing and difficult software they've worked with.



#8 Offline dspdrew - Posted April 8 2017 - 1:26 AM

dspdrew
  • LocationSanta Ana, CA

Nothing. There are no settings anywhere in the control panel for this plugin... Maybe the forgot it, or decided to get rid of it. I guess hard coding it will be the way to do it then.



#9 Offline dspdrew - Posted April 8 2017 - 1:31 AM

dspdrew
  • LocationSanta Ana, CA

ok I looked at it again:

$exclude = $this->settings['topicosrecentes_forums'];

to

$exclude = "45, 46, 48";

and don't touch $excludeforums

 

Nope. Not working.



#10 Offline Barristan - Posted April 8 2017 - 6:21 AM

Barristan

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • LocationBindlach, Bavaria, Germany

Maybe there is another file because this one is for ajax requests:

public_forums_ajax_recentTopics extends ipsAjaxCommand

You could also check if there is a settings.php file and change the vlaue of 'topicosrecentes_forums' there.



#11 Offline dspdrew - Posted April 8 2017 - 6:53 AM

dspdrew
  • LocationSanta Ana, CA

Yeah... I just noticed that even if I delete every bit of code in that entire file, nothing changes on the site. :lol: So, something's wrong. That file is obviously not being used. I'll have to check it out later today; I can't work on it now.



#12 Offline MikeMc - Posted April 8 2017 - 12:59 PM

MikeMc

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationOhio

So I see that IP.Board has disabled the official 3.x marketplace and download - that sucks.

 

Did some quick searching and found this, that appears to possibly be an upgraded version of what you are using.... https://webflake.sx/...ent-topics-322/

 

In the screenshot, I found the configuration item you are looking for and should be what Barristan was referring to regarding settings.

 

 
Screen Shot 2017 04 08 At 4.58.22 PM

 

 


Edited by MikeMc, April 8 2017 - 2:45 PM.

Prenolepis imparis:  founding queens x15 | Journal


#13 Offline dspdrew - Posted April 8 2017 - 2:30 PM

dspdrew
  • LocationSanta Ana, CA

Can't see that picture.



#14 Offline MikeMc - Posted April 8 2017 - 2:45 PM

MikeMc

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationOhio

Uploaded to album and modified post, you should be able to see it now.


Prenolepis imparis:  founding queens x15 | Journal


#15 Offline dspdrew - Posted April 8 2017 - 5:25 PM

dspdrew
  • LocationSanta Ana, CA

Yeah that file I originally posted is actually from that exact plugin. I think I had it installed at one time, but then uninstalled it because it wasn't at all what I wanted. It looks like i left the files behind. I realize now that what I call the "Newest Topics" is actually one of IPS's own built-in hooks.

 

The only thing I have ever found is in class_forums.php.

 

public function hooks_recentTopics( $topicCount=10, $output=true )

 

This is where I am able to change the amount of topics that show. Doesn't this mean there must be a page of code for the function "hooks_recentTopics()"?



#16 Offline MikeMc - Posted April 8 2017 - 5:30 PM

MikeMc

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationOhio

Do you mind letting me know which version of the board you are using (feel free to pm or email it to me) and I can try and dig in a little bit?

 

To answer your question, yes there should be a ...

hooks_recentTopics()

... somewhere. But it's probably likely that filtering out forums is not in that function and would need to be manually written, either by changing the database call or filtering the return values.


Prenolepis imparis:  founding queens x15 | Journal


#17 Offline dspdrew - Posted April 8 2017 - 5:33 PM

dspdrew
  • LocationSanta Ana, CA

Yeah that's what I plan to do.

 

I just noticed the code is right below it. :lol:  I'm checking it out now.


  • MikeMc likes this

#18 Offline MikeMc - Posted April 8 2017 - 5:34 PM

MikeMc

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationOhio

Yeah that's what I plan to do.

 

I just noticed the code is right below it. :lol:  I'm checking it out now.

 

g'luck!


Prenolepis imparis:  founding queens x15 | Journal


#19 Offline dspdrew - Posted April 8 2017 - 11:33 PM

dspdrew
  • LocationSanta Ana, CA

Okay... THIS is the code for the "Newest Topics" column. I removed all the extra line spaces, and other random spaces.

public function hooks_recentTopics( $topicCount=10, $output=true )
{
    /* INIT */
    $topicIDs   = array();
    $topic_rows = array();
    $timesUsed  = array();
    $bvnp       = explode( ',', $this->settings['vnp_block_forums'] );

    $this->registry->class_localization->loadLanguageFile( array( 'public_topic', 'public_forums' ), 'forums' );

    /* Grab last X data */
    foreach( $this->forum_by_id as $forumID => $forumData )
    {
        if ( ! $forumData['can_view_others'] AND ! $this->_memberData['is_mod'] )
        {
            continue;
        }
        if ( $forumData['password'] != '' )
        {
            continue;
        }

        if ( ! $this->registry->permissions->check( 'read', $forumData ) )
        {
            continue;
        }

        if ( is_array( $bvnp ) AND count( $bvnp ) )
        {
            if ( in_array( $forumID, $bvnp ) )
            {
                continue;
            }
        }
 
        /* Still here? */
        $_topics = $this->lastXThaw( $forumData['last_x_topic_ids'] );

        if ( is_array( $_topics ) )
        {
            foreach( $_topics as $id => $time )
        {
            if( in_array( $time, $timesUsed ) )
                {
                    while( in_array( $time, $timesUsed ) )
                    {
                        $time +=1;
                    }
                }

                $timesUsed[]       = $time;
                $topicIDs[ $time ] = $id;
            }
        }
    }

    $timesUsed    = array();
 
        if ( is_array( $topicIDs ) && count( $topicIDs ) )
    {
        krsort( $topicIDs );

        /* We get up to double in case some of the latest are moved_to links - we do another array_slice afterwards to limit to right limit */
        $_topics   = array_slice( $topicIDs, 0, $topicCount * 2 );

        if ( is_array( $_topics ) && count( $_topics ) )
        {
            /* Query Topics */
            $this->registry->DB()->build( array(
                'select'   => 't.tid, t.title as topic_title, t.title_seo, t.start_date, t.starter_id, t.starter_name, t.moved_to, t.views, t.posts, t.topic_answered_pid',
                'from'     => array( 'topics' => 't' ),
                'where'    => 't.tid IN (' . implode( ',', array_values( $_topics ) ) . ')',
                'add_join' => array(
                    array(
                        'select' => 'm.*',
                        'from'   => array( 'members' => 'm' ),
                        'where'  => 'm.member_id=t.starter_id',
                        'type'   => 'left',
                    ),
                    array(
                        'select' => 'pp.*',
                        'from'   => array( 'profile_portal' => 'pp' ),
                        'where'  => 'm.member_id=pp.pp_member_id',
                        'type'   => 'left',
                    ),
                )
            )
        );
 
        $outer = $this->registry->DB()->execute();

        while( $r = $this->registry->DB()->fetch( $outer ) )
            {
                if( !empty($r['moved_to']) )
                {
                    continue;
                }
 
                $time = $r['start_date'];

                if( in_array( $time, $timesUsed ) )
                {
                    while( in_array( $time, $timesUsed ) )
                    {
                        $time +=1;
                    }
                }

                $timesUsed[]          = $time;
                $topics_rows[ $time ] = IPSMember::buildDisplayData( $r );
            }

            /* Got any results? */
            if ( count($topics_rows) )
            {
                krsort( $topics_rows );
                $topics_rows = array_slice( $topics_rows, 0, $topicCount );
            }
        }
    }

    if( $output )
    {
        return $this->registry->output->getTemplate('boards')->hookRecentTopics( $topics_rows );
    }
    else
    {
        return $topics_rows;
    }
}


#20 Offline Barristan - Posted April 9 2017 - 1:12 AM

Barristan

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • LocationBindlach, Bavaria, Germany
add a new array declaration to the init block like that:
 
/* INIT */
$topicIDs   = array();
$topic_rows = array();
$ignoredForumIDs = array(45, 46, 48);
 
add a new if statement under one of the existing within the foreach block.
if ( $forumData['password'] != '' )
{
  continue;
}

if ( in_array( $forumID, $ignoredForumIDs ) )
{
  continue;
}

  • T.C. and MikeMc like this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users