3 * Copyright 2019, Franck Villaume - TrivialDev
4 * This file is a part of Fusionforge.
6 * Fusionforge is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Fusionforge is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
20 require_once $gfcommon.'include/FFObject.class.php';
22 $DIARYNOTE_OBJ = array();
25 * diarynote_get_object() - Get diary note object by diary note ID.
26 * You should always use this instead of instantiating the object directly
28 * @param int $note_id The ID of the diary note - required
29 * @param int|bool $res The result set handle ("SELECT * FROM user_diary WHERE id = $1")
30 * @return DiaryNote a diary note object or false on failure
32 function &diarynote_get_object($note_id, $res = false) {
33 global $DIARYNOTE_OBJ;
34 if (!isset($DIARYNOTE_OBJ["_".$note_id."_"])) {
36 //the db result handle was passed in
38 $res = db_query_params('SELECT * FROM user_diary WHERE id = $1',
41 if (!$res || db_numrows($res) < 1) {
42 $DIARYNOTE_OBJ["_".$note_id."_"] = false;
44 $arr = db_fetch_array($res);
45 $DIARYNOTE_OBJ["_".$note_id."_"] = new DiaryNote(user_get_object($arr['user_id']), $note_id, $arr);
48 return $DIARYNOTE_OBJ["_".$note_id."_"];
51 class DiaryNote extends FFObject {
54 * Associative array of data from db.
56 * @var array $data_array.
68 * cached return value of getVotes
69 * @var int|bool $votes
74 * cached return value of getVoters
75 * @var int|bool $voters
82 function __construct(&$User, $noteid = false, $arr = false) {
83 parent::__construct();
84 if (!forge_get_config('use_diary')) {
85 $this->setError(_('Diary feature is off.'));
88 if ($User->isError()) {
89 $this->setError('DiaryNote: '. $User->getErrorMessage());
95 if (!$arr || !is_array($arr)) {
96 if (!$this->fetchData($noteid)) {
100 $this->data_array =& $arr;
101 if ($this->data_array['user_id'] != $this->User->getID()) {
102 $this->setError(_('DiaryNote')._(': ')._('user_id in db result does not match User Object'));
103 $this->data_array = null;
107 if (!$this->isPublic()) {
108 if (!$this->getUser->getID() != user_getid()) {
109 $this->setPermissionDeniedError();
110 $this->data_array = null;
118 * getUser - get the $user object this note is associated with.
120 * @return Object The $user object.
122 function &getUser() {
127 * fetchData() - re-fetch the data for this diarynote from the database.
129 * @param int $noteid The diary note id.
130 * @return bool success
132 function fetchData($noteid) {
133 $res = db_query_params('SELECT * FROM user_diary WHERE id=$1',
135 if (!$res || db_numrows($res) < 1) {
136 $this->setError(_('DiaryNote: Invalid noteid'));
139 $this->data_array = db_fetch_array($res);
140 db_free_result($res);
145 * isPublic - whether this diary note is available to the general public.
147 * @return bool is_public.
149 function isPublic() {
150 return (($this->data_array['is_public'] == 1) ? true : false);
153 function getDatePostedOn() {
154 return date(_('Y-m-d H:i'), $this->data_array['date_posted']);
157 function getSummary() {
158 return $this->data_array['summary'];
161 function getDetails() {
163 $result_html = util_gen_cross_ref($this->data_array['details']);
164 $parsertype = forge_get_config('diary_parser_type');
165 switch ($parsertype) {
167 require_once $gfcommon.'include/Markdown.include.php';
168 $result_html = FF_Markdown($result_html);
171 $result_html = nl2br($result_html);
177 return $this->data_array['id'];
181 return '/developer/?view=detail&diary_id='.$this->getID().'&diary_user='.$this->getUser()->getID();
184 function getAbstract() {
186 //get the first paragraph of the diary note.
187 if (strstr($this->data_array['details'], '<br/>')) {
188 $arr = explode('<br/>', $this->data_array['details']);
190 $arr = explode("\n", $this->data_array['details']);
192 $abstract = util_gen_cross_ref($arr[0]);
193 $parsertype = forge_get_config('diary_parser_type');
194 switch ($parsertype) {
196 require_once $gfcommon.'include/Markdown.include.php';
197 $abstract = FF_Markdown($abstract);
200 $abstract = nl2br($abstract);
202 $arr_v = $this->getVotes();
203 $content = html_e('div', array('class' => 'widget-sticker-header box'), html_e('div', array(), util_make_link($this->getLink(), $this->getSummary()).' - '.$arr_v[0].' '._('Vote(s)').' '._('by').' ').util_display_user($this->getUser()->getUnixname(), $this->getUser()->getID(), $this->getUser()->GetRealname()));
204 $content .= html_e('div', array('class' => 'widget-sticker-body'), $abstract.html_e('br').util_make_link($this->getLink(),_('... Read more')));
205 $content .= html_e('div', array('class' => 'widget-sticker-footer'), _('Posted')._(': ').$this->getDatePostedOn());
206 return html_e('div', array('class' => 'widget-sticker-container'), $content);
210 * castVote - Vote on this tracker item or retract the vote
211 * @param bool $value true to cast, false to retract
212 * @return bool success (false sets error message)
214 function castVote($value = true) {
215 if (!($uid = user_getid()) || $uid == 100) {
216 $this->setMissingParamsError(_('User ID not passed'));
219 if (!$this->canVote()) {
220 $this->setPermissionDeniedError();
223 $has_vote = $this->hasVote($uid);
224 if ($has_vote == $value) {
225 /* nothing changed */
229 $res = db_query_params('INSERT INTO diary_votes (diary_id, user_id) VALUES ($1, $2)',
230 array($this->getID(), $uid));
232 $res = db_query_params('DELETE FROM diary_votes WHERE diary_id = $1 AND user_id = $2',
233 array($this->getID(), $uid));
236 $this->setError(db_error());
243 * hasVote - Check if a user has voted on this group item
245 * @param int|bool $uid user ID (default: current user)
246 * @return bool true if a vote exists
248 function hasVote($uid = false) {
252 if (!$uid || $uid == 100) {
255 $res = db_query_params('SELECT * FROM diary_votes WHERE diary_id = $1 AND user_id = $2',
256 array($this->getID(), $uid));
257 return (db_numrows($res) == 1);
261 * getVotes - get number of valid cast and potential votes
263 * @return array|bool (votes, voters, percent)
265 function getVotes() {
266 if ($this->votes !== false) {
270 $voters = $this->getVoters();
271 if (($numvoters = count($voters)) < 1) {
272 $this->votes = array(0, 0, 0);
276 $res = db_query_params('SELECT COUNT(*) AS count FROM diary_votes WHERE diary_id = $1 AND user_id = ANY($2)',
277 array($this->getID(), db_int_array_to_any_clause($voters)));
278 $db_count = db_fetch_array($res);
279 $numvotes = $db_count['count'];
281 /* check for invalid values */
282 if ($numvotes < 0 || $numvoters < $numvotes) {
283 $this->votes = array(-1, -1, 0);
285 $this->votes = array($numvotes, $numvoters,
286 (int)($numvotes * 100 / $numvoters + 0.5));
292 * canVote - check whether the current user can vote on
293 * items in this tracker
295 * @return bool true if they can
298 if ((user_getid() != $this->getUser()->getID()) && in_array(user_getid(), $this->getVoters())) {
305 * getVoters - get IDs of users that may vote on
306 * items in this tracker
308 * @return array list of user IDs
310 function getVoters() {
311 if ($this->voters !== false) {
312 return $this->voters;
315 $this->voters = array();
316 $res = db_query_params('SELECT user_id FROM users WHERE status = $1 AND user_id != $2', array('A', $this->getUser()->getID()));
317 $this->voters = util_result_column_to_array($res);
318 return $this->voters;