Last active
December 30, 2015 06:59
-
-
Save ablik/7792824 to your computer and use it in GitHub Desktop.
TvCurrencyConverter plugin updated
This file contains hidden or 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 | |
//*********************************** | |
// 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 = str_replace(" ","",$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 { | |
$check_select = "SELECT * FROM ".$modx->getFullTableName('site_tmplvar_contentvalues')." WHERE tmplvarid = ".$fid1." AND contentid=".$id.""; | |
$check_col = $modx->db->query($check_select); | |
$check = $modx->db->getColumn(id, $check_col); | |
if ($check !=NULL) { | |
$sql="UPDATE $tvc SET `value`='$fval1' WHERE `tmplvarid`='$fid1' AND `contentid`='$id'"; | |
} else { | |
$sql="INSERT INTO $tvc (tmplvarid, contentid, value) values ($fid1, $id, '$fval1')"; | |
} | |
} | |
//print_r($id); | |
//exit(); | |
$modx->db->query($sql); | |
} | |
if ($id == $id_curr) { | |
//todo:добавить автоматическое определение id параметра с валютой | |
$usd = '42'; | |
$eur = '45'; | |
$grn = '57'; | |
//Определяем значения курсов валют | |
$curr_usd = $modx->getTemplateVar($usd,'',$id); | |
$curr_eur = $modx->getTemplateVar($eur,'',$id); | |
$curr_grn = $modx->getTemplateVar($grn,'',$id); | |
$usd_ids = tv2id($usd, $tv_curr); | |
$eur_ids = tv2id($eur, $tv_curr); | |
$grn_ids = tv2id($grn, $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); | |
$sql_grn = "UPDATE ".$tvc." AS a " | |
. "LEFT JOIN ".$tvc." AS b " | |
. "ON a.contentid=b.contentid " | |
. "SET b.value = a.value*".$curr_grn['value']." " | |
. "WHERE a.tmplvarid = ".$tv_valuta." " | |
. "AND b.tmplvarid = ".$tv_price." " | |
. "AND b.contentid IN (".$grn_ids.")"; | |
$modx->db->query($sql_grn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment