dolibarr 22.0.5
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: setup SOCIAL_NETWORKS_DATA_<name of the network of this box> of the current entity
73 $socialNetworkTitle = '';
74 $socialNetworkUrl = '';
75 $authParams = [];
76 $socialNetworkData = empty($this->paramdef) ? null : json_decode(getDolGlobalString('SOCIAL_NETWORKS_DATA_'.$this->paramdef), true);
77 if (is_array($socialNetworkData)) {
78 $socialNetworkTitle = $socialNetworkData['title'] ?? '';
79 $socialNetworkUrl = $socialNetworkData['url'] ?? '';
80
81 foreach ($socialNetworkData as $key => $value) {
82 if ($key !== 'title' && $key !== 'url') {
83 $authParams[$key] = $value;
84 }
85 }
86 }
87
88 $fediverseParser = new SocialNetworkManager($socialNetworkTitle, $authParams);
89 $path_fediverse = DOL_DATA_ROOT.'/fediverse/temp/'.$socialNetworkTitle;
90
91 $result = $fediverseParser->fetchPosts($socialNetworkUrl, $this->max, $cachedelay, $path_fediverse, $authParams);
92
93 $title = $langs->trans("BoxTitleLastFediverseInfos", $max, dol_escape_htmltag($socialNetworkTitle));
94 if ($result < 0 || !empty($fediverseParser->error)) {
95 $errormessage = $langs->trans("FailedToRefreshDataInfoNotUpToDate", ($fediverseParser->getLastFetchDate() ? dol_print_date($fediverseParser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")));
96 if ($fediverseParser->error) {
97 $errormessage .= " - ".$fediverseParser->error;
98 }
99 $title .= " ".img_error($errormessage);
100 $this->info_box_head = array('text' => $title, 'limit' => 0);
101 } else {
102 $this->info_box_head = array(
103 'text' => $title,
104 'sublink' => $socialNetworkUrl,
105 'subtext' => $langs->trans("LastRefreshDate").': '.($fediverseParser->getLastFetchDate() ? dol_print_date($fediverseParser->getLastFetchDate(), "dayhourtext") : $langs->trans("Unknown")),
106 'subpicto' => 'globe',
107 'target' => '_blank',
108 );
109 }
110
111 $posts = $fediverseParser->getPosts();
112
113 $nbitems = count($posts);
114
115 for ($line = 0; $line < $max && $line < $nbitems; $line++) {
116 $post = $posts[$line];
117 $title = dol_escape_htmltag($post['content']);
118 $date = dol_escape_htmltag($post['created_at']);
119 $href = dol_escape_htmltag($post['url']);
120 $tooltip = dol_escape_htmltag($title);
121
122 $this->info_box_contents[$line][0] = array(
123 'td' => 'class="left" width="16"',
124 'text' => img_picto('', 'share-alt'),
125 'url' => $href,
126 'tooltip' => $tooltip,
127 'target' => 'newfediverse',
128 );
129
130 $this->info_box_contents[$line][1] = array(
131 'td' => 'class="tdoverflowmax300"',
132 'text' => $title,
133 'url' => $href,
134 'tooltip' => $tooltip,
135 'maxlength' => 0,
136 'target' => 'newfediverse',
137 );
138
139 $this->info_box_contents[$line][2] = array(
140 'td' => 'class="right nowraponall"',
141 'text' => $date,
142 );
143 }
144
145
146 // if ($nbitems == 0) {
147 // $this->info_box_contents[$line][0] = array(
148 // 'td' => 'class="center"',
149 // 'text' => '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>'
150 // );
151 // }
152 }
153
154
155
164 public function showBox($head = null, $contents = null, $nooutput = 0)
165 {
166 return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
167 }
168}
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, $allowothertags=array())
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).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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...