YaWK  24.1
Yet another WebKit
tipOfDay.php
Go to the documentation of this file.
1 <?php
2 namespace YAWK {
3  /**
4  * @details <b>Display the tip of the day. <i>(if enabled)</i></b>
5  *
6  * This class handles all tip of the day functions.
7  * Tips are stored in cms_tips database.
8  * <i>Example:</i>
9  * <code><?php YAWK\tipOfDay::displayRandom($db); ?></code>
10  * To show a random tip of the day.
11  * <p><i>Class covers backend functionality.
12  * See Methods Summary for Details!</i></p>
13  *
14  * @author Daniel Retzl <[email protected]>
15  * @copyright 2009-2015 Daniel Retzl yawk.io
16  * @license https://opensource.org/licenses/MIT
17  * @version 1.0.0
18  * @brief TipOfDay class serve functions to get and draw a tip of the day
19  */
20  class tipOfDay extends \YAWK\alert
21  {
22  public $id = 1;
23  public $published = 1;
24  public $sortation = 0;
25  public $tipClass = "";
26  public $tipHeading = "";
27  public $tipText = "";
28  public $tipLink = "";
29 
30  /**
31  * @brief Draw a tip of the day
32  * @return bool
33  */
34  public function setPublished($db, $id, $published)
35  {
36  /** @var $db \YAWK\db */
37  // update database
38  if ($db->query("UPDATE {tips} SET published = '".$published."' WHERE id = '".$id."'"))
39  { // all good
40  return true;
41  }
42  else
43  { // failed to set publish status
44  \YAWK\sys::setSyslog($db, 3, 1, "failed to set tip of the day ID #$this->id to published state 0",0, 0, 0, 0);
45  return false;
46  }
47  }
48 
49  /**
50  * @brief Store a new tip to database
51  * @param object $db Database Object
52  * @param int $published 1 = Tip is published (unseen) 0 = Tip is not published (already seen)
53  * @param string $tipHeading Heading of the tip, up to 255 chars
54  * @param string $tipText Text of the tip, up to 255 chars
55  * @param string $tipLink Link of the tip, up to 255 chars
56  *
57  */
58  public function setTip($db, $published, $tipHeading, $tipText, $tipLink)
59  {
60  /** @var $db \YAWK\db */
61  // preprate data
62  if (isset($published) && (!empty($published)))
63  { $db->quote($published); }
64  if (isset($tipHeading) && (!empty($tipHeading)))
65  { $db->quote($tipHeading); }
66  if (isset($tipText) && (!empty($tipText)))
67  { $db->quote($tipText); }
68  if (isset($tipLink) && (!empty($tipLink)))
69  { $db->quote($tipLink); }
70  // insert tip into database
71  if ($db->query("INSERT INTO {tips} (published, tipClass, tipHeading, tipText, tipLink)
72  VALUES
73  ('".$published."',
74  '".$tipHeading."',
75  '".$tipText."',
76  '".$tipLink."'"))
77  { // all good
78  return true;
79  }
80  else
81  { // failed to insert tip into database
82  return false;
83  }
84  }
85 
86  /**
87  * @brief Get a random tip from database that is still unseen.
88  * @param $db
89  * @return bool
90  */
91  public function getRandomTipData($db)
92  {
93  /** @var $db \YAWK\db */
94  if (\YAWK\settings::getSetting($db, "backendTipOfDayRepeat") == true)
95  {
96  $sql = "";
97  }
98  else
99  {
100  $sql = "WHERE published = 1";
101  }
102 
103  if ($res = $db->query("SELECT * FROM {tips} $sql ORDER BY rand() LIMIT 1"))
104  {
105  // get tip data into array
106  $row = mysqli_fetch_assoc($res);
107  // check data before we return it
108  if (is_array($row) && (!empty($row)))
109  { // walk through array
110  foreach ($row as $property => $value)
111  { // set current object properties
112  $this->$property = $value;
113  }
114  // all done
115  return true;
116  }
117  else
118  { // array is not set or empty
119  return false;
120  }
121  }
122  else
123  {
124  // query failed
125  return false;
126  }
127  }
128 
129  /**
130  * @brief Get a the next single tip which is not already seen (still published)
131  * @details To get the order, ID and published fields will be used in the query.
132  * @param object $db database object
133  * @return bool
134  */
135  public function getNextTipData($db)
136  {
137  /** @var $db \YAWK\db */
138  // query data: ordered ascending by ID, only entries that are unseen
139  if ($res = $db->query("SELECT * FROM {tips} WHERE published = 1 ORDER BY id ASC LIMIT 1"))
140  {
141  // get tip data into array
142  $row = mysqli_fetch_assoc($res);
143  // check data before we return it
144  if (is_array($row) && (!empty($row)))
145  { // all good, return data
146  foreach ($row as $property => $value)
147  { // set object properties
148  $this->$property = $value;
149  }
150  // data set, all good -
151  return true;
152  }
153  else
154  {
155  // array empty - check if repeat tips is enabled
156  if (\YAWK\settings::getSetting($db, "backendTipOfDayRepeat") == true)
157  {
158  if ($db->query("UPDATE {tips} SET published = 1"))
159  {
160  // call function again to show first tip
161  $this->getNextTipData($db);
162  }
163  }
164  // start again with 1st tip
165  return true;
166  }
167  }
168  else
169  { // query failed
170  return false;
171  }
172  }
173 
174  /**
175  * @brief Draw tip of the day.
176  * @param object $db database object
177  * @param array $lang language array
178  * @return string
179  */
180  public function drawTip($db, $lang)
181  {
182  // get next tip on load, ordered by ID
183  $this->getNextTipData($db);
184  // or get random tip on load
185  // $this->getRandomTipData($db);
186 
187  if (isset($this->tipLink) && (!empty($this->tipLink)))
188  {
189  if (filter_var($this->tipLink, FILTER_VALIDATE_URL))
190  {
191  $this->tipText .= "<br><a href=\"#\">$this->tipLink</a>";
192  }
193  }
194  if (isset($this->tipHeading) && (empty($this->tipHeading)))
195  {
196  // todo: last message that tutorial is over?
197  return false;
198  }
199  else if (isset($this->tipText) && (empty($this->tipText)))
200  {
201  // todo: last message that tutorial is over?
202  return false;
203  }
204  else
205  {
206  if($this->setPublished($db, $this->id, 0) == false)
207  {
208  return "failed to set tip to zero";
209  }
210  $this->tipHeading = $lang[$this->tipHeading];
211  $this->tipText = $lang[$this->tipText];
212  return alert::draw("tipofday", "Tip #$this->id - $this->tipHeading", "$this->tipText", '', 10000);
213  }
214  }
215 
216  } /* end class TipOfDay */
217 } /* end namespace */
print $lang['FILEMAN_UPLOAD']
Throws a fancy Bootstrap Alert (success, info, warning or danger)
Definition: alert.php:19
static draw($type, $title, $text, $redirect, $delay)
Definition: alert.php:30
static getSetting($db, $property)
Get and return value for property from settings database.
Definition: settings.php:470
TipOfDay class serve functions to get and draw a tip of the day.
Definition: tipOfDay.php:21
drawTip($db, $lang)
Draw tip of the day.
Definition: tipOfDay.php:180
$sql
Definition: message-new.php:32
This class serves methods to create backup from files.
Definition: AdminLTE.php:2
$value