/components/com_kunena/lib/kunena.announcement.class.php

https://github.com/rich20/Kunena-1.6 · PHP · 216 lines · 182 code · 20 blank · 14 comment · 35 complexity · e4caa80eac51c47cf1e086109dddfce7 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * Kunena Component
  5. * @package Kunena
  6. *
  7. * @Copyright (C) 2008 - 2011 Kunena Team. All rights reserved.
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  9. * @link http://www.kunena.org
  10. **/
  11. // Dont allow direct linking
  12. defined ( '_JEXEC' ) or die ();
  13. class CKunenaAnnouncement {
  14. public $id = null;
  15. public $title = null;
  16. public $description = null;
  17. public $sdescription = null;
  18. public $created = null;
  19. public $published = 1;
  20. public $showdate = 1;
  21. public $announcement = null;
  22. public $canEdit = false;
  23. function __construct() {
  24. $this->my = JFactory::getUser ();
  25. $this->db = JFactory::getDBO ();
  26. $this->config = KunenaFactory::getConfig ();
  27. $this->app = JFactory::getApplication ();
  28. $annmods = @explode ( ',', $this->config->annmodid );
  29. if (in_array ( $this->my->id, $annmods ) || CKunenaTools::isAdmin ()) {
  30. $this->canEdit = true;
  31. } else {
  32. $this->canEdit = false;
  33. }
  34. $this->announcement = new stdClass();
  35. $this->announcement->id = 0;
  36. $this->announcement->title = '';
  37. $this->announcement->description = '';
  38. $this->announcement->sdescription = '';
  39. $this->announcement->created = '';
  40. $this->announcement->published = 1;
  41. $this->announcement->showdate = 1;
  42. }
  43. public function &getInstance() {
  44. static $instance = NULL;
  45. if (! $instance) {
  46. $instance = new CKunenaAnnouncement ();
  47. }
  48. return $instance;
  49. }
  50. function edit($id) {
  51. if (! $this->canEdit) {
  52. while (@ob_end_clean());
  53. $this->app->redirect ( CKunenaLink::GetKunenaURL ( false ), JText::_ ( 'COM_KUNENA_POST_NOT_MODERATOR' ) );
  54. }
  55. if ($this->tokenProtection ())
  56. return false;
  57. $now = new JDate();
  58. $title = JRequest::getVar ( "title", "" );
  59. $description = JRequest::getVar ( 'description', '', 'string', JREQUEST_ALLOWRAW );
  60. $sdescription = JRequest::getVar ( 'sdescription', '', 'string', JREQUEST_ALLOWRAW );
  61. $created = JRequest::getVar ( "created", $now->toMysql() );
  62. if (!$created) $created = $now->toMysql();
  63. $published = JRequest::getInt ( "published", 1 );
  64. $showdate = JRequest::getInt ( "showdate", 1 );
  65. if (!$id) {
  66. $query = "INSERT INTO #__kunena_announcement VALUES ('',
  67. {$this->db->Quote ( $title )},
  68. {$this->db->Quote ( $sdescription )},
  69. {$this->db->Quote ( $description )},
  70. {$this->db->Quote ( $created )},
  71. {$this->db->Quote ( $published )},
  72. 0,
  73. {$this->db->Quote ( $showdate )})";
  74. $msg = JText::_ ( 'COM_KUNENA_ANN_SUCCESS_ADD' );
  75. } else {
  76. $query = "UPDATE #__kunena_announcement SET title={$this->db->Quote ( $title )},
  77. description={$this->db->Quote ( $description )},
  78. sdescription={$this->db->Quote ( $sdescription )},
  79. created={$this->db->Quote ( $created )},
  80. published={$this->db->Quote ( $published )},
  81. showdate={$this->db->Quote ( $showdate )}
  82. WHERE id=$id";
  83. $msg = JText::_ ( 'COM_KUNENA_ANN_SUCCESS_EDIT' );
  84. }
  85. $this->db->setQuery ( $query );
  86. if ($this->db->query ()) {
  87. while (@ob_end_clean());
  88. $this->app->redirect ( CKunenaLink::GetAnnouncementURL ( 'show', null, false ), $msg );
  89. }
  90. if (KunenaError::checkDatabaseError()) return;
  91. }
  92. function delete($id) {
  93. if (! $this->canEdit) {
  94. while (@ob_end_clean());
  95. $this->app->redirect ( CKunenaLink::GetKunenaURL ( false ), JText::_ ( 'COM_KUNENA_POST_NOT_MODERATOR' ) );
  96. }
  97. $query = "DELETE FROM #__kunena_announcement WHERE id={$this->db->Quote ($id)} ";
  98. $this->db->setQuery ( $query );
  99. $this->db->query ();
  100. if (KunenaError::checkDatabaseError()) return;
  101. while (@ob_end_clean());
  102. $this->app->redirect ( CKunenaLink::GetAnnouncementURL ( 'show', null, false ), JText::_ ( 'COM_KUNENA_ANN_DELETED' ) );
  103. }
  104. function getAnnouncement($id = 0, $mode=0) {
  105. if ($mode) $published = "AND published='1'";
  106. else $published = "";
  107. if (! $id) {
  108. $query = "SELECT * FROM #__kunena_announcement WHERE published='1' ORDER BY created DESC";
  109. } else {
  110. $query = "SELECT * FROM #__kunena_announcement WHERE id={$this->db->Quote($id)} $published";
  111. }
  112. $this->db->setQuery ( $query, 0, 1 );
  113. $announcement = $this->db->loadObject ();
  114. if (KunenaError::checkDatabaseError()) return;
  115. if (! $announcement) {
  116. return;
  117. }
  118. // TODO: deprecated
  119. $this->id = $announcement->id;
  120. $this->title = KunenaParser::parseText ( $announcement->title );
  121. $this->sdescription = KunenaParser::parseBBCode ( $announcement->sdescription );
  122. $this->description = KunenaParser::parseBBCode ( $announcement->description );
  123. $this->created = $announcement->created;
  124. $this->published = $announcement->published;
  125. $this->showdate = $announcement->showdate;
  126. // end deprecated
  127. $this->announcement = $announcement;
  128. }
  129. function getAnnouncements($start, $limit) {
  130. $query = "SELECT * FROM #__kunena_announcement ORDER BY created DESC";
  131. $this->db->setQuery ( $query, $start, $limit );
  132. $this->announcements = $this->db->loadObjectList ();
  133. if (KunenaError::checkDatabaseError()) return;
  134. if (empty ( $this->announcement )) {
  135. return;
  136. }
  137. }
  138. function displayBox() {
  139. if ($this->config->showannouncement && $this->id) {
  140. CKunenaTools::loadTemplate ( '/announcement/box.php' );
  141. }
  142. }
  143. function display() {
  144. if (! $this->config->showannouncement) {
  145. return;
  146. }
  147. $do = JRequest::getVar ( "do", "" );
  148. $id = intval ( JRequest::getVar ( "id", "" ) );
  149. switch ($do) {
  150. case 'read' :
  151. $this->getAnnouncement ( $id, 1 );
  152. CKunenaTools::loadTemplate ( '/announcement/read.php' );
  153. break;
  154. case 'show' :
  155. $this->getAnnouncements ( 0, 5 );
  156. CKunenaTools::loadTemplate ( '/announcement/show.php' );
  157. break;
  158. case 'edit' :
  159. if (!$this->canEdit) {
  160. while (@ob_end_clean());
  161. $this->app->redirect ( CKunenaLink::GetKunenaURL( false ), JText::_( 'COM_KUNENA_POST_NOT_MODERATOR' ));
  162. return;
  163. }
  164. $this->getAnnouncement ( $id, 0 );
  165. // Continue
  166. case 'add' :
  167. if (!$this->canEdit) {
  168. while (@ob_end_clean());
  169. $this->app->redirect(CKunenaLink::GetKunenaURL(false), JText::_('COM_KUNENA_POST_NOT_MODERATOR'));
  170. return;
  171. }
  172. CKunenaTools::loadTemplate ( '/announcement/edit.php' );
  173. break;
  174. case 'delete' :
  175. $this->delete ( $id );
  176. break;
  177. case 'doedit' :
  178. $this->edit ( $id );
  179. break;
  180. default :
  181. $this->getAnnouncements(0, 5);
  182. CKunenaTools::loadTemplate ( '/announcement/show.php' );
  183. }
  184. }
  185. function escape($var) {
  186. return htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
  187. }
  188. function tokenProtection() {
  189. // get the token put in the message form to check that the form has been valided successfully
  190. if (JRequest::checkToken () == false) {
  191. $this->app->enqueueMessage ( JText::_ ( 'COM_KUNENA_ERROR_TOKEN' ), 'error' );
  192. return true;
  193. }
  194. return false;
  195. }
  196. }