| Current Path : /home/poliximo/public_html/home_/plugins/system/ |
| Current File : /home/poliximo/public_html/home_/plugins/system/rsfppaypal.php |
<?php
/**
* @version 1.2.0
* @package RSform!Pro 1.2.0
* @copyright (C) 2007-2009 www.rsjoomla.com
* @license Commercial License, http://www.rsjoomla.com/terms-and-conditions.html
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );
/**
* RSForm! Pro system plugin
*/
class plgSystemrsfppaypal extends JPlugin
{
var $_products = array();
/**
* Constructor
*
* For php4 compatibility we must not use the __constructor as a constructor for plugins
* because func_get_args ( void ) returns a copy of all passed arguments NOT references.
* This causes problems with cross-referencing necessary for the observer design pattern.
*
* @access protected
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
* @since 1.0
*/
function plgSystemrsfppaypal( &$subject, $config )
{
parent::__construct( $subject, $config );
$this->loadLanguage( 'plg_system_rsfppaypal' );
$this->newComponents = array(21,22,23);
global $_products;
}
/*
Event Triggered Functions
*/
function rsfp_bk_onInit()
{
global $RSadapter;
$RSadapter->config['component_ids'] = array_merge($RSadapter->config['component_ids'],$this->newComponents);
$formId = intval(JRequest::getVar('formId'));
if($this->rsfp_containsComponents($formId,array(21)))
{
$db = &JFactory::getDBO();
//Cron that sets non paid subscribers to denied after 12 h
$db->setQuery("UPDATE #__rsform_submission_values sv LEFT JOIN #__rsform_submissions s ON s.SubmissionId=sv.SubmissionId SET sv.FieldValue=-1 WHERE sv.FieldName = '_STATUS' AND sv.FieldValue = 0 AND s.DateSubmitted < '".date('Y-m-d H:i:s',strtotime('-12 hours'))."'");
$db->query();
}
}
function rsfp_bk_onAfterShowComponents()
{
global $mainframe;
$db = JFactory::getDBO();
$formId = intval(JRequest::getVar('formId'));
$link1 = '';$link2='';
if($this->rsfp_containsComponents($formId,array(21)))
{
$db->setQuery("SELECT ComponentId FROM #__rsform_components WHERE ComponentTypeId = 21");
$link1 = ",'".$db->loadResult()."'";
}
if($this->rsfp_containsComponents($formId,array(23)))
{
$db->setQuery("SELECT ComponentId FROM #__rsform_components WHERE ComponentTypeId = 23");
$link2 = ",'".$db->loadResult()."'";
}
?>
<strong><?php echo JText::_('RSFP_PAYPAL_LABEL');?></strong>
<a href="#" onclick="displayTemplate('21'<?php echo $link1;?>);return false;" class="component" id="paypal">
<?php echo JText::_('RSFP_PAYPAL_SPRODUCT');?>
</a><div title="componentEdit" id="componentEdit21" class="componentEdit"></div>
<a href="#" onclick="displayTemplate('22');return false;" class="component" id="paypal">
<?php echo JText::_('RSFP_PAYPAL_MPRODUCT');?>
</a><div title="componentEdit" id="componentEdit22" class="componentEdit"></div>
<a href="#" onclick="displayTemplate('23'<?php echo $link2;?>);return false;" class="component" id="paypal">
<?php echo JText::_('RSFP_PAYPAL_TOTAL');?>
</a><div title="componentEdit" id="componentEdit23" class="componentEdit"></div>
<?php
}
function rsfp_bk_onAfterCreateComponentPreview($args = array())
{
global $RSadapter;
switch($args['ComponentTypeName']){
case 'paypalSingleProduct':
$args['out']='<td>'.$args['data']['CAPTION'].'</td>';
$args['out'].='<td><img src="'.$RSadapter->config['live_site'].'/administrator/components/com_rsform/images/icons/paypal.gif" /> '.$args['data']['CAPTION'].' - '.number_format($args['data']['PRICE'],$RSadapter->config['paypal.nodecimals'], $RSadapter->config['paypal.decimal'], $RSadapter->config['paypal.thousands']).' '.$RSadapter->config['paypal.currency'].'</td>';
break;
case 'paypalMultipleProducts':
$args['out']='<td>'.$args['data']['CAPTION'].'</td>';
$args['out'].='<td><img src="'.$RSadapter->config['live_site'].'/administrator/components/com_rsform/images/icons/paypal.gif" /> '.$args['data']['CAPTION'].'</td>';
break;
case 'paypalTotal':
$args['out']='<td>'.$args['data']['CAPTION'].'</td>';
$args['out'].='<td>'.number_format(0,$RSadapter->config['paypal.nodecimals'], $RSadapter->config['paypal.decimal'], $RSadapter->config['paypal.thousands']).' '.$RSadapter->config['paypal.currency'].'</td>';
break;
}
$out = $args['out'];
}
function rsfp_bk_onAfterShowConfigurationTabs()
{
global $RSadapter;
$formId = intval(JRequest::getVar('formId'));
$RSadapter->startTab(JText::_('RSFP_PAYPAL_LABEL'),"paypal");
$this->paypalConfigurationScreen($formId);
$RSadapter->endTab();
}
function rsfp_bk_onAfterCreateFrontComponentBody($args)
{
global $RSadapter;
$value = $args['value'];
if(is_array($value))
foreach($value as $key=>$vl)
{
if(is_array($vl) && !empty($vl))
foreach($vl as $k_vl=>$v_vl)
$value[$key][$k_vl] = RSstripVar($value[$key][$k_vl]);
else
$value[$key] = RSstripVar($value[$key]);
}
switch($args['r']['ComponentTypeId'])
{
case 21:
{
if(isset($args['data']['SHOW']) && $args['data']['SHOW']=='NO')
{
//Hidden
$args['out'] = '<input type="hidden" name="rsfp_paypal_item[]" value="'.$args['data']['PRICE'].'"/>
<input type="hidden" name="form['.$args['data']['NAME'].']" id="'.$args['data']['NAME'].'" value="'.$args['data']['CAPTION'].'"/>';
}
else
{
$args['out'] = '<input type="hidden" name="rsfp_paypal_item[]" id="'.$args['data']['NAME'].'" value="'.$args['data']['PRICE'].'"/>
<input type="hidden" name="form['.$args['data']['NAME'].']" id="'.$args['data']['NAME'].'" value="'.$args['data']['CAPTION'].'"/>';
}
} break;
case 22:
{
switch($args['data']['VIEW_TYPE'])
{
case 'DROPDOWN':
{
$args['out'] .= '<select '.($args['data']['MULTIPLE']=='YES' ? 'multiple="multiple"' : '').' name="form['.$args['data']['NAME'].'][]" id="paypal-'.$args['componentId'].'" '.$args['data']['ADDITIONALATTRIBUTES'].' onchange="getPrice_'.$args['formId'].'();" >';
$aux = RSisCode($args['data']['ITEMS']);
$aux = str_replace("\r","",$aux);
$items = explode("\n",$aux);
foreach($items as $item)
{
$buf = explode('|',$item);
$option_value = $buf[0];
$option_value_trimmed = str_replace('[c]','',$option_value);
$option_shown = count($buf) == 1 ? $buf[0] : $buf[1];
$option_shown_trimmed = str_replace('[c]','',$option_shown);
$option_shown_value = $option_shown_trimmed;
$option_shown_trimmed = count($buf) == 1 ? $buf[0] : $option_shown_trimmed.' - '.number_format($buf[0],$RSadapter->config['paypal.nodecimals'], $RSadapter->config['paypal.decimal'], $RSadapter->config['paypal.thousands']).' '.$RSadapter->config['paypal.currency'] ;
$product = array($buf[count($buf) == 1 ? 0 : 1] => count($buf) == 1 ? 0 : $buf[0]);
global $_products;
$_products = $this->merge($_products, $product);
$option_checked = false;
if (empty($value) && preg_match('/\[c\]/',$option_shown))
$option_checked = true;
if (!empty($value[$args['data']['NAME']]) && array_search($option_shown_value,$value[$args['data']['NAME']]) !== false)
$option_checked = true;
$args['out'] .= '<option '.($option_checked ? 'selected="selected"' : '').' value="'.$option_shown_value.'">'.$option_shown_trimmed.'</option>';
}
$args['out'] .= '</select>';
} break;
case 'CHECKBOX':
{
$i=0;
$aux = RSisCode($args['data']['ITEMS']);
$aux = str_replace("\r","",$aux);
$items = explode("\n",$aux);
foreach($items as $item)
{
$buf = explode('|',$item);
$option_value = $buf[0];
$option_value_trimmed = str_replace('[c]','',$option_value);
$option_shown = count($buf) == 1 ? $buf[0] : $buf[1];
$option_shown_trimmed = str_replace('[c]','',$option_shown);
$option_shown_value = $option_shown_trimmed;
$option_shown_trimmed = count($buf) == 1 ? $buf[0] : $option_shown_trimmed.' - '.number_format($buf[0],$RSadapter->config['paypal.nodecimals'], $RSadapter->config['paypal.decimal'], $RSadapter->config['paypal.thousands']).' '.$RSadapter->config['paypal.currency'] ;
if(!isset($buf[1])) $buf[1] = $option_shown_value = $buf[0] = 0;
$product = array($buf[1] => $buf[0]);
global $_products;
$_products = $this->merge($_products, $product);
$option_checked = false;
if (empty($value) && preg_match('/\[c\]/',$option_shown))
$option_checked = true;
if (!empty($value[$args['data']['NAME']]) && array_search($option_shown_value,$value[$args['data']['NAME']]) !== false)
$option_checked = true;
$args['out'] .= '<input '.($option_checked ? 'checked="checked"' : '').' name="form['.$args['data']['NAME'].'][]" type="checkbox" value="'.$option_shown_value.'" id="paypal-'.$args['componentId'].'-'.$i.'" '.$args['data']['ADDITIONALATTRIBUTES'].' onchange="getPrice_'.$args['formId'].'();" /><label for="paypal-'.$args['componentId'].'-'.$i.'">'.$option_shown_trimmed.'</label>';
if($args['data']['FLOW']=='VERTICAL') $args['out'].='<br/>';
$i++;
}
} break;
}
} break;
case 23:
{
$args['out'] = '<span id="paypal_total_'.$args['formId'].'" class="rsform_paypal_total">'.number_format(0,$RSadapter->config['paypal.nodecimals'], $RSadapter->config['paypal.decimal'], $RSadapter->config['paypal.thousands']).'</span> '.$RSadapter->config['paypal.currency'].' <input type="hidden" id="'.$args['data']['NAME'].'" value="" name="form['.$args['data']['NAME'].']" />';
} break;
}
}
function rsfp_bk_onAfterLoadHeadersSubmissions($args)
{
if($this->rsfp_containsComponents($args['SManager']->formId,$this->newComponents))
{
$args['SManager']->headers[] = JText::_('RSFP_PAYPAL_STATUS');
}
}
function rsfp_bk_onAfterLoadComponents($args)
{
if($this->rsfp_containsComponents($args['SManager']->formId,$this->newComponents))
{
//Make the status column show in the submission rows
$args['components']['_STATUS'] = 1;
//Load a label into it
foreach ($args['submissions'] as $submission_id => $submission)
{
$args['submissions'][$submission_id]['SubmissionValues']['_STATUS']['Value'] = JText::_('RSFP_PAYPAL_STATUS_'.$args['submissions'][$submission_id]['SubmissionValues']['_STATUS']['Value']);
}
}
}
function rsfp_f_onSwitchTasks()
{
$plugin_task = JRequest::getVar('plugin_task');
switch($plugin_task){
case 'paypal.notify':
$this->rsfp_f_paypalNotify();
exit();
break;
default:
break;
}
}
function rsfp_f_onBeforeFormDisplay($args)
{
global $RSadapter;
if($this->rsfp_containsComponents($args['formId'],$this->newComponents))
{
$paypals = array();
$paypals = RScomponentExists($args['formId'], 22);
$total = RScomponentExists($args['formId'], 23);
$totaldetails = RSgetComponentProperties(@$total[0]);
}
if(count(RScomponentExists($args['formId'], 22)) > 0)
{
$args['formLayout'] .='<script type="text/javascript">';
$args['formLayout'] .='
function getPrice_'.$args['formId'].'()
{
price = 0;
products = new Array();
';
global $_products;
foreach ($_products as $product => $price)
{
$product = addslashes($product);
$product = str_replace('[c]','',$product);
$args['formLayout'] .= "products['".$product."'] = '".$price."';\n";
}
foreach ($paypals as $componentId)
{
$details = RSgetComponentProperties($componentId);
if($details['MULTIPLE'] == 'YES' && $details['VIEW_TYPE']== 'DROPDOWN')
{
$args['formLayout'] .= "var elemd = document.getElementById('paypal-".$componentId."');
for(i=0;i<elemd.options.length;i++)
{
if(elemd.options[i].selected == true ) price += parseFloat(products[elemd.options[i].value]);
}";
} elseif($details['VIEW_TYPE']== 'DROPDOWN') $args['formLayout'] .= "price += parseFloat(products[document.getElementById('paypal-".$componentId."').value]);\n";
if ($details['VIEW_TYPE'] == 'CHECKBOX')
{
$args['formLayout'] .= "\n var elemc = document.getElementsByName('form[".$details['NAME']."][]');
for(i=0;i<elemc.length;i++)
{
if(elemc[i].checked == true ) price += parseFloat(products[elemc[i].value]);
}";
}
}
$args['formLayout'] .= '
document.getElementById(\'paypal_total_'.$args['formId'].'\').innerHTML = number_format( price, '.$RSadapter->config['paypal.nodecimals'].', \''.$RSadapter->config['paypal.decimal'].'\', \''.$RSadapter->config['paypal.thousands'].'\');
document.getElementById(\'paypal_total_'.$args['formId'].'\').value = price;
document.getElementById(\''.$totaldetails['NAME'].'\').value = price;
}
';
$args['formLayout'] .='</script>';
$args['formLayout'] .='<script type="text/javascript">getPrice_'.$args['formId'].'();</script>';
}
if($this->rsfp_containsComponents($args['formId'],array(21)))
{
$RSadapter = $GLOBALS['RSadapter'];
$args['formLayout'].='<script type="text/javascript">';
$args['formLayout'].="rsfp_paypal_items = document.getElementsByName('rsfp_paypal_item[]');
total = 0;
for(i=0;i<rsfp_paypal_items.length;i++)
{
total += parseInt(rsfp_paypal_items[i].value);
}
total = number_format( total, ".$RSadapter->config['paypal.nodecimals'].", '".$RSadapter->config['paypal.decimal']."', '".$RSadapter->config['paypal.thousands']."' );
document.getElementById('paypal_total_".$args['formId']."').innerHTML = total;
document.getElementById('".@$totaldetails['NAME']."').value = total;
</script>
";
}
}
function rsfp_f_onBeforeStoreSubmissions($args)
{
if($this->rsfp_containsComponents($args['formId'],$this->newComponents))
{
global $RSadapter;
$args['post']['_STATUS'] = '0';
}
}
function rsfp_f_onAfterFormProcess($args)
{
if($this->rsfp_containsComponents($args['formId'],$this->newComponents))
{
global $mainframe,$RSadapter;
$db = JFactory::getDBO();
$products = ''; $price = '';
$total = RScomponentExists($args['formId'], 23);
$totaldetails = RSgetComponentProperties($total[0]);
$multiplePayments = RScomponentExists($args['formId'], 22);
if(!empty($multiplePayments))
{
foreach($multiplePayments as $payment)
{
$pdetail = RSgetComponentProperties($payment);
$detail = RSgetSubmissionValue($args['SubmissionId'],$payment);
if($detail == '') continue;
$products .= urlencode(strip_tags($pdetail['CAPTION']).' - '.strip_tags($detail)).',';
}
$price = urlencode(RSgetSubmissionValue($args['SubmissionId'],$totaldetails['componentId']));
$products = rtrim($products,',');
} else
{
//Get Component properties
$data = RSgetComponentProperties(RSresolveComponentName('rsfp_Product', $args['formId']));
$products = urlencode(strip_tags($data['CAPTION']));
$price = urlencode($data['PRICE']);
}
//build verification code
$db->setQuery("SELECT DateSubmitted FROM #__rsform_submissions WHERE SubmissionId = '".$args['SubmissionId']."'");
$code = md5($args['SubmissionId'].$db->loadResult());
$paypal_link = $RSadapter->config['paypal.test'] ? 'https://www.paypal.com/cgi-bin/webscr' : 'https://www.sandbox.paypal.com/';
if($price > 0)
{
$link = $paypal_link . '?cmd=_xclick&business=' . urlencode($RSadapter->config['paypal.email']) . '&item_name=' . $products . '¤cy_code=' . urlencode($RSadapter->config['paypal.currency'] ). '&amount=' . $price . '&return_url='.urlencode(JURI::root().'index.php?option=com_rsform&formId='.$args['formId'].'&task=plugin&plugin_task=paypal.return&code='.$code).'¬ify_url='.urlencode(JURI::root().'index.php?option=com_rsform&formId='.$args['formId'].'&task=plugin&plugin_task=paypal.notify&code='.$code).'&return='.urlencode($RSadapter->config['paypal.return']);
$mainframe->redirect($link);
}
}
}
/*
Task Functions
*/
/*
Additional Functions
*/
//Notification receipt from Paypal
function rsfp_f_paypalNotify()
{
$db = &JFactory::getDBO();
$code = $db->getEscaped(JRequest::getVar('code'));
$formId = intval(JRequest::getVar('formId'));
$q = "UPDATE #__rsform_submission_values sv LEFT JOIN #__rsform_submissions s ON s.SubmissionId = sv.SubmissionId SET sv.FieldValue=1 WHERE sv.FieldName='_STATUS' AND sv.FormId='".$formId."' AND MD5(CONCAT(s.SubmissionId,s.DateSubmitted)) = '".$code."'";
$db->setQuery($q);
$db->query();
}
function paypalScreen()
{
echo 'paypal';
}
function paypalConfigurationScreen()
{
global $RSadapter;
?>
<div id="page-payments">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td valign="top">
<table class="adminform">
<tr>
<td width="150">
<label for="currency">
<?php echo JText::_( 'RSFP_PAYPAL_EMAIL' ).':'; ?>
</label>
</td>
<td>
<input type="text" name="rsformConfig[paypal.email]" value="<?php echo $RSadapter->config['paypal.email']; ?>" size="30" maxlength="50">
</td>
</tr>
<tr>
<td width="150">
<label for="return">
<?php echo JText::_( 'RSFP_PAYPAL_RETURN' ).':'; ?>
</label>
</td>
<td>
<input type="text" name="rsformConfig[paypal.return]" value="<?php echo $RSadapter->config['paypal.return']; ?>" size="30">
</td>
</tr>
<tr>
<td width="150">
<label for="currency">
<?php echo JText::_( 'RSFP_PAYPAL_TEST' ).':'; ?>
</label>
</td>
<td>
<?php echo JHTML::_('select.booleanlist', 'rsformConfig[paypal.test]' , '' , $RSadapter->config['paypal.test']);?>
</td>
</tr>
<tr>
<td width="150">
<label for="currency">
<?php echo JText::_( 'RSFP_PAYPAL_CURRENCY' ).':'; ?>
</label>
</td>
<td>
<input type="text" name="rsformConfig[paypal.currency]" value="<?php echo $RSadapter->config['paypal.currency']; ?>" size="4" maxlength="50">
</td>
</tr>
<tr>
<td width="150">
<label for="thousands">
<?php echo JText::_( 'RSFP_PAYPAL_THOUSANDS' ).':'; ?>
</label>
</td>
<td>
<input type="text" name="rsformConfig[paypal.thousands]" value="<?php echo $RSadapter->config['paypal.thousands']; ?>" size="4" maxlength="50">
</td>
</tr>
<tr>
<td width="150">
<label for="decimal">
<?php echo JText::_( 'RSFP_PAYPAL_DECIMAL_SEPARATOR' ).':'; ?>
</label>
</td>
<td>
<input type="text" name="rsformConfig[paypal.decimal]" value="<?php echo $RSadapter->config['paypal.decimal']; ?>" size="4" maxlength="50">
</td>
</tr>
<tr>
<td width="150">
<label for="nr.decimal">
<?php echo JText::_( 'RSFP_PAYPAL_NR_DECIMALS' ).':'; ?>
</label>
</td>
<td>
<input type="text" name="rsformConfig[paypal.nodecimals]" value="<?php echo $RSadapter->config['paypal.nodecimals']; ?>" size="4" maxlength="50">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<?php
}
function rsfp_containsComponents($formId, $components)
{
global $RSadapter;
$db = JFactory::getDBO();
$db->setQuery("SELECT count(*) CNT FROM #__rsform_components WHERE FormId='".$formId."' AND ComponentTypeId IN (".implode(',',$components).")");
return $db->loadResult();
}
function merge($a,$b)
{
foreach($b as $key => $value)
$a[$key] = $value;
return $a;
}
function rsfp_bk_onAfterShowExportComponents($formComponentsHtml, $order)
{
$formComponentsHtml .= '
<tr>
<th class="title">'._RSFORM_BACKEND_SUBMISSIONS_EXPORT_HEAD_EXPORT.'</th>
<th class="title">PayPal</th>
<th class="title">'._RSFORM_BACKEND_SUBMISSIONS_EXPORT_HEAD_COLUMN_ORDER.'</th></tr>';
$formComponentsHtml .=
'<tr class="row0">
<td><input type="checkbox" name="ExportSubmission[_STATUS]" value="1"/></td>
<td>'.JText::_('RSFP_PAYPAL_STATUS').'</td>
<td><input type="text" name="ExportOrder[_STATUS]" value="'.$order.'" size="3"/></td>
</tr>'."\r\n";
}
function rsfp_bk_onAfterLoadRowsSubmissions($args)
{
if ($args['SManager']->export && is_array($args['return']))
foreach ($args['return'] as $i => $row)
{
if (isset($row['SubmissionValues']['_STATUS']))
$args['return'][$i]['SubmissionValues']['_STATUS']['Value'] = JText::_('RSFP_PAYPAL_STATUS_'.$row['SubmissionValues']['_STATUS']['Value']);
}
}
}