1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 12:20:44 +02:00

Image to Bbcode routines and batch conversion option added to admin News area (when in debug mode)

This commit is contained in:
Cameron
2017-12-27 20:58:59 -08:00
parent 66cb38cb85
commit 2788e9745c
5 changed files with 228 additions and 66 deletions

View File

@@ -110,6 +110,8 @@ class news_admin extends e_admin_dispatcher
}
}
@@ -810,6 +812,42 @@ class news_admin_ui extends e_admin_ui
}
function handleListImageBbcodeBatch($selected, $field, $value)
{
$sql = e107::getDb();
$status = array();
$ids = implode(",", e107::getParser()->filter($selected,'int'));
if($data = $sql->retrieve("news","news_id,news_body","news_id IN (".$ids.") ",true))
{
foreach($data as $row)
{
$id = $row['news_id'];
$update = array(
'news_body' => e107::getBB()->imgToBBcode($row['news_body'], true),
'WHERE' => 'news_id = '.$row['news_id']
);
$status[$id] = $sql->update('news',$update) ? E_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
}
}
$mes = e107::getMessage();
foreach($status as $k=>$v)
{
$mes->add(LAN_UPDATED.": ".$k, $v);
}
return true;
}
function init()
{
@@ -846,6 +884,13 @@ class news_admin_ui extends e_admin_ui
}
if(deftrue('e_DEBUG'))
{
$this->batchOptions['Modify News body'] = array('image_bbcode'=>"Convert all images in news-body to [img] bbcodes.");
}
if(deftrue("ADMINUI_NEWS_VISIBILITY_MULTIPLE")) // bc workaround for those who need it. Add to e107_config.php .
{
$this->fields['news_class']['type'] = 'userclasses';

View File

@@ -3458,6 +3458,8 @@ class e_admin_controller_ui extends e_admin_controller
//something like handleListUrlTypeBatch(); for custom handling of 'url_type' field name
$method = 'handle'.$actionName.$this->getRequest()->camelize($field).'Batch';
e107::getDebug()->log("Checking for batch method: ".$method);
if(method_exists($this, $method)) // callback handling
{
$this->$method($selected, $value);
@@ -3467,6 +3469,7 @@ class e_admin_controller_ui extends e_admin_controller
//handleListBatch(); for custom handling of all field names
if(empty($selected)) return $this;
$method = 'handle'.$actionName.'Batch';
e107::getDebug()->log("Checking for batch method: ".$method);
if(method_exists($this, $method))
{
$this->$method($selected, $field, $value);
@@ -4787,6 +4790,19 @@ class e_admin_ui extends e_admin_controller_ui
parent::manageColumns();
}
/**
* Detect if a batch function has been fired.
* @param $batchKey
* @return bool
*/
public function batchTriggered($batchKey)
{
return (!empty($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == $batchKey));
}
/**
* Catch batch submit
* @param string $batch_trigger

View File

@@ -656,6 +656,102 @@ class e_bbcode
}
/**
* Replace all instances of <img> tags with [img] bbcodes - allowing image tags and their 'src' values to remain dynamic.
* @param string $html
* @param bool $fromDB if html source is directly from the database, set to true to handle '&quot;' etc.
* @return string html with <img> tags replaced by [img] bbcodes.
*/
function imgToBBcode($html, $fromDB = false)
{
$tp = e107::getParser();
if($fromDB === true)
{
$html = str_replace('&quot;','"', $html);
}
$arr = $tp->getTags($html,'img');
$srch = array("?","&");
$repl = array("\?","&amp;");
if(defined('TINYMCE_DEBUG'))
{
print_a($arr);
}
foreach($arr['img'] as $img)
{
if(substr($img['src'],0,4) == 'http' || strpos($img['src'], e_IMAGE_ABS.'emotes/')!==false) // dont resize external images or emoticons.
{
continue;
}
$regexp = '#(<img[^>]*src="'.str_replace($srch, $repl, $img['src']).'"[^>]*>)#';
$qr = $tp->thumbUrlDecode($img['src']); // extract width/height and src from thumb URLs.
if(substr($qr['src'],0,4)!=='http' && empty($qr['w']) && empty($qr['aw']))
{
$qr['w'] = $img['width'];
$qr['h'] = $img['height'];
}
$qr['ebase'] = true;
unset($img['src'],$img['srcset'],$img['@value'], $img['caption'], $img['alt']);
if(!empty($img['class']))
{
$tmp = explode(" ",$img['class']);
$cls = array();
foreach($tmp as $v)
{
if($v === 'img-rounded' || $v === 'rounded' || $v === 'bbcode' || $v === 'bbcode-img-news' || $v === 'bbcode-img')
{
continue;
}
$cls[] = $v;
}
if(empty($cls))
{
unset($img['class']);
}
else
{
$img['class'] = implode(" ",$cls);
}
}
$parms = !empty($img) ? ' '.str_replace('+', ' ', http_build_query($img,null, '&')) : "";
$code_text = str_replace($tp->getUrlConstants('raw'), $tp->getUrlConstants('sc'), $qr['src']);
$replacement = '[img'.$parms.']'.$code_text.'[/img]';
$html = preg_replace($regexp, $replacement, $html);
}
if($fromDB === true)
{
$html = str_replace('"', '&quot;', $html);
}
return $html;
}
/**
* Convert HTML to bbcode.

View File

@@ -2836,6 +2836,56 @@ class e_parse extends e_parser
/**
* Split a thumb.php url into an array which can be parsed back into the thumbUrl method. .
* @param $src
* @return array
*/
function thumbUrlDecode($src)
{
list($url,$qry) = explode("?",$src);
$ret = array();
if(strstr($url,"thumb.php") && !empty($qry)) // Regular
{
parse_str($qry,$val);
$ret = $val;
}
elseif(preg_match('/media\/img\/(a)?([\d]*)x(a)?([\d]*)\/(.*)/',$url,$match)) // SEF
{
$wKey = $match[1].'w';
$hKey = $match[3].'h';
$ret = array(
'src'=> 'e_MEDIA_IMAGE/'.$match[5],
$wKey => $match[2],
$hKey => $match[4]
);
}
elseif(preg_match('/theme\/img\/(a)?([\d]*)x(a)?([\d]*)\/(.*)/', $url, $match)) // Theme-image SEF Urls
{
$wKey = $match[1].'w';
$hKey = $match[3].'h';
$ret = array(
'src'=> 'e_THEME/'.$match[5],
$wKey => $match[2],
$hKey => $match[4]
);
}
elseif(defined('TINYMCE_DEBUG'))
{
print_a("thumbUrlDecode: No Matches");
}
return $ret;
}
/**
* Experimental: Generate a Thumb URL for use in the img srcset attribute.

View File

@@ -194,7 +194,8 @@ TEMPL;
{
$content = trim($content);
$content = $this->updateImg($content);
// $content = $this->updateImg($content);
$content = e107::getBB()->imgToBBcode($content);
// $content = $tp->parseBBTags($content,true); // replace html with bbcode equivalent
if(strip_tags($content, '<i>') == '&nbsp;') // Avoid this: [html]<p>&nbsp;</p>[/html]
@@ -219,65 +220,18 @@ TEMPL;
}
/**
* Split a thumb.php url into an array which can be parsed back into the thumbUrl method. .
* @param $src
* @return array
*/
function thumbUrlDecode($src)
{
list($url,$qry) = explode("?",$src);
$ret = array();
if(strstr($url,"thumb.php") && !empty($qry)) // Regular
{
parse_str($qry,$val);
$ret = $val;
}
elseif(preg_match('/media\/img\/(a)?([\d]*)x(a)?([\d]*)\/(.*)/',$url,$match)) // SEF
{
$wKey = $match[1].'w';
$hKey = $match[3].'h';
$ret = array(
'src'=> 'e_MEDIA_IMAGE/'.$match[5],
$wKey => $match[2],
$hKey => $match[4]
);
}
elseif(preg_match('/theme\/img\/(a)?([\d]*)x(a)?([\d]*)\/(.*)/', $url, $match)) // Theme-image SEF Urls
{
$wKey = $match[1].'w';
$hKey = $match[3].'h';
$ret = array(
'src'=> 'e_THEME/'.$match[5],
$wKey => $match[2],
$hKey => $match[4]
);
}
elseif(defined('TINYMCE_DEBUG'))
{
print_a("thumbUrlDecode: No Matches");
}
return $ret;
}
/**
* Rebuld <img> tags with modified thumbnail size.
* @deprecated @see e107::getBB()->imgToBBcode();
* @param $text
* @return mixed
*/
function updateImg($text)
/* function updateImg($text)
{
$arr = e107::getParser()->getTags($text,'img');
$tp = e107::getParser();
$arr = $tp->getTags($text,'img');
$srch = array("?","&");
$repl = array("\?","&amp;");
@@ -296,18 +250,19 @@ TEMPL;
$regexp = '#(<img[^>]*src="'.str_replace($srch, $repl, $img['src']).'"[^>]*>)#';
/*
$width = vartrue($img['width']) ? ' width="'.$img['width'].'"' : '';
$height = vartrue($img['height']) ? ' height="'.$img['height'].'"' : '';
$style = vartrue($img['style']) ? ' style="'.$img['style'].'"' : '';
$class = vartrue($img['class']) ? ' class="'.$img['class'].'"' : '';
$alt = vartrue($img['alt']) ? ' alt="'.$img['alt'].'"' : '';
$title = vartrue($img['title']) ? ' title="'.$img['title'].'"' : '';
$srcset = vartrue($img['srcset']) ? 'srcset="'.$img['srcset'].'"' : '';
*/
// $width = vartrue($img['width']) ? ' width="'.$img['width'].'"' : '';
// $height = vartrue($img['height']) ? ' height="'.$img['height'].'"' : '';
// $style = vartrue($img['style']) ? ' style="'.$img['style'].'"' : '';
// $class = vartrue($img['class']) ? ' class="'.$img['class'].'"' : '';
// $alt = vartrue($img['alt']) ? ' alt="'.$img['alt'].'"' : '';
// $title = vartrue($img['title']) ? ' title="'.$img['title'].'"' : '';
// $srcset = vartrue($img['srcset']) ? 'srcset="'.$img['srcset'].'"' : '';
$qr = $this->thumbUrlDecode($img['src']);
$qr = $tp->thumbUrlDecode($img['src']);
if(substr($qr['src'],0,4)!=='http' && empty($qr['w']) && empty($qr['aw']))
{
@@ -351,7 +306,7 @@ TEMPL;
$parms = !empty($img) ? ' '.str_replace('+', ' ', http_build_query($img,null, '&')) : "";
$code_text = str_replace(e107::getParser()->getUrlConstants('raw'), e107::getParser()->getUrlConstants('sc'), $qr['src']);
$code_text = str_replace($tp->getUrlConstants('raw'), $tp->getUrlConstants('sc'), $qr['src']);
$replacement = '[img'.$parms.']'.$code_text.'[/img]';
@@ -360,7 +315,7 @@ TEMPL;
}
return $text;
}
}*/
}