PageRenderTime 46ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/blocks/guestbook/controller.php

https://github.com/markdev/markandkitty
PHP | 462 lines | 267 code | 60 blank | 135 comment | 27 complexity | 603206eb3ae6536090792f4bbb78b9ec MD5 | raw file
  1. <?php
  2. /**
  3. * @package Blocks
  4. * @subpackage BlockTypes
  5. * @category Concrete
  6. * @author Andrew Embler <andrew@concrete5.org>
  7. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  8. * @license http://www.concrete5.org/license/ MIT License
  9. *
  10. */
  11. /**
  12. * Controller for the guestbook block, which allows site owners to add comments onto any concrete page.
  13. *
  14. * @package Blocks
  15. * @subpackage BlockTypes
  16. * @author Ryan Tyler <ryan@concrete5.org>
  17. * @author Andrew Embler <andrew@concrete5.org>
  18. * @category Concrete
  19. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  20. * @license http://www.concrete5.org/license/ MIT License
  21. *
  22. */
  23. defined('C5_EXECUTE') or die("Access Denied.");
  24. class GuestbookBlockController extends BlockController {
  25. protected $btTable = 'btGuestBook';
  26. protected $btInterfaceWidth = "300";
  27. protected $btInterfaceHeight = "260";
  28. protected $btIncludeAll = 1;
  29. /**
  30. * Used for localization. If we want to localize the name/description we have to include this
  31. */
  32. public function getBlockTypeDescription() {
  33. return t("Adds blog-style comments (a guestbook) to your page.");
  34. }
  35. public function getBlockTypeName() {
  36. return t("Guestbook");
  37. }
  38. function delete() {
  39. $ip = Loader::helper('validation/ip');
  40. if (!$ip->check()) {
  41. $this->set('invalidIP', $ip->getErrorMessage());
  42. return;
  43. }
  44. $c = Page::getCurrentPage();
  45. $E = new GuestBookBlockEntry($this->bID, $c->getCollectionID());
  46. $bo = $this->getBlockObject();
  47. $E->removeAllEntries( $c->getCollectionID() );
  48. parent::delete();
  49. }
  50. /**
  51. * returns the title
  52. * @return string $title
  53. */
  54. function getTitle() {
  55. return $this->title;
  56. }
  57. /**
  58. * returns wether or not to require approval
  59. * @return bool
  60. */
  61. function getRequireApproval() {
  62. return $this->requireApproval;
  63. }
  64. /**
  65. * returns the bool to display the form
  66. * @return bool
  67. */
  68. function getDisplayGuestBookForm() {
  69. return $this->displayGuestBookForm;
  70. }
  71. /**
  72. * Handles the form post for adding a new guest book entry
  73. *
  74. */
  75. function action_form_save_entry() {
  76. $ip = Loader::helper('validation/ip');
  77. if (!$ip->check()) {
  78. $this->set('invalidIP', $ip->getErrorMessage());
  79. return;
  80. }
  81. // get the cID from the block Object
  82. $bo = $this->getBlockObject();
  83. $c = Page::getCurrentPage();
  84. $cID = $c->getCollectionID();
  85. $v = Loader::helper('validation/strings');
  86. $errors = array();
  87. $u = new User();
  88. $uID = intval( $u->getUserID() );
  89. if($this->authenticationRequired && !$u->isLoggedIn()){
  90. $errors['notLogged'] = '- '.t("Your session has expired. Please log back in.");
  91. }elseif(!$this->authenticationRequired){
  92. if(!$v->email($_POST['email'])) {
  93. $errors['email'] = '- '.t("Invalid Email Address");
  94. }
  95. if(!$v->notempty($_POST['name'])) {
  96. $errors['name'] = '- '.t("Name is required");
  97. }
  98. }
  99. // check captcha if activated
  100. if ($this->displayCaptcha) {
  101. $captcha = Loader::helper('validation/captcha');
  102. if (!$captcha->check()) {
  103. $errors['captcha'] = '- '.t("Incorrect captcha code");
  104. }
  105. }
  106. if(!$v->notempty($_POST['commentText'])) {
  107. $errors['commentText'] = '- '.t("a comment is required");
  108. }
  109. if(count($errors)){
  110. $txt = Loader::helper('text');
  111. $E = new GuestBookBlockEntry($this->bID, $c->getCollectionID());
  112. $E->user_name = $txt->sanitize($_POST['name']).'';
  113. $E->user_email = $txt->sanitize($_POST['email']).'';
  114. $E->commentText = $txt->sanitize($_POST['commentText']);
  115. $E->uID = $uID;
  116. $E->entryID = ($_POST['entryID']?$_POST['entryID']:NULL);
  117. $this->set('response', t('Please correct the following errors:') );
  118. $this->set('errors',$errors);
  119. $this->set('Entry',$E);
  120. } else {
  121. $E = new GuestBookBlockEntry($this->bID, $c->getCollectionID());
  122. if($_POST['entryID']) { // update
  123. $bp = $this->getPermissionsObject();
  124. if($bp->canWrite()) {
  125. $E->updateEntry($_POST['entryID'], $_POST['commentText'], $_POST['name'], $_POST['email'], $uID );
  126. $this->set('response', t('The comment has been saved') );
  127. } else {
  128. $this->set('response', t('An Error occured while saving the comment') );
  129. return true;
  130. }
  131. } else { // add
  132. $E->addEntry($_POST['commentText'], $_POST['name'], $_POST['email'], (!$this->requireApproval), $cID, $uID );
  133. $this->set('response', t('Thanks! Your comment has been posted.') );
  134. }
  135. $stringsHelper = Loader::helper('validation/strings');
  136. if( $stringsHelper->email($this->notifyEmail) ){
  137. $c = Page::getCurrentPage();
  138. if(intval($uID)>0){
  139. Loader::model('userinfo');
  140. $ui = UserInfo::getByID($uID);
  141. $fromEmail=$ui->getUserEmail();
  142. $fromName=$ui->getUserName();
  143. }else{
  144. $fromEmail=$_POST['email'];
  145. $fromName=$_POST['name'];
  146. }
  147. $mh = Loader::helper('mail');
  148. $mh->to( $this->notifyEmail );
  149. $mh->addParameter('guestbookURL', Loader::helper('navigation')->getLinkToCollection($c, true));
  150. $mh->addParameter('comment', $_POST['commentText'] );
  151. $mh->from($fromEmail,$fromName);
  152. $mh->load('block_guestbook_notification');
  153. $mh->setSubject( t('Guestbook Comment Notification') );
  154. //echo $mh->body.'<br>';
  155. @$mh->sendMail();
  156. }
  157. }
  158. return true;
  159. }
  160. /**
  161. * gets a list of all guestbook entries for the current block
  162. *
  163. * @param string $order ASC|DESC
  164. * @return array
  165. */
  166. function getEntries($order = "ASC") {
  167. $bo = $this->getBlockObject();
  168. $c = Page::getCurrentPage();
  169. return GuestBookBlockEntry::getAll($this->bID, $c->getCollectionID(), $order);
  170. }
  171. /**
  172. * Loads a guestbook entry and sets the $Entry GuestBookBlockEntry object instance for use by the view
  173. *
  174. * @return bool
  175. */
  176. function action_loadEntry() {
  177. $Entry = new GuestBookBlockEntry($this->bID);
  178. $Entry->loadData($_GET['entryID']);
  179. $this->set('Entry',$Entry);
  180. return true;
  181. }
  182. /**
  183. * deltes a given Entry, sets the response message for use in the view
  184. *
  185. */
  186. function action_removeEntry() {
  187. $ip = Loader::helper('validation/ip');
  188. if (!$ip->check()) {
  189. $this->set('invalidIP', $ip->getErrorMessage());
  190. return;
  191. }
  192. $bp = $this->getPermissionsObject();
  193. if($bp->canWrite()) {
  194. $Entry = new GuestBookBlockEntry($this->bID);
  195. $Entry->removeEntry($_GET['entryID']);
  196. $this->set('response', t('The comment has been removed.') );
  197. }
  198. }
  199. /**
  200. * deltes a given Entry, sets the response message for use in the view
  201. *
  202. */
  203. function action_approveEntry() {
  204. $ip = Loader::helper('validation/ip');
  205. if (!$ip->check()) {
  206. $this->set('invalidIP', $ip->getErrorMessage());
  207. return;
  208. }
  209. $bp = $this->getPermissionsObject();
  210. if($bp->canWrite()) {
  211. $Entry = new GuestBookBlockEntry($this->bID);
  212. $Entry->approveEntry($_GET['entryID']);
  213. $this->set('response', t('The comment has been approved.') );
  214. }
  215. }
  216. /**
  217. * deltes a given Entry, sets the response message for use in the view
  218. *
  219. */
  220. function action_unApproveEntry() {
  221. $ip = Loader::helper('validation/ip');
  222. if (!$ip->check()) {
  223. $this->set('invalidIP', $ip->getErrorMessage());
  224. return;
  225. }
  226. $bp = $this->getPermissionsObject();
  227. if($bp->canWrite()) {
  228. $Entry = new GuestBookBlockEntry($this->bID);
  229. $Entry->unApproveEntry($_GET['entryID']);
  230. $this->set('response', t('The comment has been unapproved.') );
  231. }
  232. }
  233. public function getEntryCount($cID = NULL) {
  234. $ca = new Cache();
  235. $cID = (isset($cID)?$cID:$this->cID);
  236. $count = $ca->get('GuestBookCount',$cID."-".$this->bID);
  237. if(!isset($count) || $count === false) {
  238. $db = Loader::db();
  239. $q = 'SELECT count(bID) as count
  240. FROM btGuestBookEntries
  241. WHERE bID = ?
  242. AND cID = ?
  243. AND approved=1';
  244. $v = array($this->bID, $cID);
  245. $count = $db->getOne($q,$v);
  246. }
  247. return $count;
  248. }
  249. } // end class def
  250. /**
  251. * Manages indevidual guestbook entries
  252. */
  253. class GuestBookBlockEntry {
  254. /**
  255. * blocks bID
  256. * @var integer
  257. */
  258. public $bID;
  259. /**
  260. * page collectionID
  261. * @var integer
  262. */
  263. public $cID;
  264. /**
  265. * blocks uID user id
  266. * @var integer
  267. */
  268. public $uID;
  269. /**
  270. * the entry id
  271. * @var integer
  272. */
  273. public $entryID;
  274. /**
  275. * the user's name
  276. * @var string
  277. */
  278. public $user_name;
  279. /**
  280. * the user's email address
  281. * @var string
  282. */
  283. public $user_email;
  284. /**
  285. * the text for the comment
  286. * @var string
  287. */
  288. public $commentText;
  289. function __construct($bID, $cID = NULL) {
  290. $this->bID = $bID;
  291. $this->cID = $cID;
  292. }
  293. /**
  294. * Loads the object data from the db
  295. * @param integer $entryID
  296. * @return bool
  297. */
  298. function loadData($entryID) {
  299. $db = Loader::db();
  300. $data = $db->getRow("SELECT * FROM btGuestBookEntries WHERE entryID=? AND bID=?",array($entryID,$this->bID));
  301. $this->entryID = $data['entryID'];
  302. $this->user_name = $data['user_name'];
  303. $this->user_email = $data['user_email'];
  304. $this->commentText = $data['commentText'];
  305. $this->uID = $data['uID'];
  306. }
  307. /**
  308. * Adds an entry to the guestbook for the current block
  309. * @param string $comment
  310. * @param string $name
  311. * @param string $email
  312. */
  313. function addEntry($comment, $name, $email, $approved, $cID, $uID=0 ) {
  314. $txt = Loader::helper('text');
  315. $db = Loader::db();
  316. $query = "INSERT INTO btGuestBookEntries (bID, cID, uID, user_name, user_email, commentText, approved) VALUES (?, ?, ?, ?, ?, ?, ?)";
  317. $res = $db->query($query, array($this->bID, $cID, intval($uID), $txt->sanitize($name), $txt->sanitize($email), $txt->sanitize($comment), $approved) );
  318. $this->adjustCountCache(1);
  319. }
  320. /**
  321. * Adjusts cache of count bynumber specified,
  322. *
  323. * Refreshes from db if cache is invalidated or
  324. * false is called in
  325. */
  326. private function adjustCountCache($number=false){
  327. $ca = new Cache();
  328. $db = Loader::db();
  329. $count = $ca->get('GuestBookCount',$this->cID."-".$this->bID);
  330. if($count && $number){
  331. $count += $number;
  332. } else{
  333. $q = 'SELECT count(bID) as count
  334. FROM btGuestBookEntries
  335. WHERE bID = ?
  336. AND cID = ?
  337. AND approved=1';
  338. $v = Array($this->bID, $this->cID);
  339. $rs = $db->query($q,$v);
  340. $row = $rs->FetchRow();
  341. $count = $row['count'];
  342. }
  343. $ca->set('GuestBookCount',$this->cID."-".$this->bID,$count);
  344. }
  345. /**
  346. * Updates the given guestbook entry for the current block
  347. * @param integer $entryID
  348. * @param string $comment
  349. * @param string $name
  350. * @param string $email
  351. * @param string $uID
  352. */
  353. function updateEntry($entryID, $comment, $name, $email, $uID=0 ) {
  354. $db = Loader::db();
  355. $txt = Loader::helper('text');
  356. $query = "UPDATE btGuestBookEntries SET user_name=?, uID=?, user_email=?, commentText=? WHERE entryID=? AND bID=?";
  357. $res = $db->query($query, array($txt->sanitize($name), intval($uID), $txt->sanitize($email),$txt->sanitize($comment),$entryID,$this->bID));
  358. }
  359. /**
  360. * Deletes the given guestbook entry for the current block
  361. * @param integer $entryID
  362. */
  363. function removeEntry($entryID) {
  364. $db = Loader::db();
  365. $query = "DELETE FROM btGuestBookEntries WHERE entryID=? AND bID=?";
  366. $res = $db->query($query, array($entryID,$this->bID));
  367. $this->adjustCountCache(-1);
  368. }
  369. function approveEntry($entryID) {
  370. $db = Loader::db();
  371. $query = "UPDATE btGuestBookEntries SET approved = 1 WHERE entryID=? AND bID=?";
  372. $res = $db->query($query, array($entryID,$this->bID));
  373. $this->adjustCountCache(1);
  374. }
  375. function unApproveEntry($entryID) {
  376. $db = Loader::db();
  377. $query = "UPDATE btGuestBookEntries SET approved = 0 WHERE entryID=? AND bID=?";
  378. $res = $db->query($query, array($entryID,$this->bID));
  379. $this->adjustCountCache(-1);
  380. }
  381. /**
  382. * Deletes all the entries for the current block
  383. */
  384. function removeAllEntries($cID) {
  385. $db = Loader::db();
  386. $query = "DELETE FROM btGuestBookEntries WHERE bID=? AND cID = ?";
  387. $res = $db->query($query, array($this->bID, $cID));
  388. $this->adjustCountCache(false);
  389. }
  390. /**
  391. * gets all entries for the current block
  392. * @param integer $bID
  393. * @param string $order ASC|DESC
  394. * @return array $rows
  395. */
  396. public static function getAll($bID, $cID, $order="ASC") {
  397. $db = Loader::db();
  398. $query = "SELECT * FROM btGuestBookEntries WHERE bID = ? AND cID = ? ORDER BY entryDate {$order}";
  399. $rows = $db->getAll($query,array($bID,$cID));
  400. return $rows;
  401. }
  402. } // end class def