Skip to content

Commit

Permalink
[Installer] make sure already existing classes are not overwritten + …
Browse files Browse the repository at this point in the history
…add installer for object bricks + minor improvements - fixes #10 and #9
  • Loading branch information
markus-moser committed Oct 9, 2017
1 parent 77fd1cf commit ce9ad2f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
43 changes: 38 additions & 5 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Pimcore\Extension\Bundle\Installer\AbstractInstaller;
use Pimcore\Logger;
use Pimcore\Model\Object\Objectbrick\Definition;

class Installer extends AbstractInstaller
{
Expand All @@ -25,12 +26,14 @@ public function install()
$this->installPermissions();
$this->installDatabaseTables();
$this->installClasses();
$this->installBricks();

return true;
}

public function isInstalled()
{

$result = \Pimcore\Db::get()->fetchAll('SHOW TABLES LIKE "plugin_cmf_segment_assignment"');

return !empty($result);
Expand Down Expand Up @@ -230,7 +233,7 @@ public function installDatabaseTables()
}
}

public static function installClasses()
public function installClasses()
{
$sourcePath = __DIR__.'/../install/class_source';

Expand All @@ -245,19 +248,49 @@ public static function installClasses()
self::installClass('LinkActivityDefinition', $sourcePath.'/class_LinkActivityDefinition_export.json');
}

public function installBricks()
{
$sourcePath = __DIR__.'/../install/objectbrick_source';

self::installBrick('OAuth1Token', $sourcePath.'/objectbrick_OAuth1Token_export.json');
self::installBrick('OAuth2Token', $sourcePath.'/objectbrick_OAuth2Token_export.json');
}

public static function installClass($classname, $filepath)
{
$class = \Pimcore\Model\DataObject\ClassDefinition::getByName($classname);
if (!$class) {
$class = new \Pimcore\Model\DataObject\ClassDefinition();
$class->setName($classname);
$class->setGroup('CustomerManagement');

$json = file_get_contents($filepath);

$success = \Pimcore\Model\DataObject\ClassDefinition\Service::importClassDefinitionFromJson($class, $json);
if (!$success) {
Logger::err("Could not import $classname Class.");
}
}
$json = file_get_contents($filepath);
}

public static function installBrick($brickKey, $filepath)
{
try {
$brick = \Pimcore\Model\DataObject\Objectbrick\Definition::getByKey($brickKey);
} catch (\Exception $e) {
$brick = null;
}

if (!$brick) {
$brick = new \Pimcore\Model\DataObject\Objectbrick\Definition;
$brick->setKey($brickKey);

$json = file_get_contents($filepath);

$success = \Pimcore\Model\DataObject\ClassDefinition\Service::importClassDefinitionFromJson($class, $json);
if (!$success) {
Logger::err("Could not import $classname Class.");
$success = \Pimcore\Model\DataObject\ClassDefinition\Service::importObjectBrickFromJson($brick, $json);
if (!$success) {
Logger::err("Could not import $brickKey brick.");
}
}
}
}
6 changes: 3 additions & 3 deletions src/Resources/sql/segmentAssignment/datamodel.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
CREATE TABLE `plugin_cmf_segment_assignment_index` (
CREATE TABLE IF NOT EXISTS `plugin_cmf_segment_assignment_index` (
`elementId` INT(11),
`elementType` ENUM('document', 'asset', 'object'),
`segmentId` INT,
PRIMARY KEY (`elementId`, `elementType`, `segmentId`)
);

CREATE TABLE `plugin_cmf_segment_assignment_queue` (
CREATE TABLE IF NOT EXISTS `plugin_cmf_segment_assignment_queue` (
`elementId` INT(11),
`elementType` ENUM('document', 'asset', 'object'),
PRIMARY KEY (`elementId`, `elementType`)
);

CREATE TABLE `plugin_cmf_segment_assignment` (
CREATE TABLE IF NOT EXISTS `plugin_cmf_segment_assignment` (
`elementId` INT(11),
`elementType` ENUM('document', 'asset', 'object'),
`segments` TEXT,
Expand Down

0 comments on commit ce9ad2f

Please sign in to comment.