* @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 $_blog */ var $_blog = null; /** * configuration of all blogs * @var string $_configBlog */ var $_configBlog = null; /** * initilaize clas * * @access public */ function __construct() { $this->_auth = wbFactory::singleton( 'wbAuth' ); $this->_tmpl = wbFactory::singleton( 'patTemplate' ); $conf = wbFactory::singleton( 'patConfiguration' ); $conf->loadConfig( 'blogger.xml' ); $this->_configBlog = $conf->getConfigValue(); if( empty( $this->_configBlog ) ) { wbDebugger::addMsg( 'Blogger', 'Empty configuration, check "blogger.xml"', 'Config' ); } foreach( $this->_configBlog as $k => $v ) { if( isset( $v['others'] ) ) { $this->_configBlog[$k]['others'] = array_map( 'trim', explode( ',', $v['others'] ) ); } else { $this->_configBlog[$k]['others'] = array(); } } } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbAdminApp_Blogger() { $this->__construct(); } /** * run admin interface * * @access public * @return boolean true on success */ function process( $path ) { // find blog if( !empty( $path ) ) { $this->_blog =array_shift( $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(); } /** * recieve submenu items * * Submenu contains all blogs * * @access public * @return mixed $submenu */ function getMenu() { $menu = array(); foreach( $this->_configBlog as $app => $blog ) { $m = array( 'name' => $app, 'app' => $this->_appName . '/' . $app, 'title' => $blog['title'], 'brief' => $blog['brief'], ); array_push( $menu, $m ); } return $menu; } /** * display blog list * * @access private * @return boolean true on success */ function _processBlogList( ) { $list = array(); foreach( $this->_configBlog as $name => $blog ) { $b = array( 'name' => $name, 'title' => $blog['title'], 'brief' => $blog['brief'], ); array_push( $list, $b ); } $this->_loadTemplates( 'index' ); $this->_tmpl->addRows( 'blog_entry', $list ); 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' ); $ds =& wbFactory::singleton( 'wbDatasource' ); $ds->setOptions( array( $this->_blog => array( 'limit' => 20 ) ) ); $ds->setCallback( $this ); $pager = $ds->getPager( $conf['table'], $this->_request['goto'] ); if( patErrorManager::isError( $pager ) ) { return $pager; } $pager['app'] = 'Blogger/' . $this->_blog . '/List'; $this->_tmpl->addGlobalVars( $pager, 'pager_' ); $offset = $pager['offset'] * $pager['limit']; $owner = null; if( !in_array( 'list', $this->_configBlog[$this->_blog]['others'] ) ) { $owner = $this->_auth->getUserId(); } $list = $ds->getEntries( $conf['table'], $owner, array(), $offset ); if( patErrorManager::isError( $list ) ) { return $list; } $this->_tmpl->addRows( 'blog_entry', $list ); return true; } /** * add new blog entry * * @access private * @todo add user data! * @return boolean true on success */ function _processView() { $conf = $this->_configBlog[$this->_blog]; $ds = wbFactory::singleton( 'wbDatasource' ); $ds->setCallback( $this ); $id = $this->_request['id']; $data = $ds->getEntry( $conf['table'], $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_' ); // add custom details $details = array(); foreach( $conf['fields'] as $value ) { $detail = $value['attributes']; $detail['value'] = $data[$value['name']]; array_push( $details, $detail ); } $this->_tmpl->addRows( 'entry_detail', $details ); return true; } /** * add new blog entry * * @access private * @return boolean true on success */ function _processAdd() { $this->_request['id'] = 'new'; return $this->_processEdit(); } /** * edit blog entry * * @access private * @return boolean true on success */ function _processEdit() { if( !isset( $this->_configBlog[$this->_blog] ) ) { return $this->_processBlogList(); } if( !$this->_request['id'] ) { return $this->_processList(); } $conf = $this->_configBlog[$this->_blog]; $ds = wbFactory::singleton( 'wbDatasource' ); $ds->setCallback( $this ); $id = $this->_request['id']; // load and verify data $data = array(); if( $id != 'new' ) { // check load $data = $ds->getEntry( $conf['table'], $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(); } } // basic fields: title $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', ) ) ); // overwrite titlefield? if( isset( $conf['titlefield'] ) && !empty( $conf['titlefield'] ) ) { unset( $formDef['title'] ); } // add other fields foreach( $conf['fields'] as $field ) { $formDef[$field['name']] = array( 'type' => $field['type'], 'attributes' => $field['attributes'] ); // use datasource if( isset( $field['foreign'] ) && !empty( $field['foreign'] ) ) { list( $fTable, $fField ) = explode( ':', $field['foreign'] ); $params = array( 'datasource'=> 'Table', 'table' => $fTable, 'format' => '%s', 'fields' => $fField ); // allow empty value if( isset( $field['attributes']['required'] ) && $field['attributes']['required'] == 'no' ) { $params['empty'] = 'yes'; } $formDs =& wbFactory::create( 'wbFormDatasource', $params ); $formDs->setDatasource( $ds ); $formDef[$field['name']]['attributes']['values'] = $formDs->getValues();; } } $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' ); } $ds->save( $conf['table'], $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 */ function _processDelete() { $conf = $this->_configBlog[$this->_blog]; $ds = wbFactory::singleton( 'wbDatasource' ); $ds->setCallback( $this ); $id = $this->_request['id']; $data = $ds->getEntry( $conf['table'], $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 = $ds->delete( $conf['table'], $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; } /** * get form errors from form and translate them to template * * @access private * @param object $form patForm object * @return boolean true */ function _displayFormErrors( &$form ) { $errors = $form->getValidationErrors(); if( empty( $errors ) ) { return true; } $errorRows = array(); foreach( $errors as $elName => $elErrors ) { $row = array(); $el =& $form->getElement( $elName ); $atts = $el->getAttributes(); foreach( $atts as $key => $att ) { if( is_array( $att ) ) { continue; } $row['field_'.$key] = $att; } foreach( $elErrors as $err ) { foreach( $err as $key => $value ) { $row['error_' . $key] = $value; } } array_push( $errorRows, $row ); } $this->_tmpl->addRows( 'formErrors_entry', $errorRows ); $this->_tmpl->setAttribute( 'formErrors' , 'visibility', 'visible' ); 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; // 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 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 $auth =& wbFactory::singleton( 'wbAuth' ); $user = $auth->getUserData(); $data['uid'] = $user['uid']; } return true; } } ?>