Publish button code won't unpublish an item

1,390 views
Skip to first unread message

Christopher Reimer

unread,
Apr 4, 2012, 8:32:33 PM4/4/12
to joomla-de...@googlegroups.com
Greetings,

I'm trying to implement the publish button in my component table view. If the item is already unpublished (published = 0 in database), clicking the button will publish the item (published = 1 in database) and the publish message appears. When I try to unpublish the item, nothing happens.

Here's the controller code:

function publish()
   {
      // Check for request forgeries
      JRequest::checkToken() or jexit( 'Invalid Token' );

      $this->setRedirect( 'index.php?option=com_ebooksalesbutton' );

      // Initialize variables
      $db         =& JFactory::getDBO();
      $task      = JRequest::getCmd( 'task' );
      $publish   = ($task == 'publish');

      $query = 'UPDATE #__ebooksalesbutton'
      . ' SET published = ' . (int) $publish;
      $db->setQuery( $query );
      if (!$db->query()) {
         return JError::raiseWarning( 500, $item-getError() );
      }
      $this->setMessage( JText::sprintf( $publish ? 'Items published' : 'Items unpublished') );
   }

Here's the default.php code:

$checked = JHTML::_('grid.id', $i, $item->cf_id);
$published = JHTML::_('grid.published', $item->published, $i);

With this is in the table cell:

echo $published;

Any ideas as to what the problem is?

Thank you,

Chris Reimer

JSamir

unread,
Apr 4, 2012, 9:02:50 PM4/4/12
to joomla-de...@googlegroups.com
Have you looked at the core components how the publishing/uinpublishing works there? I dont remember that you need to write custom DB queries for that. There should be apropriate methods already there, if I remember correctly you only need the JHtml class in your default.php

Christopher Reimer

unread,
Apr 4, 2012, 9:31:44 PM4/4/12
to joomla-de...@googlegroups.com
I'm working off of whatever I can find on the Internet. If I removed the publish() function from the controller, the publish button stops working. I did look through the core component and couldn't find a corresponding publish() function. If there's a built in method, I don't know how to call it.

As for my current code, it works one way (0 -> 1) but not the other way (1->0). I'm still clueless about this.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/Lf2cWu_T7pYJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Christopher Reimer

unread,
Apr 4, 2012, 10:26:47 PM4/4/12
to joomla-de...@googlegroups.com
I need to add an WHERE statement for the DB call to prevent all the published fields being updated. Not sure how to pass the id number.

Is there a better way of doing this? If so, where is it documented?

Amy Stephen

unread,
Apr 4, 2012, 11:02:33 PM4/4/12
to joomla-de...@googlegroups.com
Christopher -

I think he means check out the core code.

For example, if you look at the backend weblinks controller - https://github.com/joomla/joomla-cms/blob/master/administrator/components/com_weblinks/controllers/weblinks.php or the backend com_content controller - https://github.com/joomla/joomla-cms/blob/master/administrator/components/com_content/controllers/articles.php

Neither have a publish method. But, both components have publish and unpublish actions. The parent class JControllerAdmin does have a publish method. https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/application/component/controlleradmin.php#L151

Since there is typically no reason to override or extend the parent class publish method, we can create Component Controllers without that method and simply let the parent method execute.

Hope that helps.
Amy

Christopher Reimer

unread,
Apr 5, 2012, 1:08:44 AM4/5/12
to joomla-de...@googlegroups.com
If I strip out all the "extra" code from the examples I found on the Internet, all I have left in the default.php is this:

$checked = JHTML::_('grid.id', $i, $item->cf_id);
$published = JHTML::_('grid.published', $item, $i);

However, clicking on the publish button doesn't work. Something is missing here. I'm gone through a lot of trouble to implement a simple on/off button. :/

Christopher Reimer

unread,
Apr 5, 2012, 1:25:51 AM4/5/12
to joomla-de...@googlegroups.com
When I hover the mouse over the publish button for my component, I see "Go to # on this page" in the status bar. When I hover over the button for the categories page, I see "Run script void(0);" in the status bar.

Looking at the core components, every example implements the publish button with sort. I just want to do a simple on/off button for now. Sort can come latter.

Christopher Reimer

unread,
Apr 5, 2012, 2:04:47 PM4/5/12
to joomla-de...@googlegroups.com
This keeps getting frustrating. The id number fell off the URL for the editing view even though JRoute is set correctly. *sigh*

I'm going polish off the plugin and create the database in phpMyAdmin. I need that sooner than being able to view and edit from the backend. Bells and whistles like a simple publish button will have to wait.

Amy Stephen

unread,
Apr 5, 2012, 8:18:40 PM4/5/12
to joomla-de...@googlegroups.com, AmySt...@gmail.com
Chris -

I am sorry, but it's hard to respond to what you might be looking at in the internet -- but in all honesty, your best best is to look at the current CMS source code.

Earlier, you had asked about the Controller method, which is why I shared links to com_weblinks and com_content Controller.

But, this question relates to the Administrator Manager layout, so in that case, take a look at com_content's grid layout:

https://github.com/joomla/joomla-cms/blob/master/administrator/components/com_content/views/articles/tmpl/default.php#L31

The button bar logic and build is accomplished within the view, so, here's an example for how that is done in com_content

https://github.com/joomla/joomla-cms/blob/master/administrator/components/com_content/views/articles/view.html.php#L73

In general, it can be a good idea to start with a copy of com_content and then modify it (checking every now and then to ensure it continues to work - or that you understand why it isn't working. ;-) )

It's confusing in the beginning. Although I have not found pulling my hair and crying to be helpful, I have done it none the less. But, keep in mind if you don't give up, you'll figure it out.

Stick to the source code -- it won't steer you wrong. Internet resources could be outdated or flat out wrong.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

elin

unread,
Apr 6, 2012, 1:17:39 AM4/6/12
to joomla-de...@googlegroups.com
I know it's frustrating at times, but seriously copy and paste are your friends. I think you might be looking at the state filter rather than the toggle.

This is all you typically need to have in your grid

<td align="center">
<?php echo JHtml::_('jgrid.published', $item->published, $i, 'contacts.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?>
</td>


if you have everything else set up (obviously change contacts to your component).  


Elin
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-dev-general@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

Christopher Reimer

unread,
Apr 6, 2012, 3:30:17 AM4/6/12
to joomla-de...@googlegroups.com
Getting closer but still not cigar. I'm assuming this is correct.

<?php echo JHtml::_('jgrid.published', $item->published, $i, 'ebooksalesbuttons.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?>

I had to set $canChange = 1 to get the code to work initially. Not sure what 'cb' is supposed to be. I added publish_up and publish_down to my table.

I get the void(0); message in status bar when my mouse hovers over the button. Clicking on the button causes the COM_EBOOKSALESBUTTON_N_ITEMS_PUBLISHED message to appear. Neither the button nor the database table is updated.

My code is up on github if anyone wants to take a look at it.




To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/27fybqamOsoJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.

Christopher Reimer

unread,
Apr 6, 2012, 3:43:43 AM4/6/12
to joomla-de...@googlegroups.com
Throw out the tutorial code and modify a copy of com_content? That looks somewhat promising.

To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/gr8lWiVAGXEJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.

Christopher Reimer

unread,
Apr 6, 2012, 4:44:04 PM4/6/12
to joomla-de...@googlegroups.com
I copied com_content from my current J25 installation, made all the name changes (I think), and started tracing through why it doesn't work. I'm getting this error message.

Fatal error: Call to undefined method JAdministrator::getParams() in /home/creimer/public_html/testj25/administrator/components/com_ebooksalesbutton/models/categories.php on line 55

Which is referring to this function:


protected function populateState()
{
$app = JFactory::getApplication();
$this->setState('filter.extension', $this->_extension);

// Get the parent id if defined.
$parentId = JRequest::getInt('id');
$this->setState('filter.parentId', $parentId);

$params = $app->getParams();
$this->setState('params', $params);

$this->setState('filter.published', 1);
$this->setState('filter.access', true);
}


Line 55 is $params = $app->getParams();

I'm not sure where to fix this error. The downside of using com_content as a template for my own component is that it has more functionality than what I need. I just need a simple form to enter data and a table to display data.

Sam Moffatt

unread,
Apr 6, 2012, 5:13:42 PM4/6/12
to joomla-de...@googlegroups.com
There are five main content extensions in the Joomla! core,
com_content is one but perhaps com_weblinks would be a closer fit to
what you need and would serve as a better template. All five of them
share essentially the same functionality and you can borrow from a
different extension if say weblinks doesn't do what you need it to do.

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.

elin

unread,
Apr 6, 2012, 5:17:39 PM4/6/12
to joomla-de...@googlegroups.com
com_content is not always the best to use as a model since the table is a core table, but ... so the problem is you don't have $params ... do you have params?


// Load the parameters.
$params = JComponentHelper::getParams('com_weblinks');
$this->setState('params', $params);

Is in com_weblinks ... I don't see anything setting $params in your model in your repo but I don't know what you have now. Still you might want to consider something along those lines.

Elin

Christopher Reimer

unread,
Apr 6, 2012, 5:48:41 PM4/6/12
to joomla-de...@googlegroups.com
Version 1 (repro) is based on the hello world tutorial. At one point it did have params, although I'm not sure if I still need it. I'm leaning towards creating a config table to store default values for the component and plugin.

Version 2 is a copy of com_content with all the name changes. Like the hello world tutorial, it should work once I get all the name changes straighten out. No clue about the params.

Version 3 will be a copy of com_weblinks. Maybe the third time around will work.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/bVNjABAnxXwJ.

Hannes Papenberg

unread,
Apr 6, 2012, 6:25:23 PM4/6/12
to joomla-de...@googlegroups.com
Am 06.04.2012 23:48, schrieb Christopher Reimer:
> Version 1 (repro) is based on the hello world tutorial. At one point
> it did have params, although I'm not sure if I still need it. I'm
> leaning towards creating a config table to store default values for
> the component and plugin.
Please don't. You can of course shoot yourself in the foot, but it is
still easier to simply use the code that is already provided, including
the fields to store the values in. Creating your own table for storing
config values instead of using the stuff provided by Joomla is pretty
stupid.

Hannes

Chris Davenport

unread,
Apr 6, 2012, 6:36:44 PM4/6/12
to joomla-de...@googlegroups.com
@Hannes.  Please watch your language and show a little more respect for others.  Thank you.

Chris.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.




--
Chris Davenport
Joomla Leadership Team - Production Working Group
Joomla Documentation Coordinator

Christopher Reimer

unread,
Apr 6, 2012, 6:48:20 PM4/6/12
to joomla-de...@googlegroups.com

Would it be possible to store my default values into the category params field?

My plugin queries the database with the category id (catid) to pull the needed records for processing. If I can't store the default values in a dedicated table, the category params field should be the most logical choice.

The hello world tutorial code has the params field set up on a per record basis. I don't need that.

Chris

Hannes Papenberg

unread,
Apr 6, 2012, 7:03:14 PM4/6/12
to joomla-de...@googlegroups.com
What is wrong with using the params field of jos_plugins? Set the values
in the plugin manager by using the fields in your plugins manifest file
and then retrieve the data via $this->params->get() in your plugin.

Hannes

Christopher Reimer

unread,
Apr 6, 2012, 7:27:08 PM4/6/12
to joomla-de...@googlegroups.com
I wasn't planning on having any fields in the plugin. All the changes should be done from the component side. (The plugin does allow overriding from within the tags.) Hence, I need someplace in the database to store the default values.

My component should store the default values in the params field for jos_plugins then?

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.

elin

unread,
Apr 6, 2012, 8:14:38 PM4/6/12
to joomla-de...@googlegroups.com
Plugins are in the #__extensions table in 2.5 (no you haven't lost a table)  and yes they have a built in place to store configuration just by virtue of being installed just as all components do. Let Joomla do the work for you instead of making up your own ways of doing things. 

I really think it would help if you stepped back and said what is it I am trying to do and how is it that Joomla does that. What you are trying to do does not sound unusual or non standard and you will be best off following the design patterns and taking advantage of the apis. 

Still I don't see what plugin parameters have to do with whether or not your component model is  getting the params for the component which may relate to the state toggle.

Puzzled,

Elin

> To post to this group, send an email to joomla-dev-general@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-general+unsub...@googlegroups.com.

Christopher Reimer

unread,
Apr 6, 2012, 9:03:14 PM4/6/12
to joomla-de...@googlegroups.com
I mentioned this before. I have a set of HTML buttons for various ebook retailers with different links that I want to put on each of my ebook page. I can do copy and paste, but that would be a PITA since I currently have 30 pages and I'm adding 24+ pages per year.


The plugin gets a category id (catid) to query the database for the URL links and assembles the HTML. This works really well.

The component is where I enter the category (ebook title) and the associated URL links. This where I'm having trouble.

The connection between the component and the plugin is the graphic files I used for the buttons. The graphic files are installed by the component into the media folder. I could hard code the path into the component and the plugin. Or I could store the path in the database and let that be a changeable from the component.

I'm trying to create a generalized extension that anyone can use. I'm sure there are other authors using Joomla to see their ebooks. The easy way - I'm doing this over the weekend - is to polish off the plugin and use phpMyAdmin to set up the database.

Prior to this I have only done modules and even those were more PHP than Joomla API.

To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/iCfOBuNBR34J.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.

elin

unread,
Apr 6, 2012, 9:32:22 PM4/6/12
to joomla-de...@googlegroups.com
Ok so how does the desire to have the buttons relate to the desire to have a publish/unpublish toggle?

I'm not understanding how we got from point A to point B.

Elin

Christopher Reimer

unread,
Apr 6, 2012, 9:52:00 PM4/6/12
to joomla-de...@googlegroups.com
The publish/unpublish toggle has to do with the default table view. If the URL link is published, than the plugin will create a button. If the URL link is unpublished, the plugin won't create a button.

Maybe I'm being a glutton for punishment in trying to get my component to match the default behavior of the Joomla backend user interface. Deleting the links was something I could've done with the hello world tutorial code.

To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/yesCT6iReEEJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.

elin

unread,
Apr 6, 2012, 11:02:29 PM4/6/12
to joomla-de...@googlegroups.com
Being published or unpublished and what that does to your plugin has nothing to do with whether the toggle works ... There are 3 different ways at least to change the state of an item (toggle, use the tool bar, edit + change the state field, bulk processing if you have implemented it. You started a post about a specific topic, whether the toggle works ...  but it seems as though that is not actually what you want to fix. Is that right?

This is my advice. Pick ONE problem to solve. Do no get dragged down a dozen rabbit holes. If you want to solve the button problem, focus on that. 

Write out the behavior you want. 
Determine the best way to get the behavior.
Focus solely on implementing that.

When you are done go on to the next problem.



Elin

Christopher Reimer

unread,
Apr 6, 2012, 11:22:46 PM4/6/12
to joomla-de...@googlegroups.com
From my perspective the rabbit hole is this list.

I was told to remove all the code I found on the Internet and copy/paste the toggle code by itself, which didn't quite work as the button and the database wasn't updated.

I was told to use com_content as a template, which introduced another problem with the params not being set.

I was told to use com_weblinks as a template, which I set aside for another day. The code structure looks similar to the Hello World tutorial code structure, something I might be able to jump into without too much problem.

A discussion got started along the way about setting the params field.

Next I was told explain what I was trying to achieve with my component/plugin extension.

Meanwhile, as all this carrying on went back and forth in the list, I'm spit polishing my plugin so I can do some useful work over the weekend. There's another problem but that's a PHP issue. ;)


To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/7oQs5mfIJ8YJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages