Snippets

CompAero Динамическая таблица допов + вывод на фронте с переопределением

Created by mvoevodskiy NA
<?php

switch($modx->event->name) {

    case 'OnBeforeManagerPageInit':
        if (
            isset($scriptProperties['action']) 
            and isset($scriptProperties['action']['controller'])
            and $scriptProperties['action']['controller'] == 'resource/update'
            and isset($_REQUEST['id'])
            and $resource = $modx->getObject('modResource', (int) $_REQUEST['id'])
        ) {
            
            $parent = 509;
            $services = array();
            
            $existServices = json_decode($resource->getTVValue('services'),1);
            $modx->log(1, var_dump($existServices));
            $ESbyID = array();
            foreach($existServices as $es) {
                $ESbyID[$es['id']] = $es;
            }
            
            $categories = $modx->getCollection('modResource', array('parent' => $parent));
            $idx = 1;
            foreach($categories as $category) {
                foreach($category->getMany('Children') as $child) {
                    $id = $child->get('id');
                    $services[] = array(
                        'id' => $id,
                        'MIGX_id' => $idx,
                        'title' => '['. $category->get('pagetitle'). '] '. $child->get('pagetitle'),
                        'active' => isset($ESbyID[$id]) ? $ESbyID[$id]['active'] : 'Наследует',
                        'price' => $child->getTVValue('addition_item_price') . ' ' . $child->getTVValue('addition_item_perprice'),
                        'new_price' => isset($ESbyID[$id]) ? $ESbyID[$id]['new_price'] : '',
                    );
                    $idx++;
                }
            }
            
            
            
            // $resource->setTVValue('services', '[{"MIGX_id":"1","title":"ТестТриждыИсправленный","active":"inherit","price":"1500"},{"MIGX_id":"2","title":"second","active":"enabled"}]');
            $resource->setTVValue('services', json_encode($services));
        }

}
<?php
$services = array();
$output = '';

    // $modx->log(1, 'id: '. $id);
if ($resource = $modx->getObject('modResource', (int) $id)) {
    $existServices = json_decode($resource->getTVValue('services'),1);
    // $modx->log(1, var_dump($existServices));
    $ESbyID = array();
    foreach($existServices as $es) {
        $ESbyID[$es['id']] = $es;
    }
    // $ESbyID = array();
    while ($resource->get('parent') != 8 and $resource = $resource->getOne('Parent')) {
        $existServices = json_decode($resource->getTVValue('services'),1);
        if (!$existServices) {
            continue;
        }
        // $modx->log(1, var_dump($existServices));
        foreach($existServices as $es) {
            if (isset($ESbyID[$es['id']])) {
                if (!$ESbyID[$es['id']]['new_price']) {
                    $ESbyID[$es['id']]['new_price'] = $es['new_price'];
                }
                if ($ESbyID[$es['id']]['active'] == 'Наследует') {
                    $ESbyID[$es['id']]['active'] = $es['active'];
                }
            } else {
                $ESbyID[$es['id']] = $es;
            }
        }
    }
    
    // $modx->log(1, print_r($ESbyID,1));
    
    $categories = $modx->getCollection('modResource', array('parent' => $parent));
    // $modx->log(1, 'count($categories):');
    // $modx->log(1, count($categories));
    foreach($categories as $category) {
        $children = '';
        foreach($category->getMany('Children') as $child) {
            $id = $child->get('id');
            if (!isset($ESbyID[$id]) or ($ESbyID[$id]['active'] == 'Наследует') or ($ESbyID[$id]['active'] == 'Включено')) {
                $tvs = array();
                foreach(explode(',', $includeTVs) as $tv) {
                    $tvs['tv.'.$tv] = $child->getTVValue($tv);
                }
                if (isset($ESbyID[$id]) and $ESbyID[$id]['new_price']) { 
                    $tvs['tv.addition_item_price'] = $ESbyID[$id]['new_price']; 
                }
                $children .= $modx->getChunk($tpl, array_merge($child->toArray(), $tvs));
            }
            $idx++;
        }
        $output .= $modx->getChunk($tplCat, array_merge($category->toArray(), array('children' => $children)));
        
    }
    
}

return $output;

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.