westdc-zf1/application/models/Heihe.php

309 lines
5.3 KiB
PHP
Raw Normal View History

<?php
/**
* Heihe 黑河项目通用类
*/
class Heihe
{
private $db;//传入PDO对象
//使用到的公共变量
public $tbl_heiheproject = "heiheproject" ; //黑河项目表
public $tbl_heiheuser = "heiheuser"; //黑河用户表
public $project_status; //汇交项目计划状态
function __construct($db)
{
$this->db = $db;
$this->project_status = array(
0 => "计划未提交",
1 => "计划未审核",
2 => "跟踪专家审核",
3 => "跟踪专家通过",
4 => "数据委员会通过"
);
}
//获取汇交状态
public function getStatus($code){
$status = $this->project_status;
if(isset($status[$code]))
{
return $status[$code];
}
}
//改变状态
public function ChangeStatus($p,$s){
$sql = "UPDATE ".$this->tbl_heiheproject." SET status=? WHERE id=?";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($s,$p));
return $rs;
}
2013-01-05 09:20:36 +00:00
//专家激活
public function expertActive($pid,$code,$email)
2013-01-05 09:20:36 +00:00
{
if(empty($pid))
{
return 1;
}
if(!is_numeric($pid))
{
return 1;
}
if(!preg_match("/[A-Za-z0-9]/",$code))
{
return 1;
}
if(strlen($code) != 12)
{
return 1;
}
$validation = $this->makeValidation($email);
if($code != $validation)
{
return 5;
}
$experts = $this->expertRead($pid);
if(!is_array($experts))
{
return 10;
}
$matched = false;
$match_expert = "";
foreach($experts as $k=>$v)
{
if($v['validation'] == $code)
2013-01-05 09:20:36 +00:00
{
$matched = true;
$match_expert = $v;
unset($experts[$k]);
break;
2013-01-05 09:20:36 +00:00
}
}
if($matched != true)
{
return 15;
}
if(isset($match_expert['activated']))
{
if(strtotime($match_expert['activated']) < time())
2013-01-05 09:20:36 +00:00
{
return 20;
2013-01-05 09:20:36 +00:00
}
}
$match_expert['activated'] = date("Y-m-d H:i:s",time());
$experts[] = $match_expert;
$experts = json_encode($experts);
$this->expertUpdate($pid,$experts);
return 200;
}
public function expertActiveError($level){
$errors = array(
1 => "参数错误",
5 => "您的验证码不正确",
10 => "此项目尚未邀请任何专家",
15 => "您没有被邀请,请联系数据中心进行核实",
20 => "您的邀请已经激活",
50 => "服务器错误,请刷新页面重试",
200 => "激活成功",
);
if(isset($errors[$level]))
return $errors[$level];
else
return "发生意外错误,请重试";
}
//邀请一个专家
/*
$data = array(
"name"=>'', //专家姓名
"email"=>'', //专家email
"created"=>'', //邀请时间
"validation"=>'', //验证码
"activated"=>'' //激活时间
)
*/
public function expertAdd($pid,$data){
if(!is_array($data))
{
return 0;
}
$experts = $this->expertRead($pid);
if(!is_array($experts))
{
$experts = array($data);
}else{
foreach($experts as $k=>$v)
2013-01-05 09:20:36 +00:00
{
if($v['email'] == $data['email'])
2013-01-05 09:20:36 +00:00
{
return -1;
2013-01-05 09:20:36 +00:00
}
}
$experts[] = $data;
}
$experts = json_encode($experts,JSON_NUMERIC_CHECK);
return $this->expertUpdate($pid,$experts);
}
//移除一个专家
public function expertRemove($pid,$email){
if(empty($pid) || empty($email))
{
return 0;
}
$experts = $this->expertRead($pid);
if(!is_array($experts))
{
return -1;
}else{
foreach($experts as $k=>$v)
{
if(isset($v['email']) && $v['email'] == $email)
2013-01-05 09:20:36 +00:00
{
unset($experts[$k]);
2013-01-05 09:20:36 +00:00
}
}
}
$experts = json_encode($experts);
return $this->expertUpdate($pid,$experts);
2013-01-05 09:20:36 +00:00
}
//更新专家
public function expertUpdate($pid,$obj){
2013-01-05 09:20:36 +00:00
$sql = "UPDATE ".$this->tbl_heiheproject." SET expert='$obj' WHERE id=$pid";
$rs = $this->db->exec($sql);
return $rs;
}
//读取已有的专家
public function expertRead($pid){
if(!is_numeric($pid))
{
return NULL;
}
$sql = "SELECT expert FROM ".$this->tbl_heiheproject." WHERE id=$pid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
$expert = $row['expert'];
if(empty($expert))
{
return NULL;
}
$expert = json_decode($expert,true);
2013-01-05 09:20:36 +00:00
if(count($expert)<1)
{
return 0;
}else{
return $expert;
}
}
//邀请码
public function makeValidation($email){
return substr(md5($email),5,12);
2013-01-05 09:20:36 +00:00
}
//生成邀请连接
public function makeInviteLink($pid,$code)
{
$http_base = "http://".$_SERVER ['HTTP_HOST'];
$url = $http_base."/heihe/projects/invite/".$code.'/pid/'.$pid;
return $url;
}//生成邀请连接
2013-01-05 09:20:36 +00:00
//从pgsql读取数组并拆分为php数组
public function getArray($str){
if(strlen($str)>3)
{
return explode(",",substr($str,1,-1));
}else{
return NULL;
}
}
//将php数组组装成pgsql中的数组
public function mkArray($array){
if(!is_array($array))
{
return "{".$array."}";
}
if(count($array)==1)
{
$key = max(array_keys($array));
return "{".$array[$key]."}";
}
if(count($array)>1)
{
return "{".join(",",$array)."}";
}
}
//从数组中获得键名
public function getKey($array,$needle)
{
foreach($array as $k=>$v)
{
if($v == $needle)
{
return $k;
}
}
}
}