All pastes #792210 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-11-21 06:39 UTC
rendered paste body
<?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'
    );
  }
  
  public function create_plugin($ui) {
    $plugin_uuid= UUID::get();
    $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 );
  }
}

?>