Skip to content

Commit

Permalink
Merge pull request #5 from NicolasBarbey/update
Browse files Browse the repository at this point in the history
Add i18n compatibility and no follow checkbox
  • Loading branch information
lopes-vincent authored Aug 5, 2019
2 parents 020e533 + dc55ca1 commit 690dbb3
Show file tree
Hide file tree
Showing 24 changed files with 3,518 additions and 522 deletions.
64 changes: 5 additions & 59 deletions BetterSeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@



use BetterSeo\Model\SeoNoindex;
use BetterSeo\Model\SeoNoindexQuery;
use BetterSeo\Model\BetterSeo as BetterSeoModel;
use BetterSeo\Model\BetterSeoQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Install\Database;
use Thelia\Model\Brand;
Expand All @@ -26,6 +26,8 @@
use Thelia\Model\ContentQuery;
use Thelia\Model\Folder;
use Thelia\Model\FolderQuery;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\Product;
use Thelia\Model\ProductQuery;
use Thelia\Module\BaseModule;
Expand All @@ -42,66 +44,10 @@ class BetterSeo extends BaseModule
public function postActivation(ConnectionInterface $con = null)
{
try{
SeoNoindexQuery::create()->findOne();
BetterSeoQuery::create()->findOne();
}catch(\Exception $e){
$database = new Database($con);
$database->insertSql(null,[__DIR__ . "/Config/thelia.sql"]);

$products = ProductQuery::create()->find();
/** @var Product $product */
foreach ($products as $product){
$seoObject = new SeoNoindex();
$seoObject
->setObjectType("product")
->setObjectId($product->getId())
->setNoindex(0)
->setCanonicalField(null)
->save();
}
$categories = CategoryQuery::create()->find();
/** @var Category $category */
foreach ($categories as $category){
$seoObject = new SeoNoindex();
$seoObject
->setObjectType("category")
->setObjectId($category->getId())
->setNoindex(0)
->setCanonicalField(null)
->save();
}
$brands = BrandQuery::create()->find();
/** @var Brand $brand */
foreach ($brands as $brand){
$seoObject = new SeoNoindex();
$seoObject
->setObjectType("brand")
->setObjectId($brand->getId())
->setNoindex(0)
->setCanonicalField(null)
->save();
}
$folders = FolderQuery::create()->find();
/** @var Folder $folder */
foreach ($folders as $folder){
$seoObject = new SeoNoindex();
$seoObject
->setObjectType("folder")
->setObjectId($folder->getId())
->setNoindex(0)
->setCanonicalField(null)
->save();
}
$contents = ContentQuery::create()->find();
/** @var Content $content */
foreach ($contents as $content){
$seoObject = new SeoNoindex();
$seoObject
->setObjectType("content")
->setObjectId($content->getId())
->setNoindex(0)
->setCanonicalField(null)
->save();
}
}
}
}
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.2</version>
<version>1.0.3</version>
<authors>
<author>
<name>Nicolas Barbey</name>
Expand Down
8 changes: 6 additions & 2 deletions Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
</behavior>
</table>
-->
<table name="seo_noindex" namespace="BetterSeo\Model">
<table name="better_seo" namespace="BetterSeo\Model">
<column autoIncrement="true" name="id" primaryKey="true" required="true" type="INTEGER" />
<column name="object_id" required="true" type="INTEGER"/>
<column name="object_type" required="true" type="VARCHAR"/>
<column name="noindex" required="true" default="0" type="INTEGER" />
<column name="noindex" required="true" default="0" type="TINYINT" size="4" />
<column name="nofollow" required="true" default="0" type="TINYINT" size="4" />
<column name="canonical_field" required="false" type="LONGVARCHAR" />
<behavior name="i18n">
<parameter name="i18n_columns" value="noindex, nofollow, canonical_field" />
</behavior>
</table>
<external-schema filename="local/config/schema.xml" referenceOnly="true" />
</database>
28 changes: 23 additions & 5 deletions Config/thelia.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
-- seo_noindex
-- better_seo
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `seo_noindex`;
DROP TABLE IF EXISTS `better_seo`;

CREATE TABLE `seo_noindex`
CREATE TABLE `better_seo`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`object_id` INTEGER NOT NULL,
`object_type` VARCHAR(255) NOT NULL,
`noindex` INTEGER DEFAULT 0 NOT NULL,
`canonical_field` TEXT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- better_seo_i18n
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `better_seo_i18n`;

CREATE TABLE `better_seo_i18n`
(
`id` INTEGER NOT NULL,
`locale` VARCHAR(5) DEFAULT 'en_US' NOT NULL,
`noindex` TINYINT(4) DEFAULT 0 NOT NULL,
`nofollow` TINYINT(4) DEFAULT 0 NOT NULL,
`canonical_field` TEXT,
PRIMARY KEY (`id`,`locale`),
CONSTRAINT `better_seo_i18n_FK_1`
FOREIGN KEY (`id`)
REFERENCES `better_seo` (`id`)
ON DELETE CASCADE
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
57 changes: 38 additions & 19 deletions Controller/BetterSeoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@


use BetterSeo\Form\BetterSeoForm;
use BetterSeo\Model\SeoNoindex;
use BetterSeo\Model\SeoNoindexQuery;
use BetterSeo\Model\BetterSeo;
use BetterSeo\Model\BetterSeoQuery;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Model\BrandQuery;
use Thelia\Model\CategoryQuery;
use Thelia\Model\ContentQuery;
use Thelia\Model\FolderQuery;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\ProductQuery;

class BetterSeoController extends BaseAdminController
{
Expand All @@ -32,50 +39,62 @@ public function saveAction()
$noindex = 0;
};

if (null === $nofollow = $seoForm->get('nofollow_checkbox')->getData()){
$nofollow = 0;
};

$canonical = $seoForm->get('canonical_url')->getData();
if ($canonical !== null && $canonical[0] !== '/' && filter_var($canonical, FILTER_VALIDATE_URL) === false) {
throw new \InvalidArgumentException('The value "' . (string) $canonical . '" is not a valid Url or Uri.');
}
$object_id = $this->getRequest()->get('object_id');
$object_type = $this->getRequest()->get('object_type');

$objectSeo = SeoNoindexQuery::create()
$objectSeo = BetterSeoQuery::create()
->filterByObjectId($object_id)
->filterByObjectType($object_type)
->findOne();

$lang = LangQuery::create()
->filterById($this->getRequest()->get('lang_id'))
->findOne();

if (null === $objectSeo){
$objectSeo = new SeoNoindex();
$objectSeo = new BetterSeo();
$objectSeo
->setObjectId($object_id)
->setObjectType($object_type)
->setNoindex($noindex)
->setCanonicalField($canonical)
->save();
}
else{
$objectSeo
->setNoindex($noindex)
->setCanonicalField($canonical)
->save();
->setObjectType($object_type);
}
$objectSeo
->setLocale($lang->getLocale())
->setNoindex($noindex)
->setNofollow($nofollow)
->setCanonicalField($canonical)
->save();

switch ($object_type){
case 'product':
return $this->generateRedirectFromRoute('admin.products.update',[] ,['product_id' => $object_id]);
$nameInRoute = 'products';
break;

case 'category':
return $this->generateRedirectFromRoute('admin.categories.update',[] ,['category_id' => $object_id]);
$nameInRoute = 'categories';
break;

case 'folder':
return $this->generateRedirectFromRoute('admin.folders.update',[] ,['folder_id' => $object_id]);
$nameInRoute = 'folders';
break;

case 'content':
return $this->generateRedirectFromRoute('admin.content.update',[] ,['content_id' => $object_id]);
$nameInRoute = $object_type;
break;

case 'brand':
return $this->generateRedirectFromRoute('admin.brand.update',[] ,['brand_id' => $object_id]);
$nameInRoute = $object_type;
break;

}
return $this->generateRedirectFromRoute('admin.'.$nameInRoute.'.update',[] ,[$object_type.'_id' => $object_id]);

}
}
91 changes: 51 additions & 40 deletions EventListeners/SeoListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@


use AlternateHreflang\Event\AlternateHreflangEvent;
use BetterSeo\Model\SeoNoindexQuery;
use BetterSeo\Model\BetterSeoQuery;
use CanonicalUrl\Event\CanonicalUrlEvent;
use CanonicalUrl\Event\CanonicalUrlEvents;
use Sitemap\Event\SitemapEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;

use Thelia\Core\HttpFoundation\Request;

class SeoListener implements EventSubscriberInterface
{
Expand All @@ -28,68 +28,79 @@ public function __construct(Request $request)
$this->request = $request;
}

/**
* @param CanonicalUrlEvent $event
*/

public function generateCanonical(CanonicalUrlEvent $event)
{
$BetterSeoObject = $this->getBetterSeoObject();
if (null !== $BetterSeoObject){
if (null !== $BetterSeoObject->getCanonicalField()){
$event->setUrl($BetterSeoObject->getCanonicalField());
$objectType = $this->request->get('_view');
$objectId = $this->request->get($objectType.'_id');

$betterSeoObject = $this->getBetterSeoObject($objectType, $objectId);

if (null !== $betterSeoObject){
if (null !== $betterSeoObject->getCanonicalField()){
$event->setUrl($betterSeoObject->getCanonicalField());
}
}
}

public function removeHrefLang(AlternateHreflangEvent $event)
{
$BetterSeoObject = $this->getBetterSeoObject();
if (null !== $BetterSeoObject){
if (null !== $BetterSeoObject->getCanonicalField()){
$objectType = $this->request->get('_view');
$objectId = $this->request->get($objectType.'_id');

$betterSeoObject = $this->getBetterSeoObject($objectType, $objectId);

if (null !== $betterSeoObject){
if (null !== $betterSeoObject->getCanonicalField()){
$event->setUrl(null);
}
}
}

public function checkSiteMap(SitemapEvent $event)
{
$objectId = $event->getRewritingUrl()->getViewId();
$objectType = $event->getRewritingUrl()->getView();

$betterSeoObject = $this->getBetterSeoObject($objectType, $objectId);

if (null !== $betterSeoObject){
if ($betterSeoObject->getNoindex() === 1){
$event->setHide(true);
}
}
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
CanonicalUrlEvents::GENERATE_CANONICAL => ['generateCanonical', 128],
AlternateHreflangEvent::BASE_EVENT_NAME => ['removeHrefLang',128]
];
$events = [];
if (class_exists('Sitemap\Event\SitemapEvent')){
$events[SitemapEvent::SITEMAP_EVENT] = ['checkSiteMap',128];
}
if (class_exists('AlternateHreflang\Event\AlternateHreflangEvent')){
$events[AlternateHreflangEvent::BASE_EVENT_NAME] = ['removeHrefLang',128];
}
if (class_exists('CanonicalUrl\Event\CanonicalUrlEvents')){
$events[CanonicalUrlEvents::GENERATE_CANONICAL] = ['generateCanonical', 128];
}
return $events;
}

protected function getBetterSeoObject()
protected function getBetterSeoObject($objectType, $objectId)
{
$objectType = $this->request->get('_view');
$objectId = null;

switch ($objectType){
case 'product':
$objectId = $this->request->get('product_id');
break;
case 'category':
$objectId = $this->request->get('category_id');
break;
case 'brand':
$objectId = $this->request->get('brand_id');
break;
case 'folder':
$objectId = $this->request->get('folder_id');
break;
case 'content':
$objectId = $this->request->get('content_id');
break;
}
$lang = $this->request->getSession()->getLang()->getLocale();

$seoObject = SeoNoindexQuery::create()
$betterSeoObject = BetterSeoQuery::create()
->filterByObjectType($objectType)
->filterByObjectId($objectId)
->findOne();
if (null !== $betterSeoObject){
$betterSeoObject->setLocale($lang);
}

return $seoObject;
return $betterSeoObject;
}
}
Loading

0 comments on commit 690dbb3

Please sign in to comment.