mirror of
https://github.com/e107inc/e107.git
synced 2025-07-25 08:51:50 +02:00
Closes #4720 - Enhancement - linkwords stats in the news admin area.
This commit is contained in:
@@ -21,7 +21,7 @@ if (!getperms('P') || !e107::isInstalled('linkwords'))
|
||||
e107::lan('linkwords', true);
|
||||
|
||||
|
||||
class linkwords_admin extends e_admin_dispatcher
|
||||
class linkwords_admin_config extends e_admin_dispatcher
|
||||
{
|
||||
|
||||
protected $modes = array(
|
||||
@@ -184,8 +184,10 @@ class linkwords_ui extends e_admin_ui
|
||||
|
||||
if(!empty($_POST['runLinkwordTest']))
|
||||
{
|
||||
// $text .= "<strong>Result:</strong><br />";
|
||||
$result = e107::getParser()->toHTML($_POST['test_body'], false, 'BODY');
|
||||
/** @var linkwords_admin $hookObj */
|
||||
$hookObj = e107::getAddon('linkwords', 'e_parse');
|
||||
e107::callMethod($hookObj, 'init');
|
||||
$result = e107::callMethod($hookObj, 'toHTML',$_POST['test_body'], 'BODY');
|
||||
|
||||
$text .= "<div class='well' style='padding:30px'>".$result."</div>";
|
||||
$text .= "<div class='well' style='padding:30px; margin-bottom:30px'>".htmlentities($result)."</div>";
|
||||
@@ -216,7 +218,7 @@ class linkwords_form_ui extends e_admin_form_ui
|
||||
|
||||
}
|
||||
|
||||
new linkwords_admin();
|
||||
new linkwords_admin_config();
|
||||
|
||||
require_once(e_ADMIN."auth.php");
|
||||
e107::getAdminUI()->runPage();
|
||||
|
212
e107_plugins/linkwords/e_admin.php
Normal file
212
e107_plugins/linkwords/e_admin.php
Normal file
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
|
||||
//v2.x Standard for extending admin areas.
|
||||
|
||||
|
||||
class linkwords_admin implements e_admin_addon_interface
|
||||
{
|
||||
|
||||
|
||||
public function load($event, $ids)
|
||||
{
|
||||
|
||||
switch($event)
|
||||
{
|
||||
case "news":
|
||||
$data = e107::getDb()->retrieve("news","*", "news_id IN(".$ids.")",true);
|
||||
foreach($data as $row)
|
||||
{
|
||||
$id = (int) $row['news_id'];
|
||||
$ret[$id]['stats'] = $row['news_body']."\n".$row['news_extended'];
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
// code to be executed if n is different from all labels;
|
||||
}
|
||||
|
||||
|
||||
return $ret;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Extend Admin-ui Parameters
|
||||
* @param $ui admin-ui object
|
||||
* @return array
|
||||
*/
|
||||
public function config(e_admin_ui $ui)
|
||||
{
|
||||
$action = $ui->getAction(); // current mode: create, edit, list
|
||||
$type = $ui->getEventName(); // 'wmessage', 'news' etc. (core or plugin)
|
||||
$id = $ui->getId();
|
||||
|
||||
$config = array();
|
||||
|
||||
switch($type)
|
||||
{
|
||||
case 'news': // hook into the news admin form.
|
||||
// $body = $ui->getListModel()->getData('news_body');
|
||||
// var_dump($body);
|
||||
|
||||
$config['fields']['stats'] = array ('title' => LAN_PLUGIN_LINKWORDS_NAME, 'type' => 'method', 'tab' =>1, 'noedit'=>true, 'writeParms' => array(), 'width' => 'auto', 'help' => '', 'readParms' => '', 'class' => 'left', 'thclass' => 'left', );
|
||||
|
||||
// $config['batchOptions'] = array('custom' => 'Custom Batch Command');
|
||||
break;
|
||||
}
|
||||
|
||||
//Note: 'urls' will be returned as $_POST['x__blank_url']. ie. x_{PLUGIN_FOLDER}_{YOURFIELDKEY}
|
||||
|
||||
return $config;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Process Posted Data.
|
||||
* @param object $ui admin-ui
|
||||
* @param int|array $id - Primary ID of the record being created/edited/deleted or array data of a batch process.
|
||||
*/
|
||||
public function process(e_admin_ui $ui, $id=null)
|
||||
{
|
||||
|
||||
$data = $ui->getPosted(); // ie $_POST field-data
|
||||
$type = $ui->getEventName(); // eg. 'news'
|
||||
$action = $ui->getAction(); // current mode: create, edit, list, batch
|
||||
$changed = $ui->getModel()->dataHasChanged(); // true when data has changed from what is in the DB.
|
||||
|
||||
if($action === 'delete')
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if($action === 'batch')
|
||||
{
|
||||
$id = (array) $id;
|
||||
$arrayOfRecordIds = $id['ids'];
|
||||
$command = $id['cmd'];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
if(!empty($id) )
|
||||
{
|
||||
|
||||
if(!empty($data['x__blank_url']))
|
||||
{
|
||||
|
||||
// eg. Save the data in 'blank' plugin table. .
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
class linkwords_admin_form extends e_form
|
||||
{
|
||||
/** @var linkwords_parse lw */
|
||||
private $lw;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
|
||||
$this->lw = e107::getAddon('linkwords','e_parse');
|
||||
$this->lw->init();
|
||||
|
||||
}
|
||||
/**
|
||||
* @param $curval
|
||||
* @param $mode
|
||||
* @param $att
|
||||
* @return null|string
|
||||
*/
|
||||
function x_linkwords_stats($curval, $mode, $att=null)
|
||||
{
|
||||
|
||||
unset($att);
|
||||
$vals = array();
|
||||
|
||||
if(empty($curval))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
switch($mode)
|
||||
{
|
||||
case "read":
|
||||
$clsInt = '';
|
||||
$clsExt = '';
|
||||
|
||||
$curval = str_replace(''', "'", $curval);
|
||||
$this->lw->toHTML($curval,'BODY');
|
||||
|
||||
$stats = $this->lw->getStats();
|
||||
|
||||
if(empty($stats['internal']))
|
||||
{
|
||||
$clsInt = " style='opacity:0.7'";
|
||||
}
|
||||
|
||||
|
||||
if(empty($stats['external']))
|
||||
{
|
||||
$clsExt = " style='opacity:0.7'";
|
||||
}
|
||||
|
||||
$text = "<div class='text-nowrap'><i class='fas fa-link e-tip' title=\"Internal\"></i> <span{$clsInt}>".$stats['internal']."</span> | ";
|
||||
$text .= "<i class='fas fa-external-link-alt e-tip' title=\"External\"></i> <span{$clsExt}>".$stats['external']."</span></div>";
|
||||
|
||||
return $text;
|
||||
break;
|
||||
/*
|
||||
case "write":
|
||||
$text = "<table class='table table-striped table-condensed table-bordered'>
|
||||
<tr><th class='text-right'>No.</th><th>URL</th><th>Title</th></tr>";
|
||||
|
||||
for($i = 1; $i <= 20; $i++)
|
||||
{
|
||||
$text .= "<tr>
|
||||
<td class='text-right'>" . $i . "</td>
|
||||
<td>" . $this->text('x_reference_url[url][' . $i . ']', varset($vals['url'][$i]), 255, array('class' => 'x-reference-url', 'id' => 'x-reference-url-url-' . $i, 'size' => 'block-level')) . "</td>
|
||||
<td>" . $this->text('x_reference_url[name][' . $i . ']', varset($vals['name'][$i]), 255, array('id' => 'x-reference-url-name-' . $i, 'size' => 'block-level')) . "</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
$text .= "</table>";
|
||||
|
||||
$text .= $this->hidden('meta-parse', SITEURLBASE . e_PLUGIN_ABS . "reference/meta.php", array('id' => 'meta-parse'));
|
||||
|
||||
return $text;
|
||||
break;*/
|
||||
|
||||
default:
|
||||
// code to be executed if n is different from all labels;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -20,6 +20,8 @@ class linkwords_parse
|
||||
protected $utfMode = ''; // Flag to enable utf-8 on regex
|
||||
protected $cache = true;
|
||||
protected $suppressSamePageLink = false;
|
||||
protected $hash;
|
||||
protected $admin = false;
|
||||
|
||||
protected $word_list = array(); // List of link words/phrases
|
||||
private $link_list = array(); // Corresponding list of links to apply
|
||||
@@ -34,11 +36,28 @@ class linkwords_parse
|
||||
|
||||
protected $customClass = '';
|
||||
protected $wordCount = array();
|
||||
protected $intLinks = 0;
|
||||
protected $extLinks = 0;
|
||||
protected $word_limit = array();
|
||||
|
||||
const LW_CACHE_TAG = 'linkwords';
|
||||
// protected $maxPerWord = 3;
|
||||
|
||||
|
||||
/* constructor */
|
||||
function __construct()
|
||||
{
|
||||
|
||||
// See whether they should be active on this page - if not, no point doing anything!
|
||||
if(e_ADMIN_AREA === true && empty($_POST['runLinkwordTest']))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$this->init();
|
||||
|
||||
}
|
||||
|
||||
public function enable()
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
@@ -49,6 +68,7 @@ class linkwords_parse
|
||||
$this->cache = (bool) $var;
|
||||
}
|
||||
|
||||
|
||||
public function setWordData($arr = array())
|
||||
{
|
||||
foreach($arr as $val)
|
||||
@@ -74,121 +94,6 @@ class linkwords_parse
|
||||
}
|
||||
|
||||
|
||||
/* constructor */
|
||||
function __construct()
|
||||
{
|
||||
|
||||
$tp = e107::getParser();
|
||||
$pref = e107::pref('linkwords');
|
||||
$frm = e107::getForm();
|
||||
|
||||
// $this->maxPerWord = vartrue($pref['lw_max_per_word'], 25);
|
||||
$this->customClass = vartrue($pref['lw_custom_class']);
|
||||
$this->area_opts = (array) varset($pref['lw_context_visibility']);
|
||||
$this->utfMode = (strtolower(CHARSET) === 'utf-8') ? 'u' : '';
|
||||
$this->lwAjaxEnabled = varset($pref['lw_ajax_enable'],0);
|
||||
$this->suppressSamePageLink = (bool) vartrue($pref['lw_notsamepage'], false);
|
||||
|
||||
// See whether they should be active on this page - if not, no point doing anything!
|
||||
if(e_ADMIN_AREA === true && empty($_POST['runLinkwordTest']))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Now see if disabled on specific pages
|
||||
$check_url = e_SELF.(defined('e_QUERY') ? "?".e_QUERY : '');
|
||||
$this->block_list = explode("|",substr(varset($pref['lw_page_visibility']),2)); // Knock off the 'show/hide' flag
|
||||
|
||||
foreach($this->block_list as $p)
|
||||
{
|
||||
if($p=trim($p))
|
||||
{
|
||||
if(substr($p, -1) === '!')
|
||||
{
|
||||
$p = substr($p, 0, -1);
|
||||
if(substr($check_url, strlen($p)*-1) == $p) return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strpos($check_url, $p) !== FALSE) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if($this->cache && ($temp = e107::getCache()->retrieve(self::LW_CACHE_TAG, false, true, true)))
|
||||
{
|
||||
if($data = e107::unserialize($temp))
|
||||
{
|
||||
foreach($data as $key=>$val)
|
||||
{
|
||||
$this->$key = $val;
|
||||
}
|
||||
|
||||
$this->lw_enabled = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger_error("Error reading linkwords cache: ".self::LW_CACHE_TAG);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(empty($temp)) // Either cache disabled, or no info in cache (or error reading/processing cache)
|
||||
{
|
||||
$link_sql = e107::getDb('link_sql');
|
||||
|
||||
if($link_sql->select("linkwords", "*", "linkword_active!=1"))
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
|
||||
while($row = $link_sql->fetch())
|
||||
{
|
||||
|
||||
$lw = $tp->ustrtolower($row['linkword_word']); // It was trimmed when saved *utf
|
||||
$lw = str_replace(''', "'", $lw); // Fix for apostrophies.
|
||||
|
||||
if($row['linkword_active'] == 2)
|
||||
{
|
||||
$row['linkword_link'] = ''; // Make sure linkword disabled
|
||||
}
|
||||
|
||||
if($row['linkword_active'] < 2)
|
||||
{
|
||||
$row['linkword_tooltip'] = ''; // Make sure tooltip disabled
|
||||
}
|
||||
|
||||
|
||||
if(strpos($lw,',')) // Several words to same link
|
||||
{
|
||||
$lwlist = explode(',',$lw);
|
||||
foreach ($lwlist as $lw)
|
||||
{
|
||||
$this->loadRow($lw,$row);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->loadRow($lw,$row);
|
||||
}
|
||||
}
|
||||
|
||||
if($this->cache) // Write to file for next time
|
||||
{
|
||||
$temp = [];
|
||||
foreach (array('word_list', 'word_class', 'word_limit', 'link_list', 'tip_list', 'ext_list', 'rel_list', 'LinkID') as $var)
|
||||
{
|
||||
$temp[$var] = $this->$var;
|
||||
}
|
||||
|
||||
e107::getCache()->set(self::LW_CACHE_TAG, e107::serialize($temp, 'json'), true, true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function loadRow($lw, $row)
|
||||
{
|
||||
@@ -390,6 +295,7 @@ class linkwords_parse
|
||||
$class = implode(' ',$lwClass);
|
||||
|
||||
$hash = md5($lw);
|
||||
$this->hash = $hash;
|
||||
|
||||
if(!isset($this->wordCount[$hash]))
|
||||
{
|
||||
@@ -402,6 +308,8 @@ class linkwords_parse
|
||||
{
|
||||
$this->wordCount[$hash]++;
|
||||
|
||||
|
||||
|
||||
$classCount = " lw-".$this->wordCount[$hash];
|
||||
|
||||
if(empty($linkwd))
|
||||
@@ -410,6 +318,15 @@ class linkwords_parse
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strpos($linkwd,'http')!==false)
|
||||
{
|
||||
$this->extLinks++;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->intLinks++;
|
||||
}
|
||||
|
||||
$ret .= "<a class=\"".$class.$classCount."\" ".$linkwd.$tooltip.">".$sl."</a>";
|
||||
}
|
||||
|
||||
@@ -427,7 +344,130 @@ class linkwords_parse
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function getStats()
|
||||
{
|
||||
return [
|
||||
'internal' => $this->intLinks,
|
||||
'external' => $this->extLinks,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function init(): void
|
||||
{
|
||||
|
||||
$tp = e107::getParser();
|
||||
$pref = e107::pref('linkwords');
|
||||
$frm = e107::getForm();
|
||||
|
||||
// $this->maxPerWord = vartrue($pref['lw_max_per_word'], 25);
|
||||
$this->customClass = vartrue($pref['lw_custom_class']);
|
||||
$this->area_opts = (array) varset($pref['lw_context_visibility']);
|
||||
$this->utfMode = (strtolower(CHARSET) === 'utf-8') ? 'u' : '';
|
||||
$this->lwAjaxEnabled = varset($pref['lw_ajax_enable'], 0);
|
||||
$this->suppressSamePageLink = (bool) vartrue($pref['lw_notsamepage'], false);
|
||||
|
||||
|
||||
// Now see if disabled on specific pages
|
||||
$check_url = e_SELF . (defined('e_QUERY') ? "?" . e_QUERY : '');
|
||||
$this->block_list = explode("|", substr(varset($pref['lw_page_visibility']), 2)); // Knock off the 'show/hide' flag
|
||||
|
||||
foreach($this->block_list as $p)
|
||||
{
|
||||
if($p = trim($p))
|
||||
{
|
||||
if(substr($p, -1) === '!')
|
||||
{
|
||||
$p = substr($p, 0, -1);
|
||||
if(substr($check_url, strlen($p) * -1) == $p)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strpos($check_url, $p) !== false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if($this->cache && ($temp = e107::getCache()->retrieve(self::LW_CACHE_TAG, false, true, true)))
|
||||
{
|
||||
if($data = e107::unserialize($temp))
|
||||
{
|
||||
foreach($data as $key => $val)
|
||||
{
|
||||
$this->$key = $val;
|
||||
}
|
||||
|
||||
$this->lw_enabled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger_error("Error reading linkwords cache: " . self::LW_CACHE_TAG);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(empty($temp)) // Either cache disabled, or no info in cache (or error reading/processing cache)
|
||||
{
|
||||
$link_sql = e107::getDb('link_sql');
|
||||
|
||||
if($link_sql->select("linkwords", "*", "linkword_active!=1"))
|
||||
{
|
||||
$this->lw_enabled = true;
|
||||
|
||||
while($row = $link_sql->fetch())
|
||||
{
|
||||
|
||||
$lw = $tp->ustrtolower($row['linkword_word']); // It was trimmed when saved *utf
|
||||
$lw = str_replace(''', "'", $lw); // Fix for apostrophies.
|
||||
|
||||
if($row['linkword_active'] == 2)
|
||||
{
|
||||
$row['linkword_link'] = ''; // Make sure linkword disabled
|
||||
}
|
||||
|
||||
if($row['linkword_active'] < 2)
|
||||
{
|
||||
$row['linkword_tooltip'] = ''; // Make sure tooltip disabled
|
||||
}
|
||||
|
||||
|
||||
if(strpos($lw, ',')) // Several words to same link
|
||||
{
|
||||
$lwlist = explode(',', $lw);
|
||||
foreach($lwlist as $lw)
|
||||
{
|
||||
$this->loadRow($lw, $row);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->loadRow($lw, $row);
|
||||
}
|
||||
}
|
||||
|
||||
if($this->cache) // Write to file for next time
|
||||
{
|
||||
$temp = [];
|
||||
foreach(array('word_list', 'word_class', 'word_limit', 'link_list', 'tip_list', 'ext_list', 'rel_list', 'LinkID') as $var)
|
||||
{
|
||||
$temp[$var] = $this->$var;
|
||||
}
|
||||
|
||||
e107::getCache()->set(self::LW_CACHE_TAG, e107::serialize($temp, 'json'), true, true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user