1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-03 13:17:24 +02:00

admin UI: default mode/action auto-detection (and various other ways to control them), moving code to lower level (allow reusability) - work in progress; possible future __autoload-ing fix

This commit is contained in:
secretr
2009-11-14 14:52:26 +00:00
parent 50d5bda577
commit 1c660784d6
3 changed files with 482 additions and 209 deletions

View File

@@ -9,8 +9,8 @@
* General purpose file * General purpose file
* *
* $Source: /cvs_backup/e107_0.8/class2.php,v $ * $Source: /cvs_backup/e107_0.8/class2.php,v $
* $Revision: 1.156 $ * $Revision: 1.157 $
* $Date: 2009-11-12 16:43:44 $ * $Date: 2009-11-14 14:52:26 $
* $Author: secretr $ * $Author: secretr $
* *
*/ */
@@ -2124,6 +2124,7 @@ function plugInstalled($plugname)
* } * }
* </code> * </code>
* TODO - use spl_autoload[_*] for core autoloading some day (PHP5 > 5.1.2) * TODO - use spl_autoload[_*] for core autoloading some day (PHP5 > 5.1.2)
* TODO - at this time we could create e107 version of spl_autoload_register - e_event->register/trigger('autoload')
* *
* @param string $className * @param string $className
* @return void * @return void
@@ -2159,3 +2160,10 @@ function __autoload($className)
include($filename); include($filename);
} }
} }
// register __autoload if possible to prevent its override by
// 3rd party spl_autoload_register calls
if(function_exists('spl_autoload_register'))
{
spl_autoload_register('__autoload');
}

View File

@@ -9,8 +9,8 @@
* Administration UI handlers, admin helper functions * Administration UI handlers, admin helper functions
* *
* $Source: /cvs_backup/e107_0.8/e107_handlers/admin_handler.php,v $ * $Source: /cvs_backup/e107_0.8/e107_handlers/admin_handler.php,v $
* $Revision: 1.25 $ * $Revision: 1.26 $
* $Date: 2009-11-12 16:55:50 $ * $Date: 2009-11-14 14:52:26 $
* $Author: secretr $ * $Author: secretr $
*/ */
@@ -91,7 +91,13 @@ class e_admin_request
* Current Mode * Current Mode
* @var string * @var string
*/ */
protected $_mode = 'main'; protected $_mode = '';
/**
* Default Mode
* @var string
*/
protected $_default_mode = 'main';
/** /**
* Key name for mode search * Key name for mode search
@@ -103,7 +109,13 @@ class e_admin_request
* Current action * Current action
* @var string * @var string
*/ */
protected $_action = 'index'; protected $_action = '';
/**
* Default Action
* @var string
*/
protected $_default_action = 'index';
/** /**
* Key name for action search * Key name for action search
@@ -278,9 +290,19 @@ class e_admin_request
*/ */
public function getMode() public function getMode()
{ {
if(!$this->_mode) return $this->getDefaultMode();
return $this->_mode; return $this->_mode;
} }
/**
* Get default mode
* @return string
*/
public function getDefaultMode()
{
return $this->_default_mode;
}
/** /**
* Get current mode name * Get current mode name
* *
@@ -288,7 +310,7 @@ class e_admin_request
*/ */
public function getModeName() public function getModeName()
{ {
return strtolower(str_replace('-', '_', $this->_mode)); return strtolower(str_replace('-', '_', $this->getMode()));
} }
/** /**
@@ -303,6 +325,17 @@ class e_admin_request
return $this; return $this;
} }
/**
* Set default mode
* @param string $mode
* @return e_admin_request
*/
public function setDefaultMode($mode)
{
if($mode) $this->_default_mode = $mode;
return $this;
}
/** /**
* Set mode key name * Set mode key name
* @param string $key * @param string $key
@@ -320,16 +353,26 @@ class e_admin_request
*/ */
public function getAction() public function getAction()
{ {
if(!$this->_action) return $this->getDefaultAction();
return $this->_action; return $this->_action;
} }
/**
* Get default action
* @return string
*/
public function getDefaultAction()
{
return $this->_default_action;
}
/** /**
* Get current action name * Get current action name
* @return string camelized action * @return string camelized action
*/ */
public function getActionName() public function getActionName()
{ {
return $this->camelize($this->_action); return $this->camelize($this->getAction());
} }
/** /**
@@ -345,6 +388,18 @@ class e_admin_request
return $this; return $this;
} }
/**
* Set default action
*
* @param string $action
* @return e_admin_request
*/
public function setDefaultAction($action)
{
if($action) $this->_default_action = $action;
return $this;
}
/** /**
* Set action key name * Set action key name
* @param string $key * @param string $key
@@ -927,12 +982,22 @@ class e_admin_dispatcher
/** /**
* Required (set by child class). * Required (set by child class).
* Controller map array in format * Controller map array in format
* 'MODE' => array('controller' =>'CONTROLLER_CLASS'[, 'path' => 'CONTROLLER SCRIPT PATH']); * 'MODE' => array('controller' =>'CONTROLLER_CLASS_NAME'[, 'path' => 'CONTROLLER SCRIPT PATH', 'ui' => extend of 'comments_admin_form_ui', 'uipath' => 'path/to/ui/']);
* *
* @var array * @var array
*/ */
protected $modes; protected $modes;
/**
* @var string
*/
protected $defaultMode = '';
/**
* @var string
*/
protected $defaultAction = '';
/** /**
* Optional - map 'mode/action' pair to 'modeAlias/actionAlias' * Optional - map 'mode/action' pair to 'modeAlias/actionAlias'
* @var string * @var string
@@ -954,6 +1019,11 @@ class e_admin_dispatcher
*/ */
protected $menuTitle = 'Menu'; protected $menuTitle = 'Menu';
/**
* @var string
*/
protected $pluginTitle = '';
/** /**
* Constructor * Constructor
* *
@@ -974,6 +1044,13 @@ class e_admin_dispatcher
$this->setRequest($request)->setResponse($response)->init(); $this->setRequest($request)->setResponse($response)->init();
if(!$this->defaultMode || !$this->defaultAction)
{
$this->setDefaults();
}
$request->setDefaultMode($this->defaultMode)->setDefaultAction($this->defaultAction);
// register itself // register itself
e107::setRegistry('admin/ui/dispatcher', $this); e107::setRegistry('admin/ui/dispatcher', $this);
@@ -992,6 +1069,31 @@ class e_admin_dispatcher
{ {
} }
/**
* Retrieve missing default action/mode
* @return e_admin_dispatcher
*/
public function setDefaults()
{
// try Admin menu first
if($this->adminMenu)
{
reset($this->adminMenu);
list($mode, $action) = explode('/', key($this->adminMenu), 2);
}
else
{
reset($this->modes);
$mode = key($this->modes);
$action = $this->modes[$mode]['index'];
}
if(!$this->defaultMode) $this->defaultMode = $mode;
if(!$this->defaultAction) $this->defaultAction = $action;
return $this;
}
/** /**
* Get request object * Get request object
* @return e_admin_request * @return e_admin_request
@@ -1892,29 +1994,20 @@ class e_admin_controller
//FIXME - move everything from e_admin_ui except model auto-create related code //FIXME - move everything from e_admin_ui except model auto-create related code
class e_admin_controller_ui extends e_admin_controller class e_admin_controller_ui extends e_admin_controller
{ {
/**
} * @var array UI field data
*/
class e_admin_ui extends e_admin_controller_ui
{
protected $fields = array(); protected $fields = array();
/**
* @var array default fields activated on List view
*/
protected $fieldpref = array(); protected $fieldpref = array();
protected $fieldTypes = array();
protected $dataFields = array(); /**
protected $validationRules = array(); * @var array Plugin Preference description array
*/
protected $prefs = array(); protected $prefs = array();
protected $pluginName;
protected $listQry;
protected $tableJoin;
protected $editQry;
protected $table;
protected $tableAlias;
protected $pid;
protected $pluginTitle;
protected $perPage = 20;
protected $batchDelete = true;
/** /**
* @var e_admin_model * @var e_admin_model
@@ -1936,6 +2029,238 @@ class e_admin_ui extends e_admin_controller_ui
*/ */
protected $_pref = null; protected $_pref = null;
/**
* Get all field data
* @return array
*/
public function getFields()
{
return $this->fields;
}
/**
*
* @param string $field
* @param string $key attribute name
* @param mixed $default default value if not set, default is null
* @return mixed
*/
public function getFieldAttr($field, $key, $default = null)
{
if(isset($this->fields[$field][$key]))
{
return $this->fields[$field][$key];
}
return $default;
}
/**
* Get fields stored as user preferences
* @return array
*/
public function getFieldPref()
{
return $this->fieldpref;
}
/**
* Get Config data array
* @return array
*/
public function getPrefs()
{
return $this->prefs;
}
/**
* Get column preference array
* @return array
*/
public function getUserPref()
{
global $user_pref;
return vartrue($user_pref['admin_cols_'.$this->getTableName()], array());
}
/**
* Set column preference array
* @return boolean success
*/
public function setUserPref($new)
{
global $user_pref;
$user_pref['admin_cols_'.$this->getTableName()] = $new;
$this->fieldpref = $new;
return save_prefs('user');
}
/**
* Get current model
*
* @return e_admin_model
*/
public function getModel()
{
if(null === $this->_model)
{
$this->_setModel();
}
return $this->_model;
}
/**
* Set controller model
* @param e_admin_model $model
* @return e_admin_controller_ui
*/
public function setModel($model)
{
$this->_model = $model;
return $this;
}
public function getValidationRules()
{
return $this->getModel()->getValidationRules();
}
public function getDataFields()
{
return $this->getModel()->getDataFields();
}
/**
* User defined model setter
* @return e_admin_controller_ui
*/
protected function _setModel()
{
return $this;
}
/**
* Get current tree model
* @return e_admin_tree
*/
public function getTreeModel()
{
if(null === $this->_tree_model)
{
$this->_setTreeModel();
}
return $this->_tree_model;
}
/**
* Set controller tree model
* @param e_admin_tree $tree_model
* @return e_admin_controller_ui
*/
public function setTreeModel($tree_model)
{
$this->_tree_model = $tree_model;
return $this;
}
/**
* User defined tree model setter
* @return e_admin_controller_ui
*/
protected function _setTreeModel()
{
return $this;
}
/**
* Get extended (UI) Form instance
*
* @return e_admin_form_ui
*/
public function getUI()
{
if(null === $this->_ui)
{
$this->_setUI();
}
return $this->_ui;
}
/**
* Set controller UI form
* @param e_admin_form_ui $ui
* @return e_admin_controller_ui
*/
public function setUI($ui)
{
$this->_ui = $ui;
return $this;
}
/**
* User defined UI form setter
* @return e_admin_controller_ui
*/
protected function _setUI()
{
return $this;
}
/**
* Get Config object
* @return e_plugin_pref or e_core_pref when used in core areas
*/
public function getConfig()
{
if(null === $this->_pref)
{
$this->_setConfig();
}
return $this->_pref;
}
/**
* Set Config object
* @return e_admin_controller_ui
*/
public function setConfig($config)
{
$this->_prefs = $config;
return $this;
}
/**
* User defined config setter
* @return e_admin_controller_ui
*/
protected function _setConfig()
{
return $this;
}
}
class e_admin_ui extends e_admin_controller_ui
{
protected $fieldTypes = array();
protected $dataFields = array();
protected $validationRules = array();
protected $pluginName;
protected $listQry;
protected $tableJoin;
protected $editQry;
protected $table;
protected $tableAlias;
protected $pid;
protected $pluginTitle;
protected $perPage = 20;
protected $batchDelete = true;
/** /**
* Constructor * Constructor
* @param e_admin_request $request * @param e_admin_request $request
@@ -1944,7 +2269,7 @@ class e_admin_ui extends e_admin_controller_ui
*/ */
public function __construct($request, $response, $params = array()) public function __construct($request, $response, $params = array())
{ {
$this->setDefaultAction('list'); $this->setDefaultAction($request->getDefaultAction());
$params['enable_triggers'] = true; // override $params['enable_triggers'] = true; // override
parent::__construct($request, $response, $params); parent::__construct($request, $response, $params);
@@ -2631,8 +2956,7 @@ class e_admin_ui extends e_admin_controller_ui
{ {
if(!varset($this->pid) && vartrue($this->fields)) if(!varset($this->pid) && vartrue($this->fields))
{ {
$mes = e107::getMessage(); e107::getMessage()->add("There is no <strong>pid</strong> set.", E_MESSAGE_WARNING);
$mes->add("There is no <b>pid</b> set.", E_MESSAGE_WARNING);
} }
return $this->pid; return $this->pid;
@@ -2657,7 +2981,7 @@ class e_admin_ui extends e_admin_controller_ui
public function getJoinTable($alias = false, $prefix = false) public function getJoinTable($alias = false, $prefix = false)
{ {
if($alias && $this->tableAlias) return $this->tableAlias; if($alias && $this->tableAlias) return $this->tableAlias;
return ($prefix ? '#.' : '').$this->table; return ($prefix ? '#' : '').$this->table;
} }
public function getBatchDelete() public function getBatchDelete()
@@ -2665,41 +2989,29 @@ class e_admin_ui extends e_admin_controller_ui
return $this->batchDelete; return $this->batchDelete;
} }
public function getFields() /**
* Validation rules retrieved from controller object
* @return array
*/
public function getValidationRules()
{ {
return $this->fields; return $this->validationRules;
}
public function getFieldAttr($key)
{
if(isset($this->fields[$key]))
{
return $this->fields[$key];
}
return array();
}
public function getFieldPref()
{
return $this->fieldpref;
}
public function getFieldPrefAttr($key)
{
if(isset($this->fieldpref[$key]))
{
return $this->fieldpref[$key];
}
return array();
} }
/** /**
* Get Config object * Data Field array retrieved from controller object
* @return e_plugin_pref|e_core_pref * @return array
*/ */
public function getConfig() public function getDataFields()
{ {
if(null === $this->_pref) return $this->dataFields;
}
/**
* Set Config object
* @return e_admin_ui
*/
protected function _setConfig()
{ {
$this->_pref = $this->pluginName == 'core' ? e107::getConfig() : e107::getPlugConfig($this->pluginName); $this->_pref = $this->pluginName == 'core' ? e107::getConfig() : e107::getPlugConfig($this->pluginName);
@@ -2707,69 +3019,30 @@ class e_admin_ui extends e_admin_controller_ui
foreach ($this->prefs as $key => $att) foreach ($this->prefs as $key => $att)
{ {
// create dataFields array // create dataFields array
if(vartrue($att['data'])) $dataFields[$key] = vartrue($att['data'], 'string');
{
$dataFields[$key] = $att['data'];
}
// create validation array // create validation array
if(vartrue($att['validate'])) if(vartrue($att['validate']))
{ {
$validateRules[$key] = array((true === $att['validate'] ? 'required' : $att['validate']), varset($att['rule']), $att['title'], varset($att['error'], $att['help'])); $validateRules[$key] = array((true === $att['validate'] ? 'required' : $att['validate']), varset($att['rule']), $att['title'], varset($att['error'], $att['help']));
} }
$this->_pref->setDataFields($dataFields)->setValidationRules($validateRules);
/* Not implemented in e_model yet /* Not implemented in e_model yet
elseif(vartrue($att['check'])) elseif(vartrue($att['check']))
{ {
$validateRules[$key] = array($att['check'], varset($att['rule']), $att['title'], varset($att['error'], $att['help'])); $validateRules[$key] = array($att['check'], varset($att['rule']), $att['title'], varset($att['error'], $att['help']));
}*/ }*/
} }
$this->_pref->setDataFields($dataFields)->setValidationRules($validateRules);
} return $this;
return $this->_pref;
} }
/** /**
* Get Config object * Set current model
* @return e_plugin_pref|e_core_pref
*/
public function getPrefs()
{
return $this->prefs;
}
/**
* Get column preference array
* @return array
*/
public function getUserPref()
{
global $user_pref;
return vartrue($user_pref['admin_cols_'.$this->getTableName()], array());
}
/**
* Get column preference array
* @return array
*/
public function setUserPref($new)
{
global $user_pref;
$user_pref['admin_cols_'.$this->getTableName()] = $new;
$this->fieldpref = $new;
save_prefs('user');
}
/**
* Get current model
* *
* @return e_admin_model * @return e_admin_ui
*/ */
public function getModel() public function _setModel()
{
if(null === $this->_model)
{ {
// try to create dataFields array if missing // try to create dataFields array if missing
if(!$this->dataFields) if(!$this->dataFields)
@@ -2812,43 +3085,31 @@ class e_admin_ui extends e_admin_controller_ui
->setFieldTypes($this->fieldTypes) ->setFieldTypes($this->fieldTypes)
->setDataFields($this->dataFields) ->setDataFields($this->dataFields)
->setParam('db_query', $this->editQry); ->setParam('db_query', $this->editQry);
return $this;
} }
return $this->_model; /**
} * Set current tree
* @return e_admin_ui
public function setModel($model) */
{ public function _setTreeModel()
$this->_model = $model;
}
public function getTreeModel()
{
if(null === $this->_tree_model)
{ {
// default tree model // default tree model
$this->_tree_model = new e_admin_tree_model(); $this->_tree_model = new e_admin_tree_model();
$this->_tree_model->setModelTable($this->table) $this->_tree_model->setModelTable($this->table)
->setFieldIdName($this->pid) ->setFieldIdName($this->pid)
->setParams(array('model_class' => 'e_admin_model', 'db_query' => $this->listQry)); ->setParams(array('model_class' => 'e_admin_model', 'db_query' => $this->listQry));
}
return $this->_tree_model; return $this;
}
public function setTreeModel($tree_model)
{
$this->_tree_model = $tree_model;
} }
/** /**
* Get extended (UI) Form instance * Get extended (UI) Form instance
* *
* @return e_admin_form_ui * @return e_admin_ui
*/ */
public function getUI() public function _setUI()
{
if(null === $this->_ui)
{ {
if($this->getParam('ui')) if($this->getParam('ui'))
{ {
@@ -2859,14 +3120,9 @@ class e_admin_ui extends e_admin_controller_ui
{ {
$this->_ui = new e_admin_form_ui($this); $this->_ui = new e_admin_form_ui($this);
} }
} return $this;
return $this->_ui;
} }
public function setUI($ui)
{
$this->_ui = $ui;
}
} }
class e_admin_form_ui extends e_form class e_admin_form_ui extends e_form

View File

@@ -9,8 +9,8 @@
* Release Plugin Administration UI * Release Plugin Administration UI
* *
* $Source: /cvs_backup/e107_0.8/e107_plugins/release/includes/admin.php,v $ * $Source: /cvs_backup/e107_0.8/e107_plugins/release/includes/admin.php,v $
* $Revision: 1.9 $ * $Revision: 1.10 $
* $Date: 2009-11-11 20:57:33 $ * $Date: 2009-11-14 14:52:26 $
* $Author: secretr $ * $Author: secretr $
*/ */
@@ -18,13 +18,22 @@
class plugin_release_admin extends e_admin_dispatcher class plugin_release_admin extends e_admin_dispatcher
{ {
/** /**
* Format: 'MODE' => array('controller' =>'CONTROLLER_CLASS'[, 'path' => 'CONTROLLER SCRIPT PATH', 'ui' => 'UI CLASS NAME child of e_admin_ui', 'uipath' => 'UI SCRIPT PATH']); * Format: 'MODE' => array('controller' =>'CONTROLLER_CLASS'[, 'index' => 'list', 'path' => 'CONTROLLER SCRIPT PATH', 'ui' => 'UI CLASS NAME child of e_admin_ui', 'uipath' => 'UI SCRIPT PATH']);
* Note - default mode/action is autodetected in this order:
* - $defaultMode/$defaultAction (owned by dispatcher - see below)
* - $adminMenu (first key if admin menu array is not empty)
* - $modes (first key == mode, corresponding 'index' key == action)
* @var array * @var array
*/ */
protected $modes = array( protected $modes = array(
'main' => array('controller' => 'plugin_release_admin_ui', 'path' => null, 'ui' => 'plugin_release_admin_form_ui', 'uipath' => null) 'main' => array('controller' => 'plugin_release_admin_ui', 'path' => null, 'ui' => 'plugin_release_admin_form_ui', 'uipath' => null)
); );
/* Both are optional
protected $defaultMode = null;
protected $defaultAction = null;
*/
/** /**
* Format: 'MODE/ACTION' => array('caption' => 'Menu link title'[, 'url' => '{e_PLUGIN}release/admin_config.php', 'perm' => '0']); * Format: 'MODE/ACTION' => array('caption' => 'Menu link title'[, 'url' => '{e_PLUGIN}release/admin_config.php', 'perm' => '0']);
* Additionally, any valid e_admin_menu() key-value pair could be added to the above array * Additionally, any valid e_admin_menu() key-value pair could be added to the above array