* @copyright 2005 by http://wombat.exit0.net * @package wombatModule * @subpackage Blogger */ /** * Blogger can display simple lists * * @version 1.2.0 * @package wombatModule * @subpackage Blogger */ class wbModule_Blogger extends wbModule { /** * Blogger Parameters * * -tmpldir location of template files * -blog the blog to be used - this must be set! * * @access private * @var array $_params */ var $_params = array( 'tmpldir' => 'module/Blogger', 'blog' => '*', 'id' => null, ); /** * some request defaults * * @access private * @var array $_params */ var $_requestDefaults = array( 'goto' => 'current', 'action' => 'list', 'id' => null ); /** * Blogger config * * @access private * @var array $_config */ protected $_config = null; /** * configBlog * * @access private * @var array $_configBlog */ protected $_configBlog = null; /** * datasource * * @access private * @var object $_ds */ protected $_ds; /** * switch to rss mode * @var bool */ protected $_feedRss = false; /** * current feed data * @var array */ protected $_feedData = false; /** * format for feed item ids * @var string */ protected $_feedId = false; /** * format for feed item links * @var string */ protected $_feedLink = false; /** * content fetcher * @var wbContentManager */ protected $_feedCm = false; /** * load required objects * * @access public * @return boolean $result true on success */ function __construct() { $this->_ds = wbFactory::singleton( 'wbDatasource' ); $this->_ds->setCallback( $this ); $this->_tmpl = wbFactory::singleton( 'patTemplate' ); $cats = $this->_ds->getEntries( 'blogcategory' ); $this->_config = array(); foreach( $cats as $c ) { $this->_config[$c['id']] = $c; } $this->_config['*'] = array( 'id' => '*', 'title' => 'any', 'brief' => 'all blogs', 'commentable' => 'user', 'flags' => '' ); } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbModule_Blogger() { $this->__construct(); } /** * display RSS feed * * * @param mixed $blog list of blogs to feed * @param array $config override some feed config variables * @return string $xml */ public function feed( $blog = '*', $config = array( 'path' => 'index' ) ) { if( empty( $blog ) ) { throw new wbException( 'No channel selected', 'module:blogger:1' ); } if( !is_array( $blog ) ) { $blog = array_map( 'trim', explode( ',', trim( $blog ) ) ); } foreach( $blog as $b ) { if( !isset( $this->_config[$b] ) ) { throw new wbException( 'Could not find channel "'. $b .'" in list of available blog catagories', 'module:blogger:2' ); } } if( count( $blog ) == 1 ) { $config = array_merge( $this->_config[$blog[0]], $config ); } else { $config = array_merge( $this->_config['*'], $config ); sort( $blog ); } wbFactory::includeClass( 'XML_Serializer' ); $opts = array( 'rootName' => 'rss', 'rootAttributes' => array( 'version' => '2.0' ), 'indent' => ' ', 'defaultTagName' => 'item', 'addDecl' => true, 'encoding' => 'utf-8', ); $ser = new XML_Serializer( $opts ); $siteConfig = wbFactory::getParam( 'config' ); $this->_feedCm = wbFactory::singleton( 'wbContentManager' ); $this->_feedRss = true; $this->_feedId = $siteConfig['url'] . implode( ',', $blog ) . '/%s'; $this->_feedLink = 'http://' . $siteConfig['url'] . '?path=' . $config['path'] . '&id=%s'; $this->_feedData['channel'] = array( 'ttl' => 60, 'link' => 'http://' . $siteConfig['url'], 'title' => $config['title'], 'description' => $config['brief'] ); $opts = array( 'blog' => array( 'limit' => 20 ) ); $this->_ds->setOptions( $opts ); $clause = array(); if( $blog[0] != '*' ) { $clause[] = array( 'field' => $this->_ds->getPrimaryKey( 'blogcategory' ), 'relation' => 'in', 'value' => $blog ); } $items = $this->_ds->getEntries( 'blog', null, $clause, 0 ); if( patErrorManager::isError( $items ) ) { return $items; } $ser->serialize( $this->_feedData ); $xml = $ser->getSerializedData(); return $xml; } /** * recieve content * * * * @return string $html */ public function getHtml() { $blogs = trim( $this->_params['blog'] ); // blog must be set! if( empty( $this->_params['blog'] ) ) { return patErrorManager::raiseError( 'wbModule:Blogger:1', 'Cannot process Bloggger', 'Configuration error - parameter "blog" is required!' ); } // in case there is more then one blog if( strstr( $blogs, ',' ) ) { $blogs = array_map( 'trim', explode( ',', $this->_params['blog'] ) ); } else { $blogs = array( $blogs ); } foreach( $blogs as $b ) { if( !isset( $this->_config[$b] ) ) { return patErrorManager::raiseError( 'wbModule:Blogger:2', 'Cannot process Bloggger', 'Could not find blog "'. $b .'" in list of available blog catagories' ); } } if( count( $blogs ) == 1 ) { $this->_configBlog = $this->_config[$this->_params['blog']]; $config = $this->_configBlog; foreach( $config as &$c ) { if( !is_scalar( $c ) ) { $c = count( $c ); } } $this->_tmpl->addGlobalVars( $config, 'blogconfig_' ); } else { $this->_configBlog = $this->_config['*']; } $this->_configBlog['categories'] = $blogs; // use preselected entry from parameter? if( $this->_params['id'] ) { return $this->_getView( $this->_params['id'] ); } switch( strtolower( $this->_request['action'] ) ) { case 'comment': return $this->_getComment( $this->_request['id'] ); break; case 'commentadd': return $this->_getComment( $this->_request['id'], true ); break; case 'view': return $this->_getView( $this->_request['id'] ); break; default: return $this->_getList(); break; } // this should never happen return true; } /** * Display entry and add comment * * requires id, id may be either the primary key or "first" * * @access public * @param int $id * @param bool whether a comment should be added * @return string $html */ function _getComment( $id, $add = false ) { if( !$id ) { wbDebugger::addMsg( 'Blogger', 'No id given - fallback to list-view', 'Details' ); return $this->_getList(); } // named ids? $clause = array(); $clause[] = array( 'value' => $id, 'field' => $this->_ds->getPrimaryKey( 'blog' ) ); $entry = $this->_ds->getEntries( 'blog', null, $clause ); if( patErrorManager::isError( $entry ) ) { return $entry; } $entry = $entry[0]; $category = $entry[$this->_ds->getPrimaryKey( 'blogcategory' )]; $params = array( 'template' => 'comment', 'template_dir' => $this->_params['tmpldir'], ); $user =& wbFactory::singleton( 'wbUser' ); if( !$user->isAuthenticated() ) { $add = false; } $form =& wbFactory::singleton( 'patForms', $params ); if( $add && isset( $this->_request['save'] ) ) { $form->setSubmitted( true ); if( $form->validateForm() && !$this->_checkReload() ) { $eCode = array( 'blogger', 'comment', 'autoapproved', $this->_params['blog'] ); $data = $form->getValues(); $data['flags'] = 'approved'; $data['blogid'] = $id; if( $this->_config[$category]['flags'] == 'moderated' ) { $eCode[2] = 'moderated'; $data['flags'] = 'new'; } $data[$this->_ds->getPrimaryKey( 'user' )] = $user->getUserId(); // save data $data['created'] = gmdate( 'Y-m-d H:i:s' ); $data['changed'] = $data['created']; $this->_ds->save( 'blogcomment', 'new', $data ); $this->_tmpl->addGlobalVar( 'comment_added', 'yes' ); $eLink = array( 'app' => 'Blogger/' . $data['blogid']. '/View/' . $id, ); wbEvent::trigger( implode( ':', $eCode ), _( 'New comment for "{BLOG}" saved' ), $data, $eLink ); } } $pager = array( 'goto' => 'first' ); if( isset( $this->_request['goto'] ) ) { $pager['goto'] = $this->_request['goto']; } $comments = $this->_getComments( $id, $pager ); $pager['id'] = $id; $form->renderForm( array( 'template' => 'wbModule_Blogger', 'errorTemplateContainer' => 'formErrors', 'errorTemplate' => 'formErrors_entry' ) ); $this->_tmpl->addVars( 'wbModule_Blogger', $entry ); $this->_tmpl->addGlobalVars( $pager, 'PAGER_' ); $this->_tmpl->addRows( 'list_entry', $comments ); return $this->_tmpl->getParsedTemplate( 'wbModule_Blogger' ); } /** * display details of news entry * * requires id, id may be either the primary key or "first" * * @access public * @param int $id * @return string $html */ function _getView( $id ) { if( !$id ) { wbDebugger::addMsg( 'Blogger', 'No id given - fallback to list-view', 'Details' ); return $this->_getList(); } // named ids? $clause = array(); $clause[] = array( 'value' => $id, 'field' => $this->_ds->getPrimaryKey( 'blog' ) ); $entry = $this->_ds->getEntries( 'blog', null, $clause ); if( patErrorManager::isError( $entry ) ) { return $entry; } $entry = $entry[0]; $pager = array( 'goto' => 'first' ); if( isset( $this->_request['goto'] ) ) { $pager['goto'] = $this->_request['goto']; } $comments = $this->_getComments( $id, $pager ); $this->_loadTemplates( 'view' ); $this->_tmpl->addGlobalVars( $pager, 'PAGER_' ); $this->_tmpl->addVars( 'wbModule_Blogger', $entry ); $this->_tmpl->addRows( 'list_entry', $comments ); return $this->_tmpl->getParsedTemplate( 'wbModule_Blogger' ); } /** * display list * * @access public * @return string $html */ function _getList() { $clause = array(); if( $this->_configBlog['categories'][0] != '*' ) { $clause[] = array( 'field' => $this->_ds->getPrimaryKey( 'blogcategory' ), 'relation' => 'in', 'value' => $this->_configBlog['categories'] ); } $pager = $this->_ds->getPager( 'blog', $this->_request['goto'], null, $clause ); if( patErrorManager::isError( $pager ) ) { return $pager; } $this->_tmpl->addGlobalVars( $pager, 'PAGER_' ); $offset = $pager['offset'] * $pager['limit']; $list = $this->_ds->getEntries( 'blog', null, $clause, $offset ); if( patErrorManager::isError( $list ) ) { return $list; } $this->_loadTemplates( 'list' ); if( !$this->_tmpl->exists( 'comment_list' ) ) { $this->_tmpl->addRows( 'list_entry', $list ); return $this->_tmpl->getParsedTemplate( 'wbModule_Blogger' ); } // add comments for( $i = 0; $i < count( $list ); ++$i ) { // comment pager $pager = array( 'goto' => 'first' ); if( isset( $this->_request['goto'] ) ) { $pager['goto'] = $this->_request['goto']; } $comments = $this->_getComments( $list[$i]['id'], $pager ); $pager['id'] = $list[$i]['id']; $this->_tmpl->addVars( 'comment_list', $pager, 'pager_' ); $this->_tmpl->addVars( 'list_entry', $list[$i] ); if( count( $comments ) ) { $this->_tmpl->addRows( 'comment_list_entry', $comments ); } $this->_tmpl->parseTemplate( 'list_entry', 'a' ); $this->_tmpl->clearTemplate( 'comment_list_entry' ); } return $this->_tmpl->getParsedTemplate( 'wbModule_Blogger' ); } /** * receive paged list of comments * * @param string $blog blog id * @param array $pager prototype of pager * @return */ function _getComments( $blog, &$pager = null ) { $clause = array(); $clause[] = array( 'field' => 'blogid', 'value' => $blog ); $clause[] = array( 'field' => 'flags', 'relation' => 'in_set', 'value' => 'approved' ); $offset = 0; if( is_array( $pager ) && isset( $pager['goto'] ) ) { $pager = $this->_ds->getPager( 'blogcomment', $pager['goto'], null, $clause ); if( patErrorManager::isError( $pager ) ) { return $pager; } $offset = $pager['offset'] * $pager['limit']; } $comments = $this->_ds->getEntries( 'blogcomment', null, $clause, $offset ); return $comments; } /** * callback function * * @todo there is a hack that replaces "rss.php" with "index.php" * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentries( $table, &$data, $id ) { $data['id'] = $id; if( $this->_feedRss ) { $time = strtotime( $data['created'] ); $item = array( 'title' => $data['title'], 'description' => str_replace( '/rss.php?', '/index.php?', $this->_feedCm->transform( $data['body'], 'html' ) ), 'author' => $data['realname'], 'guid' => sprintf( $this->_feedId, $id ), 'link' => sprintf( $this->_feedLink, $id ), 'pubDate' => date( 'd M Y h:i:s', $time ) . ' GMT', //'dc:creator' => $data['realname'], //'dc:date' => date( 'Y-m-dTh:i:s', $time ), ); $this->_feedData['channel'][] = $item; } return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentry( $table, &$data, $id ) { $data['id'] = $id; return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForSave( $table, &$data, $id ) { return true; } } ?>