| // +---------------------------------------------------------------------------+ // $Id$ require_once 'DB/DataObject.php'; /** * RndMsgMgr class, for managing random messages * * @package randommsg * @author Michael Willemot * @copyright Michael Willemot 2004 * @version 0.4 */ class RndMsgMgr extends SGL_Manager { function RndMsgMgr() { SGL::logMessage(null, PEAR_LOG_DEBUG); parent::SGL_Manager(); $this->pageTitle = 'RndMsg Manager :: Browse'; $this->masterTemplate = 'masterLeftCol.html'; $this->template = 'rndMsg.html'; $this->_aActionsMapping = array( 'add' => array('add'), 'insert' => array('insert', 'redirectToDefault'), 'delete' => array('delete', 'redirectToDefault'), 'list' => array('list'), ); $this->_allowedFileTypes = array( 'txt', 'csv', ); $this->crlf = SGL_String::getCrlf(); } 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->msgDelete = $req->get('frmMsgDelete'); $input->addMsgsText = $req->get('addMsgsText'); $input->submitted = ($req->get('submitted') || $req->get('upload')); // request values for upload $input->msgUpload = $req->get('upload'); $input->msgFileArray = $req->get('msgFile'); if (is_array($input->msgFileArray)) { $input->msgFileName = $input->msgFileArray['name']; $input->msgFileType = $input->msgFileArray['type']; $input->msgFileTmpName = $input->msgFileArray['tmp_name']; $input->msgFileSize = $input->msgFileArray['size']; } $aErrors = array(); if ($input->submitted) { if ($input->msgUpload) { if ($input->msgFileSize < 1) { $aErrors['noUpload'] = 'Please select a file to upload'; } else { $ext = end(explode('.', $input->msgFileName)); // check uploaded file is of valid type if (!in_array(strtolower($ext), $this->_allowedFileTypes)) { $aErrors['invalidType'] = 'Error: Not a recognised file type'; } } } else { if ($input->addMsgsText == '') { $aErrors['noMessage'] = 'Please enter one or more messages'; } } } // if errors have occured if (isset($aErrors) && count($aErrors)) { SGL::raiseMsg('Please fill in the indicated fields'); $input->error = $aErrors; $input->template = 'rndMsgAdd.html'; $this->validated = false; // fix page title if ('insert' == $input->action) { $input->pageTitle = 'RndMsg Manager :: Add'; } } } function _cmd_add(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $output->pageTitle = 'RndMsg Manager :: Add'; $output->template = 'rndMsgAdd.html'; } function _cmd_insert(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); SGL_DB::setConnection(); $output->template = 'rndMsg.html'; if ($input->msgUpload) { $aLines = $this->file2($input->msgFileTmpName); } else { $aLines = split($this->crlf, $input->addMsgsText); } $success = true; foreach ($aLines as $rndmsg) { if (trim($rndmsg) != '') { $msg = DB_DataObject::factory($this->conf['table']['rndmsg_message']); $msg->msg = trim($rndmsg); $msg->rndmsg_message_id = $this->dbh->nextId($this->conf['table']['rndmsg_message']); $success = ($success && $msg->insert()); } } if ($success) { SGL::raiseMsg('One or more messages successfully added.'); } else { SGL::raiseError('There was a problem inserting the record', SGL_ERROR_NOAFFECTEDROWS); } } function _cmd_delete(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); if (is_array($input->msgDelete)) { foreach ($input->msgDelete as $index => $msgId){ $rm = DB_DataObject::factory($this->conf['table']['rndmsg_message']); $rm->get($msgId); $rm->delete(); unset($rm); } } else { SGL::raiseError('Incorrect parameter passed to ' . __CLASS__ . '::' . __FUNCTION__, SGL_ERROR_INVALIDARGS); } SGL::raiseMsg('Message(s) successfully removed.'); } function _cmd_list(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $output->template = 'rndMsg.html'; $query = " SELECT rndmsg_message_id, msg FROM {$this->conf['table']['rndmsg_message']}"; $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; } } /** * Splits text files into an array of strings * * Can handle files whose line endings are whatever * (*nix), (M$) or (Mac) */ function file2($filename) { $fp = fopen($filename, "rb"); $buffer = fread($fp, filesize($filename)); fclose($fp); $lines = preg_split("/\r?\n|\r/", $buffer); return $lines; } } ?>