Skip to content

Commit

Permalink
Merge pull request #19 from DivanteLtd/issue/12-basic-clipboard-view
Browse files Browse the repository at this point in the history
basic-clipboard-view
  • Loading branch information
kkirsz committed Feb 22, 2018
2 parents 02c9341 + 357a0b6 commit bf36ccf
Show file tree
Hide file tree
Showing 10 changed files with 774 additions and 3 deletions.
81 changes: 81 additions & 0 deletions src/Controller/DataObjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* @category    pimcore5-clipboard
* @date        22/02/2018
* @author      Korneliusz Kirsz <kkirsz@divante.pl>
* @copyright   Copyright (c) 2018 DIVANTE (http://divante.pl)
*/

namespace Divante\ClipboardBundle\Controller;

use Divante\ClipboardBundle\Service\ClipboardService;
use Pimcore\Bundle\AdminBundle\Controller\Admin\DataObjectController as AdminDataObjectController;
use Pimcore\Logger;
use Pimcore\Model\DataObject;
use Pimcore\Model\Element;
use Pimcore\Tool;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
* Class DataObjectController
* @package Divante\ClipboardBundle\Controller
* @Route("/admin/object")
*/
class DataObjectController extends AdminDataObjectController
{
/**
* @param Request $request
* @param ClipboardService $clipboardService
* @return JsonResponse
* @Route("/get-clipboard")
*/
public function getClipboardAction(Request $request, ClipboardService $clipboardService)
{
$object = DataObject::getById(intval($request->get('id')));
if ($object->isAllowed('view')) {
$objectData = [];

$objectData['general'] = [];
$objectData['idPath'] = Element\Service::getIdPath($object);
$allowedKeys = ['o_published', 'o_key', 'o_id', 'o_type', 'o_path', 'o_modificationDate',
'o_creationDate', 'o_userOwner', 'o_userModification'];
foreach (get_object_vars($object) as $key => $value) {
if (strstr($key, 'o_') && in_array($key, $allowedKeys)) {
$objectData['general'][$key] = $value;
}
}
$objectData['general']['fullpath'] = $object->getRealFullPath();

$objectData['general']['o_locked'] = $object->isLocked();

$objectData['properties'] = Element\Service::minimizePropertiesForEditmode($object->getProperties());
$objectData['userPermissions'] = $object->getUserPermissions();
$objectData['classes'] = $this->prepareChildClasses($clipboardService->getClasses());

// grid-config
$configFile = PIMCORE_CONFIGURATION_DIRECTORY . '/object/grid/'
. $object->getId() . '-user_' . $this->getAdminUser()->getId() . '.psf';
if (is_file($configFile)) {
$gridConfig = Tool\Serialize::unserialize(file_get_contents($configFile));
if ($gridConfig) {
$selectedClassId = $gridConfig['classId'];

foreach ($objectData['classes'] as $class) {
if ($class['id'] == $selectedClassId) {
$objectData['selectedClass'] = $selectedClassId;
break;
}
}
}
}

return $this->adminJson($objectData);
} else {
Logger::debug('prevented getting folder id [ ' . $object->getId() . ' ] because of missing permissions');

return $this->adminJson(['success' => false, 'message' => 'missing_permission']);
}
}
}
74 changes: 74 additions & 0 deletions src/Controller/DataObjectHelperController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* @category    pimcore5-clipboard
* @date        22/02/2018
* @author      Korneliusz Kirsz <kkirsz@divante.pl>
* @copyright   Copyright (c) 2018 DIVANTE (http://divante.pl)
*/

namespace Divante\ClipboardBundle\Controller;

use Divante\ClipboardBundle\Service\ClipboardService;
use Pimcore\Bundle\AdminBundle\Controller\Admin\DataObjectHelperController as AdminDataObjectHelperController;
use Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
* Class DataObjectHelperController
* @package Divante\ClipboardBundle\Controller
* @Route("/admin/object-helper")
*/
class DataObjectHelperController extends AdminDataObjectHelperController
{
/**
* @param Request $request
* @param ClipboardService $clipboardService
* @return JsonResponse
* @Route("/get-clipboard-batch-jobs")
*/
public function getClipboardBatchJobsAction(Request $request, ClipboardService $clipboardService)
{
if ($request->get('language')) {
$request->setLocale($request->get('language'));
}

$folder = DataObject::getById($request->get('folderId'));
$class = DataObject\ClassDefinition::getById($request->get('classId'));

$conditionFilters = "(o_path = ? OR o_path LIKE '"
. str_replace('//', '/', $folder->getRealFullPath() . '/')
. "%')";
$conditionFilters = [$conditionFilters];

if ($request->get('filter')) {
$conditionFilters[] = DataObject\Service::getFilterCondition($request->get('filter'), $class);
}
if ($request->get('condition')) {
$conditionFilters[] = ' (' . $request->get('condition') . ')';
}

$objectIds = $clipboardService->getObjectIds();
if (empty($objectIds)) {
$objectIds[] = 0;
}

$conditionFilters[] = 'o_id IN (' . implode(', ', $objectIds) . ')';

$className = $class->getName();
$listClass = '\\Pimcore\\Model\\DataObject\\' . ucfirst($className) . '\\Listing';
$list = new $listClass();
$list->setCondition(implode(' AND ', $conditionFilters), [$folder->getRealFullPath()]);
$list->setOrder('ASC');
$list->setOrderKey('o_id');

if ($request->get('objecttype')) {
$list->setObjectTypes([$request->get('objecttype')]);
}

$jobs = $list->loadIdList();

return $this->adminJson(['success' => true, 'jobs' => $jobs]);
}
}
4 changes: 3 additions & 1 deletion src/DivanteClipboardBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function getInstaller()
public function getJsPaths()
{
return [
'/bundles/divanteclipboard/js/pimcore/startup.js'
'/bundles/divanteclipboard/js/pimcore/startup.js',
'/bundles/divanteclipboard/js/pimcore/clipboard.js',
'/bundles/divanteclipboard/js/pimcore/search.js',
];
}

Expand Down
68 changes: 68 additions & 0 deletions src/EventListener/AdminListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @category    pimcore5-clipboard
* @date        22/02/2018
* @author      Korneliusz Kirsz <kkirsz@divante.pl>
* @copyright   Copyright (c) 2018 DIVANTE (http://divante.pl)
*/

declare(strict_types=1);

namespace Divante\ClipboardBundle\EventListener;

use Divante\ClipboardBundle\Service\ClipboardService;
use Pimcore\Event\AdminEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class AdminListener
* @package Divante\ClipboardBundle\EventListener
*/
class AdminListener implements EventSubscriberInterface
{
/**
* @var ClipboardService
*/
protected $service;

/**
* AdminListener constructor.
* @param ClipboardService $service
*/
public function __construct(ClipboardService $service)
{
$this->service = $service;
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
AdminEvents::OBJECT_LIST_BEFORE_LIST_LOAD => 'onBeforeListLoad',
];
}

/**
* @param GenericEvent $event
*/
public function onBeforeListLoad(GenericEvent $event)
{
$context = $event->getArgument('context');
if (empty($context['clipboard'])) {
return;
}

$objectIds = $this->service->getObjectIds();
if (empty($objectIds)) {
$objectIds[] = 0;
}

/** @var \Pimcore\Model\Listing\AbstractListing $list */
$list = $event->getArgument('list');
$cond = $list->getCondition() . ' AND o_id IN (' . implode(', ', $objectIds) . ')';
$list->setCondition($cond);
}
}
2 changes: 1 addition & 1 deletion src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Installer extends AbstractInstaller
/**
* @var string
*/
private $sqlTableCreate = 'CREATE TABLE `bundle_divante_clipboard` (
private $sqlTableCreate = 'CREATE TABLE IF NOT EXISTS `bundle_divante_clipboard` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`userId` int(11) unsigned NOT NULL,
`objectId` int(11) unsigned NOT NULL,
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ services:
public: true
tags: ['controller.service_arguments']

Divante\ClipboardBundle\EventListener\AdminListener:
tags:
- { name: kernel.event_subscriber }

Divante\ClipboardBundle\Service\ClipboardService: ~

Divante\ClipboardBundle\Installer:
Expand Down
124 changes: 124 additions & 0 deletions src/Resources/public/js/pimcore/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
pimcore.registerNS("pimcore.plugin.DivanteClipboardBundle.Clipboard");

pimcore.plugin.DivanteClipboardBundle.Clipboard = Class.create(pimcore.object.abstract, {

id: 1,
type: "folder",
managerKey: "clipboard",

initialize: function () {
this.options = undefined;
this.getData();
},

getData: function () {
var options = this.options || {};
Ext.Ajax.request({
url: "/admin/object/get-clipboard",
params: {id: this.id},
ignoreErrors: options.ignoreNotFoundError,
success: this.getDataComplete.bind(this),
failure: function () {
pimcore.globalmanager.remove(this.managerKey);
}.bind(this)
});
},

getDataComplete: function (response) {
try {
this.data = Ext.decode(response.responseText);
this.search = new pimcore.plugin.DivanteClipboardBundle.Search(this, "folder");
this.addTab();
} catch (e) {
console.log(e);
this.closeObject();
}
},

addTab: function () {

var tabTitle = this.data.general.o_key;
if (this.id == 1) {
tabTitle = t("divante_clipboard");
}

this.tabPanel = Ext.getCmp("pimcore_panel_tabs");
var tabId = this.managerKey;

this.tab = new Ext.Panel({
id: tabId,
title: tabTitle,
closable: true,
layout: "border",
items: [this.getTabPanel()],
iconCls: "pimcore_icon_export",
object: this
});

// remove this instance when the panel is closed
this.tab.on("destroy", function () {
pimcore.globalmanager.remove(this.managerKey);
}.bind(this));

this.tab.on("activate", function () {
this.tab.updateLayout();
pimcore.layout.refresh();
}.bind(this));

this.tab.on("afterrender", function (tabId) {
this.tabPanel.setActiveItem(tabId);
// load selected class if available
if (this.data["selectedClass"]) {
this.search.setClass(this.data["selectedClass"]);
}
}.bind(this, tabId));

this.tabPanel.add(this.tab);

// recalculate the layout
pimcore.layout.refresh();
},

getTabPanel: function () {

var items = [];

var search = this.search.getLayout();
if (search) {
items.push(search);
}

this.tabbar = new Ext.TabPanel({
tabPosition: "top",
region: "center",
deferredRender: true,
enableTabScroll: true,
border: false,
items: items,
activeTab: 0
});

return this.tabbar;
},

closeObject: function () {
try {
var panel = Ext.getCmp(this.managerKey);
if (panel) {
panel.close();
} else {
console.log("to close element not found, doing nothing.");
}

pimcore.globalmanager.remove(this.managerKey);
} catch (e) {
console.log(e);
}
},

activate: function () {
var tabId = this.managerKey;
var tabPanel = Ext.getCmp("pimcore_panel_tabs");
tabPanel.setActiveItem(tabId);
}
});

0 comments on commit bf36ccf

Please sign in to comment.