1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 13:47:31 +02:00

EONE-62 (New Feature): user e_model solution draft (work in progress);

e_model/tree improvements (automated system cache support, various useful new methods, minor fixes);
mysql handler details
This commit is contained in:
secretr
2010-04-28 15:44:46 +00:00
parent 65595056f3
commit 58d06df6fd
3 changed files with 480 additions and 13 deletions

View File

@@ -84,6 +84,21 @@ class e_model
*/
protected $_message_stack = 'default';
/**
* Cache string to be used from _get/set/clearCacheData() methods
*
* @var string
*/
protected $_cache_string = null;
/**
* Force Cache even if system cahche is disabled
* Default is false
*
* @var boolean
*/
protected $_cache_force = false;
/**
* Model parameters passed mostly from external sources
*
@@ -870,9 +885,21 @@ class e_model
if($force)
{
$this->setData(array());
$this->setData(array())
->_clearCacheData();
}
$id = intval($id);
if(!$id)
{
return $this;
}
$cached = $this->_getCacheData();
if($cached !== false)
{
$this->setData($cached);
return $this;
}
$sql = e107::getDb();
$qry = str_replace('{ID}', $id, $this->getParam('db_query'));
@@ -901,10 +928,64 @@ class e_model
{
$this->addMessageDebug('SQL error #'.$sql->getLastErrorNumber().': '.$sql->getLastErrorText());
}
else
{
$this->_setCacheData();
}
return $this;
}
protected function _getCacheData()
{
if(!$this->isCacheEnabled())
{
return false;
}
$cached = e107::getCache()->retrieve_sys($this->getCacheString(true), false, $this->_cache_force);
if(false !== $cached)
{
return e107::getArrayStorage()->ReadArray($cached);
}
return false;
}
protected function _setCacheData()
{
if(!$this->isCacheEnabled())
{
return $this;
}
e107::getCache()->set_sys($this->getCacheString(true), $this->toString(false), $this->_cache_force, false);
return $this;
}
protected function _clearCacheData()
{
if(!$this->isCacheEnabled(false))
{
return $this;
}
e107::getCache()->clear_sys($this->getCacheString(true), false);
}
public function isCacheEnabled($checkId = true)
{
return (null !== $this->getCacheString() && (!$checkId || $this->getId()));
}
public function getCacheString($replace = false)
{
return ($replace ? str_replace('{ID}', $this->getId(), $this->_cache_string) : $this->_cache_string);
}
public function setCacheString($str)
{
$this->_cache_string = $str;
}
/**
* Save data to DB
* Awaiting for child class implementation
@@ -1019,11 +1100,12 @@ class e_model
*
* @param string $template
* @param boolean $parsesc parse external shortcodes, default is true
* @param e_vars $eVars simple parser data
* @return string parsed template
*/
public function toHTML($template, $parsesc = true)
public function toHTML($template, $parsesc = true, $eVars = null)
{
return e107::getParser()->parseTemplate($template, $parsesc, $this);
return e107::getParser()->parseTemplate($template, $parsesc, $this, $eVars);
}
public function toXML()
@@ -1091,7 +1173,7 @@ class e_model
}
return (string) $value;
}
return (string) e107::getArrayStorage()->WriteArray($this->getData(), $AddSlashes);
return (string) e107::getArrayStorage()->WriteArray($this->toArray(), $AddSlashes);
}
/**
@@ -2119,6 +2201,37 @@ class e_tree_model extends e_model
return $this;
}
/**
* Unset all current data
* @return e_tree_model
*/
function unsetTree()
{
$this->remove('__tree');
return $this;
}
public function isCacheEnabled()
{
return (null !== $this->getCacheString());
}
public function getCacheString()
{
return $this->_cache_string;
}
protected function _loadFromArray($array)
{
$class_name = $this->getParam('model_class', 'e_model');
$tree = array();
foreach ($array as $id => $data)
{
$tree[$id] = new $class_name($data);
}
$this->setTree($tree, true);
}
/**
* Default load method
*
@@ -2134,18 +2247,33 @@ class e_tree_model extends e_model
if ($force)
{
$this->setTree(array(), true);
$this->unsetTree()
->_clearCacheData();
$this->_total = false;
}
if($this->getParam('db_query') && $this->getParam('model_class') && class_exists($this->getParam('model_class')))
$cached = $this->_getCacheData();
if($cached !== false)
{
$this->_loadFromArray($cached);
return $this;
}
$class_name = $this->getParam('model_class', 'e_model');
// auto-load all
if(!$this->getParam('db_query') && $this->getModelTable())
{
$this->getParam('db_query', 'SELECT'.(!$this->getParam('nocount') ? ' SQL_CALC_FOUND_ROWS' : '').' * FROM '.$this->getModelTable());
}
if($this->getParam('db_query') && $class_name && class_exists($this->getParam('model_class')))
{
$sql = e107::getDb();
$class_name = $this->getParam('model_class', 'e_model');
$this->_total = $sql->total_results = false;
if($sql->db_Select_gen($this->getParam('db_query')))
{
// TODO - $sql->total_results variable type!!!
$this->_total = is_integer($sql->total_results) ? $sql->total_results : false; //requires SQL_CALC_FOUND_ROWS in query - see db handler
while($tmp = $sql->db_Fetch())
{
@@ -2165,6 +2293,19 @@ class e_tree_model extends e_model
unset($tmp);
}
if($sql->getLastErrorNumber())
{
// TODO - admin log?
$this->addMessageError('Application Error - DB query failed.') // TODO LAN
->addMessageDebug('SQL Error #'.$sql->getLastErrorNumber().': '.$sql->getLastErrorText())
->addMessageDebug($sql->getLastQuery());
}
else
{
$this->_setCacheData();
}
}
return $this;
}
@@ -2249,6 +2390,65 @@ class e_tree_model extends e_model
{
return $this->has('__tree');
}
/**
* Render model data, all 'sc_*' methods will be recongnized
* as shortcodes.
*
* @param string $template
* @param boolean $parsesc parse external shortcodes, default is true
* @param e_vars $eVars simple parser data
* @return string parsed template
*/
public function toHTML($template, $parsesc = true, $eVars = null)
{
$ret = '';
$i == 1;
foreach ($this->getTree() as $model)
{
if($eVars) $eVars->treeCounter = $i;
$ret .= $model->toHTML($template, $parsesc, $eVars);
$i++;
}
return $ret;
}
public function toXML()
{
return '';
// UNDER CONSTRUCTION
}
/**
* Convert model object to array
* @return array object data
*/
public function toArray()
{
return $this->getData();
$ret = array();
foreach ($this->getTree() as $id => $model)
{
$ret[$id] = $model->toArray();
}
return $ret;
}
/**
* Convert object data to a string
*
* @param boolean $AddSlashes
* @param string $key optional, if set method will return corresponding value as a string
* @return string
*/
public function toString($AddSlashes = true, $node_id = null)
{
if (null !== $node_id && $this->isNode($node_id))
{
return $this->getNode($node_id)->toString($AddSlashes);
}
return (string) e107::getArrayStorage()->WriteArray($this->toArray(), $AddSlashes);
}
}
class e_admin_tree_model extends e_tree_model

View File

@@ -1486,6 +1486,12 @@ class e_db_mysql
return $this->mySQLlastErrText; // Text of last error (empty string if no error)
}
function resetLastError()
{
$this->mySQLlastErrNum = 0;
$this->mySQLlastErrText = '';
}
function getLastQuery()
{
return $this->mySQLlastQuery;

View File

@@ -42,6 +42,20 @@ class e_user_model extends e_model
*/
protected $_message_stack = 'user';
/**
* Extended data
*
* @var e_user_extended_model
*/
protected $_extended_model = null;
/**
* Extended structure
*
* @var e_user_extended_strcuture
*/
protected $_extended_strcuture = null;
/**
* Get User value
*
@@ -49,7 +63,7 @@ class e_user_model extends e_model
* @param string $default
* @return mixed
*/
public function getValue($field, $default)
public function getValue($field, $default = '')
{
$field = 'user_'.$field;
return $this->get($field, $default);
@@ -68,23 +82,87 @@ class e_user_model extends e_model
return $this;
}
/**
* Get User extended value
*
* @param string$field
* @param string $default
* @return mixed
*/
public function getExtendedValue($field, $default = '')
{
return $this->getExtendedModel()->getValue($field, $default);
}
/**
* Set User extended value
*
* @param string $field
* @param mixed $value
* @return e_user_model
*/
public function setExtendedValue($field, $value)
{
$this->getExtendedModel()->setValue($field, $value);
return $this;
}
/**
* Get user extended model
*
* @return e_user_extended_model
*/
public function getExtended()
{
if(null === $this->_extended_model)
{
$this->_extended_model = new e_user_extended_model();
$this->_extended_model->load($this->getId());
}
return $this->_extended_model;
}
/**
* Set user extended model
*
* @param e_user_extended_model $extended_model
* @return e_user_model
*/
public function setExtended($extended_model)
{
$this->_extended_model = $extended_model;
return $this;
}
/**
* Get extended structure tree
*
* @return e_user_extended_strcuture_tree
*/
public function getExtendedStructure()
{
return e107::getExtendedStructure();
}
/**
* Set current object as a target
*
* @return e_user_model
*/
public function setAsTarget()
{
e107::setRegistry('targets/core/user', $this);
e107::setRegistry('targets/core/user/'.$this->getId() , $this);
return $this;
}
/**
* Clear registered target
*
* @return e_user_model
*/
public function clearTarget()
{
e107::setRegistry('targets/core/user', null);
e107::setRegistry('targets/core/user'.$this->getId(), null);
return $this;
}
@@ -94,6 +172,21 @@ class e_user_model extends e_model
public function load($user_id = 0, $force = false)
{
parent::load($user_id, $force);
if($this->getId())
{
// no errors - register
$this->setAsTarget();
}
}
public function destroy()
{
$this->clearTarget()
->removeData();
if(null !== $this->_extended_model)
{
$this->_extended_model->destroy();
}
}
}
@@ -101,11 +194,179 @@ class e_user_model extends e_model
// TODO Current user model is under construction
class e_current_user extends e_user_model
{
final public function isCurrent()
{
return true;
}
}
// TODO - add some more useful methods, sc_* methods
// TODO - add some more useful methods, sc_* methods support
class e_user extends e_user_model
{
final public function isCurrent()
{
// FIXME - check against current system user
return ($this->getId() && $this->getId() === USERID);
}
}
class e_user_extended_model extends e_model
{
/**
* @see e_model
* @var string
*/
protected $_db_table = 'user_extended';
/**
* @see e_model
* @var string
*/
protected $_field_id = 'user_extended_id';
/**
* @see e_model
* @var string
*/
protected $_message_stack = 'user';
/**
* Get User extended field value
*
* @param string$field
* @param string $default
* @return mixed
*/
public function getValue($field, $default = '')
{
$field = 'user_'.$field;
return $this->get($field, $default);
}
/**
* Set User extended field value
*
* @param string $field
* @param mixed $value
* @return e_user_model
*/
public function setValue($field, $value)
{
$field = 'user_'.$field;
$this->set($field, $value, false);
return $this;
}
/**
* Get extended structure tree
*
* @return e_user_extended_strcuture_tree
*/
public function getExtendedStructure()
{
return e107::getExtendedStructure();
}
}
class e_user_extended_structure_model extends e_model
{
/**
* @see e_model
* @var string
*/
protected $_db_table = 'user_extended_struct';
/**
* @see e_model
* @var string
*/
protected $_field_id = 'user_extended_struct_id';
/**
* @see e_model
* @var string
*/
protected $_message_stack = 'user';
/**
* Get User extended structure field value
*
* @param string$field
* @param string $default
* @return mixed
*/
public function getValue($field, $default = '')
{
$field = 'user_extended_struct'.$field;
return $this->get($field, $default);
}
/**
* Set User extended structure field value
*
* @param string $field
* @param mixed $value
* @return e_user_model
*/
public function setValue($field, $value)
{
$field = 'user_extended_struct'.$field;
$this->set($field, $value, false);
return $this;
}
/**
* Loading of single structure row not allowed for front model
*/
public function load()
{
return $this;
}
}
class e_user_extended_strcuture_tree extends e_tree_model
{
/**
* @see e_model
* @var string
*/
protected $_db_table = 'user_extended_struct';
/**
* @see e_model
* @var string
*/
protected $_field_id = 'user_extended_struct_id';
/**
* @see e_model
* @var string
*/
protected $_message_stack = 'user';
/**
* @var string
*/
protected $_cache_string = 'user';
/**
* Force system cache
* @var boolen
*/
protected $_cache_force = true;
/**
* Load tree data
*
* @see e107_handlers/e_tree_model#load($force)
*/
public function load($force = false)
{
$this->setParam('nocount', true)
->setParam('model_class', 'e_user_extended_structure_model');
parent::load($force);
return $this;
}
}