267 lines
8.7 KiB
PHP
267 lines
8.7 KiB
PHP
|
|
<?
|
||
|
|
namespace common;
|
||
|
|
|
||
|
|
use blog\News;
|
||
|
|
use core\db\structure\Column as C;
|
||
|
|
use core\db\structure\eColumnType;
|
||
|
|
use ms\ms\structure\msTo;
|
||
|
|
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\structure\commentLang as Vars;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class Comment extends msModuleTable {
|
||
|
|
static $self;
|
||
|
|
static $table_name = 'comment';
|
||
|
|
static $controller = 'common.comment';
|
||
|
|
|
||
|
|
static $ID = 'id';
|
||
|
|
static $TEXT = 'text';
|
||
|
|
static $PROFILE = 'profile';
|
||
|
|
static $PARENT = 'parent';
|
||
|
|
static $PORTAL = 'portal';
|
||
|
|
static $TO = 'to';
|
||
|
|
static $OID = 'oid';
|
||
|
|
static $DEEP = 'deep';
|
||
|
|
static $ARCHIVED = 'archived';
|
||
|
|
static $ACCOUNT = 'account';
|
||
|
|
static $CREATED = 'created';
|
||
|
|
static $UPDATED = 'updated';
|
||
|
|
|
||
|
|
|
||
|
|
static function getTitle(){return V::get(Vars::$MODULE_NAME);}
|
||
|
|
static function getSystemLangValues():array{ return Vars::getArray(); }
|
||
|
|
public function _update(\Where $w, array $values) {
|
||
|
|
$values[self::$UPDATED] = time();
|
||
|
|
return parent::_update($w, $values);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
static function format($v){
|
||
|
|
$timestamp = $v[self::$CREATED];
|
||
|
|
return [
|
||
|
|
'id' => $v[self::$ID],
|
||
|
|
'text' => trim((string)$v[self::$TEXT]),
|
||
|
|
'profile' => \Profile::formatPublic($v),
|
||
|
|
'date' => date('Y-m-d',$timestamp),
|
||
|
|
'time' => date('H:i:s',$timestamp),
|
||
|
|
'timestamp' => $timestamp,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
static function addByProfile($to=0,$oid=0,$parent_id=0,$text=''){
|
||
|
|
$res = ['e'=>0,'m'=>''];
|
||
|
|
if(IF_PROFILE) {
|
||
|
|
$to = intval($to);
|
||
|
|
$oid = intval($oid);
|
||
|
|
$parent_id = intval($parent_id);
|
||
|
|
$text = trim((string)$text);
|
||
|
|
$isGood = false;
|
||
|
|
switch ($to) {
|
||
|
|
case msTo::NEWS:
|
||
|
|
$C = News::getByID($oid);
|
||
|
|
if($C[News::$ACCOUNT]==\Site::$owner_id){
|
||
|
|
$isGood = true;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
$isGood = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
if ($text == '') {
|
||
|
|
$res = ['e' => 1, 'm' => 'Введите комментарий'];
|
||
|
|
}
|
||
|
|
if($isGood and $res['e']!=1){
|
||
|
|
$deep = 0;
|
||
|
|
if($parent_id) {
|
||
|
|
$parent = Comment::getByID($parent_id);
|
||
|
|
if($parent[Comment::$ACCOUNT]!=\Site::$owner_id){
|
||
|
|
$parent_id = 0;
|
||
|
|
} else {
|
||
|
|
$deep = $parent[Comment::$DEEP] + 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$r = Comment::insert([
|
||
|
|
Comment::$PARENT => $parent_id,
|
||
|
|
Comment::$TO => $to,
|
||
|
|
Comment::$OID => $oid,
|
||
|
|
Comment::$TEXT => $text,
|
||
|
|
Comment::$DEEP => $deep,
|
||
|
|
]);
|
||
|
|
$res['id'] = $r->id;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$res = ['e' => 1, 'm' => 'Для оставления комментария, пожалуйста, авторизуйтесь'];
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public static function get($to,$oid,$options = []){
|
||
|
|
$res = [];
|
||
|
|
$to = intval($to);
|
||
|
|
$oid = intval($oid);
|
||
|
|
if($to and $oid) {
|
||
|
|
$q = [
|
||
|
|
\Query::LEFT_JOINS => [
|
||
|
|
[\Profile::$table_name,\Profile::$ID,self::$PROFILE],
|
||
|
|
[Image::$table_name,Image::$ID,\Profile::$IMAGE],
|
||
|
|
],
|
||
|
|
\Query::WHERE => new \Where(\Where::_and([
|
||
|
|
\Where::_operator(self::$TO,'=',$to),
|
||
|
|
\Where::_operator(self::$OID,'=',$oid),
|
||
|
|
\Where::_operator(self::$ARCHIVED,'=',0),
|
||
|
|
])),
|
||
|
|
\Query::SORT=>[self::$ID=>'ASC'],
|
||
|
|
];
|
||
|
|
$count = intval($options['count']);
|
||
|
|
$page = intval($options['page']);
|
||
|
|
$offset = $count * $page;
|
||
|
|
if($count){
|
||
|
|
$q[\Query::COUNT] = $count;
|
||
|
|
$q[\Query::OFFSET] = $offset;
|
||
|
|
}
|
||
|
|
|
||
|
|
$r = self::select($q);
|
||
|
|
|
||
|
|
while($l = \DB::fetch($r)){
|
||
|
|
$parent_id = $l[self::$PARENT];
|
||
|
|
$id = $l[self::$ID];
|
||
|
|
$res['base'][$id] = $l;
|
||
|
|
$res['format'][$id] = self::format($l);
|
||
|
|
$res[$parent_id][] = $id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function installUniques() {
|
||
|
|
\DB::addIndexManyColumns(self::$table_name,[self::$TO,self::$OID]);
|
||
|
|
\DB::addIndex(self::$table_name,self::$PROFILE);
|
||
|
|
\DB::addIndex(self::$table_name,self::$ARCHIVED);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static function getForm($values = [], $options = []) :array {
|
||
|
|
return [
|
||
|
|
self::getCommonFormInputs(),
|
||
|
|
[
|
||
|
|
Form::VIEW=>Form::ROW,
|
||
|
|
Form::COLS => [
|
||
|
|
[
|
||
|
|
Form::VIEW =>Form::INPUT,
|
||
|
|
Form::COL_CLASSES =>'col-12',
|
||
|
|
Form::KEY_VALUE =>self::$NAME,
|
||
|
|
Form::TYPE =>eInputType::TEXT,
|
||
|
|
Form::INPUT_OPT => [
|
||
|
|
Input::PLACEHOLDER => $c = V::get(Vars::$NAME),
|
||
|
|
Input::LABEL => $c,
|
||
|
|
],
|
||
|
|
],
|
||
|
|
]
|
||
|
|
],
|
||
|
|
self::getCommonFormSubmits($values),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
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::$TEXT,
|
||
|
|
C::TYPE => eColumnType::TEXT,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PROFILE,
|
||
|
|
C::TYPE => eColumnType::VARCHAR,
|
||
|
|
C::DEFAULT => intval(PID),
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PARENT,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => 0,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$PORTAL,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => intval(\Site::$portal_id)
|
||
|
|
]),
|
||
|
|
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$TO,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => 0,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$OID,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => 0,
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$DEEP,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => 0
|
||
|
|
]),
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$ARCHIVED,
|
||
|
|
C::TYPE => eColumnType::TINYINT,
|
||
|
|
C::DEFAULT => 0,
|
||
|
|
]),
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
new C([
|
||
|
|
C::VAR_NAME => &self::$ACCOUNT,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::LENGTH => 11,
|
||
|
|
C::TH => V::get(Vars::$ACCOUNT),
|
||
|
|
C::DEFAULT => intval(\Site::$owner_id),
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
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 C([
|
||
|
|
C::VAR_NAME => &self::$UPDATED,
|
||
|
|
C::TYPE => eColumnType::INT,
|
||
|
|
C::DEFAULT => time(),
|
||
|
|
C::TH => V::get(Vars::$UPDATED),
|
||
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$UPDATED]);},
|
||
|
|
c::HIDDEN=>true,
|
||
|
|
]),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
new Comment();
|
||
|
|
|
||
|
|
|
||
|
|
///
|
||
|
|
|