|
| 1 | +<?php |
| 2 | +require_once(INCLUDE_DIR . 'class.plugin.php'); |
| 3 | + |
| 4 | +class PluginsAjaxAPI extends AjaxController { |
| 5 | + |
| 6 | + /* |
| 7 | + * Protect all routines in this controller |
| 8 | + */ |
| 9 | + function access() { |
| 10 | + global $thisstaff; |
| 11 | + if (!$thisstaff || !$thisstaff->isAdmin()) |
| 12 | + Http::response(403, 'Access Denied'); |
| 13 | + |
| 14 | + return true; |
| 15 | + } |
| 16 | + |
| 17 | + // helper func to look up plugin & instance |
| 18 | + private function lookup($plugin_id, $instance_id=0) { |
| 19 | + if (!($plugin = PluginManager::lookup( (int) $plugin_id))) |
| 20 | + Http::response(404, 'No such plugin'); |
| 21 | + |
| 22 | + if ($instance_id && !($instance = $plugin->getInstance( (int) $instance_id))) |
| 23 | + Http::response(404, 'No such plugin instance'); |
| 24 | + |
| 25 | + return [$plugin, $instance]; |
| 26 | + } |
| 27 | + |
| 28 | + function getInstances($plugin_id) { |
| 29 | + list($plugin,)= $this->lookup($plugin_id); |
| 30 | + $pjax_container = '#items'; |
| 31 | + include(STAFFINC_DIR . 'templates/plugin-instances.tmpl.php'); |
| 32 | + } |
| 33 | + |
| 34 | + function updateInstance($plugin_id, $instance_id) { |
| 35 | + list($plugin, $instance) = $this->lookup($plugin_id, $instance_id); |
| 36 | + $errors = array(); |
| 37 | + if ($_POST && $instance->update($_POST, $errors)) |
| 38 | + Http::response(201, $this->encode([ |
| 39 | + 'redirect' => sprintf('plugins.php?id=%d#instances', |
| 40 | + $plugin->getId()) |
| 41 | + ])); |
| 42 | + $form = $instance->getForm(); |
| 43 | + $action = "#plugins/{$plugin->getId()}/instances/{$instance->getId()}/update"; |
| 44 | + include STAFFINC_DIR . 'templates/plugin-instance-modal.tmpl.php'; |
| 45 | + } |
| 46 | + |
| 47 | + function addInstance($plugin_id) { |
| 48 | + list($plugin,) = $this->lookup($plugin_id); |
| 49 | + $errors = array(); |
| 50 | + if ($_POST && ($instance=$plugin->addInstance($_POST, $errors))) |
| 51 | + Http::response(201, $this->encode([ |
| 52 | + 'redirect' => sprintf('plugins.php?id=%d#instances', |
| 53 | + $plugin->getId()) |
| 54 | + ])); |
| 55 | + // This should return cached form with errors (if any) |
| 56 | + $form = $plugin->getConfigForm(); |
| 57 | + // Set action |
| 58 | + $action = "#plugins/{$plugin->getId()}/instances/add"; |
| 59 | + include(STAFFINC_DIR . 'templates/plugin-instance-modal.tmpl.php'); |
| 60 | + } |
| 61 | +} |
| 62 | +?> |
0 commit comments