<?php
class MY_Model extends CI_Model
{
protected $table = '';
protected function switch_db($webinar_id)
{
if (! $this->db->db_select('webinar_' . $webinar_id)) die('Вебинар не существует');
}
public function get($webinar_id, $id = 0, $filter = [])
{
$this->switch_db($webinar_id);
$where_clause = ($id == 0 ? [] : ['id' => $id]) + $filter;
$query = $this->db->get_where($this->table, $where_clause);
return ($id == 0 ? $query->result_array() : $query->row_array());
}
public function get_only_status($webinar_id, $id = 0, $filter = [])
{
$this->switch_db($webinar_id);
$where_clause = ($id == 0 ? [] : ['id' => $id]) + $filter;
$query = $this->db->select('id, status')->get_where($this->table, $where_clause);
return ($id == 0 ? $query->result_array() : $query->row_array());
}
public function add($webinar_id, $data)
{
$this->switch_db($webinar_id);
$data['status'] = 0;
$data['date'] = date('Y-m-d H:i:s');
$this->db->insert($this->table, $data);
}
public function delete($webinar_id, $id)
{
$this->switch_db($webinar_id);
$this->db->delete($this->table, ['id' => $id]);
}
public function edit($webinar_id, $id, $data)
{
$this->switch_db($webinar_id);
$data['date'] = date('Y-m-d H:i:s');
$this->db->update($this->table, $data, ['id' => $id]);
}
public function update_status($webinar_id, $data)
{
$this->switch_db($webinar_id);
if ($data['status'] == 1)
{
$tool = $this->get($webinar_id, $data['tool_id']);
if (! empty($tool)) foreach (['banners','timers','polls','randoms','subscriptions'] as $table)
$this->db->update($table, ['status' => 0], ['block_id' => $tool['block_id'],'status' => 1]);
}
$this->db->update($this->table, ['status' => $data['status']], ['id' => $data['tool_id']]);
}
}