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

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

	public function __construct()
    {
        parent::__construct();
        $this->viewFolder = "ayarlar_v";
        $this->load->model("ayarlar_model");


        if (!get_active_user()){
            redirect(base_url("panel/login"));
        }
    }

	public function index ()
    {
        $viewData = new stdClass();
        $items = $this->ayarlar_model->get
        (
            array
            (
                "id"    => 1
            )
        );
        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "update";
        $viewData->item = $items;
        $viewData->viewTitle = "Ayarlar";
        $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->ayarlar_model->get
        (
            array
            (
                "id"    => $id
            )
        );
        $viewData->viewFolder = $this->viewFolder;
        $viewData->subViewFolder = "update";
        $viewData->item = $items;
        $viewData->viewTitle = "Ayarlar";
        $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
    }

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

        //kurallar yazılır
        $this->form_validation->set_rules("companyname", "Şirket Adı", "required|trim");
        $this->form_validation->set_rules("eposta", "E-Posta Adresi", "required|trim");
        $this->form_validation->set_rules("pagetitle", "Sayfa Başlığı", "required|trim");
        $this->form_validation->set_rules("metadesc", "Meta Description", "required|trim");
        $this->form_validation->set_rules("metakey", "Meta Keywords", "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) {

            if ($_FILES["file"]["name"] <> "")
            {
                $file_ext       = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION); // codeigniter dosya uzantısı alma işlemi
                $newfileName    = turkceYap($this->input->post("companyname")).".".$file_ext; // uzantı ve isim birleştirilir

                $config["allowed_types"]    = "jpg|png|jpeg"; // kabul edilecek dosya tipleri
                $config["upload_path"]      = "uploads/$this->viewFolder/"; /*dosyanın yükleneceği dizin*/
                $config["file_name"]        = $newfileName;

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

                $upload = $this->upload->do_upload("file");
                $uploaded_file  =   $this->upload->data("file_name"); /*upload edilen resmin ismini alıyoruz*/
            } 
            else {$uploaded_file = $this->security->xss_clean($this->input->post("eskiresim"));}


                $guncelle = $this->ayarlar_model->update 
                (
                    array (
                        "id" => 1
                    ),

                    array
                    (
                        "company_name"  => $this->security->xss_clean($this->input->post("companyname")),
                        "adress"        => $this->security->xss_clean($this->input->post("adres")),
                        "about_us"      => $this->security->xss_clean($this->input->post("hakkimizda")),
                        "mission"       => $this->security->xss_clean($this->input->post("mission")),
                        "vision"        => $this->security->xss_clean($this->input->post("vision")),
                        "logo"          => $uploaded_file,
                        "phone1"        => $this->security->xss_clean($this->input->post("telefon_one")),
                        "phone2"        => $this->security->xss_clean($this->input->post("telefon_two")),
                        "phone3"        => $this->security->xss_clean($this->input->post("mobile")),
                        "faks"          => $this->security->xss_clean($this->input->post("faks")),
                        "email"         => $this->security->xss_clean($this->input->post("eposta")),
                        "twitter"       => $this->security->xss_clean($this->input->post("twitter")),
                        "facebook"      => $this->security->xss_clean($this->input->post("facebook")),
                        "instagram"     => $this->security->xss_clean($this->input->post("instagram")),
                        "youtube"       => $this->security->xss_clean($this->input->post("youtube")),
                        "pinterest"     => $this->security->xss_clean($this->input->post("pinterest")),
                        "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")),
                        "updateDate"    => date("Y-m-d H:i:s")
                    )
                );
                //$soneklenenId = $this->db->insert_id();

                if ($guncelle)
                {
                    $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/ayarlar"));
        }

        //formda boşluklar yada şarta uymayan bişey varsa ekrana hata basılır
        else
            {
                $viewData = new stdClass();

                 $items = $this->ayarlar_model->get(
                    array
                    (
                        "id"    => 1
                    )
                );
                $viewData->item = $items;
                $viewData->viewFolder = $this->viewFolder;
                $viewData->subViewFolder = "update";
                $viewData->form_hatasi = true;
                $viewData->viewTitle = "Ayarlar";
                $this->load->view("{$viewData->viewFolder}/{$viewData->subViewFolder}/index", $viewData);
            }
    }
}
