diff --git a/api/Repairs.php b/api/Repairs.php new file mode 100644 index 0000000..6cfdea1 --- /dev/null +++ b/api/Repairs.php @@ -0,0 +1,256 @@ +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 __repairs 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 __repairs 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 __repairs 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 __repairs 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 __repairs 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 __repairs WHERE id=? LIMIT 1", intval($id)); + if($this->db->query($query)) + { + $query = $this->db->placehold("DELETE FROM __comments WHERE type='repairs' 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 __repairs WHERE id=?", intval($id)); + $this->db->query($query); + $filename = $this->db->result('image'); + if(!empty($filename)) + { + $query = $this->db->placehold("UPDATE __repairs SET image=NULL WHERE id=?", $id); + $this->db->query($query); + $query = $this->db->placehold("SELECT count(*) as count FROM __repairs 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 __repairs WHERE id=? LIMIT 1", $id); + $date = $this->db->result('date'); + + $this->db->query("(SELECT id FROM __repairs WHERE date=? AND id>? AND visible ORDER BY id limit 1) + UNION + (SELECT id FROM __repairs 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 __repairs WHERE id=? LIMIT 1", $id); + $date = $this->db->result('date'); + + $this->db->query("(SELECT id FROM __repairs WHERE date=? AND id AND visible ORDER BY id DESC limit 1) + UNION + (SELECT id FROM __repairs 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; + } +} diff --git a/api/Simpla.php b/api/Simpla.php index d025a6e..01945d2 100644 --- a/api/Simpla.php +++ b/api/Simpla.php @@ -53,6 +53,7 @@ class Simpla 'marka' => 'Marka', 'model' => 'Model', 'services' => 'Services', + 'repairs' => 'Repairs', ); // Созданные объекты diff --git a/repairs.sql b/repairs.sql new file mode 100644 index 0000000..ccbf177 --- /dev/null +++ b/repairs.sql @@ -0,0 +1,41 @@ +create table s_repair_photo +( + id int auto_increment + primary key, + action_id int default 0 not null, + img varchar(100) default '' not null, + position int default 0 not null +) + engine = MyISAM + charset = utf8; +; + + +create table s_repairs +( + id int auto_increment + primary key, + name text not null, + url varchar(255) default '' not null, + meta_title text not null, + meta_keywords text not null, + meta_description text not null, + annotation text not null, + image varchar(255) default '' not null, + text longtext not null, + visible tinyint(1) default 0 not null, + date timestamp default '0000-00-00 00:00:00' not null +) + engine = MyISAM + charset = utf8; + +create index `1gb_VISIBLE_DATE_ID_AUTO` + on s_repairs (visible, date, id); + +create index enabled + on s_repairs (visible); + +create index url + on s_repairs (url); + + diff --git a/simpla/.passwd b/simpla/.passwd index c4affb1..cf73062 100644 --- a/simpla/.passwd +++ b/simpla/.passwd @@ -1,4 +1,4 @@ -admin:$apr1$5asceyrg$9bgPtSc50E9hoCmmIPk3K0:banners,products,categories,brands,features,orders,labels,users,groups,coupons,pages,blog,comments,feedbacks,import,export,backup,stats,design,settings,currency,delivery,payment,managers,license,callbacks,articles_categories,articles,marka,model -alaev:$apr1$cmbuh3df$9VJhmckpYKDz81JPTOyzh1:banners,products,categories,brands,features,orders,labels,users,groups,coupons,pages,blog,comments,feedbacks,import,export,backup,stats,design,settings,currency,delivery,payment,managers,license,callbacks,articles_categories,articles,marka,model -Grib:$apr1$sbaru2xk$OgkV5q93tqQ0aGihz6JgV.:products,categories,brands,features,orders,labels,users,groups,coupons,pages,blog,comments,feedbacks,import,export,backup,stats,design,settings,currency,delivery,payment,managers,license,callbacks,articles,articles_categories,marka,model -Olga:$apr1$c6m9xwli$AN5vPxodjhlkHf1jFUgO0/:products,categories,brands,features,pages,blog,comments,feedbacks,callbacks,articles_categories,articles,marka,model \ No newline at end of file +admin:$apr1$5asceyrg$9bgPtSc50E9hoCmmIPk3K0:banners,products,categories,brands,features,orders,labels,users,groups,coupons,pages,blog,comments,feedbacks,import,export,backup,stats,design,settings,currency,delivery,payment,managers,license,callbacks,articles_categories,articles,marka,model, repairs +alaev:$apr1$cmbuh3df$9VJhmckpYKDz81JPTOyzh1:banners,products,categories,brands,features,orders,labels,users,groups,coupons,pages,blog,comments,feedbacks,import,export,backup,stats,design,settings,currency,delivery,payment,managers,license,callbacks,articles_categories,articles,marka,model, repairs +Grib:$apr1$sbaru2xk$OgkV5q93tqQ0aGihz6JgV.:products,categories,brands,features,orders,labels,users,groups,coupons,pages,blog,comments,feedbacks,import,export,backup,stats,design,settings,currency,delivery,payment,managers,license,callbacks,articles,articles_categories,marka,model, repairs +Olga:$apr1$c6m9xwli$AN5vPxodjhlkHf1jFUgO0/:products,categories,brands,features,pages,blog,comments,feedbacks,callbacks,articles_categories,articles,marka,model, repairs \ No newline at end of file diff --git a/simpla/IndexAdmin.php b/simpla/IndexAdmin.php index c790121..0f94c1f 100644 --- a/simpla/IndexAdmin.php +++ b/simpla/IndexAdmin.php @@ -71,6 +71,8 @@ class IndexAdmin extends Simpla 'ServicesMenuAdmin' => 'pages', 'ServicesAdmin' => 'pages', 'ServiceAdmin' => 'pages', + 'RepairsAdmin' => 'repairs', + 'RepairsPostAdmin' => 'repairs', ); // Конструктор diff --git a/simpla/RepairsAdmin.php b/simpla/RepairsAdmin.php new file mode 100644 index 0000000..4a171ef --- /dev/null +++ b/simpla/RepairsAdmin.php @@ -0,0 +1,72 @@ +request->method('post')) + { + // Действия с выбранными + $ids = $this->request->post('check'); + if(is_array($ids)) + switch($this->request->post('action')) + { + case 'disable': + { + $this->repairs->update_post($ids, array('visible'=>0)); + break; + } + case 'enable': + { + $this->repairs->update_post($ids, array('visible'=>1)); + break; + } + case 'delete': + { + foreach($ids as $id) + $this->repairs->delete_post($id); + break; + } + } + } + + $filter = array(); + $filter['page'] = max(1, $this->request->get('page', 'integer')); + $filter['limit'] = 20; + + // Поиск + $keyword = $this->request->get('keyword', 'string'); + if(!empty($keyword)) + { + $filter['keyword'] = $keyword; + $this->design->assign('keyword', $keyword); + } + + $posts_count = $this->repairs->count_posts($filter); + // Показать все страницы сразу + if($this->request->get('page') == 'all') + $filter['limit'] = $posts_count; + + $posts = $this->repairs->get_posts($filter); + $this->design->assign('posts_count', $posts_count); + + $this->design->assign('pages_count', ceil($posts_count/$filter['limit'])); + $this->design->assign('current_page', $filter['page']); + + $this->design->assign('posts', $posts); + + + + + return $this->design->fetch('repairs.tpl'); + } +} diff --git a/simpla/RepairsPostAdmin.php b/simpla/RepairsPostAdmin.php new file mode 100644 index 0000000..22b67bb --- /dev/null +++ b/simpla/RepairsPostAdmin.php @@ -0,0 +1,91 @@ +request->method('post')) + { + $post->id = $this->request->post('id', 'integer'); + $post->name = $this->request->post('name'); + $post->date = date('Y-m-d', strtotime($this->request->post('date'))); + + $post->visible = $this->request->post('visible', 'boolean'); + + $post->url = $this->request->post('url', 'string'); + $post->meta_title = $this->request->post('meta_title'); + $post->meta_keywords = $this->request->post('meta_keywords'); + $post->meta_description = $this->request->post('meta_description'); + + $post->annotation = $this->request->post('annotation'); + $post->text = $this->request->post('body'); + + // Не допустить одинаковые URL разделов. + if(($a = $this->repairs->get_post($post->url)) && $a->id!=$post->id) + { + $this->design->assign('message_error', 'url_exists'); + } + else + { + if(empty($post->id)) + { + $post->id = $this->repairs->add_post($post); + $post = $this->repairs->get_post($post->id); + $this->design->assign('message_success', 'added'); + } + else + { + $this->repairs->update_post($post->id, $post); + $post = $this->repairs->get_post($post->id); + $this->design->assign('message_success', 'updated'); + } + + // Удаление изображения + if($this->request->post('delete_image')) + { + $this->repairs->delete_image($post->id); + } + // Загрузка изображения + $image = $this->request->files('image'); + if(!empty($image['name']) && in_array(strtolower(pathinfo($image['name'], PATHINFO_EXTENSION)), $this->allowed_image_extentions)) + { + if ($image_name = $this->image->upload_image($image['tmp_name'], $image['name'])) + { + $this->repairs->delete_image($post->id); + $this->repairs->update_post($post->id, array('image'=>$image_name)); + $post->image = $image_name; + } + else + { + $this->design->assign('error', 'error uploading image'); + } + } + + $post = $this->repairs->get_post(intval($post->id)); + + } + } + else + { + $post->id = $this->request->get('id', 'integer'); + $post = $this->repairs->get_post(intval($post->id)); + if(!$post->id) $post->visible = 1; + } + + if(empty($post->date)) + $post->date = date($this->settings->date_format, time()); + + $this->design->assign('post', $post); + + $this->db->query("SELECT * FROM __repairs_photo WHERE repair_id='" . $post->id . "' ORDER BY position, id DESC"); + $photos = $this->db->results(); + foreach($photos as $ph) $ph->img = Img::get('files/post/' . $ph->img, array('width'=>120, 'height'=>120)); + $this->design->assign('repair_photos', $photos); + + + return $this->design->fetch('repairs_post.tpl'); + } +} \ No newline at end of file diff --git a/simpla/design/html/manager.tpl b/simpla/design/html/manager.tpl index 57937fe..79d8bf6 100644 --- a/simpla/design/html/manager.tpl +++ b/simpla/design/html/manager.tpl @@ -116,7 +116,8 @@ $(function() { 'articles_categories' => 'Категории статей', 'marka' => 'Марка авто', - 'model' => 'Модель авто' + 'model' => 'Модель авто', + 'repairs' => 'Автосервис' ]} {foreach $perms as $p=>$name} diff --git a/simpla/design/html/repair_photo.tpl b/simpla/design/html/repair_photo.tpl new file mode 100644 index 0000000..099af1d --- /dev/null +++ b/simpla/design/html/repair_photo.tpl @@ -0,0 +1,46 @@ +{literal} + + + + + +
+
+