1

I'm extending my controller's publishing method for a task called "setup".

This is the extended method:

/**
 * publish function.
 * 
 * @access public
 * @return void
 */
public function publish()
{
    $app      = JFactory::getApplication();
    $jinput   = JFactory::getApplication()->input;
    $ids      = $jinput->get('cid', '', 'array');
    $task     = $this->getTask();
    $date     = JFactory::getDate();            

    $modelitem  = $this->getModel('Item');
    $tableitem  = JTable::getInstance('Item','BestiaTable');      

    foreach($ids as $id)
    {
        $tableitem->load($id);

        switch($task)
        {
            case 'publish'  : $tableitem->autorenew = 1;  
                              break;    

            case 'unpublish': $tableitem->autorenew = 1;
                              break;

            case 'archive'  : $tableitem->autorenew    = 0;
                              $tableitem->terminatedon = $date->toSql();     
                              break;

            case 'trash'    : $tableitem->autorenew    = 0;
                              $tableitem->terminatedon = $date->toSql();
                              $tableitem->end          = $date->toSql();
                              $tableitem->overwriteend = 1;

            case 'setup'    : $SetupHelper             = SetupHelper::getInstance();
                              $setupitem               = $SetupHelper->setupItem($id);
                              if ($setupitem)
                              {

                              }
                              else
                              {
                                  $app->enqueueMessage(implode(", ", $SetupHelper->getErrors()), 'warning');          
                              }
                              break;
            break;
        }

        $tableitem->store();            
    }

    $publish = parent::publish();
    $this->setRedirect('index.php?option=com_bestia&view=items', false);

    return true;
}

Okay - publishing and unpublishing works fine, but the setup-task which is started by a toolbar-button like this:

JToolbarHelper::custom('items.setup', 'cogs', '', 'Setup item', true);

Always changes the item's state to 0 instead of -1 (which is the state for "wait for setup" or 1 (which is the state for "setup okay").

How can I change this behavior and tell the method which state to use?

  • Just to let you know, you're missing a break; at the end of case 'trash', and you have 2 break;'s at the end of case 'setup' – Lodder Nov 4 '15 at 16:18
3

You are calling parent::publish(). This method has an array which maps tasks to states (line 190):

$data = array('publish' => 1, 'unpublish' => 0, 'archive' => 2, 'trash' => -2, 'report' => -3);

Then it gets the task (next line):

$task = $this->getTask();

Which is "setup" in your case. After that it tries to read the value of the task in the array:

$value = JArrayHelper::getValue($data, $task, 0, 'int');

But it won't find "setup" in that array. So the value is 0. That explains why your state changes to 0.

I wouldn't call parent::publish() if your task is "setup". You could avoid that if you wrap that call like this:

 if($task != "setup")
 {
     $publish                             =   parent::publish();
 }
 else
 {
    // Check the code from JControllerAdmin how to set models state 
    // e.g. $modelitem->publish($id, -1); set a nice message and how to redirect
    // back to your list
 }
  • Thanks a lot - but if I use $modelitem->publish($id, -1); ... if($task != 'setup') { $publish = parent::publish(); } The state does not change to -1 :-( – MyFault Nov 5 '15 at 12:19
  • If the publish with -1 doesn't work probably have to write your own database query which updates the state. With default publish method from JModelAdmin it should work but I don't know if you overwrite something from there. The query is not too complicated. If you have problems, open a new question please. – fruppel Nov 5 '15 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.