Skip to content

Commit

Permalink
Merge branch 'feature/craft4' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.MD
#	composer.json
  • Loading branch information
HannahDeWachter committed Sep 30, 2022
2 parents 7de86a4 + 857b4d7 commit a60e228
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
13 changes: 12 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ 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/).

## 2.0.0 - 2022-09-30
### Added
- Craft 4 compatibility
- Added Flexmail Interests Field
### Changed
- Deprecated Flexmail Preferences Field
- Deprecated Flexmail Interest Labels Field

## 2.0.0-beta.1 - 2022-03-15
### Added
- Craft 4 compatibility

## 1.0.6 - 2022-04-04
### Fixed
- Don't throw exception when status code is 409 Conflict
- Link for updateContact


## 1.0.5 - 2022-03-03
### Fixed
- Fixed response parsing caused by API changes
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "statikbe/craft-flexmail",
"description": "Flexmail integration for Craft CMS",
"type": "craft-plugin",
"version": "1.0.6",
"version": "2.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -22,7 +22,7 @@
}
],
"require": {
"craftcms/cms": "^3.7.0"
"craftcms/cms": "^4.0.0-beta.1"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 5 additions & 2 deletions src/Flexmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
namespace statikbe\flexmail;

use Craft;
use craft\base\Model;
use craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
use statikbe\flexmail\fields\FlexmailInterestLabelsField;
use statikbe\flexmail\fields\FlexmailInterestsField;
use statikbe\flexmail\fields\FlexmailPreferencesField;
use statikbe\flexmail\models\Settings;
use statikbe\flexmail\services\Api;
Expand Down Expand Up @@ -41,7 +43,7 @@ class Flexmail extends Plugin
/**
* @var string
*/
public $schemaVersion = '1.0.0';
public string $schemaVersion = '1.0.0';

// Public Methods
// =========================================================================
Expand All @@ -60,6 +62,7 @@ public function init()
function (RegisterComponentTypesEvent $event) {
$event->types[] = FlexmailPreferencesField::class;
$event->types[] = FlexmailInterestLabelsField::class;
$event->types[] = FlexmailInterestsField::class;
}
);

Expand All @@ -71,7 +74,7 @@ function (RegisterComponentTypesEvent $event) {

// Protected Methods
// =========================================================================
protected function createSettingsModel()
protected function createSettingsModel(): Model
{
return new Settings();
}
Expand Down
3 changes: 3 additions & 0 deletions src/console/controllers/DebugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function actionGetSources()
$this->stdout(' - ', Console::FG_GREY);
$this->stdout($i['name'] . PHP_EOL, Console::FG_CYAN);
}
return true;
}

public function actionGetInterestLabels()
Expand All @@ -26,6 +27,7 @@ public function actionGetInterestLabels()
$this->stdout(' - ', Console::FG_GREY);
$this->stdout($i['name'] . PHP_EOL, Console::FG_CYAN);
}
return true;
}

public function actionGetPreferences()
Expand All @@ -36,5 +38,6 @@ public function actionGetPreferences()
$this->stdout(' - ', Console::FG_GREY);
$this->stdout($i['title'] . PHP_EOL, Console::FG_CYAN);
}
return true;
}
}
2 changes: 1 addition & 1 deletion src/controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class ContactsController extends Controller
{
protected $allowAnonymous = true;
protected bool|array|int $allowAnonymous = true;

public function actionAdd()
{
Expand Down
8 changes: 5 additions & 3 deletions src/fields/FlexmailInterestLabelsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FlexmailInterestLabelsField extends Dropdown
/**
* @inheritdoc
*/
public $optgroups = false;
public bool $optgroups = false;

public static function displayName(): string
{
Expand All @@ -21,13 +21,15 @@ public static function displayName(): string

protected function options(): array
{
Craft::$app->deprecator->log("flexmail_interest_labels_field", "Interest Labels are deprecated in Flexmail. Please use Interests instead.", __CLASS__);

try {

$labels = Flexmail::getInstance()->api->getInterestLabels();
$data = [];
$data[0]['value'] = '';
$data[0]['label'] = Craft::t('flexmail', 'Select an interest label');
foreach ($preferences['data']['_embedded']['item'] as $i) {
foreach ($labels['data']['_embedded']['item'] as $i) {
$data[$i['id']]['value'] = $i['id'];
$data[$i['id']]['label'] = $i['name'];
}
Expand All @@ -38,7 +40,7 @@ protected function options(): array
}
}

public function getSettingsHtml()
public function getSettingsHtml(): string
{
return false;
}
Expand Down
45 changes: 45 additions & 0 deletions src/fields/FlexmailInterestsField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace statikbe\flexmail\fields;

use Craft;
use craft\fields\Dropdown;
use statikbe\flexmail\Flexmail;

class FlexmailInterestsField extends Dropdown
{

/**
* @inheritdoc
*/
public bool $optgroups = false;

public static function displayName(): string
{
return Craft::t('flexmail', 'Flexmail Interests');
}

protected function options(): array
{
try {

$labels = Flexmail::getInstance()->api->getInterests();
$data = [];
$data[0]['value'] = '';
$data[0]['label'] = Craft::t('flexmail', 'Select an interest');
foreach ($labels['data']['_embedded']['item'] as $i) {
$data[$i['id']]['value'] = $i['id'];
$data[$i['id']]['label'] = $i['name'];
}
return $data;
} catch (\Exception $e) {
Craft::error($e->getMessage());
return [];
}
}

public function getSettingsHtml(): string
{
return false;
}
}
5 changes: 3 additions & 2 deletions src/fields/FlexmailPreferencesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FlexmailPreferencesField extends Dropdown
/**
* @inheritdoc
*/
public $optgroups = false;
public bool $optgroups = false;

public static function displayName(): string
{
Expand All @@ -21,6 +21,7 @@ public static function displayName(): string

protected function options(): array
{
Craft::$app->deprecator->log("flexmail_preferences_field", "Preferences are deprecated in Flexmail. Please use Interests instead.", __CLASS__);
try {

$preferences = Flexmail::getInstance()->api->getPreferences();
Expand All @@ -38,7 +39,7 @@ protected function options(): array
}
}

public function getSettingsHtml()
public function getSettingsHtml(): string
{
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

class Settings extends Model
{
public $apiUsername;
public string $apiUsername= '';

public $apiToken;
public string $apiToken = '';

public $defaultSource;
public int $defaultSource = 0;

public function rules()
public function rules(): array
{
$rules = parent::defineRules();
$rules[] = [['apiUsername', 'apiToken'], 'required'];
Expand Down
30 changes: 24 additions & 6 deletions src/services/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace statikbe\flexmail\services;

use craft\base\Component;
use craft\helpers\App;
use craft\helpers\Json;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;
Expand All @@ -21,24 +22,24 @@ class Api extends Component
* Your flexmail API username
* @var string
*/
private $username;
private string $username;

/**
* Your flexmail API password (also called private token)
* @var string
*/
private $token;
private string $token;

/**
* Base url for the current flexmail API endpoint
* @var string
*/
private $baseUrl = 'https://api.flexmail.eu';
private string $baseUrl = 'https://api.flexmail.eu';

public function init()
public function init(): void
{
$this->username = Craft::parseEnv(Flexmail::getInstance()->getSettings()->apiUsername);
$this->token = Craft::parseEnv(Flexmail::getInstance()->getSettings()->apiToken);
$this->username = App::parseEnv(Flexmail::getInstance()->getSettings()->apiUsername);
$this->token = App::parseEnv(Flexmail::getInstance()->getSettings()->apiToken);
}


Expand Down Expand Up @@ -117,6 +118,23 @@ function() {
);
}

/**
* @return array|mixed|void
* @throws \Psr\Http\Client\ClientExceptionInterface
* @link https://api.flexmail.eu/documentation/#get-/interests
*/
public function getInterests()
{
return Craft::$app->getCache()->getOrSet(
"plugin_flexmail_interests",
function() {
$url = $this->baseUrl . '/interests';
return $this->sendRequest($url);
},
216000
);
}

/**
* @return array|mixed|void
* @throws \Psr\Http\Client\ClientExceptionInterface
Expand Down
4 changes: 2 additions & 2 deletions src/services/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class Contact extends Component
*/
public $api;

private $baseUrl = 'https://api.flexmail.eu';
private string $baseUrl = 'https://api.flexmail.eu';

private $contact;


public function init()
public function init(): void
{
$this->api = Flexmail::getInstance()->api;
}
Expand Down

0 comments on commit a60e228

Please sign in to comment.