Skip to content

Commit

Permalink
feature update
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Sampl committed Feb 21, 2016
1 parent 8a4f70a commit 55a5325
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 77 deletions.
20 changes: 20 additions & 0 deletions assets/css/seo_preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,24 @@
font-size: .625rem;
color: #999;
width: 520px;
}

.seo_preview_link {
display: inline-block;
position: relative;
top: -2px;
}

.seo_preview_link .seo_preview_wrapper {
position: absolute;
right:0;
margin-top: 20px;
background: #fff;
min-width:650px;
z-index: 99;
padding:10px;
box-shadow:0 0 3px rgba(0,0,0,.3);
}
.seo_preview_link.na a {
cursor:not-allowed;
}
Binary file modified assets/icons/seo_error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icons/seo_na.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icons/seo_ok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/icons/seo_success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 16 additions & 16 deletions assets/js/seo_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ jQuery(document).ready(function($){
seoPageTitle.change();
})

$('[name^="title"]').change();
//$('[name^="title"]').change();

$(document).on('keyup change cut paste','[name^="pageTitle"]',function(){
updateVars($(this));
v = $(this).val() ? $(this).val() : seoTitle.val();
c = v.length + seoPreview.find('.root_page_title').text().length;
console.log(c);
max = seoCount.find('.title .max').text();
l = max-c;

Expand All @@ -32,7 +31,7 @@ jQuery(document).ready(function($){
seoCount.find('.title').removeClass('error ok warn').addClass(status);
})

$('[name^="pageTitle"]').change();
//$('[name^="pageTitle"]').change();

$(document).on('keyup change cut paste','[name^="description"]',function(){
updateVars($(this));
Expand Down Expand Up @@ -80,18 +79,19 @@ jQuery(document).ready(function($){
}
}

})
$('a[data-show-preview]').mouseenter(function(){
id = $(this).data('show-preview');
$target = $('[data-seo-preview="'+id+'"').show();

var SEOdecodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
$target.css({
position: 'absolute',
top: 0
})
})

var SEOencodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''));
}
return buf.join('');
};
$('a[data-show-preview]').mouseleave(function(){
id = $(this).data('show-preview');
$target = $('[data-seo-preview="'+id+'"').hide();
})

})
138 changes: 123 additions & 15 deletions classes/SeoPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static function getPageStatus($arrPage)
{

$status = 'na';

if ($arrPage['type'] != 'root' && $arrPage['type'] != 'redirect' && $arrPage['type'] != 'forward') {

$title = $arrPage['pageTitle'];
Expand All @@ -28,41 +28,149 @@ static function getPageStatus($arrPage)
}

}

return static::getStatusHtml($arrPage, $status);

return $status;

}

static function checkTitleLength($title)
{
$titleLength = $GLOBALS['seoPreview']['titleLength'];
if (strlen($title) >= $titleLength - 20 && strlen($title) <= $titleLength + 5) {
return true;
}
return false;
}

static function checkDescriptionLength($description)
{
$descriptionLength = $GLOBALS['seoPreview']['descriptionLength'];
if (strlen($description) >= $descriptionLength - 20 && strlen($description) <= $descriptionLength + 5) {
return true;
}
return false;
}

$titleLength = $GLOBALS['seoPreview']['titleLength'];

if (strlen($title) >= $titleLength - 20 && strlen($title) <= $titleLength + 5) {
return true;
static function getPreviewTitleStatus($objPage)
{

$status = '';
$title = $objPage->pageTitle;

if(!$title) {
$title = $objPage->title;
}

$rootId = $objPage->trail[0];
$rootPage = self::getRootPage($rootId);
$rootTitle = $rootPage->title;
$title .= ' - '.$rootTitle;

$maxLength = $GLOBALS['seoPreview']['titleLength'];
$titleLength = strlen($title);

$l = $maxLength - $titleLength;

if($l < 0) {
$status = 'error';
} elseif($l < 10) {
$status = 'ok';
} elseif($l < 20) {
$status = 'warn';
}

return array(
'status' => $status,
'length' => $titleLength
);

return false;
}

static function checkDescriptionLength($description)
static function getPreviewDescriptionStatus($objPage)
{
// $status = '';
$description = $objPage->description;

$descriptionLength = $GLOBALS['seoPreview']['descriptionLength'];
$maxLength = $GLOBALS['seoPreview']['descriptionLength'];
$descriptionLength = strlen($description);

if (strlen($description) >= $descriptionLength - 20 && strlen($description) <= $descriptionLength + 5) {
return true;
$l = $maxLength - $descriptionLength;

if($l < 0) {
$status = 'error';
} elseif($l < 10) {
$status = 'ok';
} elseif($l < 20) {
$status = 'warn';
}

return false;
return array(
'status' => $status,
'length' => $descriptionLength
);
}



static function getStatusHtml($arrPage, $status)
{
return '<img src="system/modules/seo_preview/assets/icons/seo_' . $status . '.png" />';
}

static function getStatusHtml($arrPage, $status) {
static function getRootPage($id) {

if($GLOBALS['seo_preview']['rootPage'][$id]) {
return $GLOBALS['seo_preview']['rootPage'][$id];
}

$objRootPage = \Controller::getPageDetails($id);
$GLOBALS['seo_preview']['rootPage'][$id] = $objRootPage;
return $objRootPage;
}

static function generatePreview($arrPage, $pageList = false) {

$GLOBALS['TL_JAVASCRIPT']['jquery'] = 'assets/jquery/core/' . $GLOBALS['TL_ASSETS']['JQUERY'] . '/jquery.min.js';
$GLOBALS['TL_JAVASCRIPT']['noconflict'] = 'system/modules/seo_preview/assets/js/noconflict.js';
$GLOBALS['TL_JAVASCRIPT']['seo_preview'] = 'system/modules/seo_preview/assets/js/seo_preview.js';

$GLOBALS['TL_CSS']['seo_preview'] = 'system/modules/seo_preview/assets/css/seo_preview.css';

$env = \Contao\Environment::getInstance();

if(is_array($arrPage)) {
$id = $arrPage['id'];
} else {
$id = $arrPage->id;
}

$objPage = \Controller::getPageDetails($id);
$rootPage = self::getRootPage($objPage->trail[0]);

$objTemplate = new FrontendTemplate('be_seopreview');

$objTemplate->titleLength = $GLOBALS['seoPreview']['titleLength'];
$objTemplate->descriptionLength = $GLOBALS['seoPreview']['descriptionLength'];
$objTemplate->pageList = $pageList;

$objTemplate->page = $objPage->row();
$objTemplate->title = $objPage->pageTitle ? $objPage->pageTitle : $objPage->title;
$objTemplate->rootTitle = $rootPage->pageTitle ? $rootPage->pageTitle : $rootPage->title;
$objTemplate->description = $objPage->description ? $objPage->description : $GLOBALS['TL_LANG']['tl_page']['noDescription'];
$objTemplate->url = $env->url.'/'.($GLOBALS['TL_CONFIG']['addLanguageToUrl'] ? $objPage->language.'/' : '').Controller::generateFrontendUrl($objPage->row());

$objTemplate->titleStatus = self::getPreviewTitleStatus($objPage);
$objTemplate->descriptionStatus = self::getPreviewDescriptionStatus($objPage);
$objTemplate->ignoreRootTitle = $rootPage->ignoreRootTitle;

return '<img src="system/modules/seo_preview/assets/icons/seo_'.$status.'.png" />';
$objTemplate->seo_preview_noDescription = $GLOBALS['TL_LANG']['tl_page']['seo_preview_noDescription'];
$objTemplate->seo_preview_headline = $GLOBALS['TL_LANG']['tl_page']['seo_preview_headline'];
$objTemplate->seo_preview_title = $GLOBALS['TL_LANG']['tl_page']['seo_preview_title'];
$objTemplate->seo_preview_description = $GLOBALS['TL_LANG']['tl_page']['seo_preview_description'];
$objTemplate->seo_preview_info = $GLOBALS['TL_LANG']['tl_page']['seo_preview_info'];

return $objTemplate->parse();
}

}
64 changes: 32 additions & 32 deletions dca/tl_page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

// config

$GLOBALS['TL_DCA']['tl_page']['config']['onload_callback'][] = array('tl_page_seo_preview', 'addField');

// operations

$GLOBALS['TL_DCA']['tl_page']['list']['operations']['seoPreviewStatus'] = array
(
'label' => &$GLOBALS['TL_LANG']['tl_page']['edit'],
Expand All @@ -8,22 +14,34 @@
'button_callback' => array('tl_page_seo_preview', 'getStatus')
);


$GLOBALS['TL_DCA']['tl_page']['config']['onload_callback'][] = array('tl_page_seo_preview', 'addField');
// fields

$GLOBALS['TL_DCA']['tl_page']['fields']['seoPreview'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_page']['seoPreview'],
'input_field_callback' => array('tl_page_seo_preview', 'generatePreview'),
'input_field_callback' => array('SeoPreview', 'generatePreview'),
'eval' => array('tl_class'=>'clr'),
);

$GLOBALS['TL_DCA']['tl_page']['fields']['ignoreRootTitle'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_page']['ignoreRootTitle'],
'exclude' => true,
'inputType' => 'checkbox',
'eval' => array('tl_class'=>'w50'),
'sql' => "char(1) NOT NULL default ''"
);


class tl_page_seo_preview extends tl_page {

public function addField() {

foreach ($GLOBALS['TL_DCA']['tl_page']['palettes'] as $k => &$palette) {
$skipTypes = array('__selector__','root','forward','redirect');

if($k == 'root') {
$palette = str_replace('pageTitle', 'pageTitle,ignoreRootTitle', $palette);
}

if(in_array($k, $skipTypes)) {
continue;
}
Expand All @@ -33,38 +51,20 @@ public function addField() {

public function getStatus($row, $href, $label, $title, $icon, $attributes) {

$content = SeoPreview::getPageStatus($row);
return '<a style="position:relative;top:-1px;" href="'.$this->addToUrl($href.'&amp;id='.$row['id']).'" title="'.specialchars($title).'"'.$attributes.'>'.$content.'</a> ';
}

public function generatePreview($dc) {
$status = SeoPreview::getPageStatus($row);
$content = SeoPreview::getStatusHtml($row, $status);

$GLOBALS['TL_JAVASCRIPT'][] = 'assets/jquery/core/' . $GLOBALS['TL_ASSETS']['JQUERY'] . '/jquery.min.js';
$GLOBALS['TL_JAVASCRIPT'][] = 'system/modules/seo_preview/assets/js/noconflict.js';
$GLOBALS['TL_JAVASCRIPT'][] = 'system/modules/seo_preview/assets/js/seo_preview.js';

$GLOBALS['TL_CSS'][] = 'system/modules/seo_preview/assets/css/seo_preview.css';
if($status != 'na') {
$preview = SeoPreview::generatePreview($row, true);
}

$objPage = $this->getPageDetails($dc->id);
$rootPage = $this->getPageDetails($objPage->trail[0]);
$html = '<div class="seo_preview_link '.$status.'">';
$html .= '<a data-show-preview="'.$row['id'].'" href="'.$this->addToUrl($href.'&amp;id='.$row['id']).'" title="'.specialchars($title).'"'.$attributes.'>'.$content.'</a> ';
$html .= $preview;
$html .= '</div>';

$objTemplate = new FrontendTemplate('be_seopreview');

$objTemplate->titleLength = $GLOBALS['seoPreview']['titleLength'];
$objTemplate->descriptionLength = $GLOBALS['seoPreview']['descriptionLength'];

$objTemplate->page = $objPage->row();
$objTemplate->title = $objPage->pageTitle ? $objPage->pageTitle : $objPage->title;
$objTemplate->rootTitle = $rootPage->pageTitle ? $rootPage->pageTitle : $rootPage->title;
$objTemplate->description = $objPage->description ? $objPage->description : $GLOBALS['TL_LANG']['tl_page']['noDescription'];
$objTemplate->url = $this->Environment->url.'/'.($GLOBALS['TL_CONFIG']['addLanguageToUrl'] ? $objPage->language.'/' : '').$this->generateFrontendUrl($objPage->row());
return $html;
}

$objTemplate->seo_preview_noDescription = $GLOBALS['TL_LANG']['tl_page']['seo_preview_noDescription'];
$objTemplate->seo_preview_headline = $GLOBALS['TL_LANG']['tl_page']['seo_preview_headline'];
$objTemplate->seo_preview_title = $GLOBALS['TL_LANG']['tl_page']['seo_preview_title'];
$objTemplate->seo_preview_description = $GLOBALS['TL_LANG']['tl_page']['seo_preview_description'];
$objTemplate->seo_preview_info = $GLOBALS['TL_LANG']['tl_page']['seo_preview_info'];

return $objTemplate->parse();
}
}
1 change: 1 addition & 0 deletions languages/de/tl_page.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

$GLOBALS['TL_LANG']['tl_page']['seoPreview'] = array('Suchmaschinen-Vorschau','');
$GLOBALS['TL_LANG']['tl_page']['ignoreRootTitle'] = array('Root-Titel ignorieren', 'Ignoriert den Root-Titel in der SEO-Vorschau');
$GLOBALS['TL_LANG']['tl_page']['seo_preview_noDescription'] = '** Keine Seitenbeschreibung vorhanden **';
$GLOBALS['TL_LANG']['tl_page']['seo_preview_headline'] = 'Suchmaschinenvorschau';
$GLOBALS['TL_LANG']['tl_page']['seo_preview_title'] = 'Titel';
Expand Down
Loading

0 comments on commit 55a5325

Please sign in to comment.