Jump to content
Sign in to follow this  
Alejandro

Programatically create and update listings

Recommended Posts

Here's something you can try to programmatically create listings from other extensions/plugins:

<?php
// Load the JReviews framework
require_once JPATH_BASE.'/components/com_jreviews/jreviews/framework.php';

// Set the form token
$_POST[cmsFramework::getToken()] = 1;

// Let the framework know the code is accessed directly so it suppresses output headers
$_REQUEST['requested'] = 1;

// Build the request data
$params = [];

// Routing data

$params['data']['controller'] = 'listings';
$params['data']['action'] = '_save';

// Ensure that the form passes the consent checkbox validation if it's enabled
$params['data']['consent'] = true;

// Add  the standard fields
// Catid is required and must be a JReviews category, title is required if not disabled
$params['data']['Listing'] = [
	'catid' => 23,
	'title' => 'Programatically submitted listing',
	'introtext' => '<p>This is the listing summary.</p>',
	'fulltext' => '<p>This is the listing description.</p>',
];

// Add the custom field data
// For select,radio,checkbox fields, use the field option values
// Checkbox and multiselect fields need to use array inputs
$params['data']['Field']['Listing'] = [
	'jr_website' => 'http://origamiappbuilder.com',
	'jr_checkbox' => ['one','two']
];

$Dispatcher = new S2Dispatcher('jreviews');

// If running the script via cron without a specific Joomla user you will want to 
// set the listing owner by uncommenting the two lines below
// to load it using the user ID
// $auth = S2Object::make('auth');
// $auth->loadUser(46);

$output = $Dispatcher->dispatch($params);

$output = json_decode($output,true);

if ($output['success'] == true) 
{
	// Listing was successfully created

	$listingId = $output['listing_id'];	
}

 

Share this post


Link to post

This is awesome, Alejandro.  Thanks.

I'm about to embed this code into the scraper plugin I'm working on.  One quick question.  If I'm "updating" a listing rather than creating one, do I just supply the 'ID' key/value in the $params['data']['Listing'] ?

eg. 

$params['data']['Listing'] = [
	'ID' => 123,
	'catid' => 23,
	'title' => 'Programatically submitted listing',
	'introtext' => '<p>This is the listing summary.</p>',
	'fulltext' => '<p>This is the listing description.</p>',
];

 

Share this post


Link to post

To update, you include the 'id' key (it's lowercase because it's normalized for Joomla and WordPress).

For new listings, to insert as a user, you need to "load the user" first and JReviews will automatically pick it up in the controller. I already gave you the code for that above so you need to uncomment it and pass the right user ID.

// If running the script via cron without a specific Joomla user you will want to 
// set the listing owner by uncommenting the two lines below
// to load it using the user ID
// $auth = S2Object::make('auth');
// $auth->loadUser(46);

 

Share this post


Link to post

Is there any initialization I need to perform?  They are getting created during a cron job.

I get:

Warning: fopen(/var/www/html/wp-content/plugins/s2framework/tmp/cache/core/locale-jreviews_overrides/locale.js): failed to open stream: No such file or directory in /var/www/html/wp-content/plugins/jreviews/jreviews/controllers/my_controller.php on line 440

Share this post


Link to post

That's a warning that can be ignored. Remember I told you that this is a "hacky" way of doing it and that hasn't changed. JReviews is trying to create the missing file on that line, so I am not sure why it's complaining that it's not there if it's trying to create it. The file it's also not needed for what you are trying to do, but it's  part of the logic that runs whenever a controller action is invoked.

Share this post


Link to post

I've finally gotten the scraper to correctly (somewhat) create the listing.  I'm using the code above (with the loadUser()) to "update" the listing and hopefully create any required meta-data.

I do get the following output:

(
    [success] => 1
    [str] => 
    [is_new] => 
    [html] => <div class="jrSuccess">

		Thank you for your submission.
		<a class="jrButton" href="http://dhdev.kola.com/evans-syndrome/">Go to listing</a>

	</div>
    [listing_id] => 123
    [moderation] => 
    [mediaForm] => 
)

But when I pull up the category page, I don't see either of the two scraped listings that were submitted.  There is one that was manually entered through the submission form, but neither of the programmatically added ones.  I don't have any custom field data (yet).

Share this post


Link to post

I'm wondering.  It appears for the listing I entered through the submission form has an empty/default row in the wp_jreviews_content table.  It shows up in the list.

Actually, I just tested that theory and inserted another empty row replacing contentid with the post ID.  The one I added shows up now.

Share this post


Link to post

I managed to get that record created by adding a 'listing_note' to the custom field data.

                $params['data']['Field']['Listing'] = [
                        'listing_note' => 'Scraped Listing',
                ];

Another strangeness I noticed...  When I added 2 diseases with the scraper, the showed up on the listing/category page, however it still said "1 results - showing 1 - 1", but 3 items were listed.  I then added a 4th with a manual submission form, and they all show up and it now says "4 results - showing 1 - 4".  So however that is generated isn't getting "touched" with the submission dispatch.  Do you happen to know how those are updated?

Share this post


Link to post

That's just the session cache which specifically caches the count to avoid running that query on every page load since most of the time this doesn't change often. You can disable the session cache in the JReviews configuration/cache settings. If you don't want to disable it and still check that the count is right, load the page in a new private tab so a new session is started.

By the way, depending on what you are doing you may find this post useful as well:

 

Share this post


Link to post

I understand that Catid is required and must be a JReviews listing category.  Is it possible to have an array of categories (with the others as children of the JReviews cateogory)?

For example, the JReviews listing type & category is Diseases.

I have sub-categories of Diseases to which the individual disease listings also belong to, eg. Blood Diseases, Endocrine Diseases, etc.

When I pass an array for catid I get an error.  Or is this something I need to change/update afterward?  I found that if I had created the post with it already related to all the categories and sub-categories, then do the submission, it removes all the sub-categories from the post/listing and just leaves the one category.

Share this post


Link to post

It seems to allow me to attach them to other categories (sub-categories) afterward though.  I there any issue or possible downfall if I did that?  The sub-categories are only for classification, nothing to do with JReviews.

Or would it be better to create "tags"?

Edited by bk-1530585905

Share this post


Link to post

Since JReviews doesn't officially support multiple categories per listing I really wouldn't be able to tell you what the effects of doing that would be, if any. If you can achieve the same thing with "tags" then that's something you should consider as well.

Share this post


Link to post

I have a question about custom field data.  Not sure if this is the right place or a new topic.

Anyway, I created a custom field called "jr_othernames", which is essentially synonyms.  Its a multi-select field, and the manual submission form allows you to add them.

Can I add them using the programatic interface above?  I noticed in the database wp_jreviews_content table it added the column jr_othernames and inserted the value "*adrenal-hypoplasia*hypoadrenocorticism-familial*".  I see in the wp_jreviews_fieldoptions table it has the mapping of text to value, value being whats in the jr_othernames column and text is whats displayed on screen as well as whats in the "export", eg.

|       57 |      24 | Adrenal hypoplasia           | adrenal-hypoplasia           |       |        1 |               |               |       0 |             |

|       58 |      24 | Hypoadrenocorticism familial | hypoadrenocorticism-familial |       |        2 |               |               |       0 |             |

How would I go about creating these othernames "on the fly"?  Do I just put something in the $params['data']['Field']['Listing'] paramter for jr_othernames?  Will that create the option?  Or how do I dynamically add new options as they're discovered?

Share this post


Link to post

Yes, it is possible to dynamically create new options for fields while submitting a new listing. The feature is called  "Click2add" and can be enabled for select and multiple select fields directly in the individual field's settings. Once you do that if you go to the listing form you'll see a new "Add" button next to the field which lets you create new options when the listing is submitted. You can also take advantage of this feature when creating listings programmatically with the method I gave you before. In the custom field section you would add the field TEXT followed by |click2add which lets JReviews know this should be treated as a new option.

// Add the custom field data
// For select,radio,checkbox fields, use the field option values
// Checkbox and multiselect fields need to use array inputs
$params['data']['Field']['Listing'] = [
	'jr_othernames' => [
		'Desease 1|click2add',
		'Desease 2|click2add',
	]
];

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Please refer to our Privacy Policy.