PageRenderTime 38ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/inc/News.php

http://github.com/thorsten/phpMyFAQ
PHP | 416 lines | 275 code | 44 blank | 97 comment | 26 complexity | b60fca739f9dff38714f3436d6b9699a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * The News class for phpMyFAQ news
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package PMF_News
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @author Matteo Scaramuccia <matteo@scaramuccia.com>
  15. * @copyright 2006-2012 phpMyFAQ Team
  16. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  17. * @link http://www.phpmyfaq.de
  18. * @since 2006-06-25
  19. */
  20. if (!defined('IS_VALID_PHPMYFAQ')) {
  21. exit();
  22. }
  23. /**
  24. * The News class for phpMyFAQ news
  25. *
  26. * @category phpMyFAQ
  27. * @package PMF_News
  28. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  29. * @author Matteo Scaramuccia <matteo@scaramuccia.com>
  30. * @copyright 2006-2012 phpMyFAQ Team
  31. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  32. * @link http://www.phpmyfaq.de
  33. * @since 2006-06-25
  34. */
  35. class PMF_News
  36. {
  37. /**
  38. * @var PMF_Configuration
  39. */
  40. private $_config;
  41. /**
  42. * Language strings
  43. *
  44. * @var string
  45. */
  46. private $pmf_lang;
  47. /**
  48. * Constructor
  49. *
  50. * @param PMF_Configuration
  51. *
  52. * @return PMF_News
  53. */
  54. public function __construct(PMF_Configuration $config)
  55. {
  56. global $PMF_LANG;
  57. $this->_config = $config;
  58. $this->pmf_lang = $PMF_LANG;
  59. }
  60. /**
  61. * Return the latest news data
  62. *
  63. * @param boolean $showArchive Show archived news
  64. * @param boolean $active Show active news
  65. * @param boolean $forceConfLimit Force to limit in configuration
  66. *
  67. * @return array
  68. */
  69. public function getLatestData($showArchive = false, $active = true, $forceConfLimit = false)
  70. {
  71. $news = array();
  72. $counter = 0;
  73. $now = date('YmdHis');
  74. $query = sprintf("
  75. SELECT
  76. *
  77. FROM
  78. %sfaqnews
  79. WHERE
  80. date_start <= '%s'
  81. AND
  82. date_end >= '%s'
  83. %s
  84. AND
  85. lang = '%s'
  86. ORDER BY
  87. datum DESC",
  88. SQLPREFIX,
  89. $now,
  90. $now,
  91. $active ? "AND active = 'y'" : '',
  92. $this->_config->getLanguage()->getLanguage()
  93. );
  94. $result = $this->_config->getDb()->query($query);
  95. if ($this->_config->get('records.numberOfShownNewsEntries') > 0 && $this->_config->getDb()->numRows($result) > 0) {
  96. while (($row = $this->_config->getDb()->fetchObject($result))) {
  97. $counter++;
  98. if (($showArchive && ($counter > $this->_config->get('records.numberOfShownNewsEntries'))) ||
  99. ((!$showArchive) && (!$forceConfLimit) &&
  100. ($counter <= $this->_config->get('records.numberOfShownNewsEntries'))) ||
  101. ((!$showArchive) && $forceConfLimit)) {
  102. $item = array(
  103. 'id' => $row->id,
  104. 'lang' => $row->lang,
  105. 'date' => $row->datum,
  106. 'lang' => $row->lang,
  107. 'header' => $row->header,
  108. 'content' => $row->artikel,
  109. 'authorName' => $row->author_name,
  110. 'authorEmail' => $row->author_email,
  111. 'dateStart' => $row->date_start,
  112. 'dateEnd' => $row->date_end,
  113. 'active' => ('y' == $row->active),
  114. 'allowComments' => ('y' == $row->comment),
  115. 'link' => $row->link,
  116. 'linkTitle' => $row->linktitel,
  117. 'target' => $row->target);
  118. $news[] = $item;
  119. }
  120. }
  121. }
  122. return $news;
  123. }
  124. /**
  125. * Function for generating the HTML5 code for the current news
  126. *
  127. * @param boolean $showArchive Show archived news
  128. * @param boolean $active Show active news
  129. *
  130. * @return string
  131. */
  132. public function getNews($showArchive = false, $active = true)
  133. {
  134. $output = '';
  135. $news = $this->getLatestData($showArchive, $active);
  136. $date = new PMF_Date($this->_config);
  137. foreach ($news as $item) {
  138. $url = sprintf('%s?action=news&amp;newsid=%d&amp;newslang=%s',
  139. PMF_Link::getSystemRelativeUri(),
  140. $item['id'],
  141. $item['lang']);
  142. $oLink = new PMF_Link($url, $this->_config);
  143. if (isset($item['header'])) {
  144. $oLink->itemTitle = $item['header'];
  145. }
  146. $output .= sprintf(
  147. '<header><h3><a name="news_%d" href="%s">%s <img class="goNews" src="assets/img/more.gif" width="11" height="11" alt="%s" /></a></h3></header>',
  148. $item['id'],
  149. $oLink->toString(),
  150. $item['header'],
  151. $item['header']
  152. );
  153. $output .= sprintf('%s', $item['content']);
  154. if (strlen($item['link']) > 1) {
  155. $output .= sprintf(
  156. '<br />%s <a href="%s" target="_%s">%s</a>',
  157. $this->pmf_lang['msgInfo'],
  158. $item['link'],
  159. $item['target'],
  160. $item['linkTitle']);
  161. }
  162. $output .= sprintf('
  163. <div class="date">%s</div>',
  164. $date->format(PMF_Date::createIsoDate($item['date']))
  165. );
  166. }
  167. return ('' == $output) ? $this->pmf_lang['msgNoNews'] : $output;
  168. }
  169. /**
  170. * Fetches all news headers
  171. *
  172. * @return array
  173. */
  174. public function getNewsHeader()
  175. {
  176. $headers = array();
  177. $now = date('YmdHis');
  178. $query = sprintf("
  179. SELECT
  180. id, datum, lang, header, active, date_start, date_end
  181. FROM
  182. %sfaqnews
  183. WHERE
  184. lang = '%s'
  185. ORDER BY
  186. datum DESC",
  187. SQLPREFIX,
  188. $this->_config->getLanguage()->getLanguage()
  189. );
  190. $result = $this->_config->getDb()->query($query);
  191. if ($this->_config->getDb()->numRows($result) > 0) {
  192. while ($row = $this->_config->getDb()->fetchObject($result)) {
  193. $expired = ($now > $row->date_end);
  194. $headers[] = array(
  195. 'id' => $row->id,
  196. 'lang' => $row->lang,
  197. 'header' => $row->header,
  198. 'date' => PMF_Date::createIsoDate($row->datum),
  199. 'active' => $row->active,
  200. 'expired' => $expired
  201. );
  202. }
  203. }
  204. return $headers;
  205. }
  206. /**
  207. * Fetches a news entry identified by its ID
  208. *
  209. * @param integer $id ID of news
  210. * @param boolean $admin Is admin
  211. *
  212. * @return array
  213. */
  214. function getNewsEntry($id, $admin = false)
  215. {
  216. $news = array();
  217. $query = sprintf(
  218. "SELECT
  219. *
  220. FROM
  221. %sfaqnews
  222. WHERE
  223. id = %d
  224. AND
  225. lang = '%s'",
  226. SQLPREFIX,
  227. $id,
  228. $this->_config->getLanguage()->getLanguage());
  229. $result = $this->_config->getDb()->query($query);
  230. if ($this->_config->getDb()->numRows($result) > 0) {
  231. if ($row = $this->_config->getDb()->fetchObject($result)) {
  232. $content = $row->artikel;
  233. $active = ('y' == $row->active);
  234. $allowComments = ('y' == $row->comment);
  235. $expired = (date('YmdHis') > $row->date_end);
  236. if (!$admin) {
  237. if (!$active) {
  238. $content = $this->pmf_lang['err_inactiveNews'];
  239. }
  240. if ($expired) {
  241. $content = $this->pmf_lang['err_expiredNews'];
  242. }
  243. }
  244. $news = array(
  245. 'id' => $row->id,
  246. 'lang' => $row->lang,
  247. 'date' => PMF_Date::createIsoDate($row->datum),
  248. 'header' => $row->header,
  249. 'content' => $content,
  250. 'authorName' => $row->author_name,
  251. 'authorEmail' => $row->author_email,
  252. 'dateStart' => $row->date_start,
  253. 'dateEnd' => $row->date_end,
  254. 'active' => $active,
  255. 'allowComments' => $allowComments,
  256. 'link' => $row->link,
  257. 'linkTitle' => $row->linktitel,
  258. 'target' => $row->target);
  259. }
  260. }
  261. return $news;
  262. }
  263. /**
  264. * Adds a new news entry
  265. *
  266. * @param array $data Array with news data
  267. *
  268. * @return boolean
  269. */
  270. function addNewsEntry($data)
  271. {
  272. $query = sprintf("
  273. INSERT INTO
  274. %sfaqnews
  275. (id, datum, lang, header, artikel, author_name, author_email, date_start, date_end, active, comment,
  276. link, linktitel, target)
  277. VALUES
  278. (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
  279. SQLPREFIX,
  280. $this->_config->getDb()->nextId(SQLPREFIX.'faqnews', 'id'),
  281. $data['date'],
  282. $data['lang'],
  283. $data['header'],
  284. $data['content'],
  285. $data['authorName'],
  286. $data['authorEmail'],
  287. $data['dateStart'],
  288. $data['dateEnd'],
  289. $data['active'],
  290. $data['comment'],
  291. $data['link'],
  292. $data['linkTitle'],
  293. $data['target']);
  294. if (!$this->_config->getDb()->query($query)) {
  295. return false;
  296. }
  297. return true;
  298. }
  299. /**
  300. * Updates a new news entry identified by its ID
  301. *
  302. * @param integer $id News ID
  303. * @param array $data Array with news data
  304. *
  305. * @return boolean
  306. */
  307. function updateNewsEntry($id, Array $data)
  308. {
  309. $query = sprintf("
  310. UPDATE
  311. %sfaqnews
  312. SET
  313. datum = '%s',
  314. lang = '%s',
  315. header = '%s',
  316. artikel = '%s',
  317. author_name = '%s',
  318. author_email = '%s',
  319. date_start = '%s',
  320. date_end = '%s',
  321. active = '%s',
  322. comment = '%s',
  323. link = '%s',
  324. linktitel = '%s',
  325. target = '%s'
  326. WHERE
  327. id = %d",
  328. SQLPREFIX,
  329. $data['date'],
  330. $data['lang'],
  331. $data['header'],
  332. $data['content'],
  333. $data['authorName'],
  334. $data['authorEmail'],
  335. $data['dateStart'],
  336. $data['dateEnd'],
  337. $data['active'],
  338. $data['comment'],
  339. $data['link'],
  340. $data['linkTitle'],
  341. $data['target'],
  342. $id);
  343. if (!$this->_config->getDb()->query($query)) {
  344. return false;
  345. }
  346. return true;
  347. }
  348. /**
  349. * Deletes a news entry identified by its ID
  350. *
  351. * @param integer $id News ID
  352. *
  353. * @return boolean
  354. */
  355. function deleteNews($id)
  356. {
  357. $query = sprintf("
  358. DELETE FROM
  359. %sfaqnews
  360. WHERE
  361. id = %d
  362. AND
  363. lang = '%s'",
  364. SQLPREFIX,
  365. $id,
  366. $this->language->getLanguage());
  367. if (!$this->_config->getDb()->query($query)) {
  368. return false;
  369. }
  370. return true;
  371. }
  372. }