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

class Threadsrot_model extends MY_Model {

    public $table = 'threadsort';
    protected $insert_filed = array('fid','sortid','pic_type','fields', 'template');
    public function __construct()
    {
        parent::__construct();
    }


    public function update_sort($fid, $sortid, $template, $pic_type, $fields)
    {  
        $data['template'] = $template;
        $data['fid'] = $fid;
        $data['sortid'] = $sortid;
        $data['pic_type'] = $pic_type;

        $data['fields'] = !empty($fields) ? serialize($fields) : '';


        $this->db->where('fid', $fid);
        $this->db->where('sortid', $sortid);
        $result = $this->db->get($this->table)->row_array();

        if(empty($result)){
            $this->insert($data);
        }else{
            $this->db->where('id', $result['id']);
            $this->db->update($this->table, $data);
        }    

    }

    public function delete_by_fid($fid){
        $this->db->where('fid', $fid);
        $this->db->delete($this->table);
    }

    public function get_by_fid($fid){
        $this->db->where('fid', $fid);

        $result = $this->db->get($this->table)->result_array();
        $data = array();

        foreach($result as $row){
            $row['fields'] = unserialize($row['fields']);
            $data[$row['sortid']] = $row;
        }

        return $data;
    }

}