Skip to content

Commit

Permalink
Merge branch 'release/v1.0.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-claeyssens committed Dec 27, 2021
2 parents 05b0f60 + 1a45815 commit 2dea1b0
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pluginName":"Storychief v3","pluginDescription":"Craft CMS plugin to use with Storychief","pluginVersion":"1.0.9","pluginAuthorName":"Storychief","pluginVendorName":"storychief","pluginAuthorUrl":"https://github.com/Story-Chief/","pluginAuthorGithub":"","codeComments":"","pluginComponents":"","consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
{"pluginName":"Storychief v3","pluginDescription":"Craft CMS plugin to use with Storychief","pluginVersion":"1.0.10","pluginAuthorName":"Storychief","pluginVendorName":"storychief","pluginAuthorUrl":"https://github.com/Story-Chief/","pluginAuthorGithub":"","codeComments":"","pluginComponents":"","consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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/).

## 1.0.10 - 2021-12-27
### Fixed
- Fixed queries with table prefix (e.g. `craft_`).
### Added
- Support for Redactor fields

## 1.0.9 - 2021-08-26
### Added
- Added beforeEntryPublish and beforeEntryUpdate events which allow altering of the entry before it is saved.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "storychief/craft-cms-v3-storychief",
"description": "Craft CMS plugin to use with Storychief",
"type": "craft-plugin",
"version": "1.0.9",
"version": "1.0.10",
"keywords": [
"craft",
"cms",
Expand Down
37 changes: 23 additions & 14 deletions src/controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use storychief\storychiefv3\events\EntryPublishEvent;
use storychief\storychiefv3\events\EntryUpdateEvent;
use storychief\storychiefv3\events\EntrySaveEvent;
use storychief\storychiefv3\storychief\Helpers\StoryChiefHelper;

class WebhookController extends Controller
{
Expand Down Expand Up @@ -167,6 +168,7 @@ protected function handlePublishEventType()
protected function handleUpdateEventType()
{
$criteria = \craft\elements\Entry::find();
$criteria->anyStatus();
$criteria->id = $this->payload['data']['external_id'];
if ($this->_isLanguageSetInPayload() && $site = $this->_getSiteForLanguage()) {
$criteria->siteId = $site['id'];
Expand Down Expand Up @@ -201,6 +203,7 @@ protected function handleUpdateEventType()
protected function handleDeleteEventType()
{
$criteria = \craft\elements\Entry::find();
$criteria->anyStatus();
$criteria->id = $this->payload['data']['external_id'];
if ($this->_isLanguageSetInPayload() && $site = $this->_getSiteForLanguage()) {
$criteria->siteId = $site['id'];
Expand Down Expand Up @@ -274,19 +277,25 @@ private function _map(Entry $entry)

// map other fields
foreach ($mapping as $fieldHandle => $scHandle) {
if (!empty($scHandle)) {
$field = Craft::$app->fields->getFieldByHandle($fieldHandle);
$class = str_replace('craft\\fields', '\\storychief\\storychiefv3\\storychief\\FieldTypes',
get_class($field)) . 'StoryChiefFieldType';
if (class_exists($class)) {
$value = $this->_filterPayloadData($scHandle);
if ($value) {
$scField = new $class();
if ($scField instanceof StoryChiefFieldTypeInterface) {
$entry->setFieldValue($fieldHandle, $scField->prepFieldData($field, $value));
}
}
}
if (empty($scHandle)) {
continue;
}

$field = Craft::$app->fields->getFieldByHandle($fieldHandle);
$class = StoryChiefHelper::getStoryChiefFieldClass($field);

if (!class_exists($class)) {
continue;
}

$value = $this->_filterPayloadData($scHandle);
if (!$value) {
continue;
}

$scField = new $class();
if ($scField instanceof StoryChiefFieldTypeInterface) {
$entry->setFieldValue($fieldHandle, $scField->prepFieldData($field, $value));
}
}

Expand Down Expand Up @@ -369,7 +378,7 @@ private function _getSiteForLanguage($language = null)
{
return (new \craft\db\Query())
->select(['id'])
->from('sites')
->from('{{%sites}}')
->where([
'language' => $language ?: $this->payload['data']['language'],
'groupId' => $this->group->id])
Expand Down
17 changes: 11 additions & 6 deletions src/storychief/FieldTypes/AssetsStoryChiefFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,34 @@ public function prepFieldData(Field $field, $fieldData)
{
if ($field->useSingleFolder) {
$volumeUID = explode(':', $field->singleUploadLocationSource)[1];
$subPath = $field['singleUploadLocationSubpath'];
} else {
$volumeUID = explode(':', $field->defaultUploadLocationSource)[1];
$subPath = $field['defaultUploadLocationSubpath'];
}

$volumeID = Craft::$app->getVolumes()->getVolumeByUid($volumeUID)->id;

$folderID = Craft::$app->assets->getRootFolderByVolumeId($volumeID)->id;

$preppedData = [];

$tempFolder = Craft::$app->getPath()->tempAssetUploadsPath;

// get remote image and store in temp path
$imageInfo = pathinfo($fieldData);
$tempPath = $tempFolder . $imageInfo['basename'];
$tempPath = Craft::$app->getPath()->getTempPath() . DIRECTORY_SEPARATOR . $imageInfo['basename'];

file_put_contents($tempPath, fopen($fieldData, 'r'));

$filename = Assets::prepareAssetName($imageInfo['basename']);

$asset = new Asset();
$asset->tempFilePath = $tempPath;
$asset->filename = $filename;
$asset->folderId = $volumeID;
$asset->volumeId = $volumeID;
$asset->folderId = $folderID;
$asset->folderPath = $subPath;
$asset->avoidFilenameConflicts = true;
$response = Craft::$app->elements->saveElement($asset);

$response = Craft::$app->elements->saveElement($asset);

// if the response is a success, get the file id
if ($response) {
Expand Down
4 changes: 2 additions & 2 deletions src/storychief/FieldTypes/CategoriesStoryChiefFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function prepFieldData(Field $field, $fieldData)
$categoryGroupUID = str_replace('group:', '', $field->source);
$categoryGroup = (new \craft\db\Query())
->select(['id'])
->from('categorygroups')
->from('{{%categorygroups}}')
->where(['uid' => $categoryGroupUID])
->one();

Expand All @@ -36,7 +36,7 @@ public function prepFieldData(Field $field, $fieldData)
$i = 0;
while ($i < $limit) {
$categoryName = $fieldData[$i];

$criteria = Category::find();
$criteria->groupId = $categoryGroupID;
$criteria->title = Db::escapeParam($categoryName);
Expand Down
6 changes: 3 additions & 3 deletions src/storychief/FieldTypes/TagsStoryChiefFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public function prepFieldData(Field $field, $fieldData)
if (!is_array($fieldData)) {
$fieldData = array($fieldData);
}

$source = $field->source;
list($type, $groupUid) = explode(':', $source);

$tagGroup = (new \craft\db\Query())
->select(['id'])
->from('taggroups')
->from('{{%taggroups}}')
->where(['uid' => $groupUid])
->one();

Expand All @@ -45,7 +45,7 @@ public function prepFieldData(Field $field, $fieldData)
$criteria->status = null;
$criteria->groupId = $groupId;
$criteria->title = Db::escapeParam($tagName);

$elements = $criteria->ids();

$preppedData = array_merge($preppedData, $elements);
Expand Down
59 changes: 59 additions & 0 deletions src/storychief/Helpers/StoryChiefHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
<?php namespace storychief\storychiefv3\storychief\Helpers;

use craft\fields\Assets;
use craft\fields\Categories;
use craft\fields\Checkboxes;
use craft\fields\Dropdown;
use craft\fields\Entries;
use craft\fields\Lightswitch;
use craft\fields\MultiSelect;
use craft\fields\PlainText;
use craft\fields\RadioButtons;
use craft\fields\Tags;
use storychief\storychiefv3\storychief\FieldTypes\AssetsStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\CategoriesStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\CheckboxesStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\DropdownStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\EntriesStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\LightswitchStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\MultiSelectStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\PlainTextStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\RadioButtonsStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\RichTextStoryChiefFieldType;
use storychief\storychiefv3\storychief\FieldTypes\TagsStoryChiefFieldType;

class StoryChiefHelper
{
public static function parseBoolean($value)
Expand Down Expand Up @@ -38,4 +60,41 @@ public static function parseBoolean($value)

return $result;
}

public static function getStoryChiefFieldClass($field)
{
if (!$field) {
return null;
}

switch (get_class($field)) {
// Default available fields
case Assets::class:
return AssetsStoryChiefFieldType::class;
case Categories::class:
return CategoriesStoryChiefFieldType::class;
case Tags::class:
return TagsStoryChiefFieldType::class;
case Entries::class:
return EntriesStoryChiefFieldType::class;
case Checkboxes::class:
return CheckboxesStoryChiefFieldType::class;
case RadioButtons::class:
return RadioButtonsStoryChiefFieldType::class;
case Dropdown::class:
return DropdownStoryChiefFieldType::class;
case Lightswitch::class:
return LightswitchStoryChiefFieldType::class;
case MultiSelect::class:
return MultiSelectStoryChiefFieldType::class;
case PlainText::class:
return PlainTextStoryChiefFieldType::class;

// Plugin installed fields
case 'craft\redactor\Field':
return RichTextStoryChiefFieldType::class;
default:
return null;
}
}
}
35 changes: 21 additions & 14 deletions src/variables/StoryChiefVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace storychief\storychiefv3\variables;

use storychief\storychiefv3\storychief\FieldTypes\StoryChiefFieldTypeInterface;
use storychief\storychiefv3\storychief\Helpers\StoryChiefHelper;
use craft;

class StoryChiefVariable
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getAllStoryChiefFields()
'type' => 'textarea',
],
];

$settings = Craft::$app->plugins->getPlugin('storychief-v3')->getSettings();
$custom_fields = $settings['custom_field_definitions'];

Expand All @@ -70,21 +71,27 @@ public function getAllStoryChiefFields()
public function getStoryChiefFieldOptions($fieldHandle)
{
$field = \Craft::$app->fields->getFieldByHandle($fieldHandle);
$class = str_replace('craft\\fields', '\\storychief\\storychiefv3\\storychief\\FieldTypes', get_class($field)).'StoryChiefFieldType';
$class = StoryChiefHelper::getStoryChiefFieldClass($field);

if (!$class || !class_exists($class)) {
return null;
}

$scField = new $class();
if (!$scField instanceof StoryChiefFieldTypeInterface) {
return null;
}

$allFields = $this->getAllStoryChiefFields();
$supportedTypes = $scField->supportedStorychiefFieldTypes();
$options = [];
if (class_exists($class)) {
$field = new $class();
if ($field instanceof StoryChiefFieldTypeInterface) {
$supportedTypes = $field->supportedStorychiefFieldTypes();
foreach ($allFields as $item) {
if (in_array($item['type'], $supportedTypes)) {
$options[] = [
'label' => $item['label'],
'value' => $item['name'],
];
}
}

foreach ($allFields as $item) {
if (in_array($item['type'], $supportedTypes, true)) {
$options[] = [
'label' => $item['label'],
'value' => $item['name'],
];
}
}

Expand Down

0 comments on commit 2dea1b0

Please sign in to comment.