Will PayPal Pro processor work for PayPal Advance Payments? - civicrm

I am working with CiviCRM in a Wordpress install, and it has a payment processor written for PayPal Pro. However, we need to use PayPal Advanced instead. Does anyone know if the processor / API for PayPal Pro is somehow backwards compatible for PayPal Advanced Payments?
Here is the current IPN code:
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* #package CRM
* #copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
class CRM_Core_Payment_PayPalProIPN extends CRM_Core_Payment_BaseIPN {
static $_paymentProcessor = NULL;
function __construct() {
parent::__construct();
}
function getValue($name, $abort = TRUE) {
if (!empty($_POST)) {
$rpInvoiceArray = array();
$value = NULL;
$rpInvoiceArray = explode('&', $_POST['rp_invoice_id']);
foreach ($rpInvoiceArray as $rpInvoiceValue) {
$rpValueArray = explode('=', $rpInvoiceValue);
if ($rpValueArray[0] == $name) {
$value = $rpValueArray[1];
}
}
if ($value == NULL && $abort) {
echo "Failure: Missing Parameter $name<p>";
exit();
}
else {
return $value;
}
}
else {
return NULL;
}
}
static function retrieve($name, $type, $location = 'POST', $abort = TRUE) {
static $store = NULL;
$value = CRM_Utils_Request::retrieve($name, $type, $store,
FALSE, NULL, $location
);
if ($abort && $value === NULL) {
CRM_Core_Error::debug_log_message("Could not find an entry for $name in $location");
echo "Failure: Missing Parameter<p>";
exit();
}
return $value;
}
function recur(&$input, &$ids, &$objects, $first) {
if (!isset($input['txnType'])) {
CRM_Core_Error::debug_log_message("Could not find txn_type in input request");
echo "Failure: Invalid parameters<p>";
return FALSE;
}
if ($input['txnType'] == 'recurring_payment' &&
$input['paymentStatus'] != 'Completed'
) {
CRM_Core_Error::debug_log_message("Ignore all IPN payments that are not completed");
echo "Failure: Invalid parameters<p>";
return FALSE;
}
$recur = &$objects['contributionRecur'];
// make sure the invoice ids match
// make sure the invoice is valid and matches what we have in
// the contribution record
if ($recur->invoice_id != $input['invoice']) {
CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request recur is " . $recur->invoice_id . " input is " . $input['invoice']);
echo "Failure: Invoice values dont match between database and IPN request recur is " . $recur->invoice_id . " input is " . $input['invoice'];
return FALSE;
}
$now = date('YmdHis');
// fix dates that already exist
$dates = array('create', 'start', 'end', 'cancel', 'modified');
foreach ($dates as $date) {
$name = "{$date}_date";
if ($recur->$name) {
$recur->$name = CRM_Utils_Date::isoToMysql($recur->$name);
}
}
$sendNotification = FALSE;
$subscriptionPaymentStatus = NULL;
//List of Transaction Type
/*
recurring_payment_profile_created RP Profile Created
recurring_payment RP Sucessful Payment
recurring_payment_failed RP Failed Payment
recurring_payment_profile_cancel RP Profile Cancelled
recurring_payment_expired RP Profile Expired
recurring_payment_skipped RP Profile Skipped
recurring_payment_outstanding_payment RP Sucessful Outstanding Payment
recurring_payment_outstanding_payment_failed RP Failed Outstanding Payment
recurring_payment_suspended RP Profile Suspended
recurring_payment_suspended_due_to_max_failed_payment RP Profile Suspended due to Max Failed Payment
*/
//set transaction type
$txnType = $_POST['txn_type'];
//Changes for paypal pro recurring payment
switch ($txnType) {
case 'recurring_payment_profile_created':
$recur->create_date = $now;
$recur->contribution_status_id = 2;
$recur->processor_id = $_POST['recurring_payment_id'];
$recur->trxn_id = $recur->processor_id;
$subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_START;
$sendNotification = TRUE;
break;
case 'recurring_payment':
if ($first) {
$recur->start_date = $now;
}
else {
$recur->modified_date = $now;
}
//contribution installment is completed
if ($_POST['profile_status'] == 'Expired') {
$recur->contribution_status_id = 1;
$recur->end_date = $now;
$sendNotification = TRUE;
$subscriptionPaymentStatus = CRM_Core_Payment::RECURRING_PAYMENT_END;
}
// make sure the contribution status is not done
// since order of ipn's is unknown
if ($recur->contribution_status_id != 1) {
$recur->contribution_status_id = 5;
}
break;
}
$recur->save();
if ($sendNotification) {
$autoRenewMembership = FALSE;
if ($recur->id &&
isset($ids['membership']) && $ids['membership']
) {
$autoRenewMembership = TRUE;
}
//send recurring Notification email for user
CRM_Contribute_BAO_ContributionPage::recurringNotify($subscriptionPaymentStatus,
$ids['contact'],
$ids['contributionPage'],
$recur,
$autoRenewMembership
);
}
if ($txnType != 'recurring_payment') {
return;
}
if (!$first) {
//check if this contribution transaction is already processed
//if not create a contribution and then get it processed
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->trxn_id = $input['trxn_id'];
if ($contribution->trxn_id && $contribution->find()) {
CRM_Core_Error::debug_log_message("returning since contribution has already been handled");
echo "Success: Contribution has already been handled<p>";
return TRUE;
}
$contribution->contact_id = $ids['contact'];
$contribution->financial_type_id = $objects['contributionType']->id;
$contribution->contribution_page_id = $ids['contributionPage'];
$contribution->contribution_recur_id = $ids['contributionRecur'];
$contribution->receive_date = $now;
$contribution->currency = $objects['contribution']->currency;
$contribution->payment_instrument_id = $objects['contribution']->payment_instrument_id;
$contribution->amount_level = $objects['contribution']->amount_level;
$objects['contribution'] = &$contribution;
}
$this->single($input, $ids, $objects,
TRUE, $first
);
}
function single(&$input, &$ids, &$objects, $recur = FALSE, $first = FALSE) {
$contribution = &$objects['contribution'];
// make sure the invoice is valid and matches what we have in the contribution record
if ((!$recur) || ($recur && $first)) {
if ($contribution->invoice_id != $input['invoice']) {
CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request");
echo "Failure: Invoice values dont match between database and IPN request<p>contribution is" . $contribution->invoice_id . " and input is " . $input['invoice'];
return FALSE;
}
}
else {
$contribution->invoice_id = md5(uniqid(rand(), TRUE));
}
if (!$recur) {
if ($contribution->total_amount != $input['amount']) {
CRM_Core_Error::debug_log_message("Amount values dont match between database and IPN request");
echo "Failure: Amount values dont match between database and IPN request<p>";
return FALSE;
}
}
else {
$contribution->total_amount = $input['amount'];
}
$transaction = new CRM_Core_Transaction();
// fix for CRM-2842
// if ( ! $this->createContact( $input, $ids, $objects ) ) {
// return false;
// }
$participant = &$objects['participant'];
$membership = &$objects['membership'];
$status = $input['paymentStatus'];
if ($status == 'Denied' || $status == 'Failed' || $status == 'Voided') {
return $this->failed($objects, $transaction);
}
elseif ($status == 'Pending') {
return $this->pending($objects, $transaction);
}
elseif ($status == 'Refunded' || $status == 'Reversed') {
return $this->cancelled($objects, $transaction);
}
elseif ($status != 'Completed') {
return $this->unhandled($objects, $transaction);
}
// check if contribution is already completed, if so we ignore this ipn
if ($contribution->contribution_status_id == 1) {
$transaction->commit();
CRM_Core_Error::debug_log_message("returning since contribution has already been handled");
echo "Success: Contribution has already been handled<p>";
return TRUE;
}
$this->completeTransaction($input, $ids, $objects, $transaction, $recur);
}
function main($component = 'contribute') {
CRM_Core_Error::debug_var('GET', $_GET, TRUE, TRUE);
CRM_Core_Error::debug_var('POST', $_POST, TRUE, TRUE);
$objects = $ids = $input = array();
$input['component'] = $component;
// get the contribution and contact ids from the GET params
$ids['contact'] = self::getValue('c', TRUE);
$ids['contribution'] = self::getValue('b', TRUE);
$this->getInput($input, $ids);
if ($component == 'event') {
$ids['event'] = self::getValue('e', TRUE);
$ids['participant'] = self::getValue('p', TRUE);
$ids['contributionRecur'] = self::getValue('r', FALSE);
}
else {
// get the optional ids
$ids['membership'] = self::retrieve('membershipID', 'Integer', 'GET', FALSE);
$ids['contributionRecur'] = self::getValue('r', FALSE);
$ids['contributionPage'] = self::getValue('p', FALSE);
$ids['related_contact'] = self::retrieve('relatedContactID', 'Integer', 'GET', FALSE);
$ids['onbehalf_dupe_alert'] = self::retrieve('onBehalfDupeAlert', 'Integer', 'GET', FALSE);
}
if (!$ids['membership'] && $ids['contributionRecur']) {
$sql = "
SELECT m.id
FROM civicrm_membership m
INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contribution_id = %1
WHERE m.contribution_recur_id = %2
LIMIT 1";
$sqlParams = array(1 => array($ids['contribution'], 'Integer'),
2 => array($ids['contributionRecur'], 'Integer'),
);
if ($membershipId = CRM_Core_DAO::singleValueQuery($sql, $sqlParams)) {
$ids['membership'] = $membershipId;
}
}
$paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
'PayPal', 'id', 'name'
);
if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
return FALSE;
}
self::$_paymentProcessor = &$objects['paymentProcessor'];
if ($component == 'contribute' || $component == 'event') {
if ($ids['contributionRecur']) {
// check if first contribution is completed, else complete first contribution
$first = TRUE;
if ($objects['contribution']->contribution_status_id == 1) {
$first = FALSE;
}
return $this->recur($input, $ids, $objects, $first);
}
else {
return $this->single($input, $ids, $objects, FALSE, FALSE);
}
}
else {
return $this->single($input, $ids, $objects, FALSE, FALSE);
}
}
function getInput(&$input, &$ids) {
if (!$this->getBillingID($ids)) {
return FALSE;
}
$input['txnType'] = self::retrieve('txn_type', 'String', 'POST', FALSE);
$input['paymentStatus'] = self::retrieve('payment_status', 'String', 'POST', FALSE);
$input['invoice'] = self::getValue('i', TRUE);
$input['amount'] = self::retrieve('mc_gross', 'Money', 'POST', FALSE);
$input['reasonCode'] = self::retrieve('ReasonCode', 'String', 'POST', FALSE);
$billingID = $ids['billing'];
$lookup = array(
"first_name" => 'first_name',
"last_name" => 'last_name',
"street_address-{$billingID}" => 'address_street',
"city-{$billingID}" => 'address_city',
"state-{$billingID}" => 'address_state',
"postal_code-{$billingID}" => 'address_zip',
"country-{$billingID}" => 'address_country_code',
);
foreach ($lookup as $name => $paypalName) {
$value = self::retrieve($paypalName, 'String', 'POST', FALSE);
$input[$name] = $value ? $value : NULL;
}
$input['is_test'] = self::retrieve('test_ipn', 'Integer', 'POST', FALSE);
$input['fee_amount'] = self::retrieve('mc_fee', 'Money', 'POST', FALSE);
$input['net_amount'] = self::retrieve('settle_amount', 'Money', 'POST', FALSE);
$input['trxn_id'] = self::retrieve('txn_id', 'String', 'POST', FALSE);
}
}

Related

A new entity was found through the relationship?

I have a handle code
if ($this->request->isMethod('POST') && $valid) {
$em = $this->getDoctrine()->getManager();
$formData = $form->getData();
$staffValue = $formData['staff'];
$campaignDetailFilter = $modelCampaignDetail->getRepository()->countDataByCampaignDetailId($formData['campaignDetail']);
$total = count($campaignDetailFilter);
$totalStaff = count($formData['staff']);
foreach ($campaignDetailFilter as $valDetailId) {
$detailDataEntity = $modelDetailData->getEntity($valDetailId['id']);
$batchSize = $total / $totalStaff;
$i = 0;
foreach ($staffValue as $staffVal) {
$detailDataEntity->setStaff($staffVal);
$em->persist($detailDataEntity);
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
}
++$i;
}
$em->flush();
$em->clear();
}
}
But when I give $i = 0 it gets an error: A new entity was found through the relationship...that was not configured to cascade persist operations for entity.
You should remove $em->clear().
if (($i % $batchSize) === 0) {
$em->flush();
}

How reuse formatted Doctrine 2 with QueryBuilder formatted query in other query

I have formatted query with doctrine query builder. It is kind of complicated because in that query there are some my own defined doctrine functions and doctrine extensions. Also very important to mention that this query using filter which is require some preselected values. The problem is when i want to count records from that query. Of course i Can write count(results), but it is very bad for performance.
I try to create new sql query with my own predefined query as source, but the problem that parameters are not injected.
Query which is formatted and which result i want to count :
private function getCategoriesQuery(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
$qb = $this->createQueryBuilder('c')
->select('c.id as id')
->addSelect('c.title as title')
->addSelect('DATE_FORMAT(c.updationDate, \'%Y-%m-%d %H:%i:%S\') as updationDate')
->addSelect('DATE_FORMAT(c.creationDate, \'%Y-%m-%d %H:%i:%S\') as creationDate')
->addSelect('coalesce(sum(r.amount), 0.00) as total')
->addSelect('round(AveragePerMonth(coalesce(sum(r.amount), 0.00), c.creationDate, CURRENT_DATE()), 2) as averagePerMonth')
->leftJoin('c.records', 'r')
->groupBy('c.id')
->andWhere('c.user = :user')->setParameter('user', $this->user);
if ($filter) $this->applyFilters($filter, $qb);
if ($sort) $this->applySort($sort, $qb);
if ($pagination) $this->applyPagination($pagination, $qb);
return $qb->getQuery();
}
I tried:
public function countUserCategories(Filter $filter = null)
{
$rsm = new ResultSetMapping();
$sql = $this->_em->createNativeQuery('select count(*) from ('.$this->getCategoriesQuery($filter)->getSQL().') as src', $rsm);
return $sql->getSingleScalarResult();
}
I expected the answer if there is possibility to combine these queries and if there is possiblity how i can do that. Thanks
ApplyFilter method:
private function applyFilters(Filter $filter, QueryBuilder $qb)
{
if ($filter->getCategories()) {
$qb->andWhere('c.id IN(:categories)')->setParameter('categories', $filter->getCategories());
}
if ($filter->getEndDate()) {
$qb->andWhere('r.date <= :endDate')->setParameter('endDate', $filter->getEndDate());
}
if ($filter->getStartDate()) {
$qb->andWhere('r.date >= :startDate')->setParameter('startDate', $filter->getStartDate());
}
if ($filter->getStartTotal() !== null) {
$qb->andHaving('total >= :startTotal')->setParameter('startTotal', $filter->getStartTotal());
}
if ($filter->getEndTotal() !== null) {
$qb->andHaving('total <= :endTotal')->setParameter('endTotal', $filter->getEndTotal());
}
if ($filter->getStartAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth >= :startAveragePerMonth')->setParameter('startAveragePerMonth', $filter->getStartAveragePerMonth());
}
if ($filter->getEndAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth <= :endAveragePerMonth')->setParameter('endAveragePerMonth', $filter->getEndAveragePerMonth());
}
}
Suggested way to solve this issue but return response : The query returned multiple rows. Change the query or use a different result function like getScalarResult().
public function countUserCategories(Filter $filter = null)
{
return (clone $this->getCategoriesQueryBuilder($filter))
->resetDQLPart('select')
->select('COUNT(c)')
->getQuery()
->getSingleScalarResult();
}
UPDATE
I make this work by using SQL query builder
Query builder creation
private function getCategoriesQueryBuilder(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
$qb = $this->_em->getConnection()->createQueryBuilder()
->select('c.id AS id')
->addSelect('c.title AS title')
->addSelect('DATE_FORMAT(c.updation_date, \'%Y-%m-%d %H:%i:%S\') AS updationDate')
->addSelect('DATE_FORMAT(c.creation_date, \'%Y-%m-%d %H:%i:%S\') AS creationDate')
->addSelect('COALESCE(SUM(r.amount), 0.00) AS total')
->addSelect('(SELECT ROUND(COALESCE(SUM(r.amount), 0.00) / COUNT(DISTINCT(DATE_FORMAT(calendar.date, \'%Y-%m\'))), 2)
FROM calendar as calendar
WHERE calendar.date BETWEEN DATE_FORMAT(c.creation_date, \'%Y-%m-%d\') AND DATE_FORMAT(CURRENT_DATE(), \'%Y-%m-%d\'))
AS averagePerMonth')
->from('category', 'c')
->leftJoin('c', 'record', 'r', 'r.category_id = c.id')
->join('c', 'users', 'u', 'u.id = c.user_id')
->where('u.id = :userId')->setParameter('userId', $this->user->getId())
->groupBy('c.id');
if ($sort) $this->applySort($sort, $qb);
if ($pagination) $this->applyPagination($pagination, $qb);
if ($filter) $this->applyFilter($filter, $qb);
return $qb;
}
Filter
private function applyFilter(Filter $filter, QueryBuilder $qb)
{
if ($filter->getCategories()) {
$qb->andWhere('c.id IN(:categories)')->setParameter('categories', $filter->getCategories());
}
if ($filter->getEndDate()) {
$qb->andWhere('r.date <= :endDate')->setParameter('endDate', $filter->getEndDate());
}
if ($filter->getStartDate()) {
$qb->andWhere('r.date >= :startDate')->setParameter('startDate', $filter->getStartDate());
}
if ($filter->getStartTotal() !== null) {
$qb->andHaving('total >= :startTotal')->setParameter('startTotal', $filter->getStartTotal());
}
if ($filter->getEndTotal() !== null) {
$qb->andHaving('total <= :endTotal')->setParameter('endTotal', $filter->getEndTotal());
}
if ($filter->getStartAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth >= :startAveragePerMonth')->setParameter('startAveragePerMonth', $filter->getStartAveragePerMonth());
}
if ($filter->getEndAveragePerMonth() !== null) {
$qb->andHaving('averagePerMonth <= :endAveragePerMonth')->setParameter('endAveragePerMonth', $filter->getEndAveragePerMonth());
}
}
Count
public function countUserCategories(Filter $filter = null)
{
$subQuery = $this->getCategoriesQueryBuilder($filter);
$params = $subQuery->getParameters();
return $this->_em->getConnection()->createQueryBuilder()
->select('COUNT(src.id)')
->from('(' . $subQuery . ')', 'src')
->setParameters($params)
->execute()
->fetchColumn(0);
}
Fetch
public function findUserCategories(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
return $this->getCategoriesQueryBuilder($filter, $sort, $pagination)->execute()->fetchAll();
}
You can use the original query as subquery to get count.
public function countUserCategories(Filter $filter = null)
{
$querysub = $this->getCategoriesQuery($filter);
$query = $this->getDoctrine()->getConnection()->createQueryBuilder();
$query
->select('COUNT(*)')
->from('(' . $querysub->getSQL() . ')', 'ttt');
$i = 0;
foreach ($querysub->getParameters() as $parameter) {
$query->setParameter($i++, $parameter->getValue(), $parameter->getType());
}
$count = $query->execute()->fetchColumn(0);
return $count;
}
This will work if parameters are set in order with their appearance in query.

Duplicate existing contacts alert

In Vtiger 6.5.0 open source, I wants to create a alert function to warn users that the conact's mobile is existing? could you please help me. I'm fresher.
Thanks,
Loi
You can refer the function wich exist in Account module for checking Duplicate Account Name.
Please follow this files you will get an idea.
This is the code flow how its done In Account Module
Registring Pre Save Event
http://code.vtiger.com/vtiger/vtigercrm/blob/master/layouts/vlayout/modules/Accounts/resources/Edit.js#L250
This teh Fucntion to check Duplicate in cache, If not calls the Helper function
http://code.vtiger.com/vtiger/vtigercrm/blob/master/layouts/vlayout/modules/Accounts/resources/Edit.js#L83
This the Helper function which makes the call to server
http://code.vtiger.com/vtiger/vtigercrm/blob/master/resources/helper.js#L166
This is the action function which is responsible for Serving the request which came from Helper Function
http://code.vtiger.com/vtiger/vtigercrm/blob/master/modules/Accounts/actions/CheckDuplicate.php#L30
And this is the function which checks for Duplicate
http://code.vtiger.com/vtiger/vtigercrm/blob/master/modules/Accounts/models/Record.php#L57
Hope this helps.
Hi Victor please follow this steps
modules\Leads\actions\Checkprimaryemail.php
<?php
class Leads_Checkprimaryemail_Action extends Vtiger_BasicAjax_Action {
public function checkPermission(Vtiger_Request $request) {
return;
}
public function process(Vtiger_Request $request) {
global $adb;
$moduleName = $request->get('module');
$recordId = $request->get('recordId');
$primary_email = $request->get('primary_email');
/*Lead Details*/
$lead_query = "select * from vtiger_leaddetails
inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_leaddetails.leadid
where vtiger_crmentity.deleted = 0 and vtiger_leaddetails.email='".$primary_email."'";
$lead_result = $adb->query($lead_query);
$lead_email = $adb->query_result($lead_result,0,'email');
$lead_numrows = $adb->num_rows($lead_result);
/*Contact Details*/
$cont_query = "select * from vtiger_contactdetails
inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_contactdetails.contactid
where vtiger_crmentity.deleted = 0 and vtiger_contactdetails.email='".$primary_email."'";
$cont_result = $adb->query($cont_query);
$cont_email = $adb->query_result($cont_result,0,'email');
$cont_numrows = $adb->num_rows($cont_result);
if($recordId != '' ){
if($primary_email == $lead_email && $lead_numrows == 1 ){
$emailtrue = 0;
} elseif($primary_email == $cont_email && $cont_numrows >= 1 ) {
$emailtrue = 1;
}
} else {
if(($lead_numrows >=1 || $cont_numrows >=1 ) || ($lead_numrows >=1 && $cont_numrows >= 1) ){
$emailtrue = 1;
} else {
$emailtrue = 0;
}
}
$emailData = array($emailtrue);
$response = new Vtiger_Response();
$response->setResult($emailData);
$response->emit();
}
}
?>
After Create One other file
layouts\vlayout\modules\Leads\resources\Edit.js
Vtiger_Edit_Js("Leads_Edit_Js", {
}, {
changeEvent: function (container) {
jQuery('input[name="email"]').on('focusout', function (e) {
var email = jQuery('input[name="email"]').val();
var recordId = jQuery('input[name="record"]').val();
var email_length = email.length;
if (email != '') {
if (email_length > 100) {
var errorMessage = app.vtranslate('JS_EMAIL_LENGTH_VALIDATION');
params = {
text: errorMessage,
'type': 'error',
};
Vtiger_Helper_Js.showMessage(params);
}
var progressIndicatorElement = jQuery.progressIndicator({
'position': 'html',
'blockInfo': {
'enabled': true
}
});
var postData = {
"module": 'Leads',
"action": "Checkprimaryemail",
"primary_email": email,
"recordId": recordId
}
AppConnector.request(postData).then(
function (data) {
progressIndicatorElement.progressIndicator({'mode': 'hide'});
if (data['result'] == 1) {
jQuery('#emailalready_exists').val(1);
var errorMessage = app.vtranslate('JS_EMAIL_EXIST');
params = {
text: errorMessage,
'type': 'error',
};
Vtiger_Helper_Js.showMessage(params);
} else {
jQuery('#emailalready_exists').val(0);
}
},
function (error, err) {}
);
e.preventDefault();
}
});
},
registerBasicEvents: function (container) {
this._super(container);
this.changeEvent(container);
}
});
To check duplicate records in vTiger follow below steps:
Register checkDuplicate function in registerBasicEvents
1: \layouts\vlayout\modules\Contacts\resources\Edit.js
getmobile : function(container){
return jQuery('input[name="mobile"]',container).val();
},
getRecordId : function(container){
return jQuery('input[name="record"]',container).val();
},
DuplicateCheck : function(form) {
var thisInstance = this;
if(typeof form == 'undefined') {
form = this.getForm();
}
jQuery( "#mobileFieldId" ).change(function() {
var mobile = thisInstance.getmobile(form);
var recordId = thisInstance.getRecordId(form);
var params = {
'module' : "Contacts",
'action' : "CheckDuplicate",
'mobile' : mobile,
'record' : recordId
}
AppConnector.request(params).then(
function(data) {
var response = data['result'];
var result = response['success'];
if(result == true) {
var message_params = {
title : app.vtranslate('JS_MESSAGE'),
text: response['message'],
animation: 'show',
type: 'error'
};
Vtiger_Helper_Js.showPnotify(message_params);
jQuery(".btn-success").attr('disabled',true);
return false;
} else {
jQuery(".btn-success").attr('disabled',false);
}
}
);
});
},
2: Create new file in** \modules\Contacts\actions\CheckDuplicate.php
Follow the same process / code as given in \modules\Accounts\actions\CheckDuplicate.php
3: Add new function checkDuplicate() in \modules\Contacts\models\Record.php
And follow same process as given in \modules\Accounts\models\Record.php having function checkDuplicate()
Note: Don't forget to change the db table name, class name module wise.
Hope this will help you. Thank you.

how to add product to cart in opencart

Below is add to product code . But I am not getting where the values are storing . Kindly help to find out solution for this . I want to know logic behind this code
public function add($product_id, $qty = 1, $option = array(), $recurring_id = 0) {
$this->data = array();
$product['product_id'] = (int)$product_id;
if ($option) {
$product['option'] = $option;
}
if ($recurring_id) {
$product['recurring_id'] = (int)$recurring_id;
}
$key = base64_encode(serialize($product));
if ((int)$qty && ((int)$qty > 0)) {
if (!isset($this->session->data['cart'][$key])) {
$this->session->data['cart'][$key] = (int)$qty;
} else {
$this->session->data['cart'][$key] += (int)$qty;
}
}
}
The product details with options are stored in $key = base64_encode(serialize($product));. Where $this->session->data['cart'][$key] contains the number of quantity added by the customer.
For more details check the getProducts() function on the same page. Where you can find
foreach ($this->session->data['cart'] as $key => $quantity) {
....
$product = unserialize(base64_decode($key));
....
}

Regex to validate information (Letter & Number only)

Regex to validate information
I tried the following:
if(preg_match("/[A-Za-z0-9]+/", $ingame_name) == TRUE){
header("Location: newitem.php?username=". $ingame_name ."&email=". $email);
} else {
$invalidusername = '<font color=red>Oops, invalid username! Username
may only contain numbers and letters.</font><br>';
}
Which didn't work, also tried flipping the statements still didn't work...
And my final attempt
if ($_REQUEST['do'] == 'submit')
{
$ingame_name= trim($_POST['ingame_name']);
$email = trim($_POST['email']);
if(eregi("/[A-Za-z0-9]+/", $ingame_name))
{
$invalidusername = '<font color=red>Oops, invalid username! Username may only contain numbers and letters.</font><br>';
$ingame_name = 'invalid';
}
if (eregi("^[a-zA-Z0-9_]+#[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
{
$invalidemail = '<font color=red>Oops, seems that you have an error with your email format.</font></br>';
$email = 'invalid';
}
if ( $ingame_name = 'invalid' || $email = 'invalid')
{
/* do nothin */
}
else
{
header("Location: item.php?username=". $ingame_name ."&email=". $email);
}
}
Nothing seems to work,
Try this:
<?php
$valid_submit = ($_REQUEST && isset($_REQUEST['do']) && $_REQUEST['do'] == 'submit') ? true : false;
if ($valid_submit) {
$ingame_name= ($_POST && isset($_POST['ingame_name'])) ? trim($_POST['ingame_name']) : '';
$email= ($_POST && isset($_POST['email'])) ? trim($_POST['email']) : '';
$invalidusername = '';
$invalidemail = '';
if(!preg_match("/^[A-Za-z0-9]+$/", $ingame_name)) {
$invalidusername = '<font color=red>Oops, invalid username! Username may only contain numbers and letters.</font><br>';
$ingame_name = 'invalid';
}
// This is a much more efficient method to validate email addresses
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$invalidemail = '<font color=red>Oops, seems that you have an error with your email format.</font></br>';
$email = 'invalid';
}
if ( $ingame_name != 'invalid' && $email != 'invalid') {
$location = 'item.php?username=' . $ingame_name . '&email=' . $email;
header("Location: $location");
}
else {
//echo $invalidusername . $invalidemail;
}
}