| // +---------------------------------------------------------------------------+ // $Id$ require_once 'DB/DataObject.php'; /** * Allows users to leave guestbook entries. * * @package guestbook * @author Boris Kerbikov * @version $Revision: 1.22 $ * @since PHP 4.1 */ class GuestbookMgr extends SGL_Manager { function GuestbookMgr() { SGL::logMessage(null, PEAR_LOG_DEBUG); parent::SGL_Manager(); $this->pageTitle = 'Guestbook Manager'; $this->template = 'guestbookList.html'; $this->_aActionsMapping = array( 'add' => array('add'), 'insert' => array('insert', 'redirectToDefault'), 'list' => array('list'), ); } function validate($req, &$input) { SGL::logMessage(null, PEAR_LOG_DEBUG); $this->validated = true; $input->error = array(); $input->pageTitle = $this->pageTitle; $input->masterTemplate = $this->masterTemplate; $input->template = $this->template; $input->action = ($req->get('action')) ? $req->get('action') : 'list'; $input->submitted = $req->get('submitted'); $input->guestbook = (object)$req->get('guestbook'); if ($input->submitted || in_array($input->action, array('insert', 'update'))) { require_once 'Validate.php'; $v = & new Validate(); if (empty($input->guestbook->name)) { $aErrors['name'] = 'Please, specify your name'; } if (empty($input->guestbook->email)) { $aErrors['email'] = 'Please, specify your email'; } elseif (!$v->email($input->guestbook->email)) { $aErrors['email'] = 'Your email is not correctly formatted'; } if (empty($input->guestbook->message)) { $aErrors['message'] = 'Please, fill in the message text'; } if ($this->conf['GuestbookMgr']['useCaptcha']) { require_once SGL_CORE_DIR . '/Captcha.php'; $captcha = new SGL_Captcha(); if (!$captcha->validateCaptcha($input->guestbook->captcha)) { $aErrors['captcha'] = 'You must enter the number in this field'; } $input->captcha = $captcha->generateCaptcha(); $input->useCaptcha = true; } } // if errors have occured if (isset($aErrors) && count($aErrors)) { SGL::raiseMsg('Please fill in the indicated fields'); $input->error = $aErrors; $input->template = 'guestbookAdd.html'; $this->validated = false; // save currect title if ('insert' == $input->action) { $input->pageTitle .= ' :: Add'; } } } function _cmd_add(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $output->pageTitle = 'Guestbook Manager :: Add'; $output->template = 'guestbookAdd.html'; // build ordering select object $output->guestbook = DB_DataObject::factory($this->conf['table']['guestbook']); if ($this->conf['GuestbookMgr']['useCaptcha']) { require_once SGL_CORE_DIR . '/Captcha.php'; $captcha = new SGL_Captcha(); $output->captcha = $captcha->generateCaptcha(); $output->useCaptcha = true; } } function _cmd_insert(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); SGL_DB::setConnection(); $newEntry = DB_DataObject::factory($this->conf['table']['guestbook']); $newEntry->setFrom($input->guestbook); $newEntry->guestbook_id = $this->dbh->nextId($this->conf['table']['guestbook']); $newEntry->date_created = SGL_Date::getTime(true); $success = $newEntry->insert(); if ($success) { SGL::raiseMsg('new guestbook entry saved successfully', true, SGL_MESSAGE_INFO); } else { SGL::raiseError('There was a problem inserting the record', SGL_ERROR_NOAFFECTEDROWS); } if($this->conf['GuestbookMgr']['sendNotificationEmail']) { $this->sendEmail($newEntry, $input->moduleName); } } function _cmd_list(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $output->pageTitle = 'Welcome to our Guestbook'; $query = " SELECT guestbook_id, date_created, name, email, message FROM {$this->conf['table']['guestbook']} ORDER BY guestbook_id DESC"; $limit = $_SESSION['aPrefs']['resPerPage']; $pagerOptions = array( 'mode' => 'Sliding', 'delta' => 3, 'perPage' => $limit, ); $aPagedData = SGL_DB::getPagedData($this->dbh, $query, $pagerOptions); $output->aPagedData = $aPagedData; if (is_array($aPagedData['data']) && count($aPagedData['data'])) { $output->pager = ($aPagedData['totalItems'] <= $limit) ? false : true; } } function sendEmail($oEntry, $moduleName) { SGL::logMessage(null, PEAR_LOG_DEBUG); require_once SGL_CORE_DIR . '/Emailer.php'; $options = array( 'toEmail' => $this->conf['email']['info'], 'toRealName' => 'Admin', 'fromEmail' => "\"{$oEntry->name}\" <{$oEntry->email}>", 'fromEmailAdress' => $oEntry->email, 'fromRealName' => $oEntry->name, 'replyTo' => $oEntry->email, 'subject' => SGL_String::translate('New guestbook entry in') .' '. $this->conf['site']['name'], 'deleteURL' => SGL_Output::makeUrl('delete','guestbookadmin','guestbook',array(),'frmDelete[]|'.$oEntry->guestbook_id), 'body' => $oEntry->message, 'template' => SGL_THEME_DIR . '/' . $_SESSION['aPrefs']['theme'] . '/' . $moduleName . '/email_guestbook_notification.php', ); $message = & new SGL_Emailer($options); $ok = $message->prepare(); return ($ok) ? $message->send() : $ok; } } ?>