* Admin: Implement new feature to upload multiple images for login background image in site-configuration and show them randomly.

This commit is contained in:
Hadi Nategh 2018-05-28 15:31:32 +02:00
parent fefe0e803f
commit 6aeb7a5c1e
4 changed files with 95 additions and 17 deletions

View File

@ -22,12 +22,13 @@ class admin_config
/** /**
* Upload function to store anonymous images into instance files_dir/anon_images * Upload function to store anonymous images into instance files_dir/anon_images
* *
* @param type $file file info array * @param array $file file info array
* @param type $dir directory to store file * @param array|string $value current value of login_background_file
* *
*/ */
function ajax_upload_anon_images ($file) function ajax_upload_anon_images ($file, $value)
{ {
if (!isset($GLOBALS['egw_info']['user']['apps']['admin'])) die('no rights to be here!');
$path = $GLOBALS['egw_info']['server']['files_dir'].'/anon-images'; $path = $GLOBALS['egw_info']['server']['files_dir'].'/anon-images';
$success = false; $success = false;
$response = Api\Json\Response::get(); $response = Api\Json\Response::get();
@ -40,9 +41,10 @@ class admin_config
} }
if ($success) if ($success)
{ {
$response->data(array( $value = array_merge($value, array(
'path' => $GLOBALS['egw_info']['server']['webserver_url'].'/api/anon_images.php?src='.urlencode($file[$tmp_file[0]]['name']).'&'.filemtime($destination) $GLOBALS['egw_info']['server']['webserver_url'].'/api/anon_images.php?src='.urlencode($file[$tmp_file[0]]['name']).'&'.filemtime($destination),
)); ));
$response->data($value);
} }
else else
{ {
@ -50,6 +52,36 @@ class admin_config
} }
} }
/**
* Removes images from anon-images directory
*
* @param type $files
*/
function remove_anon_images ($files)
{
if (!isset($GLOBALS['egw_info']['user']['apps']['admin'])) die('no rights to be here!');
if ($files)
{
$base = $GLOBALS['egw_info']['server']['files_dir'].'/anon-images';
foreach ($files as $file)
{
$parts = explode('anon_images.php?src=', $file);
$parts = explode('&', $parts[1]);
$path = $base.'/'.urldecode($parts[0]);
if (is_writable(dirname($base)) && file_exists($path))
{
unlink($path);
}
}
}
}
/**
* List of configs
*
* @param type $_content
* @throws Api\Exception\WrongParameter
*/
function index($_content=null) function index($_content=null)
{ {
if (is_array($_content)) if (is_array($_content))
@ -107,6 +139,9 @@ class admin_config
// support old validation hooks // support old validation hooks
$_POST = array('newsettings' => &$_content['newsettings']); $_POST = array('newsettings' => &$_content['newsettings']);
// Remove actual files (cleanup) of deselected urls from login_background_file
$this->remove_anon_images(array_diff($c->config_data['login_background_file'], $_content['newsettings']['login_background_file']));
/* Load hook file with functions to validate each config (one/none/all) */ /* Load hook file with functions to validate each config (one/none/all) */
Api\Hooks::single(array( Api\Hooks::single(array(
'location' => 'config_validate', 'location' => 'config_validate',

View File

@ -1273,5 +1273,21 @@ app.classes.admin = AppJS.extend(
resizable: false, resizable: false,
position: 'left top' position: 'left top'
}, et2_dialog._create_parent('mail')); }, et2_dialog._create_parent('mail'));
},
/**
* Triggers upload for background image and updates its taglist
*
* @param {type} node
* @param {type} widget
*/
login_background_update: function(node, widget)
{
var taglist = widget._parent._children[0];
egw.json('admin.admin_config.ajax_upload_anon_images',
[widget.get_value(), taglist.get_value()],
function(_data){
taglist.set_value(_data);
}).sendRequest()
} }
}); });

View File

@ -77,10 +77,13 @@
<textbox id="newsettings[login_logo_title]" width="100%"/> <textbox id="newsettings[login_logo_title]" width="100%"/>
</row> </row>
<row> <row>
<description value="Upload your background image or enter the URL" label="%s:"/>
<vbox> <vbox>
<textbox id="newsettings[login_background_file]" width="100%"/> <description value="Upload your background image or enter the URL" label="%s:"/>
<file width="100%" label="upload" onchange="egw.json('admin.admin_config.ajax_upload_anon_images',[widget.get_value()], function(_data){widget._parent._children[0].set_value(_data.path)}).sendRequest()"/> <description value="If you wish to have randomly selected images you may upload multiple images."/>
</vbox>
<vbox>
<taglist id="newsettings[login_background_file]" width="100%" allowFreeEntries="true" empty_label="Upload your background image or enter the URL"/>
<file width="100%" label="upload" onchange="app.admin.login_background_update"/>
</vbox> </vbox>
</row> </row>
<row> <row>

View File

@ -172,15 +172,9 @@ class Login
$tmpl->set_var('website_title', $GLOBALS['egw_info']['server']['site_title']); $tmpl->set_var('website_title', $GLOBALS['egw_info']['server']['site_title']);
$tmpl->set_var('template_set',$this->framework->template); $tmpl->set_var('template_set',$this->framework->template);
if (substr($GLOBALS['egw_info']['server']['login_background_file'], 0, 4) == 'http' ||
$GLOBALS['egw_info']['server']['login_background_file'][0] == '/') $var['background_file'] = self::pick_login_background($GLOBALS['egw_info']['server']['login_background_file']);
{
$var['background_file'] = $GLOBALS['egw_info']['server']['login_background_file'];
}
else
{
$var['background_file'] = Api\Image::find('api',$GLOBALS['egw_info']['server']['login_background_file']?$GLOBALS['egw_info']['server']['login_background_file']:'login_background', '', null);
}
if (substr($GLOBALS['egw_info']['server']['login_logo_file'], 0, 4) == 'http' || if (substr($GLOBALS['egw_info']['server']['login_logo_file'], 0, 4) == 'http' ||
$GLOBALS['egw_info']['server']['login_logo_file'][0] == '/') $GLOBALS['egw_info']['server']['login_logo_file'][0] == '/')
{ {
@ -245,6 +239,36 @@ class Login
$this->framework->render($tmpl->fp('loginout','login_form'),false,false); $this->framework->render($tmpl->fp('loginout','login_form'),false,false);
} }
/**
* Function to pick login background from given values. It picks them randomly
* if there's more than one image in the list.
*
* @param array|string $backgrounds array of background urls or an url as string
*
* @return string returns full url of background image
*/
static function pick_login_background($backgrounds)
{
if (is_array($backgrounds))
{
$chosen = $backgrounds[rand(0, count($backgrounds)-1)];
}
else
{
$chosen = $backgrounds;
}
if (substr($chosen, 0, 4) == 'http' ||
$chosen[0] == '/')
{
return $chosen;
}
else
{
return Api\Image::find('api',$chosen ? $chosen : 'login_background', '', null);
}
}
/** /**
* displays a login denied message * displays a login denied message
*/ */