<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Gallery_model extends MY_Model {

  public function __construct() {
    parent::__construct();
  }

  public function getgallery($opts = array()) {
    $keyword = trim($this->input->get_post('keyword', TRUE));
    $keyword = $this->db->escape_str($keyword);
    $status = $this->db->escape_str($this->input->get_post('status', TRUE));

    if (!array_key_exists('condition', $opts) || $opts['condition'] == '') {
      $opts['condition'] = "status !='2' ";
    } else {
      $opts['condition'] = "status !='2' " . $opts['condition'];
    }
    if ($keyword != '') {
      $opts['condition'].= " AND gallery_name like '%" . $keyword . "%'";
    }
    if ($status != '') {
      $opts['condition'].= " AND status='$status' ";
    }
    $opts['order'] = "gallery_id asc ";
    $opts['condition'].= " ";
    $fetch_config = array('condition' => $opts['condition'],
        'order' => $opts['order'],
        'return_type' => "array"
        );
    if (array_key_exists('debug', $opts)) {
      $fetch_config['debug'] = $opts['debug'];
    }
    if (array_key_exists('field', $opts) && $opts['field'] != '') {
      $fetch_config['field'] = $opts['field'];
    }
    if (array_key_exists('limit', $opts) && applyFilter('NUMERIC_GT_ZERO', $opts['limit']) > 0) {
      $fetch_config['limit'] = $opts['limit'];
    }
    if (array_key_exists('offset', $opts) && applyFilter('NUMERIC_WT_ZERO', $opts['offset']) != -1) {
      $fetch_config['start'] = $opts['offset'];
    }
    $result = $this->findAll('wps_gallery as a', $fetch_config);
    return $result;
  }

  public function get_gallery_by_id($id) {
    $id = applyFilter('NUMERIC_GT_ZERO', $id);

    if ($id > 0) {
      $condtion = "status !='2' AND gallery_id=$id";
      $fetch_config = array(
          'condition' => $condtion,
          'debug' => FALSE,
          'return_type' => "array"
      );
      $result = $this->find('wps_gallery', $fetch_config);
      return $result;
    }
  }
  
  public function get_gallery_comment($limit = '10', $offset = '0', $param = array()) {

    $status = @$param['status'];
    $gallery_id = @$param['gallery_id'];
    $orderby = @$param['orderby'];
    $where = @$param['where'];
    $keyword = @$param['keyword'];

    if ($gallery_id != '') {
      $this->db->where("gallery_id", "$gallery_id");
    }
    if ($status != '') {
      $this->db->where("status", "$status");
    }
    if ($where != '') {
      $this->db->where($where);
    }
    if ($orderby != '') {
      $this->db->order_by($orderby);
    } else {
      $this->db->order_by('comment_id  ', 'desc');
    }
    if ($keyword != '') {
      $this->db->where("(comment LIKE '%" . $keyword . "%')");
    }
    // $this->db->group_by("rev.review_id"); 	
    if ($limit != FALSE) {
      $this->db->limit($limit, $offset);
    }
    $this->db->select('SQL_CALC_FOUND_ROWS *', FALSE);
    $this->db->from('wps_gallery_comment ');
    //$this->db->where('status =','1');
    $q = $this->db->get();
    //echo_sql();
    $result = $q->result_array();
    $result = ($limit == '1') ? $result[0] : $result;
    return $result;
  }

}
// model end here