Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom status button in joomla component

The Joomla com_content has small toggle button for the article's status "publish" to publish or unpublish the articles. So, I want to have same type of toggle button in my component also to approve or disapprove users.

Now, I want some advice from experts on how to go about. I have gone through com_content but I don't really understand that how should i begin. I can't understand com_content approach and code because I am not coding in line with Joomla 2.5.

How should I start with this ?

like image 672
Raaman Rai Avatar asked Feb 18 '23 19:02

Raaman Rai


1 Answers

i made it work on my own. let me share the experience for those who will need it in future. my table field or database field is approved and its value is 0 initially (which means the record is unapproved by the admin)

in my layout/default page i have the code as below for the 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've $row->approved which is my field from db. then i've job.approve for which i have created a job.php file and placed in helpers directory. the code for job.php is:

<?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've registered two tasks in controller as approve and unapprove along with approve function:

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);
    }

Thereafter, i've added the following function in 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;
 }

Please don't forget to include the job.php file in your view/view.html.php file as 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 i am not using JForm nor my code is in joomla 1.7 style. But i am following the MVC architecture. So, i am not sure if my method will work for people who are coding in joomla 1.7 and above style.

like image 60
Raaman Rai Avatar answered Feb 28 '23 03:02

Raaman Rai