133 lines
4.1 KiB
PHP
133 lines
4.1 KiB
PHP
|
|
<?
|
||
|
|
namespace common\comment\discuss;
|
||
|
|
|
||
|
|
use common\comment\Discuss;
|
||
|
|
use core\db\structure\Column as C;
|
||
|
|
use core\db\structure\eColumnType;
|
||
|
|
use ui\form\structure\eInputType;
|
||
|
|
use ui\Form;
|
||
|
|
use ui\input\Input;
|
||
|
|
use core\lang\structure\LangVariable as V;
|
||
|
|
use ms\ms\structure\msModuleTable;
|
||
|
|
use common\comment\discuss\vote\structure\commentDiscussVoteLang as Vars;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class Vote extends msModuleTable {
|
||
|
|
static $self;
|
||
|
|
static $table_name = 'commentDiscussVote';
|
||
|
|
static $controller = 'common.discuss.vote';
|
||
|
|
|
||
|
|
static $ID = 'id';
|
||
|
|
static $DISCUSS = 'discuss';
|
||
|
|
static $PROFILE = 'profile';
|
||
|
|
static $CREATED = 'created';
|
||
|
|
|
||
|
|
static function getTitle(){return V::get(Vars::$MODULE_NAME);}
|
||
|
|
static function getSystemLangValues():array{ return Vars::getArray(); }
|
||
|
|
|
||
|
|
public static function installUniques(){
|
||
|
|
\DB::addIndex(self::$table_name,self::$DISCUSS);
|
||
|
|
\DB::addUnique(self::$table_name,self::$table_name.'_unique',[self::$DISCUSS,self::$PROFILE]);
|
||
|
|
}
|
||
|
|
|
||
|
|
static public function toggle($discuss_id,$profile_id = PID){
|
||
|
|
$res = false;
|
||
|
|
$discuss_id = intval($discuss_id);
|
||
|
|
$profile_id = intval($profile_id);
|
||
|
|
if ($discuss_id and $profile_id) {
|
||
|
|
$r = self::get($discuss_id,$profile_id);
|
||
|
|
if($r){
|
||
|
|
$res = false;
|
||
|
|
self::unvote($discuss_id,$profile_id);
|
||
|
|
} else {
|
||
|
|
$res = true;
|
||
|
|
self::vote($discuss_id,$profile_id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
static public function vote($discuss_id,$profile_id = PID){
|
||
|
|
$discuss_id = intval($discuss_id);
|
||
|
|
$profile_id = intval($profile_id);
|
||
|
|
if($discuss_id and $profile_id){
|
||
|
|
self::insert([
|
||
|
|
self::$DISCUSS => $discuss_id,
|
||
|
|
self::$PROFILE => $profile_id,
|
||
|
|
]);
|
||
|
|
Discuss::remathVotes($discuss_id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static public function get($discuss_id,$profile_id = PID){
|
||
|
|
$res = null;
|
||
|
|
$discuss_id = intval($discuss_id);
|
||
|
|
$profile_id = intval($profile_id);
|
||
|
|
$r = self::select([
|
||
|
|
\Query::WHERE => new \Where(\Where::_and([
|
||
|
|
\Where::_operator(self::$DISCUSS,'=',$discuss_id),
|
||
|
|
\Where::_operator(self::$PROFILE,'=',$profile_id),
|
||
|
|
])),
|
||
|
|
\Query::COUNT => 1,
|
||
|
|
]);
|
||
|
|
if(\DB::numRows($r)){
|
||
|
|
$res = \DB::fetch($r);
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
static public function unvote($discuss_id,$profile_id = PID){
|
||
|
|
$discuss_id = intval($discuss_id);
|
||
|
|
$profile_id = intval($profile_id);
|
||
|
|
if($discuss_id and $profile_id){
|
||
|
|
$vote = self::get($discuss_id,$profile_id);
|
||
|
|
if($vote){
|
||
|
|
self::deleteByID($vote[self::$ID]);
|
||
|
|
Discuss::remathVotes($vote[self::$DISCUSS]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static function columnInfo() : array {
|
||
|
|
return [
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME =>&self::$ID,
|
||
|
|
C::TYPE =>eColumnType::INT,
|
||
|
|
C::AUTO_INCREMENT =>true,
|
||
|
|
C::PRIMARY =>true,
|
||
|
|
C::DEFAULT =>0,
|
||
|
|
C::HIDDEN =>true,
|
||
|
|
C::TH =>V::get(Vars::$ID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$DISCUSS,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => intval(PID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PROFILE,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => intval(PID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$CREATED,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::LENGTH => 11,
|
||
|
|
C::TH => V::get(Vars::$CREATED),
|
||
|
|
C::DEFAULT => time(),
|
||
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$CREATED]);},
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
new Vote();
|
||
|
|
|
||
|
|
|
||
|
|
///
|
||
|
|
|