mediasource - дополнение к staticSaver

Скрипт для установки источников файло для statciSaver

<?php
ini_set('display_errors', 1);
ini_set("max_execution_time", 50);
define('MODX_API_MODE', true);
require 'index.php';

$category_id = false;
$category_name = '';
if($modCategories = $modx->getObject('modCategory' , array('category' => 'system'))){
    $category_id = $modCategories->get('id');
    $category_name = $modCategories->get('category');
}

if($category_id) {

    /* @var modMediaSource $StaticTemplates */
    /* @var modMediaSource $StaticSnippets */
    /* @var modMediaSource $StaticSnippets */
    /* @var modMediaSource $StaticPlugins */
    /* @var modMediaSource $StaticChunks */

    // StaticTemplates
    if(!$StaticTemplates = $modx->getObject('modMediaSource', array('name' => 'StaticTemplates'))){
        $StaticTemplates = $modx->newObject('modMediaSource');
        $StaticTemplates->set('name','StaticTemplates');
        $StaticTemplates->set('description','статичных шаблонов');
        $StaticTemplates->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:18:"/static/templates/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticTemplates->save();
    }

    // StaticSnippets
    if(!$StaticSnippets = $modx->getObject('modMediaSource', array('name' => 'StaticSnippets'))){
        $StaticSnippets = $modx->newObject('modMediaSource');
        $StaticSnippets->set('name','StaticSnippets');
        $StaticSnippets->set('description','статичных сниппетов');
        $StaticSnippets->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:16:"static/snippets/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticSnippets->save();
    }

    // StaticPlugins
    if(!$StaticPlugins = $modx->getObject('modMediaSource', array('name' => 'StaticPlugins'))){
        $StaticPlugins = $modx->newObject('modMediaSource');
        $StaticPlugins->set('name','StaticPlugins');
        $StaticPlugins->set('description','статичных плагинов');
        $StaticPlugins->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:15:"static/plugins/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticPlugins->save();
    }
    // StaticChunks
    if(!$StaticChunks = $modx->getObject('modMediaSource', array('name' => 'StaticChunks'))){
        $StaticChunks = $modx->newObject('modMediaSource');
        $StaticChunks->set('name','StaticChunks');
        $StaticChunks->set('description','статичных чанков');
        $StaticChunks->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:14:"static/chunks/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticChunks->save();
    }

    $default_template = 1;
    if($modTemplate = $modx->getObject('modTemplate', array('templatename' => 'page'))){
        $default_template = $modTemplate->get('id');
    }

    // Установка настроек для StaticSaver
    $options = array(
        'staticsaver.include_category' => true,
        'staticsaver.static_chunk_file_extension' => 'tpl',
        'staticsaver.static_chunk_media_source' => $StaticChunks->get('id'),
        'staticsaver.static_plugin_file_extension' => 'php',
        'staticsaver.static_plugin_media_source' => $StaticPlugins->get('id'),
        'staticsaver.static_snippet_file_extension' => 'php',
        'staticsaver.static_snippet_media_source' => $StaticSnippets->get('id'),
        'staticsaver.static_template_file_extension' => 'tpl',
        'staticsaver.static_template_media_source' => $StaticTemplates->get('id'),
        'default_template' => $default_template, // Установка шаблона по умолчанию
        'pdotools_fenom_default' => 1,
        'pdotools_fenom_modx' => 1,
        'pdotools_fenom_parser' => 1,
        'pdotools_fenom_php' => 1,
        'cache_default' => 0,
    );

    foreach ($options as $key => $value) {
        if($tmTender = $modx->getObject('modSystemSetting', $key)){
            $tmTender->set('value',$value);
            $tmTender->save();
        }
    }

    /* @var modChunk $modChunk */
    $source_id = $StaticChunks->get('id');
    $Properties = $StaticChunks->getProperties();
    $q = $modx->newQuery('modChunk');
    $q->where(array(
        'category' => $category_id,
        'static' => false
    ));
    if($objectList = $modx->getCollection('modChunk', $q)) {
        foreach ($objectList as $modChunk) {
            $name_chunk = $modChunk->get('name');
            $path = $category_name.'/'.$name_chunk.'.tpl';
            $modChunk->set('source', $source_id);
            $modChunk->set('static', true);
            $modChunk->set('static_file', $path);
            $modChunk->save();
        }
    }

    /* @var modSnippet $modSnippet */
    $source_id = $StaticSnippets->get('id');
    $Properties = $StaticSnippets->getProperties();
    $q = $modx->newQuery('modSnippet');
    $q->where(array(
        'category' => $category_id,
        'static' => false
    ));
    if($objectList = $modx->getCollection('modSnippet', $q)) {
        foreach ($objectList as $modSnippet) {
            $name_chunk = $modSnippet->get('name');
            $path = $category_name.'/'.$name_chunk.'.php';
            $modSnippet->set('source', $source_id);
            $modSnippet->set('static', true);
            $modSnippet->set('static_file', $path);
            $modSnippet->save();
        }
    }

    /* @var modTemplate $modTemplate */
    $source_id = $StaticTemplates->get('id');
    $Properties = $StaticTemplates->getProperties();
    $q = $modx->newQuery('modTemplate');
    $q->where(array(
        'category' => $category_id,
        'static' => false
    ));
    if($objectList = $modx->getCollection('modSnippet', $q)) {
        foreach ($objectList as $modTemplate) {
            $name_chunk = $modTemplate->get('templatename');
            $path = $category_name.'/'.$name_chunk.'.tpl';
            $modTemplate->set('source', $source_id);
            $modTemplate->set('static', true);
            $modTemplate->set('static_file', $path);
            $modTemplate->save();
        }
    }
}

echo 'Установка завершена';


12 июня 2017, 12:48    23
Наверх