Skip to content

Commit

Permalink
Merge pull request #14052 from jdalsem/4.3-features
Browse files Browse the repository at this point in the history
feat(icons): added FontAwesome zip upload so admins can update icons
  • Loading branch information
jdalsem committed Jun 20, 2022
2 parents 6cc557d + 7be4ca6 commit 12d91e0
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 18 deletions.
38 changes: 38 additions & 0 deletions actions/admin/site/icons.php
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\HttpFoundation\File\UploadedFile;

$site = elgg_get_site_entity();

if (get_input('icon_remove')) {
Expand All @@ -10,4 +12,40 @@
$site->saveIconFromUploadedFile('icon', 'favicon');
}

$remove_zip = function() {
elgg_delete_directory(elgg_get_data_path() . 'fontawesome');
elgg_remove_config('font_awesome_zip');

// view locations and simplecache need to be updated
elgg_invalidate_caches();
};

$zip = elgg_get_uploaded_file('font_awesome_zip');
if (get_input('remove_font_awesome_zip')) {
$remove_zip();
} elseif ($zip instanceof UploadedFile && extension_loaded('zip')) {
$archive = new \ZipArchive();
if ($archive->open($zip->getPathname()) !== true) {
return elgg_error_response(elgg_echo('admin:site_icons:font_awesome:zip:error'));
}

// remove existing upload
$remove_zip();

// extraction could take some time
set_time_limit(0);

$base_folder = elgg_get_data_path() . 'fontawesome';
$archive->extractTo($base_folder);

// rename folder for easier mapping in the future
foreach (glob($base_folder . '/*', GLOB_ONLYDIR) as $item) {
// rename the first folder we find
rename($item, $base_folder . '/webfont');
break;
}

elgg_save_config('font_awesome_zip', $zip->getClientOriginalName());
}

return elgg_ok_response(elgg_echo('save:success'));
5 changes: 5 additions & 0 deletions engine/classes/Elgg/Cache/ViewCacher.php
Expand Up @@ -53,6 +53,11 @@ public function registerCoreViews() {

$spec = Includer::includeFile($file);
if (is_array($spec)) {
// check for uploaded fontawesome font
if (elgg_get_config('font_awesome_zip')) {
$spec['default']['font-awesome/'] = elgg_get_data_path() . 'fontawesome/webfont/';
}

$this->views->mergeViewsSpec($spec);
}
}
Expand Down
54 changes: 50 additions & 4 deletions engine/classes/Elgg/Traits/Entity/ProfileData.php
Expand Up @@ -9,6 +9,11 @@
* @since 3.1
*/
trait ProfileData {

/**
* @var array all profile data
*/
protected $_profile_data = [];

/**
* Store profile data
Expand Down Expand Up @@ -37,6 +42,8 @@ public function setProfileData(string $profile_field_name, $value, int $access_i
$this->annotate("profile:{$profile_field_name}", $v, $access_id, $this->guid);
}

$this->_profile_data = null;

// for BC, keep storing fields in MD, but we'll read annotations only
$this->$profile_field_name = $value;
}
Expand All @@ -49,10 +56,16 @@ public function setProfileData(string $profile_field_name, $value, int $access_i
* @return null|mixed null if no profile data was found
*/
public function getProfileData(string $profile_field_name) {
$annotations = $this->getAnnotations([
'annotation_name' => "profile:{$profile_field_name}",
'limit' => false,
]);

if (empty($this->guid)) {
// no way to return all temp annotations for an unsaved entity
$annotations = $this->getAnnotations([
'annotation_name' => "profile:{$profile_field_name}",
'limit' => false,
]);
} else {
$annotations = elgg_extract("profile:{$profile_field_name}", $this->getAllProfileAnnotations());
}

if (empty($annotations)) {
return null;
Expand Down Expand Up @@ -80,6 +93,37 @@ public function getProfileData(string $profile_field_name) {
return $result;
}

/**
* Returns all profile annotations
*
* @return array
*/
protected function getAllProfileAnnotations(): array {
// store logged in user guid to prevent unwanted access to annotations when switching logged in user during script run (e.g. ElggCoreUserTest)
$logged_in_user_guid = elgg_get_logged_in_user_guid();
if (!isset($this->_profile_data[$logged_in_user_guid])) {
$annotations = $this->getAnnotations([
'limit' => false,
'wheres' => function(\Elgg\Database\QueryBuilder $qb, $main_alias) {
return $qb->compare("{$main_alias}.name", 'LIKE', 'profile:%', ELGG_VALUE_STRING);
},
]);

$profile_data = [];
foreach ($annotations as $annotation) {
if (!isset($profile_data[$annotation->name])) {
$profile_data[$annotation->name] = [];
}

$profile_data[$annotation->name][] = $annotation;
}

$this->_profile_data[$logged_in_user_guid] = $profile_data;
}

return $this->_profile_data[$logged_in_user_guid];
}

/**
* Remove profile data
*
Expand All @@ -91,6 +135,8 @@ public function deleteProfileData(string $profile_field_name) {
$result = $this->deleteAnnotations("profile:{$profile_field_name}");
$result &= $this->deleteMetadata($profile_field_name);

$this->_profile_data = null;

return $result;
}
}
5 changes: 5 additions & 0 deletions languages/en.php
Expand Up @@ -590,6 +590,11 @@
'admin:site_icons' => "Site Icons",
'admin:site_icons:site_icon' => "Site Icon",
'admin:site_icons:info' => "Upload an icon related to your site. This icon will be used as a favicon and when showing the site for example as the sender in site notifications.",
'admin:site_icons:font_awesome' => "Font Awesome",
'admin:site_icons:font_awesome:zip' => "Upload ZIP file",
'admin:site_icons:font_awesome:zip:help' => "Here you can upload a Font Awesome download from https://fontawesome.com/download. This webfont will be served locally.",
'admin:site_icons:font_awesome:zip:error' => "Uploaded ZIP can not be extracted",
'admin:site_icons:font_awesome:remove_zip' => "Remove uploaded font",
'admin:site_settings' => "Site Settings",
'admin:site:description' => "This admin panel allows you to control global settings for your site. Choose an option below to get started.",
'admin:site:opt:linktext' => "Configure site...",
Expand Down
5 changes: 1 addition & 4 deletions mod/profile/views/default/forms/profile/edit.php
Expand Up @@ -2,10 +2,7 @@
/**
* Edit profile form
*
* @tip Use 'profile:fields','profile' hook to modify profile fields configuration.
* Profile fields are configuration as an array of $shortname => $input_type pairs,
* where $shortname is the metadata name used to store the value, and the $input_type is
* an input view used to render the field input element.
* @tip Use 'fields', 'user:user' hook to modify profile fields configuration.
*
* @uses vars['entity']
*/
Expand Down
22 changes: 12 additions & 10 deletions views/default/admin/cron.php
Expand Up @@ -28,19 +28,21 @@

// cron output
$msg = $cron_service->getLog('output', $period);
if ($msg) {
if (!empty($msg)) {
$msg = nl2br($msg);
}

if (!empty($msg) && elgg_in_context('widgets')) {
$wrapped_message = elgg_format_element('div', [
'id' => "cron_{$period}",
'class' => 'hidden',
], $msg);

$msg = elgg_view_url("#cron_{$period}", elgg_echo('show'), ['rel' => 'toggle']);
$msg .= $wrapped_message;
if (elgg_in_context('widgets')) {
$msg = elgg_format_element('div', [], $msg);
$msg = elgg_view('output/url', [
'href' => false,
'text' => false,
'icon' => 'info',
'class' => ['elgg-lightbox'],
'data-colorbox-opts' => json_encode(['html' => $msg]),
]);
}
}

$row[] = elgg_format_element('td', [], $msg);

$table_content .= elgg_format_element('tr', [], implode(PHP_EOL, $row));
Expand Down
24 changes: 24 additions & 0 deletions views/default/forms/admin/site/icons.php
Expand Up @@ -9,6 +9,30 @@

echo elgg_view_module('info', elgg_echo('admin:site_icons:site_icon'), $site_icon);

if (extension_loaded('zip')) {
$current_zip = elgg_get_config('font_awesome_zip');
$fa = elgg_view_field([
'#type' => 'file',
'#label' => elgg_echo('admin:site_icons:font_awesome:zip'),
'#help' => elgg_echo('admin:site_icons:font_awesome:zip:help'),
'name' => 'font_awesome_zip',
'value' => $current_zip,
'accept' => '.zip',
]);

if ($current_zip) {
$fa .= elgg_view_field([
'#type' => 'checkbox',
'#label' => elgg_echo('admin:site_icons:font_awesome:remove_zip'),
'name' => 'remove_font_awesome_zip',
'value' => 1,
'switch' => true,
]);
}

echo elgg_view_module('info', elgg_echo('admin:site_icons:font_awesome'), $fa);
}

$footer = elgg_view_field([
'#type' => 'submit',
'value' => elgg_echo('save'),
Expand Down

0 comments on commit 12d91e0

Please sign in to comment.