1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 13:47:31 +02:00

Improved linkwords, allowing for greater control over styling of specific words.

This commit is contained in:
Cameron
2016-05-12 14:57:03 -07:00
parent 34ef4a1ae2
commit 44f46317ab
4 changed files with 66 additions and 28 deletions

View File

@@ -88,9 +88,12 @@
'linkword_link' => array ( 'title' => LWLAN_6, 'type' => 'text', 'data' => 'str', 'width' => 'auto', 'inline' => true, 'help' => '', 'readParms' => '', 'writeParms' => 'size=xxlarge', 'class' => 'left', 'thclass' => 'left', ), 'linkword_link' => array ( 'title' => LWLAN_6, 'type' => 'text', 'data' => 'str', 'width' => 'auto', 'inline' => true, 'help' => '', 'readParms' => '', 'writeParms' => 'size=xxlarge', 'class' => 'left', 'thclass' => 'left', ),
'linkword_active' => array ( 'title' => LAN_ACTIVE, 'type' => 'dropdown', 'data' => 'int', 'width' => 'auto', 'batch' => true, 'filter' => true, 'inline' => true, 'help' => '', 'readParms' => '', 'writeParms' => '', 'left' => 'center', 'thclass' => 'left', ), 'linkword_active' => array ( 'title' => LAN_ACTIVE, 'type' => 'dropdown', 'data' => 'int', 'width' => 'auto', 'batch' => true, 'filter' => true, 'inline' => true, 'help' => '', 'readParms' => '', 'writeParms' => '', 'left' => 'center', 'thclass' => 'left', ),
'linkword_tooltip' => array ( 'title' => LWLAN_50, 'type' => 'textarea', 'data' => 'str', 'width' => 'auto', 'inline' => true, 'help' => '', 'readParms' => '', 'writeParms' => '', 'class' => 'left', 'thclass' => 'left', ), 'linkword_tooltip' => array ( 'title' => LWLAN_50, 'type' => 'textarea', 'data' => 'str', 'width' => 'auto', 'inline' => true, 'help' => '', 'readParms' => '', 'writeParms' => array('size'=>'xxlarge'), 'class' => 'left', 'thclass' => 'left', ),
'linkword_tip_id' => array ( 'title' => LAN_ID, 'type' => 'number', 'data' => 'int', 'width' => '5%', 'help' => LWLAN_63, 'readParms' => '', 'writeParms' => '', 'class' => 'left', 'thclass' => 'left', ), 'linkword_limit' => array ( 'title' => "Max. links/tips", 'type' => 'number', 'data' => 'int', 'width' => '10%', 'help' => LWLAN_63, 'readParms' => '', 'writeParms' => array('default'=>3), 'class' => 'right', 'thclass' => 'right', ),
'linkword_tip_id' => array ( 'title' => LAN_ID, 'type' => 'number', 'data' => 'int', 'width' => '5%', 'help' => LWLAN_63, 'readParms' => '', 'writeParms' => '', 'class' => 'right', 'thclass' => 'right', ),
'linkword_newwindow' => array ( 'title' => LWLAN_55, 'type' => 'boolean', 'data' => 'int', 'width' => 'auto', 'inline' => true, 'help' => '', 'filter'=>true, 'readParms' => '', 'writeParms' => '', 'class' => 'center', 'thclass' => 'center', ), 'linkword_newwindow' => array ( 'title' => LWLAN_55, 'type' => 'boolean', 'data' => 'int', 'width' => 'auto', 'inline' => true, 'help' => '', 'filter'=>true, 'readParms' => '', 'writeParms' => '', 'class' => 'center', 'thclass' => 'center', ),
'options' => array ( 'title' => LAN_OPTIONS, 'type' => null, 'data' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center last', 'forced' => '1', ), 'options' => array ( 'title' => LAN_OPTIONS, 'type' => null, 'data' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center last', 'forced' => '1', ),
); );
@@ -102,7 +105,7 @@
'lw_ajax_enable' => array('title'=> LWLAN_59, 'type'=>'boolean', 'data' => 'string','help'=>''), 'lw_ajax_enable' => array('title'=> LWLAN_59, 'type'=>'boolean', 'data' => 'string','help'=>''),
'lw_notsamepage' => array('title'=> LWLAN_64, 'type'=>'boolean', 'data' => 'string','help'=>LWLAN_65), 'lw_notsamepage' => array('title'=> LWLAN_64, 'type'=>'boolean', 'data' => 'string','help'=>LWLAN_65),
'linkword_omit_pages' => array('title'=> LWLAN_28, 'type'=>'textarea', 'data' => 'string','help'=>''), 'linkword_omit_pages' => array('title'=> LWLAN_28, 'type'=>'textarea', 'data' => 'string','help'=>''),
'lw_max_per_word' => array('title'=> "Maximum links/tips per word", 'type'=>'number', 'data' => 'string','help'=>'If the same word is found multiple times in a piece of text.'), // 'lw_max_per_word' => array('title'=> "Maximum links/tips per word", 'type'=>'number', 'data' => 'string','help'=>'If the same word is found multiple times in a piece of text.'),
'lw_custom_class' => array('title'=> "Custom CSS Class", 'type'=>'text', 'writeParms'=>array('placeholder'=> LAN_OPTIONAL ), 'data' => 'string','help'=>'Will add this class to all generated links.'), 'lw_custom_class' => array('title'=> "Custom CSS Class", 'type'=>'text', 'writeParms'=>array('placeholder'=> LAN_OPTIONAL ), 'data' => 'string','help'=>'Will add this class to all generated links.'),
); );

View File

@@ -35,10 +35,10 @@ define('LW_CACHE_ENABLE', FALSE);
class e_tohtml_linkwords class e_tohtml_linkwords
{ {
var $lw_enabled = FALSE; // Default to disabled to start protected $lw_enabled = FALSE; // Default to disabled to start
var $lwAjaxEnabled = FALSE; // Adds in Ajax-compatible links var $lwAjaxEnabled = FALSE; // Adds in Ajax-compatible links
var $utfMode = ''; // Flag to enable utf-8 on regex var $utfMode = ''; // Flag to enable utf-8 on regex
var $word_list = array(); // List of link words/phrases protected $word_list = array(); // List of link words/phrases
var $link_list = array(); // Corresponding list of links to apply var $link_list = array(); // Corresponding list of links to apply
var $ext_list = array(); // Flags to determine 'open in new window' for link var $ext_list = array(); // Flags to determine 'open in new window' for link
var $tip_list = array(); // Store for tooltips var $tip_list = array(); // Store for tooltips
@@ -46,9 +46,11 @@ class e_tohtml_linkwords
var $area_opts = array(); // Process flags for the various contexts var $area_opts = array(); // Process flags for the various contexts
var $block_list = array(); // Array of 'blocked' pages var $block_list = array(); // Array of 'blocked' pages
protected $word_class = array();
protected $customClass = ''; protected $customClass = '';
protected $wordCount = array(); protected $wordCount = array();
protected $maxPerWord = 3; // protected $maxPerWord = 3;
/* constructor */ /* constructor */
@@ -57,8 +59,9 @@ class e_tohtml_linkwords
$tp = e107::getParser(); $tp = e107::getParser();
$pref = e107::pref('core'); $pref = e107::pref('core');
$frm = e107::getForm();
$this->maxPerWord = vartrue($pref['lw_max_per_word'], 25); // $this->maxPerWord = vartrue($pref['lw_max_per_word'], 25);
$this->customClass = vartrue($pref['lw_custom_class'],''); $this->customClass = vartrue($pref['lw_custom_class'],'');
$this->area_opts = $pref['lw_context_visibility']; $this->area_opts = $pref['lw_context_visibility'];
$this->utfMode = (strtolower(CHARSET) == 'utf-8') ? 'u' : ''; // Flag to enable utf-8 on regex //@TODO utfMode probably obsolete $this->utfMode = (strtolower(CHARSET) == 'utf-8') ? 'u' : ''; // Flag to enable utf-8 on regex //@TODO utfMode probably obsolete
@@ -110,8 +113,9 @@ class e_tohtml_linkwords
if($link_sql->select("linkwords", "*", "linkword_active!=1")) if($link_sql->select("linkwords", "*", "linkword_active!=1"))
{ {
$this->lw_enabled = TRUE; $this->lw_enabled = true;
while($row = $link_sql->db_Fetch())
while($row = $link_sql->fetch())
{ {
$lw = $tp->uStrToLower($row['linkword_word']); // It was trimmed when saved *utf $lw = $tp->uStrToLower($row['linkword_word']); // It was trimmed when saved *utf
@@ -135,18 +139,25 @@ class e_tohtml_linkwords
foreach ($lwlist as $lw) foreach ($lwlist as $lw)
{ {
$this->word_list[] = trim($lw); $this->word_list[] = trim($lw);
$this->word_class[] = 'lw-'.$frm->name2id($lw);
$this->word_limit[] = vartrue($row['linkword_limit'],3);
$this->link_list[] = $row['linkword_link']; $this->link_list[] = $row['linkword_link'];
$this->tip_list[] = $row['linkword_tooltip']; $this->tip_list[] = $row['linkword_tooltip'];
$this->ext_list[] = $row['linkword_newwindow']; $this->ext_list[] = $row['linkword_newwindow'];
$this->LinkID[] = $lwID; $this->LinkID[] = $lwID;
} }
} }
else else
{ {
$this->word_list[] = $lw; $this->word_list[] = $lw;
$this->word_class[] = 'lw-'.$frm->name2id($lw);
$this->word_limit[] = vartrue($row['linkword_limit'],3);
$this->link_list[] = $row['linkword_link']; $this->link_list[] = $row['linkword_link'];
$this->tip_list[] = $row['linkword_tooltip']; $this->tip_list[] = $row['linkword_tooltip'];
$this->ext_list[] = $row['linkword_newwindow']; $this->ext_list[] = $row['linkword_newwindow'];
$this->LinkID[] = $lwID; $this->LinkID[] = $lwID;
} }
} }
@@ -233,8 +244,15 @@ class e_tohtml_linkwords
// Consider next line - stripos is PHP5, and mb_stripos is PHP >= 5.2 - so may well often require handling // Consider next line - stripos is PHP5, and mb_stripos is PHP >= 5.2 - so may well often require handling
// while (($first < $limit) && (stripos($text,$this->word_list[$first]) === FALSE)) { $first++; }; // *utf (stripos is PHP5 - compatibility handler implements) // while (($first < $limit) && (stripos($text,$this->word_list[$first]) === FALSE)) { $first++; }; // *utf (stripos is PHP5 - compatibility handler implements)
while (($first < $limit) && (strpos($tp->ustrtolower($text),$this->word_list[$first]) === FALSE)) { $first++; }; // *utf while (($first < $limit) && (strpos($tp->ustrtolower($text), $this->word_list[$first]) === false))
if ($first == $limit) return $text; // Return if no linkword found {
$first++;
} // *utf
if ($first == $limit)
{
return $text; // Return if no linkword found
}
// There's at least one occurrence of the linkword in the text // There's at least one occurrence of the linkword in the text
// Prepare all info once only // Prepare all info once only
@@ -263,6 +281,7 @@ class e_tohtml_linkwords
$lwClass[] = 'lw-tip '.$this->customClass; $lwClass[] = 'lw-tip '.$this->customClass;
} }
} }
if ($this->link_list[$first]) // Got link if ($this->link_list[$first]) // Got link
{ {
$newLink = $tp->replaceConstants($this->link_list[$first], 'full'); $newLink = $tp->replaceConstants($this->link_list[$first], 'full');
@@ -273,11 +292,16 @@ class e_tohtml_linkwords
$lwClass[] = 'lw-link '.$this->customClass; $lwClass[] = 'lw-link '.$this->customClass;
} }
} }
elseif(!empty($this->word_class[$first]))
{
$lwClass[] = $this->word_class[$first];
}
if (!count($lwClass)) if (!count($lwClass))
{ {
return $this->linksproc($sl,$first+1,$limit); // Nothing to do - move on to next word (shouldn't really get here) // return $this->linksproc($sl,$first+1,$limit); // Nothing to do - move on to next word (shouldn't really get here)
} }
if (count($linkrel)) if (count($linkrel))
{ {
@@ -285,8 +309,9 @@ class e_tohtml_linkwords
} }
// This splits the text into blocks, some of which will precisely contain a linkword // This splits the text into blocks, some of which will precisely contain a linkword
$split_line = preg_split('#\b('.$lw.')\b#i'.$this->utfMode, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); // *utf (selected) $split_line = preg_split('#\b('.$lw.')(?:\s|\b)#i'.$this->utfMode, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); // *utf (selected)
$class = "class='".implode(' ',$lwClass)."' "; // $class = "".implode(' ',$lwClass)."' ";
$class = implode(' ',$lwClass);
$hash = md5($lw); $hash = md5($lw);
@@ -295,14 +320,24 @@ class e_tohtml_linkwords
$this->wordCount[$hash] = 0; $this->wordCount[$hash] = 0;
} }
foreach ($split_line as $sl) foreach ($split_line as $count=>$sl)
{ {
if ($tp->uStrToLower($sl) == $lw && $this->wordCount[$hash] < $this->maxPerWord) // Do linkword replace // We know the linkword is already lower case // *utf if ($tp->uStrToLower($sl) == $lw && $this->wordCount[$hash] < $this->word_limit[$first]) // Do linkword replace // We know the linkword is already lower case // *utf
{ {
$this->wordCount[$hash]++; $this->wordCount[$hash]++;
$ret .= '<a '.$class.$linkwd.$tooltip.'>'.$sl.'</a>'; $classCount = " lw-".$this->wordCount[$hash];
if(empty($linkwd))
{
$ret .= '<span class="'.$class.$classCount.'" '.$tooltip.'>'.$sl.'</span>';
}
else
{
$ret .= '<a class="'.$class.$classCount.'" '.$linkwd.$tooltip.'>'.$sl.'</a>';
}
} }
elseif (trim($sl)) // Something worthwhile left - look for more linkwords in it elseif (trim($sl)) // Something worthwhile left - look for more linkwords in it
{ {
@@ -320,4 +355,3 @@ class e_tohtml_linkwords
?>

View File

@@ -1,5 +1,5 @@
/* Linkwords */
a.lw-tip { cursor: help; text-decoration-style:dotted } a.lw-tip { cursor: help; text-decoration-style:dotted }
span.lw-tip { cursor: help; border-bottom:1px dotted; }
a.lw-link { cursor: pointer; text-decoration-style:solid } a.lw-link { cursor: pointer; text-decoration-style:solid }

View File

@@ -6,5 +6,6 @@ linkword_link varchar(250) NOT NULL default '',
linkword_tooltip text NOT NULL, linkword_tooltip text NOT NULL,
linkword_tip_id int(10) UNSIGNED NOT NULL Default '0', linkword_tip_id int(10) UNSIGNED NOT NULL Default '0',
linkword_newwindow tinyint(1) unsigned NOT NULL default '1', linkword_newwindow tinyint(1) unsigned NOT NULL default '1',
linkword_limit int(3) UNSIGNED NOT NULL default '0',
PRIMARY KEY (linkword_id) PRIMARY KEY (linkword_id)
) ENGINE=MyISAM AUTO_INCREMENT=1; ) ENGINE=MyISAM AUTO_INCREMENT=1;