248 lines
7.7 KiB
PHP
248 lines
7.7 KiB
PHP
|
|
<?
|
||
|
|
namespace vcard;
|
||
|
|
|
||
|
|
use common\Image;
|
||
|
|
use core\db\structure\Column as C;
|
||
|
|
use core\db\structure\eColumnType;
|
||
|
|
use core\lang\structure\LangVariable as V;
|
||
|
|
use ms\ms\structure\msModuleTable;
|
||
|
|
use themes\Mstome;
|
||
|
|
use themes\mstome\mstome\structure\eMstomeType;
|
||
|
|
use vcard\Buy;
|
||
|
|
use vcard\buy\structure\eVcardBuyAccess;
|
||
|
|
|
||
|
|
|
||
|
|
class Profile extends msModuleTable {
|
||
|
|
static $self;
|
||
|
|
static $table_name = 'vcardProfile';
|
||
|
|
static $controller = 'vcard.profile';
|
||
|
|
|
||
|
|
static $ID = 'id';
|
||
|
|
static $BALANCE = 'balance';
|
||
|
|
static $EXPIRE = 'expire';
|
||
|
|
static $SALE = 'sale';
|
||
|
|
static $SALE_TIME = 'sale_time';
|
||
|
|
static $MAX_PROFILE = 'max_profile';
|
||
|
|
static $VIP = 'vip';
|
||
|
|
|
||
|
|
static $data;
|
||
|
|
static $buy_rules;
|
||
|
|
|
||
|
|
static function format($v){
|
||
|
|
$profile = \Profile::getByID($v[self::$ID]);
|
||
|
|
$img = Image::getByID(intval($profile[\Profile::$IMAGE]));
|
||
|
|
return [
|
||
|
|
'avatar' => Image::imageResize($img[Image::$URL],200,200,100,['crop'=>'center']),
|
||
|
|
'name' => trim((string)\Profile::$data[\Profile::$NAME]),
|
||
|
|
'sname' => trim((string)\Profile::$data[\Profile::$SNAME]),
|
||
|
|
'email' => trim((string)\Profile::$data[\Profile::$LOGIN]),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
static function getMyCardCount(){
|
||
|
|
return self::getCardCount(PID);
|
||
|
|
}
|
||
|
|
static function getCardCount($pid){
|
||
|
|
$res = 0;
|
||
|
|
$pid = intval($pid);
|
||
|
|
if($pid) {
|
||
|
|
$r = Card::select([
|
||
|
|
\Query::SELECT => ['COUNT('.Card::$ID.') as cc'],
|
||
|
|
\Query::WHERE => new \Where(\Where::_operator(Card::$PROFILE,'=',$pid))
|
||
|
|
]);
|
||
|
|
$l = \db::fetch($r);
|
||
|
|
$res = intval($l['cc']);
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
static function getMyBuyRules(){
|
||
|
|
if(!IF_PROFILE) return [];
|
||
|
|
if(self::$buy_rules) return self::$buy_rules;
|
||
|
|
$r = Buy::select([
|
||
|
|
\Query::WHERE => new \Where(\Where::_and([
|
||
|
|
\Where::_operator(Buy::$PROFILE,'=',PID)
|
||
|
|
]))
|
||
|
|
]);
|
||
|
|
while($l = \DB::fetch($r)){
|
||
|
|
self::$buy_rules[] = $l[Buy::$RULE];
|
||
|
|
}
|
||
|
|
return self::$buy_rules;
|
||
|
|
}
|
||
|
|
|
||
|
|
static function isExpire($profile){
|
||
|
|
return ($profile[self::$EXPIRE]<date('Y-m-d'));
|
||
|
|
}
|
||
|
|
|
||
|
|
static function isMeExpire(){
|
||
|
|
$data = self::$data;
|
||
|
|
return ($data[self::$EXPIRE]<date('Y-m-d'));
|
||
|
|
}
|
||
|
|
|
||
|
|
static function isHasGlobalAccess(){
|
||
|
|
if(!IF_PROFILE) return false;
|
||
|
|
$data = self::$data;
|
||
|
|
if($data[self::$VIP]) return true;
|
||
|
|
if(!self::isMeExpire()) return true;
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
static function hasAccessToBlock($block_type){
|
||
|
|
$res = self::isHasGlobalAccess();
|
||
|
|
if($res==null) {
|
||
|
|
$rule = eVcardBuyAccess::getBlockAccessRule($block_type);
|
||
|
|
if($rule==eVcardBuyAccess::NONE) $res = true;
|
||
|
|
else {
|
||
|
|
$rules = self::getMyBuyRules();
|
||
|
|
$res = in_array($rule, $rules);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
static function hasAccessBackgroundClass($class){
|
||
|
|
$res = self::isHasGlobalAccess();
|
||
|
|
if($res==null) {
|
||
|
|
$rule = eVcardBuyAccess::getBackgroundAccessRule($class);
|
||
|
|
if($rule==eVcardBuyAccess::NONE) $res = true;
|
||
|
|
else {
|
||
|
|
$res = Profile::hasAccessToProduct(eVcardBuyAccess::EXPIRE) ;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
static function hasAccessToProduct($product_type){
|
||
|
|
switch ($product_type) {
|
||
|
|
case eVcardBuyAccess::ADD_PROFILE_1:
|
||
|
|
case eVcardBuyAccess::ADD_PROFILE_2:
|
||
|
|
case eVcardBuyAccess::ADD_PROFILE_5:
|
||
|
|
case eVcardBuyAccess::EXPIRE_1:
|
||
|
|
case eVcardBuyAccess::EXPIRE_3:
|
||
|
|
case eVcardBuyAccess::EXPIRE_6:
|
||
|
|
case eVcardBuyAccess::EXPIRE_12:
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
$res = self::isHasGlobalAccess();
|
||
|
|
|
||
|
|
if($res==null) {
|
||
|
|
$rule = eVcardBuyAccess::getRule($product_type);
|
||
|
|
switch ($rule){
|
||
|
|
case eVcardBuyAccess::NONE:
|
||
|
|
return false;
|
||
|
|
case eVcardBuyAccess::EXPIRE:
|
||
|
|
return !self::isMeExpire();
|
||
|
|
}
|
||
|
|
$rules = self::getMyBuyRules();
|
||
|
|
$res = in_array($rule, $rules);
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static function mathProfile($pid){
|
||
|
|
$pid = intval($pid);
|
||
|
|
if($pid) {
|
||
|
|
// Считаем баланс
|
||
|
|
$r = Balance::select($q = [
|
||
|
|
\Query::SELECT => ['SUM('.Balance::$VALUE.') as cc'],
|
||
|
|
\Query::WHERE => new \Where(
|
||
|
|
\Where::_operator(Balance::$PROFILE,'=',$pid)
|
||
|
|
)
|
||
|
|
]);
|
||
|
|
$l = \DB::fetch($r);
|
||
|
|
$balance = cfloatval($l['cc']);
|
||
|
|
|
||
|
|
Profile::updateById($pid,[
|
||
|
|
Profile::$BALANCE => $balance,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
static function init(){
|
||
|
|
$PID = \Profile::$data[\Profile::$ID];
|
||
|
|
self::$data = self::getOneEqualByColumn(self::$ID,$PID);
|
||
|
|
if(!self::$data){
|
||
|
|
$profile = [
|
||
|
|
self::$ID => $PID,
|
||
|
|
];
|
||
|
|
switch (Mstome::$TYPE){
|
||
|
|
case eMstomeType::MSTOME:
|
||
|
|
$profile[Profile::$SALE] = 10;
|
||
|
|
$profile[Profile::$SALE_TIME] = time()+24*60*60*1;
|
||
|
|
break;
|
||
|
|
case eMstomeType::CNTCTME:
|
||
|
|
$profile[Profile::$SALE] = 0;
|
||
|
|
$profile[Profile::$SALE_TIME] = 0;
|
||
|
|
$profile[Profile::$BALANCE] = 0;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
self::insert($profile);
|
||
|
|
self::$data = self::getOneEqualByColumn(self::$ID,$PID);
|
||
|
|
|
||
|
|
|
||
|
|
switch (Mstome::$TYPE){
|
||
|
|
case eMstomeType::MSTOME:
|
||
|
|
break;
|
||
|
|
case eMstomeType::CNTCTME:
|
||
|
|
Balance::insert([
|
||
|
|
Balance::$VALUE => 19,
|
||
|
|
Balance::$PROFILE => $PID,
|
||
|
|
Balance::$IS_BONUS => 1,
|
||
|
|
]);
|
||
|
|
self::mathProfile($PID);
|
||
|
|
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function installUniques() {
|
||
|
|
\DB::addIndex(self::$table_name,self::$ID);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static function columnInfo() : array {
|
||
|
|
return [
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME =>&self::$ID,
|
||
|
|
C::TYPE =>eColumnType::INT,
|
||
|
|
C::PRIMARY =>true,
|
||
|
|
C::DEFAULT =>0,
|
||
|
|
C::HIDDEN =>true,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$BALANCE,
|
||
|
|
C::TYPE => eColumnType::DOUBLE,
|
||
|
|
C::LENGTH => '10,2',
|
||
|
|
C::DEFAULT => 0,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$EXPIRE,
|
||
|
|
C::TYPE => eColumnType::DATE,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$SALE,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => 10
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$SALE_TIME,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => time()+24*60*60*1
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$MAX_PROFILE,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => 1,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$VIP,
|
||
|
|
C::TYPE => eColumnType::TINYINT,
|
||
|
|
C::DEFAULT => 0,
|
||
|
|
]),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
new Profile();
|
||
|
|
|