<?php
$a= 'a'; $b= 0;
if ($a==true && $b==false && $a==$b)
  echo 'true is false!';
     
Home | Software | TextpatternFacebook  Google  ReddIt  StumbleUponContact | Imprint
     

Stand with Julian Assange   Stellungnahme zur Krisenpolitik

     
The lum_p module

A typical admin-side-plugin with lum_p could start like this:

if (@txpinterface!='admin' || !include_plugin('lum_p'))
	return;
lum_p_reg(
	'plugin=0.2.3|my_wonderful_plugin|my_wpl|1,2'.n
	.'tab_=admin|my_wpl|||My wonderful plugin'
);

The lum_p_reg-call does this:

  • Register with lum_p, making sure it is lum_p version 0.2.3 or newer
  • Connect the long name ("my_wonderful_plugin") with the short prefix ("my_wpl")
  • Set default privilege to 1,2
  • In the 2. line (the one starting with "tab_"), a new subtab is registered under "Admin". Without lum_p, this would have to look like
    add_privs('my_wpl', '1,2');
    register_tab('admin', 'my_wpl', 'My wonderful plugin');
    register_callback('my_wpl', 'my_wpl', '', '');
    

This is all Textpattern has to load for "'My wonderful plugin" on each and every http-request. The "my_wpl()"-function is only pulled from the database if someone actually clicks on the "Admin/My wonderful plugin"-button. A special comment line is used to tell lum_p where to cut:

/*|lum_p_i|cut|*/

After that, you can define some preference values and data, like this:

lum_p_v_def('my_wpl', 'text', 'hello world!');

lum_p_v_data('my_wpl', 'carver16x16.gif',
'R0lGODdhEAAQAMIFAEA8NnNlKbuhU6+sp/j69v///////////ywAAAAAEAAQAAADOUi63D4u
EkDbgIzWde/SAKZ0HBhgZBYCwSkI4sO+cTS8gcS8bK7Tg56ENhI2iKWWCMkInpoSj06SAAA7');

The difference between values and data is that the former appears on the automatically generated "Options"-page for your plugin while the latter does not. Also, values are not deleted when the plugin is uninstalled, thus the users configuration is retained. To force deletion of values "text" and "foo" when uninstalling, use

lum_p_opt('my_wpl', 'v_del', 'text,foo');

Now, to actually have a function stored in the DB, write

lum_p_f_decl('my_wpl');
/**
 * The main function for my_wonderful_plugin
 * lum_p automatically generates a function called my_wpl(), which will load
 * this my_wpl_() from the DB (if its not already present) and execute it.
 */
function my_wpl_ ($event, $step) {
  if ($step=='') {
    pagetop('My wonderful plugin');
    echo
      'Your configured text is: <b>'.lum_p_v('my_wpl', 'text').'</b><br />'
      .'A wonderful image: <img src="index.php?event=my_wpl&step=carver16x16.gif">';
  } else {
    header("Content-type: image/gif");
    /* lum_p_v will call doSlash() on the names, so it should be safe to pass
      $step directly (except when you don't want the other my_wpl-values to
      be exposed like this...) */
    die(base64_decode(lum_p_v('my_wpl', $step)));
  }
}

The lum_p_f_decl() is not only an ordinary function call, but also serves as block separator when the plugin is installed. This only works if it is on a line on its own (without any comments). The above example shows the shortest possible name, if you need some more functions it looks like this:

lum_p_f_decl('my_wpl', 'foo');
function my_wpl_foo_ ($arg) { return $arg+1; }
function my_wpl_bar ($arg) { return $arg-1; }

Here, the block "my_wpl_foo" contains 2 functions. my_wpl_bar() is only available after my_wpl_foo() has been called at least once. You can have as many function-blocks as you like.

Thus, a fully functional plugin (with its own "Options"-page!) is ready. Only the code in this example holds the string "My wonderful plugin" twice, which of course is bad for i18n. So, better let lum_p also take care of that:

lum_p_l_set('my_wpl', array(
  'en-gb' => array(
    '#tab_my_wpl'=>'My wonderful plugin',
    'txt'=>'Your configured text is:',
    'img'=>'A wonderful image:',
  ),
  'de-de' => array(
    '#tab'=>'Mein wundervolles plugin',
    'txt'=>'Der konfigurierte Text ist:',
    'img'=>'Ein wundervolles Bild:',
  ),
));

This must come after lum_p_v_def(). If some plugin of yours doesn't need any config-values, put a

lum_p_v_def('');

before it. The "#" before the "tab" causes the string to be saved in section "common" instead of "my_wpl", the name in this case would be tab_my_wpl. This is done order to be loaded by Textpattern early on so it can appear in the menu. The registration-stuff can then be shortened to

lum_p_reg(
	'plugin=0.2.3|my_wonderful_plugin|my_wpl|1,2'.n
	.'tab_=admin|my_wpl'
);

'cause the "tab_"-command will replace an empty "title"-string by gTxt('tab_'.$event).

So, finally our complete multilingual plugin looks like

if (@txpinterface!='admin' || !include_plugin('lum_p'))
	return;
lum_p_reg(
	'plugin=0.2.3|my_wonderful_plugin|my_wpl|1,2'.n
	.'tab_=admin|my_wpl'
);

/*|lum_p_i|cut|*/

lum_p_v_def('my_wpl', 'text', 'hello world!');

lum_p_v_data('my_wpl', 'carver16x16.gif',
'R0lGODdhEAAQAMIFAEA8NnNlKbuhU6+sp/j69v///////////ywAAAAAEAAQAAADOUi63D4u
EkDbgIzWde/SAKZ0HBhgZBYCwSkI4sO+cTS8gcS8bK7Tg56ENhI2iKWWCMkInpoSj06SAAA7');

lum_p_l_set('my_wpl', array(
  'en-gb' => array(
    '#tab_my_wpl'=>'My wonderful plugin',
    'txt'=>'Your configured text is:',
    'img'=>'A wonderful image:',
  ),
  'de-de' => array(
    '#tab_my_wpl'=>'Mein wundervolles plugin',
    'txt'=>'Der konfigurierte Text ist:',
    'img'=>'Ein wundervolles Bild:',
  ),
));

lum_p_f_decl('my_wpl');
/**
 * The main function for my_wonderful_plugin
 * lum_p automatically generates a function called my_wpl(), which will load
 * this my_wpl_() from the DB (if its not already present) and execute it.
 */
function my_wpl_ ($event, $step) {
  if ($step=='') {
    pagetop(gTxt('tab_my_wpl'));
    echo
      lum_p_l('my_wpl', 'txt').' <b>'.lum_p_v('my_wpl', 'text').'</b><br />'
      .lum_p_l('my_wpl', 'img').' <img src="index.php?event=my_wpl&step=carver16x16.gif">';
  } else {
    header("Content-type: image/gif");
    /* lum_p_v will call doSlash() on the names, so it should be safe to pass
      $step directly (except when you don't want the other my_wpl-values to
      be exposed like this...) */
    die(base64_decode(lum_p_v('my_wpl', $step)));
  }
}

To try it out, just create my_wonderful_plugin with that even more wonderful ied_plugin_composer, paste the code, check "Enabled", "Has prefs" and "Event notify", select Plugin type "Admin" and save. After that, the subtab might be called "tab_my_wpl" instead of "My wonderful plugin" (or "Mein wundervolles plugin" if you have selected german language) and it might work only after clicking it twice, but these little nuisances will go away by themselves.

This example can be edited like any other plugin, but when a lum_p using plugin gets installed the usual way via the "Admin/Plugins/Install plugin"-textbox, things are different. Then, only the part before the /*|lum_p_i|cut|*/ would show up in the editor, the rest hides elsewhere in the database. To edit the whole plugin, roll back (=click the version number in the plugin composers list).

Get v1.0

Screenshot:
     
©2020 Lutz Möhrmann