我对Joomla来说真的很新,1.6看起来也很新,所以我在将数据保存到模块后端的模块参数字段时遇到了一些麻烦。
让我解释一下。
我有一个小型联系人管理器,我希望人们使用Joomla 1.6中的joomla模块订阅。现在模块已创建,一切正常。我甚至在模块后端创建一个自定义字段,调用我的API并使用所需数据填充下拉框。
我希望能够将选择保存为模块参数,以便在我的模板中轻松访问它。这似乎很难做到,因为我无法找到任何文档。
基本上我的流程如下。
我让onchange事件工作,甚至运行带有post请求的脚本,但在我用来处理帖子的PHP文件中保存数据我无法让数据库实例对其进行任何操作。这是解释,这是代码:
模块后端中的自定义字段
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
);
}
}
保存参数的实用程序文件
$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';
}
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'));
}
我希望你能给我一些建议,因为我怎么会像疯子一样陷入困境。唯一的限制是它必须是一个模块。老板的命令。
答案 0 :(得分:0)
看起来没人跟这个问题有关。
我找到了一种解决方法,但不使用ajax。