Ever wanted to have a column/field like published in
articles for your own component? Well here is how you can make one.
If you go to com_content and see how it is done there you
will see that for published it uses the following line of code in
views/articles/tmpl/default.php
<?php echo JHtml::_('jgrid.published', $item->state, $i, 'articles.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?>
Simplying it you can use it as
<?php echo JHtml::_('jgrid.published', $item->state, $i, 'articles.'); ?>
Which is
<?php echo JHtml::_('jgrid.published', $item->yourFieldName, $i, 'classPrefix.'); ?>
If you just put this one line you will be able to see that
your Boolean field will start showing correctly, and especially if you field
name is ‘published’ then you are done and you can click on it to toggle between
publish and un publish, however if your field name is something other than
published e.g. approved or available then you have some work to do.
First you will need to create a new class in com_componentName/helpers/html/className.php
This will be a JFieldClassName class having code like this
<?php
defined('_JEXEC') or die;
abstract class JHtmlClassName
{
static function approved($value = 0, $i)
{
$states = array(0=> array('disabled.png','tableName.approved','’,'Toggle to approve'),
1=> array('tick.png', 'tableName.unapproved', ', 'Toggle to unapprove'), );
$state = JArrayHelper::getValue($states, (int) $value, $states[1]);
$html = JHtml::_('image', 'admin/'.$state[0], JText::_($state[2]), NULL, true); $html = '<a href="#" onclick="return listItemTask(\'cb'.$i.'\',\''.$state[1].'\')" title="'.JText::_($state[3]).'">'. $html.'</a>';
}
return $html;
}
} ?>
Now to call this you will have to first register it for both
approved/unapproved in controller’s contruct
com_componentName/controllers/classNames.php
public function __construct($config = array())
{
parent::__construct($config);
$this->registerTask('unapproved', 'approved');
}
com_componentName/controllers/classNames.php
public function __construct($config = array())
{
parent::__construct($config);
$this->registerTask('unapproved', 'approved');
}
Followed by the function which will call the model to update
the status:
function approved()
{
$ids = JRequest::getVar('cid', array(), '', 'array');
$values = array('approved' => 1, 'unapproved' => 0);
$task = $this->getTask();
$value = JArrayHelper::getValue($values, $task, 0, 'int');
$model = $this->getModel();
if (!$model->approved($ids, $value)) {
JError::raiseWarning(500, $model->getError());
}
$this->setRedirect(url to get you back to the same page);
}
Now its all up are ready and you can just create a function
in your respective model which takes the value (which will be 0 or 1) and id
and update the status , and finally you can use it by writing the following
line in default.php
<?php echo JHtml::_('className.approved', $item->approved, $i, 'tableName.'); ?>
You will need to include the html helper file you created
first by putting the following like at the top of default.php
thanks
ReplyDeletewhat is that tableName?????
ReplyDeleteThis is great I've just started to implement on one of my components it's showing correctly publish and unpublish icons but I wonder how could i use the function that changes the state from publish to unpublish.
ReplyDeletehere's my example function
function approveCampaign(){
$db = &JFactory::getDBO();
$query = "UPDATE six1n_jg_campaigns SET published ='1' WHERE id='6'";
$db->setQuery($query);
$db->query();
}
Best regards
Is there a way to avoid using a static tableName, but using the current controller as tableName?
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletebye this code task published and unpublished not pass to the hidden field
ReplyDeleteplease reply me about this