summaryrefslogtreecommitdiffstats
path: root/plugins/plugin.class.php
blob: 6e9bc77ab1f0a93cf686d74029b9c8fb7eeb6c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
 * @file
 * Linkit Plugin interface.
 *
 * Provides an interface and classes to implement Linkit plugins.
 */

/**
 * Linkit plugin interface.
 */
interface LinkitPluginInterface {
  /**
   * This method gets called when searching for items to link to in the
   * autocomplete field.
   */
  public function autocomplete_callback();
}

/**
 *
 * LinkitPlugin.
 */
abstract class LinkitPlugin implements LinkitPluginInterface {

  /**
   * Initialize this plugin with the plugin and the profile.
   *
   * @param array $plugin
   *   The plugin array.
   * @param object $profile
   *   The Linkit profile object.
   */
  function __construct($plugin, $profile) {
    $this->plugin = $plugin;
    $this->profile = $profile;
  }

  /**
   * Return a string representing this handler's name in the UI.
   */
  function ui_title() {
    if (!isset($this->plugin['ui_title'])) {
      return check_plain($this->plugin['module'] . ':' . $this->plugin['name']);
    }
    return check_plain($this->plugin['ui_title']);
  }

  /**
   * Return a string representing this handler's description in the UI.
   */
  function ui_description() {
    if (isset($this->plugin['ui_description'])) {
      return check_plain($this->plugin['ui_description']);
    }
  }

  /**
   * Set the search string.
   *
   * @param string $serach_string.
   */
  function setSearchString($serach_string) {
    $this->serach_string = $serach_string;
  }

  /**
   * Build the label that will be used in the search result for each row.
   */
  function buildLabel($label) {
    return check_plain($label);
  }

  /**
   * Build an URL based in the path and the options.
   */
  function buildPath($path, $options = array()) {
    return url($path, $options);
  }

  /**
   * Build the search row description.
   *
   * If there is a "result_description", run it thro token_replace.
   *
   * @param object $data
   *   An object that will be used in the token_place function
   *
   * @see token_replace()
   */
  function buildDescription($data) {
    if (isset($this->profile->data[$this->plugin['name']]['result_description'])) {
      return token_replace(check_plain($this->profile->data[$this->plugin['name']]['result_description']), array(
        $this->plugin_name => $data,
      ));
    }
  }

  /**
   * Returns a string to use as the search result group name.
   */
  function buildGroup($group_name) {
    return check_plain($group_name);
  }

  /**
   * Returns a string with CSS classes that will be added to the search result
   * row for this item.
   *
   * @return
   *   A string with CSS classes
   */
  function buildRowClass($row_classes) {
    if (is_array($row_classes)) {
      $row_classes = implode(' ', $row_classes);
    }
    return $row_classes;
  }

  /**
   * Generate a settings form for this handler.
   * Uses the standard Drupal FAPI.
   *
   * @return
   *   An array containing any custom form elements to be displayed in the
   *   profile editing form
   */
  function buildSettingsForm() {}

  /**
   * Determine if the handler is considered 'broken', meaning it's a
   * a placeholder used when a handler can't be found.
   */
  function broken() { }
}

/**
 * A special handler to take the place of missing or broken handlers.
 */
class LinkitPluginBroken extends LinkitPlugin {

  function ui_title() { return t('Broken/missing handler'); }

  function ui_description() {}
  function autocomplete_callback() {}

  /**
   * Determine if the handler is considered 'broken'.
   */
  function broken() { return TRUE; }
}