Created
February 25, 2018 22:41
-
-
Save andronex/d1fe8f903b3ca877e91657a2cc7225dd to your computer and use it in GitHub Desktop.
Создание MODX ресурсов из MIGX TV с привязкой картинок к галерее ресурса, выполненной на ms2Gallery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MODX_API_MODE', true); | |
require_once dirname((__FILE__)) . '/index.php'; | |
$modx->getService('error','error.modError'); | |
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML'); | |
$modx->setLogLevel(modX::LOG_LEVEL_ERROR); | |
$modx->ms2Gallery = $modx->getService('ms2gallery', 'ms2Gallery', MODX_CORE_PATH . 'components/ms2gallery/model/ms2gallery/'); | |
$json = $modx->getObject('modTemplateVarResource', array( | |
'tmplvarid' => 24, | |
'contentid' => 26 | |
)); | |
$array = json_decode($json->value, true); | |
$data = array(); | |
$offset = ($_GET['offset']) ? $_GET['offset'] : 0; | |
$ii = $row = 0; | |
$count = 2; | |
foreach($array as $master){ | |
if($ii < $offset) { | |
$ii++; | |
continue; | |
} | |
$ii++; | |
$row++; | |
if($row >= $count+1) break; | |
$data = array(); | |
$data['pagetitle'] = $master['name']; | |
$data['description'] = $master['description']; | |
$data['content'] = $master['maincontent']; | |
$data['template'] = 23; | |
$data['introtext'] = '[[*pagetitle]], [[*rang]] | [[*salon:master_salon_name]]'; | |
$data['published'] = true; | |
$data['class_key'] = 'modDocument'; | |
$data['context_key'] = 'web'; | |
$data['parent'] = 26; | |
$data['tvs'] = true; | |
$data['tv25'] = $master['image']; | |
$data['tv34'] = $master['rang']; | |
$data['tv26'] = str_replace(array('img', 'title'), array('image_url', 'name'), $master['sert']); | |
$data['media_source'] = $modx->getOption('ms2gallery_source_default'); | |
$salons = $master['salon']; | |
if(!is_numeric($master['salon']) && !empty($master['salon'])){ | |
$salons = ''; | |
foreach($master['salon'] as $salon){ | |
$salons .= $salon . '||'; | |
} | |
$salons = substr($salons, 0, -2); | |
} | |
$data['tv35'] = $salons; | |
$services = $master['services']; | |
if(!is_numeric($master['services']) && !empty($master['services'])){ | |
$services = ''; | |
foreach($master['services'] as $service){ | |
$services .= $service . '||'; | |
} | |
$services = substr($services, 0, -2); | |
} | |
$data['tv31'] = $services; | |
$data['files'] = $data['filealt'] = array(); | |
if(!empty($master['gallery'])){ | |
foreach(json_decode($master['gallery'], true) as $img){ | |
$data['files'][] = $img['img']; | |
$data['filealt'][] = $img['title']; | |
} | |
} | |
//print_r($data); | |
$modx->error->reset(); // Сброс ошибок | |
$response = $modx->runProcessor('resource/create', $data); | |
$object = $response->getObject(); | |
$id = $object['id']; | |
if(!empty($data['files'])){ | |
if (!empty($data['media_source'])) { | |
if ($resource = $modx->getObject('modResource', $id)) { | |
$properties = $resource->getProperties('ms2gallery'); | |
$properties['media_source'] = $data['media_source']; | |
$resource->setProperties($properties, 'ms2gallery'); | |
$resource->save(); | |
} | |
} | |
// загружаем файлы | |
foreach ($data['files'] as $key => $file) { | |
// Вызов процессора загрузки | |
$response = $modx->runProcessor('gallery/upload', array( | |
'file' => $file, // Путь к файлу | |
'id' => $id | |
), | |
array('processors_path' => MODX_CORE_PATH . 'components/ms2gallery/processors/mgr/') | |
); | |
// Вывод результата работы процессора | |
if ($response->isError()) { | |
print_r($response->getAllErrors()); | |
} | |
//привязка name и alt для картинок | |
if($data['filealt'][$key]){ | |
$response = $modx->runProcessor('gallery/update', array( | |
'id' => $response->response['object']['id'], | |
'name' => $data['filealt'][$key], | |
'alt' => $data['filealt'][$key], | |
), | |
array('processors_path' => MODX_CORE_PATH . 'components/ms2gallery/processors/mgr/') | |
); | |
// Вывод результата работы процессора | |
if ($response->isError()) { | |
print_r($response->getAllErrors()); | |
} | |
} | |
} | |
} | |
//exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment