Skip to content

Instantly share code, notes, and snippets.

@Pathologic
Last active May 4, 2018 11:44
Show Gist options
  • Save Pathologic/e47dc311723c92173b46111bfddf77a3 to your computer and use it in GitHub Desktop.
Save Pathologic/e47dc311723c92173b46111bfddf77a3 to your computer and use it in GitHub Desktop.
SG2TV
//<?php
/**
* SG2TV
*
* saves first or last image from SimpleGallery to TV
*
* @category plugin
* @version 1.0.0
* @internal @properties &tvName=TV name;text; &templates=Templates;text; &position=Image to save;list;first,last;first
* @internal @events OnDocFormSave
* @internal @modx_category Content
* @internal @disabled 1
**/
include_once(MODX_BASE_PATH . 'assets/lib/MODxAPI/modResource.php');
if (isset($tvName)) {
$doc = new modResource($modx);
$doc->edit($id);
if (isset($templates)) {
$templates = explode(',', $templates);
if (!in_array($doc->get('template', $templates))) return;
}
if ($doc->issetField($tvName) && empty($doc->get($tvName))) {
$dir = isset($position) && $position == 'first' ? 'ASC' : 'DESC';
$q = $modx->db->query("SELECT `sg_image` FROM {$modx->getFullTableName('sg_images')} WHERE `sg_isactive`=1 AND `sg_rid`={$id} ORDER BY `sg_index` {$dir} LIMIT 1");
if ($image = $modx->db->getValue($q)) {
$doc->set($tvName, $image)->save(false, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment