phpajaxjoomla-extensionsjoomla1.6

Module development trouble with saving module params using ajax


I am really new to Joomla and 1.6 is also very new it seems so I am having a bit of trouble to save data into the module params field in the module back end.

Let me explain a bit better.

I have a small contacts manager that I want people to subscribe to using a joomla module in Joomla 1.6. Now the module is created and everything works fine. I even got it right to create a custom field in the module back end that calls my API and populates the dropdown box with the data needed.

I want to be able to save the selection as a module param to easily access it in my template. This seems to be very hard to do because I cannot find any documentation on it whatsoever.

Basically my process is as follows.

  1. I go to the module manager in Joomla admin.
  2. I select my installed module and let it open.
  3. The Code runs to populate the dropdown box with an amount of list names a contact can subscribe to.
  4. The kicker - I want to be able to select a list name and save an ID into the module params field in the Joomla DB onchange with an ajax request -

I get the onchange event to work and even run the script with the post request but in the PHP file I use to handle the post and save the data I cannot get the DB instance to do any operations on it. Thats the explanation, here's the code :

custom field in the module backend

defined('_JEXEC') or die('Direct Access to this location is not allowed.');
jimport('joomla.html.html');
//import the necessary class definition for formfield
jimport('joomla.form.formfield');

// Include API utility file
require_once(dirname(__FILE__) . '/../lib/pmailer_subscription_api.php');

/**
 * Defines the JFormFieldLists class for the pMailer subscription module for
 * Joomla CMS version 1.6 to get the lists provided the API key.
 *
 * @category  Joomla
 * @package   Modules
 * @copyright 2011 Prefix Technologies Pty (Ltd) (http://www.prefix.co.za/)
 * @link      http://www.pmailer.co.za/
 */

class JFormFieldLists extends JFormField
{
    /**
     * The form field type.
     *
     * @var  string
     * @since 1.6
     */
    protected $type = 'lists'; //the form field type

    /**
     * Method to retrieve the lists that resides in your pMailer account using
     * the API.
     *
     * @return array The field option objects.
     * @since 1.6
     */
    protected function getInput()
    {
        $document = JFactory::getDocument();
        $document->addScript(
            JURI::base() . '../modules/mod_pmailer_subscription/lib/'
            . 'mod_pmailer_subscription.js'
        );

        $options = array();
        $attr = '';

        /*
         * Initialize JavaScript field attributes. This is the path to the
         * ajax handler that will save your list selection.
         */
        $attr .= $this->element['onchange'] = (string)
            'onchange="javascript: saveListSelection(\''
                . JURI::base()
                . '../modules/mod_pmailer_subscription/lib/utils.php'
            . '\')"';

        // Get the database instance
        $db = JFactory::getDbo();
        // Build the select query
        $query = 'SELECT params FROM jos_modules'
            . ' WHERE module="mod_pmailer_subscription"';
        $db->setQuery($query);
        $params = $db->loadObjectList();

        // Decode the options to get thje api key and url
        $options = json_decode($params[0]->params, true);

        // Create a new API utility class
        $api = new PMailerSubscriptionApiV1_0(
            $options['enterprise_url'],
            $options['pmailer_api_key']
        );

        // Get the lists needed for subscription
        $response = $api->getLists();

        // Make a default entry for the dropdown
        $lists = array('0' => 'Please select a list');

        // Builds the options for the dropdown
        foreach ( $response['data'] as $list )
        {
            $lists[$list['list_id']] = $list['list_name'];

        }

        // The dropdown output
        return JHTML::_(
            'select.genericlist',
            $lists,
            'mod_pmailer_lists_box',
            trim($attr),
            'id',
            'title',
            $this->value
        );

    }

}

The utility file that saves the param

$db = JFactory::getDbo();

if ( (isset($_POST['op']) === true) && ($_POST['op'] === 'saveList') )
{
    $query = 'SELECT params FROM jos_modules'
        . ' WHERE module="mod_pmailer_subscription"';
    $db->setQuery($query);
    $params = $db->loadObjectList();

    // Decode the options to get thje api key and url
    $options = json_decode($params[0]->params, true);

    $options['list_selection'] = (int)$_POST['id'];

    $new_params = json_encode($options);

    $query = 'UPDATE jos_modules SET params = "' . $new_params
        . ' WHERE module="mod_pmailer_subscription"';

    $db->query($query);

    echo 'success';

}

The Javascript

// Gets called on dropdown change event
function saveListSelection(url)
{
    // Ajax code here to save list selection
    var x = new Request({
        url: url,
        method: 'post'
    }).send('op=saveList&id=' + $('mod_pmailer_lists_box').get('value'));

}

I hope you can give me some advise as how to this I am stuck like crazy. The only constraint is that it HAS to be a module. Boss's orders.


Solution

  • Looks like no one relates to this question.

    I have found a way to fix it though by NOT using ajax.