Custom status button in joomla component

Joomla com_content

has a small toggle button for the "publish" article status for posting or posting articles. So, I want my component to also have a toggle button to approve or reject users.

Now, I want some expert advice on how to do this. I got through com_content

, but I really don't understand how to start. I cannot understand the approach com_content

and the code because I am not coding according to Joomla 2.5.

How do I start with this?

+3


source to share


2 answers


I did it myself. let me share my experience for those who need it in the future. my field or database field has been approved and its value is 0 initially (this means the admin has not approved the entry)

in my default layout / page I have code as below for toggle button:

    <?php
    $k = 0;
    for ($i=0, $n=count( $this->items ); $i < $n; $i++)
    {
       $row = &$this->items[$i];
    ..................
    ..................
    ?>
    ..................

    <td align="center">
       <?php echo JHtml::_('job.approve', $row->approved, $i); ?>
    </td>

      

Note that I have $ row-> approved which is my field from db. then I have a job.approve for which I have created a job.php file and placed in the helper directory. code for job.php:

<?php
// no direct access
defined('_JEXEC') or die;

/**
 * @package     Joomla.Administrator
 * @subpackage  com_content
 */
abstract class JHtmlJob
{
    /**
     * @param   int $value  The state value
     * @param   int $i
     */
    static function approve($value = 0, $i)
    {
        // Array of image, task, title, action
        $states = array(
            0   => array('disabled.png',    'approve',  'Unapproved',   'Toggle to approve'),
            1   => array('tick.png',        'unapprove',    'Approved',     'Toggle to unapprove'),
        );
        $state  = JArrayHelper::getValue($states, (int) $value, $states[1]);
        $html   = JHtml::_('image', 'admin/'.$state[0], JText::_($state[2]), NULL, true);
        //if ($canChange) {
            $html   = '<a href="#" onclick="return listItemTask(\'cb'.$i.'\',\''.$state[1].'\')" title="'.JText::_($state[3]).'">'
                    . $html.'</a>';
        //}

        return $html;
    }
}

      

Then I registered two tasks in the controller as approved and not approved along with the function approval:



public function __construct($config = array())
    {
        parent::__construct($config);

        $this->registerTask('unapprove', 'approve');
    }

    /**
     * Method to toggle the featured setting of a list of articles.
     *
     * @return  void
     * @since   1.6
     */
    function approve()
    {
        // Initialise variables.
        $user   = JFactory::getUser();
        $ids    = JRequest::getVar('cid', array(), '', 'array');
        $values = array('approve' => 1, 'unapprove' => 0);
        $task   = $this->getTask();
        $value  = JArrayHelper::getValue($values, $task, 0, 'int');

        if (empty($ids)) {
            JError::raiseWarning(500, JText::_('JERROR_NO_ITEMS_SELECTED'));
        }
        else {
            // Get the model.
            $model = $this->getModel('jobs');

            // Publish the items.
            if (!$model->approve($ids, $value)) {
                JError::raiseWarning(500, $model->getError());
            }
        }

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option'));
        $this->setRedirect($redirectTo);
    }

      

Subsequently, I added the following function in the model to update the value to 0 or 1.

function approve($cid, $publish) {

         if (count( $cid ))
         {
             JArrayHelper::toInteger($cid);
             $cids = implode( ',', $cid );
             $query = 'UPDATE #__tbljobs'
                   . ' SET approved = '.(int) $publish
                   . ' WHERE id IN ( '.$cids.' )';
                  $this->_db->setQuery( $query );
                if (!$this->_db->query()) {
                    $this->setError($this->_db->getErrorMsg());
                    return false;
                 }
           }
           return true;
 }

      

Don't forget to include your job.php file in your view / html.php file as shown below:

<?php
defined('_JEXEC') or die('Restricted Access');
jimport('joomla.application.component.view');

require_once JPATH_COMPONENT .'/helpers/job.php';

Class JobsViewListJobs extends JView
{

      

And remember that I am not using JForm, but my code is joomla 1.7 style. But I am following MVC architecture. So, I'm not sure if my method will work for people who code in joomla 1.7 style and above.

+5


source


You can use this to create a publish button More details -

JHtml::_('jgrid.published', $item->state, $i, 'articles.', $canChange);

      

Or this html -

<?php if($item->approve){?>
<td class="center">
    <a class="jgrid hasTip" href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i?>','items.disapprove')" title=""><span class="state publish"><span class="text">Disapprove</span></span></a>
</td>
<?php }else{?>
<td class="center">
    <a class="jgrid hasTip" href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i?>','items.approve')" title=""><span class="state unpublish"><span class="text">Approve</span></span></a>
</td>
<?php }?>

      

In items.approve

and items.disapprove

, items is controller

and approve and disapprove

- the task of the element controller. `



In your controller add this function -

public function __construct($config = array()){
                parent::__construct($config);
                $this->registerTask('unapproved', 'approved');
}
function approved() { 
    $ids = JRequest::getVar('cid', array(), '', 'array'); 
    JArrayHelper::toInteger($ids );
    $cids = implode( ',', $ids); 
    $values = array('approved' => 1, 'unapproved' => 0); 
    $task = $this->getTask(); 
    $value = JArrayHelper::getValue($values, $task, 0, 'int');      
    $db =& JFactory::getDBO();      
    $query = 'UPDATE #__tbljobs' . ' SET approved = '.(int) $value . ' WHERE id IN ( '.$cids.' )';
    $db->setQuery($query);
    $result = $db->query();
    $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&task=display'); 
    $this->setRedirect($redirectTo); 
} 

      

Read This - Joomla 2.5 Extends the jgrid.published Column in a Custom Component

Hope this helps.

+2


source







All Articles