私はJoomlaを初めて使用し、1.6も非常に新しいようです。モジュールバックエンドのモジュールパラメータフィールドにデータを保存するのに少し苦労しています。
もう少しうまく説明しましょう。
Joomla 1.6 の joomla モジュールを使用して購読してもらいたい小さな連絡先マネージャーがあります。これでモジュールが作成され、すべて正常に動作します。API を呼び出してドロップダウン ボックスに必要なデータを入力するモジュール バックエンドにカスタム フィールドを作成することさえできました。
テンプレートで簡単にアクセスできるように、選択をモジュール パラメータとして保存できるようにしたいと考えています。ドキュメントがまったく見つからないため、これを行うのは非常に難しいようです。
基本的に私のプロセスは次のとおりです。
- Joomla admin のモジュール マネージャーに移動します。
- インストールしたモジュールを選択して開きます。
- コードが実行され、連絡先が購読できるリスト名の量がドロップダウン ボックスに入力されます。
- キッカー- リスト名を選択し、ajax リクエストで onchange の Joomla DB のモジュール params フィールドに ID を保存できるようにしたい -
onchange イベントが機能し、ポスト リクエストでスクリプトを実行することさえできますが、ポストを処理してデータを保存するために使用する PHP ファイルでは、DB インスタンスに操作を実行させることができません。それが説明です。コードは次のとおりです。
モジュール バックエンドのカスタム フィールド
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'));
}
どうすればいいかアドバイスをいただければ幸いです。私は狂ったように立ち往生しています。唯一の制約は、モジュールでなければならないということです。ボスの命令。