Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Moved code to post trait

givanzf30e1ddc9557
diff --git a/app/component/posts.php b/app/component/posts.php
index ee3bca8..5e87cdb 100755
--- a/app/component/posts.php
+++ b/app/component/posts.php
@@ -22,18 +22,18 @@
namespace Vvveb\Component;
-use function Vvveb\__;
use function Vvveb\availableLanguages;
use function Vvveb\get;
use Vvveb\Sql\PostSQL;
use Vvveb\System\Component\ComponentBase;
use Vvveb\System\Event;
-use Vvveb\System\Images;
-use function Vvveb\url;
+use Vvveb\System\Traits\Post;
class Posts extends ComponentBase {
+ use Post;
+
public static $defaultOptions = [
- 'page' => ['url', 1],
+ 'page' => ['url'],
'post_id' => 'url',
'language_id' => null,
'source' => 'autocomplete',
@@ -90,14 +90,18 @@ class Posts extends ComponentBase {
function results() {
$posts = new PostSQL();
- if ($page = $this->options['page']) {
- $this->options['start'] = ($page - 1) * $this->options['limit'];
+ if (! $this->options['page'] && ! $this->options['start']) {
+ $this->options['page'] = 1;
}
+ if ($page = $this->options['page']) {
+ $this->options['start'] = ($page - 1) * ((int) ($this->options['limit'] ?? 4));
+ }
+ /*
if ($this->options['limit'] && ! $page) {
$this->options['start'] = 0;
}
-
+ */
if (isset($this->options['post_id']) && is_array($this->options['post_id']) && $this->options['source'] == 'autocomplete') {
$this->options['post_id'] = array_keys($this->options['post_id']);
} else {
@@ -118,79 +122,17 @@ class Posts extends ComponentBase {
$this->options['search'] .= '*';
}
+ //if only one taxonomy_item_id is provided then add it to array
+ if (isset($this->options['taxonomy_item_id']) && ! is_array($this->options['taxonomy_item_id'])) {
+ $this->options['taxonomy_item_id'] = [$this->options['taxonomy_item_id']];
+ }
+
$results = $posts->getAll($this->options);
//$languages = availableLanguages();
- $type = $this->options['type'] ?: 'post';
+ $this->options['type'] = $this->options['type'] ?: 'post';
if ($results && isset($results['post'])) {
- foreach ($results['post'] as $id => &$post) {
- if (isset($post['images'])) {
- $post['images'] = json_decode($post['images'], 1);
-
- foreach ($post['images'] as &$image) {
- $image = Images::image($image, 'post', $this->options['image_size']);
- }
- }
-
- foreach (['categories' => 'category', 'tags' => 'tag', 'taxonomy' => $this->options['taxonomy'] ?? ''] as $taxonomy => $route) {
- if (isset($post[$taxonomy])) {
- $post[$taxonomy] = json_decode($post[$taxonomy], 1);
- $count = $this->options[$taxonomy];
-
- if (! $post[$taxonomy]) {
- continue;
- }
-
- if (is_numeric($count) && is_array($post[$taxonomy])) {
- $post[$taxonomy] = array_slice($post[$taxonomy], 0, $count);
- }
-
- foreach ($post[$taxonomy] as &$cat) {
- $cat['url'] = url("content/$route/index", $cat);
- }
- }
- }
-
- if (isset($post['image'])) {
- $post['image'] = $post['images'][] = Images::image($post['image'], 'post', $this->options['image_size']);
- }
-
- if (isset($post['avatar'])) {
- $post['avatar'] = $post['avatar'] = Images::image($post['avatar'], 'admin');
- }
-
- if (empty($post['excerpt']) && ! empty($post['content'])) {
- $post['excerpt'] = substr(strip_tags($post['content']), 0, $this->options['excerpt_limit']);
- }
-
- //comments translations
- $post['comment_text'] = sprintf(__('%d comment', '%d comments', (int)$post['comment_count']), $post['comment_count']);
-
- //date formatting that can be used for url parameters
- $date = date_parse($post['created_at']);
-
- foreach (['year', 'day', 'month', 'hour', 'minute'] as $key) {
- $post[$key] = $date[$key] ?? '';
- }
-
- $language = [];
-
- if ($post['language_id'] != $this->options['default_language_id']) {
- $language = ['language' => $this->options['language']];
- }
-
- //rfc
- $post['pubDate'] = date('r', strtotime($post['created_at']));
- $post['modDate'] = date('r', strtotime($post['updated_at']));
- $post['lastMod'] = date('Y-m-d\TH:i:sP', strtotime($post['updated_at']));
-
- //url
- $url = ['slug' => $post['slug'], 'post_id' => $post['post_id']] + $language;
- $post['url'] = url("content/$type/index", $url);
- $post['full-url'] = url("content/$type/index", $url + ['host' => SITE_URL, 'scheme' => $_SERVER['REQUEST_SCHEME'] ?? 'http']);
- $post['author-url'] = url('content/user/index', $post);
- $post['comments-url'] = $post['url'] . '#comments';
- }
+ $this->posts($results['post'], $this->options);
}
//if archive then pass year and month
diff --git a/system/traits/post.php b/system/traits/post.php
new file mode 100644
index 0000000..1ad1350
--- /dev/null
+++ b/system/traits/post.php
@@ -0,0 +1,115 @@
+<?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\System\Traits;
+
+use function Vvveb\__;
+use Vvveb\System\Images;
+use function Vvveb\url;
+
+trait Post {
+ function posts(&$posts, &$options = []) {
+ foreach ($posts as &$post) {
+ $post = $this->post($post, $options);
+ }
+
+ return $posts;
+ }
+
+ function post(&$post, &$options = []) {
+ $type = $options['type'] ?? 'post';
+
+ if (isset($post['images'])) {
+ $post['images'] = json_decode($post['images'], 1);
+
+ foreach ($post['images'] as &$image) {
+ $image = Images::image($image, 'post', $options['image_size'] ?? 'medium');
+ }
+ }
+
+ foreach (['categories' => 'category', 'tags' => 'tag', 'taxonomy' => $options['taxonomy'] ?? ''] as $taxonomy => $route) {
+ if (isset($post[$taxonomy])) {
+ $post[$taxonomy] = json_decode($post[$taxonomy], 1);
+ $count = $options[$taxonomy];
+
+ if (! $post[$taxonomy]) {
+ continue;
+ }
+
+ if (is_numeric($count) && is_array($post[$taxonomy])) {
+ $post[$taxonomy] = array_slice($post[$taxonomy], 0, $count);
+ }
+
+ foreach ($post[$taxonomy] as &$cat) {
+ $cat['url'] = url("content/$route/index", $cat);
+ }
+ }
+ }
+
+ if (isset($post['image'])) {
+ $post['image'] = $post['images'][] = Images::image($post['image'], 'post', $options['image_size'] ?? 'medium');
+ }
+
+ if (isset($post['avatar'])) {
+ $post['avatar'] = $post['avatar'] = Images::image($post['avatar'], 'admin');
+ }
+
+ if (empty($post['excerpt']) && ! empty($post['content'])) {
+ $post['excerpt'] = substr(strip_tags($post['content']), 0, $options['excerpt_limit'] ?? 250);
+ }
+
+ //comments translations
+ $post['comment_text'] = sprintf(__('%d comment', '%d comments', (int)$post['comment_count']), $post['comment_count']);
+
+ //date formatting that can be used for url parameters
+ $date = date_parse($post['created_at']);
+
+ foreach (['year', 'day', 'month', 'hour', 'minute'] as $key) {
+ $post[$key] = $date[$key] ?? '';
+ }
+
+ $language = [];
+
+ if ($post['language_id'] != $options['default_language_id']) {
+ $language = ['language' => $options['language']];
+
+ if (! $post['name']) {
+ $post['name'] = '[' . __('No translation') . ']';
+ $post['slug'] = 'no-translation';
+ }
+ }
+
+ //rfc
+ $post['pubDate'] = date('r', strtotime($post['created_at']));
+ $post['modDate'] = date('r', strtotime($post['updated_at']));
+ $post['lastMod'] = date('Y-m-d\TH:i:sP', strtotime($post['updated_at']));
+
+ //url
+ $url = ['slug' => $post['slug'], 'post_id' => $post['post_id']] + $language;
+ $post['url'] = url("content/$type/index", $url);
+ $post['full-url'] = url("content/$type/index", $url + ['host' => SITE_URL, 'scheme' => $_SERVER['REQUEST_SCHEME'] ?? 'http']);
+ $post['author-url'] = url('content/user/index', $post);
+ $post['comments-url'] = $post['url'] . '#comments';
+
+ return $post;
+ }
+}