<?php

class Gallery extends Public_Controller {

  public function __construct() {
    parent::__construct();
    //$this->load->helper(array('gallery/gallery'));
    $this->load->model(array('gallery/gallery_model'));
  }

  public function index() {
    if (!isset($this->meta_info['entity_id'])) {
      redirect(base_url(''));
    }
    $gallery_id = (int) $this->meta_info['entity_id'];
    $have_sub_gal = get_db_field_value('wps_gallery', 'parent_id', "WHERE parent_id = '$gallery_id' ");
    if ($gallery_id > 0) {
      if ($have_sub_gal > 0) {
        $this->gallery_listing($gallery_id);
      } else {
        //echo "pro"; exit;
        $this->gallery_listing($gallery_id);
      }
    } else {
      //echo "cat"; exit;
      $this->gallery_listing($gallery_id);
    }
  }

  public function gallery_listing($cid) {
    $ord = "ORDER BY gallery_name ASC";
    $data['active_page'] = 'Gallery Listing';
    $data['page_title'] = "Gallery";
    $data['heading_title'] = 'Gallery Lists';
    $data['unq_section'] = "Gallery";

    if ($cid > 0) {
      $ord = "ORDER BY gallery_name DESC";
      $qry = $this->db->query("select * from wps_gallery where gallery_id ='" . $cid . "'")->row();
      $data['active_page'] = $qry->gallery_name;
    }

    $record_per_page = (int) $this->input->post('per_page');
    if (array_key_exists('entity_id', $this->meta_info) && $this->meta_info['entity_id'] > 0) {
      $parent_segment = (int) $this->meta_info['entity_id'];
    } else {
      $parent_segment = (int) $this->uri->segment(3);
    }
    $page_segment = find_paging_segment();
    $config['per_page'] = ( $record_per_page > 0 ) ? $record_per_page : $this->config->item('per_page');
    $offset = (int) $this->uri->segment($page_segment, 0);
    $parent_id = ( $parent_segment > 0 ) ? $parent_segment : '0';
    $base_url = ( $parent_segment > 0 ) ? "gallery/gallery_listing/$parent_id/pg/" : "gallery/gallery_listing/pg/";
    
    //category
    $condtion_array_gallery = array(
        'field' => "*,( SELECT COUNT(gallery_id) FROM wps_gallery AS b
        WHERE b.parent_id=a.gallery_id ) AS total_subcategories",
        'condition' => "AND parent_id = '0' AND status='1' ",
        //'limit' => $config['per_page'],
        //'offset' => $offset,
        'debug' => FALSE
    );
    $res_gallery = $this->gallery_model->getgallery($condtion_array_gallery);
    $data['gallery'] = $res_gallery;
    //end here
    
    $condtion_array = array(
        'field' => "*,( SELECT COUNT(gallery_id) FROM wps_gallery AS b
        WHERE b.parent_id=a.gallery_id ) AS total_subcategories",
        'condition' => ($parent_id > 0)?"AND parent_id = '$parent_id' AND status='1'":"AND parent_id != '0' AND status='1'",
        'limit' => $config['per_page'],
        'offset' => $offset,
        'debug' => FALSE
    );
    $res_array = $this->gallery_model->getgallery($condtion_array);
    
    $data['total_rows'] = $config['total_rows'] = $this->gallery_model->total_rec_found;
    $data['page_links'] = front_pagination("$base_url", $config['total_rows'], $config['per_page'], $page_segment);
    $data['res'] = $res_array;
    //trace($res_array);
    $this->load->view('gallery/view_gallery', $data);
  }
  
  
  public function getGalleryDetails(){
    $galleryId = $this->input->get_post('galleryId');
    $cparam = array('gallery_id' => $galleryId, "status" => 1);
    $comm = $this->gallery_model->get_gallery_comment(FALSE, FALSE, $cparam);
    $imageName = $this->input->get_post('galleryImg');
    $gimage = get_image('gallery',$imageName, '', '', 'R');
    $viewText = '';
    $viewText .= '
          <div class="user-info">
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 pleft pright popimg">
              <img src="'.$gimage.'" class="responsive-img"/>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 comb commentBox">
              <div class="comch">'.count($comm).' Comments</div>
              <textarea  name="comment" type="text" class="comcomment" id="comment" value="" placeholder="Add a comment..."  /></textarea>
              <input type="hidden" name="gallery_id" id="gallery_id" value="'.$galleryId.'" />
              <button class="compbt" id="postComment">POST</button>
              <div class="popcom">';
              foreach($comm as $comment){
                $viewText .= '<div class="com1">
                  <strong>'.$comment['mem_name'].'</strong> <span class="gr1">'.getDateFormat($comment['post_date'],3).'</span><br />
                <p>'.$comment['comment'].'</p>    
                <br />
                </div>';
              }
              $viewText .= '<div class="glin1"></div>                
              </div>
              <div class="bsocial mobpb" style="margin-top:8px; margin-bottom:8px;">
                <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5a40d07cdf99e7aa"></script>
                <div class="addthis_inline_share_toolbox_z8un"></div>
              </div>
            </div>              
          </div>
          <div class="clear"></div>';
    echo $viewText;
    
  }
  
  public function postComment() {
    $galleryId = $this->input->get_post('galleryId');
    $comment = $this->input->get_post('comments');    
    if($galleryId > 0){
      $posted_data = array(
          'gallery_id' => $galleryId,
          'comment' => $comment,
          'post_date' => $this->config->item('config.date.time'),
          'status' => '1'
      );
      $this->gallery_model->safe_insert('wps_gallery_comment', $posted_data, FALSE);
      echo 'success';
    }else{
      echo 'error';
    }    
  }

}