<?php

namespace Modules\Downloads\Models;

use CodeIgniter\Model;

class DownloadsModel extends Model
{

  protected $table = 'downloads';
    protected $primaryKey = 'id';

    protected $allowedFields = ['title', 'alias', 'published_date', 'photo','file', 'file_size', 'file_type', 'author', 'for_sale', 'content','excerpt', 'category_id', 'ordering', 'status', 'access', 'hits', 'tags', 'urls', 'metadesc', 'metakey', 'created_by', 'updated_at', 'updated_by'];

    protected $returnType     = 'array';
    protected $useTimestamps  = true;
    protected $useSoftDeletes = true;
    
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    //Get all Post Categories
    public function getAll()
    {
      $builder = $this->db->table('downloads as d');
      $builder->select('d.*, dc.name as category_name, first_name, last_name, ');
      $builder->join('downloads_category as dc', 'dc.id=d.category_id');
      $builder->join('users as u', 'u.id=d.created_by');
      $builder->where('d.deleted_at', null);
      $builder->orderBy('id', 'DESC');
		  $result = $builder->get()->getResult();
		  return $result;
    }
    public function getSinglePost($id)
    {
      $builder = $this->db->table('downloads as d');
      $builder->select('d.*, pc.name as category_name, first_name, last_name, ');
      $builder->where('id', $id);
      $builder->join('downloads_category as dc', 'dc.id=d.category_id', 'left');
      $builder->join('users as u', 'u.id=d.created_by');
		  $result = $builder->get()->getRow();
		  return $result;
    }
    //Published Cat Posts
    public function getPublishedPosts($id)
    {
      $builder = $this->db->table('downloads');
      $builder->where('id', $id);
      $builder->where('status', 'Published');
      echo $builder->countAll();
    }

    //All Downloads for Homepage
    public function forHomePage()
    {
      $builder = $this->db->table('downloads as d');
      $builder->select('d.*, dc.name as category_name, first_name, last_name, ');
      $builder->join('downloads_category as dc', 'dc.id=d.category_id', 'left');
      $builder->join('users as u', 'u.id=d.created_by');
      $builder->where('d.status', 'Published');
      $builder->orderBy('d.published_date', 'DESC');
	  $result = $builder->get()->getResult();
      return $result;
    }
    
    //All Downloads for Homepage
    public function getDownloadsPerCategoryPerAccess($access, $category)
    {
      $builder = $this->db->table('downloads as d');
      $builder->select('d.*, dc.name as category_name, dc.alias as category_alias, first_name, last_name, ');
      $builder->join('downloads_category as dc', 'dc.id=d.category_id', 'left');
      $builder->join('users as u', 'u.id=d.created_by', 'left');
      $builder->where('d.status', 'Published');
      $builder->where('d.access', $access);
      $builder->where('d.category_id', $category);
      $builder->orWhere('dc.alias', $category);
      $builder->orderBy('d.published_date', 'DESC');
	  $result = $builder->get()->getResult();
      return $result;
    }
}
