Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Admin search

givanz3f4ad8817ee2
diff --git a/admin/controller/tools/search.php b/admin/controller/tools/search.php
new file mode 100644
index 0000000..f118ac8
--- /dev/null
+++ b/admin/controller/tools/search.php
@@ -0,0 +1,172 @@
+<?php
+
+/**
+ * Vvveb
+ *
+ * Copyright (C) 2022 Ziadin Givan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Vvveb\Controller\Tools;
+
+use function Vvveb\__;
+use Vvveb\Controller\Base;
+use Vvveb\System\Images;
+use function Vvveb\url;
+
+
+class Search extends Base {
+ private function searchMenu($text, &$results, &$menu) {
+ foreach ($menu as $key => $value) {
+ if (isset($value['heading'])) {
+ continue;
+ }
+
+ if (isset($value['name']) && (stripos($value['name'], $text) !== false)) {
+ $results[$value['name']] = [
+ 'icon' => $value['icon'] ?? '',
+ 'icon-img' => $value['icon-img'] ?? '',
+ 'text' => $value['name'],
+ 'url' => $value['url'],
+ ];
+ }
+
+ if (isset($value['items']) && is_array($value['items'])) {
+ $this->searchMenu($text, $results, $value['items']);
+ }
+ }
+
+ return $results;
+ }
+
+ function index() {
+ $text = $this->request->get['search'] ?? '';
+ $type = $this->request->get['type'] ?? '';
+
+ $this->view->search = $text;
+ $this->view->type = $type;
+
+ if (strlen($text) < 3) {
+ $this->view->search = [];
+ return;
+ }
+
+ $products = new \Vvveb\Sql\ProductSQL();
+
+ $options = [
+ 'limit' => 5,
+ 'like' => $text,
+ ] + $this->global;
+
+ unset($options['admin_id']);
+ $results = $products->getAll($options);
+
+ $search = [];
+
+ if (isset($results['product'])) {
+ $key = __('product');
+ foreach ($results['product'] as $product) {
+ $product['image'] = Images::image($product['image'], 'product', 'thumb');
+ $url = url(['module' => 'product/product', 'slug'=> $product['slug'], 'product_id' => $product['product_id'], 'type' => $product['type']]);
+
+ $search[$key][] = [
+ 'type' => 'cardimage',
+ 'src' => $product['image'],
+ 'text' => $product['name'],
+ 'url' => $url,
+ ];
+ }
+ }
+
+ //if (count($search) < 5) {
+ $posts = new \Vvveb\Sql\PostSQL();
+ $results = $posts->getAll($options);
+
+ if (isset($results['post'])) {
+ $key = __('post');
+ foreach ($results['post'] as $post) {
+ $post['image'] = Images::image($post['image'], 'post', 'thumb');
+ $url = url(['module' => 'content/post', 'slug'=> $post['slug'], 'post_id' => $post['post_id'], 'type' => $post['type']]);
+
+ $search[$key][] = [
+ 'type' => 'cardimage',
+ 'src' => $post['image'],
+ 'text' => $post['name'],
+ 'url' => $url,
+ ];
+ }
+ }
+ //}
+
+ $users = new \Vvveb\Sql\UserSQL();
+ $results = $users->getAll($options + ['search' => $text]);
+
+ if (isset($results['user'])) {
+ $key = __('user');
+ foreach ($results['user'] as $user) {
+ $url = url(['module' => 'user/user', 'user_id' => $user['user_id']]);
+ if (isset($user['avatar'])) {
+ $user['image']= Images::image($user['avatar'], 'user');
+ }
+
+
+ $search[$key][] = [
+ 'type' => 'cardimage',
+ 'src' => $user['image'],
+ 'text' => $user['first_name'] . ' ' . $user['last_name'] . ' (' . $user['username'] . ') ' . $user['email'],
+ 'url' => $url,
+ ];
+ }
+ }
+
+ $results = [];
+ $this->searchMenu($text, $results, $this->view->menu);
+
+ if ($results) {
+ $search[__('Settings')] = $results;
+ }
+
+ $pages = [
+ 'index/index' => __('Home'),
+ 'product/index' => __('Shop'),
+ 'content' => __('Blog'),
+ 'user/index' => __('User'),
+ 'user/login/index' => __('Login'),
+ 'user/signup/index' => __('Signup'),
+ 'cart/cart/index' => __('Cart'),
+ 'checkout/checkout/index' => __('Checkout'),
+ ];
+
+ $key = __('pages');
+ foreach ($pages as $route => $name) {
+ if (stripos($name, $text) !== false) {
+ $url = url($route);
+ $search[$key][$url] = [
+ 'icon' => 'la la-external-link-alt',
+ 'text' => $name,
+ 'url' => $url,
+ ];
+
+ break;
+ }
+ }
+
+ $this->view->search = $search;
+ }
+
+ function autocomplete() {
+ }
+}
diff --git a/admin/template/tools/search.tpl b/admin/template/tools/search.tpl
new file mode 100755
index 0000000..77d8936
--- /dev/null
+++ b/admin/template/tools/search.tpl
@@ -0,0 +1,27 @@
+import(common.tpl)
+
+@group = .search-results .group
+@item = .search-results .group .list-group-item
+
+@group|deleteAllButFirstChild
+@item|deleteAllButFirstChild
+
+@group|before = <?php
+ foreach($this->search as $groupName => $group) {
+?>
+ @group [data-v-group-name]|innerText = <?php echo htmlspecialchars(ucfirst($groupName));?>
+
+ @item|before = <?php
+ foreach($group as $name => $item) {
+ ?>
+
+ @item [data-v-item-*]|innerText = $item['@@__data-v-item-(*)__@@']
+ @item [data-v-item-url]|href = $item['url']
+ @item [data-v-item-icon]|class = $item['icon']
+ @item [data-v-item-image]|src = $item['src']
+
+ @item|after = <?php
+ } ?>
+
+@group|after = <?php
+} ?>
\ No newline at end of file
diff --git a/public/js/admin.js b/public/js/admin.js
index 746401a..ac6c148 100755
--- a/public/js/admin.js
+++ b/public/js/admin.js
@@ -26,6 +26,7 @@ import {Plugins} from './admin/controller/plugins.js';
import {Table} from './admin/controller/table.js';
import {Cache} from './admin/controller/cache.js';
import {HeartBeat} from './admin/heartbeat.js';
+import {Search} from './admin/controller/search.js';
if (window.Vvveb === undefined) window.Vvveb = {};
@@ -33,6 +34,7 @@ window.themes = Themes;
window.plugins = Plugins;
window.table = Table;
window.cache = Cache;
+window.search = Search;
Router.init();
diff --git a/public/js/admin/controller/search.js b/public/js/admin/controller/search.js
new file mode 100644
index 0000000..4f65b93
--- /dev/null
+++ b/public/js/admin/controller/search.js
@@ -0,0 +1,30 @@
+var prevSearchValue;
+
+class SearchController {
+
+ autocomplete(e, element) {
+ let url = element.href;
+
+ if (element.value) {
+ let loading = this.form.querySelector(".loading");
+ if (loading && prevSearchValue != element.value) {
+ loading.classList.remove('d-none');
+ document.querySelector(".search-results")?.replaceChildren();
+ }
+
+ prevSearchValue = element.value;
+
+ delay(() => loadAjax(window.location.pathname + location.search, ".search-results", function(data) {
+ if (loading) {
+ loading.classList.add('d-none');
+ }
+ }, {'module':'tools/search', search:element.value}), 1000);
+ } else {
+ document.querySelector(".search-results")?.replaceChildren();
+ }
+ }
+
+}
+
+let Search = new SearchController();
+export {Search};