<?php

class portfolioModel
    extends baseModel
{
    public function savePictures($data)
    {
        $portfolio = ["user_id" => $_SESSION['user_id']];

        if (!empty($data['files'])) {
            require_once 'models/uploadsModel.php';
            $uploadsModel = new uploadsModel();
            foreach ($data['files'] as $file) {
                $file = $uploadsModel->select($file);
                if (empty($file))
                    continue;

                $portfolio['upload_id'] = $file['id'];
                $portfolio['image']     = $file['file'];
                $this->db->insert("portfolio_entry", $portfolio);

                $uploadsModel->update($file['id'], ['portfolio_id' => 1]);
            }
        }
    }

    public function getList($cond = [])
    {
        $this->db->order_by("date_created ASC");
        $entries = $this->db->select("portfolio_entry");
        return $entries;
    }

    function deleteFile($file_id) {
        $this->db->where('id=?', $file_id);
        $file = $this->db->select('uploads')[0];

        if ($file) {
            unlink($file['file']);
        }

        $this->db->where("upload_id = ?", $file_id);
        $this->db->delete("portfolio_entry");

        $this->db->where('id=?', $file_id);
        $this->db->delete('uploads');
    }
}
