dolibarr 21.0.0-alpha
box_fediverse.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25include_once DOL_DOCUMENT_ROOT.'/core/class/fediverseparser.class.php';
26include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
27
32{
33 public $boxcode = "lastfediverseinfos";
34 public $boximg = "object_share-alt";
35 public $boxlabel = "BoxLastFediverseInfos";
36 public $depends = array("socialnetworks");
37
41 public $paramdef;
42
49 public function __construct($db, $param)
50 {
51 $this->db = $db;
52 $this->paramdef = $param;
53 $this->urltoaddentry = DOL_URL_ROOT.'/admin/fediverse.php';
54 $this->msgNoRecords = 'NoRecordFound';
55 }
56
64 public function loadBox($max = 5, $cachedelay = 3600)
65 {
66 global $langs;
67
68 $langs->load("boxes");
69
70 $this->max = $max;
71
72 // Get Fediverse feed URL
73 $sql = '';
74 if (!empty($this->paramdef)) {
75 $sql = "SELECT value FROM ".MAIN_DB_PREFIX."const";
76 $sql .= " WHERE name like '%SOCIAL_NETWORKS_DATA_".$this->db->escape($this->paramdef)."%'";
77 }
78 $resql = $this->db->query($sql);
79 $num = $this->db->num_rows($resql);
80
81 $socialNetworkTitle = '';
82 $socialNetworkUrl = '';
83 $authParams = [];
84 if ($num > 0) {
85 $obj = $this->db->fetch_row($resql);
86 $socialNetworkData = json_decode($obj[0], true);
87 $socialNetworkTitle = $socialNetworkData['title'];
88 $socialNetworkUrl = $socialNetworkData['url'];
89
90 foreach ($socialNetworkData as $key => $value) {
91 if ($key !== 'title' && $key !== 'url') {
92 $authParams[$key] = $value;
93 }
94 }
95 }
96
97 $fediverseParser = new SocialNetworkManager($socialNetworkTitle);
98 $path_fediverse = DOL_DATA_ROOT.'/fediverse/temp/'.$socialNetworkTitle;
99
100 $result = $fediverseParser->fetchPosts($socialNetworkUrl, $this->max, $cachedelay, $path_fediverse);
101
102 $title = $langs->trans("BoxTitleLastFediverseInfos", $max, dol_escape_htmltag($socialNetworkTitle));
103 if ($result < 0 || !empty($fediverseParser->error)) {
104 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($fediverseParser->getLastFetchDate() ? dol_print_date($fediverseParser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
105 if ($fediverseParser->error) {
106 $errormessage .= " - ".$fediverseParser->error;
107 }
108 $title .= " ".img_error($errormessage);
109 $this->info_box_head = array('text' => $title, 'limit' => 0);
110 } else {
111 $this->info_box_head = array(
112 'text' => $title,
113 'sublink' => $socialNetworkUrl,
114 'subtext' => $langs->trans("LastRefreshDate").': '.($fediverseParser->getLastFetchDate() ? dol_print_date($fediverseParser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
115 'subpicto' => 'globe',
116 'target' => '_blank',
117 );
118 }
119
120 $posts = $fediverseParser->getPosts();
121
122 $nbitems = count($posts);
123
124 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
125 $post = $posts[$line];
126 $title = dol_escape_htmltag($post['content']);
127 $date = dol_escape_htmltag($post['created_at']);
128 $href = dol_escape_htmltag($post['url']);
129 $tooltip = dol_escape_htmltag($title);
130
131 $this->info_box_contents[$line][0] = array(
132 'td' => 'class="left" width="16"',
133 'text' => img_picto('', 'share-alt'),
134 'url' => $href,
135 'tooltip' => $tooltip,
136 'target' => 'newfediverse',
137 );
138
139 $this->info_box_contents[$line][1] = array(
140 'td' => 'class="tdoverflowmax300"',
141 'text' => $title,
142 'url' => $href,
143 'tooltip' => $tooltip,
144 'maxlength' => 0,
145 'target' => 'newfediverse',
146 );
147
148 $this->info_box_contents[$line][2] = array(
149 'td' => 'class="right nowraponall"',
150 'text' => $date,
151 );
152 }
153
154
155 // if ($nbitems == 0) {
156 // $this->info_box_contents[$line][0] = array(
157 // 'td' => 'class="center"',
158 // 'text' => '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>'
159 // );
160 // }
161 }
162
163
164
173 public function showBox($head = null, $contents = null, $nooutput = 0)
174 {
175 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
176 }
177}
Class ModeleBoxes.
Class to manage Social network posts.
Class to manage the box to show Fediverse posts.
loadBox($max=5, $cachedelay=3600)
Load data into info_box_contents array to show array later.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
__construct($db, $param)
Constructor.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...