2009-11-24 16:32:02 +00:00
< ? php
2010-03-01 13:02:43 +00:00
/*
* e107 website system
*
* Copyright ( C ) 2008 - 2010 e107 Inc ( e107 . org )
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
* Administration User Interface logic
*
* $URL $
* $Id $
*/
/**
* @ package e107
2010-05-28 22:10:20 +00:00
* @ subpackage e107_handlers
2010-03-01 13:02:43 +00:00
* @ version $Id $
*
* Administration User Interface logic
*/
2009-11-24 16:32:02 +00:00
/**
2010-03-01 13:02:43 +00:00
* @ todo core request handler ( non - admin ), core response
2009-11-24 16:32:02 +00:00
*/
class e_admin_request
{
/**
* Current GET request array
* @ var array
*/
protected $_request_qry ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Current POST array
* @ var array
*/
protected $_posted_qry ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Current Mode
* @ var string
*/
protected $_mode = '' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Default Mode
* @ var string
*/
protected $_default_mode = 'main' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Key name for mode search
* @ var string
*/
protected $_mode_key = 'mode' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Current action
* @ var string
*/
protected $_action = '' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Default Action
* @ var string
*/
protected $_default_action = 'index' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Key name for action search
* @ var string
*/
protected $_action_key = 'action' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Current ID
* @ var integer
*/
protected $_id = 0 ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Key name for ID search
* @ var string
*/
protected $_id_key = 'id' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Constructor
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string | array $qry [ optional ]
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
public function __construct ( $request_string = null , $parse = true )
{
if ( null === $request_string )
{
$request_string = str_replace ( '&' , '&' , e_QUERY );
}
if ( $parse )
{
$this -> parseRequest ( $request_string );
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Parse request data
* @ param string | array $request_data
* @ return e_admin_request
*/
protected function parseRequest ( $request_data )
{
if ( is_string ( $request_data ))
{
parse_str ( $request_data , $request_data );
}
$this -> _request_qry = ( array ) $request_data ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Set current mode
if ( isset ( $this -> _request_qry [ $this -> _mode_key ]))
{
$this -> _mode = preg_replace ( '/[^\w]/' , '' , $this -> _request_qry [ $this -> _mode_key ]);
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Set current action
if ( isset ( $this -> _request_qry [ $this -> _action_key ]))
{
$this -> _action = preg_replace ( '/[^\w]/' , '' , $this -> _request_qry [ $this -> _action_key ]);
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Set current id
if ( isset ( $this -> _request_qry [ $this -> _id_key ]))
{
$this -> _id = intval ( $this -> _request_qry [ $this -> _id_key ]);
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$this -> _posted_qry = $_POST ; //raw?
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Retrieve variable from GET scope
* If $key is null , all GET data will be returned
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $key [ optional ]
* @ param mixed $default [ optional ]
* @ return mixed
*/
public function getQuery ( $key = null , $default = null )
{
if ( null === $key )
{
return $this -> _request_qry ;
}
return ( isset ( $this -> _request_qry [ $key ]) ? $this -> _request_qry [ $key ] : $default );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set / Unset GET variable
* If $key is array , $value is not used .
2009-12-23 15:12:13 +00:00
* If $value is null , ( string ) $key is unset
*
2009-11-24 16:32:02 +00:00
* @ param string | array $key
* @ param mixed $value [ optional ]
* @ return e_admin_request
*/
public function setQuery ( $key , $value = null )
{
if ( is_array ( $key ))
{
foreach ( $key as $k => $v )
{
$this -> setQuery ( $k , $v );
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( null === $value )
{
unset ( $this -> _request_qry [ $key ]);
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$this -> _request_qry [ $key ] = $value ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Retrieve variable from POST scope
* If $key is null , all POST data will be returned
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $key [ optional ]
* @ param mixed $default [ optional ]
* @ return mixed
*/
public function getPosted ( $key = null , $default = null )
{
if ( null === $key )
{
return $this -> _posted_qry ;
}
return ( isset ( $this -> _posted_qry [ $key ]) ? $this -> _posted_qry [ $key ] : $default );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set / Unset POST variable
* If $key is array , $value is not used .
2009-12-23 15:12:13 +00:00
* If $value is null , ( string ) $key is unset
*
2009-11-24 16:32:02 +00:00
* @ param object $key
* @ param object $value [ optional ]
* @ return e_admin_request
*/
public function setPosted ( $key , $value = null )
{
if ( is_array ( $key ))
{
2010-11-04 23:27:47 +00:00
if ( empty ( $key ))
{
$this -> _posted_qry = array (); //POST reset
return $this ;
}
2009-11-24 16:32:02 +00:00
foreach ( $key as $k => $v )
{
$this -> setPosted ( $k , $v );
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( null === $value )
{
unset ( $this -> _posted_qry [ $key ]);
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$tp = e107 :: getParser ();
$this -> _posted_qry [ $tp -> post_toForm ( $key )] = $tp -> post_toForm ( $value );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current mode
* @ return string
*/
public function getMode ()
{
if ( ! $this -> _mode ) return $this -> getDefaultMode ();
return $this -> _mode ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get default mode
* @ return string
*/
public function getDefaultMode ()
{
return $this -> _default_mode ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current mode name
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ return string
*/
public function getModeName ()
{
return strtolower ( str_replace ( '-' , '_' , $this -> getMode ()));
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Reset current mode
* @ param string $mode
* @ return e_admin_request
*/
public function setMode ( $mode )
{
$this -> _mode = preg_replace ( '/[^\w]/' , '' , $mode );
$this -> setQuery ( $this -> _mode_key , $this -> _mode );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set default mode
* @ param string $mode
* @ return e_admin_request
*/
public function setDefaultMode ( $mode )
{
if ( $mode ) $this -> _default_mode = $mode ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set mode key name
* @ param string $key
* @ return e_admin_request
*/
public function setModeKey ( $key )
{
$this -> _mode_key = $key ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current action
2009-12-13 21:52:32 +00:00
* @ return TBD
2009-11-24 16:32:02 +00:00
*/
public function getAction ()
{
if ( ! $this -> _action ) return $this -> getDefaultAction ();
return $this -> _action ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get default action
* @ return string
*/
public function getDefaultAction ()
{
return $this -> _default_action ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current action name
* @ return string camelized action
*/
public function getActionName ()
{
return $this -> camelize ( $this -> getAction ());
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Reset current action
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $action
* @ return e_admin_request
*/
public function setAction ( $action )
{
$this -> _action = preg_replace ( '/[^\w]/' , '' , $action );
$this -> setQuery ( $this -> _action_key , $this -> _action );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set default action
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $action
* @ return e_admin_request
*/
public function setDefaultAction ( $action )
{
if ( $action ) $this -> _default_action = $action ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set action key name
* @ param string $key
* @ return e_admin_request
*/
public function setActionKey ( $key )
{
$this -> _action_key = $key ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current ID
* @ return integer
*/
public function getId ()
{
return $this -> _id ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Reset current ID
* @ param string $id
* @ return e_admin_request
*/
public function setId ( $id )
{
$id = intval ( $id );
$this -> _id = $id ;
$this -> setQuery ( $this -> _id_key , $id );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set id key name
* @ param string $key
* @ return e_admin_request
*/
public function setIdKey ( $key )
{
$this -> _id_key = $key ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Build query string from current request array
* NOTE : changing url separator to & amp ; ( $encode == true ) ( thus URL XHTML compliance ) works in PHP 5.1 . 2 + environment
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string | array $merge_with [ optional ] override request values
* @ param boolean $encode if true & amp ; separator will be used , all values will be http encoded , default true
* @ param string | array $exclude_from_query numeric array / comma separated list of vars to be excluded from current query , true - don ' t use current query at all
* @ return string url encoded query string
*/
public function buildQueryString ( $merge_with = array (), $encode = true , $exclude_from_query = '' )
{
$ret = $this -> getQuery ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
//special case - exclude all current
if ( true === $exclude_from_query )
{
2010-10-04 22:14:05 +00:00
$exclude_from_query = array_keys ( $ret );
2009-11-24 16:32:02 +00:00
}
// to array
if ( is_string ( $exclude_from_query ))
{
$exclude_from_query = array_map ( 'trim' , explode ( ',' , $exclude_from_query ));
}
2009-12-23 15:12:13 +00:00
if ( $exclude_from_query )
2009-11-24 16:32:02 +00:00
{
foreach ( $exclude_from_query as $var )
{
unset ( $ret [ $var ]);
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( is_string ( $merge_with ))
{
parse_str ( $merge_with , $merge_with );
}
$ret = array_merge ( $ret , ( array ) $merge_with );
$separator = '&' ;
if ( $encode )
{
$separator = '&' ;
//$ret = array_map('rawurlencode', $ret);
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$ret = http_build_query ( $ret , 'numeric_' , $separator );
if ( ! $encode )
{
return rawurldecode ( $ret );
}
return $ret ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Convert string to CamelCase
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $str
* @ return string
*/
public function camelize ( $str )
{
return implode ( '' , array_map ( 'ucfirst' , explode ( '-' , str_replace ( '_' , '-' , $str ))));
}
}
/**
* TODO - front response parent , should do all the header . php work
*/
class e_admin_response
{
/**
* Body segments
*
* @ var array
*/
protected $_body = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Title segments
*
* @ var unknown_type
*/
protected $_title = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* e107 meta title
*
* @ var array
*/
protected $_e_PAGETITLE = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* e107 meta description
*
* @ var array
*/
protected $_META_DESCRIPTION = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* e107 meta keywords
*
* @ var array
*/
protected $_META_KEYWORDS = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Render mods
*
* @ var array
*/
protected $_render_mod = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Meta title segment description
*
* @ var string
*/
protected $_meta_title_separator = ' - ' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Title segment separator
*
* @ var string
*/
protected $_title_separator = ' » ' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Constructor
*
*/
public function __construct ()
{
$this -> _render_mod [ 'default' ] = 'admin_page' ;
}
/**
* Set body segments for a namespace
*
* @ param string $content
* @ param string $namespace segment namesapce
* @ return e_admin_response
*/
function setBody ( $content , $namespace = 'default' )
{
$this -> _body [ $namespace ] = $content ;
return $this ;
}
/**
* Append body segment to a namespace
*
* @ param string $content
* @ param string $namespace segment namesapce
* @ return e_admin_response
*/
function appendBody ( $content , $namespace = 'default' )
{
if ( ! isset ( $this -> _body [ $namespace ]))
{
$this -> _body [ $namespace ] = array ();
}
$this -> _body [ $namespace ][] = $content ;
return $this ;
}
/**
* Prepend body segment to a namespace
*
* @ param string $content
* @ param string $namespace segment namespace
* @ return e_admin_response
*/
function prependBody ( $content , $namespace = 'default' )
{
if ( ! isset ( $this -> _body [ $namespace ]))
{
$this -> _body [ $namespace ] = array ();
}
$this -> _body [ $namespace ] = array_merge ( array ( $content ), $this -> _body [ $namespace ]);
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get body segments from a namespace
*
* @ param string $namespace segment namesapce
* @ param boolean $reset reset segment namespace
* @ param string | boolean $glue if false return array , else return string
* @ return string | array
*/
function getBody ( $namespace = 'default' , $reset = false , $glue = '' )
{
$content = vartrue ( $this -> _body [ $namespace ], array ());
if ( $reset )
{
$this -> _body [ $namespace ] = array ();
}
if ( is_bool ( $glue ))
{
return ( $glue ? $content : implode ( '' , $content ));
}
return implode ( $glue , $content );
}
/**
* Set title segments for a namespace
*
* @ param string $title
* @ param string $namespace
* @ return e_admin_response
*/
function setTitle ( $title , $namespace = 'default' )
{
$this -> _title [ $namespace ] = array ( $title );
return $this ;
}
/**
* Append title segment to a namespace
*
* @ param string $title
* @ param string $namespace segment namesapce
* @ return e_admin_response
*/
function appendTitle ( $title , $namespace = 'default' )
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
if ( empty ( $title ))
{
return $this ;
}
if ( ! isset ( $this -> _title [ $namespace ]))
{
$this -> _title [ $namespace ] = array ();
}
2009-12-23 15:12:13 +00:00
$this -> _title [ $namespace ][] = $title ;
2009-11-24 16:32:02 +00:00
return $this ;
}
/**
* Prepend title segment to a namespace
*
* @ param string $title
* @ param string $namespace segment namespace
* @ return e_admin_response
*/
function prependTitle ( $title , $namespace = 'default' )
{
if ( empty ( $title ))
{
return $this ;
}
if ( ! isset ( $this -> _title [ $namespace ]))
{
$this -> _title [ $namespace ] = array ();
}
$this -> _title [ $namespace ] = array_merge ( array ( $title ), $this -> _title [ $namespace ]);
return $this ;
}
/**
* Get title segments from namespace
*
* @ param string $namespace
* @ param boolean $reset
* @ param boolean | string $glue
* @ return unknown
*/
function getTitle ( $namespace = 'default' , $reset = false , $glue = ' - ' )
{
$content = array ();
if ( isset ( $this -> _title [ $namespace ]) && is_array ( $this -> _title [ $namespace ]))
{
$content = $this -> _title [ $namespace ];
}
if ( $reset )
{
unset ( $this -> _title [ $namespace ]);
}
if ( is_bool ( $glue ) || empty ( $glue ))
{
return ( $glue ? $content : implode ( $this -> _title_separator , $content ));
}
return implode ( $glue , $content );
}
/**
* Set render mode for a namespace
*
* @ param string $render_mod
* @ param string $namespace
* @ return e_admin_response
*/
function setRenderMod ( $render_mod , $namespace = 'default' )
{
$this -> _render_mod [ $namespace ] = $render_mod ;
return $this ;
}
/**
* Set render mode for namespace
*
* @ param string $namespace
* @ return string
*/
function getRenderMod ( $namespace = 'default' )
{
return varset ( $this -> _render_mod [ $namespace ], null );
}
/**
* Add meta title , description and keywords segments
*
* @ param string $meta property name
* @ param string $content meta content
* @ return e_admin_response
*/
function addMetaData ( $meta , $content )
{
$tp = e107 :: getParser ();
$meta = '_' . $meta ;
if ( isset ( $this -> { $meta }) && ! empty ( $content ))
{
2010-02-08 16:50:28 +00:00
$this -> { $meta }[] = strip_tags ( $content );
2009-11-24 16:32:02 +00:00
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add meta title segment
*
* @ param string $title
* @ return e_admin_response
*/
function addMetaTitle ( $title )
{
$this -> addMetaData ( 'e_PAGETITLE' , $title );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add meta description segment
*
* @ param string $description
* @ return e_admin_response
*/
function addMetaDescription ( $description )
{
$this -> addMetaData ( 'META_DESCRIPTION' , $description );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add meta keywords segment
*
* @ param string $keyword
* @ return e_admin_response
*/
function addMetaKeywords ( $keyword )
{
$this -> addMetaData ( 'META_KEYWORDS' , $keyword );
return $this ;
}
/**
* Send e107 meta - data
*
* @ return e_admin_response
*/
function sendMeta ()
{
//HEADERF already included or meta content already sent
if ( e_AJAX_REQUEST || defined ( 'HEADER_INIT' ) || defined ( 'e_PAGETITLE' ))
return $this ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( ! defined ( 'e_PAGETITLE' ) && ! empty ( $this -> _e_PAGETITLE ))
{
define ( 'e_PAGETITLE' , implode ( $this -> _meta_title_separator , $this -> _e_PAGETITLE ));
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( ! defined ( 'META_DESCRIPTION' ) && ! empty ( $this -> _META_DESCRIPTION ))
{
define ( 'META_DESCRIPTION' , implode ( ' ' , $this -> _META_DESCRIPTION ));
}
if ( ! defined ( 'META_KEYWORDS' ) && ! empty ( $this -> _META_KEYWORDS ))
{
define ( 'META_KEYWORDS' , implode ( ', ' , $this -> _META_KEYWORDS ));
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add content segment to the header namespace
*
* @ param string $content
* @ return e_admin_response
*/
function addHeaderContent ( $content )
{
$this -> appendBody ( $content , 'header_content' );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get page header namespace content segments
*
* @ param boolean $reset
* @ param boolean $glue
* @ return string
*/
function getHeaderContent ( $reset = true , $glue = " \n \n " )
{
return $this -> getBody ( 'header_content' , $reset , $glue );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Switch to iframe mod
* FIXME - implement e_IFRAME to frontend - header_default . php
*
* @ return e_admin_response
*/
function setIframeMod ()
{
global $HEADER , $FOOTER , $CUSTOMHEADER , $CUSTOMFOOTER ;
2009-12-23 15:12:13 +00:00
$HEADER = $FOOTER = '' ;
2009-11-24 16:32:02 +00:00
$CUSTOMHEADER = $CUSTOMFOOTER = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// New
if ( ! defined ( 'e_IFRAME' ))
{
define ( 'e_IFRAME' , true );
}
return $this ;
}
/**
* Send Response Output
*
* @ param string $name segment
* @ param array $options valid keys are : messages | render | meta | return | raw | ajax
* @ return mixed
*/
function send ( $name = 'default' , $options = array ())
{
if ( is_string ( $options ))
{
parse_str ( $options , $options );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Merge with all available default options
$options = array_merge ( array (
2009-12-23 15:12:13 +00:00
'messages' => true ,
'render' => true ,
'meta' => false ,
'return' => false ,
2009-11-24 16:32:02 +00:00
'raw' => false ,
'ajax' => false
), $options );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$content = $this -> getBody ( $name , true );
2009-12-23 15:12:13 +00:00
$title = $this -> getTitle ( $name , true );
2009-11-24 16:32:02 +00:00
$return = $options [ 'return' ];
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $options [ 'ajax' ] || e_AJAX_REQUEST )
{
$type = $options [ 'ajax' ] && is_string ( $options [ 'ajax' ]) ? $options [ 'ajax' ] : '' ;
$this -> getJsHelper () -> sendResponse ( $type );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $options [ 'messages' ])
{
$content = e107 :: getMessage () -> render () . $content ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $options [ 'meta' ])
{
$this -> sendMeta ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// raw output expected - force return array
if ( $options [ 'raw' ])
{
return array ( $title , $content , $this -> getRenderMod ( $name ));
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
//render disabled by the controller
if ( ! $this -> getRenderMod ( $name ))
{
$options [ 'render' ] = false ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $options [ 'render' ])
{
return e107 :: getRender () -> tablerender ( $title , $content , $this -> getRenderMod ( $name ), $return );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $return )
{
return $content ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
print ( $content );
return '' ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get JS Helper instance
*
* @ return e_jshelper
*/
public function getJsHelper ()
{
return e107 :: getSingleton ( 'e_jshelper' , true , 'admin_response' );
}
}
/**
* TODO - request related code should be moved to core
* request handler
*/
class e_admin_dispatcher
{
/**
* @ var e_admin_request
*/
protected $_request = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var e_admin_response
*/
protected $_response = null ;
2009-12-23 15:12:13 +00:00
/**
2009-11-24 16:32:02 +00:00
* @ var e_admin_controller
*/
protected $_current_controller = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Required ( set by child class ) .
2009-12-23 15:12:13 +00:00
* Controller map array in format
2009-11-24 16:32:02 +00:00
* 'MODE' => array ( 'controller' => 'CONTROLLER_CLASS_NAME' [, 'path' => 'CONTROLLER SCRIPT PATH' , 'ui' => extend of 'comments_admin_form_ui' , 'uipath' => 'path/to/ui/' ]);
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ var array
*/
protected $modes ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var string
*/
protected $defaultMode = '' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var string
*/
protected $defaultAction = '' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Optional - map 'mode/action' pair to 'modeAlias/actionAlias'
2009-11-24 16:32:02 +00:00
* @ var string
*/
protected $adminMenuAliases = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Optional ( set by child class ) .
* Required for admin menu render
* Format : 'mode/action' => array ( 'caption' => 'Link title' [, 'perm' => '0' , 'url' => '{e_PLUGIN}plugname/admin_config.php' ], ... );
* All valid key - value pair ( see e_admin_menu function ) are accepted .
* @ var array
*/
protected $adminMenu = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Optional ( set by child class ) .
* @ var string
*/
protected $menuTitle = 'Menu' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var string
*/
protected $pluginTitle = '' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Constructor
*
2009-11-24 16:32:02 +00:00
* @ param string | array | e_admin_request $request [ optional ]
* @ param e_admin_response $response
*/
public function __construct ( $request = null , $response = null , $auto_observe = true )
{
if ( null === $request || ! is_object ( $request ))
{
$request = new e_admin_request ( $request );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( null === $response )
{
$response = new e_admin_response ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$this -> setRequest ( $request ) -> setResponse ( $response ) -> init ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( ! $this -> defaultMode || ! $this -> defaultAction )
{
$this -> setDefaults ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$request -> setDefaultMode ( $this -> defaultMode ) -> setDefaultAction ( $this -> defaultAction );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// register itself
e107 :: setRegistry ( 'admin/ui/dispatcher' , $this );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $auto_observe )
{
$this -> runObservers ( true );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined constructor - called before _initController () method
* @ return e_admin_dispatcher
*/
public function init ()
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Retrieve missing default action / mode
* @ return e_admin_dispatcher
*/
public function setDefaults ()
{
// try Admin menu first
if ( $this -> adminMenu )
{
reset ( $this -> adminMenu );
2010-12-27 12:38:14 +00:00
list ( $mode , $action ) = explode ( '/' , key ( $this -> adminMenu ), 3 );
2009-11-24 16:32:02 +00:00
}
else
{
reset ( $this -> modes );
$mode = key ( $this -> modes );
$action = $this -> modes [ $mode ][ 'index' ];
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( ! $this -> defaultMode ) $this -> defaultMode = $mode ;
if ( ! $this -> defaultAction ) $this -> defaultAction = $action ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get request object
* @ return e_admin_request
*/
public function getRequest ()
{
return $this -> _request ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set request object
* @ param e_admin_request $request
* @ return e_admin_dispatcher
*/
public function setRequest ( $request )
{
$this -> _request = $request ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get response object
* @ return e_admin_response
*/
public function getResponse ()
{
return $this -> _response ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set response object
* @ param e_admin_response $response
* @ return e_admin_dispatcher
*/
public function setResponse ( $response )
{
$this -> _response = $response ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Dispatch & render all
2009-12-23 15:12:13 +00:00
*
* @ param boolean $return if true , array ( title , body , render_mod ) will be returned
2009-11-24 16:32:02 +00:00
* @ return string | array current admin page body
*/
public function run ( $return = false )
{
return $this -> runObserver () -> renderPage ( $return );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Run observers / headers only , should be called before header . php call
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ return e_admin_dispatcher
*/
public function runObservers ( $run_header = true )
{
//search for $actionName.'Observer' method. Additional $actionName.$triggerName.'Trigger' methods will be called as well
$this -> getController () -> dispatchObserver ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
//search for $actionName.'Header' method, js manager should be used inside for sending JS to the page,
// meta information should be created there as well
if ( $run_header )
{
$this -> getController () -> dispatchHeader ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
}
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Run page action .
* If return type is array , it should contain allowed response options ( see e_admin_response :: send ())
* Available return type string values :
* - render_return : return rendered content ( see e107 :: getRender () -> tablerender ()), add system messages , send meta information
* - render : outputs rendered content ( see e107 :: getRender () -> tablerender ()), add system messages
* - response : return response object
* - raw : return array ( title , content , render mode )
* - ajax : force ajax output ( and exit )
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string | array $return_type expected string values : render | render_out | response | raw | ajax [ _text | _json | _xml ]
* @ return mixed
*/
public function runPage ( $return_type = 'render' )
{
$response = $this -> getController () -> dispatchPage ();
if ( is_array ( $return_type ))
{
return $response -> send ( 'default' , $return_type );
}
switch ( $return_type )
{
case 'render_return' :
$options = array (
2009-12-23 15:12:13 +00:00
'messages' => true ,
'render' => true ,
'meta' => true ,
'return' => true ,
2009-11-24 16:32:02 +00:00
'raw' => false
);
break ;
case 'raw' :
$options = array (
2009-12-23 15:12:13 +00:00
'messages' => false ,
'render' => false ,
'meta' => false ,
'return' => true ,
2009-11-24 16:32:02 +00:00
'raw' => true
);
break ;
case 'ajax' :
case 'ajax_text' :
case 'ajax_xml' ;
case 'ajax_json' ;
$options = array (
2009-12-23 15:12:13 +00:00
'messages' => false ,
'render' => false ,
'meta' => false ,
'return' => false ,
2009-11-24 16:32:02 +00:00
'raw' => false ,
'ajax' => str_replace ( array ( 'ajax_' , 'ajax' ), array ( '' , 'text' ), $return_type )
);
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'response' :
return $response ;
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'render' :
default :
$options = array (
2009-12-23 15:12:13 +00:00
'messages' => true ,
'render' => true ,
'meta' => false ,
'return' => false ,
2009-11-24 16:32:02 +00:00
'raw' => false
);
break ;
}
return $response -> send ( 'default' , $options );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Proxy method
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ return string
*/
public function getHeader ()
{
return $this -> getController () -> getHeader ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current controller object
* @ return e_admin_controller
*/
public function getController ()
{
if ( null === $this -> _current_controller )
{
$this -> _initController ();
}
return $this -> _current_controller ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Try to init Controller from request using current controller map
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ return e_admin_dispatcher
*/
protected function _initController ()
{
$request = $this -> getRequest ();
$response = $this -> getResponse ();
if ( isset ( $this -> modes [ $request -> getModeName ()]) && isset ( $this -> modes [ $request -> getModeName ()][ 'controller' ]))
{
$class_name = $this -> modes [ $request -> getModeName ()][ 'controller' ];
$class_path = vartrue ( $this -> modes [ $request -> getModeName ()][ 'path' ]);
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $class_path )
{
require_once ( e107 :: getParser () -> replaceConstants ( $class_path ));
}
if ( $class_name && class_exists ( $class_name )) //NOTE: autoload in the play
{
$this -> _current_controller = new $class_name ( $request , $response );
//give access to current request object, user defined init
2009-12-23 15:12:13 +00:00
$this -> _current_controller -> setRequest ( $this -> getRequest ()) -> init ();
2009-11-24 16:32:02 +00:00
}
else
{
2009-12-23 15:12:13 +00:00
//TODO - get default controller (core or user defined), set Action for
2009-11-24 16:32:02 +00:00
//'Controller not found' page, add message(?), break
2009-12-23 15:12:13 +00:00
// get default controller
2009-11-24 16:32:02 +00:00
$this -> _current_controller = $this -> getDefaultController ();
// add messages
e107 :: getMessage () -> add ( 'Can\'t find class ' . ( $class_name ? $class_name : 'n/a' ), E_MESSAGE_ERROR )
-> add ( 'Requested: ' . e_SELF . '?' . $request -> buildQueryString (), E_MESSAGE_DEBUG );
2009-12-23 15:12:13 +00:00
//
2009-11-24 16:32:02 +00:00
$request -> setMode ( $this -> getDefaultControllerName ()) -> setAction ( 'e404' );
2009-12-23 15:12:13 +00:00
$this -> _current_controller -> setRequest ( $request ) -> init ();
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( vartrue ( $this -> modes [ $request -> getModeName ()][ 'ui' ]))
{
$class_name = $this -> modes [ $request -> getModeName ()][ 'ui' ];
$class_path = vartrue ( $this -> modes [ $request -> getModeName ()][ 'uipath' ]);
if ( $class_path )
{
require_once ( e107 :: getParser () -> replaceConstants ( $class_path ));
}
if ( class_exists ( $class_name )) //NOTE: autoload in the play
{
$this -> _current_controller -> setParam ( 'ui' , new $class_name ( $this -> _current_controller ));
}
}
$this -> _current_controller -> setParam ( 'modes' , $this -> modes );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Default controller object - needed if controller not found
* @ return e_admin_controller
*/
public function getDefaultController ()
{
$class_name = $this -> getDefaultControllerName ();
return new $class_name ( $this -> getRequest (), $this -> getResponse ());
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Default controller name - needed if controller not found
2009-12-13 21:52:32 +00:00
* @ return string name of controller
2009-11-24 16:32:02 +00:00
*/
public function getDefaultControllerName ()
{
return 'e_admin_controller' ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Admin Menu Generator
* @ return string
*/
function renderMenu ()
{
$tp = e107 :: getParser ();
$var = array ();
2010-12-27 12:38:14 +00:00
$selected = false ;
2009-11-24 16:32:02 +00:00
foreach ( $this -> adminMenu as $key => $val )
{
2010-12-27 12:38:14 +00:00
$tmp = explode ( '/' , trim ( $key , '/' ), 3 );
2011-05-18 13:41:19 +00:00
2010-12-27 12:38:14 +00:00
// custom 'selected' check
if ( isset ( $val [ 'selected' ]) && $val [ 'selected' ]) $selected = $val [ 'selected' ] === true ? $key : $val [ 'selected' ];
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
foreach ( $val as $k => $v )
{
switch ( $k )
{
case 'caption' :
$k2 = 'text' ;
2010-10-30 15:34:48 +00:00
$v = defset ( $v , $v );
2009-11-24 16:32:02 +00:00
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'url' :
$k2 = 'link' ;
$v = $tp -> replaceConstants ( $v , 'abs' ) . '?mode=' . $tmp [ 0 ] . '&action=' . $tmp [ 1 ];
break ;
2011-05-18 13:41:19 +00:00
2010-12-27 12:38:14 +00:00
case 'uri' :
$k2 = 'link' ;
$v = $tp -> replaceConstants ( $v , 'abs' );
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
default :
$k2 = $k ;
break ;
}
2010-03-01 12:59:26 +00:00
if ( $val [ 'perm' ] != null ) // check perms
2009-12-28 00:29:39 +00:00
{
if ( getperms ( $val [ 'perm' ]))
{
2010-03-01 12:59:26 +00:00
$var [ $key ][ $k2 ] = $v ;
}
2009-12-28 00:29:39 +00:00
}
else
{
2010-03-01 12:59:26 +00:00
$var [ $key ][ $k2 ] = $v ;
2009-12-28 00:29:39 +00:00
}
2010-03-01 12:59:26 +00:00
2009-11-24 16:32:02 +00:00
}
// TODO slide down menu options?
if ( ! vartrue ( $var [ $key ][ 'link' ]))
{
$var [ $key ][ 'link' ] = e_SELF . '?mode=' . $tmp [ 0 ] . '&action=' . $tmp [ 1 ]; // FIXME - URL based on $modes, remove url key
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/* $var [ $key ][ 'text' ] = $val [ 'caption' ];
$var [ $key ][ 'link' ] = ( vartrue ( $val [ 'url' ]) ? $tp -> replaceConstants ( $val [ 'url' ], 'abs' ) : e_SELF ) . '?mode=' . $tmp [ 0 ] . '&action=' . $tmp [ 1 ];
$var [ $key ][ 'perm' ] = $val [ 'perm' ]; */
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$request = $this -> getRequest ();
2010-12-27 12:38:14 +00:00
if ( ! $selected ) $selected = $request -> getMode () . '/' . $request -> getAction ();
2009-11-24 16:32:02 +00:00
$selected = vartrue ( $this -> adminMenuAliases [ $selected ], $selected );
return e_admin_menu ( $this -> menuTitle , $selected , $var );
}
}
class e_admin_controller
{
/**
* @ var e_admin_request
*/
protected $_request ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var e_admin_response
*/
protected $_response ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var array User defined parameters
*/
protected $_params = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var string default action name
*/
protected $_default_action = 'index' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Constructor
2009-11-24 16:32:02 +00:00
* @ param e_admin_request $request [ optional ]
*/
public function __construct ( $request , $response , $params = array ())
{
$this -> _params = array_merge ( array ( 'enable_triggers' => false ), $params );
$this -> setRequest ( $request )
-> setResponse ( $response )
-> setParams ( $params );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined init
* Called before dispatch routine
*/
public function init ()
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get controller parameter
* Currently used core parameters :
* - enable_triggers : don ' t use it direct , see { @ link setTriggersEnabled ()}
* - modes - see dispatcher :: $modes
* - ajax_response - text | xml | json - default is 'text' ; this should be set by the action method
* - TODO - more parameters / add missing to this list
2009-12-23 15:12:13 +00:00
*
* @ param string $key [ optional ] if null - get whole array
2009-11-24 16:32:02 +00:00
* @ param mixed $default [ optional ]
* @ return mixed
*/
public function getParam ( $key = null , $default = null )
{
if ( null === $key )
{
return $this -> _params ;
}
return ( isset ( $this -> _params [ $key ]) ? $this -> _params [ $key ] : $default );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set parameter
* @ param string $key
* @ param mixed $value
* @ return e_admin_controller
*/
public function setParam ( $key , $value )
{
if ( null === $value )
{
unset ( $this -> _params [ $key ]);
return $this ;
}
$this -> _params [ $key ] = $value ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Merge passed parameter array with current parameters
* @ param array $params
* @ return e_admin_controller
*/
public function setParams ( $params )
{
$this -> _params = array_merge ( $this -> _params , $params );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Reset parameter array
* @ param array $params
* @ return e_admin_controller
*/
public function resetParams ( $params )
{
$this -> _params = $params ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current request object
2009-12-23 15:12:13 +00:00
* @ return e_admin_request
2009-11-24 16:32:02 +00:00
*/
public function getRequest ()
{
return $this -> _request ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set current request object
* @ param e_admin_request $request
* @ return e_admin_controller
*/
public function setRequest ( $request )
{
$this -> _request = $request ;
return $this ;
}
/**
* Get current response object
2009-12-23 15:12:13 +00:00
* @ return e_admin_response
2009-11-24 16:32:02 +00:00
*/
public function getResponse ()
{
return $this -> _response ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set current response object
* @ param e_admin_response $response
* @ return e_admin_controller
*/
public function setResponse ( $response )
{
$this -> _response = $response ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Request proxy method
2009-11-24 16:32:02 +00:00
* @ param string $key [ optional ]
* @ param mixed $default [ optional ]
* @ return mixed
*/
public function getQuery ( $key = null , $default = null )
{
return $this -> getRequest () -> getQuery ( $key , $default );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Request proxy method
2009-11-24 16:32:02 +00:00
* @ param string | array $key
* @ param mixed $value [ optional ]
* @ return e_admin_controller
*/
public function setQuery ( $key , $value = null )
{
$this -> getRequest () -> setQuery ( $key , $value );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Request proxy method
2009-11-24 16:32:02 +00:00
* @ param string $key [ optional ]
* @ param mixed $default [ optional ]
* @ return mixed
*/
public function getPosted ( $key = null , $default = null )
{
return $this -> getRequest () -> getPosted ( $key , $default );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Request proxy method
2009-11-24 16:32:02 +00:00
* @ param string $key
* @ param mixed $value [ optional ]
* @ return e_admin_controller
*/
public function setPosted ( $key , $value = null )
{
$this -> getRequest () -> setPosted ( $key , $value );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add page title , response proxy method
*
* @ param string $title
* @ param boolean $meta add to meta as well
* @ return e_admin_controller
*/
public function addTitle ( $title , $meta = true )
{
$this -> getResponse () -> appendTitle ( $title );
if ( $meta ) $this -> addMetaTitle ( $title );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add page meta title , response proxy method .
* Should be called before header . php
*
* @ param string $title
* @ return e_admin_controller
*/
public function addMetaTitle ( $title )
{
$this -> getResponse () -> addMetaTitle ( $title );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Add header content , response proxy method
* Should be called before header . php
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $content
* @ return e_admin_controller
*/
public function addHeader ( $content )
{
$this -> getResponse () -> addHeaderContent ( $content );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get header content , response proxy method
*
* @ return string
*/
public function getHeader ()
{
return $this -> getResponse () -> getHeaderContent ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current mode , response proxy method
* @ return string
*/
public function getMode ()
{
return $this -> getRequest () -> getMode ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current actin , response proxy method
* @ return string
*/
public function getAction ()
{
return $this -> getRequest () -> getAction ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current ID , response proxy method
* @ return string
*/
public function getId ()
{
return $this -> getRequest () -> getId ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get response owned JS Helper instance , response proxy method
*
* @ return e_jshelper
*/
public function getJsHelper ()
{
return $this -> getResponse () -> getJsHelper ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
protected function _preDispatch ( $action = '' )
{
2009-12-23 15:12:13 +00:00
if ( ! $action ) $action = $this -> getRequest () -> getActionName ();
$method = $this -> toMethodName ( $action , 'page' );
2009-11-24 16:32:02 +00:00
if ( ! method_exists ( $this , $method ))
{
$this -> getRequest () -> setAction ( $this -> getDefaultAction ());
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// switch to 404 if needed
$method = $this -> toMethodName ( $this -> getRequest () -> getActionName (), 'page' );
if ( ! method_exists ( $this , $method ))
{
$this -> getRequest () -> setAction ( 'e404' );
2010-11-04 23:27:47 +00:00
e107 :: getMessage () -> add ( sprintf ( LAN_UI_404_METHOD_ERROR , $method ), E_MESSAGE_ERROR );
2009-11-24 16:32:02 +00:00
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Dispatch observer , check for triggers
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $action [ optional ]
* @ return e_admin_controller
*/
public function dispatchObserver ( $action = null )
{
$request = $this -> getRequest ();
if ( null === $request )
{
$request = new e_admin_request ();
$this -> setRequest ( $request );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$this -> _preDispatch ( $action );
if ( null === $action )
{
$action = $request -> getActionName ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// check for observer
$actionObserverName = $this -> toMethodName ( $action , 'observer' , e_AJAX_REQUEST );
if ( method_exists ( $this , $actionObserverName ))
{
$this -> $actionObserverName ();
}
// check for triggers, not available in Ajax mode
if ( ! e_AJAX_REQUEST && $this -> triggersEnabled ())
{
$posted = $request -> getPosted ();
foreach ( $posted as $key => $value )
{
if ( strpos ( $key , 'etrigger_' ) === 0 )
{
2009-12-23 15:12:13 +00:00
$actionTriggerName = $this -> toMethodName ( $action . $request -> camelize ( substr ( $key , 9 )), 'trigger' , false );
2009-11-24 16:32:02 +00:00
if ( method_exists ( $this , $actionTriggerName ))
{
$this -> $actionTriggerName ( $value );
}
//Check if triggers are still enabled
if ( ! $this -> triggersEnabled ())
{
break ;
}
}
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Dispatch header , not allowed in Ajax mode
* @ param string $action [ optional ]
* @ return e_admin_controller
*/
public function dispatchHeader ( $action = null )
{
// not available in Ajax mode
if ( e_AJAX_REQUEST )
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$request = $this -> getRequest ();
if ( null === $request )
{
$request = new e_admin_request ();
$this -> setRequest ( $request );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$this -> _preDispatch ( $action );
if ( null === $action )
{
$action = $request -> getActionName ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// check for observer
2009-12-23 15:12:13 +00:00
$actionHeaderName = $this -> toMethodName ( $action , 'header' , false );
2009-11-24 16:32:02 +00:00
if ( method_exists ( $this , $actionHeaderName ))
{
$this -> $actionHeaderName ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
//send meta data
$this -> getResponse () -> sendMeta ();
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Dispatch controller action
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $action [ optional ]
* @ return e_admin_response
*/
public function dispatchPage ( $action = null )
{
$request = $this -> getRequest ();
if ( null === $request )
{
$request = new e_admin_request ();
$this -> setRequest ( $request );
}
$response = $this -> getResponse ();
2009-12-23 15:12:13 +00:00
$this -> _preDispatch ( $action );
2009-11-24 16:32:02 +00:00
if ( null === $action )
{
$action = $request -> getActionName ();
}
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
// check for observer
2009-12-23 15:12:13 +00:00
$actionName = $this -> toMethodName ( $action , 'page' );
2009-11-24 16:32:02 +00:00
$ret = '' ;
if ( ! method_exists ( $this , $actionName )) // pre dispatch already switched to default action/not found page if needed
{
e107 :: getMessage () -> add ( 'Action ' . $actionName . ' no found!' , E_MESSAGE_ERROR );
return $response ;
}
ob_start (); //catch any output
$ret = $this -> { $actionName }();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
//Ajax XML/JSON communication
if ( e_AJAX_REQUEST && is_array ( $ret ))
{
$response_type = $this -> getParam ( 'ajax_response' , 'xml' );
ob_clean ();
$js_helper = $response -> getJsHelper ();
2009-12-23 15:12:13 +00:00
foreach ( $ret as $act => $data )
2009-11-24 16:32:02 +00:00
{
$js_helper -> addResponse ( $data , $act );
}
$js_helper -> sendResponse ( $response_type );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$ret .= ob_get_clean ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Ajax text response
if ( e_AJAX_REQUEST )
{
$response_type = 'text' ;
$response -> getJsHelper () -> addResponse ( $ret ) -> sendResponse ( $response_type );
}
else
{
$response -> appendBody ( $ret );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $response ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function E404Observer ()
{
2010-11-04 23:27:47 +00:00
$this -> getResponse () -> setTitle ( LAN_UI_404_TITLE_ERROR );
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function E404Page ()
{
2010-11-04 23:27:47 +00:00
return '<div class="center">' . LAN_UI_404_BODY_ERROR . '</div>' ; // TODO - lan
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function E404AjaxPage ()
{
exit ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic redirect handler , it handles almost everything we would need .
* Additionally , it moves currently registered system messages to SESSION message stack
* In almost every case { @ link redirectAction ()} and { @ link redirectMode ()} are better solution
2009-12-23 15:12:13 +00:00
*
* @ param string $action defaults to current action
* @ param string $mode defaults to current mode
2009-11-24 16:32:02 +00:00
* @ param string | array $exclude_query comma delimited variable names to be excluded from current query OR TRUE to exclude everything
* @ param string | array $merge_query query string ( & amp ; delimiter ) or associative array to be merged with current query
* @ param string $path default to e_SELF
* @ return void
*/
public function redirect ( $action = null , $mode = null , $exclude_query = '' , $merge_query = array (), $path = null )
{
$request = $this -> getRequest ();
if ( $mode ) $request -> setMode ( $mode );
if ( $action ) $request -> setAction ( $action );
if ( ! $path ) $path = e_SELF ;
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
$url = $path . '?' . $request -> buildQueryString ( $merge_query , false , $exclude_query );
// Transfer all messages to session
2009-12-23 15:12:13 +00:00
e107 :: getMessage () -> moveToSession ();
2009-11-24 16:32:02 +00:00
// write session data
session_write_close ();
// do redirect
header ( 'Location: ' . $url );
exit ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Convenient redirect () proxy method , make life easier when redirecting between actions
2009-11-24 16:32:02 +00:00
* in same mode .
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $action [ optional ]
* @ param string | array $exclude_query [ optional ]
* @ param string | array $merge_query [ optional ]
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
public function redirectAction ( $action = null , $exclude_query = '' , $merge_query = array ())
{
$this -> redirect ( $action , null , $exclude_query , $merge_query );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Convenient redirect to another mode ( doesn ' t use current Query state )
* If path is empty , it ' ll be auto - detected from modes ( dispatcher ) array
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $mode
* @ param string $action
* @ param string | array $query [ optional ]
* @ param string $path
* @ return void
*/
public function redirectMode ( $mode , $action , $query = array (), $path = null )
{
if ( ! $path && $this -> getParam ( 'modes' ))
{
$modes = $this -> getParam ( 'modes' );
if ( vartue ( $modes [ $mode ]) && vartrue ( $modes [ $mode ][ 'url' ]))
{
$path = e107 :: getParser () -> replaceConstants ( $modes [ $mode ][ 'url' ], 'abs' );
}
}
$this -> redirect ( $action , $mode , true , $query , $path );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Convert action name to method name
2009-12-23 15:12:13 +00:00
*
* @ param string $action_name formatted ( e . g . request method getActionName ()) action name
2009-11-24 16:32:02 +00:00
* @ param string $type page | observer | header | trigger
* @ param boolean $ajax force with true / false , if null will be auto - resolved
2009-12-13 21:52:32 +00:00
* @ return string
2009-11-24 16:32:02 +00:00
*/
public function toMethodName ( $action_name , $type = 'page' , $ajax = null )
{
if ( null === $ajax ) $ajax = e_AJAX_REQUEST ; //auto-resolving
return $action_name . ( $ajax ? 'Ajax' : '' ) . ucfirst ( strtolower ( $type ));
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Check if there is a trigger available in the posted data
2010-11-04 23:27:47 +00:00
* @ param array $exclude
2009-11-24 16:32:02 +00:00
* @ return boolean
*/
2010-11-04 23:27:47 +00:00
public function hasTrigger ( $exclude = array ())
2009-11-24 16:32:02 +00:00
{
2009-12-23 15:12:13 +00:00
$posted = array_keys ( $this -> getPosted ());
2009-11-24 16:32:02 +00:00
foreach ( $posted as $key )
{
2010-11-04 23:27:47 +00:00
if ( ! in_array ( $key , $exclude ) && strpos ( $key , 'etrigger_' ) === 0 )
2009-11-24 16:32:02 +00:00
{
return true ;
}
}
return false ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get default action
* @ return string action
*/
public function getDefaultAction ()
{
return $this -> _default_action ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Set default action
2009-11-24 16:32:02 +00:00
* @ param string $action_name
* @ return e_admin_controller
*/
public function setDefaultAction ( $action_name )
{
$this -> _default_action = $action_name ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ return boolean
*/
public function triggersEnabled ()
{
return $this -> getParam ( 'enable_triggers' );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ param boolean $flag
* @ return e_admin_controller
*/
public function setTriggersEnabled ( $flag )
{
$this -> setParam ( 'enable_triggers' , $flag );
return $this ;
}
}
//FIXME - move everything from e_admin_ui except model auto-create related code
class e_admin_controller_ui extends e_admin_controller
{
/**
* @ var array UI field data
*/
protected $fields = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var array default fields activated on List view
*/
protected $fieldpref = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var array Plugin Preference description array
*/
protected $prefs = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Data required for _modifyListQry () to automate
* db query building
2009-12-23 15:12:13 +00:00
* @ var array
2009-11-24 16:32:02 +00:00
*/
protected $tableJoin = array ();
2011-05-18 13:41:19 +00:00
2011-05-07 06:22:44 +00:00
/**
* Array of table names and their aliases . ( detected from listQry )
* db query building
* @ var array
*/
protected $joinAlias = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Main model table alias
* @ var string
*/
protected $tableAlias ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var string plugin name
*/
protected $pluginName ;
2009-12-23 15:12:13 +00:00
/**
* @ var string
*/
protected $defaultOrderField = null ;
/**
* @ var string
*/
protected $defaultOrder = 'asc' ;
/**
* @ var string SQL order , false to disable order , null is default order
*/
protected $listOrder = null ;
2011-05-18 13:41:19 +00:00
2010-12-27 12:38:14 +00:00
/**
* Structure same as TreeModel parameters used for building the load () SQL
* @ var additional SQL to be applied when auto - building the list query
*/
protected $listQrySql = array ();
2009-12-23 15:12:13 +00:00
/**
* @ var boolean
*/
2009-11-24 16:32:02 +00:00
protected $batchDelete = true ;
2011-07-25 02:41:49 +00:00
/**
* @ var boolean
*/
protected $batchCopy = false ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Could be LAN constant ( mulit - language support )
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ var string plugin name
*/
protected $pluginTitle ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Default ( db ) limit value
* @ var integer
*/
protected $perPage = 20 ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var e_admin_model
*/
protected $_model = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var e_admin_tree_model
*/
protected $_tree_model = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var e_admin_tree_model
*/
protected $_ui = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ var e_plugin_pref | e_core_pref
*/
protected $_pref = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function getBatchDelete ()
{
return $this -> batchDelete ;
}
2011-07-25 02:41:49 +00:00
public function getBatchCopy ()
{
return $this -> batchCopy ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ return string
*/
public function getPluginName ()
{
return $this -> pluginName ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ return string
*/
public function getPluginTitle ()
{
return deftrue ( $this -> pluginTitle , $this -> pluginTitle );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get all field data
* @ return array
*/
public function getFields ()
{
return $this -> fields ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $field
* @ param string $key attribute name
* @ param mixed $default default value if not set , default is null
* @ return mixed
*/
public function getFieldAttr ( $field , $key = null , $default = null )
{
if ( isset ( $this -> fields [ $field ]))
{
if ( null !== $key )
{
return isset ( $this -> fields [ $field ][ $key ]) ? $this -> fields [ $field ][ $key ] : $default ;
}
return $this -> fields [ $field ];
}
return $default ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $field
* @ param string $key attribute name
* @ param mixed $value default value if not set , default is null
* @ return e_admin_controller_ui
*/
public function setFieldAttr ( $field , $key = null , $value = null )
{
// add field array
if ( is_array ( $field ))
{
foreach ( $field as $f => $atts )
{
$this -> setFieldAttr ( $f , $atts );
}
return $this ;
}
// remove a field
if ( null === $key )
{
unset ( $this -> fields [ $field ]);
return $this ;
}
// add to attribute array of a field
if ( is_array ( $key ))
{
foreach ( $key as $k => $att )
{
$this -> setFieldAttr ( $field , $k , $att );
}
return $this ;
}
// remove attribute from field attribute set
if ( null === $value && $key != 'type' )
{
unset ( $this -> fields [ $field ][ $key ]);
return $this ;
}
// set attribute value
$this -> fields [ $field ][ $key ] = $value ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get fields stored as user preferences
* @ return array
*/
public function getFieldPref ()
{
return $this -> fieldpref ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Get Config data array
2009-11-24 16:32:02 +00:00
* @ return array
*/
public function getPrefs ()
{
return $this -> prefs ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function getPerPage ()
{
return $this -> perPage ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function getPrimaryName ()
{
return $this -> getModel () -> getFieldIdName ();
}
2009-12-23 15:12:13 +00:00
public function getDefaultOrderField ()
{
return ( $this -> defaultOrder ? $this -> defaultOrderField : $this -> getPrimaryName ());
}
public function getDefaultOrder ()
{
return ( $this -> defaultOrder ? $this -> defaultOrder : 'asc' );
}
2009-11-24 16:32:02 +00:00
/**
* Get column preference array
* @ return array
*/
public function getUserPref ()
{
2010-11-02 11:41:38 +00:00
//global $user_pref;
// return vartrue($user_pref['admin_cols_'.$this->getTableName()], array());
return e107 :: getUser () -> getPref ( 'admin_cols_' . $this -> getTableName (), array ());
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set column preference array
* @ return boolean success
*/
public function setUserPref ( $new )
{
2010-11-02 11:41:38 +00:00
//global $user_pref;
//e107::getUser()->getConfig()->setData($new);
//$user_pref['admin_cols_'.$this->getTableName()] = $new;
//$this->fieldpref = $new;
//return save_prefs('user');
2009-11-24 16:32:02 +00:00
$this -> fieldpref = $new ;
2010-11-02 11:41:38 +00:00
return e107 :: getUser () -> getConfig ()
-> set ( 'admin_cols_' . $this -> getTableName (), $new )
-> save ();
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current model
*
* @ return e_admin_model
*/
public function getModel ()
{
if ( null === $this -> _model )
{
$this -> _setModel ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this -> _model ;
}
/**
* Set controller model
* @ param e_admin_model $model
* @ return e_admin_controller_ui
*/
public function setModel ( $model )
{
$this -> _model = $model ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get model validation array
* @ return array
*/
public function getValidationRules ()
{
return $this -> getModel () -> getValidationRules ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get model data field array
* @ return array
*/
public function getDataFields ()
{
return $this -> getModel () -> getDataFields ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get model table or alias
* @ param boolean $alias get table alias on true , default false
* @ param object $prefix add e107 special '#' prefix , default false
* @ return string
*/
public function getTableName ( $alias = false , $prefix = false )
{
if ( $alias ) return ( $this -> tableAlias ? $this -> tableAlias : '' );
return ( $prefix ? '#' : '' ) . $this -> getModel () -> getModelTable ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function getIfTableAlias ( $prefix = false , $quote = false )
{
$alias = $this -> getTableName ( true );
if ( $alias )
{
return $alias ;
2009-12-23 15:12:13 +00:00
}
2009-11-24 16:32:02 +00:00
return ( ! $quote ? $this -> getTableName ( false , $prefix ) : '`' . $this -> getTableName ( false , $prefix ) . '`' );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get join table data
* @ param string $table if null all data will be returned
* @ param string $att_name search for specific attribute , default null ( no search )
* @ return mixed
*/
public function getJoinData ( $table = null , $att_name = null , $default_att = null )
{
if ( null === $table )
{
return $this -> tableJoin ;
}
if ( null === $att_name )
{
return ( isset ( $this -> tableJoin [ $table ]) ? $this -> tableJoin [ $table ] : array ());
}
return ( isset ( $this -> tableJoin [ $table ][ $att_name ]) ? $this -> tableJoin [ $table ][ $att_name ] : $default_att );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function setJoinData ( $table , $data )
{
2009-12-23 15:12:13 +00:00
if ( null === $data )
2009-11-24 16:32:02 +00:00
{
unset ( $this -> tableJoin [ $table ]);
return $this ;
}
$this -> tableJoin [ $table ] = ( array ) $data ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined model setter
* @ return e_admin_controller_ui
*/
protected function _setModel ()
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get current tree model
2010-11-04 23:27:47 +00:00
* @ return e_admin_tree_model
2009-11-24 16:32:02 +00:00
*/
public function getTreeModel ()
{
if ( null === $this -> _tree_model )
{
$this -> _setTreeModel ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this -> _tree_model ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set controller tree model
2010-11-04 23:27:47 +00:00
* @ param e_admin_tree_model $tree_model
2009-11-24 16:32:02 +00:00
* @ return e_admin_controller_ui
*/
public function setTreeModel ( $tree_model )
{
$this -> _tree_model = $tree_model ;
return $this ;
}
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
/**
* Get currently parsed model while in list mode
* Model instance is registered by e_form :: renderListForm ()
*
* @ return e_admin_model
*/
public function getListModel ()
{
return e107 :: getRegistry ( 'core/adminUI/currentListModel' );
}
2011-05-18 13:41:19 +00:00
2010-11-08 09:20:12 +00:00
public function setListModel ( $model )
{
e107 :: setRegistry ( 'core/adminUI/currentListModel' , $model );
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined tree model setter
* @ return e_admin_controller_ui
*/
protected function _setTreeModel ()
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Get extended ( UI ) Form instance
*
* @ return e_admin_form_ui
*/
public function getUI ()
{
if ( null === $this -> _ui )
{
$this -> _setUI ();
}
return $this -> _ui ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set controller UI form
* @ param e_admin_form_ui $ui
* @ return e_admin_controller_ui
*/
public function setUI ( $ui )
{
$this -> _ui = $ui ;
return $this ;
}
/**
* User defined UI form setter
* @ return e_admin_controller_ui
*/
protected function _setUI ()
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Get Config object
2009-11-24 16:32:02 +00:00
* @ return e_plugin_pref or e_core_pref when used in core areas
*/
public function getConfig ()
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
if ( null === $this -> _pref )
{
$this -> _setConfig ();
}
return $this -> _pref ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Set Config object
2009-11-24 16:32:02 +00:00
* @ return e_admin_controller_ui
*/
public function setConfig ( $config )
{
$this -> _prefs = $config ;
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined config setter
* @ return e_admin_controller_ui
*/
protected function _setConfig ()
{
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Manage column visibility
* @ param string $batch_trigger
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
public function manageColumns ()
{
$cols = array ();
$posted = $this -> getPosted ( 'e-columns' , array ());
foreach ( $this -> getFields () as $field => $attr )
{
if (( vartrue ( $attr [ 'forced' ]) || in_array ( $field , $posted )) && ! vartrue ( $attr [ 'nolist' ]))
{
$cols [] = $field ;
continue ;
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $cols )
{
$this -> setUserPref ( $cols );
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Handle posted batch options routine
* @ param string $batch_trigger
* @ return e_admin_controller_ui
*/
protected function _handleListBatch ( $batch_trigger )
{
$tp = e107 :: getParser ();
//$multi_name = vartrue($this->fields['checkboxes']['toggle'], 'multiselect');
$multi_name = $this -> getFieldAttr ( 'checkboxes' , 'toggle' , 'multiselect' );
$selected = array_values ( $this -> getPosted ( $multi_name , array ()));
2009-12-23 15:12:13 +00:00
2010-11-04 23:27:47 +00:00
//if(empty($selected)) return $this; - allow empty (no selected) submit for custom batch handlers - e.g. Export CSV
// requires writeParams['batchNoCheck'] == true!!!
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
$selected = array_map ( 'intval' , $selected );
$trigger = $tp -> toDB ( explode ( '__' , $batch_trigger ));
2009-12-23 15:12:13 +00:00
2009-11-28 15:34:46 +00:00
$this -> setTriggersEnabled ( false ); //disable further triggering
2009-11-24 16:32:02 +00:00
switch ( $trigger [ 0 ])
{
case 'delete' : //FIXME - confirmation screen
2010-11-04 23:27:47 +00:00
//method handleListDeleteBatch(); for custom handling of 'delete' batch
// if(empty($selected)) return $this;
// don't check selected data - subclass need to check additional post variables(confirm screen)
2009-11-24 16:32:02 +00:00
$method = 'handle' . $this -> getRequest () -> getActionName () . 'DeleteBatch' ;
if ( method_exists ( $this , $method )) // callback handling
{
$this -> $method ( $selected );
}
break ;
2009-12-23 15:12:13 +00:00
case 'bool' :
2010-11-04 23:27:47 +00:00
if ( empty ( $selected )) return $this ;
2009-11-24 16:32:02 +00:00
$field = $trigger [ 1 ];
$value = $trigger [ 2 ] ? 1 : 0 ;
//something like handleListBoolBatch(); for custom handling of 'bool' batch
$method = 'handle' . $this -> getRequest () -> getActionName () . 'BoolBatch' ;
if ( method_exists ( $this , $method )) // callback handling
{
$this -> $method ( $selected , $field , $value );
break ;
}
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'boolreverse' :
2010-11-04 23:27:47 +00:00
if ( empty ( $selected )) return $this ;
2009-11-24 16:32:02 +00:00
$field = $trigger [ 1 ];
//something like handleListBoolreverseBatch(); for custom handling of 'boolreverse' batch
$method = 'handle' . $this -> getRequest () -> getActionName () . 'BoolreverseBatch' ;
if ( method_exists ( $this , $method )) // callback handling
{
$this -> $method ( $selected , $field );
break ;
}
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
default :
$field = $trigger [ 0 ];
2010-11-04 23:27:47 +00:00
$value = $trigger [ 1 ];
$params = $this -> getFieldAttr ( $field , 'writeParms' , array ());
if ( ! is_array ( $params )) parse_str ( $params , $params );
if ( ! vartrue ( $params [ 'batchNoCheck' ]) && empty ( $selected ))
{
return $this ;
}
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
//something like handleListUrlTypeBatch(); for custom handling of 'url_type' field name
$method = 'handle' . $this -> getRequest () -> getActionName () . $this -> getRequest () -> camelize ( $field ) . 'Batch' ;
if ( method_exists ( $this , $method )) // callback handling
{
$this -> $method ( $selected , $value );
break ;
}
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
//handleListBatch(); for custom handling of all field names
2010-11-04 23:27:47 +00:00
if ( empty ( $selected )) return $this ;
2009-11-24 16:32:02 +00:00
$method = 'handle' . $this -> getRequest () -> getActionName () . 'Batch' ;
if ( method_exists ( $this , $method ))
{
$this -> $method ( $selected , $field , $value );
}
break ;
}
return $this ;
2009-12-23 15:12:13 +00:00
}
2009-11-24 16:32:02 +00:00
/**
* Handle requested filter dropdown value
* @ param string $value
* @ return array field -> value
*/
protected function _parseFilterRequest ( $filter_value )
{
$tp = e107 :: getParser ();
if ( ! $filter_value || $filter_value === '___reset___' )
{
return array ();
}
$filter = $tp -> toDB ( explode ( '__' , $filter_value ));
$res = array ();
switch ( $filter [ 0 ])
{
2009-12-23 15:12:13 +00:00
case 'bool' :
2009-11-24 16:32:02 +00:00
// direct query
$res = array ( $filter [ 1 ], $filter [ 2 ]);
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
default :
//something like handleListUrlTypeFilter(); for custom handling of 'url_type' field name filters
$method = 'handle' . $this -> getRequest () -> getActionName () . $this -> getRequest () -> camelize ( $filter [ 0 ]) . 'Filter' ;
if ( method_exists ( $this , $method )) // callback handling
{
return $this -> $method ( $filter [ 1 ], $selected );
}
else // default handling
{
$res = array ( $filter [ 0 ], $filter [ 1 ]);
}
break ;
}
return $res ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Convert posted to model values after submit ( based on field type )
* @ param array $data
* @ return void
*/
protected function convertToData ( & $data )
{
$model = new e_model ( $data );
foreach ( $this -> getFields () as $key => $attributes )
{
2010-02-12 16:35:43 +00:00
$value = vartrue ( $attributes [ 'dataPath' ]) ? $model -> getData ( $attributes [ 'dataPath' ]) : $model -> get ( $key );
2009-11-24 16:32:02 +00:00
if ( null === $value )
{
2010-02-12 16:35:43 +00:00
continue ;
2009-11-24 16:32:02 +00:00
}
switch ( $attributes [ 'type' ])
{
case 'datestamp' :
if ( ! is_numeric ( $value ))
{
2010-03-01 12:59:26 +00:00
$value = trim ( $value ) ? e107 :: getDateConvert () -> toTime ( $value , 'input' ) : 0 ;
2009-11-24 16:32:02 +00:00
}
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'ip' : // TODO - ask Steve if this check is required
2010-02-12 16:35:43 +00:00
//if(strpos($value, '.') !== FALSE)
2009-11-24 16:32:02 +00:00
{
$value = trim ( $value ) ? e107 :: getInstance () -> ipEncode ( $value ) : '' ;
}
break ;
2011-05-18 13:41:19 +00:00
2010-11-08 09:20:12 +00:00
case 'dropdown' : // TODO - ask Steve if this check is required
case 'lanlist' :
if ( is_array ( $value ))
{
2011-05-18 13:41:19 +00:00
// no sanitize here - data is added to model posted stack
2010-11-08 09:20:12 +00:00
// and validated & sanitized before sent to db
2011-05-18 13:41:19 +00:00
//$value = array_map(array(e107::getParser(), 'toDB'), $value);
$value = implode ( ',' , $value );
2010-11-08 09:20:12 +00:00
}
break ;
2009-11-24 16:32:02 +00:00
}
2010-02-12 16:35:43 +00:00
if ( vartrue ( $attributes [ 'dataPath' ]))
{
$model -> setData ( $attributes [ 'dataPath' ], $value );
}
else
{
$model -> set ( $key , $value );
}
2010-03-01 12:59:26 +00:00
2009-11-24 16:32:02 +00:00
}
2010-03-01 12:59:26 +00:00
2010-02-12 16:35:43 +00:00
$data = $model -> getData ();
2009-12-24 22:36:34 +00:00
unset ( $model );
2009-11-24 16:32:02 +00:00
$this -> toData ( $data );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined method for converting POSTED to MODEL data
* @ param array $data posted data
* @ param string $type current action type - edit , create , list or user defined
* @ return void
*/
protected function toData ( & $data , $type = '' )
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Take approproate action after successfull submit
*
* @ param integer $id optional , needed only if redirect action is 'edit'
* @ param string $noredirect_for don ' t redirect if action equals to its value
*/
protected function doAfterSubmit ( $id = 0 , $noredirect_for = '' )
{
if ( $noredirect_for && $noredirect_for == $this -> getPosted ( '__after_submit_action' ) && $noredirect_for == $this -> getAction ())
{
2009-12-23 15:12:13 +00:00
return ;
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$choice = $this -> getPosted ( '__after_submit_action' , 0 );
switch ( $choice ) {
case 'create' : // create
$this -> redirectAction ( 'create' , 'id' );
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'edit' : // edit
$this -> redirectAction ( 'edit' , '' , 'id=' . $id );
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'list' : // list
$this -> redirectAction ( 'list' , 'id' );
break ;
default :
$choice = explode ( '|' , str_replace ( '{ID}' , $id , $choice ), 3 );
$this -> redirectAction ( preg_replace ( '/[^\w\-]/' , '' , $choice [ 0 ]), vartrue ( $choice [ 1 ]), vartrue ( $choice [ 2 ]));
break ;
}
return ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Build ajax auto - complete filter response
* @ return string response markup
*/
protected function renderAjaxFilterResponse ( $listQry = '' )
{
2010-03-01 12:59:26 +00:00
$debug = false ;
2009-11-24 16:32:02 +00:00
$srch = $this -> getPosted ( 'searchquery' );
$this -> getRequest () -> setQuery ( 'searchquery' , $srch ); //_modifyListQry() is requiring GET String
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$ret = '<ul>' ;
2010-11-04 23:27:47 +00:00
$ret .= '<li>' . $srch . '<span class="informal warning"> ' . LAN_FILTER_LABEL_TYPED . '</span></li>' ; // fix Enter - search for typed word only
2009-11-24 16:32:02 +00:00
$reswords = array ();
if ( trim ( $srch ) !== '' )
{
// Build query
$qry = $this -> _modifyListQry ( false , true , 0 , 20 , $listQry );
2010-11-04 23:27:47 +00:00
//file_put_contents(e_LOG.'uiAjaxResponseSQL.log', $qry."\n\n", FILE_APPEND);
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
// Make query
$sql = e107 :: getDb ();
2010-03-01 12:59:26 +00:00
if ( $qry && $sql -> db_Select_gen ( $qry , $debug ))
2009-11-24 16:32:02 +00:00
{
while ( $res = $sql -> db_Fetch ())
{
$tmp1 = array ();
$tmp = array_values ( preg_grep ( '#' . $srch . '#i' , $res ));
foreach ( $tmp as $w )
{
2009-12-23 15:12:13 +00:00
if ( $w == $srch )
2009-11-24 16:32:02 +00:00
{
array_unshift ( $reswords , $w ); //exact match
continue ;
}
preg_match ( '#[\S]*(' . $srch . ')[\S]*#i' , $w , $tmp1 );
if ( $tmp1 [ 0 ]) $reswords [] = $tmp1 [ 0 ];
}
}
}
2009-12-23 15:12:13 +00:00
// Build response
2009-11-24 16:32:02 +00:00
$reswords = array_unique ( $reswords );
if ( $reswords )
{
$ret .= '<li>' . implode ( " </li> \n \t <li> " , $reswords ) . '</li>' ;
}
}
2009-12-23 15:12:13 +00:00
2010-11-04 23:27:47 +00:00
$ret .= '<li><span class="informal warning"> ' . LAN_FILTER_LABEL_CLEAR . ' </span></li>' ; // clear filter option
2009-11-24 16:32:02 +00:00
$ret .= '</ul>' ;
return $ret ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Parses all available field data , adds internal attributes for handling join requests
2009-11-24 16:32:02 +00:00
* @ return e_admin_controller_ui
*/
protected function parseAliases ()
{
2010-11-08 09:20:12 +00:00
if ( $this -> _alias_parsed ) return $this ; // already parsed!!!
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
if ( $this -> getJoinData ())
{
foreach ( $this -> getJoinData () as $table => $att )
{
if ( strpos ( $table , '.' ) !== false )
{
$tmp = explode ( '.' , $table , 2 );
$this -> setJoinData ( $table , null );
$att [ 'alias' ] = $tmp [ 0 ];
$att [ 'table' ] = $tmp [ 1 ];
$att [ '__tablePath' ] = $att [ 'alias' ] . '.' ;
$att [ '__tableFrom' ] = '`#' . $att [ 'table' ] . '` AS ' . $att [ 'alias' ];
2011-05-18 13:41:19 +00:00
$this -> setJoinData ( $att [ 'alias' ], $att );
2009-11-24 16:32:02 +00:00
unset ( $tmp );
continue ;
}
$att [ 'table' ] = $table ;
$att [ 'alias' ] = '' ;
$att [ '__tablePath' ] = '`#' . $att [ 'table' ] . '`.' ;
$att [ '__tableFrom' ] = '`#' . $att [ 'table' ] . '`' ;
$this -> setJoinData ( $table , $att );
}
}
2011-05-18 13:41:19 +00:00
2011-05-07 06:22:44 +00:00
$this -> joinAlias (); // generate Table Aliases from listQry
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
// check for table & field aliases
$fields = array (); // preserve order
foreach ( $this -> fields as $field => $att )
{
2009-12-23 15:12:13 +00:00
// tableAlias.fieldName.fieldAlias
2011-05-18 13:41:19 +00:00
if ( strpos ( $field , '.' ) !== false ) // manually entered alias.
2009-11-24 16:32:02 +00:00
{
$tmp = explode ( '.' , $field , 3 );
$att [ 'table' ] = $tmp [ 0 ] ? $tmp [ 0 ] : $this -> getIfTableAlias ( false );
$att [ 'alias' ] = vartrue ( $tmp [ 2 ]);
$att [ 'field' ] = $tmp [ 1 ];
$field = $att [ 'alias' ] ? $att [ 'alias' ] : $tmp [ 1 ];
2009-12-23 15:12:13 +00:00
$fields [ $field ] = $att ;
2009-11-24 16:32:02 +00:00
unset ( $tmp );
}
else
{
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
$att [ 'table' ] = $this -> getIfTableAlias ( false );
2011-05-07 06:22:44 +00:00
if ( isset ( $this -> joinAlias [ $this -> table ]) && $field != 'checkboxes' && $field != 'options' )
{
2011-05-18 13:41:19 +00:00
$att [ 'alias' ] = $this -> joinAlias [ $this -> table ] . " . " . $field ;
2011-05-07 06:22:44 +00:00
}
else
{
2011-05-18 13:41:19 +00:00
$att [ 'alias' ] = " " ;
2011-05-07 06:22:44 +00:00
}
2009-11-24 16:32:02 +00:00
$att [ 'field' ] = $field ;
$fields [ $field ] = $att ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $fields [ $field ][ 'table' ] == $this -> getIfTableAlias ( false ))
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
$fields [ $field ][ '__tableField' ] = $att [ 'alias' ] ? $att [ 'alias' ] : $this -> getIfTableAlias ( true , true ) . '.' . $att [ 'field' ];
$fields [ $field ][ '__tableFrom' ] = $this -> getIfTableAlias ( true , true ) . '.' . $att [ 'field' ] . ( $att [ 'alias' ] ? ' AS ' . $att [ 'alias' ] : '' );
}
else
{
$fields [ $field ][ '__tableField' ] = $this -> getJoinData ( $fields [ $field ][ 'table' ], '__tablePath' ) . $field ;
}
/* if ( $fields [ $field ][ 'table' ])
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
if ( $fields [ $field ][ 'table' ] == $this -> getIfTableAlias ( false ))
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
$fields [ $field ][ '__tableField' ] = $att [ 'alias' ] ? $att [ 'alias' ] : $this -> getIfTableAlias ( true , true ) . '.' . $att [ 'field' ];
$fields [ $field ][ '__tableFrom' ] = $this -> getIfTableAlias ( true , true ) . '.' . $att [ 'field' ] . ( $att [ 'alias' ] ? ' AS ' . $att [ 'alias' ] : '' );
}
else
{
$fields [ $field ][ '__tableField' ] = $this -> getJoinData ( $fields [ $field ][ 'table' ], '__tablePath' ) . $field ;
}
}
else
{
$fields [ $field ][ '__tableField' ] = '`' . $this -> getTableName ( false , true ) . '`.' . $field ;
} */
}
2011-05-07 06:22:44 +00:00
2009-11-24 16:32:02 +00:00
$this -> fields = $fields ;
2010-11-08 09:20:12 +00:00
$this -> _alias_parsed = true ;
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2011-05-07 06:22:44 +00:00
/**
* Intuitive LEFT JOIN Qry support . ( preferred )
* Generate array of table names and their alias - auto - detected from listQry ;
2011-05-18 13:41:19 +00:00
* eg . $listQry = " SELECT m.*, u.user_id,u.user_name FROM #core_media AS m LEFT JOIN #user AS u ON m.media_author = u.user_id " ;
2011-05-07 06:22:44 +00:00
*/
protected function joinAlias ()
{
//TODO - editQry
2011-05-18 13:41:19 +00:00
// TODO - auto-detect fields that belong to other tables. eg. u.user_id,u.user_name and adjust query to suit.
if ( $this -> listQry )
2011-05-07 06:22:44 +00:00
{
preg_match_all ( " /`?#([ \ w-]+)`? \ s*(as|AS) \ s*([ \ w-])/im " , $this -> listQry , $matches );
2011-05-18 13:41:19 +00:00
2011-05-07 06:22:44 +00:00
foreach ( $matches [ 1 ] AS $k => $v )
{
if ( varset ( $matches [ 3 ][ $k ]))
{
$this -> joinAlias [ $v ] = $matches [ 3 ][ $k ]; // array. eg $this->joinAlias['core_media'] = 'm';
2011-05-18 13:41:19 +00:00
}
2011-05-07 06:22:44 +00:00
}
}
2011-05-18 13:41:19 +00:00
2011-05-07 06:22:44 +00:00
}
2009-11-24 16:32:02 +00:00
// TODO - abstract, array return type, move to parent?
protected function _modifyListQry ( $raw = false , $isfilter = false , $forceFrom = false , $forceTo = false , $listQry = '' )
{
$searchQry = array ();
$filterFrom = array ();
$request = $this -> getRequest ();
$tp = e107 :: getParser ();
$tablePath = $this -> getIfTableAlias ( true , true ) . '.' ;
$tableFrom = '`' . $this -> getTableName ( false , true ) . '`' . ( $this -> getTableName ( true ) ? ' AS ' . $this -> getTableName ( true ) : '' );
$tableSFieldsArr = array (); // FROM for main table
$tableSJoinArr = array (); // FROM for join tables
$filter = array ();
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
$searchQuery = $tp -> toDB ( $request -> getQuery ( 'searchquery' , '' ));
$searchFilter = $this -> _parseFilterRequest ( $request -> getQuery ( 'filter_options' , '' ));
list ( $filterField , $filterValue ) = $searchFilter ;
if ( $filterField && $filterValue !== '' && isset ( $this -> fields [ $filterField ]))
{
$searchQry [] = $this -> fields [ $filterField ][ '__tableField' ] . " = ' " . $filterValue . " ' " ;
}
2011-05-07 06:22:44 +00:00
2009-11-24 16:32:02 +00:00
// main table should select everything
$tableSFieldsArr [] = $tablePath . '*' ;
foreach ( $this -> getFields () as $key => $var )
{
// disabled or system
2010-11-04 23:27:47 +00:00
if (( vartrue ( $var [ 'nolist' ]) && ! vartrue ( $var [ 'filter' ])) || null === $var [ 'type' ])
2009-11-24 16:32:02 +00:00
{
continue ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// select FROM... for main table
if ( $var [ 'alias' ] && vartrue ( $var [ '__tableFrom' ]))
{
$tableSFieldsArr [] = $var [ '__tableFrom' ];
}
2009-12-23 15:12:13 +00:00
// filter for WHERE and FROM clauses
2010-10-19 01:04:20 +00:00
$searchable_types = array ( 'text' , 'textarea' , 'bbarea' , 'user' , 'email' ); //method?
2009-11-24 16:32:02 +00:00
if ( trim ( $searchQuery ) !== '' && in_array ( $var [ 'type' ], $searchable_types ))
{
2009-12-23 15:12:13 +00:00
$filter [] = $var [ '__tableField' ] . " REGEXP (' " . $searchQuery . " ') " ;
2009-11-24 16:32:02 +00:00
if ( $isfilter )
{
$filterFrom [] = $var [ '__tableField' ];
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
}
}
}
2009-12-23 15:12:13 +00:00
2011-05-07 06:22:44 +00:00
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
if ( $isfilter )
{
if ( ! $filterFrom ) return false ;
$tableSFields = implode ( ', ' , $filterFrom );
}
else
{
$tableSFields = $tableSFieldsArr ? implode ( ', ' , $tableSFieldsArr ) : $tablePath . '*' ;
}
2009-12-23 15:12:13 +00:00
2010-03-01 12:59:26 +00:00
2009-11-24 16:32:02 +00:00
$jwhere = array ();
$joins = array ();
2010-11-04 23:27:47 +00:00
//file_put_contents(e_LOG.'uiAjaxResponseSFields.log', $tableSFields."\n\n", FILE_APPEND);
//file_put_contents(e_LOG.'uiAjaxResponseFields.log', print_r($this->getFields(), true)."\n\n", FILE_APPEND);
2009-12-23 15:12:13 +00:00
if ( $this -> getJoinData ())
2009-11-24 16:32:02 +00:00
{
$qry = " SELECT SQL_CALC_FOUND_ROWS " . $tableSFields ;
foreach ( $this -> getJoinData () as $jtable => $tparams )
{
// Select fields
if ( ! $isfilter )
{
$fields = vartrue ( $tparams [ 'fields' ]);
if ( '*' === $fields )
{
$tableSJoinArr [] = " { $tparams [ '__tablePath' ] } * " ;
}
else
{
$tableSJoinArr [] = $fields ;
/* $fields = explode ( ',' , $fields );
foreach ( $fields as $field )
{
$qry .= " , { $tparams [ '__tablePath' ] } ` " . trim ( $field ) . '`' ;
} */
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Prepare Joins
$joins [] = "
" .vartrue( $tparams['joinType'] , 'LEFT JOIN'). " { $tparams [ '__tableFrom' ]} ON " .(vartrue( $tparams['leftTable'] ) ? $tparams['leftTable'] .'.' : $tablePath ). " `".vartrue($tparams['leftField'])."` = { $tparams [ '__tablePath' ]} `".vartrue($tparams['rightField'])."` " .(vartrue( $tparams['whereJoin'] ) ? ' '. $tparams['whereJoin'] : '');
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Prepare Where
if ( vartrue ( $tparams [ 'where' ]))
{
$jwhere [] = $tparams [ 'where' ];
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
//From
2010-03-01 12:59:26 +00:00
$qry .= $tableSJoinArr ? ', ' . implode ( ', ' , $tableSJoinArr ) . " FROM " . $tableFrom : " FROM " . $tableFrom ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Joins
if ( count ( $joins ) > 0 )
{
$qry .= " \n " . implode ( " \n " , $joins );
}
}
else
{
$qry = $listQry ? $listQry : " SELECT SQL_CALC_FOUND_ROWS " . $tableSFields . " FROM " . $tableFrom ;
}
2011-05-18 13:41:19 +00:00
2010-11-16 10:13:44 +00:00
// group field - currently auto-added only if there are joins
2011-05-18 13:41:19 +00:00
// TODO - groupField property
2010-11-16 10:13:44 +00:00
$groupField = '' ;
if ( $joins && $this -> getPrimaryName ())
{
$groupField = $tablePath . $this -> getPrimaryName ();
}
2009-11-24 16:32:02 +00:00
if ( $raw )
{
2010-12-27 12:38:14 +00:00
$rawData = array (
2011-05-18 13:41:19 +00:00
'joinWhere' => $jwhere ,
'filter' => $filter ,
2010-12-27 12:38:14 +00:00
'listQrySql' => $this -> listQrySql ,
2011-05-18 13:41:19 +00:00
'filterFrom' => $filterFrom ,
'search' => $searchQry ,
2010-12-27 12:38:14 +00:00
'tableFromName' => $tableFrom ,
);
2009-11-24 16:32:02 +00:00
$rawData [ 'tableFrom' ] = $tableSFieldsArr ;
$rawData [ 'joinsFrom' ] = $tableSJoinArr ;
$rawData [ 'joins' ] = $joins ;
2010-11-16 10:13:44 +00:00
$rawData [ 'groupField' ] = $groupField ;
2009-11-24 16:32:02 +00:00
$rawData [ 'orderField' ] = isset ( $this -> fields [ $orderField ]) ? $this -> fields [ $orderField ][ '__tableField' ] : '' ;
$rawData [ 'orderType' ] = $request -> getQuery ( 'asc' ) == 'desc' ? 'DESC' : 'ASC' ;
$rawData [ 'limitFrom' ] = false === $forceFrom ? intval ( $request -> getQuery ( 'from' , 0 )) : intval ( $forceFrom );
$rawData [ 'limitTo' ] = false === $forceTo ? intval ( $this -> getPerPage ()) : intval ( $forceTo );
return $rawData ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// join where
if ( count ( $jwhere ) > 0 )
{
$searchQry [] = " ( " . implode ( " AND " , $jwhere ) . " ) " ;
}
// filter where
if ( count ( $filter ) > 0 )
{
$searchQry [] = " ( " . implode ( " OR " , $filter ) . " ) " ;
}
2011-05-18 13:41:19 +00:00
2010-12-27 12:38:14 +00:00
// more user added sql
if ( isset ( $this -> listQrySql [ 'db_where' ]) && $this -> listQrySql [ 'db_where' ])
{
if ( is_array ( $this -> listQrySql [ 'db_where' ]))
{
$searchQry [] = implode ( " AND " , $this -> listQrySql [ 'db_where' ]);
}
else
{
$searchQry [] = $this -> listQrySql [ 'db_where' ];
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// where query
if ( count ( $searchQry ) > 0 )
{
$qry .= " WHERE " . implode ( " AND " , $searchQry );
}
2011-05-18 13:41:19 +00:00
2010-11-16 10:13:44 +00:00
// GROUP BY if needed
if ( $groupField )
{
$qry .= ' GROUP BY ' . $groupField ;
}
2009-12-23 15:12:13 +00:00
2010-11-15 09:02:16 +00:00
// only when no custom order is required
if ( $this -> listOrder && ! $request -> getQuery ( 'field' ) && ! $request -> getQuery ( 'asc' ))
2009-11-24 16:32:02 +00:00
{
2009-12-23 15:12:13 +00:00
$qry .= ' ORDER BY ' . $this -> listOrder ;
}
elseif ( false !== $this -> listOrder )
{
$orderField = $request -> getQuery ( 'field' , $this -> getDefaultOrderField ());
$orderDef = ( null === $request -> getQuery ( 'asc' , null ) ? $this -> getDefaultOrder () : $request -> getQuery ( 'asc' ));
if ( isset ( $this -> fields [ $orderField ]) && strpos ( $this -> listQry , 'ORDER BY' ) == FALSE ) //override ORDER using listQry (admin->sitelinks)
{
// no need of sanitize - it's found in field array
$qry .= ' ORDER BY ' . $this -> fields [ $orderField ][ '__tableField' ] . ' ' . ( strtolower ( $orderDef ) == 'desc' ? 'DESC' : 'ASC' );
}
2009-11-24 16:32:02 +00:00
}
if ( $this -> getPerPage () || false !== $forceTo )
{
$from = false === $forceFrom ? intval ( $request -> getQuery ( 'from' , 0 )) : intval ( $forceFrom );
if ( false === $forceTo ) $forceTo = $this -> getPerPage ();
$qry .= ' LIMIT ' . $from . ', ' . intval ( $forceTo );
}
2011-05-18 13:41:19 +00:00
// Debug Filter Query.
2011-05-07 06:22:44 +00:00
// echo $qry;
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
return $qry ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Manage submit item
* Note : $callbackBefore will break submission if returns false
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ param string $callbackBefore existing method from $this scope to be called before submit
* @ param string $callbackAfter existing method from $this scope to be called after successfull submit
* @ param string $noredirectAction passed to doAfterSubmit ()
2009-12-13 21:52:32 +00:00
* @ return boolean
2009-11-24 16:32:02 +00:00
*/
2009-11-28 15:34:46 +00:00
protected function _manageSubmit ( $callbackBefore = '' , $callbackAfter = '' , $callbackError = '' , $noredirectAction = '' )
2009-11-24 16:32:02 +00:00
{
$model = $this -> getModel ();
$old_data = $model -> getData ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$_posted = $this -> getPosted ();
$this -> convertToData ( $_posted );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( $callbackBefore && method_exists ( $this , $callbackBefore ))
{
$data = $this -> $callbackBefore ( $_posted , $old_data , $model -> getId ());
if ( false === $data )
{
// we don't wanna loose posted data
$model -> setPostedData ( $_posted , null , false , false );
return false ;
}
if ( $data && is_array ( $data ))
{
// add to model data fields array if required
foreach ( $data as $f => $val )
{
if ( $this -> getFieldAttr ( $f , 'data' ))
{
$model -> setDataField ( $f , $this -> getFieldAttr ( $f , 'data' ));
}
}
$_posted = array_merge ( $_posted , $data );
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Scenario I - use request owned POST data - toForm already executed
$model -> setPostedData ( $_posted , null , false , false )
-> save ( true );
// Scenario II - inner model sanitize
//$this->getModel()->setPosted($this->convertToData($_POST, null, false, true);
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// Take action based on use choice after success
if ( ! $this -> getModel () -> hasError ())
{
// callback (if any)
2009-12-23 15:12:13 +00:00
if ( $callbackAfter && method_exists ( $this , $callbackAfter ))
2009-11-24 16:32:02 +00:00
{
$this -> $callbackAfter ( $model -> getData (), $old_data , $model -> getId ());
}
2010-10-26 07:41:20 +00:00
$model -> setMessages ( true ); //FIX - move messages (and session messages) to the default stack
2009-11-24 16:32:02 +00:00
$this -> doAfterSubmit ( $model -> getId (), $noredirectAction );
return true ;
}
2009-11-28 15:34:46 +00:00
elseif ( $callbackError && method_exists ( $this , $callbackError ))
{
// suppress messages if callback returns TRUE
if ( true !== $this -> $callbackError ( $_posted , $old_data , $model -> getId ()))
{
// Copy model messages to the default message stack
$model -> setMessages ();
}
return false ;
}
2009-12-23 15:12:13 +00:00
2009-11-28 15:34:46 +00:00
// Copy model messages to the default message stack
$model -> setMessages ();
2009-11-24 16:32:02 +00:00
return false ;
}
}
class e_admin_ui extends e_admin_controller_ui
{
protected $fieldTypes = array ();
protected $dataFields = array ();
protected $validationRules = array ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
protected $table ;
protected $pid ;
protected $listQry ;
protected $editQry ;
2011-05-18 13:41:19 +00:00
2010-10-30 15:34:48 +00:00
/**
* Markup to be auto - inserted before List filter
* @ var string
*/
public $preFiliterMarkup = '' ;
2011-05-18 13:41:19 +00:00
2010-10-30 15:34:48 +00:00
/**
* Markup to be auto - inserted after List filter
* @ var string
*/
public $postFiliterMarkup = '' ;
2011-05-18 13:41:19 +00:00
2010-10-30 15:34:48 +00:00
/**
* Markup to be auto - inserted at the top of Create form
* @ var string
*/
public $headerCreateMarkup = '' ;
2011-05-18 13:41:19 +00:00
2010-10-30 15:34:48 +00:00
/**
* Markup to be auto - inserted at the bottom of Create form
* @ var string
*/
public $footerCreateMarkup = '' ;
2011-05-18 13:41:19 +00:00
2010-10-30 15:34:48 +00:00
/**
* Markup to be auto - inserted at the top of Update form
* @ var string
*/
public $headerUpdateMarkup = '' ;
2011-05-18 13:41:19 +00:00
2010-10-30 15:34:48 +00:00
/**
* Markup to be auto - inserted at the bottom of Update form
* @ var string
*/
public $footerUpdateMarkup = '' ;
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
/**
* Show confirm screen before ( batch / single ) delete
* @ var boolean
*/
public $deleteConfirmScreen = false ;
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Constructor
2009-11-24 16:32:02 +00:00
* @ param e_admin_request $request
* @ param e_admin_response $response
* @ param array $params [ optional ]
*/
public function __construct ( $request , $response , $params = array ())
{
2009-12-23 15:12:13 +00:00
$this -> setDefaultAction ( $request -> getDefaultAction ());
2009-11-24 16:32:02 +00:00
$params [ 'enable_triggers' ] = true ; // override
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
parent :: __construct ( $request , $response , $params );
if ( ! $this -> pluginName )
{
$this -> pluginName = 'core' ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$ufieldpref = $this -> getUserPref ();
if ( $ufieldpref )
{
$this -> fieldpref = $ufieldpref ;
}
2009-12-23 15:12:13 +00:00
$this -> addTitle ( $this -> pluginTitle , true ) -> parseAliases ();
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Catch fieldpref submit
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
public function ListEcolumnsTrigger ()
{
2009-11-28 15:34:46 +00:00
$this -> setTriggersEnabled ( false ); //disable further triggering
2009-11-24 16:32:02 +00:00
parent :: manageColumns ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Catch batch submit
* @ param string $batch_trigger
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
public function ListBatchTrigger ( $batch_trigger )
{
2009-12-23 15:12:13 +00:00
$this -> setPosted ( 'etrigger_batch' , null );
2011-05-18 13:41:19 +00:00
if ( $this -> getPosted ( 'etrigger_cancel' ))
{
2010-11-04 23:27:47 +00:00
$this -> setPosted ( array ());
return ; // always break on cancel!
}
$this -> deleteConfirmScreen = true ; // Confirm screen ALWAYS enabled when multi-deleting!
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
// proceed ONLY if there is no other trigger, except delete confirmation
if ( $batch_trigger && ! $this -> hasTrigger ( array ( 'etrigger_delete_confirm' ))) $this -> _handleListBatch ( $batch_trigger );
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Batch delete trigger
* @ param array $selected
* @ return void
*/
protected function handleListDeleteBatch ( $selected )
{
if ( ! $this -> getBatchDelete ())
{
2010-11-04 23:27:47 +00:00
e107 :: getMessage () -> add ( LAN_UI_BATCHDEL_ERROR , E_MESSAGE_WARNING );
2009-11-24 16:32:02 +00:00
return ;
2009-12-23 15:12:13 +00:00
}
2010-11-04 23:27:47 +00:00
if ( $this -> deleteConfirmScreen )
{
if ( ! $this -> getPosted ( 'etrigger_delete_confirm' ))
{
// ListPage will show up confirmation screen
$this -> setPosted ( 'delete_confirm_value' , implode ( ',' , $selected ));
return ;
}
else
{
// already confirmed, resurrect selected values
$selected = array_map ( 'intval' , explode ( ',' , $this -> getPosted ( 'delete_confirm_value' )));
}
}
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
// delete one by one - more control, less performance
// TODO - pass afterDelete() callback to tree delete method?
2009-11-26 17:14:07 +00:00
$set_messages = true ;
2009-11-24 16:32:02 +00:00
foreach ( $selected as $id )
{
2009-11-26 17:14:07 +00:00
$data = array ();
$model = $this -> getTreeModel () -> getNode ( $id );
if ( $model )
2009-11-24 16:32:02 +00:00
{
2009-11-26 17:14:07 +00:00
$data = $model -> getData ();
if ( $this -> beforeDelete ( $data , $id ))
2009-11-24 16:32:02 +00:00
{
2009-11-26 17:14:07 +00:00
$check = $this -> getTreeModel () -> delete ( $id );
if ( ! $this -> afterDelete ( $data , $id , $check ))
{
$set_messages = false ;
}
2009-11-24 16:32:02 +00:00
}
}
}
//$this->getTreeModel()->delete($selected);
2009-11-26 17:14:07 +00:00
if ( $set_messages ) $this -> getTreeModel () -> setMessages ();
2010-11-04 23:27:47 +00:00
$this -> redirect ();
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2011-07-25 02:41:49 +00:00
/** TODO
* Batch copy trigger
* @ param array $selected
* @ return void
*/
protected function handleListCopyBatch ( $selected )
{
// Batch Copy
$set_messages = true ;
$this -> getTreeModel () -> copy ( $selected );
if ( $set_messages ) $this -> getTreeModel () -> setMessages ();
$this -> redirect ();
}
2009-11-24 16:32:02 +00:00
/**
* Batch boolean trigger
* @ param array $selected
* @ return void
*/
protected function handleListBoolBatch ( $selected , $field , $value )
{
$cnt = $this -> getTreeModel () -> update ( $field , $value , $selected , $value , false );
if ( $cnt )
{
2010-11-04 23:27:47 +00:00
$this -> getTreeModel () -> addMessageSuccess ( sprintf ( LAN_UI_BATCH_BOOL_SUCCESS , $cnt ));
2009-11-24 16:32:02 +00:00
}
$this -> getTreeModel () -> setMessages ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Batch boolean reverse trigger
* @ param array $selected
* @ return void
*/
protected function handleListBoolreverseBatch ( $selected , $field , $value )
{
$tree = $this -> getTreeModel ();
$cnt = $tree -> update ( $field , " 1- { $field } " , $selected , null , false );
if ( $cnt )
{
2010-11-04 23:27:47 +00:00
$tree -> addMessageSuccess ( sprintf ( LAN_UI_BATCH_REVERSED_SUCCESS , $cnt ));
2009-11-24 16:32:02 +00:00
//sync models
$tree -> load ( true );
}
$this -> getTreeModel () -> setMessages ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Batch default ( field ) trigger
* @ param array $selected
* @ return void
*/
protected function handleListBatch ( $selected , $field , $value )
{
$cnt = $this -> getTreeModel () -> update ( $field , " ' " . $value . " ' " , $selected , $value , false );
if ( $cnt )
2009-12-23 15:12:13 +00:00
{
$vttl = $this -> getUI () -> renderValue ( $field , $value , $this -> getFieldAttr ( $field ));
2010-11-04 23:27:47 +00:00
$this -> getTreeModel () -> addMessageSuccess ( sprintf ( LAN_UI_BATCH_UPDATE_SUCCESS , $vttl , $cnt ));
2009-11-24 16:32:02 +00:00
}
$this -> getTreeModel () -> setMessages ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Catch delete submit
* @ param string $batch_trigger
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
public function ListDeleteTrigger ( $posted )
{
2011-05-18 13:41:19 +00:00
if ( $this -> getPosted ( 'etrigger_cancel' ))
{
2010-11-04 23:27:47 +00:00
$this -> setPosted ( array ());
return ; // always break on cancel!
}
2011-05-18 13:41:19 +00:00
// TODO - investigate - strange post vale of delete triggers, switched to key
// for quick fix
$id = intval ( key ( $posted )); //intval(array_shift($posted));
2010-11-04 23:27:47 +00:00
if ( $this -> deleteConfirmScreen && ! $this -> getPosted ( 'etrigger_delete_confirm' ))
{
// forward data to delete confirm screen
$this -> setPosted ( 'delete_confirm_value' , $id );
return ; // User confirmation expected
}
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
$this -> setTriggersEnabled ( false );
2009-11-24 16:32:02 +00:00
$data = array ();
$model = $this -> getTreeModel () -> getNode ( $id );
if ( $model )
{
$data = $model -> getData ();
2009-11-26 17:14:07 +00:00
if ( $this -> beforeDelete ( $data , $id ))
2009-11-24 16:32:02 +00:00
{
2009-11-26 17:14:07 +00:00
$check = $this -> getTreeModel () -> delete ( $id );
if ( $this -> afterDelete ( $data , $id , $check ))
{
$this -> getTreeModel () -> setMessages ();
}
2009-11-24 16:32:02 +00:00
}
2009-12-12 16:40:41 +00:00
else
{
$this -> getTreeModel () -> setMessages (); // errors
}
2009-11-24 16:32:02 +00:00
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined pre - delete logic
*/
public function beforeDelete ( $data , $id )
{
return true ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-11-26 17:14:07 +00:00
* User defined after - delete logic
2009-11-24 16:32:02 +00:00
*/
2009-11-26 17:14:07 +00:00
public function afterDelete ( $deleted_data , $id , $deleted_check )
2009-11-24 16:32:02 +00:00
{
2009-11-26 17:14:07 +00:00
return true ;
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* List action header
* @ return void
*/
public function ListHeader ()
{
e107 :: getJs () -> headerCore ( 'core/tabs.js' )
-> headerCore ( 'core/admin.js' );
}
/**
* List action observer
* @ return void
*/
public function ListObserver ()
{
$this -> getTreeModel () -> setParam ( 'db_query' , $this -> _modifyListQry ( false , false , false , false , $this -> listQry )) -> load ();
$this -> addTitle ( 'List' ); // FIXME - get captions from dispatch list
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Filter response ajax page
* @ return string
*/
public function FilterAjaxPage ()
{
return $this -> renderAjaxFilterResponse ( $this -> listQry ); //listQry will be used only if available
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic List action page
* @ return string
*/
public function ListPage ()
{
2010-11-04 23:27:47 +00:00
if ( $this -> deleteConfirmScreen && ! $this -> getPosted ( 'etrigger_delete_confirm' ) && $this -> getPosted ( 'delete_confirm_value' ))
{
// 'edelete_confirm_data' set by single/batch delete trigger
return $this -> getUI () -> getConfirmDelete ( $this -> getPosted ( 'delete_confirm_value' )); // User confirmation expected
}
2009-11-24 16:32:02 +00:00
return $this -> getUI () -> getList ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* List action observer
* @ return void
*/
public function ListAjaxObserver ()
{
$this -> getTreeModel () -> setParam ( 'db_query' , $this -> _modifyListQry ( false , false , 0 , false , $this -> listQry )) -> load ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic List action page ( Ajax )
* @ return string
*/
public function ListAjaxPage ()
{
return $this -> getUI () -> getList ( true );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Edit observer
*/
public function EditObserver ()
{
$this -> getModel () -> load ( $this -> getId ());
$this -> addTitle ( LAN_UPDATE , true );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Create submit trigger
*/
public function EditCancelTrigger ()
{
$this -> redirectAction ( 'list' , 'id' );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Edit submit trigger
*/
public function EditSubmitTrigger ()
{
2009-11-28 15:34:46 +00:00
$this -> _manageSubmit ( 'beforeUpdate' , 'afterUpdate' , 'onUpdateError' , 'edit' );
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Edit - send JS to page Header
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
function EditHeader ()
{
e107 :: getJs () -> requireCoreLib ( 'core/admin.js' );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Edit page
* @ return string
*/
public function EditPage ()
{
return $this -> CreatePage ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Create observer
* @ return string
*/
public function CreateObserver ()
{
2009-11-28 15:34:46 +00:00
$this -> setTriggersEnabled ( true );
2009-11-24 16:32:02 +00:00
$this -> addTitle ( LAN_CREATE , true );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Create submit trigger
*/
public function CreateCancelTrigger ()
{
$this -> redirectAction ( 'list' , 'id' );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Generic Create submit trigger
*/
public function CreateSubmitTrigger ()
{
2009-11-28 15:34:46 +00:00
$this -> _manageSubmit ( 'beforeCreate' , 'afterCreate' , 'onCreateError' );
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-11-28 15:34:46 +00:00
* User defined pre - create logic , return false to prevent DB query execution
2009-11-24 16:32:02 +00:00
*/
public function beforeCreate ( $new_data , $old_data )
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined after - create logic
*/
public function afterCreate ( $new_data , $old_data , $id )
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-11-28 15:34:46 +00:00
* User defined error handling , return true to suppress model messages
*/
public function onCreateError ( $new_data , $old_data )
{
}
2009-12-23 15:12:13 +00:00
2009-11-28 15:34:46 +00:00
/**
* User defined pre - update logic , return false to prevent DB query execution
2009-11-24 16:32:02 +00:00
*/
public function beforeUpdate ( $new_data , $old_data , $id )
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined after - update logic
*/
public function afterUpdate ( $new_data , $old_data , $id )
{
}
2009-12-23 15:12:13 +00:00
2009-11-28 15:34:46 +00:00
/**
* User defined error handling , return true to suppress model messages
*/
public function onUpdateError ( $new_data , $old_data , $id )
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Create - send JS to page Header
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-24 16:32:02 +00:00
*/
function CreateHeader ()
{
// TODO - invoke it on className (not all textarea elements)
e107 :: getJs () -> requireCoreLib ( 'core/admin.js' );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
*
2009-12-13 21:52:32 +00:00
* @ return TBD
2009-11-24 16:32:02 +00:00
*/
public function CreatePage ()
{
return $this -> getUI () -> getCreate ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function PrefsSaveTrigger ()
{
$this -> getConfig ()
-> setPostedData ( $this -> getPosted (), null , false , false )
//->setPosted('not_existing_pref_test', 1)
-> save ( true );
$this -> getConfig () -> setMessages ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function PrefsPage ()
{
return $this -> getUI () -> getSettings ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Parent overload
* @ return e_admin_ui
*/
protected function parseAliases ()
{
// parse table
if ( strpos ( $this -> table , '.' ) !== false )
{
$tmp = explode ( '.' , $this -> table , 2 );
2009-12-23 15:12:13 +00:00
$this -> table = $tmp [ 1 ];
2009-11-24 16:32:02 +00:00
$this -> tableAlias = $tmp [ 0 ];
unset ( $tmp );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
parent :: parseAliases ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
public function getPrimaryName ()
{
2010-11-04 23:27:47 +00:00
// Option for working with tables having no PID
if ( ! varset ( $this -> pid ) && vartrue ( $this -> fields ) && false !== $this -> pid )
2009-11-24 16:32:02 +00:00
{
2010-11-04 23:27:47 +00:00
e107 :: getMessage () -> add ( LAN_UI_NOPID_ERROR , E_MESSAGE_WARNING );
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this -> pid ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function getTableName ( $alias = false , $prefix = false )
{
if ( $alias ) return ( $this -> tableAlias ? $this -> tableAlias : '' );
return ( $prefix ? '#' : '' ) . $this -> table ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Validation rules retrieved from controller object
* @ return array
*/
public function getValidationRules ()
{
return $this -> validationRules ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Data Field array retrieved from controller object
* @ return array
*/
public function getDataFields ()
{
return $this -> dataFields ;
}
2009-12-23 15:12:13 +00:00
2009-11-26 09:02:46 +00:00
/**
* Set read and write parms with drop - down - list array data ( ie . type = 'dropdown' )
* @ param str $field
* @ param array $array [ optional ]
2009-12-13 21:52:32 +00:00
* @ return none
2009-11-26 09:02:46 +00:00
*/
2009-12-23 15:12:13 +00:00
public function setDropDown ( $field , $array ) //TODO Have Miro check this.
2009-11-26 09:02:46 +00:00
{
$this -> fields [ $field ][ 'readParms' ] = $array ;
$this -> fields [ $field ][ 'writeParms' ] = $array ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
2009-12-23 15:12:13 +00:00
* Set Config object
2009-11-24 16:32:02 +00:00
* @ return e_admin_ui
*/
protected function _setConfig ()
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
$this -> _pref = $this -> pluginName == 'core' ? e107 :: getConfig () : e107 :: getPlugConfig ( $this -> pluginName );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$dataFields = $validateRules = array ();
foreach ( $this -> prefs as $key => $att )
{
// create dataFields array
$dataFields [ $key ] = vartrue ( $att [ 'data' ], 'string' );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// create validation array
if ( vartrue ( $att [ 'validate' ]))
{
$validateRules [ $key ] = array (( true === $att [ 'validate' ] ? 'required' : $att [ 'validate' ]), varset ( $att [ 'rule' ]), $att [ 'title' ], varset ( $att [ 'error' ], $att [ 'help' ]));
}
2009-12-23 15:12:13 +00:00
/* Not implemented in e_model yet
2009-11-24 16:32:02 +00:00
elseif ( vartrue ( $att [ 'check' ]))
{
$validateRules [ $key ] = array ( $att [ 'check' ], varset ( $att [ 'rule' ]), $att [ 'title' ], varset ( $att [ 'error' ], $att [ 'help' ]));
} */
}
$this -> _pref -> setDataFields ( $dataFields ) -> setValidationRules ( $validateRules );
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set current model
*
* @ return e_admin_ui
*/
public function _setModel ()
{
// try to create dataFields array if missing
if ( ! $this -> dataFields )
{
$this -> dataFields = array ();
foreach ( $this -> fields as $key => $att )
{
if (( false !== varset ( $att [ 'data' ]) && null !== $att [ 'type' ] && ! vartrue ( $att [ 'noedit' ])) || vartrue ( $att [ 'forceSave' ]))
{
$this -> dataFields [ $key ] = vartrue ( $att [ 'data' ], 'str' );
}
}
}
// TODO - do it in one loop, or better - separate method(s) -> convertFields(validate), convertFields(data),...
if ( ! $this -> validationRules )
{
$this -> validationRules = array ();
foreach ( $this -> fields as $key => $att )
{
if ( null === $att [ 'type' ] || vartrue ( $att [ 'noedit' ]))
{
continue ;
}
if ( vartrue ( $att [ 'validate' ]))
{
$this -> validationRules [ $key ] = array (( true === $att [ 'validate' ] ? 'required' : $att [ 'validate' ]), varset ( $att [ 'rule' ]), $att [ 'title' ], varset ( $att [ 'error' ], $att [ 'help' ]));
}
/* elseif ( vartrue ( $att [ 'check' ])) could go ?
{
$this -> checkRules [ $key ] = array ( $att [ 'check' ], varset ( $att [ 'rule' ]), $att [ 'title' ], varset ( $att [ 'error' ], $att [ 'help' ]));
} */
}
}
2011-05-18 13:41:19 +00:00
2010-12-27 12:38:14 +00:00
// don't touch it if already exists
if ( $this -> _model ) return $this ;
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
// default model
$this -> _model = new e_admin_model ();
$this -> _model -> setModelTable ( $this -> table )
-> setFieldIdName ( $this -> pid )
-> setValidationRules ( $this -> validationRules )
2010-05-05 15:05:32 +00:00
-> setDbTypes ( $this -> fieldTypes )
2009-11-24 16:32:02 +00:00
-> setDataFields ( $this -> dataFields )
2009-11-28 15:34:46 +00:00
-> setMessageStackName ( 'admin_ui_model_' . $this -> table )
2009-12-23 15:12:13 +00:00
-> setParam ( 'db_query' , $this -> editQry );
2009-11-24 16:32:02 +00:00
return $this ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Set current tree
* @ return e_admin_ui
*/
public function _setTreeModel ()
{
// default tree model
$this -> _tree_model = new e_admin_tree_model ();
$this -> _tree_model -> setModelTable ( $this -> table )
2011-05-30 16:43:01 +00:00
-> setFieldIdName ( $this -> pid )
2009-11-28 15:34:46 +00:00
-> setMessageStackName ( 'admin_ui_tree_' . $this -> table )
-> setParams ( array ( 'model_class' => 'e_admin_model' , 'model_message_stack' => 'admin_ui_model_' . $this -> table , 'db_query' => $this -> listQry ));
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this ;
}
/**
* Get extended ( UI ) Form instance
*
* @ return e_admin_ui
*/
public function _setUI ()
{
if ( $this -> getParam ( 'ui' ))
{
$this -> _ui = $this -> getParam ( 'ui' );
$this -> setParam ( 'ui' , null );
}
else // default ui
{
$this -> _ui = new e_admin_form_ui ( $this );
}
return $this ;
}
}
class e_admin_form_ui extends e_form
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
/**
* @ var e_admin_ui
*/
protected $_controller = null ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Constructor
* @ param e_admin_ui $controller
* @ param boolean $tabindex [ optional ] enable form element auto tab - indexing
*/
function __construct ( $controller , $tabindex = false )
{
$this -> _controller = $controller ;
parent :: __construct ( $tabindex );
2009-12-23 15:12:13 +00:00
// protect current methods from conflict.
2009-11-24 16:32:02 +00:00
$this -> preventConflict ();
// user constructor
$this -> init ();
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
protected function preventConflict ()
{
2010-11-04 23:27:47 +00:00
$err = false ;
$fields = $this -> getController () -> getFields ();
foreach ( $fields as $field => $foptions )
2009-11-24 16:32:02 +00:00
{
2010-11-04 23:27:47 +00:00
// check form custom methods
if ( $foptions [ 'type' ] === 'method' && method_exists ( 'e_form' , $field )) // check even if type is not method. - just in case of an upgrade later by 3rd-party.
2009-11-24 16:32:02 +00:00
{
2010-11-04 23:27:47 +00:00
e107 :: getMessage () -> addError ( sprintf ( LAN_UI_FORM_METHOD_ERROR , $field ));
$err = true ;
2009-12-23 15:12:13 +00:00
}
2009-11-24 16:32:02 +00:00
}
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
/* if ( $err )
2009-11-24 16:32:02 +00:00
{
2010-11-04 23:27:47 +00:00
//echo $err;
//exit;
} */
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* User defined init
*/
public function init ()
{
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* TODO - lans
2009-12-23 15:12:13 +00:00
* Generic DB Record Creation Form .
2009-11-24 16:32:02 +00:00
* @ return string
*/
function getCreate ()
{
$controller = $this -> getController ();
2009-12-23 15:12:13 +00:00
$request = $controller -> getRequest ();
2009-11-24 16:32:02 +00:00
if ( $controller -> getId ())
{
2010-11-04 23:27:47 +00:00
$legend = sprintf ( LAN_UI_EDIT_LABEL , $controller -> getId ());
2010-10-30 15:34:48 +00:00
$form_start = vartrue ( $controller -> headerUpdateMarkup );
$form_end = vartrue ( $controller -> footerUpdateMarkup );
2009-11-24 16:32:02 +00:00
}
else
{
2010-11-04 23:27:47 +00:00
$legend = LAN_UI_CREATE_LABEL ;
2010-10-30 15:34:48 +00:00
$form_start = vartrue ( $controller -> headerCreateMarkup );
$form_end = vartrue ( $controller -> footerCreateMarkup );
2009-11-24 16:32:02 +00:00
}
2010-10-30 15:34:48 +00:00
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
$forms = $models = array ();
$forms [] = array (
'id' => $this -> getElementId (),
//'url' => e_SELF,
//'query' => 'self', or custom GET query, self is default
'tabs' => true , // TODO - NOT IMPLEMENTED YET - enable tabs (only if fieldset count is > 1)
'fieldsets' => array (
'create' => array (
'legend' => $legend ,
'fields' => $controller -> getFields (), //see e_admin_ui::$fields
2010-10-30 15:34:48 +00:00
'header' => $form_start ,
'footer' => $form_end ,
2009-11-24 16:32:02 +00:00
'after_submit_options' => true , // or true for default redirect options
'after_submit_default' => $request -> getPosted ( '__after_submit_action' , $controller -> getDefaultAction ()), // or true for default redirect options
2009-12-23 15:12:13 +00:00
'triggers' => 'auto' , // standard create/update-cancel triggers
2009-11-24 16:32:02 +00:00
)
2009-12-23 15:12:13 +00:00
)
2009-11-24 16:32:02 +00:00
);
$models [] = $controller -> getModel ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this -> renderCreateForm ( $forms , $models , e_AJAX_REQUEST );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* TODO - lans
2009-12-23 15:12:13 +00:00
* Generic Settings Form .
2009-11-24 16:32:02 +00:00
* @ return string
*/
function getSettings ()
{
$controller = $this -> getController ();
2009-12-23 15:12:13 +00:00
$request = $controller -> getRequest ();
2010-11-04 23:27:47 +00:00
$legend = LAN_UI_PREF_LABEL ;
2009-11-24 16:32:02 +00:00
$forms = $models = array ();
$forms [] = array (
'id' => $this -> getElementId (),
//'url' => e_SELF,
//'query' => 'self', or custom GET query, self is default
'tabs' => false , // TODO - NOT IMPLEMENTED YET - enable tabs (only if fieldset count is > 1)
'fieldsets' => array (
'settings' => array (
'legend' => $legend ,
'fields' => $controller -> getPrefs (), //see e_admin_ui::$prefs
'after_submit_options' => false ,
'after_submit_default' => false , // or true for default redirect options
2009-12-23 15:12:13 +00:00
'triggers' => array ( 'save' => array ( LAN_SAVE , 'update' )), // standard create/update-cancel triggers
2009-11-24 16:32:02 +00:00
)
2009-12-23 15:12:13 +00:00
)
2009-11-24 16:32:02 +00:00
);
$models [] = $controller -> getConfig ();
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $this -> renderCreateForm ( $forms , $models , e_AJAX_REQUEST );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* Create list view
* Search for the following GET variables :
* - from : integer , current page
2009-12-23 15:12:13 +00:00
*
2009-11-24 16:32:02 +00:00
* @ return string
*/
public function getList ( $ajax = false )
{
$tp = e107 :: getParser ();
$controller = $this -> getController ();
$request = $controller -> getRequest ();
$id = $this -> getElementId ();
$tree = $options = array ();
2009-12-23 15:12:13 +00:00
$tree [ $id ] = $controller -> getTreeModel ();
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
// if going through confirm screen - no JS confirm
$controller -> setFieldAttr ( 'options' , 'noConfirm' , $controller -> deleteConfirmScreen );
2009-11-24 16:32:02 +00:00
$options [ $id ] = array (
'id' => $this -> getElementId (), // unique string used for building element ids, REQUIRED
'pid' => $controller -> getPrimaryName (), // primary field name, REQUIRED
//'url' => e_SELF, default
//'query' => $request->buildQueryString(array(), true, 'ajax_used'), - ajax_used is now removed from QUERY_STRING - class2
'head_query' => $request -> buildQueryString ( 'field=[FIELD]&asc=[ASC]&from=[FROM]' , false ), // without field, asc and from vars, REQUIRED
'np_query' => $request -> buildQueryString ( array (), false , 'from' ), // without from var, REQUIRED for next/prev functionality
'legend' => $controller -> getPluginTitle (), // hidden by default
'form_pre' => ! $ajax ? $this -> renderFilter ( $tp -> post_toForm ( array ( $controller -> getQuery ( 'searchquery' ), $controller -> getQuery ( 'filter_options' ))), $controller -> getMode () . '/' . $controller -> getAction ()) : '' , // needs to be visible when a search returns nothing
'form_post' => '' , // markup to be added after closing form element
'fields' => $controller -> getFields (), // see e_admin_ui::$fields
'fieldpref' => $controller -> getFieldPref (), // see e_admin_ui::$fieldpref
2009-12-23 15:12:13 +00:00
'table_pre' => '' , // markup to be added before opening table element
2011-07-25 02:41:49 +00:00
'table_post' => ! $tree [ $id ] -> isEmpty () ? $this -> renderBatch ( $controller -> getBatchDelete (), $controller -> getBatchCopy ()) : '' ,
2009-12-23 15:12:13 +00:00
'fieldset_pre' => '' , // markup to be added before opening fieldset element
2009-11-24 16:32:02 +00:00
'fieldset_post' => '' , // markup to be added after closing fieldset element
'perPage' => $controller -> getPerPage (), // if 0 - no next/prev navigation
'from' => $controller -> getQuery ( 'from' , 0 ), // current page, default 0
'field' => $controller -> getQuery ( 'field' ), //current order field name, default - primary field
'asc' => $controller -> getQuery ( 'asc' , 'desc' ), //current 'order by' rule, default 'asc'
);
return $this -> renderListForm ( $options , $tree , $ajax );
}
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
public function getConfirmDelete ( $ids , $ajax = false )
{
$controller = $this -> getController ();
$request = $controller -> getRequest ();
$fieldsets = array ();
$forms = array ();
$id_array = explode ( ',' , $ids );
$delcount = count ( $id_array );
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
e107 :: getMessage () -> addWarning ( sprintf ( LAN_UI_DELETE_WARNING , $delcount ));
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
$fieldsets [ 'confirm' ] = array (
'fieldset_pre' => '' , // markup to be added before opening fieldset element
'fieldset_post' => '' , // markup to be added after closing fieldset element
'table_head' => '' , // markup between <thead> tag
// Colgroup Example: array(0 => array('class' => 'label', 'style' => 'text-align: left'), 1 => array('class' => 'control', 'style' => 'text-align: left'));
'table_colgroup' => '' , // array to be used for creating markup between <colgroup> tag (<col> list)
'table_pre' => '' , // markup to be added before opening table element
'table_post' => '' , // markup to be added after closing table element
'table_rows' => '' , // rows array (<td> tags)
2011-05-18 13:41:19 +00:00
'table_body' => '' , // string body - used only if rows empty
2010-11-04 23:27:47 +00:00
'pre_triggers' => '' ,
'triggers' => array ( 'hidden' => $this -> hidden ( 'etrigger_delete[' . $ids . ']' , $ids ), 'delete_confirm' => array ( LAN_CONFDELETE , 'submit' , $ids ), 'cancel' => array ( LAN_CANCEL , 'cancel' )),
);
if ( $delcount > 1 )
{
$fieldsets [ 'confirm' ][ 'triggers' ][ 'hidden' ] = $this -> hidden ( 'etrigger_batch' , 'delete' );
}
2011-05-18 13:41:19 +00:00
2010-11-04 23:27:47 +00:00
$forms [ $id ] = array (
'id' => $this -> getElementId (), // unique string used for building element ids, REQUIRED
'url' => e_SELF , // default
'query' => $request -> buildQueryString ( array (), true , 'ajax_used' ), // - ajax_used is now removed from QUERY_STRING - class2
'legend' => $controller -> addTitle ( LAN_UI_DELETE_LABEL ), // hidden by default
'form_pre' => '' , // markup to be added before opening form element
'form_post' => '' , // markup to be added after closing form element
'header' => '' , // markup to be added after opening form element
'footer' => '' , // markup to be added before closing form element
'fieldsets' => $fieldsets ,
);
return $this -> renderForm ( $forms , $ajax );
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
function renderFilter ( $current_query = array (), $location = '' , $input_options = array ())
{
if ( ! $input_options ) $input_options = array ( 'size' => 20 );
if ( ! $location )
{
$location = 'main/list' ; //default location
}
2009-12-23 15:12:13 +00:00
$l = e107 :: getParser () -> post_toForm ( explode ( '/' , $location ));
2009-11-24 16:32:02 +00:00
if ( ! is_array ( $input_options ))
{
parse_str ( $input_options , $input_options );
}
$input_options [ 'id' ] = false ;
$input_options [ 'class' ] = 'tbox input-text filter' ;
2010-10-30 15:34:48 +00:00
$controller = $this -> getController ();
$filter_pre = vartrue ( $controller -> preFiliterMarkup );
$filter_post = vartrue ( $controller -> postFiliterMarkup );
2009-11-24 16:32:02 +00:00
$text = "
< form method = 'get' action = '".e_SELF."' >
< fieldset class = 'e-filter' >
2010-11-04 23:27:47 +00:00
< legend class = 'e-hideme' > " .LAN_LABEL_LABEL_SELECTED. " </ legend >
2010-10-30 15:34:48 +00:00
" . $filter_pre . "
2009-11-24 16:32:02 +00:00
< div class = 'left' >
" . $this->text ('searchquery', $current_query[0] , 50, $input_options ). "
" . $this->select_open ('filter_options', array('class' => 'tbox select filter', 'id' => false)). "
2010-11-04 23:27:47 +00:00
" . $this->option (LAN_FILTER_LABEL_DISPLAYALL, ''). "
" . $this->option (LAN_FILTER_LABEL_CLEAR, '___reset___'). "
2009-11-24 16:32:02 +00:00
" . $this->renderBatchFilter ('filter', $current_query[1] ). "
" . $this->select_close (). "
2009-12-23 15:12:13 +00:00
< div class = 'e-autocomplete' ></ div >
2009-11-24 16:32:02 +00:00
" . $this->hidden ('mode', $l[0] ). "
" . $this->hidden ('action', $l[1] ). "
" . $this->admin_button ('etrigger_filter', 'etrigger_filter', 'filter e-hide-if-js', LAN_FILTER, array('id' => false)). "
< span class = 'indicator' style = 'display: none;' >
2010-11-04 23:27:47 +00:00
< img src = '".e_IMAGE_ABS."generic/loading_16.gif' class = 'icon action S16' alt = '".LAN_LOADING."' />
2009-11-24 16:32:02 +00:00
</ span >
</ div >
2010-10-30 15:34:48 +00:00
" . $filter_post . "
2009-11-24 16:32:02 +00:00
</ fieldset >
</ form >
2009-12-23 15:12:13 +00:00
" ;
2009-11-24 16:32:02 +00:00
e107 :: getJs () -> requireCoreLib ( 'scriptaculous/controls.js' , 2 );
//TODO - external JS
e107 :: getJs () -> footerInline ( "
//autocomplete fields
2009-12-23 15:12:13 +00:00
\ $\ $ ( 'input[name=searchquery]' ) . each ( function ( el , cnt ) {
2009-11-24 16:32:02 +00:00
if ( ! cnt ) el . activate ();
else return ;
new Ajax . Autocompleter ( el , el . next ( 'div.e-autocomplete' ), '".e_SELF."?mode=".$l[0]."&action=filter' , {
paramName : 'searchquery' ,
minChars : 2 ,
frequency : 0.5 ,
2009-12-23 15:12:13 +00:00
afterUpdateElement : function ( txt , li ) {
2009-11-24 16:32:02 +00:00
var cfrm = el . up ( 'form' ), cont = cfrm . next ( '.e-container' );
if ( ! cont ) {
return ;
2009-12-23 15:12:13 +00:00
}
2009-11-24 16:32:02 +00:00
cfrm . submitForm ( cont );
},
indicator : el . next ( 'span.indicator' ),
parameters : 'ajax_used=1'
});
var sel = el . next ( 'select.filter' );
if ( sel ) {
2009-12-23 15:12:13 +00:00
sel . observe ( 'change' , function ( e ) {
2009-11-24 16:32:02 +00:00
var cfrm = e . element () . up ( 'form' ), cont = cfrm . next ( '.e-container' );
if ( cfrm && cont && e . element () . value != '___reset___' ) {
e . stop ();
cfrm . submitForm ( cont );
return ;
}
e107Helper . selectAutoSubmit ( e . element ());
});
}
});
" );
2009-12-23 15:12:13 +00:00
return $text ;
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// FIXME - use e_form::batchoptions(), nice way of buildig batch dropdown - news administration show_batch_options()
2011-07-25 02:41:49 +00:00
function renderBatch ( $allow_delete = false , $allow_copy = false )
2009-12-23 15:12:13 +00:00
{
2011-07-25 02:41:49 +00:00
// $allow_copy = TRUE;
2009-11-24 16:32:02 +00:00
$fields = $this -> getController () -> getFields ();
if ( ! varset ( $fields [ 'checkboxes' ]))
{
return '' ;
2009-12-23 15:12:13 +00:00
}
2010-11-04 23:27:47 +00:00
// TODO - core ui-batch-option class!!! REMOVE INLINE STYLE!
2009-11-24 16:32:02 +00:00
$text = "
< div class = 'buttons-bar left' >
< img src = '".e_IMAGE_ABS."generic/branchbottom.gif' alt = '' class = 'icon action' />
2010-11-04 23:27:47 +00:00
" . $this->select_open ('etrigger_batch', array('class' => 'tbox select batch e-autosubmit reset', 'id' => false)). "
" . $this->option (LAN_BATCH_LABEL_SELECTED, ''). "
2011-07-25 02:41:49 +00:00
" .( $allow_copy ? $this->option (LAN_COPY, 'copy', false, array('class' => 'ui-batch-option class', 'other' => 'style= " padding - left : 15 px " ')) : ''). "
" .( $allow_delete ? $this->option (LAN_DELETE, 'delete', false, array('class' => 'ui-batch-option class', 'other' => 'style= " padding - left : 15 px " ')) : ''). "
2009-11-24 16:32:02 +00:00
" . $this->renderBatchFilter ('batch'). "
" . $this->select_close (). "
" . $this->admin_button ('e__execute_batch', 'e__execute_batch', 'batch e-hide-if-js', 'Execute', array('id' => false)). "
</ div >
" ;
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
return $text ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
// TODO - do more
2009-12-23 15:12:13 +00:00
function renderBatchFilter ( $type = 'batch' , $selected = '' ) // Common function used for both batches and filters.
2009-11-24 16:32:02 +00:00
{
2010-11-04 23:27:47 +00:00
$optdiz = array ( 'batch' => LAN_BATCH_LABEL_PREFIX . ' ' , 'filter' => LAN_FILTER_LABEL_PREFIX . ' ' );
2009-11-24 16:32:02 +00:00
$table = $this -> getController () -> getTableName ();
$text = '' ;
$textsingle = '' ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
foreach ( $this -> getController () -> getFields () as $key => $val )
{
if ( ! varset ( $val [ $type ]))
{
continue ;
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
$option = array ();
$parms = vartrue ( $val [ 'writeParms' ], array ());
if ( is_string ( $parms )) parse_str ( $parms , $parms );
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
switch ( $val [ 'type' ])
{
2010-10-30 15:34:48 +00:00
case 'bool' :
2009-11-24 16:32:02 +00:00
case 'boolean' : //TODO modify description based on $val['parm]
$option [ 'bool__' . $key . '__1' ] = LAN_YES ;
$option [ 'bool__' . $key . '__0' ] = LAN_NO ;
if ( $type == 'batch' )
{
$option [ 'boolreverse__' . $key ] = LAN_BOOL_REVERSE ;
}
break ;
2009-11-28 15:34:46 +00:00
2009-12-23 15:12:13 +00:00
case 'templates' :
2009-11-28 15:34:46 +00:00
case 'layouts' :
$parms [ 'raw' ] = true ;
$val [ 'writeParms' ] = $parms ;
$tmp = $this -> renderElement ( $key , '' , $val );
foreach ( $tmp as $k => $name )
{
$option [ $key . '__' . $k ] = $name ;
}
break ;
2009-12-23 15:12:13 +00:00
case 'dropdown' : // use the array $parm;
2010-11-08 09:20:12 +00:00
if ( ! is_array ( varset ( $parms [ '__options' ]))) parse_str ( $parms [ '__options' ], $parms [ '__options' ]);
$opts = $parms [ '__options' ];
2011-05-18 13:41:19 +00:00
if ( vartrue ( $opts [ 'multiple' ]))
2010-11-08 09:20:12 +00:00
{
// no batch support for multiple, should have some for filters soon
2011-05-18 13:41:19 +00:00
continue ;
2010-11-08 09:20:12 +00:00
}
2009-11-24 16:32:02 +00:00
unset ( $parms [ '__options' ]); //remove element options if any
foreach ( $parms as $k => $name )
{
$option [ $key . '__' . $k ] = $name ;
}
break ;
2011-05-18 13:41:19 +00:00
2010-11-08 09:20:12 +00:00
case 'lanlist' : // use the array $parm;
if ( ! is_array ( varset ( $parms [ '__options' ]))) parse_str ( $parms [ '__options' ], $parms [ '__options' ]);
$opts = $parms [ '__options' ];
2011-05-18 13:41:19 +00:00
if ( vartrue ( $opts [ 'multiple' ]))
2010-11-08 09:20:12 +00:00
{
// no batch support for multiple, should have some for filters soon
2011-05-18 13:41:19 +00:00
continue ;
2010-11-08 09:20:12 +00:00
}
$options = e107 :: getLanguage () -> getLanSelectArray ();
foreach ( $options as $code => $name )
{
$option [ $key . '__' . $code ] = $name ;
}
break ;
2009-12-23 15:12:13 +00:00
case 'datestamp' : // use $parm to determine unix-style or YYYY-MM-DD
//TODO last hour, today, yesterday, this-month, last-month etc.
2009-11-24 16:32:02 +00:00
/* foreach ( $val [ 'parm' ] as $k => $name )
{
2009-12-23 15:12:13 +00:00
$text .= $frm -> option ( $name , $type . '__' . $key . " __ " . $k );
2009-11-24 16:32:02 +00:00
} */
break ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
case 'userclass' :
//case 'userclasses':
$classes = e107 :: getUserClass () -> uc_required_class_list ( vartrue ( $parms [ 'classlist' ], '' ));
foreach ( $classes as $k => $name )
{
$option [ $key . '__' . $k ] = $name ;
}
2009-12-23 15:12:13 +00:00
break ;
2009-11-24 16:32:02 +00:00
case 'method' :
2011-05-18 13:41:19 +00:00
$method = $key ;
2009-11-24 16:32:02 +00:00
$list = call_user_func_array ( array ( $this , $method ), array ( '' , $type , $parms ));
2011-05-18 13:41:19 +00:00
2009-11-24 16:32:02 +00:00
if ( is_array ( $list ))
{
//check for single option
if ( isset ( $list [ 'singleOption' ]))
{
$textsingle .= $list [ 'singleOption' ];
continue ;
}
// non rendered options array
foreach ( $list as $k => $name )
{
$option [ $key . '__' . $k ] = $name ;
}
}
elseif ( ! empty ( $list )) //optgroup, continue
{
$text .= $list ;
continue ;
}
break ;
2011-05-18 13:41:19 +00:00
case 'user' : // TODO - User Filter
//$option[$key.'__'.$k] = $name;
2011-05-07 06:22:44 +00:00
break ;
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
if ( count ( $option ) > 0 )
{
$text .= " \t " . $this -> optgroup_open ( $optdiz [ $type ] . defset ( $val [ 'title' ], $val [ 'title' ]), varset ( $disabled )) . " \n " ;
foreach ( $option as $okey => $oval )
{
2009-12-23 15:12:13 +00:00
$text .= $this -> option ( $oval , $okey , $selected == $okey ) . " \n " ;
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
$text .= " \t " . $this -> optgroup_close () . " \n " ;
2009-11-24 16:32:02 +00:00
}
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
return $textsingle . $text ;
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
public function getElementId ()
{
$controller = $this -> getController ();
return str_replace ( '_' , '-' , ( $controller -> getPluginName () == 'core' ? 'core-' . $controller -> getTableName () : 'plugin-' . $controller -> getPluginName ()));
}
2009-12-23 15:12:13 +00:00
2009-11-24 16:32:02 +00:00
/**
* @ return e_admin_ui
*/
public function getController ()
2009-12-23 15:12:13 +00:00
{
2009-11-24 16:32:02 +00:00
return $this -> _controller ;
}
}
include_once ( e107 :: coreTemplatePath ( 'admin_icons' ));
/**
* TODO :
* 1. [ DONE - a good start ] move abstract peaces of code to the proper classes
* 2. [ DONE - at least for alpha release ] remove duplicated code ( e_form & e_admin_form_ui ), refactoring
* 3. make JS Manager handle Styles ( . css files and inline CSS )
* 4. [ DONE ] e_form is missing some methods used in e_admin_form_ui
2009-12-23 15:12:13 +00:00
* 5. [ DONE ] date convert needs string - to - datestamp auto parsing , strptime () is the solution but needs support for
* Windows and PHP < 5.1 . 0 - build custom strptime () function ( php_compatibility_handler . php ) on this -
2009-11-24 16:32:02 +00:00
* http :// sauron . lionel . free . fr / ? page = php_lib_strptime ( bad license so no copy / paste is allowed ! )
* 6. [ DONE - read / writeParms introduced ] $fields [ parms ] mess - fix it , separate list / edit mode parms somehow
* 7. clean up / document all object vars ( e_admin_ui , e_admin_dispatcher )
* 8. [ DONE hopefully ] clean up / document all parameters ( get / setParm ()) in controller and model classes
* 9. [ DONE ] 'ip' field type - convert to human readable format while showing / editing record
* 10. draggable ( or not ? ) ordering ( list view )
* 11. [ DONE ] realtime search filter ( typing text ) - like downloads currently
* 12. [ DONE ] autosubmit when 'filter' dropdown is changed ( quick fix ? )
2009-12-23 15:12:13 +00:00
* 13. tablerender captions
2009-11-24 16:32:02 +00:00
* 14. [ DONE ] textareas auto - height
* 15. [ DONE ] multi JOIN table support ( optional ), aliases
* 16. tabs support ( create / edit view )
* 17. tree list view ( should handle cases like Site Links admin page )
*/