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;
117 * getUser - get the $user object this note is associated with.
119 * @return Object The $user object.
121 function &getUser() {
126 * fetchData() - re-fetch the data for this diarynote from the database.
128 * @param int $noteid The diary note id.
129 * @return bool success
131 function fetchData($noteid) {
132 $res = db_query_params('SELECT * FROM user_diary WHERE id=$1',
134 if (!$res || db_numrows($res) < 1) {
135 $this->setError(_('DiaryNote: Invalid noteid'));
138 $this->data_array = db_fetch_array($res);
139 db_free_result($res);
144 * isPublic - whether this diary note is available to the general public.
146 * @return bool is_public.
148 function isPublic() {
149 return (($this->data_array['is_public'] == 1) ? true : false);
152 function getDatePostedOn() {
153 return date(_('Y-m-d H:i'), $this->data_array['date_posted']);
156 function getSummary() {
157 return $this->data_array['summary'];
160 function getDetails() {
162 $result_html = util_gen_cross_ref($this->data_array['details']);
163 $parsertype = forge_get_config('diary_parser_type');
164 switch ($parsertype) {
166 require_once $gfcommon.'include/Markdown.include.php';
167 $result_html = FF_Markdown($result_html);
170 $result_html = nl2br($result_html);
176 return $this->data_array['id'];
180 return '/developer/?view=detail&diary_id='.$this->getID().'&diary_user='.$this->getUser()->getID();
183 function getAbstract() {
185 //get the first paragraph of the diary note.
186 if (strstr($this->data_array['details'], '<br/>')) {
187 $arr = explode('<br/>', $this->data_array['details']);
189 $arr = explode("\n", $this->data_array['details']);
191 $abstract = util_gen_cross_ref($arr[0]);
192 $parsertype = forge_get_config('diary_parser_type');
193 switch ($parsertype) {
195 require_once $gfcommon.'include/Markdown.include.php';
196 $abstract = FF_Markdown($abstract);
199 $abstract = nl2br($abstract);
201 $arr_v = $this->getVotes();
202 $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()));
203 $content .= html_e('div', array('class' => 'widget-sticker-body'), $abstract.html_e('br').util_make_link($this->getLink(),_('... Read more')));
204 $content .= html_e('div', array('class' => 'widget-sticker-footer'), _('Posted')._(': ').$this->getDatePostedOn());
205 return html_e('div', array('class' => 'widget-sticker-container'), $content);
209 * castVote - Vote on this tracker item or retract the vote
210 * @param bool $value true to cast, false to retract
211 * @return bool success (false sets error message)
213 function castVote($value = true) {
214 if (!($uid = user_getid()) || $uid == 100) {
215 $this->setMissingParamsError(_('User ID not passed'));
218 if (!$this->canVote()) {
219 $this->setPermissionDeniedError();
222 $has_vote = $this->hasVote($uid);
223 if ($has_vote == $value) {
224 /* nothing changed */
228 $res = db_query_params('INSERT INTO diary_votes (diary_id, user_id) VALUES ($1, $2)',
229 array($this->getID(), $uid));
231 $res = db_query_params('DELETE FROM diary_votes WHERE diary_id = $1 AND user_id = $2',
232 array($this->getID(), $uid));
235 $this->setError(db_error());
242 * hasVote - Check if a user has voted on this group item
244 * @param int|bool $uid user ID (default: current user)
245 * @return bool true if a vote exists
247 function hasVote($uid = false) {
251 if (!$uid || $uid == 100) {
254 $res = db_query_params('SELECT * FROM diary_votes WHERE diary_id = $1 AND user_id = $2',
255 array($this->getID(), $uid));
256 return (db_numrows($res) == 1);
260 * getVotes - get number of valid cast and potential votes
262 * @return array|bool (votes, voters, percent)
264 function getVotes() {
265 if ($this->votes !== false) {
269 $lvoters = $this->getVoters();
270 if (($numvoters = count($lvoters)) < 1) {
271 $this->votes = array(0, 0, 0);
275 $res = db_query_params('SELECT COUNT(*) AS count FROM diary_votes WHERE diary_id = $1 AND user_id = ANY($2)',
276 array($this->getID(), db_int_array_to_any_clause($lvoters)));
277 $db_count = db_fetch_array($res);
278 $numvotes = $db_count['count'];
280 /* check for invalid values */
281 if ($numvotes < 0 || $numvoters < $numvotes) {
282 $this->votes = array(-1, -1, 0);
284 $this->votes = array($numvotes, $numvoters,
285 (int)($numvotes * 100 / $numvoters + 0.5));
291 * canVote - check whether the current user can vote on
292 * items in this tracker
294 * @return bool true if they can
297 if ((user_getid() != $this->getUser()->getID()) && in_array(user_getid(), $this->getVoters())) {
304 * getVoters - get IDs of users that may vote on
305 * items in this tracker
307 * @return array list of user IDs
309 function getVoters() {
310 if ($this->voters !== false) {
311 return $this->voters;
314 $this->voters = array();
315 $res = db_query_params('SELECT user_id FROM users WHERE status = $1 AND user_id != $2', array('A', $this->getUser()->getID()));
316 $this->voters = util_result_column_to_array($res);
317 return $this->voters;