<?php
/*
* Class for functions that are used in different parts of the module. E.g: functions for API calls
*/
class Commercesciences_Base_Helper_Data extends Mage_Core_Helper_Abstract{
const STEP_ZERO = 0;
const STEP_ONE = 1;
const STEP_TWO = 2;
const CONFIG_EMAIL = 'commercesciences/required_param/email';
const DEFAULT_MYSQL_TIMEOUT = 10;
protected $_step = null;
protected $_firstStore = null;
/**
* handles the errors
*/
public function handleError($error){
// TODO Ron Gross 2/1/2013: Inline this function
throw new Exception($error);
}
/**
* return the current step of the system (can be 0,1 or 2)
*
* @return int
*/
public function getStep(){
// TODO Ron Gross 2/1/2013 - reverse this if
if(!$this->_step){
// Note: "1" is the ID of the singleton Model "config"
$csConfig = Mage::getModel("commercesciences_base/config")->load("1");
if(!$csConfig || !$csConfig->getId()){
$csConfig = Mage::getModel("commercesciences_base/config");
// TODO Sveta 2/1/2013 - please document:
// 1. How can this situation happen?
// 2. Why is $csConfig saved here?
$csConfig->save();
}
if(!$csConfig->getUserId()){
// No user ID --> Haven't registered yet, step 0.
$this->_step = Commercesciences_Base_Helper_Data::STEP_ZERO;
return $this->_step;
}
//we have userId, means that we are on step 1 or 2
$aStateArr = $this->getActiveState();
if($aStateArr['error'] != false){
$this->handleError($aStateArr['error']);
// TODO Ron Gross 2/1/2013 - remove this 'return' (above statement just throws an exception)
return;
}
if($aStateArr['data'] == 'NotConfigured'){
$this->_step = Commercesciences_Base_Helper_Data::STEP_ONE;
}elseif($aStateArr['data'] == 'Hidden' || $aStateArr['data'] == 'Visible'){
$this->_step = Commercesciences_Base_Helper_Data::STEP_TWO;
}else{
$error = $this->__("Error ocurred. Your updates weren't saved. Please contact ComemrceScience for support (error id: 005)");
throw new Exception($error);
return;
}
}
return $this->_step;
}
/**
* converts the first layer of stdObject to array
* @param stdObject $stdObj
* @return array
*/
public function stdObject2Array($stdObj) {
if (is_object($stdObj)){
$stdObj = get_object_vars($stdObj);
}
return $stdObj;
}
/**
* register the email + domain on commercesciences
*
* @return array - format [error] => ''
*/
public function register($email, $storeUrl){
// TODO Ron Gross 2/1/2013 - refactor into a method
$RESTClient = new Zend_Rest_Client("http://api.commercesciences.com");
$httpClient = $RESTClient->getHttpClient();
$httpClient->setConfig(array(
"timeout" => 30
));
try {
$timeBeforeApiCall = time();
//get the extension version
$extVersion = Mage::getResourceSingleton('core/resource')->getDbVersion('cs_setup');
$response = $RESTClient->restPost("/magento/registerPost", array('email' => $email, 'storeURL'=>$storeUrl, 'platformVersion' => Mage::getVersion(), 'extensionVersion' => $extVersion));
$responseJson = $response->getBody();
$timeAfterApiCall = time();
$parsedResponseArr = $this->stdObject2Array(json_decode($responseJson));
if(!isset($parsedResponseArr['good'])){
//timeout occured
return array('error' => $this->__("The CommerceSciences server is currently busy, your updates weren't saved. Please try again later. (error id: 003)"));
}
if($parsedResponseArr['good'] == false){
if(isset($parsedResponseArr['fieldErrors']) && $parsedResponseArr['fieldErrors']){
$fieldErrorsArr = $this->stdObject2Array($parsedResponseArr['fieldErrors']);
$errorMsg = '';
foreach($fieldErrorsArr as $field => $fError){
$errorMsg .= "<br />";
$errorMsg .= $this->__($field).": ".$this->__($fError);
}
return array('error' => $errorMsg);
}elseif(isset($parsedResponseArr['globalError']) && $parsedResponseArr['globalError']){
return array('error' => $this->__($parsedResponseArr['globalError']));
}
}
$parsedResponse = $this->stdObject2Array($parsedResponseArr['data']);
if(!isset($parsedResponse['securityToken']) || !$parsedResponse['securityToken']
|| !isset($parsedResponse['userID']) || !$parsedResponse['userID']
|| !isset($parsedResponse['tag']) || !$parsedResponse['tag']){
return $this->__("Error ocurred. Your updates weren't saved. Please contact ComemrceScience for support (error id: 001)");
}
if($timeAfterApiCall-$timeBeforeApiCall > self::DEFAULT_MYSQL_TIMEOUT){
//pobably mysql serer has gone away, save data in the session
$adminSession = Mage::getModel('customer/session');
$adminSession->setCsUserId($parsedResponse['userID']);
$adminSession->setCsSecurityToken($parsedResponse['securityToken']);
$adminSession->setCsTag($parsedResponse['tag']);
}else{
$csConfig = Mage::getModel("commercesciences_base/config")->load("1");
if(!$csConfig){
$csConfig = Mage::getModel("commercesciences_base/config");
}
$csConfig->setSecurityToken($parsedResponse['securityToken'])
->setUserId($parsedResponse['userID'])
->setTag($parsedResponse['tag'])
->save();
}
$message = '';
if(isset($parsedResponse['message']) && $parsedResponse['message']){
$message = $parsedResponse['message'];
}
return array('error' => false, 'message' => $message);
}catch(Exception $e){
//timeout or other unhandled exception was thrown
return array('error' => $this->__($e->getMessage()));
}
}
/**
* Change the show/hide state of the bar
*
* @param int $changeTo
* @return array
*/
protected function changeActiveState($changeTo=1){
$csConfig = Mage::getModel("commercesciences_base/config")->load("1");
if(!$csConfig || !$csConfig->getUserId() || !$csConfig->getSecurityToken()){
//we must be already at least on step 1, so the DB record has to exist
return array('error' => $this->__("Error ocurred. Your updates weren't saved. Please contact ComemrceScience for support (error id: 002)"));
}
// TODO Ron Gross 2/1/2013 - refactor into a method
$RESTClient = new Zend_Rest_Client("http://api.commercesciences.com");
$httpClient = $RESTClient->getHttpClient();
$httpClient->setConfig(array(
"timeout" => 30
));
try{
if($changeTo){
$response = $RESTClient->restPost("/magento/showBarPost", array('userID' => $csConfig->getUserId(), 'securityToken'=>$csConfig->getSecurityToken()));
}else{
$response = $R
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一个浮动的页脚(工具栏),放置在您的网页的底部,可以提高您的Magento商店。 有您的购物者,您的商店的关键资产在同一个地方,永远在线的,包括-在现场搜索,结帐,即时聊天,社交按钮,优惠券和更多(支持所有现有的技术供应商的搜索,聊天,等等)。 给个人的经验。自动触发每个购物者的正确的经验。针对特定的消费者提供实时的优惠券,免费送货,引发个人发售,市场消息或实时聊天支持。 用法很简单和天才:2分钟设置,没有必要的,直观的配置面板和仪表板。轻松定制,充分满足您的店铺设计及主题。
资源推荐
资源详情
资源评论
收起资源包目录
Personal_Bar-1.0.16.rar (23个子文件)
Personal_Bar-1.0.16
adminhtml
default
default
commercesciences
css
skin.css 6KB
images
intro-img.png 33KB
setup-takes-2-minutes.png 9KB
icons-boxed.png 63KB
CS-Logo-innerpage.png 2KB
layout
commercesciences.xml 233B
Commercesciences
Base
Model
Config.php 385B
Observer.php 6KB
Mysql4
Config.php 358B
Resource
Setup.php 182B
Helper
Data.php 13KB
sql
cs_setup
mysql4-install-0.0.1.php 607B
mysql4-upgrade-1.0.6-1.0.8.php 640B
mysql4-upgrade-0.0.1-1.0.6.php 640B
Block
Adminhtml
Config
Form
Renderer
Campaignid.php 6KB
About.php 2KB
Group
About.php 1KB
Isactive.php 3KB
Isactive.php.orig 4KB
etc
config.xml 5KB
system.xml 4KB
modules
Commercesciences_Base.xml 192B
package.xml 4KB
共 23 条
- 1
资源评论
JonSco
- 粉丝: 94
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功