Skip to content

Instantly share code, notes, and snippets.

@mythicallage
Created January 11, 2013 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mythicallage/4514994 to your computer and use it in GitHub Desktop.
Save mythicallage/4514994 to your computer and use it in GitHub Desktop.
MODx Revo snippet. This a snippet is generator a list of possible values ​​from a list of existing values by ​​tv name.
<?php
/*[[newGetTvList? &tvname=`articlestags`]]
* Аналогично:
*@EVAL return $modx->runSnippet('GetTvList',array('tvname'=>'articlestags'));
*
*
*Убирает все пробелы, заменяет ','=>'||'
*Сниппет получает все значения ТВ по tv.name и формирует в список возможных значений
*для tv Множественный выбор, Одиночный выбор.
*/
if(!isset($modx)) return;
$tvname = $modx->getOption('tvname', $scriptProperties,'articlestags');
$q = $modx->newQuery('modTemplateVar', array('name' => $tvname));
$q->limit(1);
$q->select('id');
$result = array();
if ($q->prepare() && $q->stmt->execute()) {
$ids = $q->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
if (count($ids)){
$q = $modx->newQuery('modTemplateVarResource', array('tmplvarid:IN' => $ids));
$q->select('value');
if ($q->prepare() && $q->stmt->execute()) {
$res = $q->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
if (count($res)) {
foreach ($res as $v)
{
$tags = explode(',', $v);
foreach ($tags as $tag)
{
$tag = trim($tag);
//$tag = mb_strtolower(trim($tag), 'utf-8');
//$tag = mb_strtoupper(mb_substr($tag, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($tag, 1, mb_strlen($tag), 'UTF-8');
if (!in_array($tag, $result))
{
$result[] = $tag;
}
}
}
}
}
}
}
return implode('||', $result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment