<?php
class Creator extends Plugin {
/**
* Required Plugin Information
**/
function info() {
return array(
'name' => 'Creator',
'license' => 'Apache License 2.0',
'url' => 'http://habariproject.org/',
'author' => 'Habari Community',
'authorurl' => 'http://habariproject.org/',
'version' => '0.1',
'description' => 'Plugin creator for Habari',
'copyright' => '2007'
);
}
function create_uuid() {
// copied from http://no.php.net/manual/fr/function.uniqid.php#65879
// The field names refer to RFC 4122 section 4.1.2
return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
mt_rand(0, 65535), // 16 bits for "time_mid"
mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
// 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
// (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
// 8 bits for "clk_seq_low"
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
);
}
public function create_plugin($ui) {
$plugin_uuid= $this->create_uuid();
$plugin_name= strtolower($ui->controls['plugin_name']->value);
$plugin_author= strtolower($ui->controls['plugin_author']->value);
$plugin_body= <<<EOT
<?php
class $plugin_name extends Plugin {
function info() {
return array(
'url' => '',
'name' => '$plugin_name',
'license' => 'Apache License 2.0',
'author' => '$plugin_author',
'version' => '0.1',
'description' => ''
);
}
function action_update_check() {
Update::add( '$plugin_name', '$plugin_uuid', \$this->info->version );
}
EOT;
if ( strtolower($ui->controls['configurable']->value == 1) ) {
$plugin_body.= <<<EOT
/**
* Add actions to the plugin page for this plugin
* @param array \$actions An array of actions that apply to this plugin
* @param string \$plugin_id The string id of a plugin, generated by the system
* @return array The array of actions to attach to the specified \$plugin_id
**/
public function filter_plugin_config(\$actions, \$plugin_id) {
if (\$plugin_id == \$this->plugin_id()) {
\$actions[] = 'Configure';
}
return \$actions;
}
/**
* Respond to the user selecting an action on the plugin page
* @param string \$plugin_id The string id of the acted-upon plugin
* @param string \$action The action string supplied via the filter_plugin_config hook
**/
public function action_plugin_ui(\$plugin_id, \$action) {
if (\$plugin_id == \$this->plugin_id()) {
switch (\$action) {
case 'Configure' :
// Expand this to create the Configure UI
// See the FormUI documentation for details
\$ui = new FormUI(strtolower(get_class(\$this)));
\$ui->on_success(array(\$this, 'updated_config'));
\$ui->out();
break;
}
}
}
/**
* Returns true if plugin config form values defined in action_plugin_ui should be stored in options by Habari
* @return boolean True if options should be stored
**/
public function updated_config(\$ui) {
return true;
}
EOT;
}
// Close the class
$plugin_body.= <<<EOT
}
?>
EOT;
// Save $plugin_body;
$plugin_directory= dirname(__FILE__) . '/../' . $plugin_name . '/';
$plugin_file= strtolower($plugin_name) . '.plugin.php';
// create directory
mkdir($plugin_directory) or die ("Could not make directory");
// create plugin
$fh = fopen($plugin_directory . $plugin_file, 'w') or die("can't open file");
fwrite($fh, $plugin_body);
fclose($fh);
// No need to store the returned values, so return false
return false;
}
/**
* Add actions to the plugin page for this plugin
* @param array $actions An array of actions that apply to this plugin
* @param string $plugin_id The string id of a plugin, generated by the system
* @return array The array of actions to attach to the specified $plugin_id
**/
public function filter_plugin_config($actions, $plugin_id) {
if ( $plugin_id == $this->plugin_id() ) {
$actions[]= 'Create Plugin';
}
return $actions;
}
/**
* Respond to the user selecting an action on the plugin page
* @param string $plugin_id The string id of the acted-upon plugin
* @param string $action The action string supplied via the filter_plugin_config hook
**/
public function action_plugin_ui($plugin_id, $action) {
if ( $plugin_id == $this->plugin_id() ) {
switch ( $action ) {
case 'Create Plugin' :
$ui= new FormUI(strtolower(get_class($this)));
$plugin_name= $ui->add('text', 'plugin_name', 'Plugin Name:');
$plugin_author= $ui->add('text', 'plugin_author', 'Plugin Author:');
$configurable= $ui->add('select', 'configurable', 'Plugin is configurable');
$configurable->options= array('0' => 'No', '1' => 'Yes');
$ui->on_success(array($this, 'create_plugin'));
$ui->out();
break;
}
}
}
function action_update_check() {
Update::add( 'Creator', 'fd6fb33c-df30-49f4-c116-9cc08bbd9611', $this->info->version );
}
}
?>