<?php

namespace Modules\Downloads\Models;

use CodeIgniter\Model;

class DownloadsTagsModel extends Model
{

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

    protected $allowedFields = ['tag_id', 'download_id', 'updated_at'];

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

    public function getAll()
    {
        $builder = $this->db->table('downloads_tags');
        $builder->where('deleted_at', null);
        $results = $builder->get()->getResult();
        return $results;
    }

    //Get Tags matching the Download
    public function getDownloadTags($download)
    {
        $builder = $this->db->table('downloads_tags as dt');
        $builder->select('dt.*, t.name as tag, t.alias as tag_alias,');
        $builder->join('tags as t', 't.id=dt.tag_id');
        $builder->join('downloads as d', 'd.id=dt.download_id');
        $builder->where('dt.deleted_at', null);
        $builder->where('dt.download_id', $download);
        $builder->orWhere('d.alias', $download);
        $results = $builder->get()->getResult();
        return $results;
    }

    //Display TAGS within Downloads
    public function getTagsInDownloads()
    {
      $builder = $this->db->table('downloads_tags as dt');
      $builder->select('dt.*, t.name as tag, t.alias as tag_alias,');
      $builder->join('tags as t', 't.id=dt.tag_id');
      $builder->groupBy('dt.tag_id');
      $builder->orderBy('RAND()');
      $builder->limit(25);
      $results = $builder->get()->getResult();
      return $results;
    }

    //Get single Tag attached to Downloads
    public function getSingleTagsInDownloads($tag)
    {
      $builder = $this->db->table('downloads_tags as dt');
      $builder->select('p.*,dt.download_id, dt.tag_id,t.name as tag_name, pc.name as category, pc.alias as category_alias, pmc.master_category_id, pmc.name as master_category, pmc.alias as master_category_alias');
      $builder->join('tags as t', 't.id=pt.tag_id');
      $builder->join('downloads as d', 'd.id=dt.download_id');
      $builder->join('downloads_category as dc', 'dc.id=d.category_id');
      $builder->join('downloads_category as dmc', 'dmc.id=dc.master_category_id');
      $builder->where('dt.tag_id', $tag);
      $builder->orWhere('d.alias', $tag);
      $results = $builder->get()->getRow();
      return $results;
    }

    public function getSingleTagsAllDownloads($tag)
    {
      $builder = $this->db->table('downloads_tags as dt');
      $builder->select('d.*,dt.download_id, dt.tag_id,t.name as tag_name, dc.name as category, dc.alias as category_alias, dmc.master_category_id, dmc.name as master_category, dmc.alias as master_category_alias');
      $builder->join('tags as t', 't.id=dt.tag_id');
      $builder->join('downloads as d', 'd.id=dt.download_id');
      $builder->join('downloads_category as dc', 'dc.id=d.category_id');
      $builder->join('downloads_category as dmc', 'dmc.id=dc.master_category_id');
      $builder->where('dt.tag_id', $tag);
      $builder->orWhere('d.alias', $tag);
      $results = $builder->get()->getResult();
      return $results;
    }
}