Recent Topics

1 Dec 08, 2006 19:39    

I'm working on an Extra Fields plugin. You can see the rough draft of it [url=http://brendoman.com/dev/191/plugins/extrafields_plugin/_extrafields.plugin.txt]here[/url]. I think most of the backoffice stuff is done, and now I'm working on a sidebar block that shows a list of fields with sublists of field values under each field. Each value is a link and the link should show you a list of posts that have that value for that field.

At the moment I'm setting the link to disp=ef, which keeps the default MainList from loading up, then I use the SkinBeginHtmlHead event to create my own $MainList and set $disp back to 'posts'. So far, so good. I can search my plugin db tables to get a list of all the post IDs for posts that I want to show. I just don't know how to add them to my MainList. I guess I need to either

a) Pass ItemList an array of post IDs, or

b) Pass ItemList some more sql to teach it how to look in my plugin's db tables.

I'm using 1.9.1. Is there a way to do this, or is this going to take a new plugin hook?

2 Dec 16, 2006 12:16

Not sure if this has anything to do with what you want, but what i'm doing with a plugin right now, is create a $MainList / ItemList and then merge it's $Blah->rows with $MainList->rows, then kill my one. My one will be local.

3 Dec 16, 2006 14:53

I should clarify. It's not the overriding that is really giving me the problem. It's no problem to create my own ItemList object using the standard method and call it $MainList. The problem is that I want to create th ItemList object using a completely custom method, viz. I'm using a separate sql table to decide which items make it into the list.

I'll add another option to my list above. YaBBa thought it already worked like this, but neither of us can find it.

c) Create an empty ItemList object, then add items to it one at a time like this: $MainList->add($item); where $item is either an Item object or an item id number.

4 Dec 16, 2006 15:14

This should do it

	global $MainList;

	// Make a list of Item Rows
	$rows = array();
	
	// Get the Item
	$Item_row = /* code goes here to get the database row of the item */;
	$rows[] = $Item_row;
	
	// Merge the rows
	$MainList->rows = $rows;
	$MainList->result_num_rows += sizeof($rows);
	
	// Create the Objs
	$i = 0;
	foreach( $rows as $row )
	{
		// Prebuild object:
		$MainList->Obj[$i] = new $MainList->objType( $row, $MainList->dbtablename, $MainList->dbprefix, $MainList->dbIDname ); // COPY!!

		// To avoid potential future waste, cache this object:
		$MainList->DataObjectCache->add( $MainList->Obj[$i] );

		// Make a list of posts for future queries!
		array_unshift( $MainList->postIDarray, $row->{$MainList->dbIDname} );	// new row at beginning (fplanque>>why?)

		$i++;
	}
	
	// Update the categories to suit the new posts
	cat_load_postcats_cache();

Edit: made a few updates.

Edit: another fix

6 Dec 16, 2006 16:07

Yeh that code was a quick put together.

Here's the latest code i've used (it's working fine right now for me in my own plugin), you will need to adapt it appropriatly, and probably just use [] = , instead of array_unshift.

		// Get the sticky post status id and restrict
		$mysql_query =
			'SELECT * '.
			'FROM evo_posts '.
			'WHERE `'.$MainList->dbprefix.'ptyp_ID` = '.$DB->quote($this->Sticky->ID).' '.
			'ORDER BY post_datestart DESC';
			
		$rows = $DB->get_results( $mysql_query, OBJECT, 'Get Sticky Posts' );
		$rows_size = sizeof($rows);
		
		// Create the Objs
		for (
			$i = 0, $n = $rows_size;
			$i < $n;
			$i++
		)
		{
			# Get the row
			
			$row = $rows[$i];
			$row_exists = array_search($row, $MainList->rows);
			
			# Prepend the item
				
			// Prepend the row
			array_unshift( $MainList->rows, $row );
			
			// Create the Obj
			$Obj = new $MainList->objType($row, $MainList->dbtablename, $MainList->dbprefix, $MainList->dbIDname);
			
			// Append the object
			array_unshift( $MainList->Obj, $Obj );
			
			// Make a list of posts for future queries!
			array_unshift( $MainList->postIDarray, $Obj->ID );
			
			# If the row existed already then lets remove it
			if ( $row_exists )
			{
				$row_exists++; // To accomadate for the newly added row
				unset($MainList->rows[$row_exists]);
				unset($MainList->Obj[$row_exists]);
				unset($MainList->postIDarray[array_search($Obj->ID, $MainList->postIDarray)]);
			}
			
		}
		$MainList->result_num_rows = sizeof($MainList->rows);
		if( !empty($MainList->postIDarray) )
		{
			$MainList->postIDlist = implode( ',', $MainList->postIDarray );
		}

7 Dec 16, 2006 16:18

It works. Well done and thank you.

8 Dec 16, 2006 16:24

It works. Well done and thank you.

Thanks. :)

And you may want to update the select (if you are using my select) to something like this;

		// Get the sticky post status id and restrict
		$mysql_query =
			'SELECT * '.
			'FROM '.$MainList->ItemQuery->dbtablename.' '.
				'INNER JOIN T_postcats ON '.$MainList->ItemQuery->dbIDname.' = postcat_post_ID '.
				'INNER JOIN T_categories ON postcat_cat_ID = cat_ID '.
			'WHERE `'.$MainList->dbprefix.'ptyp_ID` = '.$DB->quote($this->Sticky->ID).' '.
				'AND '.statuses_where_clause('', $MainList->ItemQuery->dbprefix, $MainList->ItemQuery->blog).' '.
				'AND cat_blog_ID = '.$MainList->ItemQuery->blog.' '.
			'ORDER BY post_datestart ASC';

9 Dec 16, 2006 16:25

Nah, I've already replaced your query with mine.


Form is loading...