<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Galeri extends CI_Controller
{
	public $viewFolder = "";

	public function __construct()
    {
        parent::__construct();
        $this->viewFolder = "galeri_v";

        $this->load->model("galeri_model");
        $this->load->model("image_model");
        $this->load->model("video_model");
        $this->load->model("file_model");
    }

	public function index ()
    {
        $viewData = new stdClass();

        /*Tablodan verilerin getirildiği satır*/

        if ($this->input->post("searchword") == "") {
            $items = $this->galeri_model->get_all(
                array(), "rank ASC"
            );
            $viewData->aramaVar = "hayir";
        }
        else {
            $viewData->aramaVar = "evet";
            $items = $this->galeri_model->get_all_like(
                array(
                    "baslik"    => $this->input->post("searchword")
                ), "rank ASC"
            );
        }
        $viewData->viewFolder = $this->viewFolder;
        $viewData->viewTitle = "Galeri";
        $viewData->items = $items;
        //$viewData->subViewFolder = "list";
        $this->load->view("{$viewData->viewFolder}/index", $viewData);
    }



    /*EKRANA BASILAN YNİ KAYIT FORMU*/
    public function yenikayitFormu()
    {
        $viewData = new stdClass();
        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "add";
        $viewData->viewTitle = "Galeri";
        $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
    }

    /*yeni kayıt veritabanı ve form kontrol işlemleri yapılıyor*/
    public function formKaydet()
    {
        $this->load->library("form_validation");

        //kurallar yazılır
        $this->form_validation->set_rules("title", "Galeri Adı", "required|trim");

        $this->form_validation->set_message(
            array(
                "required" => "<strong>{field}</strong> Alanını Doldurunuz / Seçiniz",
                "is_unique" => "<strong>{field}</strong> ile Kayıt var"
            )
        );
        //form validation çalıştırılı
        $validate = $this->form_validation->run();

        //başarılı ise kayıt işlemi başlar
        if ($validate) {

            $gallery_type = $this->input->post("gallery_type");
            $folder_name = turkceYap($this->input->post("title"));
            $path = "uploads/$this->viewFolder/";

            if ($gallery_type == "image") {
                $path = "$path/images/$folder_name";
            } else if ($gallery_type == "file") {
                $path = "$path/files/$folder_name";
            }



            if ($gallery_type != "video") {
                if (!mkdir($path, 0755)) {
                    $alert = array
                    (
                        "text" => "Galeri oluşturulurken problem meydana geldi (Yetki Hatası)",
                        "type" => "error"
                    );

                    $this->session->set_flashdata("alert", $alert);
                    redirect(base_url("backend/galeri"));
                    die();
                }
            }
                $ekle = $this->galeri_model->add(
                    array
                    (
                        "title" => $this->security->xss_clean($this->input->post("title")),
                        "gallery_type" => $this->security->xss_clean($this->input->post("gallery_type")),
                        "folder_name" => $folder_name,
                        "digeradres" => $this->security->xss_clean($this->input->post("farkliadres")),
                        "pagetitle" => $this->security->xss_clean($this->input->post("pagetitle")),
                        "metadesc" => $this->security->xss_clean($this->input->post("metadesc")),
                        "metakey" => $this->security->xss_clean($this->input->post("metakey")),
                        "addDate" => date("Y-m-d H:i:s"),
                        "addTime" => date("H:i:s"),
                        "isactive" => 1
                    )
                );
                //$soneklenenId = $this->db->insert_id();

                if ($ekle) {
                    $alert = array
                    (
                        "text" => "Kayıt İşlemi Başarı ile Yapıldı ",
                        "type" => "success"
                    );
                } else {
                    $alert = array
                    (
                        "text" => "İşlem Başarısız",
                        "type" => "alert"
                    );
                }
                //İşlemin sonucunu sessiona yazma işlemi...
                $this->session->set_flashdata("alert", $alert);
                redirect(base_url("backend/galeri"));
            } //formda boşluklar yada şarta uymayan bişey varsa ekrana hata basılır
            else {
                $viewData = new stdClass();
                $viewData->viewFolder = $this->viewFolder;
                $viewData->subViewFolder = "add";
                $viewData->form_hatasi = true;
                $viewData->viewTitle = "Galeri";
                $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
            }
    }

    /*kayıt güncelleme formunu ekrana basan fonksiyon*/
    public function guncellemeFormu($id)
    {
        $viewData = new stdClass();
        $items = $this->galeri_model->get
        (
            array
            (
                "id"    => $id
            )
        );
        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "update";
        $viewData->item = $items;
        $viewData->viewTitle = "Galeri";
        $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
    }

    /*güncelleme islemleri*/
    public function guncellemeformKaydet($id,$OldfolderName = "")
    {
        $this->load->library("form_validation");

        //kurallar yazılır
        $this->form_validation->set_rules("title", "Galeri Adı", "required|trim");
        $this->form_validation->set_message(
            array(
                "required" => "<strong>{field}</strong> Alanını Doldurunuz / Seçiniz",
                "is_unique" => "<strong>{field}</strong> ile Kayıt var"
            )
        );
        //form validation çalıştırılı
        $validate = $this->form_validation->run();

        //başarılı ise kayıt işlemi başlar
        if ($validate)
        {

            $gallery_type1 = $this->input->post("gallery_type");
            $folder_name = turkceYap($this->input->post("title"));
            $path = "uploads/$this->viewFolder/";

            if ($gallery_type1 == "image")
            {
                $path = "$path/images/";
            }
            else if ($gallery_type1 == "file")
            {
                $path = "$path/files/";
            }

            if ($gallery_type1 != "video")
            {
                if (!rename("$path/$OldfolderName", "$path/$folder_name")) {
                    $alert = array
                    (
                        "text"  => "Galeri güncellenirken problem meydana geldi (Yetki Hatası)",
                        "type"  => "alert"
                    );
                    $this->session->set_flashdata("alert", $alert);
                    redirect(base_url("backend/galeri"));
                }
            }

            $guncelle = $this->galeri_model->update(
                array (
                    "id" => $id
                ),
                array
                (
                    "title" => $this->security->xss_clean($this->input->post("title")),
                    "folder_name" => $folder_name,
                    "digeradres" => $this->security->xss_clean($this->input->post("farkliadres")),
                    "pagetitle" => $this->security->xss_clean($this->input->post("pagetitle")),
                    "metadesc" => $this->security->xss_clean($this->input->post("metadesc")),
                    "metakey" => $this->security->xss_clean($this->input->post("metakey")),
                )
            );
            //$soneklenenId = $this->db->insert_id();

            if ($guncelle)
            {
                $alert = array
                (
                    "text"  => "Güncelleme işlemi başarı ile gerçekleştirildi ",
                    "type"  => "success"
                );
            }
            else {
                $alert = array
                (
                    "text"  => "Güncelleme esnasında bir hata meydana geldi",
                    "type"  => "error"
                );
            }
            //İşlemin sonucunu sessiona yazma işlemi...
            $this->session->set_flashdata("alert", $alert);
            redirect(base_url("backend/galeri"));
        }

        //formda boşluklar yada şarta uymayan bişey varsa ekrana hata basılır
        else
        {
            $viewData = new stdClass();
            $items = $this->galeri_model->get(
                array (
                    "id"        => $id
                )
            );
            $viewData->viewFolder = $this->viewFolder;
            $viewData->subViewFolder = "update";
            $viewData->form_hatasi = true;
            $viewData->item = $items;
            $viewData->viewTitle = "Galeri";
            $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
        }
    }

    /*KAYIT SİLME İŞLEMLERİ*/
    public function veriSil ($id)
    {

        $gallery = $this->galeri_model->get(
            array(
                "id" => $id
            )
        );

        if ($gallery)
        {
            if ($gallery->gallery_type != "video")
            {
                if ($gallery->gallery_type == "image"){
                    $path = "uploads/$this->viewFolder/images/$gallery->folder_name";
                }
                else if ($gallery->gallery_type == "file")
                    $path = "uploads/$this->viewFolder/files/$gallery->folder_name";

                $delete_folder = rmdir($path);

                if (!$delete_folder)
                {
                    $alert = array
                    (
                        "text"  => "Klasör Silme işleminde bir hata meydana geldi",
                        "type"  => "error"
                    );
                    $this->session->set_flashdata("alert", $alert);
                    redirect(base_url("backend/galeri"));
                    die();
                }
            }
        }
        $delete = $this->galeri_model->delete(
            array(
                "id" => $id
            )
        );

        if ($delete)
        {
            $alert = array
            (
                "text"  => "Silme işlemi başarı ile gerçekleştirildi ",
                "type"  => "success"
            );
        }
        else
        {
            $alert = array
            (
                "text"  => "Silme işleminde bir hata meydana geldi",
                "type"  => "error"
            );
        }

        $this->session->set_flashdata("alert", $alert);
        redirect(base_url("backend/galeri"));
    }

    /*AKTİF PSAİF İŞLEMLERİ*/
    public function veriAktifPasif ($id)
    {
        if ($id)
        {
            $isActive = ($this->input->post("data") == "true") ? 1 : 0;
            $this->galeri_model->update(
                array (
                    "id" => $id,
                ),
                array (
                    "isactive" => $isActive
                )
            );
        }
    }


    /*foto galeri listeleme*/
    public function fotoYukleform ($id)
    {
        $viewData = new stdClass();

        $item = $this->galeri_model->get(
            array (
                "id" => $id
            )
        );

        $viewData->item = $item;

        if ($item->gallery_type == "image")
        {
            $viewData->image_item = $this->image_model->get_all
            (
                array
                (
                    "gallery_id" => $id,
                ), "rank ASC"
            );
        } else if ($item->gallery_type == "file") {
            $viewData->image_item = $this->file_model->get_all
            (
                array
                (
                   "gallery_id" => $id,
                ), "rank ASC"
            );
        } else {
            $viewData->image_item = $this->video_model->get_all
            (
                array
                (
                   "gallery_id" => $id,
                ), "rank ASC"
            );
        }

        
        $viewData->viewFolder       = $this->viewFolder;
        $viewData->subViewFolder    = "image";
        $viewData->viewTitle        = "Galeri";
        $viewData->gallery_type     = $item->gallery_type;
        $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
    }

    public function image_upload ($id, $gallery_type, $folderName)
    {
        $rasgele = sifre_uretici(5);
        $yenidosyaismi = date("YmdHis")."_".$rasgele;
        $file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION); // codeigniter dosya uzantısı alma işlemi
        $newfileName = $yenidosyaismi.".".$file_ext; // uzantı ve isim birleştirilir

        $config["allowed_types"]    = ($gallery_type == "file") ? "pdf|doc|docx|xls|ppt":"jpg|png|jpeg|gif"; // kabul edilecek dosya tipleri
        $config["upload_path"]      = ($gallery_type == "image") ? "uploads/$this->viewFolder/images/$folderName/":"uploads/$this->viewFolder/files/$folderName/"; /*dosyanın yükleneceği dizin*/
        $config["file_name"]        = $newfileName;

        $this->load->library("upload", $config); /*upload libryisini ekliyoruz*/
        $upload = $this->upload->do_upload("file");

        if ($upload)
        {
            $uploaded_file  =   $this->upload->data("file_name"); /*upload edilen resmin ismini alıyoruz*/
            $modelName = ($gallery_type == "image") ? "image_model":"file_model";
            $ekleimage = $this->$modelName->add
            (array
                (
                    "gallery_id"    => $id,
                    "url"           => "{$config["upload_path"]}$uploaded_file",
                    "rank"          => 0,
                    "isactive"      => 1,
                    "addDate"       => date("Y-m-d H:i:s")
                )
            );
        } else {
            $alert = array
                (
                    "text"  => "Bir hata meydana geldi",
                    "type"  => "error"
                );
                $this->session->set_flashdata("alert", $alert);
                redirect(base_url("backend/galeri"));
        }
    }

    /*resim silinmesi*/
    public function fotoSil ($id,$gallery_type,$folderName,$galleryid)
    {

        $modelName = ($gallery_type == "image") ? "image_model":"file_model";

        $this->subViewFolder = "galeri_v";
        $item = $this->$modelName->get
        (
            array(
                "id"    => $id
            )
        );

        if (unlink("$item->url"))
        {
            $delete = $this->$modelName->delete(
                array(
                    "id" => $id
                )
            );

            if ($delete)
            {
                $alert = array
                (
                    "text"  => "Silme işlemi başarı ile gerçekleştirildi ",
                    "type"  => "success"
                );
            }
            else
            {
                $alert = array
                (
                    "text"  => "Silme işleminde bir hata meydana geldi",
                    "type"  => "alert"
                );
            }

            $this->session->set_flashdata("alert", $alert);
            redirect(base_url("backend/galeri/fotoYukleform/$galleryid"));
        }
    }

    /*yenilenen image listesini çalıştıran view*/
    public function refresh_image_list($id, $gallery_type)
    {
        $viewData = new stdClass();

       $viewData->item = $this->galeri_model->get(
            array (
                "id" => $id
            )
        );

        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "image";
        $viewData->viewTitle = "Galeri";

        $modelName = ($gallery_type == "image") ? "image_model" : "file_model";

        $viewData->image_item = $this->$modelName->get_all
        (
            array
            (
                "gallery_id" => $id,
            ),  "rank ASC"
        );
        $viewData->gallery_type = $gallery_type;
        $render_html = $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/render_elements/image_list_v", $viewData, true);
        echo $render_html;
    }





/*VİDEO GALERİ İŞLEMLERİ BAŞLIYOR*/
    
    public function videolist ($id)
    {
        $viewData = new stdClass();

        $gallery = $this->galeri_model->get(
            array(
                "id" => $id
            )
        );

        if ($this->input->post("searchword") == "") {
            $items = $this->video_model->get_all(
                array(
                    "gallery_id" => $id
                ), "rank ASC"
            );
            $viewData->aramaVar = "hayir";
        }
        else {
            $viewData->aramaVar = "evet";
            $items = $this->video_model->get_all_like(
                array(
                    "baslik"    => $this->input->post("searchword")
                ), "rank ASC"
            );
        }
        $viewData->viewFolder = $this->viewFolder;
        $viewData->viewTitle = "Video Galeri";
        $viewData->items = $items;
        $viewData->gallery = $gallery;
        //$viewData->subViewFolder = "list";
        $this->load->view("{$viewData->viewFolder}/video/index", $viewData);
    }

    /*video galeri formu*/
    public function yenivideokayitformu($galleryid)
    {
        $viewData = new stdClass();


        $gallery_info = $this->galeri_model->get(
            array (
                "id" => $galleryid
            )
        );

        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "add";
        $viewData->viewTitle = "Video Galeri";
        $viewData->gallery_info = $gallery_info;

        $this->load->view("{$viewData->viewFolder}/video/{$viewData->subViewFolder}/index", $viewData);
    }

    /*yeni kayıt veritabanı ve form kontrol işlemleri yapılıyor*/
    public function videoformKaydet($galleryid)
    {
        $this->load->library("form_validation");

        //kurallar yazılır
        $this->form_validation->set_rules("url", "Video URL", "required|trim");
        $this->form_validation->set_rules("videoadi", "Video Adı", "required|trim");

        $this->form_validation->set_message(
            array(
                "required" => "<strong>{field}</strong> Alanını Doldurunuz / Seçiniz"
            )
        );
        //form validation çalıştırılı
        $validate = $this->form_validation->run();

        //başarılı ise kayıt işlemi başlar
        if ($validate)
        {

                $ekle = $this->video_model->add (
                    array
                    (
                        "gallery_id"=> $galleryid,
                        "url"  => $this->security->xss_clean($this->input->post("url")),
                        "videoadi"  => $this->security->xss_clean($this->input->post("videoadi")),
                        "pagetitle" => $this->security->xss_clean($this->input->post("pagetitle")),
                        "metadesc"  => $this->security->xss_clean($this->input->post("metadesc")),
                        "metakey"   => $this->security->xss_clean($this->input->post("metakey")),
                        "addDate"   => date("Y-m-d"),
                        "isactive"  => 1
                    )
                );
                //$soneklenenId = $this->db->insert_id();

                if ($ekle)
                {
                    $alert = array
                    (
                        "text"  => "Kayıt İşlemi Başarı ile Yapıldı ",
                        "type"  => "success"
                    );
                }
                else {
                    $alert = array
                    (
                        "text"  => "İşlem Başarısız",
                        "type"  => "error"
                    );
                }
                //İşlemin sonucunu sessiona yazma işlemi...
                $this->session->set_flashdata("alert", $alert);
                redirect(base_url("backend/galeri/videoList/$galleryid"));
        }

        //formda boşluklar yada şarta uymayan bişey varsa ekrana hata basılır
        else
            {
                $viewData = new stdClass();
                $viewData->viewFolder = $this->viewFolder;
                $viewData->subViewFolder = "add";
                $viewData->form_hatasi = true;
                $viewData->viewTitle = "Galeri";
                $this->load->view("{$viewData->viewFolder}/video/{$viewData->subViewFolder}/index", $viewData);
            }
    }

    /*video güncelleme formunu ekrana basan fonksiyon*/
    public function videoguncellemeFormu($id,$galleryid)
    {
        $viewData = new stdClass();


        $gallery_info = $this->galeri_model->get(
            array (
                "id" => $galleryid
            )
        );
        
        $items = $this->video_model->get
        (
            array
            (
                "id"    => $id
            )
        );
        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "update";
        $viewData->item = $items;
        $viewData->viewTitle = "Video Galeri";
        $viewData->gallery_info = $gallery_info;
        $this->load->view("{$viewData->viewFolder}/video/{$viewData->subViewFolder}/index", $viewData);
    }

    public function videoguncelleformKaydet($id,$galleryid)
    {
        $this->load->library("form_validation");

        //kurallar yazılır
        $this->form_validation->set_rules("url", "URL", "required|trim");
        $this->form_validation->set_rules("videoadi", "Video Adı", "required|trim");
        $this->form_validation->set_message(
            array(
                "required" => "<strong>{field}</strong> Alanını Doldurunuz / Seçiniz"
            )
        );
        //form validation çalıştırılı
        $validate = $this->form_validation->run();

        //başarılı ise kayıt işlemi başlar
        if ($validate)
        {
            $guncelle = $this->video_model->update(
                array (
                    "id" => $id
                ),
                array
                (
                    "url"           => $this->security->xss_clean($this->input->post("url")),
                    "videoadi"      => $this->security->xss_clean($this->input->post("videoadi")),
                    "pagetitle"     => $this->security->xss_clean($this->input->post("pagetitle")),
                    "metadesc"      => $this->security->xss_clean($this->input->post("metadesc")),
                    "metakey"       => $this->security->xss_clean($this->input->post("metakey"))
                )
            );
            //$soneklenenId = $this->db->insert_id();

            if ($guncelle)
            {
                $alert = array
                (
                    "text"  => "Güncelleme işlemi başarı ile gerçekleştirildi ",
                    "type"  => "success"
                );
            }
            else {
                $alert = array
                (
                    "text"  => "Güncelleme esnasında bir hata meydana geldi",
                    "type"  => "error"
                );
            }
            //İşlemin sonucunu sessiona yazma işlemi...
            $this->session->set_flashdata("alert", $alert);
            redirect(base_url("backend/galeri/videoList/$galleryid"));
        }

        //formda boşluklar yada şarta uymayan bişey varsa ekrana hata basılır
        else
        {
            $viewData = new stdClass();
            $items = $this->haberler_model->get(
                array (
                    "id"        => $id
                )
            );
            $viewData->viewFolder = $this->viewFolder;
            $viewData->subViewFolder = "update";
            $viewData->form_hatasi = true;
            $viewData->item = $items;
            $viewData->viewTitle = "Haberler";
            $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
        }
    }


    /*VİDEO SİLME İŞLEMLERİ*/
    public function videosil ($id,$galleryid)
    {
        $delete = $this->video_model->delete(
            array(
                "id" => $id
            )
        );

        if ($delete)
        {
            $alert = array
            (
                "text"  => "Silme işlemi başarı ile gerçekleştirildi ",
                "type"  => "success"
            );
        }
        else
        {
            $alert = array
            (
                "text"  => "Silme işleminde bir hata meydana geldi",
                "type"  => "error"
            );
        }

        $this->session->set_flashdata("alert", $alert);
        redirect(base_url("backend/galeri/videoList/$galleryid"));
    }



     /*AKTİF PSAİF İŞLEMLERİ*/
    public function videoaktifpasif ($id)
    {
        if ($id)
        {
            $isActive = ($this->input->post("data") == "true") ? 1 : 0;
            $this->video_model->update(
                array (
                    "id" => $id,
                ),
                array (
                    "isactive" => $isActive
                )
            );
        }
    }

}
