Topic: One click publishing, approval, and frontpaged item

This will probably be useful only for those that wish to build their own backend for their website and want a quick one click option for publishing an article or whatever else from the content view page.  I have built a CMS for my site that I am still working on and use this bit of code to do just that.

My main content section of the backend is just a table that prints off the id, title, cateory of a content item, etc, kinda similar to Joomla's administration.  In the table, is an image which shows whether an item is published, admin approved ( I have journalists and even special users that can post content ), and an option to make an article a frontpage item.

Now, onto some code ( note, some of this code I need to trim on ).  First, here are the functions I registered...

$objAjax->registerFunction( array( 'makeFrontpage', &$content, 'makeFrontpage' ) );
$objAjax->registerFunction( array( 'unmakeFrontpage', &$content, 'unmakeFrontpage' ) );
$objAjax->registerFunction( array( 'Approve', &$content, 'Approve' ) );
$objAjax->registerFunction( array( 'unApprove', &$content, 'unapprove' ) );
$objAjax->registerFunction( array( 'Publish', &$content, 'Publish' ) );
$objAjax->registerFunction( array( 'unPublish', &$content, 'unPublish' ) );

All the functions sit in a seperate class file for handling content, which is why it may seem registered alittle differently.  Next, further down in the code after my database query, I had to determine what image will be shown by default to represent the publishing, approval, and frontpage states...

 // Frontpage and approval image checks
        $fpimg = ( $row->frontpage == '1' ) ? '<a href="javascript:void(0);" onclick="xajax_unmakeFrontpage( ' .$row->id. ' );"><img src="images/icons/frontpage_true.png" alt="Frontpage Item" border="0" /></a>' : '<a href="javascript:void(0);" onclick="xajax_makeFrontpage( ' .$row->id. ' );"><img src="images/icons/frontpage_false.png" alt="Not Frontpage Item" border="0" /></a>';
        $apimg = ( $row->approved == '1' ) ? '<a href="javascript:void(0);" onclick="xajax_unApprove( ' .$row->id. ' );"><img src="images/icons/frontpage_true.png" alt="Approved Item" border="0" /></a>' : '<a href="javascript:void(0);" onclick="xajax_Approve( ' .$row->id. ' );"><img src="images/icons/frontpage_false.png" alt="Not Approved Item" border="0" /></a>';
        $pimg = ( $row->published == '1' ) ? '<a href="javascript:void(0);" onclick="xajax_unPublish( ' .$row->id. ' );"><img src="images/icons/frontpage_true.png" alt="Published Item" border="0" /></a>' : '<a href="javascript:void(0);" onclick="xajax_Publish( ' .$row->id. ' );"><img src="images/icons/frontpage_false.png" alt="Unpublished Item" border="0" /></a>';

In the column for publishing, etc I would print off the image ( echo $fpimg, etc ), which contains the onclick code to execute our XAJAX processes.  One important step!!  In the table, I needed a unique id so that I can return a new value after XAJAX executes.

<td><span id="p_<?php echo $row->id; ?>">

Using a span, I simply set and id of p_ and the items id, where p_ represents publishing, a_represented approval, and f_represented frontpage columns, respectively.

Now here are the functions I used.  Note, these could probably be trimmed down a bit to use one function publishing, approval, and frontpage rather than two per.  I just wanted to build the basics first and return to trim things the best I could... but here it is...

/**
    * Make content item frontpage item
    * @param integer ID to effect
    */
    public function makeFrontpage( $id ) {
            global $database;
            
            // Check out id value
            $id = intval( $id );
            
            $query   = "UPDATE " . $database->prefix . "content"
                 . "\n SET frontpage = '1'"
             . "\n WHERE id  = '" .$id. "' LIMIT 1";

            $execute = $database->query( $query );

        // Instantiate object response now for redirect
        $objResponse = new xajaxResponse();

        if( $execute ) {
                $image = '<a href="javascript:void(0);" onclick="xajax_unmakeFrontpage( ' .$id. ' );"><img src="images/icons/frontpage_true.png" alt="Frontpage Item" border="0" /></a>';
            } else {
                $image = '<a href="javascript:void(0);" onclick="xajax_makeFrontpage( ' .$id. ' );"><img src="images/icons/frontpage_false.png" alt="Not Frontpage Item" border="0" /></a>';
            }
        $objResponse->addAssign( 'fp_'.$id, 'innerHTML', $image );
        return $objResponse->getXML();
    }
    /**
    * Make content item non frontpage item
    * @param integer ID to effect
    */
    public function unmakeFrontpage( $id ) {
            global $database;

            // Clean input
            $id = intval( $id );
            
            $query   = "UPDATE " . $database->prefix . "content"
                 . "\n SET frontpage = '0'"
             . "\n WHERE id  = '" .$id. "' LIMIT 1";
            $execute = $database->query( $query );

        // Instantiate object response now for redirect
        $objResponse = new xajaxResponse();

        if( $execute ) {
                $image = '<a href="javascript:void(0);" onclick="xajax_makeFrontpage( ' .$id. ' );"><img src="images/icons/frontpage_false.png" alt="Not Frontpage Item" border="0" /></a>';
            } else {
                $image = '<a href="javascript:void(0);" onclick="xajax_unmakeFrontpage( ' .$id. ' );"><img src="images/icons/frontpage_true.png" alt="Frontpage Item" border="0" /></a>';
            }
        $objResponse->addAssign( 'fp_'.$id, 'innerHTML', $image );
        return $objResponse->getXML();
    }
    /**
    * Make content item frontpage item
    * @param integer ID to effect
    */
    public function Publish( $id ) {
            global $database;
            
            // Clean input
            $id = intval( $id );
            
            $query   = "UPDATE " . $database->prefix . "content"
                 . "\n SET published = '1'"
             . "\n WHERE id  = '" .$id. "' LIMIT 1";
            $execute = $database->query( $query );

        // Instantiate object response now for redirect
        $objResponse = new xajaxResponse();

        if( $execute ) {
                $image = '<a href="javascript:void(0);" onclick="xajax_unPublish( ' .$id. ' );"><img src="images/icons/frontpage_true.png" alt="Published Item" border="0" /></a>';
            } else {
                $image = '<a href="javascript:void(0);" onclick="xajax_Publish( ' .$id. ' );"><img src="images/icons/frontpage_false.png" alt="Unpublished Item" border="0" /></a>';
            }
        $objResponse->addAssign( 'p_'.$id, 'innerHTML', $image );
        return $objResponse->getXML();
    }
    /**
    * Make content item non frontpage item
    * @param integer ID to effect
    */
    public function unPublish( $id ) {
            global $database;
            
            // Clean input
            $id = intval( $id );
            
            $query   = "UPDATE " . $database->prefix . "content"
                 . "\n SET published = '0'"
             . "\n WHERE id  = '" .$id. "' LIMIT 1";
            $execute = $database->query( $query );

        // Instantiate object response now for redirect
        $objResponse = new xajaxResponse();

        if( $execute ) {
                $image = '<a href="javascript:void(0);" onclick="xajax_Publish( ' .$id. ' );"><img src="images/icons/frontpage_false.png" alt="Unpublished Item" border="0" /></a>';
            } else {
                $image = '<a href="javascript:void(0);" onclick="xajax_unPublish( ' .$id. ' );"><img src="images/icons/frontpage_true.png" alt="Published Item" border="0" /></a>';
            }
        $objResponse->addAssign( 'p_'.$id, 'innerHTML', $image );
        return $objResponse->getXML();
    }
    /**
    * Make content item frontpage item
    * @param integer ID to effect
    */
    public function Approve( $id ) {
            global $database;
            
            // Clean id
            $id = intval( $id );
            
            $query   = "UPDATE " . $database->prefix . "content"
                 . "\n SET approved = '1'"
             . "\n WHERE id  = '" .$id. "' LIMIT 1";
            $execute = $database->query( $query );

        // Instantiate object response now for redirect
        $objResponse = new xajaxResponse();

        if( $execute ) {
                $image = '<a href="javascript:void(0);" onclick="xajax_unApprove( ' .$id. ' );"><img src="images/icons/frontpage_true.png" alt="Approved Item" border="0" /></a>';
            } else {
                $image = '<a href="javascript:void(0);" onclick="xajax_Approve( ' .$id. ' );"><img src="images/icons/frontpage_false.png" alt="Not Approved Item" border="0" /></a>';
            }
        $objResponse->addAssign( 'ap_'.$id, 'innerHTML', $image );
        return $objResponse->getXML();
    }
    /**
    * Make content item non frontpage item
    * @param integer ID to effect
    */
    public function unApprove( $id ) {
            global $database;
            
            // Clean id
            $id = intval( $id );
            
            $query   = "UPDATE " . $database->prefix . "content"
                 . "\n SET approved = '0'"
             . "\n WHERE id  = '" .$id. "' LIMIT 1";
            $execute = $database->query( $query );

        // Instantiate object response now for redirect
        $objResponse = new xajaxResponse();

        if( $execute ) {
                $image = '<a href="javascript:void(0);" onclick="xajax_Approve( ' .$id. ' );"><img src="images/icons/frontpage_false.png" alt="Not Approved Item" border="0" /></a>';
            } else {
                $image = '<a href="javascript:void(0);" onclick="xajax_unApprove( ' .$id. ' );"><img src="images/icons/frontpage_true.png" alt="Approved Item" border="0" /></a>';
            }
        $objResponse->addAssign( 'ap_'.$id, 'innerHTML', $image );
        return $objResponse->getXML();
    }

So basically what we have here is a table that shows our content data.  If an item is published, and image showing it is published is shown which a link for XAJAX to unpublish the item.  If it is successfull, it will return the unpublished image linked to make it published, and so forth.

I have tested and it seems to work really fast.  The if/else to determine the image could probably be put into a ternary as well, which would cut down a bit on code.  Again, not the smoothest thing, just an example of one click toggle controls here.

Note - I used 0.2 for this example.