Skip to content

Instantly share code, notes, and snippets.

@Dmi3yy
Forked from ablik/plugin.tcc.php
Created December 4, 2013 18:44
Show Gist options
  • Save Dmi3yy/7793136 to your computer and use it in GitHub Desktop.
Save Dmi3yy/7793136 to your computer and use it in GitHub Desktop.
//<?php
//***********************************
// TVCurrencyConverter plugin v1.1 for MODx 1.0.x
//***********************************
// Eugeny `zk` Krylov - http://nopreset.ru
// Support topic: http://modx-shopkeeper.ru/forum/viewtopic.php?id=239
//***********************************
// Description: TV Currency Converter
// Configuration: &tv_price=TV цены;string;цена &tv_valuta=TV цены в валюте;string;доллары &id_curr=id ресурса с курсом;string;1 &tv_curr=TV курса;string;курс
// System Events: OnDocFormSave
//***********************************
//update by ablik
$e = &$modx->Event;
$tvc = $modx->getFullTableName('site_tmplvar_contentvalues');
function tv2id ($tvid, $tvval) {
global $modx;
$sql = "SELECT contentid FROM ".$modx->getFullTableName('site_tmplvar_contentvalues')." WHERE tmplvarid = ".$tvval." AND value=".$tvid."";
$select = $modx->db->query($sql);
$ids = $modx->db->getColumn(contentid, $select);
$ids = implode(",", $ids);
return $ids;
}
if ($e->name == 'OnDocFormSave'){
$field1 = $modx->getTemplateVar($tv_price,'',$id);
if ( is_array($field1) ) {
$fid1 = $field1['id'];
$field2 = $modx->getTemplateVar($tv_valuta,'',$id);
$fid2 = $field2['id'];
$fval2 = $field2['value'];
$take_curr = $modx->getTemplateVar($tv_curr,'',$id);
$field3 = $modx->getTemplateVar($take_curr['value'],'',$id_curr);
$fval3 = $field3['value'];
$fval1 = round($fval2 * $fval3,0);
if ($mode == 'new') {
$sql="INSERT INTO $tvc (tmplvarid, contentid, value) values ($fid1, $id, '$fval1')";
} else {
$sql="UPDATE $tvc SET value='$fval1' WHERE tmplvarid=$fid1 AND contentid=$id";
}
$modx->db->query($sql);
}
if ($id == $id_curr) {
//todo:добавить автоматическое определение id параметра с валютой
$usd = '42';
$eur = '45';
//Определяем значения курсов валют
$curr_usd = $modx->getTemplateVar($usd,'',$id);
$curr_eur = $modx->getTemplateVar($eur,'',$id);
$usd_ids = tv2id($usd, $tv_curr);
$eur_ids = tv2id($eur, $tv_curr);
//todo: переписать в один универсальный sql запрос
$sql_usd = "UPDATE ".$tvc." AS a "
. "LEFT JOIN ".$tvc." AS b "
. "ON a.contentid=b.contentid "
. "SET b.value = a.value*".$curr_usd['value']." "
. "WHERE a.tmplvarid = ".$tv_valuta." "
. "AND b.tmplvarid = ".$tv_price." "
. "AND b.contentid IN (".$usd_ids.")";
$modx->db->query($sql_usd);
$sql_eur = "UPDATE ".$tvc." AS a "
. "LEFT JOIN ".$tvc." AS b "
. "ON a.contentid=b.contentid "
. "SET b.value = a.value*".$curr_eur['value']." "
. "WHERE a.tmplvarid = ".$tv_valuta." "
. "AND b.tmplvarid = ".$tv_price." "
. "AND b.contentid IN (".$eur_ids.")";
$modx->db->query($sql_eur);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment