Version 4.0.13.1

This commit is contained in:
Neo committed 2025-12-19 04:57:54 -08:00
1 parent f9e857d255
commit c6a86f0d9b
7517 files changed
+597400 -668041

No files matched your search

@@ -0,0 +1,298 @@
<?php
/**
* @brief Customization AJAX actions
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @since 07 May 2013
* @version SVN_VERSION_NUMBER
*/
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Members AJAX actions
*/
class _ajax extends \IPS\Dispatcher\Controller
{
/**
* Return a CSS or HTML menu
*
* @return html
*/
public function loadMenu()
{
$id = \IPS\Request::i()->id;
$t_type = \IPS\Request::i()->t_type ?: 'templates';
$t_app = \IPS\Request::i()->t_app ?: 'core';
$t_location = \IPS\Request::i()->t_location ?: ( ( \IPS\Request::i()->t_app ) ? false : 'front' );
if ( $t_type == 'templates' )
{
$t_group = \IPS\Request::i()->t_group ?: ( ( \IPS\Request::i()->t_app ) ? false : 'global' );
$t_template = \IPS\Request::i()->t_name ?: ( ( \IPS\Request::i()->t_app ) ? false : 'globalTemplate' );
}
else
{
$t_group = \IPS\Request::i()->t_group ?: ( ( \IPS\Request::i()->t_app ) ? false : 'custom' );
$t_template = \IPS\Request::i()->t_name ?: ( ( \IPS\Request::i()->t_app ) ? false : 'custom.css' );
}
$theme = \IPS\Theme::load( $id );
if ( $t_type == 'templates' )
{
$templateNames = $theme->getRawTemplates( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT );
}
else
{
$templateNames = $theme->getRawCss( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT );
}
if ( $t_type === 'templates' )
{
/* Remove Admin Templates */
foreach( $templateNames as $app => $items )
{
foreach( $templateNames[ $app ] as $location => $items )
{
if ( $location == 'admin' )
{
unset( $templateNames[ $app ][ $location ] );
}
}
}
}
else
{
if ( $theme->by_skin_gen )
{
/* Remove all but custom/custom.css */
foreach( $templateNames as $app => $items )
{
foreach( $templateNames[ $app ] as $location => $items )
{
if ( $location === 'front' )
{
foreach( $templateNames[ $app ][ $location ] as $path => $items )
{
if ( $path === 'custom' )
{
foreach( $items as $name => $data )
{
if ( $name !== 'custom.css' )
{
unset( $templateNames[ $app ][ $location ][ $path ][ $name ] );
}
}
}
else
{
unset( $templateNames[ $app ][ $location ][ $path ] );
}
}
}
else
{
unset( $templateNames[ $app ][ $location ] );
}
}
}
}
else
{
/* Remove Admin Templates */
foreach( $templateNames as $app => $items )
{
foreach( $templateNames[ $app ] as $location => $items )
{
if ( $location == 'admin' )
{
unset( $templateNames[ $app ][ $location ] );
}
}
}
}
}
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'customization' )->templateEditorMenu( $theme, $templateNames, array( 'app' => $t_app, 'location' => $t_location, 'group' => $t_group, 'template' => $t_template, 'type' => $t_type) );
}
/**
* Return a CSS or HTML template as JSON
*
* @return json
*/
public function loadTemplate()
{
$t_type = \IPS\Request::i()->t_type;
$t_app = \IPS\Request::i()->t_app;
$t_location = \IPS\Request::i()->t_location;
$t_group = ( \IPS\Request::i()->t_group ) ?: '.';
$t_name = \IPS\Request::i()->t_name;
$id = \IPS\Request::i()->id;
$skinSet = \IPS\Theme::load( $id );
if ( $t_type == 'templates' )
{
$templateBits = $skinSet->getRawTemplates( $t_app, $t_location, $t_group, \IPS\Theme::RETURN_ALL );
$templateBit = ( ! empty( $t_name ) ) ? $templateBits[ $t_app ][ $t_location ][ $t_group ][ $t_name ] : null;
$templateBit['template_content'] = htmlentities( $templateBit['template_content'], \IPS\HTMLENTITIES, 'UTF-8', TRUE );
}
else
{
$templateNames = $skinSet->getRawCss( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT );
$templateBits = $skinSet->getRawCss( $t_app, $t_location, $t_group, \IPS\Theme::RETURN_ALL );
$templateBit = ( ! empty( $t_name ) ) ? $templateBits[ $t_app ][ $t_location ][ $t_group ][ $t_name ] : null;
$templateBit['css_content'] = htmlentities( $templateBit['css_content'], \IPS\HTMLENTITIES, 'UTF-8', TRUE );
}
if ( $templateBit !== null )
{
\IPS\Output::i()->json( $templateBit );
}
else
{
\IPS\Output::i()->json( array( 'error' => true ) );
}
}
/**
* [AJAX] Search templates
*
* @return void
*/
public function searchtemplates()
{
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_templates_manage' );
$theme = \IPS\Theme::load( \IPS\Request::i()->id );
$parents = array( $theme->_id );
$where = array();
if ( \IPS\Request::i()->term )
{
$where[] = array( 'LOWER(template_content) LIKE ?', '%' . mb_strtolower( \IPS\Request::i()->term ) . '%' );
}
if ( in_array( 'inherited', explode( ',', \IPS\Request::i()->filters ) ) )
{
try
{
foreach( $theme->parents() as $parent )
{
$parents[] = $parent->_id;
}
}
catch( \OutOfRangeException $e ) { }
}
array_push( $parents, 0 );
if ( !in_array( 'unique', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'template_user_added=0' );
}
if ( !in_array( 'unmodified', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'template_user_edited>0' );
}
$where[] = array( 'template_set_id IN (' . implode( ',' , $parents ) . ')' );
$select = \IPS\Db::i()->select(
'*, INSTR(\',' . implode( ',' , $parents ) . ',\', CONCAT(\',\',template_set_id,\',\') ) as theorder',
'core_theme_templates',
$where,
'template_location, template_group, template_name, theorder desc'
);
$return = array();
foreach( $select as $result )
{
if ( $result['template_user_edited'] )
{
$outOfDate = $result['template_user_edited'] < \IPS\Application::load( $result['template_app'] )->long_version;
if ( ( $outOfDate and !in_array( 'outofdate', explode( ',', \IPS\Request::i()->filters ) ) ) or ( !$outOfDate and !in_array( 'modified', explode( ',', \IPS\Request::i()->filters ) ) ) )
{
unset( $return[ $result['template_app'] ][ $result['template_location'] ][ $result['template_group'] ][ $result['template_name'] ] );
continue;
}
}
$return[ $result['template_app'] ][ $result['template_location'] ][ $result['template_group'] ][ $result['template_name'] ] = $result['template_name'];
}
\IPS\Output::i()->json( $return );
}
/**
* [AJAX] Search CSS
*
* @return void
*/
public function searchcss()
{
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_templates_manage' );
$theme = \IPS\Theme::load( \IPS\Request::i()->id );
$parents = array( $theme->_id );
$where = array();
if ( \IPS\Request::i()->term )
{
$where[] = array( 'LOWER(css_content) LIKE ?', '%' . mb_strtolower( \IPS\Request::i()->term ) . '%' );
}
if ( in_array( 'inherited', explode( ',', \IPS\Request::i()->filters ) ) )
{
try
{
foreach( $theme->parents() as $parent )
{
$parents[] = $parent->_id;
}
}
catch( \OutOfRangeException $e ) { }
}
array_push( $parents, 0 );
if ( !in_array( 'unique', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'css_added_to=?', \IPS\Request::i()->id );
}
if ( !in_array( 'unmodified', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'css_user_edited>0' );
}
$where[] = array( 'css_set_id IN (' . implode( ',' , $parents ) . ')' );
$return = array();
foreach( \IPS\Db::i()->select(
'*, INSTR(\',' . implode( ',' , $parents ) . ',\', CONCAT(\',\',css_set_id,\',\') ) as theorder',
'core_theme_css',
$where,
'css_location, css_path, css_name, theorder desc'
) as $result )
{
if ( $result['css_user_edited'] )
{
$outOfDate = $result['css_user_edited'] < \IPS\Application::load( $result['css_app'] )->long_version;
if ( !$result['css_added_to'] and ( ( $outOfDate and !in_array( 'outofdate', explode( ',', \IPS\Request::i()->filters ) ) ) or ( !$outOfDate and !in_array( 'modified', explode( ',', \IPS\Request::i()->filters ) ) ) ) )
{
unset( $return[ $result['css_app'] ][ $result['css_location'] ][ $result['css_path'] ][ $result['css_name'] ] );
continue;
}
}
$return[ $result['css_app'] ][ $result['css_location'] ][ $result['css_path'] ][ $result['css_name'] ] = $result['css_name'];
}
\IPS\Output::i()->json( $return );
}
}
@@ -0,0 +1,315 @@
<?php
/**
* @brief Email template management
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @since 02 Jul 2013
* @version SVN_VERSION_NUMBER
*/
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Email template management
*/
class _emails extends \IPS\Dispatcher\Controller
{
/**
* Execute
*
* @return void
*/
public function execute()
{
\IPS\Dispatcher::i()->checkAcpPermission( 'emails_manage' );
parent::execute();
}
/**
* Manage
*
* @return void
*/
public function manage()
{
/* Create the table */
$table = new \IPS\Helpers\Table\Db( 'core_email_templates', \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), array( "template_parent=?", 0 ) );
$table->langPrefix = 'emailtpl_';
$table->mainColumn = 'template_name';
$table->include = array( 'template_name', 'template_app' );
$table->quickSearch = 'template_name';
$table->limit = 100;
/* Default sort options */
$table->sortBy = $table->sortBy ?: 'template_name';
$table->sortDirection = $table->sortDirection ?: 'desc';
/* Custom parsers */
$table->parsers = array(
'template_name' => function( $val, $row )
{
return \IPS\Member::loggedIn()->language()->addToStack( 'emailtpl_' . $val );
},
'template_app' => function( $val, $row )
{
return \IPS\Member::loggedIn()->language()->addToStack( '__app_' . $val );
}
);
$table->rowButtons = function( $row )
{
$return = array();
/* There isn't a separate permission option because literally the only thing you can do with email templates is edit the */
$return['edit'] = array(
'icon' => 'pencil',
'title' => 'edit',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=form&key=' ) . $row['template_key'],
);
if( $row['template_edited'] )
{
$return['revert'] = array(
'icon' => 'undo',
'title' => 'revert',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=revert&key=' ) . $row['template_key'],
);
$return['export'] = array(
'icon' => 'download',
'title' => 'download',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=export&key=' ) . $row['template_key'],
);
}
return $return;
};
/* Import button */
\IPS\Output::i()->sidebar['actions'] = array(
'upload' => array(
'title' => 'upload_email_template',
'icon' => 'upload',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=import' ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('upload_email_template') )
),
);
/* Display */
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('emailtpl_header');
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->block( 'title', (string) $table );
}
/**
* Export a customized email template
*
* @return void
*/
public function export()
{
/* Get the template info */
try
{
$template = \IPS\DB::i()->select( '*', 'core_email_templates', array( 'template_parent>0 AND template_key=?', \IPS\Request::i()->key ) )->first();
}
catch ( \UnderflowException $e )
{
\IPS\Output::i()->error( 'email_template_not_found', '3S128/3', 403, '' );
}
$xml = \IPS\Xml\SimpleXML::create('emails');
$xml->addChild( 'template', array(
'template_app' => $template['template_app'],
'template_name' => $template['template_name'],
'template_content_html' => $template['template_content_html'],
'template_content_plaintext' => $template['template_content_plaintext'],
'template_data' => $template['template_data'],
'template_key' => $template['template_key'],
) );
$name = addslashes( str_replace( array( ' ', '.', ',' ), '_', $template['template_name'] ) . '.xml' );
\IPS\Output::i()->sendOutput( $xml->asXML(), 200, 'application/xml', array( 'Content-Disposition' => \IPS\Output::getContentDisposition( 'attachment', $name ) ) );
}
/**
* Form to import an email template
*
* @return void
*/
public function import()
{
$form = new \IPS\Helpers\Form( 'form', 'upload' );
$form->add( new \IPS\Helpers\Form\Upload( 'email_template_file', NULL, TRUE, array( 'allowedFileTypes' => array( 'xml' ), 'temporary' => TRUE ) ) );
if ( $values = $form->values() )
{
/* Open XML file */
$xml = \IPS\Xml\SimpleXML::loadFile( $values['email_template_file'] );
if( !count($xml->template) )
{
\IPS\Output::i()->error( 'email_template_badform', '1S128/4', 403, '' );
}
foreach( $xml->template as $template )
{
$update = array(
'template_name' => (string) $template->template_name,
'template_data' => (string) $template->template_data,
'template_app' => (string) $template->template_app,
'template_content_html' => (string) $template->template_content_html,
'template_content_plaintext' => (string) $template->template_content_plaintext,
'template_key' => (string) $template->template_key,
);
try
{
$existing = \IPS\DB::i()->select( '*', 'core_email_templates', array( 'template_parent=0 AND template_key=?', $update['template_key'] ) )->first();
}
catch ( \UnderflowException $e )
{
\IPS\Output::i()->error( \IPS\Member::loggedIn()->language()->addToStack('email_template_noexist', FALSE, array( 'sprintf' => array( $update['template_name'] ) ) ), '1S128/5', 403, '' );
}
$update['template_parent'] = $existing['template_id'];
\IPS\DB::i()->replace( 'core_email_templates', $update );
\IPS\DB::i()->update( 'core_email_templates', array( 'template_edited' => 1 ), array( 'template_id=?', $existing['template_id'] ) );
}
/* Redirect */
\IPS\Session::i()->log( 'acplogs__emailtemplate_updated' );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), 'email_template_uploaded' );
}
/* Display */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->block( \IPS\Member::loggedIn()->language()->addToStack('upload_email_template'), $form, FALSE );
}
/**
* Restore an email template to the original unedited version
*
* @return void
*/
public function revert()
{
/* Get the template info for the log */
$template = \IPS\DB::i()->select( '*', 'core_email_templates', array( 'template_parent=0 AND template_key=?', \IPS\Request::i()->key ) )->first();
/* Revert any user-edited copies of the specified template */
\IPS\DB::i()->delete( 'core_email_templates', array( 'template_parent>0 AND template_key=?', \IPS\Request::i()->key ) );
/* Reset edited flag on parent */
\IPS\Db::i()->update( 'core_email_templates', array( 'template_edited' => 0 ), array( 'template_id=?', $template['template_id'] ) );
/* Rebuild template */
$this->buildTemplate( $template );
/* Log and redirect */
\IPS\Session::i()->log( 'acplog__emailtpl_reverted', array( $template['template_name'] => FALSE ) );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), 'reverted' );
}
/**
* Edit an email template
*
* @return void
*/
public function form()
{
/* Get the template */
if( empty( \IPS\Request::i()->key ) )
{
\IPS\Output::i()->error( 'emailtpl_nofind', '3S128/1', 403, '' );
}
try
{
$template = \IPS\DB::i()->select( '*', 'core_email_templates', array( 'template_key=?', \IPS\Request::i()->key ), 'template_parent DESC', 1 )->first();
}
catch ( \UnderflowException $e )
{
\IPS\Output::i()->error( 'emailtpl_nofind', '3S128/2', 403, '' );
}
/* Figure out tags for form helpers */
$_tags = explode( ",", $template['template_data'] );
$tags = array();
foreach( $_tags as $_tag )
{
$_tag = explode( '=', $_tag );
$tags[ '{' . trim( $_tag[0] ) . '}' ] = '';
}
/* Start the form */
$form = new \IPS\Helpers\Form;
$form->class = 'ipsForm_vertical';
$form->add( new \IPS\Helpers\Form\Codemirror( 'content_html', $template['template_content_html'], TRUE, array( 'tags' => $tags ) ) );
$form->add( new \IPS\Helpers\Form\TextArea( 'content_plaintext', $template['template_content_plaintext'], FALSE, array( 'tags' => $tags, 'rows' => 14 ) ) );
/* Handle submissions */
if ( $values = $form->values() )
{
$save = array(
'template_name' => $template['template_name'],
'template_data' => $template['template_data'],
'template_key' => $template['template_key'],
'template_content_html' => $values['content_html'],
'template_content_plaintext' => $values['content_plaintext'],
'template_app' => $template['template_app'],
'template_parent' => $template['template_parent'] ?: $template['template_id'],
);
if ( !empty($template['template_parent']) )
{
\IPS\Db::i()->update( 'core_email_templates', $save, array( 'template_id=?', $template['template_id'] ) );
}
else
{
\IPS\Db::i()->insert( 'core_email_templates', $save );
\IPS\Db::i()->update( 'core_email_templates', array( 'template_edited' => 1 ), array( 'template_id=?', $template['template_id'] ) );
}
$this->buildTemplate( $save );
\IPS\Session::i()->log( 'acplogs__emailtpl_edited', array( $template['template_name'] => FALSE ) );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), 'saved' );
}
/* Output */
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('emailtpl_edit');
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->block( 'emailtpl_edit', $form );
}
/**
* Build a template for later execution
*
* @param array $template Template data from core_email_templates
* @return void
*/
public function buildTemplate( $template )
{
$htmlFunction = 'namespace IPS\Theme;' . "\n" . \IPS\Theme::compileTemplate( $template['template_content_html'], "email_html_{$template['template_app']}_{$template['template_name']}", $template['template_data'] );
$ptFunction = 'namespace IPS\Theme;' . "\n" . \IPS\Theme::compileTemplate( $template['template_content_plaintext'], "email_plaintext_{$template['template_app']}_{$template['template_name']}", $template['template_data'] );
$key = $template['template_key'] . '_email_html';
\IPS\Data\Store::i()->$key = $htmlFunction;
$key = $template['template_key'] . '_email_plaintext';
\IPS\Data\Store::i()->$key = $ptFunction;
}
}
Whitespace-only changes.
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,516 @@
<?php
/**
* @brief theme Settings
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @since 17 May 2013
* @version SVN_VERSION_NUMBER
*/
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* themesettings
*/
class _themesettings extends \IPS\Dispatcher\Controller
{
/**
* Execute
*
* @return void
*/
public function execute()
{
parent::execute();
/* Display */
\IPS\Output::i()->breadcrumb = array(
array(
\IPS\Http\Url::internal('app=core&module=customization&controller=themes'),
'menu__' . \IPS\Dispatcher::i()->application->directory . '_' . \IPS\Dispatcher::i()->module->key
),
array(
NULL,
\IPS\Member::loggedIn()->language()->addToStack( 'core_theme_set_title_' . intval( \IPS\Request::i()->set_id ) )
)
);
}
/**
* Manage
*
* @return void
*/
protected function manage()
{
/* Load Theme */
try
{
$theme = \IPS\Theme::load( \IPS\Request::i()->set_id );
}
catch ( \OutOfRangeException $e )
{
\IPS\Output::i()->error( 'node_error', '2C260/1', 404, '' );
}
if ( \IPS\Theme::designersModeEnabled() )
{
\IPS\Theme\Advanced\Theme::loadLanguage( $theme->id );
}
/* What tabs do we have? */
$tabs = array();
foreach ( \IPS\Db::i()->select( 'sc_tab_key', 'core_theme_settings_fields', array( 'sc_set_id=?', $theme->id ), NULL, NULL, 'sc_tab_key' ) as $tab )
{
$tabs[ $tab ] = 'theme_custom_tab_' . $tab;
}
/* Which is the active tab? */
if ( isset( \IPS\Request::i()->tab ) and isset( $tabs[ \IPS\Request::i()->tab ] ) )
{
$activeTab = \IPS\Request::i()->tab;
}
else
{
$_tabs = array_keys( $tabs );
$activeTab = array_shift( $_tabs );
}
/* Create tree */
$self = $this;
$tree = new \IPS\Helpers\Tree\Tree(
\IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id={$theme->id}" ),
"TITLE",
// getRoots
function() use( $theme, $activeTab, $self )
{
$return = array();
foreach( \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_set_id=? AND sc_tab_key=?', $theme->id, $activeTab ), 'sc_order' ) as $row )
{
$return[ $row['sc_id'] ] = call_user_func( array( $self, '_settingRow' ), $row );
}
return $return;
},
// getRow
function( $id ) use ( $self )
{
return call_user_func( array( $self, '_settingRow' ), \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_id=?', $id ) )->first() );
},
// getRowParentId
function()
{
return NULL;
},
// getChildren
function()
{
return array();
},
// getRootButtons
function()
{
return array();
},
FALSE,
TRUE,
TRUE
);
/* If this is an AJAX request, just return it */
if( \IPS\Request::i()->isAjax() )
{
\IPS\Output::i()->output = (string) $tree;
return;
}
/* Output */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->tabs( $tabs, $activeTab, $tree, \IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id={$theme->id}" ) );
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('theme_custom_setting_page_title');
if ( \IPS\Member::loggedIn()->hasAcpRestriction( 'core', 'customization', 'theme_settings_add' ) )
{
\IPS\Output::i()->sidebar['actions']['add'] = array(
'icon' => 'plus',
'title' => 'theme_custom_setting_add',
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('theme_custom_setting_add') ),
'link' => \IPS\Http\Url::internal("app=core&module=customization&controller=themesettings&set_id={$theme->id}&do=form"),
);
}
}
/**
* Get row for tree
*
* @param array $setting The row from the database
* @return string
*/
public function _settingRow( $setting )
{
$buttons = array();
if ( \IPS\Member::loggedIn()->hasAcpRestriction( 'core', 'customization', 'theme_settings_add' ) )
{
$buttons['edit'] = array(
'icon' => 'pencil',
'title' => 'edit',
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack( $setting['sc_title'] ) ),
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&do=form&id=' ) . $setting['sc_id'] . '&set_id=' . $setting['sc_set_id'],
);
}
if ( \IPS\Member::loggedIn()->hasAcpRestriction( 'core', 'customization', 'theme_settings_remove' ) )
{
$buttons['delete'] = array(
'icon' => 'times-circle',
'title' => 'delete',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&do=delete&id=' ) . $setting['sc_id'] . '&set_id=' . $setting['sc_set_id'],
'data' => array( 'delete' => '' ),
);
}
return \IPS\Theme::i()->getTemplate( 'trees', 'core' )->row(
\IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id={$setting['sc_set_id']}" ),
$setting['sc_id'],
\IPS\Member::loggedIn()->language()->addToStack( $setting['sc_title'] ),
FALSE,
$buttons,
$setting['sc_key'],
NULL,
$setting['sc_order'],
FALSE,
NULL,
NULL,
NULL,
FALSE,
FALSE,
FALSE
);
}
/**
* Tree sorting calls to do=reorder, but we want to let the individual tabs handle that in this case.
* This method just takes the request and passes it to manage(), which in turn passes it to the correct tab, which then finally handles the reordering.
*
* @return void
*/
public function reorder()
{
/* Normalise AJAX vs non-AJAX */
if( isset( \IPS\Request::i()->ajax_order ) )
{
$order = array();
$position = 1;
foreach( \IPS\Request::i()->ajax_order as $id => $parent )
{
$order[ $id ] = $position++;
}
}
/* Non-AJAX way */
else
{
$order = \IPS\Request::i()->order;
}
/* Okay, now order */
foreach( $order as $id => $position )
{
\IPS\Db::i()->update( 'core_theme_settings_fields', array( 'sc_order' => $position ), array( 'sc_id=?', $id ) );
}
/* Write themesettings.json for the default theme */
if ( \IPS\IN_DEV and \IPS\Request::i()->set_id == 1 )
{
\IPS\Theme\Dev\Theme::writeThemeSettingsToDisk();
}
/* If this is an AJAX request, just respond */
if( \IPS\Request::i()->isAjax() )
{
return;
}
/* Otherwise, redirect */
else
{
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id=" . \IPS\Request::i()->set_id ) );
}
}
/**
* Add/Edit Theme Setting
*
* @return void
*/
public function form()
{
$current = NULL;
$setId = intval( \IPS\Request::i()->set_id );
$id = intval( \IPS\Request::i()->id );
$set = \IPS\Theme::load( $setId );
if ( $id )
{
$current = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_id=?', $id ) )->first();
}
else
{
/* Check permission */
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_settings_add' );
}
/* Apps */
$apps = array();
foreach( \IPS\Application::applications() as $key => $data )
{
$apps[ $key ] = $data->_title;
}
/* Tab keys */
$tabs = iterator_to_array( \IPS\Db::i()->select( 'sc_tab_key', 'core_theme_settings_fields', array( 'sc_set_id=?', $setId ) )->setKeyField( 'sc_tab_key' )->setValueField('sc_tab_key') );
/* Build form */
$form = new \IPS\Helpers\Form();
$form->hiddenValues['set_id'] = $setId;
$form->add( new \IPS\Helpers\Form\Text( 'theme_custom_setting_title', $current['sc_title'], TRUE ) );
$form->add( new \IPS\Helpers\Form\Select( 'theme_custom_setting_app', $id ? $current['sc_app'] : null, TRUE, array( 'options' => $apps ) ) );
$form->add( new \IPS\Helpers\Form\Text( 'theme_custom_setting_key' , ( $id ? $current['sc_key'] : null ), TRUE, array(
'placeholder' => \IPS\Member::loggedIn()->language()->addToStack('theme_custom_setting_key_placeholder')
),
function( $val )
{
/* Make sure key is unique */
try
{
$row = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( "sc_set_id=? AND sc_key=?", \IPS\Request::i()->set_id, $val ) )->first();
if ( isset( \IPS\Request::i()->id ) )
{
if ( $row['sc_id'] != \IPS\Request::i()->id )
{
throw new \InvalidArgumentException('core_theme_settings_error_key_not_unique');
}
}
}
catch( \UnderflowException $e )
{
/* Key is OK as select failed */
}
return true;
} ) );
if ( count( $tabs ) )
{
$form->add( new \IPS\Helpers\Form\Radio( 'theme_custom_setting_tab_key_type', 'existing', FALSE, array(
'options' => array( 'existing' => 'theme_custom_setting_tab_key_o_existing',
'new' => 'theme_custom_setting_tab_key_o_new' ),
'toggles' => array( 'existing' => array( 'theme_template_tab_key_existing' ),
'new' => array( 'theme_template_tab_key_new' ) )
) ) );
$form->add( new \IPS\Helpers\Form\Select( 'theme_template_tab_key_existing', $id ? $current['sc_tab_key'] : null, TRUE, array( 'options' => array_map( function( $item )
{
return ( \IPS\Member::loggedIn()->language()->addToStack('theme_custom_tab_' . $item) ) ?: $item;
}, $tabs ) ), NULL, NULL, NULL, 'theme_template_tab_key_existing' ) );
}
else
{
$form->hiddenValues['theme_custom_setting_tab_key_type'] = 'new';
}
$form->add( new \IPS\Helpers\Form\Translatable( 'theme_template_tab_key_new', NULL, FALSE, array(
'app' => 'core',
'key' => ( $current ? "theme_custom_tab_{$current['sc_tab_key']}" : NULL )
),
function( $val )
{
$val = preg_replace( '#[^0-9a-z_]#', '', mb_strtolower( $val[ \IPS\Lang::defaultLanguage() ] ) );
if ( \IPS\Request::i()->theme_custom_setting_tab_key_type === 'new' )
{
/* Make sure its unique */
try
{
$row = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( "sc_set_id=? AND sc_tab_key=?", \IPS\Request::i()->set_id, $val ) )->first();
if ( isset( \IPS\Request::i()->id ) )
{
if ( $row['sc_id'] != \IPS\Request::i()->id )
{
throw new \InvalidArgumentException('core_theme_settings_error_tab_key_not_unique');
}
}
}
catch( \UnderflowException $e )
{
/* Key is OK as select failed */
}
}
return true;
}, 'theme_custom_tab_', NULL, 'theme_template_tab_key_new' ) );
$form->add( new \IPS\Helpers\Form\Select( 'theme_custom_setting_type', $id ? $current['sc_type'] : 'Text', TRUE, array(
'options' => array(
'Editor' => 'pf_type_Editor',
'Number' => 'pf_type_Number',
'Radio' => 'pf_type_Radio',
'Select' => 'pf_type_Select',
'Text' => 'pf_type_Text',
'TextArea' => 'pf_type_TextArea',
'Upload' => 'pf_type_Upload',
'Url' => 'pf_type_Url',
'Color' => 'pf_type_Color',
'YesNo' => 'pf_type_YesNo',
'other' => 'theme_custom_setting_type_other',
),
'toggles' => array(
'Radio' => array( 'sc_content' ),
'Select' => array( 'sc_content', 'sc_multiple' ),
'other' => array( 'sc_manual_code' )
)
) ) );
$form->add( new \IPS\Helpers\Form\Stack( 'theme_custom_setting_type_content', ( $id and $current['sc_type'] != 'other' ) ? json_decode( $current['sc_content'], TRUE ) : array(), FALSE, array( 'stackFieldType' => 'KeyValue' ), NULL, NULL, NULL, 'sc_content' ) );
$form->add( new \IPS\Helpers\Form\YesNo( 'theme_custom_setting_type_multiple', $id ? $current['sc_multiple'] : FALSE, FALSE, array(), NULL, NULL, NULL, 'sc_multiple' ) );
$form->add( new \IPS\Helpers\Form\Codemirror( 'theme_custom_setting_manual_code', '<?php' . "\n\n" . ( ( $id and $current['sc_type'] == 'other' ) ? $current['sc_content'] : 'return new \IPS\Helpers\Form\Text( "core_theme_setting_title_{$row[\'sc_id\']}", $value, FALSE, array(), NULL, NULL, NULL, \'theme_setting_\' . $row[\'sc_key\'] );' ), NULL, array( 'mode' => 'php' ), function( $val )
{
$val = trim( preg_replace( '/^<\?php(.*)$/is', '$1', trim( $val ) ) );
if ( $val )
{
$row = array( 'sc_id' => 0, 'sc_key' => \IPS\Request::i()->theme_custom_setting_key );
$value = NULL;
if ( !( eval( $val ) instanceof \IPS\Helpers\Form\FormAbstract ) )
{
throw new \DomainException('theme_custom_setting_manual_code_bad');
}
}
elseif ( \IPS\Request::i()->theme_custom_setting_type == 'other' )
{
throw new \DomainException('form_required');
}
}, NULL, NULL, 'sc_manual_code' ) );
$form->add( new \IPS\Helpers\Form\Text( 'theme_custom_setting_type_default', $id ? $current['sc_default'] : FALSE, FALSE, array(), NULL, NULL, NULL, 'sc_default' ) );
if ( \IPS\IN_DEV )
{
$form->add( new \IPS\Helpers\Form\YesNo( 'theme_custom_setting_show_in_vse', $id ? $current['sc_show_in_vse'] : FALSE, FALSE, array(), NULL, NULL, NULL, 'sc_show_in_vse' ) );
}
if ( $values = $form->values() )
{
$save = array(
'sc_set_id' => $setId,
'sc_key' => $values['theme_custom_setting_key'],
'sc_type' => $values['theme_custom_setting_type'],
'sc_default' => $values['theme_custom_setting_type_default'],
'sc_multiple' => $values['theme_custom_setting_type_multiple'],
'sc_updated' => time(),
'sc_app' => $values['theme_custom_setting_app'],
'sc_show_in_vse' => ( isset( $values['theme_custom_setting_show_in_vse'] ) ) ? $values['theme_custom_setting_show_in_vse'] : 0,
'sc_content' => ( $values['theme_custom_setting_type'] === 'other' ) ? trim( preg_replace( '/^<\?php(.*)$/is', '$1', trim( $values['theme_custom_setting_manual_code'] ) ) ) : json_encode( $values['theme_custom_setting_type_content'] ),
'sc_title' => $values['theme_custom_setting_title'],
);
$type = ( isset( \IPS\Request::i()->theme_custom_setting_tab_key_type ) ) ? \IPS\Request::i()->theme_custom_setting_tab_key_type : $values['theme_custom_setting_tab_key_type'];
if ( $type === 'existing' )
{
$save['sc_tab_key'] = $values['theme_template_tab_key_existing'];
}
else
{
$save['sc_tab_key'] = preg_replace( '#[^0-9a-z]#', '', mb_strtolower( $values['theme_template_tab_key_new'][ \IPS\Lang::defaultLanguage() ] ) );
}
if ( $current )
{
\IPS\Db::i()->update( 'core_theme_settings_fields', $save, array( 'sc_id=?', $current['sc_id'] ) );
$id = $current['sc_id'];
\IPS\Session::i()->log( 'acplogs__theme_setting_edited', array( $save['sc_key'] => FALSE ) );
}
else
{
$save['sc_order'] = \IPS\Db::i()->select( 'MAX(sc_order)', 'core_theme_settings_fields', array( 'sc_set_id=? AND sc_tab_key=?', $setId, $save['sc_tab_key'] ) )->first() + 1;
$id = \IPS\Db::i()->insert( 'core_theme_settings_fields', $save );
\IPS\Session::i()->log( 'acplogs__theme_setting_created', array( $save['sc_key'] => FALSE ) );
}
/* Default is changed, change value? */
if ( $save['sc_default'] !== $current['sc_default'] )
{
try
{
$value = \IPS\Db::i()->select( '*', 'core_theme_settings_values', array( "sv_id=?", $id ) )->first();
if ( $value['sv_value'] == $current['sc_default'] )
{
\IPS\Db::i()->update( 'core_theme_settings_values', array( 'sv_value' => $save['sc_default'] ), array( "sv_id=?", $id ) );
}
}
catch( \UnderFlowException $e ) {}
}
if ( $type === 'new' )
{
\IPS\Lang::saveCustom( 'core', "theme_custom_tab_" . $save['sc_tab_key'], $values['theme_template_tab_key_new'] );
}
if ( \IPS\IN_DEV AND $setId === 1 )
{
\IPS\Theme\Dev\Theme::writeThemeSettingsToDisk();
}
if ( ! $current )
{
$field = $set->getCustomSettingField( array_merge( $save, array( 'sc_id' => $id ) ) );
$themeSettings = json_decode( $set->template_settings, TRUE );
$themeSettings[ $values['theme_custom_setting_key'] ] = $field::stringValue( $values['theme_custom_setting_type_default'] );
$set->template_settings = json_encode( $themeSettings );
$set->save();
}
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&set_id=' . $setId ), 'saved' );
}
/* Display */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->block( 'theme_set_custom_setting', $form, FALSE );
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack( 'core_theme_set_title_' . $setId );
}
/**
* Delete
*
* @return void
*/
public function delete()
{
$setId = intval( \IPS\Request::i()->set_id );
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_settings_remove' );
try
{
$current = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_id=?', \IPS\Request::i()->id ) )->first();
\IPS\Session::i()->log( 'acplogs__theme_setting_deleted', array( $current['sc_key'] => FALSE ) );
\IPS\Db::i()->delete( 'core_theme_settings_fields', array( 'sc_id=?', \IPS\Request::i()->id ) );
}
catch ( \Exception $e ) { }
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&set_id=' . $setId ) );
}
}