/**
  * @param ActionData $actionData
  *
  * @return void
  */
 public function run(&$actionData)
 {
     $monsterContentConfigs = [];
     foreach ($this->entities as $definition) {
         if (!isset($definition['entity'], $definition['attributes'])) {
             continue;
         }
         $entity = $definition['entity'];
         $definition['attributes'] = (array) $definition['attributes'];
         /** @var yii\base\Model $model */
         $models = ArrayHelper::getValue($actionData->entities, $entity, []);
         $model = is_object($models) ? $models : reset($models);
         if ($model === null) {
             Yii::info("Entities list: " . yii\helpers\VarDumper::dumpAsString($actionData->entities));
             Yii::info((array) ArrayHelper::getValue($actionData->entities, $entity, []));
             Yii::info("Searching for " . $entity);
             throw new \RuntimeException("Model is empty");
         }
         foreach ($definition['attributes'] as $index => $attribute) {
             $materials = $model->{$attribute};
             if (is_array($materials) || is_object($materials)) {
                 $monsterContentConfigs[] = ['materials' => $materials, 'uniqueContentId' => $model::className() . ":{$index}:" . $model->id];
             } else {
                 Yii::warning("Model attribute {$attribute} of entity {$entity} is not array or object");
             }
         }
     }
     $content = '';
     foreach ($monsterContentConfigs as $index => $config) {
         $content .= "<!-- MonsterContent::{$index} -->\n" . MonsterContent::widget($config) . "\n\n";
     }
     $actionData->content = $content;
 }
 public function actionNewBlock()
 {
     if (isset($_POST['block'], $_POST['uniqueContentId'], $_POST['materialIndex']) === false || Yii::$app->request->isAjax === false) {
         throw new yii\web\BadRequestHttpException();
     }
     $content = \DotPlant\Monster\MonsterContent::makeMaterial(uniqid('monsterContentUNIQUE', true), $_POST['materialIndex'], ['material' => $_POST['block']], true)->run();
     return $this->renderAjax('new-block', ['content' => $content]);
 }
    public function testMonsterContent()
    {
        // clear cache
        FileHelper::removeDirectory(Yii::getAlias('@app/monster/templates/'));
        $out = MonsterContent::widget(['uniqueContentId' => 'site-index', 'materials' => ['foo' => ['material' => 'example.example-bundle.group1.block1']]]);
        $expected = <<<html
<div class="test"><div class="foo-tst">Hello, bar</div><ul class="nav">

            <li class="nav__item" data-recursive-item-key="0">One<ul class="nav__subnav nav__subnav_nest_1">            <li class="nav__item" data-recursive-item-key="0">1.1</li>
                        <li class="nav__item" data-recursive-item-key="1">1.2<ul class="nav__subnav nav__subnav_nest_2">            <li class="nav__item" data-recursive-item-key="0">1.2.1</li>
                        <li class="nav__item" data-recursive-item-key="1">1.2.2</li>
            </ul></li>
            </ul></li>

</ul></div>
html;
        $expected = preg_replace('/\\s+/', ' ', $expected);
        $out = preg_replace('/\\s+/', ' ', $out);
        static::assertSame($expected, $out);
    }
Example #4
0
<?php

/** @var \yii\web\View $this */
/** @var string $url */
use DotPlant\Monster\assets\JsTreeDark;
use kartik\icons\Icon;
use yii\helpers\Json;
use yii\helpers\Url;
if (parse_url($url, PHP_URL_QUERY)) {
    $url .= '&__monster_edit_mode__=1';
} else {
    $url .= '?__monster_edit_mode__=1';
}
JsTreeDark::register($this);
Icon::map($this);
echo \DotPlant\Monster\MonsterContent::widget(['uniqueContentId' => 'visual-builder', 'materials' => [['material' => 'core.visual-builder.components.builder', 'data' => ['environments' => [['icon' => Icon::show('list-ul'), 'environment' => 'structure'], ['icon' => Icon::show('tree'), 'environment' => 'page-structure'], ['icon' => Icon::show('list-alt'), 'environment' => 'materials'], ['icon' => Icon::show('paint-brush'), 'environment' => 'customization'], ['icon' => Icon::show('link'), 'environment' => 'action']], 'url' => $url]]]]);
/** @var \DotPlant\Monster\Repository $repository */
$repository = Yii::$app->get('monsterRepository');
//! @todo Is cache needed for Repository::dataForBuilder ?
$bundles = Json::encode($repository->dataForBuilder());
$newBlockUrl = Json::encode(Url::to(['/monster/visual-builder/new-block']));
$js = <<<js
    window.VisualBuilderSettings = {
      'bundles': {$bundles},
      'new-block-url': {$newBlockUrl}
    };
    \$(function() {
      window.FrontendMonster.builder;
    });
js;
$this->registerJs($js, \yii\web\View::POS_BEGIN);
<?php

/**
 * @var \yii\web\View    $this
 * @var TemplateRegion[] $templateRegions
 * @var array            $dataByTemplateRegion
 * @var Page             $model
 */
use app\models\Page;
use DotPlant\Monster\models\TemplateRegion;
use DotPlant\Monster\MonsterContent;
use yii\helpers\ArrayHelper;
//use yii\helpers\VarDumper;
//
//VarDumper::dump($dataByTemplateRegion, 20, true);die();
foreach ($templateRegions as $region) {
    $config = ['uniqueContentId' => 'template-' . $region->template->id . '/' . $region->key, 'regionId' => $region->id, 'regionKey' => $region->key, 'regionEntityDependent' => (bool) $region->entity_dependent, 'contentDescription' => $region->name . " [{$region->key}]"];
    $materials = $region->content;
    if ($region->entity_dependent) {
        $config['uniqueContentId'] = $model->uniqueContentIdPrefix() . '/' . $region->key;
        if (isset($model->content[$region->key])) {
            $materials = $model->content[$region->key];
        }
    }
    $config['data'] = ArrayHelper::getValue($dataByTemplateRegion, $region->key, []);
    $config['materials'] = $materials;
    echo MonsterContent::widget($config);
}
Example #6
0
    public function testConditions()
    {
        $this->clearCache();
        $dataTrue = ['fooRegion' => ['foo' => 'bar', 'bar' => 'bar']];
        $dataFalse = ['fooRegion' => ['foo' => 'fasdgfalsdf', 'bar' => 'bar']];
        $testMaterial = ['fooRegion' => ['material' => 'example.example-bundle.group1.conditions1']];
        $out = MonsterContent::widget(['uniqueContentId' => 'conditionsTest', 'data' => $dataTrue, 'materials' => $testMaterial]);
        $expected = <<<html
<div class="test">
<div class="test__true">OK</div>
</div>
html;
        static::assertSame($this->trimSpaces($expected), $this->trimSpaces($out));
        $out = MonsterContent::widget(['uniqueContentId' => 'conditionsTest', 'data' => $dataFalse, 'materials' => $testMaterial]);
        $expected = <<<html
<div class="test">
<div class="test__false">not ok</div>
</div>
html;
        static::assertSame($this->trimSpaces($expected), $this->trimSpaces($out));
    }
 /**
  * @param MonsterContentTrait $entityWithMaterials
  * @param MonsterProvidersTrait $entityWithProviders
  */
 protected function handleMaterialsProviders(&$entityWithMaterials, &$entityWithProviders)
 {
     $hash = ProvidersHelper::hashEntityProviders($entityWithProviders);
     // check all entity materials for existing providers
     // if no provider for material - create one with sample data
     $entityMaterials = $entityWithMaterials->getMaterials();
     if ($entityWithMaterials instanceof TemplateRegion) {
         $entityMaterials = [$entityWithMaterials->key => $entityMaterials];
     }
     /** @var string $region */
     /** @var array $materials */
     /** @var array $entityMaterials */
     foreach ($entityMaterials as $region => $materials) {
         /** @var string $key */
         /** @var array $materialConfiguration */
         foreach ($materials as $key => $materialConfiguration) {
             $hashToSearch = "{$region}.{$key}";
             if (in_array($hashToSearch, $hash, true) === false) {
                 $providerId = ProvidersHelper::ensureStaticProvider($entityWithProviders);
                 $providers = $entityWithProviders->getEntityDataProviders();
                 if (array_key_exists($region, $providers[$providerId]['entities']) === false) {
                     $providers[$providerId]['entities'][$region] = [];
                 }
                 $material = MonsterContent::repository()->material($materialConfiguration['material']);
                 $providers[$providerId]['entities'][$region][$key] = $material->sampleData();
                 $entityWithProviders->setEntityDataProviders($providers);
             }
         }
     }
     if ($this->action() === self::ACTION_SAVE) {
         $entityWithProviders->saveProviders();
     }
 }