<?php use Illuminate\Database\Capsule\Manager as DB; use API\Core\Tool; use API\Model\Plugin; use API\OAuthServer\OAuthHelper; $version_plugins = Tool::makeEndpoint(function ($version) { OAuthHelper::needsScopes(['version', 'plugins']); $plugins = Tool::paginateCollection(Plugin::short()->with('authors', 'versions', 'descriptions')->withAverageNote()->descWithLang(Tool::getRequestLang())->withGlpiVersion($version)); Tool::endWithJson($plugins); }); $app->get('/version/:version/plugin', $version_plugins); $app->options('/version/:version/plugin', function () { });
/** * Fetching infos of a single plugin */ $single = function ($key) use($app) { $plugin = Plugin::with('descriptions', 'authors', 'versions', 'screenshots', 'tags')->short()->withAverageNote()->withNumberOfVotes()->withCurrentVersion()->withDownloads()->where('key', '=', $key)->where('active', '=', 1)->first(); if ($plugin) { Tool::endWithJson($plugin); } else { Tool::endWithJson(['error' => 'No plugin has that key'], 400); } }; /** * List of all plugins */ $all = function () use($app) { $plugins = Tool::paginateCollection(Plugin::short()->with('authors', 'versions', 'descriptions')->withDownloads()->withAverageNote()->descWithLang(Tool::getRequestLang())->where('active', '=', 1)); Tool::endWithJson($plugins); }; /** * Most popular plugins */ $popular = function () use($app) { $popular_plugins = Plugin::popularTop(10)->where('active', '=', 1)->get(); Tool::endWithJson($popular_plugins); }; /** * Trending plugins * most popular the 2 last weeks */ $trending = function () use($app) { $trending_plugins = Plugin::trendingTop(10)->where('active', '=', 1)->get();
<?php /** * Search * * This REST module hooks on * following URLs * * /search */ use API\Core\Tool; use Illuminate\Database\Capsule\Manager as DB; // Minimal length of search string $search_min_length = 2; $search = function () use($app) { global $search_min_length, $allowed_languages; $body = Tool::getBody(); if ($body == NULL || !isset($body->query_string) || strlen($body->query_string) < $search_min_length) { return Tool::endWithJson(["error" => "Your search string needs to " . "have at least " . $search_min_length . " chars"], 400); } $query_string = $body->query_string; $_search = Tool::paginateCollection(\API\Model\Plugin::short()->with('authors', 'versions', 'descriptions')->withDownloads()->withAverageNote()->descWithLang(Tool::getRequestLang())->where('name', 'LIKE', "%{$query_string}%")->orWhere('plugin_description.short_description', 'LIKE', "%{$query_string}%")->orWhere('plugin_description.long_description', 'LIKE', "%{$query_string}%")); Tool::endWithJson($_search); }; $app->post('/search', $search); $app->options('/search', function () { });
<?php /** * Search * * This REST module hooks on * following URLs * * /search */ use API\Core\Tool; use Illuminate\Database\Capsule\Manager as DB; use API\OAuthServer\OAuthHelper; // Minimal length of search string $search_min_length = 2; $search = Tool::makeEndpoint(function () use($app) { OAuthHelper::needsScopes(['plugins:search']); global $search_min_length, $allowed_languages; $body = Tool::getBody(); if ($body == NULL || !isset($body->query_string) || strlen($body->query_string) < $search_min_length) { Tool::endWithJson(["error" => "Your search string needs to " . "have at least " . $search_min_length . " chars"], 400); } $query_string = $body->query_string; $_search = Tool::paginateCollection(\API\Model\Plugin::short()->with('authors', 'versions', 'descriptions')->withAverageNote()->descWithLang(Tool::getRequestLang())->where('active', '=', true)->where(function ($q) use($query_string) { return $q->where('name', 'LIKE', "%{$query_string}%")->orWhere('key', 'LIKE', "%{$query_string}%")->orWhere('plugin_description.short_description', 'LIKE', "%{$query_string}%")->orWhere('plugin_description.long_description', 'LIKE', "%{$query_string}%"); })->orderBy('download_count', 'DESC')->orderBy('note', 'DESC')->orderBy('name', 'ASC')); Tool::endWithJson($_search); }); $app->post('/search', $search); $app->options('/search', function () { });