Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VARIOUS #25

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ This method will be called by `HasOneEdit::getInlineFields` even if your class d

### Using with your own form

To add support to your own forms, you need to add the `SGN\HasOneEdit\UpdateFormExtension` extension to your controller and call `$this->extend('updateEditForm', $form)` before returning the form to the template. Without this, the fields will not get populated with the values from the `has_one` though saving will work.
To add support to your own forms, you need to add the `Sunnysideup\HasOneEdit\UpdateFormExtension` extension to your controller and call `$this->extend('updateEditForm', $form)` before returning the form to the template. Without this, the fields will not get populated with the values from the `has_one` though saving will work.
6 changes: 3 additions & 3 deletions _config/hasoneedit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ After: '#coreextensions'
---
SilverStripe\CMS\Controllers\CMSMain:
extensions:
- SGN\HasOneEdit\UpdateFormExtension
- Sunnysideup\HasOneEdit\UpdateFormExtension
SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest:
extensions:
- SGN\HasOneEdit\UpdateFormExtension
- Sunnysideup\HasOneEdit\UpdateFormExtension
SilverStripe\ORM\DataObject:
extensions:
- SGN\HasOneEdit\DataObjectExtension
- Sunnysideup\HasOneEdit\DataObjectExtension
42 changes: 28 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
{
"name": "stevie-mayhew/hasoneedit",
"name": "sunnysideup/hasoneedit",
"type": "silverstripe-vendormodule",
"description": "Allows editing the fields of a has_one object directly in the CMS",
"keywords": ["silverstripe", "has_one", "cms"],
"keywords": [
"silverstripe",
"has_one",
"cms",
"silverstripe",
"Cms",
"SilverStripe CMS",
"HasOne",
"Has one",
"Form Fields"
],
"license": "WTFPL",
"authors": [{
"name": "Simon Welsh",
"email": "simon@simon.geek.nz",
"homepage": "http://simon.geek.nz"
}],
"authors": [
{
"name": "Simon Welsh",
"email": "simon@simon.geek.nz",
"homepage": "http://simon.geek.nz"
},
{
"name": "Stevie Mayhew",
"email": "NotSure@somewhere-in-a-universe-not-far-from-here.com"
}
],
"require": {
"silverstripe/framework": "~4.0 || ~5.0",
"silverstripe/asset-admin": "~1.0 || ~2.0"
},
"replace" : {
"simonwelsh/hasoneedit": "*"
"replace": {
"simonwelsh/hasoneedit": "*",
"stevie-mayhew/hasoneedit": "*"
},
"autoload": {
"psr-4": {"SGN\\HasOneEdit\\": "src/"}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"psr-4": {
"Sunnysideup\\HasOneEdit\\": "src/"
}
},
"prefer-stable": true,
Expand Down
33 changes: 28 additions & 5 deletions src/DataObjectExtension.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace SGN\HasOneEdit;
namespace Sunnysideup\HasOneEdit;

use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;

Expand All @@ -16,14 +17,19 @@ public function onBeforeWrite()
$toWrite = [];

foreach ($changed as $name => $value) {
if (!HasOneEdit::isHasOneEditField($name)) continue;
if (!HasOneEdit::isHasOneEditField($name)) {
continue;
}

list($relationName, $fieldOnRelation) = HasOneEdit::getRelationNameAndField($name);
$relatedObject = HasOneEdit::getRelationRecord($this->owner, $relationName);
if ($relatedObject === null) continue;
if ($relatedObject === null) {
continue;
}

$changed = $this->checkIfFieldHasChangeForHasOnedEdit($relatedObject, $fieldOnRelation, $value);

$relatedObject->setCastedField($fieldOnRelation, $value['after']);
if ($relatedObject->isChanged(null, DataObject::CHANGE_VALUE)) {
if ($changed) {
$toWrite[$relationName] = $relatedObject;
}
}
Expand All @@ -33,4 +39,21 @@ public function onBeforeWrite()
$this->owner->setField("{$relationName}ID", $obj->ID);
}
}

private function checkIfFieldHasChangeForHasOnedEdit($relatedObject, $fieldOnRelation, $value)
{
$relatedObject->setCastedField($fieldOnRelation, $value['after']);
$dbs = Config::inst()->get($relatedObject->ClassName, 'db');
$type = $dbs[$fieldOnRelation] ?? '';

// special case for Enum
if($type && stripos($type, 'Enum') === 0) {
$dbFieldObject = $relatedObject->dbObject($fieldOnRelation);
return $dbFieldObject->getDefault() !== $value['after'];
} else {
return ! empty($value['after']) && $relatedObject->isChanged($fieldOnRelation, DataObject::CHANGE_VALUE);
}
}


}
15 changes: 8 additions & 7 deletions src/HasOneEdit.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?php

namespace SGN\HasOneEdit;
namespace Sunnysideup\HasOneEdit;

use SilverStripe\ORM\DataObject;

/**
* Class HasOneEdit
* @package SGN\HasOneEdit
* @package Sunnysideup\HasOneEdit
*/
class HasOneEdit
{
/**
*
*/
const FIELD_SEPARATOR = '-_1_-';
public const FIELD_SEPARATOR = '-_1_-';

/**
*
*/
const SUPPORTED_SEPARATORS = [
public const SUPPORTED_SEPARATORS = [
self::FIELD_SEPARATOR,
':',
'/',
Expand Down Expand Up @@ -65,9 +66,9 @@ public static function isHasOneEditField($field)
* @param string $fieldName
* @return string
*/
public static function normaliseSeparator($fieldName)
public static function normaliseSeparator(string $fieldName): string
{
return str_replace(static::SUPPORTED_SEPARATORS, static::FIELD_SEPARATOR, $fieldName);
return str_replace(static::SUPPORTED_SEPARATORS, static::FIELD_SEPARATOR, (string) $fieldName);
}

/**
Expand All @@ -83,7 +84,7 @@ public static function normaliseSeparator($fieldName)
*/
public static function getInlineFields(DataObject $parent, string $relation, ?array $fieldsToShow = [])
{
/** @var \SilverStripe\ORM\DataObject|\SGN\HasOneEdit\ProvidesHasOneInlineFields $relatedObject */
/** @var \SilverStripe\ORM\DataObject|\Sunnysideup\HasOneEdit\ProvidesHasOneInlineFields $relatedObject */
$relatedObject = static::getRelationRecord($parent, $relation);

return $relatedObject->provideHasOneInlineFields($relation, $fieldsToShow);
Expand Down
2 changes: 1 addition & 1 deletion src/HasOneUploadField.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SGN\HasOneEdit;
namespace Sunnysideup\HasOneEdit;

use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\File;
Expand Down
18 changes: 12 additions & 6 deletions src/ProvidesHasOneInlineFields.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

namespace SGN\HasOneEdit;
namespace Sunnysideup\HasOneEdit;

/**
* Trait ProvidesHasOneInlineFields
* @package SGN\HasOneEdit
* @package Sunnysideup\HasOneEdit
* @mixin \SilverStripe\ORM\DataObject
*/
trait ProvidesHasOneInlineFields
{

/**
* @param string $relationName
* @param array $fieldsToShow
Expand All @@ -25,11 +24,18 @@ public function provideHasOneInlineFields(string $relationName, ?array $fieldsTo
$finalFieldsToShow = [];
if (count($fieldsToShow) > 0) {
foreach ($fieldsToShow as $fieldsToShowEntry) {
$relList = array_keys($this->Config()->get($fieldsToShowEntry));
$list = is_array($relList) ? $relList : [$fieldsToShowEntry];
if(in_array($fieldsToShowEntry, ['db', 'has_one', 'has_many', 'many_many', 'belongs_to', 'belongs_many_many'])) {
$relList = $this->Config()->get($fieldsToShowEntry);
if(is_array($relList)) {
$fieldsToShowEntry = array_keys($relList);
} else {
$fieldsToShowEntry = [];
}
}
$fieldsToShowEntry = is_array($fieldsToShowEntry) ? $fieldsToShowEntry : [$fieldsToShowEntry];
$finalFieldsToShow = array_merge(
$finalFieldsToShow,
$list
$fieldsToShowEntry
);
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/UpdateFormExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SGN\HasOneEdit;
namespace Sunnysideup\HasOneEdit;

use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Core\Extension;
Expand All @@ -26,7 +26,9 @@ public function updateEditForm(Form $form)

foreach ($fieldList->dataFields() as $name => $field) {
$name = HasOneEdit::normaliseSeparator($name);
if (!HasOneEdit::isHasOneEditField($name)) continue;
if (!HasOneEdit::isHasOneEditField($name)) {
continue;
}

$field->setName($name);

Expand All @@ -36,11 +38,15 @@ public function updateEditForm(Form $form)
}

// Skip populating value if record doesn't exist yet, or field already has value
if (!$record || $field->Value()) continue;
if (!$record || $field->Value()) {
continue;
}

list($relationName, $fieldOnRelation) = HasOneEdit::getRelationNameAndField($name);
$relatedObject = HasOneEdit::getRelationRecord($record, $relationName);
if ($relatedObject === null) continue;
if ($relatedObject === null) {
continue;
}

if ($relatedObject->hasField($fieldOnRelation)) {
$field->setValue($relatedObject->getField($fieldOnRelation));
Expand Down