Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0.5'
Browse files Browse the repository at this point in the history
* release/1.0.5:
  Changelog for 1.0.5
  More docblock fixing
  Docblock fixing
  Better Property declarations
  Changelog and Readme for product types
  Product Type context now working
  Load the JS on product types
  • Loading branch information
joshangell committed Sep 27, 2018
2 parents e32b035 + 118c6bc commit 2447007
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 36 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).


## Unreleased


## 1.0.5 - 2018-09-27

### Added
- Added support for Product Types if Commerce is installed


## 1.0.4 - 2018-09-26

### Fixed
- Fixed an error in composer.json where the version string was not matching the tag name.
- Fixed an error in composer.json where the version string was not matching the tag name


## 1.0.3 - 2018-09-25
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ return [

Targets are alternative templates that you want to make available to Live Preview, you can add them by going to the Portal tab in the main navigation.

If you have multiple Sites then you can load different templates for each Site, as well as restrict a Target to either a Section or Category Group.
If you have multiple Sites then you can load different templates for each Site, as well as restrict a Target to either a Section, Category Group or Product Type.

Once you have a Target added you will see a select input appear in the Live Preview window allowing your users to efficiently preview their content across multiple templates.

Expand Down
42 changes: 28 additions & 14 deletions src/Portal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,19 @@
use craft\events\TemplateEvent;
use craft\helpers\Json;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\events\RegisterUrlRulesEvent;
use craft\web\View;

use craft\commerce\Plugin as CommercePlugin;

use yii\base\Event;
use yii\web\NotFoundHttpException;

/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
* it as simple as we can, but the training wheels are off. A little prior knowledge is
* going to be required to write a plugin.
*
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
*
* https://craftcms.com/docs/plugins/introduction
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*
* @property TargetsService $targets
* @method Settings getSettings()
Expand All @@ -57,6 +49,14 @@ class Portal extends Plugin
*/
public static $plugin;

/**
* Set to true if Craft Commerce is installed
*
* @var bool
*/
public static $commerceInstalled;


// Public Properties
// =========================================================================

Expand All @@ -67,6 +67,7 @@ class Portal extends Plugin
*/
public $schemaVersion = '0.1.0';


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

Expand All @@ -86,6 +87,9 @@ public function init()
parent::init();
self::$plugin = $this;

// Check if Commerce is installed
self::$commerceInstalled = class_exists(CommercePlugin::class);

// Register our CP routes
Event::on(
UrlManager::class,
Expand Down Expand Up @@ -143,6 +147,7 @@ function(TemplateEvent $event) {
* Loads up the CP resources we need for Live Preview.
*
* @throws NotFoundHttpException
* @throws \craft\errors\SiteNotFoundException
* @throws \yii\base\InvalidConfigException
*/
private function _loadLivePreviewCpResources()
Expand All @@ -157,10 +162,10 @@ private function _loadLivePreviewCpResources()
$context = false;

// Entries
if (count($segments) >= 3 && $segments[ 0 ] == 'entries')
if (count($segments) >= 3 && $segments[ 0 ] === 'entries')
{

if ($segments[ 2 ] == 'new')
if ($segments[ 2 ] === 'new')
{
$section = Craft::$app->sections->getSectionByHandle($segments[ 1 ]);
}
Expand All @@ -183,14 +188,23 @@ private function _loadLivePreviewCpResources()

}
// Category groups
else if (count($segments) >= 3 && $segments[ 0 ] == 'categories')
else if (count($segments) >= 3 && $segments[ 0 ] === 'categories')
{
$group = Craft::$app->categories->getGroupByHandle($segments[ 1 ]);
if ($group)
{
$context = 'categoryGroup:'.$group->id;
}
}
// Product Types
else if ($this::$commerceInstalled && count($segments) >= 4 && $segments[ 0 ] === 'commerce' && $segments[ 1 ] === 'products')
{
$productType = CommercePlugin::getInstance()->productTypes->getProductTypeByHandle($segments[ 2 ]);
if ($productType)
{
$context = 'productType:'.$productType->id;
}
}


// Then, if we are we can get the data we need and run
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/livepreview/LivePreviewAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class LivePreviewAsset extends AssetBundle
{
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/src/LivePreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @copyright Copyright (c) 2018 Angell & Co
* @link https://angell.io
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/

if (typeof Portal === 'undefined')
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/TargetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class TargetsController extends Controller
{
Expand Down
2 changes: 1 addition & 1 deletion src/errors/TargetNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class TargetNotFoundException extends Exception
{
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Install extends Migration
{
Expand Down
2 changes: 1 addition & 1 deletion src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Settings extends Model
{
Expand Down
11 changes: 9 additions & 2 deletions src/models/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
/**
* Target Model
*
* @property Target_SiteSettings[] $siteSettings Site-specific settings
* @property int|null $id ID
* @property string|null $name Name
* @property string|null $context Context
* @property Target_SiteSettings[] $siteSettings Site settings
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Target extends Model
{
Expand All @@ -52,6 +55,10 @@ class Target extends Model
*/
public $siteSettings;


// Private Properties
// =========================================================================

/**
* @var array|null Context Options
*/
Expand Down
17 changes: 11 additions & 6 deletions src/models/Target_SiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
use yii\base\InvalidConfigException;

/**
* Target Model
* Target Model.
*
* Models are containers for data. Just about every time information is passed
* between services, controllers, and templates in Craft, it’s passed via a model.
*
* https://craftcms.com/docs/plugins/models
* @property int|null $id ID
* @property int|null $targetId Target ID
* @property int|null $siteId Site ID
* @property string $template Template
* @property Target $target Target
* @property Site $site Site
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Target_SiteSettings extends Model
{
Expand All @@ -56,6 +58,9 @@ class Target_SiteSettings extends Model
*/
public $template;

// Private Properties
// =========================================================================

/**
* @var Target|null
*/
Expand Down
2 changes: 1 addition & 1 deletion src/records/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Target extends ActiveRecord
{
Expand Down
2 changes: 1 addition & 1 deletion src/records/Target_SiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Target_SiteSettings extends ActiveRecord
{
Expand Down
32 changes: 30 additions & 2 deletions src/services/Targets.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
use craft\models\CategoryGroup;
use craft\models\Section;

use craft\commerce\Plugin as CommercePlugin;
use craft\commerce\models\ProductType;

/**
* Targets Service
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
class Targets extends Component
{
Expand Down Expand Up @@ -239,6 +242,26 @@ public function getContextOptions(): array

}

// Product Types
if (Portal::$commerceInstalled) {
$productTypes = CommercePlugin::getInstance()->productTypes->getAllProductTypes();
if (!empty($productTypes)) {

$return[ ] = [ 'optgroup' => Craft::t('commerce', 'Product Types') ];

/** @var ProductType $productType */
foreach ($productTypes as $productType) {

$id = 'productType:'.$productType->id;

$return[ $id ] = [
'label' => $productType->name,
'value' => $id,
];
}
}
}

return $return;
}

Expand Down Expand Up @@ -373,7 +396,10 @@ public function deleteTargetById(int $targetId): bool
* Deletes a target.
*
* @param Target $target The target
*
* @return bool Whether the target was deleted successfully
* @throws \Throwable
* @throws \yii\db\Exception
*/
public function deleteTarget(Target $target): bool
{
Expand All @@ -399,8 +425,10 @@ public function deleteTarget(Target $target): bool
* Returns if the target’s template path is valid.
*
* @param Target $target
* @param int $siteId
* @param int $siteId
*
* @return bool
* @throws \yii\base\Exception
*/
public function isTargetTemplateValid(Target $target, int $siteId): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/templates/targets/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @copyright Copyright (c) 2018 Angell & Co
* @link https://angell.io
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
#}

Expand Down
2 changes: 1 addition & 1 deletion src/translations/en/portal.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author Angell & Co
* @package Portal
* @since 0.1.0
* @since 1.0.0
*/
return [
'Portal plugin loaded' => 'Portal plugin loaded',
Expand Down

0 comments on commit 2447007

Please sign in to comment.