Joomla - Component: Updating publish state


You can change publish state, publish or unpublish. Follow this step to make the toggle live.

  1. Open "admin.hello.php" file, add following code to switch():
    switch($task){
    
      case 'remove':
        removeHello();
        break;
    		
      case 'publish':
        changeHello(1 );
        break;
    
      case 'unpublish':
        changeHello(0 );
        break;				
    		
      default:
        showHello();
        break;
    
    }
    
  2. Add changeHello() function:
    function changeHello( $state=0 )
    {
      global $mainframe;
    
      // Initialize variables
      $db 	=& JFactory::getDBO();
    
      // define variable $cid from GET
      $cid = JRequest::getVar( 'cid' , array() , '' , 'array' );	
      JArrayHelper::toInteger($cid);
    
      // Check there is/are item that will be changed. 
      //If not, show the error.
      if (count( $cid ) < 1) {
        $action = $state ? 'publish' : 'unpublish';
        JError::raiseError(500, JText::_( 'Select an item 
        to' .$action, true ) );
      }
    
      // Prepare sql statement, if cid more than one, 
      // it will be "cid1, cid2, cid3, ..."
      $cids = implode( ',', $cid );
    
      $query = 'UPDATE #__hello'
      . ' SET published = ' . (int) $state
      . ' WHERE id IN ( '. $cids .' )'
      ;
      // Execute query
      $db->setQuery( $query );
      if (!$db->query()) {
        JError::raiseError(500, $db->getErrorMsg() );
      }
    
      if (count( $cid ) == 1) {
        $row =& JTable::getInstance('hello', 'Table');
        $row->checkin( intval( $cid[0] ) );
      }
    
      // After all, redirect to front page
      $mainframe->redirect( 'index.php?option=com_hello' );
    }
    


Series this article:
Joomla - Component: Create Hello World Component on Front End
Joomla - Component: Create Hello World Component on Back End
Joomla - Component: Registering Your Component in database
Joomla - Component: Create Component Link for Front End
Joomla - Component: Creating Toolbars
Joomla - Component: Available Toolbar Buttons
Joomla - Component: Creating the Database Table
Joomla - Component: Creating Form Input Data at Back End
Joomla - Component: Saving Data to Database
Joomla - Component: Creating a List Screen
Joomla - Component: Creating Advance List Screen
Joomla - Component: Editing Data
Joomla - Component: Deleting Data
Joomla - Component: Updating publish state
Joomla - Component: Hello From Database at Front End
Joomla - Component: a Hello page
Joomla - Component: Generating Friendly Links
Joomla - Component: Adding Comment ( Create Table )
Joomla - Component: Adding Comment (Create Form)
Joomla - Component: Adding Comment ( Insert Data )
Joomla - Component: Adding Comment ( Displaying Comments )
Joomla: Fast Road to Understand Component Programming

Bookmark and Share Tag: joomla, cms, Joomla Component Category: PHP Framework Post : November 06th 2007 Read: 52,902

advertisements


blog comments powered by Disqus