* @copyright 2004 by gERD Schaufelberger * @package wombat * @subpackage admin */ /** * admin interface * * * Table definition comes from configuration, still, * the following fields are required: * * - 'key' name it as you like * - uid user id * - title title to be displayed in lists etc (can be changed with attribute titlefield) * - created datetime * - changed datetime * * all other fields are defined in admin.xml in section blogger. * Those definitions are used to create patForm-Elements, as well * See documentation of patForms * * @version 1.1.0 * @package wombat * @subpackage Admin */ class wbAdminApp_Blogger extends wbAdminApp { /** * default values for request variables * @access protected * @var array $_requestDefaults * @see $_request */ var $_requestDefaults = array( 'action' => '_no_action_set', 'goto' => 'first', ); /** * name of this application * @var string $_appName */ var $_appName = 'Blogger'; /** * blog name * @var string */ var $_blog = null; /** * datasource object * @var wbDatasource */ var $_ds = null; /** * configuration of all blogs * @var string */ var $_configBlog = null; /** * initilaize clas * * @access public */ function __construct() { $this->_auth = wbFactory::singleton( 'wbAuth' ); $this->_tmpl = wbFactory::singleton( 'patTemplate' ); $conf = wbFactory::singleton( 'patConfiguration' ); $this->_ds = wbFactory::create( 'wbDatasource' ); $this->_ds->setCallback( $this ); $cats = $this->_ds->getEntries( 'blogcategory' ); $this->_configBlog = array(); foreach( $cats as $cat ) { if( isset( $cat['other'] ) ) { $cat['others'] = array_map( 'trim', explode( ',', $cat['other'] ) ); } else { $cat['others'] = array(); } $this->_configBlog[$cat['id']] = $cat; } } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbAdminApp_Blogger() { $this->__construct(); } /** * run admin interface * * @access public * @param array $path * @return boolean true on success */ function process( $path ) { if( empty( $this->_configBlog ) ) { $this->_blog = 'new'; return $this->_processConfigure( $path ); } // find blog if( !empty( $path ) ) { $this->_blog = array_shift( $path ); // allow to create a new blog category if( $this->_blog == 'new' ) { return $this->_processConfigure( $path ); } if( !isset( $this->_configBlog[$this->_blog] ) ) { wbDebugger::addMsg( 'Blogger', 'Recieved undefined blog name: "' . $this->_blog . '"', 'Blog' ); $this->_blog = false; } } $this->_tmpl->addGlobalVar( 'blog_name', $this->_blog ); $action = 'List'; if( !empty( $path ) ) { $action = array_shift( $path ); } // no blog selected! if( !$this->_blog ) { return $this->_processBlogList(); } $function = '_process' . $action; if( !method_exists( $this, $function ) ) { return $this->_processBlogList(); } return $this->$function( $path ); } /** * recieve submenu items * * Submenu contains all blogs * * @access public * @return mixed $submenu */ function getMenu() { $menu = array(); foreach( $this->_configBlog as $blog ) { $m = array( 'name' => $blog['id'], 'app' => $this->_appName . '/' . $blog['id'], 'title' => $blog['title'], 'brief' => $blog['brief'], ); array_push( $menu, $m ); } return $menu; } /** * display blog list * * @access private * @return boolean true on success */ function _processBlogList( ) { $this->_loadTemplates( 'index' ); $this->_tmpl->addRows( 'blog_entry', array_values( $this->_configBlog ) ); return true; } /** * display blog list * * @access private * @return boolean true on success */ function _processList() { if( !isset( $this->_configBlog[$this->_blog] ) ) { return $this->_processBlogList(); } $conf = $this->_configBlog[$this->_blog]; $this->_loadTemplates( 'pager', false ); $this->_loadTemplates( 'list' ); $this->_ds->setOptions( array( 'blog' => array( 'limit' => 20 ) ) ); $owner = null; if( !in_array( 'list', $this->_configBlog[$this->_blog]['others'] ) ) { $owner = $this->_auth->getUserId(); } $clause = array(); $clause[] = array( 'field' => $this->_ds->getPrimaryKey( 'blogcategory' ), 'value' => $this->_blog ); $pager = $this->_ds->getPager( 'blog', $this->_request['goto'], $owner, $clause ); if( patErrorManager::isError( $pager ) ) { return $pager; } $offset = $pager['offset'] * $pager['limit']; $pager['app'] = 'Blogger/' . $this->_blog . '/List'; $this->_tmpl->addGlobalVars( $pager, 'pager_' ); // finally we get the list $list = $this->_ds->getEntries( 'blog', $owner, $clause, $offset ); if( patErrorManager::isError( $list ) ) { return $list; } $this->_tmpl->addRows( 'blog_entry', $list ); return true; } /** * show blog entry * * @access private * @todo add user data! * @return boolean true on success */ function _processView( $path ) { $conf = $this->_configBlog[$this->_blog]; if( !isset( $path[0] ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } $id = $path[0]; $data = $this->_ds->getEntry( 'blog', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } $this->_loadTemplates( 'view' ); // add user data? $this->_tmpl->addGlobalVars( $data, 'entry_' ); // list comments $this->_loadTemplates( 'pager', false ); $clause = array(); $clause[] = array( 'field' => 'blogid', 'value' => $id ); $goto = 'first'; if( $this->_request['goto'] ) { $goto = $this->_request['goto']; } $pager = $this->_ds->getPager( 'blogcomment', $goto, null, $clause ); if( patErrorManager::isError( $pager ) ) { return $pager; } $pager['app'] = 'Blogger/' . $this->_blog . '/View/' . $id; $offset = $pager['offset'] * $pager['limit']; $comments = $this->_ds->getEntries( 'blogcomment', null, $clause, $offset ); $this->_tmpl->addGlobalVar( 'blog_id', $id ); $this->_tmpl->addGlobalVars( $pager, 'pager_' ); $this->_tmpl->addRows( 'comment_entry', $comments ); return true; } /** * add new blog entry * * @access private * @return boolean true on success */ function _processAdd( $path ) { $path = array( 'new' ); return $this->_processEdit( $path ); } /** * edit blog entry * * @access private * @return boolean true on success * @todo check permssions */ function _processEdit( $path ) { if( !isset( $this->_configBlog[$this->_blog] ) ) { return $this->_processBlogList(); } if( !isset( $path[0] ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } $id = $path[0]; $conf = $this->_configBlog[$this->_blog]; // load and verify data $data = array(); if( $id != 'new' ) { // check load $data = $this->_ds->getEntry( 'blog', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } // check role if( !in_array( $data['role'], array( 'owner', 'publisher' ) ) ) { $this->_addMsg( _( 'You don\'t have the permssions to edit entry "{TITLE}".' ), $data ); return $this->_processList(); } } // get datasource for category $cats = array(); foreach( $this->_configBlog as $catId => $cat ) { $cats[] = array( 'value' => $catId, 'label' => $cat['title'] . ' / ' . substr( $cat['brief'], 0, 20 ) . '...' ); } // build form definition $formDef = array( 'title' => array( 'type' => 'String', 'attributes' => array( 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => _('Title'), 'title' => _('Title'), 'description' => _('The title will be displayed in lists and headlines, Therefore it should be short and descriptive.'), 'maxlength' => '50', 'minlength' => '3', ) ), $this->_ds->getPrimaryKey( 'blogcategory' ) => array( 'type' => 'Enum', 'attributes' => array( 'default' => $this->_blog, 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => _('Category'), 'title' => _('Category'), 'description' => _('Sort this article into a cateogry'), 'values' => $cats ) ), 'body' => array( 'type' => 'WBCmXml', 'attributes' => array( 'required' => 'yes', 'display' => 'yes', 'edit' => 'yes', 'label' => _('Text'), 'title' => _('Text'), 'description' => _('The article itself. '), 'maxlength' => 50000, 'minlength' => 10, 'config' => 'blog.js', 'rows' => 20 ) ) ); $formParams = array( 'elements' => $formDef ); $form = wbFactory::create( 'patForms', $formParams ); // process data if( $this->_request['action'] == 'save' ) { $form->setSubmitted( true ); if( $form->validateForm() ) { $data = $form->getValues(); // save data if( !$this->_checkReload() ) { if( $id == 'new' ) { $data['created'] = gmdate( 'Y-m-d H:i:s' ); } $this->_ds->save( 'blog', $id, $data ); } // transform "title field" if( isset( $conf['titlefield'] ) && !empty( $conf['titlefield'] ) ) { $data['title'] = $data[$conf['titlefield']]; } // go to edit page! if( $id == '_new' ) { $this->_addMsg( _( 'Saved new entries "{TITLE}"' ), $data ); } else { $this->_addMsg( _( 'Saved entry "{TITLE}"' ), $data ); } return $this->_processList(); } else { $this->_displayFormErrors( $form ); } } else { $form->setValues( $data ); } $this->_loadTemplates( 'submit', false ); $this->_loadTemplates( 'edit' ); $this->_tmpl->addGlobalVar( 'id', $id ); $renderer = patForms::createRenderer( 'Array' ); $form->setRenderer( $renderer ); $elements = $form->renderForm(); $this->_tmpl->addRows( 'blog_entry', $elements ); return true; } /** * remove blog entry * * @access private * @return boolean true on success * @todo check permissions */ function _processDelete( $path ) { $conf = $this->_configBlog[$this->_blog]; if( !isset( $path[0] ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } $id = $path[0]; $data = $this->_ds->getEntry( 'blog', $id ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } // check role if( !in_array( $data['role'], array( 'owner', 'publisher' ) ) ) { $this->_addMsg( _( 'You don\'t have the permssions to delete entry "{TITLE}".' ), $data ); return $this->_processList(); } if( $this->_request['action'] == 'force' ) { if( !$this->_checkReload() ) { $res = $this->_ds->delete( 'blog', $id ); if( patErrorManager::isError( $res ) ) { return $res; } } $this->_addMsg( _( 'Removed entry "{TITLE}".' ), $data ); return $this->_processList(); } $this->_loadTemplates( 'delete' ); $this->_tmpl->addGlobalVars( $data, 'entry_' ); return true; } /** * Manipulate comment * * @access protected * @return boolean true on success */ function _processComment( $path ) { $conf = $this->_configBlog[$this->_blog]; // get proper blog entry if( !isset( $path[0] ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } $blog = $path[0]; $data = $this->_ds->getEntry( 'blog', $blog ); if( patErrorManager::isError( $data ) ) { return $data; } if( !is_array( $data ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processList(); } if( !isset( $path[1] ) ) { $path[1] = '__undefined'; } $this->_tmpl->addGlobalVar( 'blog_id', $path[0] ); switch( strtolower( $path[1] ) ) { case 'add'; $params = array( 'template' => 'comment_edit', 'template_dir' => $this->_appName, 'base_dir' => wbFactory::getParam( 'systemDir' ) . '/admin/templates' ); $form =& wbFactory::singleton( 'patForms', $params ); if( isset( $this->_request['action'] ) && $this->_request['action'] == 'save' ) { $form->setSubmitted( true ); if( $form->validateForm() ) { if( !$this->_checkReload() ) { $eCode = array( 'blogger', 'comment', 'autoapproved', $this->_blog ); $data = $form->getValues(); $data['blogid'] = $blog; $data['flags'] = 'approved'; $data['created'] = gmdate( 'Y-m-d H:i:s' ); $data['changed'] = $data['created']; $data[$this->_ds->getPrimaryKey( 'user' )] = $this->_auth->getUserId(); // save data $this->_ds->save( 'blogcomment', 'new', $data ); $this->_tmpl->addGlobalVar( 'comment_added', 'yes' ); $eLink = array( 'app' => 'Blogger/' . $this->_blog. '/View/' . $blog, ); wbEvent::trigger( implode( ':', $eCode ), _( 'New comment for "{BLOGID}" saved' ), $data, $eLink ); } $this->_addMsg( _( 'Comment added' ) ); return $this->_processView( $path ); } } $this->_loadTemplates( 'submit', false ); $form->renderForm( array( 'template' => 'form', 'errorTemplateContainer' => 'formErrors', 'errorTemplate' => 'formErrors_entry' ) ); return true; break; case 'approve'; if( !isset( $path[2] ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processView( $path ); } $id = $path[2]; $data = array( 'flags' => 'approved' ); $this->_ds->save( 'blogcomment', $id, $data ); return $this->_processView( $path ); break; case 'delete'; if( !isset( $path[2] ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processView( $path ); } // receive comment $id = $path[2]; $comment = $this->_ds->getEntry( 'blogcomment', $id ); if( patErrorManager::isError( $comment ) ) { return $comment; } if( !is_array( $comment ) ) { $this->_addMsg( _( 'Invalid entry - please select entry from list.' ) ); return $this->_processView( $path ); } if( $this->_request['action'] == 'force' ) { if( !$this->_checkReload() ) { $res = $this->_ds->delete( 'blogcomment', $id ); if( patErrorManager::isError( $res ) ) { return $res; } } $this->_addMsg( _( 'Removed comment' ), $data ); return $this->_processView( $path ); } $this->_loadTemplates( 'comment_delete' ); $this->_tmpl->addGlobalVars( $comment, 'entry_' ); break; default: return $this->_processView( $path ); break; } } /** * Mangle blog category * * @access protected * @param array $path * @return boolean true on success */ function _processConfigure( $path ) { $id = $this->_blog; $params = array( 'template' => 'category_edit', 'template_dir' => $this->_appName, 'base_dir' => wbFactory::getParam( 'systemDir' ) . '/admin/templates' ); $form =& wbFactory::singleton( 'patForms', $params ); $this->_tmpl->addGlobalVar( 'id', $id ); if( !isset( $this->_request['action'] ) || $this->_request['action'] != 'save' ) { $this->_loadTemplates( 'submit', false ); if( $id != 'new' ) { $form->setValues( $this->_configBlog[$id] ); } $form->renderForm( array( 'template' => 'form', 'errorTemplateContainer' => 'formErrors', 'errorTemplate' => 'formErrors_entry' ) ); return true; } $form->setSubmitted( true ); if( $form->validateForm() ) { $data = $form->getValues(); if( !$this->_checkReload( $data ) ) { $this->_ds->save( 'blogcategory', $id, $data ); } $this->_addMsg( _( 'Categoy added' ) ); } $this->_loadTemplates( 'submit', false ); $form->renderForm( array( 'template' => 'form', 'errorTemplateContainer' => 'formErrors', 'errorTemplate' => 'formErrors_entry' ) ); return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentries( $table, &$data, $id ) { $data['id'] = $id; if( $table == 'blogcategory' ) { return true; } // use different titlefield $conf =& $this->_configBlog[$this->_blog]; if( isset( $conf['titlefield'] ) && !empty( $conf['titlefield'] ) ) { $data['title'] = $data[$conf['titlefield']]; } $data['role'] = 'other'; if( $this->_auth->getUserId() == $data['uid'] ) { $data['role'] = 'owner'; } else if( in_array( 'publish', $conf['others'] ) ) { $data['role'] = 'publisher'; } 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; // use different titlefield $conf =& $this->_configBlog[$this->_blog]; if( isset( $conf['titlefield'] ) && !empty( $conf['titlefield'] ) ) { $data['title'] = $data[$conf['titlefield']]; } $data['role'] = 'other'; if( $this->_auth->getUserId() == $data['uid'] ) { $data['role'] = 'owner'; } else if( in_array( 'publish', $conf ) ) { $data['role'] = 'publisher'; } return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForSave( $table, &$data, $id ) { // add timestamps $data['changed'] = date( 'Y-m-d H:i:s' ); if( $id == 'new' ) { $data['created'] = $data['changed']; // add owner's id as creator $auth =& wbFactory::singleton( 'wbAuth' ); $user = $auth->getUserData(); $data['uid'] = $user['uid']; } return true; } } ?>