]>
Commit | Line | Data |
---|---|---|
90dc8f2b VP |
1 | <?php |
2 | ||
440ff671 VP |
3 | /* Hook Definitions |
4 | * | |
5 | * Hooks let you do things in your plugin, like add nav items for your | |
6 | * own pages, add extra cards to the overview and more (to come) | |
7 | */ | |
8 | /** HOOKTYPE_NAVBAR | |
9 | * | |
10 | * @param array $pages | |
11 | * Receives an array of pages. For example: | |
12 | * $pages = ["Overview" => ""]; | |
13 | * | |
14 | * So when you call this hook, you must refer to the | |
15 | * parameter by reference. For example: | |
16 | * Hook::func(HOOKTYPE_NAVBAR, 'add_navbar_item'); | |
17 | * | |
18 | * function add_navbar_item(&$pages) // remember the & to use by reference | |
19 | * { insert_hacks_here(); } | |
20 | */ | |
21 | define('HOOKTYPE_NAVBAR', 100); | |
22 | ||
23 | /** HOOKTYPE_PRE_HEADER | |
24 | * | |
25 | * This doesn't receive anything, however you must still specify an | |
26 | * parameter for your hook function, because it's referring to memory. Sorry =] | |
27 | * | |
28 | * Currently this is only used by the "sql_auth" plugin by Valware in order to | |
29 | * redirect users immediately to the login page. | |
30 | * | |
31 | * Putting HTML in this hook is not a good idea. | |
32 | */ | |
33 | define('HOOKTYPE_PRE_HEADER', 101); | |
34 | ||
35 | /** HOOKTYPE_PRE_OVERVIEW_CARD | |
36 | * | |
37 | * @param object $stats | |
38 | * | |
39 | * This is called before the initial cards have loaded in the overview. | |
40 | * This lets you add your own HTML or whatever you like on the overview, | |
41 | * new cards, whatever. | |
42 | * | |
43 | * The parameter is an object containing stats used in the overview. | |
44 | * See "index.php" to see how it's used. | |
45 | * | |
46 | */ | |
47 | ||
48 | define('HOOKTYPE_PRE_OVERVIEW_CARD', 102); | |
49 | /** HOOKTYPE_OVERVIEW_CARD | |
50 | * | |
51 | * @param object $stats | |
52 | * | |
53 | * This is called after the initial cards have loaded in the overview. | |
54 | * This lets you add your own HTML or whatever you like on the overview, | |
55 | * new cards, whatever. | |
56 | * | |
57 | * The parameter is an object containing stats used in the overview. | |
58 | * See "index.php" to see how it's used. | |
59 | * | |
60 | */ | |
61 | ||
62 | ||
63 | define('HOOKTYPE_OVERVIEW_CARD', 102); | |
90dc8f2b VP |
64 | |
65 | /** | |
66 | * Class for "Hook" | |
67 | * This is the main function which gets called whenever you want to use a Hook. | |
68 | * | |
69 | * Example: | |
70 | * Calling the Hook using a function: | |
71 | * Hook::func(HOOKTYPE_NAVBAR, 'bob'); | |
72 | * | |
73 | * This Hook references the function 'bob', and will run this | |
74 | * function bob | |
75 | * { | |
76 | * echo "We rehashed!"; | |
77 | * } | |
78 | * | |
79 | * Example 2: | |
80 | * Calling the Hook using an initialized object class method: | |
81 | * Hook::func(HOOKTYPE_NAVBAR, [$this, 'method']); | |
82 | * | |
83 | * Example 3: | |
84 | * Calling the Hook using a static class method: | |
fe2a6f27 | 85 | * Hook::func(HOOKTYPE_NAVBAR, 'classname::method'); |
90dc8f2b VP |
86 | * |
87 | */ | |
88 | class Hook { | |
89 | ||
90 | /** A static list of Hooks and their associated functions */ | |
91 | private static $actions = []; | |
92 | ||
93 | /** Runs a Hook. | |
94 | * The parameter for $Hook should be a "HOOKTYPE_" as defined in hook.php | |
95 | * @param string $Hook The define or string name of the Hook. For example, HOOKTYPE_REHASH. | |
96 | * @param array &$args The array of information you are sending along in the Hook, so that other functions may see and modify things. | |
97 | * @return void Does not return anything. | |
98 | * | |
99 | */ | |
100 | public static function run($Hook, &$args = array()) | |
101 | { | |
371aa651 VP |
102 | if (!empty(self::$actions[$Hook])) |
103 | foreach (self::$actions[$Hook] as &$f) | |
104 | $f($args); | |
105 | ||
90dc8f2b VP |
106 | } |
107 | ||
108 | /** Calls a Hook | |
109 | * @param string $Hook The define or string name of the Hook. For example, HOOKTYPE_REHASH. | |
110 | * @param string|Closure $function This is a string reference to a Closure function or a class method. | |
111 | * @return void Does not return anything. | |
112 | */ | |
113 | public static function func($Hook, $function) | |
371aa651 | 114 | { |
90dc8f2b VP |
115 | self::$actions[$Hook][] = $function; |
116 | } | |
117 | ||
118 | /** Deletes a Hook | |
119 | * @param string $Hook The Hook from which we are removing a function reference. | |
120 | * @param string $function The name of the function that we are removing. | |
121 | * @return void Does not reuturn anything. | |
122 | */ | |
123 | ||
124 | public static function del($Hook, $function) | |
371aa651 | 125 | { |
90dc8f2b | 126 | for ($i = 0; isset(self::$actions[$Hook][$i]); $i++) |
371aa651 VP |
127 | if (self::$actions[$Hook][$i] == $function) |
128 | array_splice(self::$actions[$Hook],$i); | |
90dc8f2b VP |
129 | } |
130 | } |