source: subversion/branches/devel-api/program/include/rcube_plugin.php @ 2285

Last change on this file since 2285 was 2285, checked in by thomasb, 4 years ago

Allow plugins to add stylesheets + use common event interface with event 'init' to start plugin scripts

File size: 4.4 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/include/rcube_plugin.php                                      |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |  Abstract plugins interface/class                                     |
13 |  All plugins need to extend this class                                |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id: $
19
20*/
21
22/**
23 * Plugin interface class
24 *
25 * @package Core
26 */
27abstract class rcube_plugin
28{
29  public $ID;
30  public $api;
31  public $task;
32  protected $home;
33
34  /**
35   * Default constructor.
36   */
37  public function __construct($api)
38  {
39    $this->ID = get_class($this);
40    $this->api = $api;
41    $this->home = $api->dir . DIRECTORY_SEPARATOR . $this->ID;
42  }
43 
44  /**
45   * Initialization method, needs to be implemented by the plugin itself
46   */
47  abstract function init();
48
49  /**
50   * Register a callback function for a specific (server-side) hook
51   *
52   * @param string Hook name
53   * @param mixed Callback function as string or array with object reference and method name
54   */
55  public function add_hook($hook, $callback)
56  {
57    $this->api->register_hook($hook, $callback);
58  }
59 
60  /**
61   * Load localized texts from the plugins dir
62   *
63   * @param string Directory to search in
64   * @param string Domain to save texts
65   */
66  public function add_texts($dir)
67  {
68    if (empty($domain))
69      $domain = $this->ID;
70   
71    $lang = $_SESSION['language'];
72    $locdir = slashify(realpath(slashify($this->home) . $dir));
73    $texts = array();
74   
75    foreach (array('en_US', $lang) as $lng) {
76      @include($locdir . $lng . '.inc');
77      $texts = (array)$labels + (array)$messages + (array)$texts;
78    }
79
80    // prepend domain to text keys and add to the application texts repository
81    if (!empty($texts)) {
82      $add = array();
83      foreach ($texts as $key => $value)
84        $add[$domain.'.'.$key] = $value;
85
86      $rcmail = rcmail::get_instance();
87      $rcmail->load_language($lang, $add);
88    }
89  }
90 
91  /**
92   * Wrapper for rcmail::gettext() adding the plugin ID as domain
93   */
94  function gettext($p)
95  {
96    return rcmail::get_instance()->gettext($p, $this->ID);
97  }
98 
99  /**
100    * Register a handler for a specific client-request action
101    *
102    * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
103    *
104    * @param string Action name (should be unique)
105    * @param mixed Callback function as string or array with object reference and method name
106   */
107  public function register_action($action, $callback)
108  {
109    $this->api->register_action($action, $this->ID, $callback);
110  }
111
112  /**
113   * Register a handler function for a template object
114   *
115   * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" />
116   * will be replaced by the return value if the registered callback function.
117   *
118   * @param string Object name (should be unique and start with 'plugin.')
119   * @param mixed Callback function as string or array with object reference and method name
120   */
121  public function register_handler($name, $callback)
122  {
123    $this->api->register_handler($name, $this->ID, $callback);
124  }
125
126  /**
127   * Make this javascipt file available on the client
128   *
129   * @param string File path; absolute or relative to the plugin directory
130   */
131  public function include_script($fn)
132  {
133    $this->api->include_script($this->ressource_url($fn));
134  }
135
136  /**
137   * Make this stylesheet available on the client
138   *
139   * @param string File path; absolute or relative to the plugin directory
140   */
141  public function include_stylesheet($fn)
142  {
143    $this->api->include_stylesheet($this->ressource_url($fn));
144  }
145
146  /**
147   * Make the given file name link into the plugin directory
148   */
149  private function ressource_url($fn)
150  {
151    if ($fn[0] != '/' && !eregi('^https?://', $fn))
152      return $this->ID . '/' . $fn;
153    else
154      return $fn;
155  }
156
157
158}
159
Note: See TracBrowser for help on using the repository browser.