83 Posts
squareweb
5 years ago
Topic

Hi,

I want to toggle a status-field by Ajax-call. Is there any "right" e.g. preferred approach. Of course I can write my own solution but this introduces security risks...

Is there a Seblod way? I just need to update a status field in a cck_table...

Thanks in advance.

Get a VIP membership
4229 Posts
Kadministrator
5 years ago
1
Level 1

Basically - my advice would be to create standalone script, possibly with the help of Joomla classes (e.g. https://docs.joomla.org/Using_Joomla_Ajax_Interface ) that will be called by ajax and then store changes using JCckContent classes. Of course you need to take care of input filtering and other possible issues (e.g. check form tokens), exact solutions depend on exactly what are you doing as sometimes it is better to use beforeRender or afterStore events.

83 Posts
squareweb
5 years ago
0
Level 2

Klas, thanks again for responding.

I've googled and indeed created a simple solution by using the Joomla interface.

I'll post a short tutorial today.

Thanks again.

83 Posts
squareweb
5 years ago
0
Level 1

This is a small (quick and dirty) tutorial. which by no means tends to be the only solution to handle this use case.

The case is: toggling a status-field (cck_table-field) status can be 0 or 1. Also the checkbox is replaced by image in front-end with html-typography (list-mode 2).

2 parts:

1. javascript / jQuery

2. php-server

Part 1.

=====

1a. add a class to your seblod-toggle-field. In this way we can identify and capture the clicked status with jQuery.

1b. add (code-pack) javascript field type which contains jQuery function (see code below) to handle the toggle (click) on image and to send AJAX-call to server.

Part 2.

=====

2a. In backend-seb-admin list-view use the html-typography (2) to change the seb-status-field to custom checkbox- image (see code below). You may use an image with “-1” or “-0” contained in the imag-name f.i. “my image-0.png” for statusfield-value 0 and “my image-1.png” for statusfield-value 1;

2b. create joomla-based php-page which will reflect toggle-action in database table. This page will include joomla default application-classes and joomla page-security (in header). Change the path to this page withinin the jQuery code.

Below you’ll find the example code and som images to clarify this solution.

===========================

html-code used in html-typography:

===========================

<a href='*value*' data-key="$cck->getValue('td_id')"><img src="/images/sebsys/icon-check-circel-*value*png" alt="wissel" title="wissel"></a>

===========================

JS-code in the js(code-pack)-field:

===========================

jQuery('a.sqwToggle_tdStatus').click(function (event) {

event.preventDefault();

var fout = 0;

// alert( $('img', this).attr('src'));

$.ajax({

url: '/sebdev/sebToggle_td_status.php', // php-page to call - put your path to file here

type: 'POST',

data: {dk: jQuery(this).attr('data-key')},

success: function(data) {

//alert("succes:"+data);

fout=5;

},

error: function(e) { // handle error your way

//called when there is an error

//console.log(e.message);

alert("error"+e);

fout=1;

}

});

//figure out current img-src and change 0 / 1 within

if(fout == 0){

// alert( "fout: " + fout + " -- " + $('img', this).attr('src'));

var cur_src = $('img', this).attr('src');

var new_src = '';

if(cur_src.indexOf("-0") == -1){

new_src = cur_src.replace("-1", "-0");

} else {

new_src = cur_src.replace("-0", "-1");

}

$('img', this).attr('src', new_src); // set new src-path to image

}

});

===========================

PHP-code used in joomla-php-page:

===========================

define('_JEXEC', 1);
// sent by 'post' with jQuery AJAX-call$dk = $_POST['dk'];
// include required frameworksif (stristr($_SERVER['SERVER_SOFTWARE'], 'win32' )){
define('JPATH_BASE', realpath(dirname(__FILE__).'\..' ));}else define('JPATH_BASE', realpath(dirname(__FILE__).'/..' ));
define('DS', DIRECTORY_SEPARATOR );
// including the main joomla filesrequire_once (JPATH_BASE.DS.'includes'.DS.'defines.php' );require_once (JPATH_BASE.DS.'includes'.DS.'framework.php' );

// Creating an app instance$app = JFactory::getApplication('site');
$app->initialise();
jimport('joomla.user.user' );
jimport('joomla.user.helper' );
$db =& JFactory::getDBO();$query = $db->getQuery(true);
// Fields to update - toggle field value.$fields = array($db->quoteName('td_status') . ' = ' . 'IF(td_status=1, 0, 1)');
// Conditions for which records should be updated.$conditions = array($db->quoteName('id') . ' = ' . $dk);
$query->update($db->quoteName('#__cck_store_form_trajectdeel'))->set($fields)->where($conditions);$db->setQuery($query);
// return value$retval = 0;$result = $db->execute();
if (!$result){$retval = 9;}
echo $retval;
?>












Get a VIP membership