<?php
class modelKopilka extends cmsModel {
public $table_prefix = '';
protected $approved_filter_disabled = false;
protected $approved_filtered = false;
public function setTablePrefix($prefix){
$this->table_prefix = $prefix;
return $this;
}
public function getContentItems($ctype_name, $cat_id=true, $limit=false){
$base = cmsDataBase::getInstance();
$sql_limit = "LIMIT ".$limit;
return $base->getRows("kopilka","ctype='{$ctype_name}' AND is_pub = 1","*");
}
public function getContentItem($target_id,$ctype,$is_binds=true){
$base = cmsDataBase::getInstance();
$kop = $base->getRow("kopilka","target_id='{$target_id}' AND ctype='{$ctype}'","*");
if(!$kop){ return false; }
if($is_binds){
$binds = $this->
filterEqual('kop_id',$id)->
select('SUM(i.bind)','sum')->
select('MAX(i.bind)','max')->
select('MIN(i.bind)','min')->
select('round(AVG(i.bind),-1)','avg')->
select('COUNT(i.id)','count')->
get("kopilka_binds");
if($binds){
$kop['binds'] = end($binds); $kop['binds_total'] = $kop['binds']['sum'];
$kop['binds_count'] = $kop['binds']['count'];
$kop['actual_sum'] = $kop['max_bind'] - $kop['binds']['sum'];
}else{
$kop['binds_total'] = 0;
$kop['binds_count'] = 0;
$kop['actual_sum'] = $kop['max_bind'];
}
}
$kop['kop_options'] = !empty($kop['kop_options']) ? cmsModel
::yamlToArray($kop['kop_options']) : $kop['kop_options'];
return $kop;
}
public function getKop($id,$is_binds=true){
$base = cmsDataBase::getInstance();
$kop = $base->getRow("kopilka","id='{$id}'","*");
if(!$kop){ return false; }
if($is_binds){
$binds = $this->
filterEqual('kop_id',$id)->
select('SUM(i.bind)','sum')->
select('MAX(i.bind)','max')->
select('MIN(i.bind)','min')->
select('round(AVG(i.bind),-1)','avg')->
select('COUNT(i.id)','count')->
get("kopilka_binds");
if($binds){
$kop['binds'] = end($binds); $kop['binds_total'] = $kop['binds']['sum'];
$kop['binds_count'] = $kop['binds']['count'];
$kop['actual_sum'] = $kop['max_bind'] - $kop['binds']['sum'];
}else{
$kop['binds_total'] = 0;
$kop['binds_count'] = 0;
$kop['actual_sum'] = $kop['max_bind'];
}
}
$kop['kop_options'] = !empty($kop['kop_options']) ? cmsModel
::yamlToArray($kop['kop_options']) : $kop['kop_options'];
return $kop;
}
public function countSimBinds($user_id, $kop_id, $target, $target_id=false){
if($target_id){
$this->filterEqual('target_id',$target_id);
}
$count = $this->filterEqual('user_id',$user_id)->filterEqual('kop_id',$kop_id)->filterEqual('target',$target)->getCount('kopilka_sim_binds');
return $count;
}
public function updateContentItem($id, $item){
$table_name = 'kopilka';
$update_item = $item;
$update_item['date_last_modified'] = null;
if(!empty($update_item['pub_days'])){ $update_item['pub_days'] = $update_item['pub_days'] ? $update_item['pub_days'] : 0;
}
$this->update($table_name, $id, $update_item);
return $item;
}
}