Skip to content

Commit

Permalink
Merge branch 'release/4.0.0-beta.1' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 16, 2022
2 parents 74152d5 + 0021536 commit 5f463d5
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 98 deletions.
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Do not export those files in the Composer archive (lighter dependency)
/.craftplugin export-ignore
/.gitattributes export-ignore
/.github/ export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/codeception.yml export-ignore
/composer.lock export-ignore
/buildchain/ export-ignore
/docs/ export-ignore
/src/web/assets/public/
/src/web/assets/src/
/tests/ export-ignore

# Auto detect text files and perform LF normalization
* text=auto
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @khalwat
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Similar Changelog

## 4.0.0-beta.1 - 2022.03.15

### Added

* Initial Craft CMS 4 compatibility

## 1.1.5 - 2021.06.03
### Changed
* If `orderBy` criteria is omitted, entries are now sorted by the number of relations in common. (https://github.com/nystudio107/craft-similar/issues/35)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-similar/badges/quality-score.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-similar/?branch=v1) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-similar/badges/coverage.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-similar/?branch=v1) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-similar/badges/build.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-similar/build-status/v1) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-similar/badges/code-intelligence.svg?b=v1)](https://scrutinizer-ci.com/code-intelligence)

# Similar plugin for Craft CMS 3.x
# Similar plugin for Craft CMS 4.x

Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.

Expand All @@ -10,7 +10,7 @@ Related: [Similar for Craft 2.x](https://github.com/aelvan/Similar-Craft)

## Requirements

This plugin requires Craft CMS 3.0.0 or later.
This plugin requires Craft CMS 4.0.0 or later.

## Installation

Expand Down
74 changes: 37 additions & 37 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"name": "nystudio107/craft-similar",
"description": "Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.",
"type": "craft-plugin",
"version": "1.1.5",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"elements",
"relations",
"similar"
],
"support": {
"docs": "https://nystudio107.com/plugins/similar/documentation",
"issues": "https://nystudio107.com/plugins/similar/support"
},
"license": "MIT",
"authors": [
{
"name": "nystudio107.com",
"homepage": "https://nystudio107.com/"
}
],
"require": {
"craftcms/cms": "^3.2.0"
},
"autoload": {
"psr-4": {
"nystudio107\\similar\\": "src/"
}
},
"extra": {
"name": "Similar",
"handle": "similar",
"changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft-similar/v1/CHANGELOG.md",
"class": "nystudio107\\similar\\Similar"
"name": "nystudio107/craft-similar",
"description": "Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.",
"type": "craft-plugin",
"version": "4.0.0-beta.1",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"elements",
"relations",
"similar"
],
"support": {
"docs": "https://nystudio107.com/docs/similar/",
"issues": "https://nystudio107.com/plugins/similar/support",
"source": "https://github.com/nystudio107/craft-similar"
},
"license": "MIT",
"authors": [
{
"name": "nystudio107.com",
"homepage": "https://nystudio107.com/"
}
],
"require": {
"craftcms/cms": "^4.0.0-beta.1"
},
"autoload": {
"psr-4": {
"nystudio107\\similar\\": "src/"
}
},
"extra": {
"class": "nystudio107\\similar\\Similar",
"handle": "similar",
"name": "Similar"
}
}
48 changes: 21 additions & 27 deletions src/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@

namespace nystudio107\similar;

use craft\base\Element;
use nystudio107\similar\behaviors\CountBehavior;
use nystudio107\similar\services\Similar as SimilarService;
use nystudio107\similar\variables\SimilarVariable;

use Craft;
use craft\base\Element;
use craft\base\Plugin;
use craft\elements\db\ElementQuery;
use craft\events\PopulateElementEvent;
use craft\web\twig\variables\CraftVariable;

use nystudio107\similar\behaviors\CountBehavior;
use nystudio107\similar\services\Similar as SimilarService;
use nystudio107\similar\variables\SimilarVariable;
use yii\base\Event;

/**
Expand All @@ -38,24 +36,9 @@ class Similar extends Plugin
// =========================================================================

/**
* @var Similar
* @var ?Similar
*/
public static $plugin;

// Static Methods
// =========================================================================

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'similar' => SimilarService::class,
];

parent::__construct($id, $parent, $config);
}
public static ?Similar $plugin = null;

// Public Properties
// =========================================================================
Expand All @@ -69,12 +52,23 @@ public function __construct($id, $parent = null, array $config = [])
* @var bool
*/
public bool $hasCpSection = false;

/**
* @var bool
*/
public bool $hasCpSettings = false;

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'similar' => SimilarService::class,
];

parent::__construct($id, $parent, $config);
}

// Public Methods
// =========================================================================

Expand All @@ -89,7 +83,7 @@ public function init(): void
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event): void {
static function (Event $event): void {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('similar', SimilarVariable::class);
Expand All @@ -98,11 +92,11 @@ function (Event $event): void {
Event::on(
ElementQuery::class,
ElementQuery::EVENT_AFTER_POPULATE_ELEMENT,
function(PopulateElementEvent $event): void {
static function (PopulateElementEvent $event): void {
/** @var Element $element */
$element = $event->element;
$element->attachBehavior('myCountBehavior', CountBehavior::class);
});
});

Craft::info(
Craft::t(
Expand Down
2 changes: 1 addition & 1 deletion src/behaviors/CountBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CountBehavior extends Behavior
/**
* @var int
*/
public $count;
public int $count = 0;

// Public Methods
// =========================================================================
Expand Down
46 changes: 20 additions & 26 deletions src/services/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\EntryQuery;
use craft\elements\Entry;
use craft\events\CancelableEvent;

use yii\base\Exception;
use function is_array;
use function is_object;

/**
* @author nystudio107.com
Expand All @@ -35,30 +35,30 @@ class Similar extends Component
// =========================================================================

/**
* @var string The previous order in the query
* @var string|array The previous order in the query
*/
public $preOrder;
public string|array $preOrder = [];

/**
* @var int
* @var ?int
*/
public $limit;
public ?int $limit = null;

/**
* @var Element[]
*/
public $targetElements = [];
public array $targetElements = [];

// Public Methods
// =========================================================================

/**
* @param $data
* @param array $data
*
* @return mixed
* @return array|ElementInterface
* @throws Exception
*/
public function find($data): array|\craft\elements\Entry
public function find(array $data): array|ElementInterface
{
if (!isset($data['element'])) {
throw new Exception('Required parameter `element` was not supplied to `craft.similar.find`.');
Expand All @@ -73,26 +73,21 @@ public function find($data): array|\craft\elements\Entry
$context = $data['context'];
$criteria = $data['criteria'] ?? [];

if (\is_object($criteria)) {
if (is_object($criteria)) {
/** @var ElementQueryInterface $criteria */
$criteria = $criteria->toArray();
}

// Get an ElementQuery for this Element
$elementClass = \is_object($element) ? $element::class : $element;
$elementClass = is_object($element) ? $element::class : $element;
/** @var EntryQuery $query */
$query = $this->getElementQuery($elementClass, $criteria);

// If the $query is null, just return an empty Entry
if (!$query) { // no results
return new Entry();
}

// Stash any orderBy directives from the $query for our anonymous function
$this->preOrder = $query->orderBy;
$this->limit = $query->limit;
// Extract the $tagIds from the $context
if (\is_array($context)) {
if (is_array($context)) {
$tagIds = $context;
} else {
/** @var ElementQueryInterface $context */
Expand All @@ -102,7 +97,7 @@ public function find($data): array|\craft\elements\Entry
$this->targetElements = $tagIds;

// We need to modify the actual craft\db\Query after the ElementQuery has been prepared
$query->on(ElementQuery::EVENT_AFTER_PREPARE, fn(\craft\events\CancelableEvent $event) => $this->eventAfterPrepareHandler($event));
$query->on(ElementQuery::EVENT_AFTER_PREPARE, fn(CancelableEvent $event) => $this->eventAfterPrepareHandler($event));
// Return the data as an array, and only fetch the `id` and `siteId`
$query->asArray(true);
$query->select(['elements.id', 'elements_sites.siteId']);
Expand All @@ -119,8 +114,6 @@ public function find($data): array|\craft\elements\Entry
$results = $query->all();

// Fetch the elements based on the returned `id` and `siteId`
$elements = Craft::$app->getElements();

$queryConditions = [];
$similarCounts = [];

Expand Down Expand Up @@ -177,7 +170,7 @@ public function find($data): array|\craft\elements\Entry
}

if (empty($data['criteria']['orderBy'])) {
usort($elements, fn($a, $b) => $a->count < $b->count ? 1 : ($a->count == $b->count ? 0 : -1));
usort($elements, static fn($a, $b) => $a->count < $b->count ? 1 : ($a->count == $b->count ? 0 : -1));
}

return $elements;
Expand All @@ -194,7 +187,7 @@ protected function eventAfterPrepareHandler(CancelableEvent $event): void
'count' => 'DESC',
], $this->preOrder));
} elseif (is_string($this->preOrder)) {
$query->query->orderBy('count DESC, '.str_replace('`', '', $this->preOrder));
$query->query->orderBy('count DESC, ' . str_replace('`', '', $this->preOrder));
}

$query->query->groupBy(['relations.sourceId', 'elements.id', 'elements_sites.siteId']);
Expand All @@ -220,10 +213,11 @@ protected function eventAfterPrepareHandler(CancelableEvent $event): void
/**
* Returns the element query based on $elementType and $criteria
*
* @var string|ElementInterface $elementType
* @var array $criteria
* @param string|ElementInterface $elementType
* @param array $criteria
* @return ElementQueryInterface
*/
protected function getElementQuery($elementType, array $criteria): ElementQueryInterface
protected function getElementQuery(string|ElementInterface $elementType, array $criteria): ElementQueryInterface
{
/** @var string|ElementInterface $elementType */
$query = $elementType::find();
Expand Down
2 changes: 1 addition & 1 deletion src/translations/en/similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* @since 1.0.0
*/
return [
'Similar plugin loaded' => 'Similar plugin loaded',
'{name} plugin loaded' => '{name} plugin loaded'
];
10 changes: 6 additions & 4 deletions src/variables/SimilarVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace nystudio107\similar\variables;

use craft\base\ElementInterface;
use nystudio107\similar\Similar;
use yii\base\Exception;

/**
* @author nystudio107.com
Expand All @@ -23,12 +25,12 @@ class SimilarVariable
// =========================================================================

/**
* @param $data
* @param array $data
*
* @return mixed
* @throws \yii\base\Exception
* @return array|ElementInterface
* @throws Exception
*/
public function find($data): array|\craft\elements\Entry
public function find(array $data): array|ElementInterface
{
return Similar::$plugin->similar->find($data);
}
Expand Down

0 comments on commit 5f463d5

Please sign in to comment.