2009-08-20 13:06:34 +00:00
< ? php
/*
* e107 website system
*
2010-12-29 13:39:15 +00:00
* Copyright ( C ) 2008 - 2010 e107 Inc ( e107 . org )
2009-08-20 13:06:34 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
* e107 Base Model
*
2010-12-29 13:39:15 +00:00
* $Id $
2010-02-10 18:18:01 +00:00
* $Author $
2009-08-20 13:06:34 +00:00
*/
if ( ! defined ( 'e107_INIT' )) { exit ; }
/**
2010-12-10 14:25:36 +00:00
* Base e107 Object class
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* @ package e107
* @ category e107_handlers
* @ version 1.0
* @ author SecretR
2010-12-10 14:25:36 +00:00
* @ copyright Copyright ( C ) 2010 , e107 Inc .
2009-08-20 13:06:34 +00:00
*/
2010-12-10 14:25:36 +00:00
class e_object
2009-08-20 13:06:34 +00:00
{
/**
* Object data
*
* @ var array
*/
protected $_data = array ();
2009-12-23 15:12:13 +00:00
2010-12-10 14:25:36 +00:00
/**
* Model parameters passed mostly from external sources
*
* @ var array
*/
protected $_params = array ();
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Name of object id field
* Required for { @ link getId ()()} method
*
* @ var string
*/
protected $_field_id ;
/**
* Constructor - set data on initialization
*
* @ param array $data
*/
function __construct ( $data = array ())
{
2010-12-11 15:37:39 +00:00
if ( is_array ( $data )) $this -> setData ( $data );
2010-12-10 14:25:36 +00:00
}
/**
* Set name of object ' s field id
*
* @ see getId ()
*
* @ param string $name
* @ return e_object
*/
public function setFieldIdName ( $name )
{
$this -> _field_id = $name ;
return $this ;
}
/**
* Retrieve name of object ' s field id
*
* @ see getId ()
*
* @ param string $name
* @ return string
*/
public function getFieldIdName ()
{
return $this -> _field_id ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Retrieve object primary id field value
*
* @ return mixed
*/
public function getId ()
{
2015-03-01 12:43:02 -08:00
2010-12-10 14:25:36 +00:00
if ( $this -> getFieldIdName ())
{
2015-03-01 12:43:02 -08:00
return $this -> get ( $this -> getFieldIdName (), 0 ); // default of NULL will break MySQL strict in most cases.
2010-12-10 14:25:36 +00:00
}
return $this -> get ( 'id' , 0 );
}
/**
* Set object primary id field value
*
* @ return e_object
*/
public function setId ( $id )
{
if ( $this -> getFieldIdName ())
{
2012-02-07 16:37:44 +00:00
return $this -> set ( $this -> getFieldIdName (), $id );
2010-12-10 14:25:36 +00:00
}
return $this ;
}
/**
* Retrieves data from the object ( $_data ) without
* key parsing ( performance wise , prefered when possible )
*
* @ param string $key
* @ param mixed $default
* @ return mixed
*/
public function get ( $key , $default = null )
{
return ( isset ( $this -> _data [ $key ]) ? $this -> _data [ $key ] : $default );
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Get object data
* @ return array
*/
public function getData ()
{
return $this -> _data ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Overwrite data in the object for a single field .
*
* @ param string $key
* @ param mixed $value
* @ return e_object
*/
public function set ( $key , $value )
{
$this -> _data [ $key ] = $value ;
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Set object data
* @ return e_object
*/
public function setData ( $data )
{
$this -> _data = $data ;
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Update object data
* @ return e_object
*/
public function addData ( $data )
{
foreach ( $data as $key => $val )
{
$this -> set ( $key , $val );
}
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Remove object data key
*
* @ param string $key
* @ return e_object
*/
2010-12-13 16:00:18 +00:00
public function remove ( $key )
2010-12-10 15:14:53 +00:00
{
unset ( $this -> _data [ $key ]);
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Reset object data key
*
* @ return e_object
*/
public function removeData ()
{
$this -> _data = array ();
return $this ;
}
2010-12-10 14:25:36 +00:00
/**
* Check if key is set
* @ param string $key
* @ return boolean
*/
public function is ( $key )
{
return ( isset ( $this -> _data [ $key ]));
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Check if key is set and not empty
* @ param string $key
* @ return boolean
*/
public function has ( $key )
{
return ( isset ( $this -> _data [ $key ]) && ! empty ( $this -> _data [ $key ]));
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Check if object has data
* @ return boolean
*/
public function hasData ()
{
return ! empty ( $this -> _data );
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Set parameter array
* @ param array $params
* @ return e_object
*/
public function setParams ( array $params )
{
$this -> _params = $params ;
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Update parameter array
* @ param array $params
* @ return e_object
*/
public function updateParams ( array $params )
{
2014-02-25 11:43:28 +02:00
foreach ( $params as $k => $v )
2010-12-10 14:25:36 +00:00
{
$this -> setParam ( $k , $v );
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
return $this ;
}
/**
* Get parameter array
*
* @ return array parameters
*/
public function getParams ()
{
return $this -> _params ;
}
/**
* Set parameter
*
* @ param string $key
* @ param mixed $value
* @ return e_object
*/
public function setParam ( $key , $value )
{
2010-12-29 13:39:15 +00:00
if ( null === $value )
2010-12-10 14:25:36 +00:00
{
unset ( $this -> _params [ $key ]);
}
else $this -> _params [ $key ] = $value ;
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
return $this ;
}
/**
* Get parameter
*
* @ param string $key
* @ param mixed $default
*/
public function getParam ( $key , $default = null )
{
return ( isset ( $this -> _params [ $key ]) ? $this -> _params [ $key ] : $default );
}
2014-02-25 11:43:28 +02:00
2010-12-10 15:14:53 +00:00
/**
* Convert object data to simple shortcodes ( e_vars object )
* @ return string
*/
public function toSc ()
{
return new e_vars ( $this -> _data );
2014-02-25 11:43:28 +02:00
}
2010-12-10 14:25:36 +00:00
/**
* Convert object data to array
* @ return string
*/
public function toJson ()
{
return json_encode ( $this -> _data );
}
/**
* Convert object to array
* @ return array object data
*/
public function toArray ()
{
return $this -> _data ;
}
2014-02-25 11:43:28 +02:00
2010-12-10 14:25:36 +00:00
/**
* Magic method - convert object data to an array
*
* @ return array
*/
public function __toArray ()
{
return $this -> toArray ();
}
/**
* Convert object data to a string
*
* @ param boolean $AddSlashes
* @ return string
*/
public function toString ( $AddSlashes = false )
{
return ( string ) e107 :: getArrayStorage () -> WriteArray ( $this -> toArray (), $AddSlashes );
}
/**
* Magic method - convert object data to a string
* NOTE : before PHP 5.2 . 0 the __toString method was only
* called when it was directly combined with echo () or print ()
*
* NOTE : PHP 5.3 + is throwing parse error if __toString has optional arguments .
*
* @ return string
*/
public function __toString ()
{
return $this -> toString ( false );
}
2010-12-10 15:14:53 +00:00
/**
* Magic setter
* Triggered on e . g . < code >< ? php $e_object -> myKey = 'someValue' ; </ code >
2014-02-25 11:43:28 +02:00
*
2010-12-10 15:14:53 +00:00
* @ param string $key
* @ param mixed $value
*/
public function __set ( $key , $value )
{
// Unset workaround - PHP < 5.1.0
if ( null === $value ) $this -> remove ( $key );
else $this -> set ( $key , $value );
}
/**
* Magic getter
* Triggered on e . g . < code >< ? php print ( $e_object -> myKey ); </ code >
* @ param string $key
* @ return mixed value or null if key not found
*/
public function __get ( $key )
{
if ( $this -> is ( $key ))
{
return $this -> get ( $key );
}
return null ;
}
/**
* Magic method to check if given data key is set .
* Triggered on < code >< ? php isset ( $e_object -> myKey ); </ code >
* NOTE : works on PHP 5.1 . 0 +
*
* @ param string $key
* @ return boolean
*/
public function __isset ( $key )
{
return $this -> is ( $key );
}
/**
* Magic method to unset given data key .
* Triggered on < code >< ? php unset ( $e_object -> myKey ); </ code >
* NOTE : works on PHP 5.1 . 0 +
*
* @ param string $key
*/
public function __unset ( $key )
{
$this -> remove ( $key );
}
2010-12-10 14:25:36 +00:00
}
2010-12-11 15:55:24 +00:00
/**
* Data object for e_parse :: simpleParse ()
* NEW - not inherits core e_object
* Moved from e_parse_class . php
* Could go in separate file in the future , together with e_object class
*/
class e_vars extends e_object
{
/**
* Get data array
*
* @ return array
*/
public function getVars ()
{
return $this -> getData ();
}
/**
* Set array data
*
* @ param array $array
* @ return e_vars
*/
public function setVars ( array $array )
{
$this -> setData ( $array );
return $this ;
}
/**
* Add array data to the object ( merge with existing )
*
* @ param array $array
* @ return e_vars
*/
public function addVars ( array $array )
{
$this -> addData ( $array );
}
/**
* Reset object data
*
* @ return e_vars
*/
public function emptyVars ()
{
$this -> removeData ();
return $this ;
}
/**
* Check if there is data available
*
* @ return boolean
*/
public function isEmpty ()
{
2010-12-23 10:26:40 +00:00
return ( ! $this -> hasData ());
2010-12-11 15:55:24 +00:00
}
2014-02-25 11:43:28 +02:00
2010-12-11 15:55:24 +00:00
/**
* Check if given data key is set
* @ param string $key
* @ return boolean
*/
public function isVar ( $key )
{
return $this -> is ( $key );
}
2014-02-25 11:43:28 +02:00
2010-12-11 15:55:24 +00:00
/**
* No need of object conversion , optional cloning
* @ param boolean $clone return current object clone
* @ return e_vars
*/
public function toSc ( $clone = false )
{
if ( $clone ) return clone $this ;
return $this ;
}
}
2010-12-10 14:25:36 +00:00
/**
* Base e107 Model class
*
* @ package e107
* @ category e107_handlers
* @ version 1.0
* @ author SecretR
* @ copyright Copyright ( C ) 2010 , e107 Inc .
*/
class e_model extends e_object
{
2009-10-22 14:18:18 +00:00
/**
2010-05-13 15:47:31 +00:00
* Data structure ( types ) array , required for { @ link e_front_model :: sanitize ()} method ,
2009-10-22 14:18:18 +00:00
* it also serves as a map ( find data ) for building DB queries ,
* copy / sanitize posted data to object data , etc .
2009-12-23 15:12:13 +00:00
*
2009-10-22 14:18:18 +00:00
* This can / should be overwritten by extending the class
*
* @ var array
*/
protected $_data_fields = array ();
2009-12-23 15:12:13 +00:00
2017-01-29 11:08:43 -08:00
/**
* Current model field types eg . text , bbarea , dropdown etc .
*
*
* @ var string
*/
protected $_field_input_types = array ();
2009-10-30 17:59:32 +00:00
/**
* Current model DB table , used in all db calls
2009-12-23 15:12:13 +00:00
*
2009-10-30 17:59:32 +00:00
* This can / should be overwritten / set by extending the class
2009-12-23 15:12:13 +00:00
*
2009-10-30 17:59:32 +00:00
* @ var string
*/
protected $_db_table ;
2014-02-25 11:43:28 +02:00
2013-02-26 16:08:08 -08:00
/**
2013-02-27 19:36:53 +02:00
* Current url Profile data
* Example : array ( 'route' => 'page/view/index' , 'vars' => array ( 'id' => 'page_id' , 'sef' => 'page_sef' ), 'name' => 'page_title' , 'description' => '' );
2013-02-26 16:08:08 -08:00
* @ var string
*/
2013-02-28 13:40:21 +02:00
protected $_url = array ();
2014-02-25 11:43:28 +02:00
2013-03-07 22:45:59 -08:00
/**
* Current Featurebox Profile data
* Example : array ( 'title' => 'page_title' , 'text' => '' );
* @ var string
*/
protected $_featurebox = array ();
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Runtime cache of parsed from { @ link _getData ()} keys
*
* @ var array
*/
protected $_parsed_keys = array ();
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Avoid DB calls if data is not changed
*
2009-10-20 16:05:03 +00:00
* @ see _setData ()
2009-08-20 13:06:34 +00:00
* @ var boolean
*/
protected $data_has_changed = false ;
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Namespace to be used for model related system messages in { @ link eMessage } handler
2009-12-23 15:12:13 +00:00
*
* @ var string
2009-10-20 16:05:03 +00:00
*/
protected $_message_stack = 'default' ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Cache string to be used from _get / set / clearCacheData () methods
*
* @ var string
*/
protected $_cache_string = null ;
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Force Cache even if system cahche is disabled
* Default is false
2010-05-05 15:05:32 +00:00
*
2010-04-28 15:44:46 +00:00
* @ var boolean
*/
protected $_cache_force = false ;
2009-12-23 15:12:13 +00:00
2014-02-25 11:43:28 +02:00
2009-10-30 17:59:32 +00:00
/**
* Optional DB table - used for auto - load data from the DB
* @ param string $table
* @ return e_model
2009-12-23 15:12:13 +00:00
*/
2009-10-30 17:59:32 +00:00
public function getModelTable ()
{
return $this -> _db_table ;
}
/**
* Set model DB table
* @ param string $table
* @ return e_model
2009-12-23 15:12:13 +00:00
*/
2009-10-30 17:59:32 +00:00
public function setModelTable ( $table )
{
$this -> _db_table = $table ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2013-02-26 16:08:08 -08:00
/**
* Set model Url Profile
* @ param string $table
* @ return e_model
*/
public function setUrl ( $url )
{
2013-02-28 13:40:21 +02:00
if ( ! is_array ( $url )) $url = array ( 'route' => $url );
2013-02-26 16:08:08 -08:00
$this -> _url = $url ;
return $this ;
}
2014-02-25 11:43:28 +02:00
2013-02-26 16:08:08 -08:00
/**
* Get url profile
* @ return array
*/
public function getUrl ()
{
return $this -> _url ;
}
2014-02-25 11:43:28 +02:00
2013-03-07 22:45:59 -08:00
/**
* Set model Featurebox Profile
* @ param string $table
* @ return e_model
*/
public function setFeaturebox ( $fb )
{
// if(!is_array($url)) $url = array('route' => $url);
$this -> _featurebox = $fb ;
return $this ;
}
2014-02-25 11:43:28 +02:00
2013-03-07 22:45:59 -08:00
/**
* Get Featurebox profile
* @ return array
*/
public function getFeaturebox ()
{
return $this -> _featurebox ;
2014-02-25 11:43:28 +02:00
}
2013-02-27 19:36:53 +02:00
/**
* Generic URL assembling method
* @ param array $options [ optional ] see eRouter :: assemble () for $options structure
* @ param boolean $extended [ optional ] if true , method will return an array containing url , title and description of the url
* @ return mixed URL string or extended array data
*/
2016-06-09 16:43:36 -07:00
public function url ( $ids , $options = array (), $extended = false )
2013-02-27 19:36:53 +02:00
{
2014-02-25 11:43:28 +02:00
$urldata = $this -> getUrl ();
2013-02-27 19:36:53 +02:00
if ( empty ( $urldata ) || ! vartrue ( $urldata [ 'route' ])) return ( $extended ? array () : null );
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
$eurl = e107 :: getUrl ();
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
if ( empty ( $options )) $options = array ();
elseif ( ! is_array ( $options )) parse_str ( $options , $options );
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
$vars = $this -> toArray ();
2013-04-29 14:27:57 +03:00
if ( ! isset ( $options [ 'allow' ]) || empty ( $options [ 'allow' ]))
2013-02-27 19:36:53 +02:00
{
if ( vartrue ( $urldata [ 'vars' ]) && is_array ( $urldata [ 'vars' ]))
{
$vars = array ();
2014-02-25 11:43:28 +02:00
foreach ( $urldata [ 'vars' ] as $var => $field )
2013-02-27 19:36:53 +02:00
{
if ( $field === true ) $field = $var ;
$vars [ $var ] = $this -> get ( $field );
}
}
}
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
$method = isset ( $options [ 'sc' ]) ? 'sc' : 'create' ;
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
$url = e107 :: getUrl () -> $method ( $urldata [ 'route' ], $vars , $options );
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
if ( ! $extended )
{
return $url ;
}
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
return array (
2014-02-25 11:43:28 +02:00
'url' => $url ,
2013-02-27 19:36:53 +02:00
'name' => vartrue ( $urldata [ 'name' ]) ? $this -> get ( $urldata [ 'name' ]) : '' ,
'description' => vartrue ( $urldata [ 'description' ]) ? $this -> get ( $urldata [ 'description' ]) : '' ,
);
}
2014-02-25 11:43:28 +02:00
2013-03-07 22:45:59 -08:00
/**
* Generic Featurebox assembling method
* @ return mixed URL string or extended array data
*/
public function featurebox ( $options = array (), $extended = false )
{
2014-02-25 11:43:28 +02:00
}
2009-10-22 14:18:18 +00:00
/**
2009-10-28 17:05:35 +00:00
* Get data fields array
2009-10-22 14:18:18 +00:00
* @ return array
*/
public function getDataFields ()
{
return $this -> _data_fields ;
}
2009-12-23 15:12:13 +00:00
2017-01-29 11:08:43 -08:00
/**
* @ param $key
* @ return bool
*/
public function getFieldInputType ( $key )
{
if ( isset ( $this -> _field_input_types [ $key ]))
{
return $this -> _field_input_types [ $key ];
}
return false ;
}
2009-10-28 17:05:35 +00:00
/**
* Set Predefined data fields in format key => type
* @ return e_model
*/
public function setDataFields ( $data_fields )
{
$this -> _data_fields = $data_fields ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2017-01-29 11:08:43 -08:00
/**
* Set Predefined data fields in format key => type
* @ return e_model
*/
public function setFieldInputTypes ( $fields )
{
$this -> _field_input_types = $fields ;
return $this ;
}
2009-11-18 09:32:31 +00:00
/**
* Set Predefined data field
* @ return e_model
*/
public function setDataField ( $field , $type )
{
$this -> _data_fields [ $field ] = $type ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Retrieves data from the object ( $_data ) without
* key parsing ( performance wise , prefered when possible )
*
* @ see _getDataSimple ()
* @ param string $key
* @ param mixed $default
* @ return mixed
*/
2009-09-03 14:15:36 +00:00
public function get ( $key , $default = null )
2009-08-20 13:06:34 +00:00
{
2009-09-03 14:15:36 +00:00
return $this -> _getDataSimple (( string ) $key , $default );
2009-08-20 13:06:34 +00:00
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Retrieves data from the object ( $_data )
* If $key is empty , return all object data
*
* @ see _getData ()
* @ param string $key
* @ param mixed $default
* @ param integer $index
* @ return mixed
*/
public function getData ( $key = '' , $default = null , $index = null )
{
return $this -> _getData ( $key , $default , $index );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Overwrite data in the object for a single field . Key is not parsed .
* Public proxy of { @ link _setDataSimple ()}
* Data isn ' t sanitized so use this method only when data comes from trustable sources ( e . g . DB )
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
*
* @ see _setData ()
* @ param string $key
* @ param mixed $value
* @ param boolean $strict update only
* @ return e_model
*/
public function set ( $key , $value = null , $strict = false )
{
return $this -> _setDataSimple ( $key , $value , $strict );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Overwrite data in the object . Public proxy of { @ link _setData ()}
* Data isn ' t sanitized so use this method only when data comes from trustable sources ( e . g . DB )
*
* @ see _setData ()
* @ param string | array $key
* @ param mixed $value
* @ param boolean $strict update only
* @ return e_model
*/
public function setData ( $key , $value = null , $strict = false )
{
return $this -> _setData ( $key , $value , $strict );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Add data to the object .
* Retains existing data in the object .
* Public proxy of { @ link _addData ()}
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* If $override is false , data will be updated only ( check against existing data )
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* @ param string | array $key
* @ param mixed $value
* @ param boolean $override override existing data
* @ return e_model
*/
public function addData ( $key , $value = null , $override = true )
{
return $this -> _addData ( $key , $value , $override );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Unset single field from the object .
* Public proxy of { @ link _unsetDataSimple ()}
*
* @ param string $key
* @ return e_model
*/
public function remove ( $key )
{
return $this -> _unsetDataSimple ( $key );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Unset data from the object .
* $key can be a string only . Array will be ignored .
* '/' inside the key will be treated as array path
* if $key is null entire object will be reset
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* Public proxy of { @ link _unsetData ()}
*
* @ param string | null $key
* @ return e_model
*/
public function removeData ( $key = null )
{
return $this -> _unsetData ( $key );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* @ param string $key
* @ return boolean
*/
public function has ( $key )
{
return $this -> _hasData ( $key );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* @ param string $key
* @ return boolean
*/
public function hasData ( $key = '' )
{
return $this -> _hasData ( $key );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* @ param string $key
* @ return boolean
*/
public function isData ( $key )
{
return $this -> _isData ( $key );
}
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
/**
* @ param boolean $new_state new object state if set
* @ return boolean
*/
public function isModified ( $new_state = null )
{
if ( is_bool ( $new_state ))
{
$this -> data_has_changed = $new_state ;
}
return $this -> data_has_changed ;
}
2009-08-20 13:06:34 +00:00
/**
2009-10-20 16:05:03 +00:00
* Retrieves data from the object
2009-08-20 13:06:34 +00:00
*
2009-10-20 16:05:03 +00:00
* If $key is empty will return all the data as an array
* Otherwise it will return value of the attribute specified by $key
* '/' inside the key will be treated as array path ( x / y / z equals to [ x ][ y ][ z ]
2009-08-20 13:06:34 +00:00
*
2009-10-20 16:05:03 +00:00
* If $index is specified it will assume that attribute data is an array
* and retrieve corresponding member .
2014-02-25 11:43:28 +02:00
*
2010-12-17 13:17:11 +00:00
* NEW : '/' supported in keys now , just use double slashes '//' as key separator
* Examples :
* - 'key//some/key/with/slashes//more' -> [ key ][ some / key / with / slashes ][ more ]
* - '//some/key' -> [ some / key ] - starting with // means - don't parse!
* - '///some/key/' -> [ / some / key / ]
* - '//key//some/key/with/slashes//more' WRONG -> single key [ key //some/key/with/slashes//more]
2014-02-25 11:43:28 +02:00
*
2009-10-20 16:05:03 +00:00
* @ param string $key
* @ param mixed $default
* @ param integer $index
* @ param boolean $posted data source
* @ return mixed
2009-08-20 13:06:34 +00:00
*/
protected function _getData ( $key = '' , $default = null , $index = null , $data_src = '_data' )
{
2009-12-23 15:12:13 +00:00
if ( '' === $key )
2009-08-20 13:06:34 +00:00
{
return $this -> $data_src ;
}
2014-02-25 11:43:28 +02:00
2010-12-17 13:17:11 +00:00
$simple = false ;
if ( strpos ( $key , '//' ) === 0 )
{
$key = substr ( $key , 2 );
$simple = true ;
}
/* elseif ( $key [ 0 ] == '/' )
{
// just use it!
2014-02-25 11:43:28 +02:00
$simple = true ;
2010-12-17 13:17:11 +00:00
} */
else
{
2014-02-25 11:43:28 +02:00
$simple = strpos ( $key , '/' ) === false ;
2010-12-17 13:17:11 +00:00
}
2009-08-20 13:06:34 +00:00
2010-12-17 11:07:18 +00:00
// Fix - check if _data[path/to/value] really doesn't exist
2010-12-17 13:17:11 +00:00
if ( ! $simple )
2009-08-20 13:06:34 +00:00
{
2010-12-17 13:17:11 +00:00
//$key = trim($key, '/');
2009-08-20 13:06:34 +00:00
if ( isset ( $this -> _parsed_keys [ $data_src . '/' . $key ]))
{
return $this -> _parsed_keys [ $data_src . '/' . $key ];
}
2014-02-25 11:43:28 +02:00
// new feature (double slash) - when searched key string is key//some/key/with/slashes//more
2010-12-17 13:17:11 +00:00
// -> search for 'key' => array('some/key/with/slashes', array('more' => value));
$keyArr = explode ( strpos ( $key , '//' ) ? '//' : '/' , $key );
2009-08-20 13:06:34 +00:00
$data = $this -> $data_src ;
2010-12-17 11:07:18 +00:00
foreach ( $keyArr as $i => $k )
2009-08-20 13:06:34 +00:00
{
2009-12-23 15:12:13 +00:00
if ( '' === $k )
2009-08-20 13:06:34 +00:00
{
return $default ;
}
2010-12-17 13:17:11 +00:00
2009-12-23 15:12:13 +00:00
if ( is_array ( $data ))
2009-08-20 13:06:34 +00:00
{
2009-12-23 15:12:13 +00:00
if ( ! isset ( $data [ $k ]))
2009-08-20 13:06:34 +00:00
{
return $default ;
}
$data = $data [ $k ];
}
2009-12-23 15:12:13 +00:00
else
2009-08-20 13:06:34 +00:00
{
return $default ;
}
}
$this -> _parsed_keys [ $data_src . '/' . $key ] = $data ;
return $data ;
}
//get $index
2009-12-23 15:12:13 +00:00
if ( isset ( $this -> { $data_src }[ $key ]))
2009-08-20 13:06:34 +00:00
{
2009-12-23 15:12:13 +00:00
if ( null === $index )
2009-08-20 13:06:34 +00:00
{
return $this -> { $data_src }[ $key ];
}
$value = $this -> { $data_src }[ $key ];
2009-12-23 15:12:13 +00:00
if ( is_array ( $value ))
2009-08-20 13:06:34 +00:00
{
2009-12-23 15:12:13 +00:00
if ( isset ( $value [ $index ]))
2009-08-20 13:06:34 +00:00
{
return $value [ $index ];
}
return $default ;
2009-12-23 15:12:13 +00:00
}
elseif ( is_string ( $value ))
2009-08-20 13:06:34 +00:00
{
$arr = explode ( " \n " , $value );
return ( isset ( $arr [ $index ]) ? $arr [ $index ] : $default );
}
return $default ;
}
return $default ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Get value from _data array without parsing the key
*
* @ param string $key
* @ param mixed $default
* @ param string $posted data source
* @ return mixed
*/
protected function _getDataSimple ( $key , $default = null , $data_src = '_data' )
{
return isset ( $this -> { $data_src }[ $key ]) ? $this -> { $data_src }[ $key ] : $default ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Overwrite data in the object .
*
* $key can be string or array .
* If $key is string , the attribute value will be overwritten by $value
* '/' inside the key will be treated as array path
*
* If $key is an array and $strict is false , it will overwrite all the data in the object .
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* If $strict is true and $data_src is '_data' , data will be updated only ( no new data will be added )
*
2010-12-17 13:17:11 +00:00
* NEW : '/' supported in keys now , just use double slashes '//' as key separator
* Examples :
* - 'key//some/key/with/slashes//more' -> [ key ][ some / key / with / slashes ][ more ]
* - '//some/key' -> [ some / key ] - starting with // means - don't parse!
* - '///some/key/' -> [ / some / key / ]
* - '//key//some/key/with/slashes//more' WRONG -> single key [ key //some/key/with/slashes//more]
2014-02-25 11:43:28 +02:00
*
2010-12-17 13:17:11 +00:00
*
2009-08-20 13:06:34 +00:00
* @ param string | array $key
* @ param mixed $value
* @ param boolean $strict
* @ param string $data_src
* @ return e_model
*/
protected function _setData ( $key , $value = null , $strict = false , $data_src = '_data' )
{
2009-12-23 15:12:13 +00:00
if ( is_array ( $key ))
2009-08-20 13:06:34 +00:00
{
2009-09-04 17:04:15 +00:00
if ( $strict )
2009-08-20 13:06:34 +00:00
{
2010-12-17 13:17:11 +00:00
foreach ( $key as $k => $v )
2009-08-20 13:06:34 +00:00
{
2010-12-17 13:17:11 +00:00
$this -> _setData ( $k , $v , true , $data_src );
2009-08-20 13:06:34 +00:00
}
return $this ;
}
2009-11-06 00:02:12 +00:00
$this -> $data_src = $key ;
2009-08-20 13:06:34 +00:00
return $this ;
2009-12-23 15:12:13 +00:00
}
2009-08-20 13:06:34 +00:00
//multidimensional array support - strict _setData for values of type array
if ( $strict && ! empty ( $value ) && is_array ( $value ))
{
foreach ( $value as $k => $v )
{
2014-02-25 11:43:28 +02:00
// new - $k couldn't be a path - e.g. 'key' 'value/value1'
2010-12-17 13:17:11 +00:00
// will result in 'key' => 'value/value1' and NOT 'key' => array('value' => value1)
$this -> _setData ( $key . '//' . $k , $v , true , $data_src );
2009-08-20 13:06:34 +00:00
}
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-12-17 13:17:11 +00:00
$simple = false ;
if ( strpos ( $key , '//' ) === 0 )
{
// NEW - leading '//' means set 'some/key' without parsing it
// Example: '//some/key'; NOTE: '//some/key//more/depth' is NOT parsed
// if you wish to have array('some/key' => array('more/depth' => value))
// right syntax is 'some/key//more/depth'
$key = substr ( $key , 2 );
$simple = true ;
}
/* elseif ( $key [ 0 ] == '/' )
{
2014-02-25 11:43:28 +02:00
$simple = true ;
2010-12-17 13:17:11 +00:00
} */
else
{
2014-02-25 11:43:28 +02:00
$simple = strpos ( $key , '/' ) === false ;
2010-12-17 13:17:11 +00:00
}
2009-12-23 15:12:13 +00:00
2014-02-25 11:43:28 +02:00
//multidimensional array support - parse key
2010-12-17 13:17:11 +00:00
if ( ! $simple )
2009-08-20 13:06:34 +00:00
{
2010-12-17 13:17:11 +00:00
//$key = trim($key, '/');
2009-08-20 13:06:34 +00:00
//if strict - update only
if ( $strict && ! $this -> isData ( $key ))
{
return $this ;
}
2014-02-25 11:43:28 +02:00
// new feature (double slash) - when parsing key: key//some/key/with/slashes//more
2010-12-17 13:17:11 +00:00
// -> result is 'key' => array('some/key/with/slashes', array('more' => value));
$keyArr = explode ( strpos ( $key , '//' ) ? '//' : '/' , $key );
//$keyArr = explode('/', $key);
2009-10-20 16:05:03 +00:00
$data = & $this -> { $data_src };
2009-12-23 15:12:13 +00:00
for ( $i = 0 , $l = count ( $keyArr ); $i < $l ; $i ++ )
2009-08-20 13:06:34 +00:00
{
2017-01-13 10:34:03 -08:00
2009-08-20 13:06:34 +00:00
$k = $keyArr [ $i ];
2009-12-23 15:12:13 +00:00
2017-01-13 10:34:03 -08:00
if ( ! isset ( $data [ $k ]) || empty ( $data [ $k ])) // PHP7.1 fix. Reset to empty array() if $data[$k] is an empty string. Reason for empty string still unknown.
2009-08-20 13:06:34 +00:00
{
$data [ $k ] = array ();
}
2017-01-13 10:34:03 -08:00
2009-08-20 13:06:34 +00:00
$data = & $data [ $k ];
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
//data has changed - optimized
if ( '_data' === $data_src && ! $this -> data_has_changed )
{
2009-08-27 23:37:24 +00:00
$this -> data_has_changed = ( ! isset ( $this -> _data [ $key ]) || $this -> _data [ $key ] != $value );
2009-08-20 13:06:34 +00:00
}
$this -> _parsed_keys [ $data_src . '/' . $key ] = $value ;
$data = $value ;
}
2009-12-23 15:12:13 +00:00
else
2009-08-20 13:06:34 +00:00
{
//if strict - update only
if ( $strict && ! isset ( $this -> _data [ $key ]))
{
return $this ;
}
if ( '_data' === $data_src && ! $this -> data_has_changed )
{
2009-08-27 23:37:24 +00:00
$this -> data_has_changed = ( ! isset ( $this -> _data [ $key ]) || $this -> { $data_src }[ $key ] != $value );
2009-08-20 13:06:34 +00:00
}
$this -> { $data_src }[ $key ] = $value ;
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Set data for the given source . More simple ( and performance wise ) version
* of { @ link _setData ()}
*
* @ param string $key
* @ param mixed $value
* @ param boolean $strict
* @ param string $data_src
* @ return e_model
*/
2009-09-03 14:15:36 +00:00
protected function _setDataSimple ( $key , $value = null , $strict = false , $data_src = '_data' )
2009-08-20 13:06:34 +00:00
{
2009-09-03 14:15:36 +00:00
$key = $key . '' ; //smart toString
2009-08-20 13:06:34 +00:00
if ( ! $strict )
{
//data has changed
if ( '_data' === $data_src && ! $this -> data_has_changed )
{
2009-08-27 23:37:24 +00:00
$this -> data_has_changed = ( ! isset ( $this -> _data [ $key ]) || $this -> _data [ $key ] != $value );
2009-08-20 13:06:34 +00:00
}
$this -> { $data_src }[ $key ] = $value ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
if ( $this -> isData ( $key ))
{
if ( '_data' === $data_src && ! $this -> data_has_changed )
{
2009-08-27 23:37:24 +00:00
$this -> data_has_changed = ( ! isset ( $this -> _data [ $key ]) || $this -> _data [ $key ] != $value );
2009-08-20 13:06:34 +00:00
}
$this -> { $data_src }[ $key ] = $value ;
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Add data to the object .
* Retains existing data in the object .
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* If $override is false , only new ( non - existent ) data will be added
2009-12-23 15:12:13 +00:00
*
2009-08-20 13:06:34 +00:00
* @ param string | array $key
* @ param mixed $value
* @ param boolean $override allow override of existing data
* @ param string $data_src data source
* @ return e_model
*/
protected function _addData ( $key , $value = null , $override = true , $data_src = '_data' )
{
if ( is_array ( $key ))
{
foreach ( $key as $k => $v )
{
$this -> _addData ( $k , $v , $override , $data_src );
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
if ( $override || ! $this -> _isData ( $key , $data_src ))
{
if ( is_array ( $value ))
{
2009-09-03 22:27:32 +00:00
if ( is_array ( $key ))
2009-08-20 13:06:34 +00:00
{
2009-09-03 22:27:32 +00:00
foreach ( $key as $k => $v )
{
$this -> _addData ( $key . '/' . $k , $v , $override , $data_src );
}
2009-08-20 13:06:34 +00:00
}
return $this ;
}
$this -> _setData ( $key , $value , false , $data_src );
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Unset data from the object from the given source .
* $key can be a string only . Array will be ignored .
* '/' inside the key will be treated as array path
* if $key is null entire object will be reset
*
* @ param string | null $key
* @ param string $data_src data source
* @ return e_model
*/
protected function _unsetData ( $key = null , $data_src = '_data' )
{
2009-12-23 15:12:13 +00:00
if ( null === $key )
2009-08-20 13:06:34 +00:00
{
if ( '_data' === $data_src && ! empty ( $this -> _data ))
{
$this -> data_has_changed = true ;
}
2009-10-07 10:53:33 +00:00
$this -> { $data_src } = array ();
2009-08-20 13:06:34 +00:00
return $this ;
2009-12-23 15:12:13 +00:00
}
2009-08-20 13:06:34 +00:00
$key = trim ( $key , '/' );
2009-12-23 15:12:13 +00:00
if ( strpos ( $key , '/' ))
2009-08-20 13:06:34 +00:00
{
$keyArr = explode ( '/' , $key );
2009-10-07 10:53:33 +00:00
$data = & $this -> { $data_src };
2009-12-23 15:12:13 +00:00
2009-10-07 10:53:33 +00:00
$unskey = array_pop ( $keyArr );
2009-12-23 15:12:13 +00:00
for ( $i = 0 , $l = count ( $keyArr ); $i < $l ; $i ++ )
2009-08-20 13:06:34 +00:00
{
$k = $keyArr [ $i ];
2009-12-23 15:12:13 +00:00
if ( ! isset ( $data [ $k ]))
2009-08-20 13:06:34 +00:00
{
return $this ; //not found
}
$data = & $data [ $k ];
}
if ( is_array ( $data ))
{
if ( '_data' === $data_src && isset ( $data [ $unskey ]))
{
$this -> data_has_changed = true ;
}
unset ( $data [ $unskey ], $this -> _parsed_keys [ $data_src . '/' . $key ]);
}
}
2009-12-23 15:12:13 +00:00
else
2009-08-20 13:06:34 +00:00
{
if ( '_data' === $data_src && isset ( $this -> { $data_src }[ $key ]))
{
$this -> data_has_changed = true ;
}
unset ( $this -> { $data_src }[ $key ]);
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Unset single field from the object from the given source . Key is not parsed
*
* @ param string $key
* @ param string $data_src data source
* @ return e_model
*/
protected function _unsetDataSimple ( $key , $data_src = '_data' )
{
if ( '_data' === $data_src && isset ( $this -> { $data_src }[ $key ]))
{
$this -> data_has_changed = true ;
}
unset ( $this -> { $data_src }[ $key ]);
return $this ;
}
/**
* If $key is empty , checks whether there ' s any data in the object
* Otherwise checks if the specified key is empty / set .
*
* @ param string $key
* @ param string $data_src data source
* @ return boolean
*/
protected function _hasData ( $key = '' , $data_src = '_data' )
{
2009-12-23 15:12:13 +00:00
if ( empty ( $key ))
2009-08-20 13:06:34 +00:00
{
return ! empty ( $this -> $data_src );
}
$value = $this -> _getData ( $key , null , null , $data_src );
return ! empty ( $value );
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Checks if the specified key is set
*
* @ param string $key
* @ param string $data_src data source
* @ return boolean
*/
protected function _isData ( $key , $data_src = '_data' )
{
return ( null !== $this -> _getData ( $key , null , null , $data_src ));
}
2009-10-20 16:05:03 +00:00
/**
* Add system message of type Information
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $message
* @ param boolean $session [ optional ]
2009-11-01 19:05:26 +00:00
* @ return e_model
2009-10-20 16:05:03 +00:00
*/
public function addMessageInfo ( $message , $session = false )
{
e107 :: getMessage () -> addStack ( $message , $this -> _message_stack , E_MESSAGE_INFO , $session );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-01 19:05:26 +00:00
/**
* Add system message of type Success
2009-12-23 15:12:13 +00:00
*
2009-11-01 19:05:26 +00:00
* @ param string $message
* @ param boolean $session [ optional ]
* @ return e_model
*/
public function addMessageSuccess ( $message , $session = false )
{
e107 :: getMessage () -> addStack ( $message , $this -> _message_stack , E_MESSAGE_SUCCESS , $session );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Add system message of type Warning
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $message
* @ param boolean $session [ optional ]
2009-11-01 19:05:26 +00:00
* @ return e_model
2009-10-20 16:05:03 +00:00
*/
public function addMessageWarning ( $message , $session = false )
{
e107 :: getMessage () -> addStack ( $message , $this -> _message_stack , E_MESSAGE_WARNING , $session );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Add system message of type Error
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $message
* @ param boolean $session [ optional ]
2009-11-01 19:05:26 +00:00
* @ return e_model
2009-10-20 16:05:03 +00:00
*/
public function addMessageError ( $message , $session = false )
{
e107 :: getMessage () -> addStack ( $message , $this -> _message_stack , E_MESSAGE_ERROR , $session );
2014-01-17 16:46:24 -08:00
e107 :: getAdminLog () -> addError ( $message , false ) -> save ( 'ADMINUI_04' );
2009-10-20 16:05:03 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Add system message of type Information
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $message
* @ param boolean $session [ optional ]
2009-11-01 19:05:26 +00:00
* @ return e_model
2009-10-20 16:05:03 +00:00
*/
public function addMessageDebug ( $message , $session = false )
{
e107 :: getMessage () -> addStack ( $message , $this -> _message_stack , E_MESSAGE_DEBUG , $session );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
2009-10-21 09:12:12 +00:00
* Render System messages ( if any )
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param boolean $session store messages to session
* @ param boolean $reset reset errors
* @ return string
2009-08-20 13:06:34 +00:00
*/
2009-10-21 09:12:12 +00:00
public function renderMessages ( $session = false , $reset = true )
2009-08-20 13:06:34 +00:00
{
2009-10-21 09:12:12 +00:00
return e107 :: getMessage () -> render ( $this -> _message_stack , $session , $reset );
2009-08-20 13:06:34 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-22 14:18:18 +00:00
/**
* Move model System messages ( if any ) to the default eMessage stack
2009-12-23 15:12:13 +00:00
*
2009-10-22 14:18:18 +00:00
* @ param boolean $session store messages to session
2009-11-01 19:05:26 +00:00
* @ return e_model
2009-10-22 14:18:18 +00:00
*/
public function setMessages ( $session = false )
{
e107 :: getMessage () -> moveStack ( $this -> _message_stack , 'default' , false , $session );
return $this ;
}
2010-02-21 17:42:07 +00:00
2010-02-08 14:52:34 +00:00
/**
* Reset model System messages
2010-02-21 17:42:07 +00:00
*
2010-02-08 14:52:34 +00:00
* @ param boolean | string $type E_MESSAGE_INFO | E_MESSAGE_SUCCESS | E_MESSAGE_WARNING | E_MESSAGE_WARNING | E_MESSAGE_DEBUG | false ( all )
* @ param boolean $session reset also session messages
* @ return e_model
*/
public function resetMessages ( $type = false , $session = false )
{
e107 :: getMessage () -> reset ( $type , $this -> _message_stack , $session );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-28 15:34:46 +00:00
/**
* Set model message stack
* @ param string $stack_name
* @ return e_model
*/
public function setMessageStackName ( $stack_name )
{
$this -> _message_stack = $stack_name ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-28 15:34:46 +00:00
/**
* Get model message stack name
* @ return string
*/
public function getMessageStackName ()
{
return $this -> _message_stack ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
2009-10-20 16:05:03 +00:00
* User defined model validation
* Awaiting for child class implementation
*
2009-08-20 13:06:34 +00:00
*/
2009-10-20 16:05:03 +00:00
public function verify ()
2009-08-20 13:06:34 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
2009-12-23 15:12:13 +00:00
* Model validation
2009-10-20 16:05:03 +00:00
* @ see e_model_admin
2009-08-20 13:06:34 +00:00
*/
2009-10-21 09:12:12 +00:00
public function validate ()
2009-08-20 13:06:34 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
2009-10-30 17:59:32 +00:00
* Generic load data from DB
2013-10-16 18:13:21 +03:00
* @ param mixed $id
2009-10-30 17:59:32 +00:00
* @ param boolean $force
2010-03-22 15:45:47 +00:00
* @ return e_model
2009-08-20 13:06:34 +00:00
*/
2013-10-16 18:13:21 +03:00
public function load ( $id = null , $force = false )
2009-10-30 17:59:32 +00:00
{
2009-12-08 17:21:36 +00:00
if ( ! $force && $this -> getId ())
2009-10-30 17:59:32 +00:00
{
return $this ;
}
2010-05-05 15:05:32 +00:00
2009-11-17 15:34:54 +00:00
if ( $force )
{
2010-04-28 15:44:46 +00:00
$this -> setData ( array ())
-> _clearCacheData ();
2009-11-17 15:34:54 +00:00
}
2013-10-16 18:13:21 +03:00
if ( $id ) $id = e107 :: getParser () -> toDB ( $id );
if ( ! $id && ! $this -> getParam ( 'db_query' ))
2010-04-28 15:44:46 +00:00
{
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
$cached = $this -> _getCacheData ();
if ( $cached !== false )
{
$this -> setData ( $cached );
return $this ;
}
2009-12-23 15:12:13 +00:00
2010-02-21 17:42:07 +00:00
$sql = e107 :: getDb ();
2009-10-30 17:59:32 +00:00
$qry = str_replace ( '{ID}' , $id , $this -> getParam ( 'db_query' ));
2010-02-21 17:42:07 +00:00
if ( $qry )
2009-10-30 17:59:32 +00:00
{
2016-02-14 19:00:12 -08:00
$res = $sql -> gen ( $qry , $this -> getParam ( 'db_debug' ) ? true : false );
2010-02-21 17:42:07 +00:00
}
else
{
2013-02-06 17:03:00 +02:00
if ( ! is_numeric ( $id )) $id = " ' { $id } ' " ;
2014-02-25 11:43:28 +02:00
2016-02-14 19:00:12 -08:00
$res = $sql -> select (
2010-02-21 17:42:07 +00:00
$this -> getModelTable (),
$this -> getParam ( 'db_fields' , '*' ),
2010-11-02 10:31:05 +00:00
$this -> getFieldIdName () . '=' . $id . ' ' . trim ( $this -> getParam ( 'db_where' , '' )),
2010-02-21 17:42:07 +00:00
'default' ,
( $this -> getParam ( 'db_debug' ) ? true : false )
);
2009-10-30 17:59:32 +00:00
}
2009-12-08 17:21:36 +00:00
2010-02-21 17:42:07 +00:00
if ( $res )
2009-10-30 17:59:32 +00:00
{
2016-02-14 19:00:12 -08:00
$this -> setData ( $sql -> fetch ());
2009-10-30 17:59:32 +00:00
}
2009-12-23 15:12:13 +00:00
2009-12-08 17:21:36 +00:00
if ( $sql -> getLastErrorNumber ())
{
$this -> addMessageDebug ( 'SQL error #' . $sql -> getLastErrorNumber () . ': ' . $sql -> getLastErrorText ());
2010-05-02 18:41:20 +00:00
$this -> addMessageDebug ( $sql -> getLastQuery ());
2009-12-08 17:21:36 +00:00
}
2010-04-28 15:44:46 +00:00
else
{
$this -> _setCacheData ();
}
2009-12-08 17:21:36 +00:00
2009-10-30 17:59:32 +00:00
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-11-02 10:31:05 +00:00
/**
* Retrieve system cache ( if any )
* @ return array | false
*/
2010-04-28 15:44:46 +00:00
protected function _getCacheData ()
{
if ( ! $this -> isCacheEnabled ())
{
return false ;
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
$cached = e107 :: getCache () -> retrieve_sys ( $this -> getCacheString ( true ), false , $this -> _cache_force );
2010-05-05 15:05:32 +00:00
if ( false !== $cached )
2010-04-28 15:44:46 +00:00
{
2015-02-14 23:34:15 -08:00
return e107 :: unserialize ( $cached );
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
return false ;
}
2010-05-05 15:05:32 +00:00
2010-11-02 10:31:05 +00:00
/**
* Set system cache if enabled for the model
* @ return e_model
*/
2010-04-28 15:44:46 +00:00
protected function _setCacheData ()
{
if ( ! $this -> isCacheEnabled ())
{
return $this ;
}
e107 :: getCache () -> set_sys ( $this -> getCacheString ( true ), $this -> toString ( false ), $this -> _cache_force , false );
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-11-02 10:31:05 +00:00
/**
* Clrear system cache if enabled for the model
* @ return e_model
*/
2010-04-28 15:44:46 +00:00
protected function _clearCacheData ()
{
if ( ! $this -> isCacheEnabled ( false ))
{
return $this ;
}
e107 :: getCache () -> clear_sys ( $this -> getCacheString ( true ), false );
2010-11-02 10:31:05 +00:00
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-11-02 10:31:05 +00:00
/**
* Clrear system cache ( public proxy ) if enabled for the model
* @ return e_model
*/
public function clearCache ()
{
return $this -> _clearCacheData ();
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2010-11-02 10:31:05 +00:00
/**
* Check if cache is enabled for the current model
* @ param boolean $checkId check if there is model ID
* @ return boolean
*/
2010-04-28 15:44:46 +00:00
public function isCacheEnabled ( $checkId = true )
{
return ( null !== $this -> getCacheString () && ( ! $checkId || $this -> getId ()));
}
2010-05-05 15:05:32 +00:00
2010-11-02 10:31:05 +00:00
/**
* Get model cache string
* @ param boolean $replace try to add current model ID ( replace destination is { ID })
* @ return string
*/
2010-04-28 15:44:46 +00:00
public function getCacheString ( $replace = false )
{
return ( $replace ? str_replace ( '{ID}' , $this -> getId (), $this -> _cache_string ) : $this -> _cache_string );
}
2010-05-05 15:05:32 +00:00
2010-11-02 10:31:05 +00:00
/**
* Set model cache string
* @ param string $str
* @ return e_model
*/
2010-04-28 15:44:46 +00:00
public function setCacheString ( $str )
{
$this -> _cache_string = $str ;
2010-11-02 10:31:05 +00:00
return $this ;
2010-04-28 15:44:46 +00:00
}
2010-05-02 18:41:20 +00:00
/**
2010-05-05 15:05:32 +00:00
* Save data to DB
* Awaiting for child class implementation
* @ see e_model_admin
2010-05-02 18:41:20 +00:00
*/
2010-05-05 15:05:32 +00:00
public function save ()
2010-05-02 18:41:20 +00:00
{
}
/**
2010-05-05 15:05:32 +00:00
* Delete DB record
* Awaiting for child class implementation
* @ see e_model_admin
2010-05-02 18:41:20 +00:00
*/
2016-02-14 19:00:12 -08:00
public function delete ( $ids , $destroy = true , $session_messages = false )
2010-05-02 18:41:20 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
2010-05-05 15:05:32 +00:00
* Create new DB recorrd
2009-08-20 13:06:34 +00:00
* Awaiting for child class implementation
2009-10-20 16:05:03 +00:00
* @ see e_model_admin
2009-08-20 13:06:34 +00:00
*/
2010-05-05 15:05:32 +00:00
public function create ()
2009-08-20 13:06:34 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Insert data to DB
* Awaiting for child class implementation
2009-10-20 16:05:03 +00:00
* @ see e_model_admin
2009-08-20 13:06:34 +00:00
*/
2010-05-05 15:05:32 +00:00
protected function dbInsert ()
2009-08-20 13:06:34 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Update DB data
* Awaiting for child class implementation
2009-10-20 16:05:03 +00:00
* @ see e_model_admin
2009-08-20 13:06:34 +00:00
*/
2016-02-14 19:00:12 -08:00
protected function dbUpdate ( $force = false , $session_messages = false )
2009-08-20 13:06:34 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-10-30 17:59:32 +00:00
/**
* Replace DB record
* Awaiting for child class implementation
* @ see e_model_admin
*/
2010-05-05 15:05:32 +00:00
protected function dbReplace ()
2009-10-30 17:59:32 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-10-30 17:59:32 +00:00
/**
* Delete DB data
* Awaiting for child class implementation
* @ see e_model_admin
*/
2010-05-05 15:05:32 +00:00
protected function dbDelete ()
2009-10-30 17:59:32 +00:00
{
}
2009-12-23 15:12:13 +00:00
2009-10-30 17:59:32 +00:00
/**
* Set parameter array
2010-02-21 17:42:07 +00:00
* Core implemented :
2016-02-15 00:56:08 -08:00
* - db_query : string db query to be passed to load () ( $sql -> gen ())
2010-02-21 17:42:07 +00:00
* - db_query
* - db_fields
* - db_where
* - db_debug
2010-11-02 10:31:05 +00:00
* - model_class : e_tree_model class / subclasses - string class name for creating nodes inside default load () method
* - clearModelCache : e_tree_model class / subclasses - clear cache per node after successful DB operation
* - noCacheStringModify : e_tree_model class / subclasses - do not add additional md5 sum to tree cache string
2009-10-30 17:59:32 +00:00
* @ param array $params
* @ return e_model
*/
public function setParams ( array $params )
{
2010-12-10 14:25:36 +00:00
parent :: setParams ( $params );
2009-10-30 17:59:32 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-26 17:14:07 +00:00
/**
* Render model data , all 'sc_*' methods will be recongnized
* as shortcodes .
2009-12-23 15:12:13 +00:00
*
2009-11-26 17:14:07 +00:00
* @ param string $template
* @ param boolean $parsesc parse external shortcodes , default is true
2010-04-28 15:44:46 +00:00
* @ param e_vars $eVars simple parser data
2009-11-26 17:14:07 +00:00
* @ return string parsed template
*/
2010-04-28 15:44:46 +00:00
public function toHTML ( $template , $parsesc = true , $eVars = null )
2009-11-26 17:14:07 +00:00
{
2010-04-28 15:44:46 +00:00
return e107 :: getParser () -> parseTemplate ( $template , $parsesc , $this , $eVars );
2009-11-26 17:14:07 +00:00
}
2009-12-23 15:12:13 +00:00
2017-01-27 18:02:57 -08:00
/**
* Export a Model configuration
* @ return string
*/
2009-11-26 17:14:07 +00:00
public function toXML ()
{
$ret = " <?xml version= \" 1.0 \" encoding= \" utf-8 \" ?> \n " ;
$ret .= " <e107Export type= \" model \" version= \" 1.0 \" timestamp= \" " . time () . " \" > \n " ;
2009-12-23 15:12:13 +00:00
2009-11-26 17:14:07 +00:00
$ret .= " \t <data> \n " ;
// TODO - handle multi dimensional arrays (already possible - field1/field2?), method toXMLValue($value, $type)
foreach ( $this -> getDataFields () as $field => $type )
{
$ret .= " \t \t <field name= \" { $field } \" type= \" { $type } \" > " ;
$ret .= $type == 'str' || $type == 'string' ? " <![CDATA[ " . $this -> getData ( $field ) . " ]]> " : $this -> getData ( $field );
$ret .= " </field> \n " ;
}
$ret .= " \t </data> \n " ;
2009-12-23 15:12:13 +00:00
2009-11-26 17:14:07 +00:00
$ret .= " </e107Export> " ;
return $ret ;
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
2009-10-20 16:05:03 +00:00
* Try to convert string to a number
* Shoud fix locale related troubles
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $value
2009-11-26 17:14:07 +00:00
* @ return integer | float
2009-08-20 13:06:34 +00:00
*/
2009-10-20 16:05:03 +00:00
public function toNumber ( $value )
2009-08-20 13:06:34 +00:00
{
2011-01-04 12:10:47 +00:00
$larr = localeconv ();
$search = array (
2014-02-25 11:43:28 +02:00
$larr [ 'decimal_point' ],
$larr [ 'mon_decimal_point' ],
$larr [ 'thousands_sep' ],
$larr [ 'mon_thousands_sep' ],
$larr [ 'currency_symbol' ],
2011-01-04 12:10:47 +00:00
$larr [ 'int_curr_symbol' ]
);
$replace = array ( '.' , '.' , '' , '' , '' , '' );
2014-02-25 11:43:28 +02:00
2011-01-04 12:10:47 +00:00
return str_replace ( $search , $replace , $value );
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2009-08-20 13:06:34 +00:00
/**
* Convert object data to a string
*
* @ param boolean $AddSlashes
2009-08-21 22:17:59 +00:00
* @ param string $key optional , if set method will return corresponding value as a string
2009-08-20 13:06:34 +00:00
* @ return string
*/
2009-08-21 22:17:59 +00:00
public function toString ( $AddSlashes = true , $key = null )
2009-08-20 13:06:34 +00:00
{
2009-08-21 22:17:59 +00:00
if ( null !== $key )
{
$value = $this -> getData ( $key );
if ( is_array ( $value ))
{
return e107 :: getArrayStorage () -> WriteArray ( $value , $AddSlashes );
}
return ( string ) $value ;
}
2010-04-28 15:44:46 +00:00
return ( string ) e107 :: getArrayStorage () -> WriteArray ( $this -> toArray (), $AddSlashes );
2009-08-20 13:06:34 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-30 17:59:32 +00:00
public function destroy ()
{
$this -> _data = array ();
$this -> _params = array ();
$this -> _data_fields = array ();
$this -> _parsed_keys = array ();
$this -> _db_table = $this -> _field_id = '' ;
$this -> data_has_changed = false ;
}
2010-12-11 15:37:39 +00:00
/**
* Disable Magic setter
*/
public function __set ( $key , $value )
{
}
/**
* Disable Magic getter
*/
public function __get ( $key )
{
}
/**
* Disable
*/
public function __isset ( $key )
{
}
/**
* Disable
*/
public function __unset ( $key )
{
}
2009-10-20 16:05:03 +00:00
}
/**
2010-05-05 15:05:32 +00:00
* Base e107 Fron Model class interface
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* Some important points :
* - model data should be always in toDB () format :
* - retrieved direct from DB
* - set & sanitized via setPostedData () -> mergePostedData ()
2009-12-23 15:12:13 +00:00
* - manually sanitized before passed to model setter ( set (), setData (), add (), addData (), etc . ) methods
2009-10-22 14:18:18 +00:00
* - $_data_fields property is important , it tells to sanitize () method how to sanitize posted data
* - if $_data_fields is missing , sanitize () will call internally e107 :: getParser () -> toDB () on the data
2009-10-20 16:05:03 +00:00
* - sanitize () is triggered by default on mergePostedData () and mergeData () methods
* - mergePostedData () and mergeData () methods will filter posted / passed data against ( in this order ) :
* - getValidator () -> getValidData () if true is passed as validate parameter ( currently disabled , gather feedback )
2009-10-22 14:18:18 +00:00
* - $_data_fields if true is passed as sanitize parameter
* - toSqlQuery () needs $_data_fields and $_field_id to work proper , $_FIELD_TYPES is optional but recommended ( faster SQL queries )
* - result array from toSqlQuery () call will be filtered against $_data_fields
2009-10-20 16:05:03 +00:00
* - in almost every case $_FIELD_TYPES shouldn 't contain ' escape ' and ' todb ' - dont' t forget you are going to pass already sanitized data ( see above )
2009-10-22 14:18:18 +00:00
* - most probably $_FIELD_TYPES will go in the future , $_data_fields alone could do the job
2009-10-20 16:05:03 +00:00
* - default db related methods ( save (), dbUpdate (), etc . ) need $_db_table
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ package e107
* @ category e107_handlers
2010-05-05 15:05:32 +00:00
* @ version $Id $
2009-10-20 16:05:03 +00:00
* @ author SecretR
2010-05-05 15:05:32 +00:00
* @ copyright Copyright ( C ) 2008 - 2010 e107 Inc .
2009-10-20 16:05:03 +00:00
*/
2010-05-05 15:05:32 +00:00
class e_front_model extends e_model
2009-10-20 16:05:03 +00:00
{
/**
* Posted data
* Back - end related
*
* @ var array
*/
protected $_posted_data = array ();
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
/**
* DB format array - see db :: _getTypes () and db :: _getFieldValue () ( mysql_class . php )
* for example
*
* This can / should be overwritten by extending the class
*
* @ var array
*/
protected $_FIELD_TYPES = array ();
2009-10-20 16:05:03 +00:00
/**
* Validation structure - see { @ link e_validator :: $_required_rules } for
* more information about the array format .
* Used in { @ link validate ()} method .
2009-10-30 17:59:32 +00:00
* TODO - check_rules ( see e_validator :: $_optional_rules )
2009-10-20 16:05:03 +00:00
* This can / should be overwritten by extending the class .
*
* @ var array
*/
protected $_validation_rules = array ();
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
protected $_optional_rules = array ();
2009-10-20 16:05:03 +00:00
/**
* @ var integer Last SQL error number
*/
protected $_db_errno = 0 ;
2009-12-23 15:12:13 +00:00
2010-03-01 12:43:56 +00:00
/**
* @ var string Last SQL error message
*/
protected $_db_errmsg = '' ;
2014-02-03 14:04:13 +02:00
/**
* @ var string Last SQL query
*/
protected $_db_qry = '' ;
2009-10-20 16:05:03 +00:00
/**
* Validator object
2009-12-23 15:12:13 +00:00
*
* @ var e_validator
2009-10-20 16:05:03 +00:00
*/
protected $_validator = null ;
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* @ return array
*/
public function getValidationRules ()
{
return $this -> _validation_rules ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Set object validation rules if $_validation_rules array is empty
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param array $vrules
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
2010-05-05 15:05:32 +00:00
public function setValidationRules ( array $vrules , $force = false )
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
if ( $force || empty ( $this -> _validation_rules ))
2009-10-20 16:05:03 +00:00
{
$this -> _validation_rules = $vrules ;
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
/**
* @ return array
*/
public function getOptionalRules ()
{
return $this -> _optional_rules ;
}
/**
* @ param array $rules
* @ return e_front_model
*/
public function setOptionalRules ( array $rules )
{
$this -> _optional_rules = $rules ;
return $this ;
}
/**
* Set object validation rules if $_validation_rules array is empty
*
* @ param string $field
* @ param array $rule
* @ param boolean $required
* @ return e_front_model
*/
public function setValidationRule ( $field , $rule , $required = true )
{
$pname = $required ? '_validation_rules' : '_optional_rules' ;
$rules = & $this -> $pname ;
$rules [ $field ] = $rule ;
return $this ;
}
/**
* Predefined data fields types , passed to DB handler
* @ return array
*/
public function getDbTypes ()
{
return ( $this -> _FIELD_TYPES ? $this -> _FIELD_TYPES : $this -> getDataFields ());
}
/**
* Predefined data fields types , passed to DB handler
*
* @ param array $field_types
2010-05-13 15:47:31 +00:00
* @ return e_front_model
2010-05-05 15:05:32 +00:00
*/
public function setDbTypes ( $field_types )
{
$this -> _FIELD_TYPES = $field_types ;
return $this ;
}
/**
* Auto field type definitions
* Disabled for now , it should auto - create _data_types
* @ param boolean $force
* @ return boolean
*/
// public function setFieldTypeDefs($force = false)
// {
// if($force || !$this->getFieldTypes())
// {
// $ret = e107::getDb()->getFieldDefs($this->getModelTable());
// if($ret)
// {
// foreach ($ret as $k => $v)
// {
// if('todb' == $v)
// {
// $ret[$k] = 'string';
// }
// }
// $this->setFieldTypes($ret);
// return true;
// }
// }
// return false;
// }
2009-10-20 16:05:03 +00:00
/**
* Retrieves data from the object ( $_posted_data ) without
* key parsing ( performance wise , prefered when possible )
*
* @ see _getDataSimple ()
* @ param string $key
* @ param mixed $default
* @ return mixed
*/
public function getPosted ( $key , $default = null )
{
return $this -> _getDataSimple (( string ) $key , $default , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Retrieves data from the object ( $_posted_data )
* If $key is empty , return all object posted data
* @ see _getData ()
* @ param string $key
* @ param mixed $default
* @ param integer $index
* @ return mixed
*/
public function getPostedData ( $key = '' , $default = null , $index = null )
{
return $this -> _getData ( $key , $default , $index , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Search for requested data from available sources in this order :
* - posted data
* - default object data
* - passed default value
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* Use this method inside forms
*
* @ param string $key
* @ param string $default
* @ param integer $index
* @ return string
*/
public function getIfPosted ( $key , $default = '' , $index = null )
{
2014-08-18 05:22:51 -07:00
$d = $this -> getDataFields ();
2016-02-13 12:57:34 -08:00
if ( ! empty ( $d [ $key ]) && ( $d [ $key ] == 'array' ))
2014-08-18 05:22:51 -07:00
{
return e107 :: unserialize ( $this -> getData (( string ) $key , $default , $index ));
}
2009-11-21 11:15:29 +00:00
$posted = $this -> getPostedData (( string ) $key , null , $index );
if ( null !== $posted )
2009-10-20 16:05:03 +00:00
{
2010-02-12 16:42:28 +00:00
// FIXED - double post_toFom() and toDB(post_toForm()) problems
// setPosted|setPostedData|addPostedData methods are storing RAW data now
2009-11-21 11:15:29 +00:00
return e107 :: getParser () -> post_toForm ( $posted );
2009-10-20 16:05:03 +00:00
}
return e107 :: getParser () -> toForm ( $this -> getData (( string ) $key , $default , $index ));
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Overwrite posted data in the object for a single field . Key is not parsed .
* Public proxy of { @ link _setDataSimple ()}
2009-12-23 15:12:13 +00:00
* Use this method to store data from non - trustable sources ( e . g . _POST ) - it doesn ' t overwrite
2009-10-20 16:05:03 +00:00
* the original object data
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $key
* @ param mixed $value
* @ param boolean $strict update only
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
2010-02-12 16:42:28 +00:00
public function setPosted ( $key , $value , $strict = false )
2009-10-20 16:05:03 +00:00
{
return $this -> _setDataSimple ( $key , $value , $strict , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Overwrite posted data in the object . Key is parsed ( multidmensional array support ) .
* Public proxy of { @ link _setData ()}
2009-12-23 15:12:13 +00:00
* Use this method to store data from non - trustable sources ( e . g . _POST ) - it doesn ' t overwrite
2009-10-20 16:05:03 +00:00
* the original object data
*
* @ param string | array $key
* @ param mixed $value
* @ param boolean $strict update only
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
2010-02-12 16:42:28 +00:00
public function setPostedData ( $key , $value = null , $strict = false )
2009-10-20 16:05:03 +00:00
{
return $this -> _setData ( $key , $value , $strict , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Add data to the object .
* Retains existing data in the object .
* Public proxy of { @ link _addData ()}
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* If $override is false , data will be updated only ( check against existing data )
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string | array $key
* @ param mixed $value
* @ param boolean $override override existing data
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
2010-02-12 16:42:28 +00:00
public function addPostedData ( $key , $value = null , $override = true )
2009-10-20 16:05:03 +00:00
{
return $this -> _addData ( $key , $value , $override , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Unset single posted data field from the object .
* Public proxy of { @ link _unsetDataSimple ()}
*
* @ param string $key
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
public function removePosted ( $key )
{
return $this -> _unsetDataSimple ( $key , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Unset posted data from the object .
* $key can be a string only . Array will be ignored .
* '/' inside the key will be treated as array path
* if $key is null entire object will be reset
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* Public proxy of { @ link _unsetData ()}
*
* @ param string | null $key
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
public function removePostedData ( $key = null )
{
return $this -> _unsetData ( $key , '_posted_data' );
}
/**
* Check if given key exists and non - empty in the posted data array
* @ param string $key
* @ return boolean
*/
public function hasPosted ( $key )
{
return $this -> _hasData ( $key , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Check if posted data is empty
* @ return boolean
*/
public function hasPostedData ()
{
return $this -> _hasData ( '' , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Check if given key exists in the posted data array
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $key
* @ return boolean
*/
public function isPosted ( $key )
{
return ( isset ( $this -> _posted_data [ $key ]));
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Check if given key exists in the posted data array ( $key us parsed )
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $key
* @ return boolean
*/
public function isPostedData ( $key )
{
return $this -> _isData ( $key , '_posted_data' );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Compares posted data vs object data
*
* @ param string $field
* @ param boolean $strict compare variable type as well
* @ return boolean
*/
public function dataHasChangedFor ( $field , $strict = false )
{
$newData = $this -> getData ( $field );
$postedData = $this -> getPostedData ( $field );
return ( $strict ? $newData !== $postedData : $newData != $postedData );
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* @ return boolean
*/
public function dataHasChanged ()
{
return $this -> data_has_changed ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Merge posted data with the object data
* Should be used on edit / update / create record ( back - end )
* Retrieved for copy Posted data will be removed ( no matter if copy is successfull or not )
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* If $strict is true , only existing object data will be copied ( update )
* If $validate is true , data will be copied only after successful validation
*
2009-12-23 15:12:13 +00:00
* @ param boolean $strict
2009-10-20 16:05:03 +00:00
* @ param boolean $sanitize sanitize posted data before move it to the object data
* @ param boolean $validate perform validation check
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
public function mergePostedData ( $strict = true , $sanitize = true , $validate = true )
{
if ( ! $this -> hasPostedData () || ( $validate && ! $this -> validate ()))
{
2009-12-23 15:12:13 +00:00
return $this ;
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2014-01-17 16:46:24 -08:00
$oldData = $this -> getData ();
// $this->addMessageDebug("OLDD".print_a($oldData,true));
2014-02-25 11:43:28 +02:00
2014-01-17 16:46:24 -08:00
2009-10-20 16:05:03 +00:00
$data = $this -> getPostedData ();
2014-01-17 16:46:24 -08:00
2010-05-14 18:45:51 +00:00
$valid_data = $validate ? $this -> getValidator () -> getValidData () : array ();
2010-05-05 15:05:32 +00:00
2009-10-20 16:05:03 +00:00
if ( $sanitize )
{
// search for db_field types
2009-10-22 14:18:18 +00:00
if ( $this -> getDataFields ())
2009-10-20 16:05:03 +00:00
{
$data = $this -> sanitize ( $data );
}
else //no db field types, use toDB()
{
2010-02-12 16:42:28 +00:00
$data = e107 :: getParser () -> toDB ( $data );
2009-10-20 16:05:03 +00:00
}
}
2009-12-23 15:12:13 +00:00
2014-01-17 16:46:24 -08:00
// $newData = $this->getPostedData();
e107 :: getAdminLog () -> addArray ( $data , $oldData );
// $this->addMessageDebug("NEWD".print_a($data,true));
2010-12-27 12:38:14 +00:00
$tp = e107 :: getParser ();
2009-12-23 15:12:13 +00:00
foreach ( $data as $field => $dt )
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
// get values form validated array when possible
// we need it because of advanced validation methods e.g. 'compare'
2010-12-27 12:38:14 +00:00
// FIX - security issue, toDb required
if ( isset ( $valid_data [ $field ])) $dt = $tp -> toDb ( $valid_data [ $field ]);
2010-05-05 15:05:32 +00:00
2009-10-20 16:05:03 +00:00
$this -> setData ( $field , $dt , $strict )
-> removePostedData ( $field );
}
2014-02-25 11:43:28 +02:00
2009-10-20 16:05:03 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Merge passed data array with the object data
* Should be used on edit / update / create record ( back - end )
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* If $strict is true , only existing object data will be copied ( update )
* If $validate is true , data will be copied only after successful validation
*
* @ param array $src_data
* @ param boolean $sanitize
* @ param boolean $validate perform validation check
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-20 16:05:03 +00:00
*/
public function mergeData ( array $src_data , $strict = true , $sanitize = true , $validate = true )
{
//FIXME
if ( ! $src_data || ( $validate && ! $this -> validate ( $src_data )))
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/* Wrong ?
// retrieve only valid data
2009-12-23 15:12:13 +00:00
if ( $validate )
2009-10-20 16:05:03 +00:00
{
$src_data = $this -> getValidator () -> getValidData ();
} */
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
if ( $sanitize )
{
// search for db_field types
2009-10-22 14:18:18 +00:00
if ( $this -> getDataFields ())
2009-10-20 16:05:03 +00:00
{
$src_data = $this -> sanitize ( $src_data );
}
else //no db field types, use toDB()
{
2016-06-06 19:54:48 -07:00
$src_data = e107 :: getParser () -> toDB ( $src_data );
2009-10-20 16:05:03 +00:00
}
}
2009-12-23 15:12:13 +00:00
2014-01-17 16:46:24 -08:00
2009-10-20 16:05:03 +00:00
foreach ( $src_data as $key => $value )
{
$this -> setData ( $key , $value , $strict );
}
2014-02-25 11:43:28 +02:00
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Validate posted data :
* 1. validate posted data against object validation rules
* 2. add validation errors to the object if any
* 3. return true for valid and false for non - valid data
*
* @ param array $data optional - data for validation , defaults to posted data
* @ return boolean
*/
2009-11-04 17:29:26 +00:00
public function validate ( $data = null )
2009-10-20 16:05:03 +00:00
{
if ( ! $this -> getValidationRules ())
{
return true ;
}
2009-12-23 15:12:13 +00:00
if ( null === $data )
2009-10-20 16:05:03 +00:00
{
$data = $this -> getPostedData ();
}
2014-02-25 11:43:28 +02:00
2013-03-08 16:00:00 +02:00
// New param to control validate process - useful when part of the data is going to be updated
// Use it with cautious!!!
$availableOnly = false ;
2014-02-25 11:43:28 +02:00
if ( $this -> getParam ( 'validateAvailable' ))
2013-03-08 16:00:00 +02:00
{
$availableOnly = true ;
$this -> setParam ( 'validateAvailable' , null ); // reset it
}
2014-02-25 11:43:28 +02:00
2013-03-08 16:00:00 +02:00
return $this -> getValidator () -> validate ( $data , $availableOnly );
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-21 09:12:12 +00:00
/**
* User defined model validation
* Awaiting for child class implementation
*
*/
public function verify ()
{
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* @ return e_validator
*/
public function getValidator ()
{
if ( null === $this -> _validator )
{
2009-10-21 10:24:32 +00:00
$this -> _validator = e107 :: getObject ( 'e_validator' );
2010-05-05 15:05:32 +00:00
$this -> _validator -> setRules ( $this -> getValidationRules ())
-> setOptionalRules ( $this -> getOptionalRules ())
-> setMessageStack ( $this -> _message_stack . '_validator' );
2009-10-20 16:05:03 +00:00
//TODO - optional check rules
}
return $this -> _validator ;
}
/**
* Add custom validation message .
2013-10-29 18:41:02 -07:00
* $field_type and $error_code will be inserted via $tp -> lanVars ()
2009-10-20 16:05:03 +00:00
* in the $message string
2009-12-23 15:12:13 +00:00
* Example :
2009-10-20 16:05:03 +00:00
* < code >
2009-12-23 15:12:13 +00:00
* $model -> addValidationError ( 'Custom error message [#%d] for %s' , 'My Field' , 1000 );
2009-10-20 16:05:03 +00:00
* //produces 'Custom error message [#1000] for My Field'
* </ code >
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param string $message
* @ param string $field_title [ optional ]
* @ param integer $error_code [ optional ]
2009-12-13 21:52:32 +00:00
* @ return object
2009-10-20 16:05:03 +00:00
*/
2012-04-03 14:37:48 +00:00
public function addValidationError ( $message , $field_title = '' , $error_code = 0 )
2009-10-20 16:05:03 +00:00
{
2012-03-16 15:16:54 +00:00
$this -> getValidator () -> addValidateMessage ( $field_title , $error_code , $message ) -> setIsValidData ( false );
2009-10-20 16:05:03 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* Render validation errors ( if any )
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param boolean $session store messages to session
* @ param boolean $reset reset errors
* @ return string
*/
public function renderValidationErrors ( $session = false , $reset = true )
{
return $this -> getValidator () -> renderValidateMessages ( $session , $reset );
}
2009-12-23 15:12:13 +00:00
2009-10-21 09:12:12 +00:00
/**
* Render System messages ( if any )
2009-12-23 15:12:13 +00:00
*
2009-10-21 09:12:12 +00:00
* @ param boolean $validation render validation messages as well
* @ param boolean $session store messages to session
* @ param boolean $reset reset errors
* @ return string
*/
public function renderMessages ( $validation = true , $session = false , $reset = true )
{
if ( $validation )
{
e107 :: getMessage () -> moveStack ( $this -> _message_stack . '_validator' , $this -> _message_stack , false , $session );
}
return parent :: renderMessages ( $session , $reset );
}
2009-12-23 15:12:13 +00:00
2009-10-22 14:18:18 +00:00
/**
* Move model System messages ( if any ) to the default eMessage stack
2009-12-23 15:12:13 +00:00
*
2009-10-22 14:18:18 +00:00
* @ param boolean $session store messages to session
2009-11-05 17:32:19 +00:00
* @ param boolean $validation move validation messages as well
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-22 14:18:18 +00:00
*/
2009-11-05 17:32:19 +00:00
public function setMessages ( $session = false , $validation = true )
2009-10-22 14:18:18 +00:00
{
if ( $validation )
{
e107 :: getMessage () -> moveStack ( $this -> _message_stack . '_validator' , 'default' , false , $session );
}
parent :: setMessages ( $session );
return $this ;
}
2010-02-21 17:42:07 +00:00
2010-02-08 14:52:34 +00:00
/**
* Reset model System messages
2010-02-21 17:42:07 +00:00
*
2010-02-08 14:52:34 +00:00
* @ param boolean | string $type E_MESSAGE_INFO | E_MESSAGE_SUCCESS | E_MESSAGE_WARNING | E_MESSAGE_WARNING | E_MESSAGE_DEBUG | false ( all )
* @ param boolean $session reset session messages
* @ param boolean $validation reset validation messages as well
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2010-02-08 14:52:34 +00:00
*/
public function resetMessages ( $type = false , $session = false , $validation = false )
{
if ( $validation )
{
e107 :: getMessage () -> reset ( $type , $this -> _message_stack . '_validator' , $session );
}
parent :: resetMessages ( $type , $session );
return $this ;
}
2009-10-20 16:05:03 +00:00
/**
* @ return boolean
*/
public function hasValidationError ()
{
return $this -> getValidator () -> isValid ();
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
* @ return boolean
*/
public function hasSqlError ()
{
return ! empty ( $this -> _db_errno );
}
2009-12-23 15:12:13 +00:00
2009-11-17 15:23:01 +00:00
/**
* @ return integer last mysql error number
*/
public function getSqlErrorNumber ()
{
return $this -> _db_errno ;
}
2009-12-23 15:12:13 +00:00
2009-11-17 15:23:01 +00:00
/**
* @ return string last mysql error message
*/
public function getSqlError ()
{
2010-03-01 12:43:56 +00:00
return $this -> _db_errmsg ;
2009-11-17 15:23:01 +00:00
}
2009-12-23 15:12:13 +00:00
2014-02-03 14:04:13 +02:00
/**
* @ return string last mysql error message
*/
public function getSqlQuery ()
{
return $this -> _db_qry ;
}
2009-10-20 16:05:03 +00:00
/**
* @ return boolean
*/
public function hasError ()
{
2009-10-21 11:57:15 +00:00
return ( $this -> hasValidationError () || $this -> hasSqlError ());
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-30 17:59:32 +00:00
/**
* Generic load data from DB
* @ param boolean $force
2010-05-05 15:05:32 +00:00
* @ return e_front_model
2009-10-30 17:59:32 +00:00
*/
2016-02-14 19:00:12 -08:00
public function load ( $id = null , $force = false )
2009-10-30 17:59:32 +00:00
{
parent :: load ( $id , $force );
2009-12-23 15:12:13 +00:00
2010-03-01 13:19:54 +00:00
$sql = e107 :: getDb ();
$this -> _db_errno = $sql -> getLastErrorNumber ();
2010-03-01 12:43:56 +00:00
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-02-03 14:04:13 +02:00
$this -> _db_qry = $sql -> getLastQuery ();
2009-10-30 17:59:32 +00:00
if ( $this -> _db_errno )
{
2010-11-04 23:27:47 +00:00
$this -> addMessageError ( 'SQL Select Error' , $session_messages ); //TODO - Lan
2011-11-30 15:17:19 +00:00
// already done by the parent
//$this->addMessageDebug('SQL Error #'.$this->_db_errno.': '.$sql->getLastErrorText());
2009-10-30 17:59:32 +00:00
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
/**
* Build query array to be used with db methods ( db_Update , db_Insert , db_Replace )
*
* @ param string $force [ optional ] force action - possible values are create | update | replace
* @ return array db query
*/
public function toSqlQuery ( $force = '' )
{
$qry = array ();
if ( $force )
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
$action = $force ;
}
else
{
$action = $this -> getId () ? 'update' : 'create' ;
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
$qry [ '_FIELD_TYPES' ] = $this -> _FIELD_TYPES ; //DB field types are optional
2014-02-25 11:43:28 +02:00
2012-02-07 16:37:44 +00:00
// support for tables with no auto-increment PK
$id = $this -> getId ();
$qry [ 'data' ][ $this -> getFieldIdName ()] = $id ;
2014-02-25 11:43:28 +02:00
2015-02-20 11:03:14 -08:00
//XXX This check is done in _setModel() of admin-ui. NULL below will break MySQL strict.
// Allow admin config to specify the best data type.
/*
2012-02-07 16:37:44 +00:00
if ( $action == 'create' && ! $id ) $qry [ '_FIELD_TYPES' ][ $this -> getFieldIdName ()] = 'NULL' ;
elseif ( is_numeric ( $id )) $qry [ '_FIELD_TYPES' ][ $this -> getFieldIdName ()] = 'integer' ;
else $qry [ '_FIELD_TYPES' ][ $this -> getFieldIdName ()] = 'string' ;
2015-02-20 11:03:14 -08:00
*/
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
foreach ( $this -> _data_fields as $key => $type )
2009-10-20 16:05:03 +00:00
{
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
if ( ! isset ( $qry [ '_FIELD_TYPES' ][ $key ]))
{
$qry [ '_FIELD_TYPES' ][ $key ] = $type ; //_FIELD_TYPES much more optional now...
}
2014-02-25 11:43:28 +02:00
2012-12-17 16:57:24 +02:00
if ( $qry [ '_FIELD_TYPES' ][ $key ] == 'set' ) //new 'set' type, could be moved in mysql handler now
2012-08-02 02:09:58 +00:00
{
2014-02-25 11:43:28 +02:00
$qry [ '_FIELD_TYPES' ][ $key ] = 'str' ;
2012-12-17 16:57:24 +02:00
if ( is_array ( $this -> getData ( $key ))) $this -> setData ( $key , implode ( ',' , $this -> getData ( $key )));
2012-08-02 02:09:58 +00:00
}
2010-05-05 15:05:32 +00:00
$qry [ 'data' ][ $key ] = $this -> getData ( $key );
2014-02-25 11:43:28 +02:00
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
switch ( $action )
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
case 'create' :
2012-02-07 16:37:44 +00:00
//$qry['data'][$this->getFieldIdName()] = NULL;
2010-05-05 15:05:32 +00:00
break ;
case 'replace' :
$qry [ '_REPLACE' ] = true ;
break ;
case 'update' :
unset ( $qry [ 'data' ][ $this -> getFieldIdName ()]);
2012-02-07 16:37:44 +00:00
if ( is_numeric ( $id )) $id = intval ( $id );
else $id = " ' " . e107 :: getParser () -> toDB ( $id ) . " ' " ;
2014-02-25 11:43:28 +02:00
$qry [ 'WHERE' ] = $this -> getFieldIdName () . '=' . $id ;
2010-05-05 15:05:32 +00:00
break ;
2009-10-20 16:05:03 +00:00
}
2014-02-25 11:43:28 +02:00
2015-02-09 14:15:25 -08:00
if ( E107_DEBUG_LEVEL == E107_DBG_SQLQUERIES )
{
2015-03-01 12:43:02 -08:00
$this -> addMessageDebug ( 'SQL Qry: ' . print_a ( $qry , true ), null );
2015-02-09 14:15:25 -08:00
}
2010-05-05 15:05:32 +00:00
return $qry ;
}
2009-12-23 15:12:13 +00:00
2010-05-05 15:05:32 +00:00
/**
* Sanitize value based on its db field type ( $_data_fields ),
* method will return null only if db field rule is not found .
* If $value is null , it ' ll be retrieved from object posted data
* If $key is an array , $value is omitted .
*
* NOTE : If $key is not found in object ' s _data_fields array , null is returned
*
* @ param mixed $key string key name or array data to be sanitized
* @ param mixed $value
* @ return mixed sanitized $value or null on failure
*/
public function sanitize ( $key , $value = null )
2009-10-30 17:59:32 +00:00
{
2010-05-05 15:05:32 +00:00
$tp = e107 :: getParser ();
if ( is_array ( $key ))
2009-10-30 17:59:32 +00:00
{
2010-05-05 15:05:32 +00:00
$ret = array ();
foreach ( $key as $k => $v )
2009-10-30 17:59:32 +00:00
{
2010-05-05 15:05:32 +00:00
if ( isset ( $this -> _data_fields [ $k ]))
{
$ret [ $k ] = $this -> sanitize ( $k , $v );
}
2009-10-30 17:59:32 +00:00
}
2010-05-05 15:05:32 +00:00
return $ret ;
2009-10-30 17:59:32 +00:00
}
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
if ( ! isset ( $this -> _data_fields [ $key ]))
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
return null ;
2009-10-20 16:05:03 +00:00
}
2010-05-05 15:05:32 +00:00
$type = $this -> _data_fields [ $key ];
if ( null === $value )
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
$value = $this -> getPostedData ( $key );
2009-10-20 16:05:03 +00:00
}
2009-12-23 15:12:13 +00:00
2017-01-29 11:08:43 -08:00
2010-05-05 15:05:32 +00:00
switch ( $type )
{
case 'int' :
case 'integer' :
return intval ( $this -> toNumber ( $value ));
break ;
2016-06-06 19:54:48 -07:00
case 'safestr' :
return $tp -> filter ( $value );
break ;
2010-05-05 15:05:32 +00:00
case 'str' :
case 'string' :
2012-02-07 16:37:44 +00:00
case 'array' :
2017-01-29 11:08:43 -08:00
$type = $this -> getFieldInputType ( $key );
return $tp -> toDB ( $value , false , false , 'model' , array ( 'type' => $type , 'field' => $key ));
2010-05-05 15:05:32 +00:00
break ;
2017-01-18 17:10:12 -08:00
case 'json' :
2017-01-25 17:57:38 -08:00
if ( empty ( $value ))
{
return null ;
}
2017-01-18 17:10:12 -08:00
return e107 :: serialize ( $value , 'json' );
break ;
2016-11-04 17:49:22 -07:00
case 'code' :
return $tp -> toDB ( $value , false , false , 'pReFs' );
break ;
2010-05-05 15:05:32 +00:00
case 'float' :
return $this -> toNumber ( $value );
break ;
case 'bool' :
case 'boolean' :
return ( $value ? true : false );
break ;
case 'model' :
return $value -> mergePostedData ( false , true , true );
break ;
case 'null' :
return ( $value ? $tp -> toDB ( $value ) : null );
break ;
}
return null ;
}
public function destroy ()
{
parent :: destroy ();
$this -> _validator = null ;
$this -> _validation_rules = array ();
$this -> _db_errno = null ;
$this -> _posted_data = array ();
$this -> data_has_changed = array ();
$this -> _FIELD_TYPES = array ();
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
2010-05-05 15:05:32 +00:00
* Update DB data
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param boolean $force force query even if $data_has_changed is false
* @ param boolean $session_messages to use or not session to store system messages
*/
2010-05-05 15:05:32 +00:00
protected function dbUpdate ( $force = false , $session_messages = false )
2009-10-20 16:05:03 +00:00
{
2014-08-15 15:55:59 +03:00
$this -> _db_errno = 0 ;
2010-05-05 15:05:32 +00:00
$this -> _db_errmsg = '' ;
2014-02-03 14:04:13 +02:00
$this -> _db_qry = '' ;
2014-02-25 11:43:28 +02:00
2014-01-17 16:46:24 -08:00
// $this->getData();
// $this->getPostedData();
2014-02-25 11:43:28 +02:00
2012-03-19 17:20:24 +00:00
if ( $this -> hasError ()) return false ;
if ( ! $this -> data_has_changed && ! $force )
2009-10-20 16:05:03 +00:00
{
2010-05-05 15:05:32 +00:00
$this -> addMessageInfo ( LAN_NO_CHANGE );
2015-11-22 10:00:46 -08:00
2009-10-20 16:05:03 +00:00
return 0 ;
}
2010-03-01 12:43:56 +00:00
$sql = e107 :: getDb ();
2014-01-17 17:21:42 -08:00
$qry = $this -> toSqlQuery ( 'update' );
$table = $this -> getModelTable ();
2014-02-25 11:43:28 +02:00
2014-01-17 17:21:42 -08:00
$res = $sql -> db_Update ( $table , $qry , $this -> getParam ( 'db_debug' , false ));
2014-08-15 15:55:59 +03:00
$this -> _db_qry = $sql -> getLastQuery ();
2009-10-20 16:05:03 +00:00
if ( ! $res )
{
2010-03-01 12:43:56 +00:00
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-08-15 15:55:59 +03:00
2009-10-20 16:05:03 +00:00
if ( $this -> _db_errno )
{
2010-05-05 15:05:32 +00:00
$this -> addMessageError ( 'SQL Update Error' , $session_messages ); //TODO - Lan
2010-03-01 13:19:54 +00:00
$this -> addMessageDebug ( 'SQL Error #' . $this -> _db_errno . ': ' . $sql -> getLastErrorText ());
2010-05-05 15:05:32 +00:00
return false ;
}
$this -> addMessageInfo ( LAN_NO_CHANGE );
return 0 ;
}
2010-11-02 10:31:05 +00:00
$this -> clearCache () -> addMessageSuccess ( LAN_UPDATED );
2014-02-25 11:43:28 +02:00
2014-01-17 17:21:42 -08:00
e107 :: getAdminLog () -> addSuccess ( 'TABLE: ' . $table , false );
e107 :: getAdminLog () -> addSuccess ( 'WHERE: ' . $qry [ 'WHERE' ], false );
2014-01-17 16:46:24 -08:00
e107 :: getAdminLog () -> save ( 'ADMINUI_02' );
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
return $res ;
}
/**
* Save data to DB
*
* @ param boolen $from_post
* @ return boolean | integer
*/
public function save ( $from_post = true , $force = false , $session_messages = false )
{
if ( ! $this -> getFieldIdName ())
{
return false ;
}
if ( $from_post )
{
2014-02-25 11:43:28 +02:00
//no strict copy, validate & sanitize
2010-05-05 15:05:32 +00:00
$this -> mergePostedData ( false , true , true );
}
if ( $this -> getId ())
{
return $this -> dbUpdate ( $force , $session_messages );
}
return false ;
}
2014-02-25 11:43:28 +02:00
2012-02-07 16:37:44 +00:00
/**
* Update record
*
* @ param boolen $from_post
* @ return boolean | integer
*/
public function update ( $from_post = true , $force = false , $session_messages = false )
{
if ( ! $this -> getFieldIdName ())
{
return false ;
}
if ( $from_post )
{
//no strict copy, validate & sanitize
$this -> mergePostedData ( false , true , true );
}
return $this -> dbUpdate ( $force , $session_messages );
}
2010-05-05 15:05:32 +00:00
/**
* Exactly what it says - your debug helper
* @ param boolean $retrun
* @ param boolean $undo
* @ return void
*/
public function saveDebug ( $return = false , $undo = true )
{
$ret = array ();
$ret [ 'validation_rules' ] = $this -> getValidationRules ();
$ret [ 'optional_validation_rules' ] = $this -> getOptionalRules ();
$ret [ 'model_base_ismodfied' ] = $this -> isModified ();
$ret [ 'model_base_data' ] = $this -> getData ();
$ret [ 'posted_data' ] = $this -> getPostedData ();
$this -> mergePostedData ( false , true , true );
$ret [ 'model_modified_data' ] = $this -> getData ();
$ret [ 'model_modified_ismodfied' ] = $this -> isModified ();
$ret [ 'validator_valid_data' ] = $this -> getValidator () -> getValidData ();
// undo
if ( $undo )
{
$this -> setData ( $ret [ 'model_base_data' ])
-> isModified ( $ret [ 'model_base_ismodfied' ])
-> setPostedData ( $ret [ 'posted_data' ]);
}
if ( $return ) return $ret ;
print_a ( $ret );
}
}
//FIXME - move e_model_admin to e_model_admin.php
/**
* Base e107 Admin Model class
*
* @ package e107
* @ category e107_handlers
* @ version $Id $
* @ author SecretR
* @ copyright Copyright ( C ) 2008 - 2010 e107 Inc .
*/
class e_admin_model extends e_front_model
{
/**
* Save data to DB
*
* @ param boolen $from_post
*/
public function save ( $from_post = true , $force = false , $session_messages = false )
{
if ( ! $this -> getFieldIdName ())
{
return false ;
}
if ( $from_post )
{
//no strict copy, validate & sanitize
$this -> mergePostedData ( false , true , true );
}
2016-05-12 19:34:57 -07:00
if ( $this -> getId () && $this -> getPostedData ( 'etrigger_submit' ) != 'create' ) // Additional Check to allow primary ID to be manually set when auto-increment PID is not used. @see userclass2.php
2010-05-05 15:05:32 +00:00
{
return $this -> dbUpdate ( $force , $session_messages );
}
2010-11-15 09:00:17 +00:00
return $this -> dbInsert ( $session_messages );
2010-05-05 15:05:32 +00:00
}
2014-02-25 11:43:28 +02:00
2012-02-07 16:37:44 +00:00
/**
* Insert record
*
* @ param boolen $from_post
* @ param boolean $session_messages
* @ return integer inserted ID or false on error
*/
public function insert ( $from_post = true , $session_messages = false )
{
if ( $from_post )
{
//no strict copy, validate & sanitize
$this -> mergePostedData ( false , true , true );
}
return $this -> dbInsert ( $session_messages );
}
2010-05-05 15:05:32 +00:00
2016-06-09 16:43:36 -07:00
public function delete ( $ids , $destroy = true , $session_messages = false )
2010-05-05 15:05:32 +00:00
{
$ret = $this -> dbDelete ();
if ( $ret )
{
if ( $destroy )
{
$this -> setMessages ( $session_messages ) -> destroy ();
2009-10-20 16:05:03 +00:00
}
}
2010-05-05 15:05:32 +00:00
return $ret ;
}
/**
* Insert data to DB
* @ param boolean $session_messages to use or not session to store system messages
2010-11-15 09:00:17 +00:00
* @ return integer
2010-05-05 15:05:32 +00:00
*/
2010-11-15 09:00:17 +00:00
protected function dbInsert ( $session_messages = false )
2010-05-05 15:05:32 +00:00
{
2014-08-15 15:55:59 +03:00
$this -> _db_errno = 0 ;
$this -> _db_errmsg = '' ;
2014-02-03 14:04:13 +02:00
$this -> _db_qry = '' ;
2010-11-15 09:00:17 +00:00
if ( $this -> hasError () /* || (!$this->data_has_changed && !$force)*/ ) // not appropriate here!
2010-05-05 15:05:32 +00:00
{
2012-02-23 08:56:33 +00:00
return false ;
2010-05-05 15:05:32 +00:00
}
$sql = e107 :: getDb ();
2014-01-17 16:46:24 -08:00
$sqlQry = $this -> toSqlQuery ( 'create' );
$table = $this -> getModelTable ();
2014-02-25 11:43:28 +02:00
2015-04-14 11:59:46 -07:00
$res = $sql -> insert ( $table , $sqlQry , $this -> getParam ( 'db_debug' , false ));
2014-08-15 15:55:59 +03:00
$this -> _db_qry = $sql -> getLastQuery ();
2010-05-05 15:05:32 +00:00
if ( ! $res )
{
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-08-15 15:55:59 +03:00
2010-05-05 15:05:32 +00:00
$this -> addMessageError ( 'SQL Insert Error' , $session_messages ); //TODO - Lan
2016-05-29 11:00:37 -07:00
$this -> addMessageDebug ( 'SQL Error #' . $this -> _db_errno . ': ' . $this -> _db_errmsg );
$this -> addMessageDebug ( 'SQL QRY Error ' . print_a ( $sqlQry , true ));
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
return false ;
}
2015-04-14 11:59:46 -07:00
e107 :: getAdminLog () -> addSuccess ( 'TABLE: ' . $table , false );
2014-01-17 16:46:24 -08:00
e107 :: getAdminLog () -> save ( 'ADMINUI_01' );
// e107::getAdminLog()->clear()->addSuccess($table,false)->addArray($sqlQry)->save('ADMINUI_01');
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
// Set the reutrned ID
$this -> setId ( $res );
2010-11-02 10:31:05 +00:00
$this -> clearCache () -> addMessageSuccess ( LAN_CREATED );
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
return $res ;
}
2009-12-23 15:12:13 +00:00
2009-10-20 16:05:03 +00:00
/**
2010-05-05 15:05:32 +00:00
* Replace data in DB
2009-12-23 15:12:13 +00:00
*
2009-10-20 16:05:03 +00:00
* @ param boolean $force force query even if $data_has_changed is false
* @ param boolean $session_messages to use or not session to store system messages
*/
2010-05-05 15:05:32 +00:00
protected function dbReplace ( $force = false , $session_messages = false )
2009-10-20 16:05:03 +00:00
{
2014-08-15 15:55:59 +03:00
$this -> _db_errno = 0 ;
$this -> _db_errmsg = '' ;
2014-02-03 14:04:13 +02:00
$this -> _db_qry = '' ;
2014-02-25 11:43:28 +02:00
2012-03-19 17:20:24 +00:00
if ( $this -> hasError ()) return false ;
if ( ! $this -> data_has_changed && ! $force )
2009-10-30 17:59:32 +00:00
{
return 0 ;
}
2010-03-01 12:43:56 +00:00
$sql = e107 :: getDb ();
2010-05-05 15:05:32 +00:00
$res = $sql -> db_Insert ( $this -> getModelTable (), $this -> toSqlQuery ( 'replace' ));
2014-08-15 15:55:59 +03:00
$this -> _db_qry = $sql -> getLastQuery ();
2009-10-20 16:05:03 +00:00
if ( ! $res )
{
2010-03-01 12:43:56 +00:00
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-08-15 15:55:59 +03:00
2009-10-20 16:05:03 +00:00
if ( $this -> _db_errno )
{
2010-05-05 15:05:32 +00:00
$this -> addMessageError ( 'SQL Replace Error' , $session_messages ); //TODO - Lan
2010-03-01 13:19:54 +00:00
$this -> addMessageDebug ( 'SQL Error #' . $this -> _db_errno . ': ' . $sql -> getLastErrorText ());
2009-10-20 16:05:03 +00:00
}
}
2010-11-02 10:31:05 +00:00
else
{
$this -> clearCache ();
}
2009-10-28 17:05:35 +00:00
return $res ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Delete DB data
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ param boolean $force force query even if $data_has_changed is false
* @ param boolean $session_messages to use or not session to store system messages
*/
2010-05-05 15:05:32 +00:00
protected function dbDelete ( $session_messages = false )
2009-10-28 17:05:35 +00:00
{
2014-08-15 15:55:59 +03:00
$this -> _db_errno = 0 ;
2010-03-01 12:43:56 +00:00
$this -> _db_errmsg = '' ;
2014-02-03 14:04:13 +02:00
$this -> _db_qry = '' ;
2014-02-25 11:43:28 +02:00
2009-10-30 17:59:32 +00:00
if ( $this -> hasError ())
{
2012-03-20 11:17:08 +00:00
return false ;
2009-10-30 17:59:32 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
if ( ! $this -> getId ())
{
$this -> addMessageError ( 'Record not found' , $session_messages ); //TODO - Lan
return 0 ;
}
2010-03-01 12:43:56 +00:00
$sql = e107 :: getDb ();
2012-02-07 16:37:44 +00:00
$id = $this -> getId ();
if ( is_numeric ( $id )) $id = intval ( $id );
else $id = " ' " . e107 :: getParser () -> toDB ( $id ) . " ' " ;
2014-01-17 16:46:24 -08:00
$table = $this -> getModelTable ();
2016-03-16 13:53:57 -07:00
$where = $this -> getFieldIdName () . '=' . $id ;
$res = $sql -> delete ( $table , $where );
2014-08-15 15:55:59 +03:00
$this -> _db_qry = $sql -> getLastQuery ();
2009-10-28 17:05:35 +00:00
if ( ! $res )
{
2010-03-01 12:43:56 +00:00
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-08-15 15:55:59 +03:00
2009-10-28 17:05:35 +00:00
if ( $this -> _db_errno )
{
$this -> addMessageError ( 'SQL Delete Error' , $session_messages ); //TODO - Lan
2010-03-01 13:19:54 +00:00
$this -> addMessageDebug ( 'SQL Error #' . $this -> _db_errno . ': ' . $sql -> getLastErrorText ());
2009-10-28 17:05:35 +00:00
}
}
2010-11-02 10:31:05 +00:00
else
{
2016-03-16 13:53:57 -07:00
if ( $table != 'admin_log' )
{
$logData = array ( 'TABLE' => $table , 'WHERE' => $where );
e107 :: getAdminLog () -> addSuccess ( $table , false );
e107 :: getAdminLog () -> addArray ( $logData ) -> save ( 'ADMINUI_03' );
}
2010-11-02 10:31:05 +00:00
$this -> clearCache ();
}
2009-10-28 17:05:35 +00:00
return $res ;
2009-10-20 16:05:03 +00:00
}
}
2009-10-22 04:15:32 +00:00
2009-10-28 17:05:35 +00:00
/**
* Model collection handler
*/
2010-05-05 15:05:32 +00:00
class e_tree_model extends e_front_model
2009-10-28 17:05:35 +00:00
{
/**
* Current model DB table , used in all db calls
* This can / should be overwritten by extending the class
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ var string
*/
protected $_db_table ;
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* All records ( no limit ) cache
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ var string
*/
2009-11-17 15:34:54 +00:00
protected $_total = false ;
2009-12-23 15:12:13 +00:00
2009-10-30 17:59:32 +00:00
/**
* Constructor
*
*/
function __construct ( $tree_data = array ())
{
if ( $tree_data )
{
$this -> setTree ( $tree_data );
}
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
public function getTotal ()
{
2009-12-23 15:12:13 +00:00
return $this -> _total ;
2009-10-28 17:05:35 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
public function setTotal ( $num )
{
$this -> _total = $num ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Set table name
* @ param object $table
* @ return e_admin_tree_model
*/
public function setModelTable ( $table )
{
$this -> _db_table = $table ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Get table name
* @ return string
*/
public function getModelTable ()
{
return $this -> _db_table ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
2009-12-23 15:12:13 +00:00
* Get array of models
2009-10-28 17:05:35 +00:00
* @ return array
*/
2009-12-23 15:12:13 +00:00
function getTree ()
2009-10-28 17:05:35 +00:00
{
2009-11-18 14:46:28 +00:00
return $this -> get ( '__tree' , array ());
2009-10-28 17:05:35 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Set array of models
* @ return e_tree_model
*/
function setTree ( $tree_data , $force = false )
{
2010-02-21 17:42:07 +00:00
if ( $force || ! $this -> isTree ())
2009-10-28 17:05:35 +00:00
{
$this -> set ( '__tree' , $tree_data );
}
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* Unset all current data
* @ return e_tree_model
*/
function unsetTree ()
{
$this -> remove ( '__tree' );
return $this ;
}
2010-05-05 15:05:32 +00:00
2016-02-14 19:00:12 -08:00
public function isCacheEnabled ( $checkId = true )
2010-04-28 15:44:46 +00:00
{
return ( null !== $this -> getCacheString ());
}
2010-05-05 15:05:32 +00:00
2016-02-14 19:00:12 -08:00
public function getCacheString ( $replace = false )
2010-04-28 15:44:46 +00:00
{
return $this -> _cache_string ;
}
2010-05-05 15:05:32 +00:00
2010-05-02 13:34:05 +00:00
protected function _setCacheData ()
{
if ( ! $this -> isCacheEnabled ())
{
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-05-02 13:34:05 +00:00
e107 :: getCache () -> set_sys (
2010-05-05 15:05:32 +00:00
$this -> getCacheString ( true ),
$this -> toString ( false , null , $this -> getParam ( 'nocount' ) ? false : true ),
$this -> _cache_force ,
2010-05-02 13:34:05 +00:00
false
);
return $this ;
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
protected function _loadFromArray ( $array )
{
2010-05-05 15:05:32 +00:00
if ( isset ( $array [ 'total' ]))
2010-05-02 13:34:05 +00:00
{
$this -> setTotal (( integer ) $array [ 'total' ]);
unset ( $array [ 'total' ]);
}
2010-04-28 15:44:46 +00:00
$class_name = $this -> getParam ( 'model_class' , 'e_model' );
$tree = array ();
2010-05-05 15:05:32 +00:00
foreach ( $array as $id => $data )
2010-04-28 15:44:46 +00:00
{
$tree [ $id ] = new $class_name ( $data );
2010-05-02 13:34:05 +00:00
$this -> _onLoad ( $tree [ $id ]);
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
$this -> setTree ( $tree , true );
}
2010-05-05 15:05:32 +00:00
2010-05-02 12:46:59 +00:00
/**
* Additional on load logic to be set from subclasses
2010-05-05 15:05:32 +00:00
*
2010-05-02 12:46:59 +00:00
* @ param e_model $node
* @ return e_tree_model
*/
protected function _onLoad ( $node )
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Default load method
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ return e_tree_model
*/
2009-10-30 17:59:32 +00:00
public function load ( $force = false )
2009-10-28 17:05:35 +00:00
{
2010-02-21 17:42:07 +00:00
// XXX What would break if changed to the most proper isTree()?
if ( ! $force && $this -> isTree ()) //!$this->isEmpty()
2009-10-30 17:59:32 +00:00
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-17 15:34:54 +00:00
if ( $force )
{
2010-04-28 15:44:46 +00:00
$this -> unsetTree ()
-> _clearCacheData ();
2010-05-05 15:05:32 +00:00
2009-11-17 15:34:54 +00:00
$this -> _total = false ;
}
2014-02-25 11:43:28 +02:00
2010-11-02 10:31:05 +00:00
if ( $this -> isCacheEnabled () && ! $this -> getParam ( 'noCacheStringModify' ))
{
2014-02-25 11:43:28 +02:00
$str = ! $this -> getParam ( 'db_query' )
?
2010-11-02 10:31:05 +00:00
$this -> getModelTable ()
. $this -> getParam ( 'nocount' )
. $this -> getParam ( 'db_where' )
. $this -> getParam ( 'db_order' )
. $this -> getParam ( 'db_limit' )
:
$this -> getParam ( 'db_query' );
2014-02-25 11:43:28 +02:00
2010-11-02 10:31:05 +00:00
$this -> setCacheString ( $this -> getCacheString () . '_' . md5 ( $str ));
}
2010-05-05 15:05:32 +00:00
$cached = $this -> _getCacheData ();
2010-04-28 15:44:46 +00:00
if ( $cached !== false )
{
$this -> _loadFromArray ( $cached );
return $this ;
}
2014-02-25 11:43:28 +02:00
2010-04-28 15:44:46 +00:00
$class_name = $this -> getParam ( 'model_class' , 'e_model' );
// auto-load all
if ( ! $this -> getParam ( 'db_query' ) && $this -> getModelTable ())
{
2012-02-07 16:37:44 +00:00
$this -> setParam ( 'db_query' , 'SELECT' . ( ! $this -> getParam ( 'nocount' ) ? ' SQL_CALC_FOUND_ROWS' : '' )
. ( $this -> getParam ( 'db_cols' ) ? ' ' . $this -> getParam ( 'db_cols' ) : ' *' ) . ' FROM #' . $this -> getModelTable ()
. ( $this -> getParam ( 'db_joins' ) ? ' ' . $this -> getParam ( 'db_joins' ) : '' )
2010-11-02 10:31:05 +00:00
. ( $this -> getParam ( 'db_where' ) ? ' WHERE ' . $this -> getParam ( 'db_where' ) : '' )
2010-05-14 18:45:51 +00:00
. ( $this -> getParam ( 'db_order' ) ? ' ORDER BY ' . $this -> getParam ( 'db_order' ) : '' )
. ( $this -> getParam ( 'db_limit' ) ? ' LIMIT ' . $this -> getParam ( 'db_limit' ) : '' )
);
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2010-05-02 14:04:41 +00:00
if ( $this -> getParam ( 'db_query' ) && $class_name && class_exists ( $class_name ))
2009-10-28 17:05:35 +00:00
{
2011-01-20 11:29:51 +00:00
$sql = e107 :: getDb ( $this -> getParam ( 'model_class' , 'e_model' ));
2009-11-02 17:45:29 +00:00
$this -> _total = $sql -> total_results = false ;
2010-05-05 15:05:32 +00:00
2016-02-15 00:56:08 -08:00
if ( $sql -> gen ( $this -> getParam ( 'db_query' ), $this -> getParam ( 'db_debug' ) ? true : false ))
2009-10-28 17:05:35 +00:00
{
2009-11-17 15:34:54 +00:00
$this -> _total = is_integer ( $sql -> total_results ) ? $sql -> total_results : false ; //requires SQL_CALC_FOUND_ROWS in query - see db handler
2009-10-28 17:05:35 +00:00
while ( $tmp = $sql -> db_Fetch ())
{
$tmp = new $class_name ( $tmp );
2009-11-28 15:34:46 +00:00
if ( $this -> getParam ( 'model_message_stack' ))
{
$tmp -> setMessageStackName ( $this -> getParam ( 'model_message_stack' ));
}
2010-05-02 12:46:59 +00:00
$this -> _onLoad ( $tmp ) -> setNode ( $tmp -> get ( $this -> getFieldIdName ()), $tmp );
2009-10-28 17:05:35 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( false === $this -> _total && $this -> getModelTable () && ! $this -> getParam ( 'nocount' ))
2009-10-30 17:59:32 +00:00
{
//SQL_CALC_FOUND_ROWS not found in the query, do one more query
2013-11-08 22:36:42 -08:00
// $this->_total = e107::getDb()->db_Count($this->getModelTable()); // fails with specific listQry
2014-02-25 11:43:28 +02:00
// Calculates correct total when using filters and search. //XXX Optimize.
2013-11-08 22:36:42 -08:00
$countQry = preg_replace ( '/(LIMIT ([\d,\s])*)$/' , " " , $this -> getParam ( 'db_query' ));
2014-02-25 11:43:28 +02:00
2013-11-08 22:36:42 -08:00
$this -> _total = e107 :: getDb () -> gen ( $countQry );
2014-02-25 11:43:28 +02:00
2009-10-30 17:59:32 +00:00
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
unset ( $tmp );
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
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 ();
}
2010-05-05 15:05:32 +00:00
2009-10-28 17:05:35 +00:00
}
return $this ;
}
/**
* Get single model instance from the collection
* @ param integer $node_id
* @ return e_model
*/
function getNode ( $node_id )
{
return $this -> getData ( '__tree/' . $node_id );
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Add or remove ( when $node is null ) model to the collection
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ param integer $node_id
* @ param e_model $node
* @ return e_tree_model
*/
function setNode ( $node_id , $node )
{
if ( null === $node )
{
$this -> removeData ( '__tree/' . $node_id );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
$this -> setData ( '__tree/' . $node_id , $node );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Check if model with passed id exists in the collection
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ param integer $node_id
* @ return boolean
*/
public function isNode ( $node_id )
{
return $this -> isData ( '__tree/' . $node_id );
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Check if model with passed id exists in the collection and is not empty
2009-12-23 15:12:13 +00:00
*
2009-10-28 17:05:35 +00:00
* @ param integer $node_id
* @ return boolean
*/
public function hasNode ( $node_id )
{
return $this -> hasData ( '__tree/' . $node_id );
}
2009-12-23 15:12:13 +00:00
2009-10-28 17:05:35 +00:00
/**
* Check if collection is empty
*
* @ return boolean
*/
function isEmpty ()
{
2009-10-30 17:59:32 +00:00
return ( ! $this -> has ( '__tree' ));
2009-10-28 17:05:35 +00:00
}
2010-02-21 17:42:07 +00:00
/**
* Check if collection is loaded ( not null )
*
* @ return boolean
*/
function isTree ()
{
return $this -> is ( '__tree' );
}
/**
* Same as isEmpty (), but with opposite boolean logic
*
* @ return boolean
*/
function hasTree ()
{
return $this -> has ( '__tree' );
}
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
/**
* 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 = '' ;
2015-02-15 02:37:36 -08:00
$i = 1 ;
2010-05-05 15:05:32 +00:00
foreach ( $this -> getTree () as $model )
2010-04-28 15:44:46 +00:00
{
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
2010-05-02 13:34:05 +00:00
* @ param boolean $total include total results property
2010-04-28 15:44:46 +00:00
* @ return array object data
*/
2010-05-02 13:34:05 +00:00
public function toArray ( $total = false )
2010-04-28 15:44:46 +00:00
{
$ret = array ();
2010-05-05 15:05:32 +00:00
foreach ( $this -> getTree () as $id => $model )
2010-04-28 15:44:46 +00:00
{
$ret [ $id ] = $model -> toArray ();
}
2010-05-02 13:34:05 +00:00
if ( $total ) $ret [ 'total' ] = $this -> getTotal ();
2010-05-05 15:05:32 +00:00
2010-04-28 15:44:46 +00:00
return $ret ;
}
/**
* Convert object data to a string
*
* @ param boolean $AddSlashes
2010-05-02 13:34:05 +00:00
* @ param string $node_id optional , if set method will return corresponding value as a string
* @ param boolean $total include total results property
2010-04-28 15:44:46 +00:00
* @ return string
*/
2010-05-02 13:34:05 +00:00
public function toString ( $AddSlashes = true , $node_id = null , $total = false )
2010-04-28 15:44:46 +00:00
{
if ( null !== $node_id && $this -> isNode ( $node_id ))
{
return $this -> getNode ( $node_id ) -> toString ( $AddSlashes );
}
2010-05-02 13:34:05 +00:00
return ( string ) e107 :: getArrayStorage () -> WriteArray ( $this -> toArray ( $total ), $AddSlashes );
2010-04-28 15:44:46 +00:00
}
2010-05-05 15:05:32 +00:00
2016-02-14 19:00:12 -08:00
public function update ( $from_post = true , $force = false , $session_messages = false )
2010-05-05 15:05:32 +00:00
{
}
2016-02-14 19:00:12 -08:00
public function delete ( $ids , $destroy = true , $session_messages = false )
2010-05-05 15:05:32 +00:00
{
}
2009-10-28 17:05:35 +00:00
}
2010-05-05 15:05:32 +00:00
class e_front_tree_model extends e_tree_model
2009-10-28 17:05:35 +00:00
{
2010-03-01 12:43:56 +00:00
/**
* @ var integer Last SQL error number
*/
protected $_db_errno = 0 ;
/**
* @ var string Last SQL error message
*/
protected $_db_errmsg = '' ;
2014-02-03 14:04:13 +02:00
/**
* @ var string Last SQL query
*/
protected $_db_qry = '' ;
2010-03-01 12:43:56 +00:00
/**
* @ return boolean
*/
public function hasSqlError ()
{
return ! empty ( $this -> _db_errno );
}
/**
* @ return integer last mysql error number
*/
public function getSqlErrorNumber ()
{
return $this -> _db_errno ;
}
/**
* @ return string last mysql error message
*/
public function getSqlError ()
{
return $this -> _db_errmsg ;
}
2014-02-03 14:04:13 +02:00
/**
* @ return string last mysql error message
*/
public function getSqlQuery ()
{
return $this -> _db_qry ;
}
2010-03-01 12:43:56 +00:00
/**
* @ return boolean
*/
public function hasError ()
{
return $this -> hasSqlError ();
}
2009-11-05 17:32:19 +00:00
/**
* Batch update tree records / nodes
* @ param string $field field name
* @ param string $value
* @ param string | array $ids numerical array or string comma separated ids
* @ param mixed $syncvalue value to be used for model data synchronization ( db value could be something like '1-field_name' ), null - no sync
* @ param boolean $sanitize [ optional ] default true
* @ param boolean $session_messages [ optional ] default false
* @ return integer updated count or false on error
*/
public function update ( $field , $value , $ids , $syncvalue = null , $sanitize = true , $session_messages = false )
{
$tp = e107 :: getParser ();
2009-12-23 15:12:13 +00:00
$sql = e107 :: getDb ();
2009-11-05 17:32:19 +00:00
if ( empty ( $ids ))
{
return 0 ;
}
if ( ! is_array ( $ids ))
{
2009-11-06 12:27:41 +00:00
$ids = explode ( ',' , $ids );
2009-11-05 17:32:19 +00:00
}
2009-12-23 15:12:13 +00:00
2012-12-17 16:57:24 +02:00
if ( true === $syncvalue )
{
$syncvalue = $value ;
}
2014-02-25 11:43:28 +02:00
2009-11-05 17:32:19 +00:00
if ( $sanitize )
{
2012-02-07 16:37:44 +00:00
$ids = array_map ( array ( $tp , 'toDB' ), $ids );
2009-11-05 17:32:19 +00:00
$field = $tp -> toDb ( $field );
2012-02-07 16:37:44 +00:00
$value = " ' " . $tp -> toDB ( $value ) . " ' " ;
2009-11-05 17:32:19 +00:00
}
$idstr = implode ( ', ' , $ids );
2009-12-23 15:12:13 +00:00
2013-10-16 18:13:21 +03:00
$res = $sql -> db_Update ( $this -> getModelTable (), " { $field } = { $value } WHERE " . $this -> getFieldIdName () . ' IN (' . $idstr . ')' , $this -> getParam ( 'db_debug' , false ));
2010-03-01 12:43:56 +00:00
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-02-03 14:04:13 +02:00
$this -> _db_qry = $sql -> getLastQuery ();
2014-02-25 11:43:28 +02:00
2009-11-05 17:32:19 +00:00
if ( ! $res )
2009-12-23 15:12:13 +00:00
{
2009-11-05 17:32:19 +00:00
if ( $sql -> getLastErrorNumber ())
{
2009-12-23 15:12:13 +00:00
$this -> addMessageError ( LAN_UPDATED_FAILED , $session_messages );
2009-11-05 17:32:19 +00:00
$this -> addMessageDebug ( 'SQL Error #' . $sql -> getLastErrorNumber () . ': ' . $sql -> getLastErrorText ());
}
else
{
2009-12-23 15:12:13 +00:00
$this -> addMessageInfo ( LAN_NO_CHANGE , $session_messages );
2009-11-05 17:32:19 +00:00
}
}
2010-11-02 10:31:05 +00:00
else
{
$this -> clearCache ();
}
2009-12-23 15:12:13 +00:00
2010-11-02 10:31:05 +00:00
$modelCacheCheck = $this -> getParam ( 'clearModelCache' );
if ( null === $syncvalue && ! $modelCacheCheck ) return $res ;
2009-12-23 15:12:13 +00:00
2009-11-05 17:32:19 +00:00
foreach ( $ids as $id )
{
2010-11-02 10:31:05 +00:00
$node = $this -> getNode ( $id );
if ( ! $node ) continue ;
2014-02-25 11:43:28 +02:00
2010-11-02 10:31:05 +00:00
if ( null !== $syncvalue )
2009-11-05 17:32:19 +00:00
{
2010-11-02 10:31:05 +00:00
$node -> set ( $field , $syncvalue )
2009-11-05 17:32:19 +00:00
-> setMessages ( $session_messages );
}
2010-11-02 10:31:05 +00:00
if ( $modelCacheCheck ) $this -> clearCache ();
2009-11-05 17:32:19 +00:00
}
2010-05-05 15:05:32 +00:00
return $res ;
}
}
class e_admin_tree_model extends e_front_tree_model
{
/**
* Batch Delete records
* @ param mixed $ids
* @ param boolean $destroy [ optional ] destroy object instance after db delete
* @ param boolean $session_messages [ optional ]
* @ return integer deleted records number or false on DB error
*/
public function delete ( $ids , $destroy = true , $session_messages = false )
{
if ( ! $ids ) return 0 ;
if ( ! is_array ( $ids ))
{
$ids = explode ( ',' , $ids );
}
2012-02-07 16:37:44 +00:00
$tp = e107 :: getParser ();
$ids = array_map ( array ( $tp , 'toDB' ), $ids );
2010-05-05 15:05:32 +00:00
$idstr = implode ( ', ' , $ids );
$sql = e107 :: getDb ();
2014-01-17 16:46:24 -08:00
$table = $this -> getModelTable ();
$sqlQry = $this -> getFieldIdName () . ' IN (\'' . $idstr . '\')' ;
2014-02-25 11:43:28 +02:00
2016-03-16 13:53:57 -07:00
$res = $sql -> delete ( $table , $sqlQry );
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-02-03 14:04:13 +02:00
$this -> _db_qry = $sql -> getLastQuery ();
2014-02-25 11:43:28 +02:00
2010-11-02 10:31:05 +00:00
$modelCacheCheck = $this -> getParam ( 'clearModelCache' );
2014-02-25 11:43:28 +02:00
2010-05-05 15:05:32 +00:00
if ( ! $res )
{
if ( $sql -> getLastErrorNumber ())
{
2013-05-09 22:48:27 -05:00
$this -> addMessageError ( 'SQL Delete Error: ' . $sql -> getLastQuery (), $session_messages ); //TODO - Lan
2010-05-05 15:05:32 +00:00
$this -> addMessageDebug ( 'SQL Error #' . $sql -> getLastErrorNumber () . ': ' . $sql -> getLastErrorText ());
}
}
2010-11-02 10:31:05 +00:00
elseif ( $destroy || $modelCacheCheck )
2010-05-05 15:05:32 +00:00
{
foreach ( $ids as $id )
{
if ( $this -> hasNode ( $id ))
{
2010-11-02 10:31:05 +00:00
$this -> getNode ( $id ) -> clearCache () -> setMessages ( $session_messages );
if ( $destroy )
{
call_user_func ( array ( $this -> getNode ( trim ( $id )), 'destroy' )); // first call model destroy method if any
$this -> setNode ( $id , null );
}
2010-05-05 15:05:32 +00:00
}
}
}
2016-03-16 13:53:57 -07:00
if ( $table != 'admin_log' )
{
$logData = array ( 'TABLE' => $table , 'WHERE' => $sqlQry );
e107 :: getAdminLog () -> addArray ( $logData ) -> save ( 'ADMINUI_03' );
}
2009-11-05 17:32:19 +00:00
return $res ;
}
2011-07-25 02:41:49 +00:00
/**
2014-02-25 11:43:28 +02:00
* Batch Copy Table Rows .
2011-07-25 02:41:49 +00:00
*/
2014-02-26 16:56:17 +02:00
public function copy ( $ids , $session_messages = false )
2011-07-25 02:41:49 +00:00
{
2012-02-07 16:37:44 +00:00
$tp = e107 :: getParser ();
$ids = array_map ( array ( $tp , 'toDB' ), $ids );
2011-07-25 02:41:49 +00:00
$idstr = implode ( ', ' , $ids );
$sql = e107 :: getDb ();
2012-02-07 16:37:44 +00:00
$res = $sql -> db_CopyRow ( $this -> getModelTable (), " * " , $this -> getFieldIdName () . ' IN (' . $idstr . ')' );
if ( false !== $res )
2011-07-25 02:41:49 +00:00
{
$this -> addMessageSuccess ( 'Copied #' . $idstr );
}
else
{
if ( $sql -> getLastErrorNumber ())
{
2012-02-07 16:37:44 +00:00
$this -> addMessageError ( 'SQL Copy Error' , $session_messages ); //TODO - Lan
2011-07-25 02:41:49 +00:00
$this -> addMessageDebug ( 'SQL Error #' . $sql -> getLastErrorNumber () . ': ' . $sql -> getLastErrorText ());
2014-02-25 11:43:28 +02:00
}
2011-07-25 02:41:49 +00:00
}
$this -> _db_errno = $sql -> getLastErrorNumber ();
$this -> _db_errmsg = $sql -> getLastErrorText ();
2014-02-03 14:04:13 +02:00
$this -> _db_qry = $sql -> getLastQuery ();
2014-02-25 11:43:28 +02:00
return $res ;
2011-07-25 02:41:49 +00:00
}
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
/**
* Get urls / url data for given nodes
*/
public function url ( $ids , $options = array (), $extended = false )
{
$ret = array ();
2014-02-25 11:43:28 +02:00
foreach ( $ids as $id )
2013-02-27 19:36:53 +02:00
{
if ( ! $this -> hasNode ( $id )) continue ;
2014-02-25 11:43:28 +02:00
2013-02-27 19:36:53 +02:00
$model = $this -> getNode ( $id );
if ( $this -> getUrl ()) $model -> setUrl ( $this -> getUrl ()); // copy url config data if available
2016-06-09 16:43:36 -07:00
$ret [ $id ] = $model -> url ( null , $options , $extended );
2013-02-27 19:36:53 +02:00
}
return $ret ;
2013-02-26 16:08:08 -08:00
}
2017-01-27 18:02:57 -08:00
/**
* Export Selected Data
* @ param $ids
* @ return null
*/
public function export ( $ids )
{
$ids = e107 :: getParser () -> filter ( $ids , 'int' );
if ( empty ( $ids ))
{
return false ;
}
$idstr = implode ( ', ' , $ids );
$table = array ( $this -> getModelTable ());
$filename = " e107Export_ " . $this -> getModelTable () . " _ " . date ( " YmdHi " ) . " .xml " ;
$query = $this -> getFieldIdName () . ' IN (' . $idstr . ') ' ; // ORDER BY '.$this->getParam('db_order') ;
e107 :: getXML () -> e107Export ( null , $table , null , array ( 'file' => $filename , 'query' => $query ));
return null ;
}
2013-05-09 22:48:27 -05:00
}