phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Joomla - Component: Updating publish state

Tag: joomla, cms, Joomla Component   Category: PHP Framework
post: 06 Nov 2007 read: 4,055


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

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

2 Responses to Joomla - Component: Updating publish state:

Hi,

Thank you very much for this tutorial, but i have some notices:

in this page http://www.phpeveryday.com/articles/Joomla-Component-Deleting-Data-P49.html

- at the first stape you must add :

case 'publish';
changeHello(1);
break;

case 'unpublish';
changeHello(0);
break;


not

switch($task){

case 'remove':
removeHello();
break;

default:
showHello();
break;

}

Me again, i think that you put the right "code" but the Javascripts codes that show the bloc of "code" hide the right "code", if you try to escape page before total loading you will get the right "code"

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


619
posting