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() && (!$this->getUser()->getID() != user_getid())) {
108 $this->setPermissionDeniedError();
109 $this->data_array = null;
115 * getUser - get the $user object this note is associated with.
117 * @return Object The $user object.
119 function &getUser() {
124 * fetchData() - re-fetch the data for this diarynote from the database.
126 * @param int $noteid The diary note id.
127 * @return bool success
129 function fetchData($noteid) {
130 $res = db_query_params('SELECT * FROM user_diary WHERE id=$1',
132 if (!$res || db_numrows($res) < 1) {
133 $this->setError(_('DiaryNote: Invalid noteid'));
136 $this->data_array = db_fetch_array($res);
137 db_free_result($res);
142 * isPublic - whether this diary note is available to the general public.
144 * @return bool is_public.
146 function isPublic() {
147 return (($this->data_array['is_public'] == 1) ? true : false);
150 function getDatePostedOn() {
151 return date(_('Y-m-d H:i'), $this->data_array['date_posted']);
154 function getSummary() {
155 return $this->data_array['summary'];
158 function getDetails() {
160 $result_html = util_gen_cross_ref($this->data_array['details']);
161 if (forge_get_config('diary_parser_type') == 'markdown') {
162 require_once $gfcommon.'include/Markdown.include.php';
163 $result_html = FF_Markdown($result_html);
165 $result_html = nl2br($result_html);
171 return $this->data_array['id'];
175 return '/developer/?view=detail&diary_id='.$this->getID().'&diary_user='.$this->getUser()->getID();
178 function getAbstract() {
180 //get the first paragraph of the diary note.
181 if (strstr($this->data_array['details'], '<br/>')) {
182 $arr = explode('<br/>', $this->data_array['details']);
184 $arr = explode("\n", $this->data_array['details']);
186 $abstract = util_gen_cross_ref($arr[0]);
187 if (forge_get_config('diary_parser_type') == 'markdown') {
188 require_once $gfcommon.'include/Markdown.include.php';
189 $abstract = FF_Markdown($abstract);
191 $abstract = nl2br($abstract);
193 $arr_v = $this->getVotes();
194 $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()));
195 $content .= html_e('div', array('class' => 'widget-sticker-body'), $abstract.html_e('br').util_make_link($this->getLink(),_('... Read more')));
196 $content .= html_e('div', array('class' => 'widget-sticker-footer'), _('Posted')._(': ').$this->getDatePostedOn());
197 return html_e('div', array('class' => 'widget-sticker-container'), $content);
201 * castVote - Vote on this tracker item or retract the vote
202 * @param bool $value true to cast, false to retract
203 * @return bool success (false sets error message)
205 function castVote($value = true) {
206 if (!($uid = user_getid()) || $uid == 100) {
207 $this->setMissingParamsError(_('User ID not passed'));
210 if (!$this->canVote()) {
211 $this->setPermissionDeniedError();
214 $has_vote = $this->hasVote($uid);
215 if ($has_vote == $value) {
216 /* nothing changed */
220 $res = db_query_params('INSERT INTO diary_votes (diary_id, user_id) VALUES ($1, $2)',
221 array($this->getID(), $uid));
223 $res = db_query_params('DELETE FROM diary_votes WHERE diary_id = $1 AND user_id = $2',
224 array($this->getID(), $uid));
227 $this->setError(db_error());
234 * hasVote - Check if a user has voted on this diary item
236 * @param int|bool $uid user ID (default: current user)
237 * @return bool true if a vote exists
239 function hasVote($uid = false) {
243 if (!$uid || $uid == 100) {
246 $res = db_query_params('SELECT * FROM diary_votes WHERE diary_id = $1 AND user_id = $2',
247 array($this->getID(), $uid));
248 return (db_numrows($res) == 1);
252 * getVotes - get number of valid cast and potential votes
254 * @return array|bool (votes, voters, percent)
256 function getVotes() {
257 if ($this->votes !== false) {
261 $lvoters = $this->getVoters();
262 if (($numvoters = count($lvoters)) < 1) {
263 $this->votes = array(0, 0, 0);
267 $res = db_query_params('SELECT COUNT(*) AS count FROM diary_votes WHERE diary_id = $1 AND user_id = ANY($2)',
268 array($this->getID(), db_int_array_to_any_clause($lvoters)));
269 $db_count = db_fetch_array($res);
270 $numvotes = $db_count['count'];
272 /* check for invalid values */
273 if ($numvotes < 0 || $numvoters < $numvotes) {
274 $this->votes = array(-1, -1, 0);
276 $this->votes = array($numvotes, $numvoters,
277 (int)($numvotes * 100 / $numvoters + 0.5));
283 * canVote - check whether the current user can vote on diary
285 * @return bool true if they can
288 if ((user_getid() != $this->getUser()->getID()) && in_array(user_getid(), $this->getVoters())) {
295 * getVoters - get IDs of users that may vote on diary
297 * @return array list of user IDs
299 function getVoters() {
300 if ($this->voters !== false) {
301 return $this->voters;
304 $this->voters = array();
305 $res = db_query_params('SELECT user_id FROM users WHERE status = $1 AND user_id != $2', array('A', $this->getUser()->getID()));
306 $this->voters = util_result_column_to_array($res);
307 return $this->voters;