Skip to content

Commit

Permalink
Merge pull request #11 from iMattPro/delete-icons
Browse files Browse the repository at this point in the history
Allow deleting icons from ACP
  • Loading branch information
iMattPro authored Dec 30, 2024
2 parents 0ce59ae + 4f9c292 commit 293c1e0
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Under development...
## To Do

- [x] Track existing images during install?
- [ ] Ability to delete images from acp
- [x] Ability to delete images from acp
- [x] Add a color picker next to the color fields
- [ ] Support multiple styles for color assignments
- [ ] Allow editing of site name and short name in ACP too
Expand Down
2 changes: 1 addition & 1 deletion acp/pwa_acp_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public function main($id, string $mode): void
$this->tpl_name = 'acp_pwakit';
$this->page_title = 'ACP_PWA_KIT_SETTINGS';

$admin_controller->main($mode);
$admin_controller->main($id, $mode);
}
}
14 changes: 12 additions & 2 deletions adm/style/acp_pwakit.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ <h3>{{ lang('WARNING') }}</h3>
<dt><label>{{ lang('ACP_PWA_KIT_ICONS') ~ lang('COLON') }}</label><br><span>{{ lang('ACP_PWA_KIT_ICONS_EXPLAIN') }}</span></dt>
<dd>
{% for icon in PWA_KIT_ICONS %}
<p>
<img src="{{ icon.src }}" alt="{{ lang('ACP_PWA_KIT_ICONS_ALT') }}"><br>{{ icon.src|split('/')|last }}<br>{{ icon.sizes }}
{% set iconName = icon.src|split('/')|last %}
<p style="position: relative;">
<span class="delete-btn-container">
<img src="{{ icon.src }}" alt="{{ iconName|e("html") }}">
<button class="delete-btn" id="delete" name="delete" value="{{ iconName }}" title="{{ lang('ACP_PWA_IMG_DELETE') }}">
<span class="fa-stack fa-2x">
{{ Icon('font', 'circle', '', true, 'fa-stack-2x fa-inverse') }}
{{ Icon('font', 'trash-can', '', true, 'fa-stack-1x') }}
</span>
</button>
</span><br>
{{ iconName }}<br>{{ icon.sizes }}
</p>
{% else %}
{{ lang('ACP_PWA_KIT_NO_ICONS') }}
Expand Down
45 changes: 44 additions & 1 deletion controller/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

class admin_controller
{
/** @var string $id */
public string $id;

/** @var string $u_action */
public string $u_action;

Expand Down Expand Up @@ -88,11 +91,14 @@ public function set_page_url(string $u_action): void
/**
* Main ACP module
*
* @param string $id
* @param string $mode
* @return void
*/
public function main(string $mode = ''): void
public function main(string $id, string $mode = ''): void
{
$this->id = $id;

if ($mode !== 'settings')
{
return;
Expand All @@ -104,6 +110,7 @@ public function main(string $mode = ''): void
$submit = $this->request->is_set_post('submit');
$upload = $this->request->is_set_post('upload');
$resync = $this->request->is_set_post('resync');
$delete = $this->request->is_set_post('delete');

if ($submit || $upload || $resync)
{
Expand All @@ -125,6 +132,10 @@ public function main(string $mode = ''): void
$this->save_settings();
}
}
else if ($delete)
{
$this->delete();
}

$this->display_settings();
}
Expand Down Expand Up @@ -245,6 +256,38 @@ public function upload(): void
$this->success('CONFIG_UPDATED');
}

/**
* Delete image
*
* @return void
*/
public function delete(): void
{
$path = $this->request->variable('delete', '');

if (confirm_box(true))
{
try
{
$this->helper->delete_icon($path);
$this->success($this->language->lang('ACP_PWA_IMG_DELETED', $path));
}
catch (runtime_exception $e)
{
$this->error($this->language->lang('ACP_PWA_IMG_DELETE_ERROR', $this->language->lang($e->getMessage())));
}
}
else
{
confirm_box(false, 'ACP_PWA_IMG_DELETE', build_hidden_fields(array(
'i' => $this->id,
'mode' => 'settings',
'delete' => $path,
'action' => $this->u_action,
)));
}
}

/**
* Trigger success message
*
Expand Down
34 changes: 34 additions & 0 deletions helper/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace phpbb\pwakit\helper;

use FastImageSize\FastImageSize;
use phpbb\exception\runtime_exception;
use phpbb\extension\manager as ext_manager;
use phpbb\pwakit\storage\storage;
use phpbb\storage\exception\storage_exception;
Expand Down Expand Up @@ -114,6 +115,39 @@ public function resync_icons(): void
}
}

/**
* Delete icon from storage and remove it from the storage table
*
* @param string $path
* @throws runtime_exception
* @return void
*/
public function delete_icon(string $path): void
{
if (empty($path))
{
throw new runtime_exception('ACP_PWA_IMG_DELETE_PATH_ERR');
}

// Remove any directory traversal attempts
$path = basename($path);

// Check for valid filename characters
if (!preg_match('/^[a-zA-Z0-9_\-.]+$/', $path))
{
throw new runtime_exception('ACP_PWA_IMG_DELETE_NAME_ERR');
}

try
{
$this->storage->delete($path);
}
catch (storage_exception $e)
{
throw new runtime_exception($e->getMessage());
}
}

/**
* Get an array of all image paths from the storage table
*
Expand Down
7 changes: 6 additions & 1 deletion language/en/acp_pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@
'ACP_PWA_BG_COLOR_EXPLAIN' => 'Used to specify an initial background colour for your web application. This colour appears in the application window before your application’s stylesheets have loaded.',
'ACP_PWA_KIT_ICONS' => 'Web application icons',
'ACP_PWA_KIT_ICONS_EXPLAIN' => 'PNG image files that represent your web application. Multiple sizes are preferred for compatibility with various devices.',
'ACP_PWA_KIT_ICONS_ALT' => 'Web App Manifest Icon',
'ACP_PWA_KIT_NO_ICONS' => 'No icons are available. Click <strong>Upload</strong> to add new icons or click <strong>Resync</strong> to find existing icons that were previously uploaded.',
'ACP_PWA_IMG_UPLOAD' => 'Upload web application icons',
'ACP_PWA_IMG_UPLOAD_EXPLAIN' => 'Upload PNG images to <samp>%s</samp>.',
'ACP_PWA_IMG_DELETE' => 'Delete image',
'ACP_PWA_IMG_DELETE_CONFIRM' => 'Are you sure you want to delete this image?',
'ACP_PWA_IMG_DELETE_ERROR' => 'An error occurred while trying to remove the image. %s',
'ACP_PWA_IMG_DELETED' => '“%s” has been removed.',
'ACP_PWA_IMG_DELETE_PATH_ERR' => 'Invalid file path provided.',
'ACP_PWA_IMG_DELETE_NAME_ERR' => 'Invalid characters in filename.',
'PWA_IMG_RESYNC_BTN' => 'Resync',
'PWA_IMG_UPLOAD_BTN' => 'Upload',
'ACP_PWA_INVALID_COLOR' => 'The colour code “<samp>%s</samp>” is not a valid hex code.',
Expand Down
30 changes: 30 additions & 0 deletions styles/all/theme/acp.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,33 @@ input[type="color"] {
padding: 0;
cursor: pointer;
}

.delete-btn-container {
position: relative;
display: inline-block;
}

.delete-btn {
font-size: 7px;
background-color: #ffffff;
border: none;
border-radius: 0 0 0 50%;
color: #8b0000;
position: absolute;
top: 0;
right: 0;
display: none;
cursor: pointer;
}

.delete-btn-container:hover .delete-btn {
display: block;
}

.delete-btn:hover {
color: #ff0000;
}

.delete-btn:active {
color: #8b0000;
}

0 comments on commit 293c1e0

Please sign in to comment.