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 }
54
62 public function loadBox($max = 5, $cachedelay = 3600)
63 {
64 global $langs, $conf;
65 $langs->load("boxes");
66
67 $this->max = $max;
68
69 // Get Fediverse feed URL
70 $sql = '';
71 if (!empty($this->paramdef)) {
72 $sql = "SELECT value FROM ".MAIN_DB_PREFIX."const";
73 $sql .= " WHERE name like '%SOCIAL_NETWORKS_DATA_".$this->db->escape($this->paramdef)."%'";
74 }
75 $resql = $this->db->query($sql);
76 $num = $this->db->num_rows($resql);
77
78 $socialNetworkTitle = '';
79 $socialNetworkUrl = '';
80 $authParams = [];
81 if ($num > 0) {
82 $obj = $this->db->fetch_row($resql);
83 $socialNetworkData = json_decode($obj[0], true);
84 $socialNetworkTitle = $socialNetworkData['title'];
85 $socialNetworkUrl = $socialNetworkData['url'];
86
87 foreach ($socialNetworkData as $key => $value) {
88 if ($key !== 'title' && $key !== 'url') {
89 $authParams[$key] = $value;
90 }
91 }
92 }
93
94 $fediverseParser = new SocialNetworkManager($socialNetworkTitle);
95 $path_fediverse = DOL_DATA_ROOT.'/fediverse/temp/'.$socialNetworkTitle;
96
97 $result = $fediverseParser->fetchPosts($socialNetworkUrl, $this->max, $cachedelay, $path_fediverse);
98
99 $title = $langs->trans("BoxTitleLastFediverseInfos", $max, dol_escape_htmltag($socialNetworkTitle));
100 if ($result < 0 || !empty($fediverseParser->error)) {
101 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($fediverseParser->getLastFetchDate() ? dol_print_date($fediverseParser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
102 if ($fediverseParser->error) {
103 $errormessage .= " - ".$fediverseParser->error;
104 }
105 $title .= " ".img_error($errormessage);
106 $this->info_box_head = array('text' => $title, 'limit' => 0);
107 } else {
108 $this->info_box_head = array(
109 'text' => $title,
110 'sublink' => $socialNetworkUrl,
111 'subtext' => $langs->trans("LastRefreshDate").': '.($fediverseParser->getLastFetchDate() ? dol_print_date($fediverseParser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
112 'subpicto' => 'globe',
113 'target' => '_blank',
114 );
115 }
116
117 $posts = $fediverseParser->getPosts();
118 $nbitems = count($posts);
119
120 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
121 $post = $posts[$line];
122 $title = dol_escape_htmltag($post['content']);
123 $date = dol_escape_htmltag($post['created_at']);
124 $href = dol_escape_htmltag($post['url']);
125 $tooltip = dol_escape_htmltag($title);
126
127 $this->info_box_contents[$line][0] = array(
128 'td' => 'class="left" width="16"',
129 'text' => img_picto('', 'share-alt'),
130 'url' => $href,
131 'tooltip' => $tooltip,
132 'target' => 'newfediverse',
133 );
134
135 $this->info_box_contents[$line][1] = array(
136 'td' => 'class="tdoverflowmax300"',
137 'text' => $title,
138 'url' => $href,
139 'tooltip' => $tooltip,
140 'maxlength' => 0,
141 'target' => 'newfediverse',
142 );
143
144 $this->info_box_contents[$line][2] = array(
145 'td' => 'class="right nowraponall"',
146 'text' => $date,
147 );
148 }
149 }
150
159 public function showBox($head = null, $contents = null, $nooutput = 0)
160 {
161 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
162 }
163}
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...