Skip to content
Permalink
Browse files

lesson6 crud mapping

  • Loading branch information...
vaajnur committed Mar 6, 2017
1 parent eacac0f commit 242e73b633c13f8d21c7955ff27cbc4a3aa6d29f
@@ -3,6 +3,7 @@
namespace Controllers\Admin;
use Models\Admin\Articles as Articles1;
use Models\Admin\Categories;
use Core\Controller;
@@ -20,18 +21,181 @@ public function index(){
$this->view->generate('', $data);
}
/**
*
*/
public function wrap_images(&$item, $key){
$item = "<img src='images/$item' alt='' class='preview'>";
}
/**
*
*/
public function delete_images($item){
if(file_exists(PATH_SITE.DS.'images'.DS.$item)){
unlink(PATH_SITE.DS.'images'.DS.$item);
}
}
/**
*
*/
public function categories_select(&$item, $key){
$item = "<option value='{$item['id']}'>{$item['name']}</option>";
}
/**
* @param array $args
*/
function article(array $args = null){
public function article(array $args = null){
$id = (isset($args[0])? $args[0]: "");
$model = new Articles1();
$categories_mod = new Categories();
// edit action
if(!empty($_POST)){
/* base values */
$data = [];
foreach ($_POST as $key => $value) {
global $data;
if(!empty($value) && $key != 'category'){
$data[$key] = $value;
}
}
/* image */
if(!empty($_FILES['image']['name'][0])){
$data["preview_pic"] = $_FILES['image']['name'][0];
$uploaded_files = [];
foreach($_FILES['image']['error'] as $key=> $error){
global $uploaded_files;
if($error == UPLOAD_ERR_OK){
$tmp_name = $_FILES['image']['tmp_name'][$key];
$name = basename($_FILES['image']['name'][$key]);
$res = move_uploaded_file($tmp_name, PATH_SITE.DS.'images'.DS.$name);
if($res){
$uploaded_files[] = $_FILES['image']['name'][$key];
}
}
}
// массив картинок переводим в строку
$data["image"] = implode(',' , $uploaded_files);
}
// категории
$category = [];
if(!empty($_POST['category'])){
foreach ($_POST['category'] as $key => $value) {
global $category;
$category[$key]['article_id'] = $id;
$category[$key]['category_id'] = $value;
}
}
$model->edit($data, $category);
}
$article = $model->getById($id);
$categories = $categories_mod->get_categories();
// строку преобразуем в массив и оборачиваем тегами
if(!empty($article['image'])){
$arr = explode(',' , $article['image']);
array_walk($arr, [$this , 'wrap_images']);
$article['image'] = implode('' ,$arr);
}
// категории преобразуем в селект
if(!empty($categories)){
array_walk($categories, [$this, 'categories_select']);
$cats = "<select name='category[]' multiple='multiple' class='form-control'>";
$cats .= implode('', $categories);
$cats .= "</select>";
}
$data = array(
'article' => $article,
'categories' => $cats,
);
$this->view->generate('', $data);
}
/**
*
*/
public function add(){
$model = new Articles1();
$categories_mod = new Categories();
if(!empty($_POST)){
/* base values */
$data = [];
foreach ($_POST as $key => $value) {
global $data;
if(!empty($value && $key != 'category')){
$data[$key] = $value;
}
}
/* image */
if(!empty($_FILES['image']['name'][0])){
$data["preview_pic"] = $_FILES['image']['name'][0];
$uploaded_files = [];
foreach($_FILES['image']['error'] as $key=> $error){
global $uploaded_files;
if($error == UPLOAD_ERR_OK){
$tmp_name = $_FILES['image']['tmp_name'][$key];
$name = basename($_FILES['image']['name'][$key]);
$res = move_uploaded_file($tmp_name, PATH_SITE.DS.'images'.DS.$name);
if($res){
$uploaded_files[] = $_FILES['image']['name'][$key];
}
}
}
// массив картинок переводим в строку
$data["image"] = implode(',' , $uploaded_files);
}
// категории
$category = [];
if(!empty($_POST['category'])){
foreach ($_POST['category'] as $key => $value) {
global $category;
$category[$key]['category_id'] = $value;
}
}
$res = $model->add($data, $category);
}
$categories = $categories_mod->get_categories();
if(!empty($categories)){
array_walk($categories, [$this, 'categories_select']);
$cats = "<select name='category[]' multiple='multiple' class='form-control'>";
$cats .= implode('', $categories);
$cats .= "</select>";
}
$data = array('categories' => $cats);
$this->view->generate('addArticle', $data);
}
/**
*
*/
public function delete($args = null){
$id = (isset($args[0])? $args[0]: "");
if($id){
$model = new Articles1();
$article = $model->getById($id);
$res = $model->delete($id);
// удаляем картинки
if($res == true){
$arr = explode(',' , $article['image']);
array_walk($arr, [$this, 'delete_images']);
}
}
$this->view->redirect('/admin');
}
}
@@ -24,12 +24,62 @@ public function index(){
*/
public function category(array $args = null){
$model = new categories1();
$id = (isset($args[0])? $args[0]: "");
$categories = new categories1();
$category = $categories->getById($id);
// edit action
if(!empty($_POST)){
/* base values */
$data = [];
foreach ($_POST as $key => $value) {
global $data;
if(!empty($value)){
$data[$key] = $value;
}
}
$res = $model->edit($data);
}
$category = $model->getById($id);
$data = array(
'category' => $category
);
$this->view->generate('', $data);
}
}
/**
*
*/
public function add(array $args = null){
$model = new categories1();
$id = (isset($args[0])? $args[0]: "");
if(!empty($_POST)){
/* base values */
$data = [];
foreach ($_POST as $key => $value) {
global $data;
if(!empty($value)){
$data[$key] = $value;
}
}
$res = $model->add($data);
}
$data = array();
$this->view->generate('addCategory', $data);
}
/**
*
*/
public function delete($args = null){
$id = (isset($args[0])? $args[0]: "");
if($id){
$model = new categories1();
$res = $model->delete($id);
}
$this->view->redirect('/admin/categories');
}
}
@@ -21,14 +21,26 @@ function index()
$this->view->generate('main', $data);
}
/**
*
*/
public function wrap_images(&$item, $key){
$item = "<img src='images/$item' alt='' class='preview'>";
}
/**
* @param array $args
*/
function article(array $args = null){
$id = (isset($args[0])? $args[0]: "");
$model = new Model_Articles();
$model = new Articles1();
$article = $model->getById($id);
// строку преобразуем в массив и оборачиваем тегами
if(!empty($article['image'])){
$arr = explode(',' , $article['image']);
array_walk($arr, [$this , 'wrap_images']);
$article['image'] = implode('' ,$arr);
}
$data = array(
'article' => $article,
'breadcrumb' => 'Главная / Статьи / '.$article['title'],
@@ -2,7 +2,6 @@
function __autoload($class_name)
{
define('APP_PATH', 'application');
echo $class_name;
$path = strtolower(str_replace("\\", DS, $class_name));
if (file_exists(PATH_SITE.DS.APP_PATH.DS.$path . ".php")) {
@@ -44,4 +44,12 @@ function generate($content_view, $data = null)
die("template_view $templ not found!!");
}
/**
* @param string $url
*/
public function redirect($url = '')
{
header("Location: " . $url);
}
}
@@ -13,7 +13,8 @@ class Articles extends Model{
* @param $id
* @return mixed
*/
public function getById($id){
public function getById($id = null){
if($id == null)return;
$sql = 'SELECT * FROM article WHERE id = %s';
$result = DateBase::query($sql, $id);
$row = $result->fetch_assoc();
@@ -24,7 +25,13 @@ public function getById($id){
*/
public function get_articles(){
$sql = 'SELECT * FROM article';
$sql = 'SELECT a.*, GROUP_CONCAT(DISTINCT c.name )as category
FROM article a
LEFT JOIN category_article ca
ON a.id = ca.article_id
LEFT JOIN category c
ON c.id = ca.category_id
GROUP BY a.id';
$result = DateBase::query($sql);
$row2 = array();
@@ -34,6 +41,54 @@ public function get_articles(){
if(empty($row2))return;
return $row2;
}
/**
*
*/
public function edit($data = null, $category = null){
if(!empty($data)){
$sql_articles = 'UPDATE article SET';
$where = 'WHERE id='.$data['id'];
$result = DateBase::build_query($sql_articles, $data, $where);
// return $result;
}
if(!empty($category)){
foreach($category as $cat){
$sql_articles = 'INSERT INTO category_article SET';
$result = DateBase::build_query($sql_articles, $cat);
}
}
}
/**
*
*/
public function add($data = null, $category = null){
if(!empty($data)){
$sql_articles = 'INSERT INTO article SET';
$result = DateBase::build_query($sql_articles, $data);
}
$id = DateBase::insert_id();
if(!empty($category) && isset($id)){
foreach($category as $key=>$cat){
$cat['article_id'] = $id;
$sql_articles = 'INSERT INTO category_article SET';
$result = DateBase::build_query($sql_articles, $cat);
}
}
}
/**
*
*/
public function delete($id = null){
if(!empty($id)){
$sql = 'DELETE FROM article WHERE id = %s';
$result = DateBase::query($sql, $id);
return $result;
}
}
}
?>
Oops, something went wrong.

0 comments on commit 242e73b

Please sign in to comment.
You can’t perform that action at this time.