257 lines
7.0 KiB
PHP
257 lines
7.0 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Simpla CMS
|
||
*
|
||
* @copyright 2011 Denis Pikusov
|
||
* @link http://simplacms.ru
|
||
* @author Denis Pikusov
|
||
*
|
||
* @editor 2014 Vitaly Raevsky
|
||
* @link http://bwdesign.ru
|
||
* @email vitaly.raevsky@gmail.com
|
||
*
|
||
*/
|
||
|
||
require_once('Simpla.php');
|
||
|
||
class Actions extends Simpla
|
||
{
|
||
|
||
/*
|
||
*
|
||
* Функция возвращает пост по его id или url
|
||
* (в зависимости от типа аргумента, int - id, string - url)
|
||
* @param $id id или url поста
|
||
*
|
||
*/
|
||
public function get_post($id)
|
||
{
|
||
if(is_int($id))
|
||
$where = $this->db->placehold(' WHERE b.id=? ', intval($id));
|
||
else
|
||
$where = $this->db->placehold(' WHERE b.url=? ', $id);
|
||
|
||
$query = $this->db->placehold("SELECT b.id, b.url, b.name, b.annotation, b.text, b.meta_title,
|
||
b.meta_keywords, b.meta_description, b.visible, b.date, b.image
|
||
FROM __actions b $where LIMIT 1");
|
||
if($this->db->query($query))
|
||
return $this->db->result();
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/*
|
||
*
|
||
* Функция возвращает массив постов, удовлетворяющих фильтру
|
||
* @param $filter
|
||
*
|
||
*/
|
||
public function get_posts($filter = array())
|
||
{
|
||
// По умолчанию
|
||
$limit = 1000;
|
||
$page = 1;
|
||
$post_id_filter = '';
|
||
$visible_filter = '';
|
||
$keyword_filter = '';
|
||
$posts = array();
|
||
|
||
if(isset($filter['limit']))
|
||
$limit = max(1, intval($filter['limit']));
|
||
|
||
if(isset($filter['page']))
|
||
$page = max(1, intval($filter['page']));
|
||
|
||
if(!empty($filter['id']))
|
||
$post_id_filter = $this->db->placehold('AND b.id in(?@)', (array)$filter['id']);
|
||
|
||
if(isset($filter['visible']))
|
||
$visible_filter = $this->db->placehold('AND b.visible = ?', intval($filter['visible']));
|
||
|
||
if(isset($filter['keyword']))
|
||
{
|
||
$keywords = explode(' ', $filter['keyword']);
|
||
foreach($keywords as $keyword)
|
||
$keyword_filter .= $this->db->placehold('AND (b.name LIKE "%'.mysql_real_escape_string(trim($keyword)).'%" OR b.meta_keywords LIKE "%'.mysql_real_escape_string(trim($keyword)).'%") ');
|
||
}
|
||
|
||
$sql_limit = $this->db->placehold(' LIMIT ?, ? ', ($page-1)*$limit, $limit);
|
||
|
||
$query = $this->db->placehold("SELECT b.id, b.url, b.name, b.annotation, b.text,
|
||
b.meta_title, b.meta_keywords, b.meta_description, b.visible,
|
||
b.date, b.image
|
||
FROM __actions b WHERE 1 $post_id_filter $visible_filter $keyword_filter
|
||
ORDER BY date DESC, id DESC $sql_limit");
|
||
|
||
$this->db->query($query);
|
||
return $this->db->results();
|
||
}
|
||
|
||
|
||
/*
|
||
*
|
||
* Функция вычисляет количество постов, удовлетворяющих фильтру
|
||
* @param $filter
|
||
*
|
||
*/
|
||
public function count_posts($filter = array())
|
||
{
|
||
$post_id_filter = '';
|
||
$visible_filter = '';
|
||
$keyword_filter = '';
|
||
|
||
if(!empty($filter['id']))
|
||
$post_id_filter = $this->db->placehold('AND b.id in(?@)', (array)$filter['id']);
|
||
|
||
if(isset($filter['visible']))
|
||
$visible_filter = $this->db->placehold('AND b.visible = ?', intval($filter['visible']));
|
||
|
||
if(isset($filter['keyword']))
|
||
{
|
||
$keywords = explode(' ', $filter['keyword']);
|
||
foreach($keywords as $keyword)
|
||
$keyword_filter .= $this->db->placehold('AND (b.name LIKE "%'.mysql_real_escape_string(trim($keyword)).'%" OR b.meta_keywords LIKE "%'.mysql_real_escape_string(trim($keyword)).'%") ');
|
||
}
|
||
|
||
$query = "SELECT COUNT(distinct b.id) as count
|
||
FROM __actions b WHERE 1 $post_id_filter $visible_filter $keyword_filter";
|
||
|
||
if($this->db->query($query))
|
||
return $this->db->result('count');
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/*
|
||
*
|
||
* Создание поста
|
||
* @param $post
|
||
*
|
||
*/
|
||
public function add_post($post)
|
||
{
|
||
if(isset($post->date))
|
||
{
|
||
$date = $post->date;
|
||
unset($post->date);
|
||
//$date_query = $this->db->placehold(', date=STR_TO_DATE(?, ?)', $date, $this->settings->date_format);
|
||
$date_query = ', date=NOW()';
|
||
}else{
|
||
$date_query = '';
|
||
}
|
||
$query = $this->db->placehold("INSERT INTO __actions SET ?% $date_query", $post);
|
||
|
||
if(!$this->db->query($query))
|
||
return false;
|
||
else
|
||
return $this->db->insert_id();
|
||
}
|
||
|
||
|
||
/*
|
||
*
|
||
* Обновить пост(ы)
|
||
* @param $post
|
||
*
|
||
*/
|
||
public function update_post($id, $post)
|
||
{
|
||
$query = $this->db->placehold("UPDATE __actions SET ?% WHERE id in(?@) LIMIT ?", $post, (array)$id, count((array)$id));
|
||
$this->db->query($query);
|
||
return $id;
|
||
}
|
||
|
||
|
||
/*
|
||
*
|
||
* Удалить пост
|
||
* @param $id
|
||
*
|
||
*/
|
||
public function delete_post($id)
|
||
{
|
||
if(!empty($id))
|
||
{
|
||
$query = $this->db->placehold("DELETE FROM __actions WHERE id=? LIMIT 1", intval($id));
|
||
if($this->db->query($query))
|
||
{
|
||
$query = $this->db->placehold("DELETE FROM __comments WHERE type='actions' AND object_id=? LIMIT 1", intval($id));
|
||
if($this->db->query($query))
|
||
return true;
|
||
}
|
||
|
||
}
|
||
return false;
|
||
}
|
||
public function delete_image($id)
|
||
{
|
||
$query = $this->db->placehold("SELECT image FROM __actions WHERE id=?", intval($id));
|
||
$this->db->query($query);
|
||
$filename = $this->db->result('image');
|
||
if(!empty($filename))
|
||
{
|
||
$query = $this->db->placehold("UPDATE __actions SET image=NULL WHERE id=?", $id);
|
||
$this->db->query($query);
|
||
$query = $this->db->placehold("SELECT count(*) as count FROM __actions WHERE image=? LIMIT 1", $filename);
|
||
$this->db->query($query);
|
||
$count = $this->db->result('count');
|
||
if($count == 0)
|
||
{
|
||
@unlink($this->config->root_dir.$this->config->original_images_dir.$filename);
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
*
|
||
* Следующий пост
|
||
* @param $post
|
||
*
|
||
*/
|
||
public function get_next_post($id)
|
||
{
|
||
$this->db->query("SELECT date FROM __actions WHERE id=? LIMIT 1", $id);
|
||
$date = $this->db->result('date');
|
||
|
||
$this->db->query("(SELECT id FROM __actions WHERE date=? AND id>? AND visible ORDER BY id limit 1)
|
||
UNION
|
||
(SELECT id FROM __actions WHERE date>? AND visible ORDER BY date, id limit 1)",
|
||
$date, $id, $date);
|
||
$next_id = $this->db->result('id');
|
||
if($next_id){
|
||
$post = $this->get_post(intval($next_id));
|
||
$post->image = Img::get('files/originals/' . $post->image, array('width' => 200, 'height' => 200));
|
||
return $post;
|
||
}
|
||
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/*
|
||
*
|
||
* Предыдущий пост
|
||
* @param $post
|
||
*
|
||
*/
|
||
public function get_prev_post($id)
|
||
{
|
||
$this->db->query("SELECT date FROM __actions WHERE id=? LIMIT 1", $id);
|
||
$date = $this->db->result('date');
|
||
|
||
$this->db->query("(SELECT id FROM __actions WHERE date=? AND id<? AND visible ORDER BY id DESC limit 1)
|
||
UNION
|
||
(SELECT id FROM __actions WHERE date<? AND visible ORDER BY date DESC, id DESC limit 1)",
|
||
$date, $id, $date);
|
||
$prev_id = $this->db->result('id');
|
||
if($prev_id){
|
||
$post = $this->get_post(intval($prev_id));
|
||
$post->image = Img::get('files/originals/' . $post->image, array('width' => 200, 'height' => 200));
|
||
return $post;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
}
|