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

Add Operator to Read the Newest Version of an Object #746

Open
wants to merge 7 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions .github/ci/files/config/export.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ services:
tags:
- { name: pimcore.data_object.grid_column_config.operator_factory, id: Alias }

pimcore.data_object.grid_column_config.operator.factory.versiongetter:
class: Pimcore\Bundle\AdminBundle\DataObject\GridColumnConfig\Operator\Factory\DefaultOperatorFactory
arguments:
$className: Pimcore\Bundle\AdminBundle\DataObject\GridColumnConfig\Operator\VersionGetter
tags:
- { name: pimcore.data_object.grid_column_config.operator_factory, id: VersionGetter }

pimcore.data_object.grid_column_config.operator.factory.workflowstate:
class: Pimcore\Bundle\AdminBundle\DataObject\GridColumnConfig\Operator\Factory\WorkflowStateFactory
tags:
Expand Down
140 changes: 140 additions & 0 deletions public/js/pimcore/object/gridcolumn/operator/VersionGetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @category Pimcore
* @package Object
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/


pimcore.registerNS("pimcore.object.gridcolumn.operator.versiongetter");
/**
* @private
*/
pimcore.object.gridcolumn.operator.versiongetter = Class.create(pimcore.object.gridcolumn.operator.text, {
type: "operator",
operatorGroup: null,
class: "VersionGetter",
iconCls: "pimcore_icon_operator_alias",
defaultText: "VersionGetter",
group: "other",

getConfigTreeNode: function (configAttributes) {
let node;
if (configAttributes) {
node = {
draggable: true,
iconCls: this.iconCls,
text: configAttributes.label,
configAttributes: configAttributes,
isTarget: true,
allowChildren: true,
expanded: true,
leaf: false,
expandable: false,
isChildAllowed: this.allowChild
};
} else {

//For building up operator list
configAttributes = {type: this.type, class: this.class};

node = {
draggable: true,
iconCls: this.iconCls,
text: this.getDefaultText(),
configAttributes: configAttributes,
isTarget: true,
leaf: true,
isChildAllowed: this.allowChild
};
}
node.isOperator = true;
return node;
},


getCopyNode: function (source) {
const copy = source.createNode({
iconCls: this.iconCls,
text: source.data.text,
isTarget: true,
leaf: false,
expandable: false,
isOperator: true,
configAttributes: {
label: source.data.text,
type: this.type,
class: this.class

},
isChildAllowed: this.allowChild
});

return copy;
},


getConfigDialog: function (node, params) {
this.node = node;

this.textfield = new Ext.form.TextField({
fieldLabel: t('label'),
length: 255,
width: 200,
value: this.node.data.configAttributes.label,
renderer: Ext.util.Format.htmlEncode
});

this.configPanel = new Ext.Panel({
layout: "form",
bodyStyle: "padding: 10px;",
items: [this.textfield],
buttons: [{
text: t("apply"),
iconCls: "pimcore_icon_apply",
handler: function () {
this.commitData(params);
}.bind(this)
}]
});

this.window = new Ext.Window({
width: 400,
height: 300,
modal: true,
title: t('settings'),
layout: "fit",
items: [this.configPanel]
});

this.window.show();
return this.window;
},

commitData: function (params) {
this.node.data.configAttributes.label = this.textfield.getValue();
this.node.set('text', this.textfield.getValue());
this.node.set('isOperator', true);

this.window.close();
if (params && params.callback) {
params.callback();
}
},

allowChild: function (targetNode, dropNode) {
if (targetNode.childNodes.length > 0) {
return false;
}
return true;
}
});
76 changes: 76 additions & 0 deletions src/DataObject/GridColumnConfig/Operator/VersionGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\AdminBundle\DataObject\GridColumnConfig\Operator;

use Pimcore\Bundle\AdminBundle\DataObject\GridColumnConfig\ResultContainer;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\DataObject\Concrete;

/**
* @internal
*/
final class VersionGetter extends AbstractOperator
{
public function getLabeledValue(array|ElementInterface $element): ResultContainer|\stdClass|null
{
$result = new \stdClass();
$result->label = $this->label;

if(!$element instanceof Concrete){
// TODO: Should we handle arrays?
return $result;
}

$children = $this->getChildren();

if (!$children) {
return $result;
} else {
$c = $children[0];

$valueArray = [];

$latestVersion = $element->getLatestVersion(null, false);
if($latestVersion){
$element = $latestVersion->loadData();
}

$childResult = $c->getLabeledValue($element);
$isArrayType = $childResult->isArrayType ?? null;
$childValues = $childResult->value;
if ($childValues && !$isArrayType) {
$childValues = [$childValues];
}

if ($childValues) {
/** @var string $childValue */
foreach ($childValues as $childValue) {
$valueArray[] = $childValue;
}
}

$result->isArrayType = $isArrayType;
if ($isArrayType) {
$result->value = $valueArray;
} else {
$result->value = $valueArray[0] ?? null;
}
}
cancan101 marked this conversation as resolved.
Show resolved Hide resolved

return $result;
}
}
Loading