1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-16 19:54:24 +02:00

Update ProcessPageEditImageSelect to use proper HTML class name attributes for checkbox and radio buttons when used with AdminThemeUikit. Plus other minor code improvements

This commit is contained in:
Ryan Cramer
2023-06-23 13:47:34 -04:00
parent d6ba21fcaf
commit 18d2fdf94a
3 changed files with 58 additions and 46 deletions

View File

@@ -467,6 +467,7 @@ class ProcessLanguageTranslator extends Process implements ConfigurableModule {
$c = strpos($matches[1], '|') ? '|' : ','; $c = strpos($matches[1], '|') ? '|' : ',';
$options = explode($c, $matches[1]); $options = explode($c, $matches[1]);
$comment = str_replace($matches[0], '', $comment); $comment = str_replace($matches[0], '', $comment);
if(empty($type)) $type = 'Radios';
} else { } else {
$options = array(); $options = array();
} }

View File

@@ -224,10 +224,6 @@ small.ui-helper-clearfix {
display: inline; display: inline;
} }
#selected_image_link {
display: inline;
}
#instructions { #instructions {
margin: 1em 0; margin: 1em 0;
clear: both; clear: both;

View File

@@ -5,7 +5,7 @@
* *
* Provides the image selecting and editing capability for rich text editors (TinyMCE/CKEditor) * Provides the image selecting and editing capability for rich text editors (TinyMCE/CKEditor)
* *
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer * ProcessWire 3.x, Copyright 2023 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @property int $hidpiDefault HiDPI/Retina checkbox default checked? * @property int $hidpiDefault HiDPI/Retina checkbox default checked?
@@ -27,7 +27,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
return array( return array(
'title' => 'Page Edit Image', 'title' => 'Page Edit Image',
'summary' => 'Provides image manipulation functions for image fields and rich text editors.', 'summary' => 'Provides image manipulation functions for image fields and rich text editors.',
'version' => 120, 'version' => 121,
'permanent' => true, 'permanent' => true,
'permission' => 'page-edit', 'permission' => 'page-edit',
); );
@@ -64,8 +64,6 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
* *
*/ */
protected $editorPage = null; protected $editorPage = null;
//protected $defaultClass = 'Pageimage';
//protected $file = '';
/** /**
* If editing a filename that is a variation, this is the width determined from the filename (123x456) * If editing a filename that is a variation, this is the width determined from the filename (123x456)
@@ -374,7 +372,9 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$originalFilename = $this->wire()->sanitizer->filename($originalFilename, false, 1024); $originalFilename = $this->wire()->sanitizer->filename($originalFilename, false, 1024);
// if requested file does not match one of our allowed extensions, abort // if requested file does not match one of our allowed extensions, abort
if(!preg_match('/\.(' . $this->extensions . ')$/iD', $file, $matches)) throw new WireException("Unknown image file"); if(!preg_match('/\.(' . $this->extensions . ')$/iD', $file, $matches)) {
throw new WireException("Unknown image file");
}
// get the original, non resized version, if present // get the original, non resized version, if present
// format: w x h crop -suffix // format: w x h crop -suffix
@@ -394,7 +394,9 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$file = "{$this->page->id},$originalFilename"; $file = "{$this->page->id},$originalFilename";
// if requested file is not one that we have, abort // if requested file is not one that we have, abort
if(!array_key_exists($file, $images)) throw new WireException("Cannot find image file '$originalFilename' on page: {$this->page->path}"); if(!array_key_exists($file, $images)) {
throw new WireException("Cannot find image file '$originalFilename' on page: {$this->page->path}");
}
// return original // return original
if(!$getVariation) return $images[$file]; if(!$getVariation) return $images[$file];
@@ -403,8 +405,12 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$original = $images[$file]; $original = $images[$file];
$variationPathname = $original->pagefiles->path() . $variationFilename; $variationPathname = $original->pagefiles->path() . $variationFilename;
$pageimage = null; $pageimage = null;
if(is_file($variationPathname)) $pageimage = $this->wire(new Pageimage($original->pagefiles, $variationPathname)); if(is_file($variationPathname)) {
if(!$pageimage) throw new WireException("Unrecognized variation file: $file"); $pageimage = $this->wire(new Pageimage($original->pagefiles, $variationPathname));
}
if(!$pageimage) {
throw new WireException("Unrecognized variation file: $file");
}
return $pageimage; return $pageimage;
} }
@@ -543,12 +549,13 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$sanitizer = $this->wire()->sanitizer; $sanitizer = $this->wire()->sanitizer;
$modules = $this->wire()->modules; $modules = $this->wire()->modules;
$input = $this->wire()->input;
$images = $this->getImages($this->page, $this->page->fields); $images = $this->getImages($this->page, $this->page->fields);
$out = ''; $out = '';
if(wireCount($images)) { if(wireCount($images)) {
$winwidth = (int) $this->wire()->input->get('winwidth'); $winwidth = (int) $input->get('winwidth');
$in = $modules->get('InputfieldImage'); /** @var InputfieldImage $in */ $in = $modules->get('InputfieldImage'); /** @var InputfieldImage $in */
$in->set('adminThumbs', true); $in->set('adminThumbs', true);
$lastFieldLabel = ''; $lastFieldLabel = '';
@@ -617,7 +624,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$btn->value = $this->_('Upload Image'); $btn->value = $this->_('Upload Image');
$btn->addClass('upload pw-modal-button pw-modal-button-visible'); $btn->addClass('upload pw-modal-button pw-modal-button-visible');
$btn->icon = 'upload'; $btn->icon = 'upload';
$changes = $this->wire('input')->get('changes'); $changes = $input->get('changes');
if($changes) { if($changes) {
foreach(explode(',', $changes) as $name) { foreach(explode(',', $changes) as $name) {
$name = $sanitizer->fieldName($name); $name = $sanitizer->fieldName($name);
@@ -682,6 +689,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
* *
*/ */
protected function makeEditURL($file, $parts = array()) { protected function makeEditURL($file, $parts = array()) {
$input = $this->wire()->input;
$file = basename($file); $file = basename($file);
$id = isset($parts['id']) ? (int) $parts['id'] : $this->page->id; $id = isset($parts['id']) ? (int) $parts['id'] : $this->page->id;
@@ -696,8 +704,8 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$parts['original'] = $file; $parts['original'] = $file;
} }
} }
if(!isset($parts['class']) && $this->wire('input')->get('class')) { if(!isset($parts['class']) && $input->get('class')) {
$class = $this->wire('input')->get('class'); $class = $input->get('class');
if($class) { if($class) {
$validClasses = array($this->alignLeftClass, $this->alignCenterClass, $this->alignRightClass); $validClasses = array($this->alignLeftClass, $this->alignCenterClass, $this->alignRightClass);
$classes = array(); $classes = array();
@@ -711,7 +719,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
if($this->field) $parts['field'] = $this->fieldName; if($this->field) $parts['field'] = $this->fieldName;
if(!isset($parts['winwidth'])) { if(!isset($parts['winwidth'])) {
$winwidth = (int) $this->wire('input')->get('winwidth'); $winwidth = (int) $input->get('winwidth');
if($winwidth) $parts['winwidth'] = $winwidth; if($winwidth) $parts['winwidth'] = $winwidth;
} }
@@ -723,7 +731,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
unset($parts['id'], $parts['file']); // in case they are set here unset($parts['id'], $parts['file']); // in case they are set here
$url = $this->wire('config')->urls->admin . "page/image/edit/?id=$id&file=$id,$file"; $url = $this->wire()->config->urls->admin . "page/image/edit/?id=$id&file=$id,$file";
foreach($parts as $key => $value) $url .= "&$key=$value"; foreach($parts as $key => $value) $url .= "&$key=$value";
@@ -739,7 +747,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
* *
*/ */
public function checkImageEditPermission($throw = true) { public function checkImageEditPermission($throw = true) {
if(!$this->rte && !$this->wire('user')->hasPermission('page-edit-images', $this->masterPage)) { if(!$this->rte && !$this->wire()->user->hasPermission('page-edit-images', $this->masterPage)) {
if($throw) { if($throw) {
throw new WirePermissionException($this->labels['noAccess']); throw new WirePermissionException($this->labels['noAccess']);
} else { } else {
@@ -782,18 +790,21 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
*/ */
public function ___executeEdit() { public function ___executeEdit() {
$input = $this->wire()->input;
/** @var WireInput $input */ $config = $this->wire()->config;
$input = $this->wire('input'); $session = $this->wire()->session;
/** @var Config $config */ $sanitizer = $this->wire()->sanitizer;
$config = $this->wire('config'); $adminTheme = $this->wire()->adminTheme;
$checkboxClass = $adminTheme instanceof AdminThemeFramework ? $adminTheme->getClass('input-checkbox') : '';
$radioClass = $adminTheme instanceof AdminThemeFramework ? $adminTheme->getClass('input-radio') : '';
$this->checkImageEditPermission(); $this->checkImageEditPermission();
if($input->post('submit_crop')) { if($input->post('submit_crop')) {
$crop = $this->processCrop(); $crop = $this->processCrop();
$parts = $this->hidpi ? array('width' => $crop->hidpiWidth()) : array(); $parts = $this->hidpi ? array('width' => $crop->hidpiWidth()) : array();
$this->wire('session')->redirect($this->makeEditURL($crop->basename, $parts)); $session->location($this->makeEditURL($crop->basename, $parts));
return ''; return '';
} else if($input->post('submit_save_replace')) { } else if($input->post('submit_save_replace')) {
return $this->processSave(true); return $this->processSave(true);
@@ -844,7 +855,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
if($this->rte) { if($this->rte) {
// when RTE mode, go to image selection again when invalid image // when RTE mode, go to image selection again when invalid image
$this->error($e->getMessage()); $this->error($e->getMessage());
$this->wire('session')->redirect("./?id={$this->page->id}"); $session->location("./?id={$this->page->id}");
return ''; return '';
} else { } else {
throw $e; throw $e;
@@ -963,7 +974,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
// prepare description (alt) // prepare description (alt)
$description = isset($_GET['description']) ? $input->get('description') : ''; // $image->description; $description = isset($_GET['description']) ? $input->get('description') : ''; // $image->description;
if(strlen($description) > 8192) $description = substr($description, 0, 8192); if(strlen($description) > 8192) $description = substr($description, 0, 8192);
$description = $this->wire('sanitizer')->entities($description); $description = $sanitizer->entities($description);
// if dealing with a variation size or crop provide the option to link to the original (larger) // if dealing with a variation size or crop provide the option to link to the original (larger)
$linkOriginalChecked = ''; $linkOriginalChecked = '';
@@ -1029,28 +1040,28 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
"<span id='selected_image_checkboxes' class='hide_when_crop'>" . "<span id='selected_image_checkboxes' class='hide_when_crop'>" .
"<span id='wrap_link_original' class='show_when_rte'>" . "<span id='wrap_link_original' class='show_when_rte'>" .
"<label class='checkbox'>" . "<label class='checkbox'>" .
"<input title='$labels[linkOriginal]' type='checkbox' $linkOriginalChecked " . "<input title='$labels[linkOriginal]' type='checkbox' class='$checkboxClass' $linkOriginalChecked " .
"name='selected_image_link' id='selected_image_link' value='{$original->url}' /> " . "name='selected_image_link' id='selected_image_link' value='$original->url' /> " .
"<i class='fa fa-link ui-priority-secondary'></i>&nbsp;" . "<i class='fa fa-link ui-priority-secondary'></i>&nbsp;" .
"{$original->width}x{$original->height}" . "{$original->width}x{$original->height}" .
"</label>" . "</label>" .
"</span>" . "</span>" .
"<span id='wrap_caption' class='show_when_rte'>" . "<span id='wrap_caption' class='show_when_rte'>" .
"<label class='checkbox'>" . "<label class='checkbox'>" .
"<input$captionChecked id='selected_image_caption' type='checkbox' value='1' title='$labels[captionTip]' />&nbsp;$labels[caption]" . "<input$captionChecked id='selected_image_caption' type='checkbox' class='$checkboxClass' value='1' title='$labels[captionTip]' />&nbsp;$labels[caption]" .
"</label>" . "</label>" .
"</span>" . "</span>" .
"<span id='wrap_hidpi' class='show_when_rte'>" . "<span id='wrap_hidpi' class='show_when_rte'>" .
"<label class='checkbox'>" . "<label class='checkbox'>" .
"<input$hidpiChecked id='selected_image_hidpi' type='checkbox' value='1' />&nbsp;$labels[hidpi]" . "<input$hidpiChecked id='selected_image_hidpi' type='checkbox' class='$checkboxClass' value='1' />&nbsp;$labels[hidpi]" .
"</label>" . "</label>" .
"</span>" . "</span>" .
"</span>" . // selected_image_checkboxes "</span>" . // selected_image_checkboxes
"<span id='selected_image_resize' class='hide_when_rte hide_when_crop hide_when_processing'>" . "<span id='selected_image_resize' class='hide_when_rte hide_when_crop hide_when_processing'>" .
"<i class='fa fa-angle-left ui-priority-secondary'></i>&nbsp;" . "<i class='fa fa-angle-left ui-priority-secondary'></i>&nbsp;" .
"$labels[useResize]&nbsp;&nbsp;" . "$labels[useResize]&nbsp;&nbsp;" .
"<label class='checkbox'><input$resizeYesChecked id='selected_image_resize_yes' name='use_resize' type='radio' value='1' />&nbsp;$labels[yes]</label>&nbsp;&nbsp;" . "<label class='checkbox'><input$resizeYesChecked id='selected_image_resize_yes' name='use_resize' type='radio' class='$radioClass' value='1' />&nbsp;$labels[yes]</label>&nbsp;&nbsp;" .
"<label class='checkbox'><input$resizeNoChecked id='selected_image_resize_no' name='use_resize' type='radio' value='0' />&nbsp;" . "<label class='checkbox'><input$resizeNoChecked id='selected_image_resize_no' name='use_resize' type='radio' class='$radioClass' value='0' />&nbsp;" .
sprintf($labels['noUse'], $originalDimension) . "</label>" . sprintf($labels['noUse'], $originalDimension) . "</label>" .
"</span>" . "</span>" .
"<button type='button' class='ui-button ui-state-active show_when_processing' id='button_saving'>" . "<button type='button' class='ui-button ui-state-active show_when_processing' id='button_saving'>" .
@@ -1162,12 +1173,12 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
*/ */
protected function processCrop() { protected function processCrop() {
$post = $this->wire('input')->post; $input = $this->wire()->input;
$cropX = (int) $post->crop_x; $cropX = (int) $input->post('crop_x');
$cropY = (int) $post->crop_y; $cropY = (int) $input->post('crop_y');
$cropW = (int) $post->crop_w; $cropW = (int) $input->post('crop_w');
$cropH = (int) $post->crop_h; $cropH = (int) $input->post('crop_h');
$image = $this->getPageimage(); $image = $this->getPageimage();
if(!$image) throw new WireException("Unable to load image"); if(!$image) throw new WireException("Unable to load image");
@@ -1253,7 +1264,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
// replace original image // replace original image
if($original->replaceFile($image2->filename())) { if($original->replaceFile($image2->filename())) {
$original->modified = time(); $original->modified = time();
$original->modifiedUser = $this->wire('user'); $original->modifiedUser = $this->wire()->user;
/** @var FieldtypeFile $fieldtype */ /** @var FieldtypeFile $fieldtype */
if($fieldtype instanceof FieldtypeFile) { if($fieldtype instanceof FieldtypeFile) {
$fieldtype->saveFileCols($this->page, $this->field, $original, array( $fieldtype->saveFileCols($this->page, $this->field, $original, array(
@@ -1360,8 +1371,9 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$this->checkImageEditPermission(); $this->checkImageEditPermission();
/** @var WireInput $input */ $input = $this->wire()->input;
$input = $this->wire('input'); $adminTheme = $this->wire()->adminTheme;
$checkboxClass = $adminTheme instanceof AdminThemeFramework ? $adminTheme->getClass('input-checkbox') : '';
$width = (int) $input->get('width'); $width = (int) $input->get('width');
$class = $this->sanitizer->name($input->get('class')); $class = $this->sanitizer->name($input->get('class'));
@@ -1382,14 +1394,18 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
$image = $this->getPageimage(true); $image = $this->getPageimage(true);
if(!$crop && strpos($image->basename, '-cropx') !== false) $image = $this->getLastCrop($image); if(!$crop && strpos($image->basename, '-cropx') !== false) {
$image = $this->getLastCrop($image);
}
if( (!$hidpi && $width < $image->width) || if( (!$hidpi && $width < $image->width) ||
($hidpi && $width < $image->hidpiWidth()) || ($hidpi && $width < $image->hidpiWidth()) ||
$flip || $rotate) { $flip || $rotate) {
$suffix = array('is'); // is=image select $suffix = array('is'); // is=image select
if($this->editorPage && $this->editorPage->id != $this->page->id) $suffix[] = "pid$this->editorPage"; // identify page that is using the variation if($this->editorPage && $this->editorPage->id != $this->page->id) {
$suffix[] = "pid$this->editorPage"; // identify page that is using the variation
}
$options = array( $options = array(
'suffix' => $suffix, 'suffix' => $suffix,
'hidpi' => $hidpi, 'hidpi' => $hidpi,
@@ -1440,7 +1456,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
"<p>" . "<p>" .
"<span id='selected_image_width'>$width</span>x" . "<span id='selected_image_width'>$width</span>x" .
"<span id='selected_image_height'>$height</span> " . "<span id='selected_image_height'>$height</span> " .
"<label><input type='checkbox' id='selected_image_hidpi' $hidpiChecked />hidpi</label><br />" . "<label><input type='checkbox' class='$checkboxClass' id='selected_image_hidpi' $hidpiChecked />hidpi</label><br />" .
"<img alt='' " . "<img alt='' " .
"id='selected_image' " . "id='selected_image' " .
"class='$class' " . "class='$class' " .
@@ -1520,7 +1536,7 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
foreach($deleteErrors as $url) { foreach($deleteErrors as $url) {
$this->error($this->_('Error deleting image variation') . " - $url"); $this->error($this->_('Error deleting image variation') . " - $url");
} }
$this->wire()->session->redirect("./?id={$this->page->id}&file=$pageimage->basename"); $this->wire()->session->location("./?id={$this->page->id}&file=$pageimage->basename");
} }
$rows[] = array( $rows[] = array(
@@ -1800,4 +1816,3 @@ class ProcessPageEditImageSelect extends Process implements ConfigurableModule {
} }
} }